revive_llvm_builder/
llvm_path.rs

1//! The revive LLVM builder constants.
2
3use std::path::PathBuf;
4use std::sync::OnceLock;
5
6pub static DIRECTORY_LLVM_TARGET: OnceLock<PathBuf> = OnceLock::new();
7
8/// The LLVM path resolver.
9pub struct LLVMPath {}
10
11impl LLVMPath {
12    /// The LLVM source directory.
13    pub const DIRECTORY_LLVM_SOURCE: &'static str = "./llvm/";
14
15    /// The LLVM host source directory for stage 1 of multistage MUSL and Emscripten builds.
16    ///
17    /// We use upstream LLVM anyways; re-use the same tree for host and target builds.
18    pub const DIRECTORY_LLVM_HOST_SOURCE: &'static str = Self::DIRECTORY_LLVM_SOURCE;
19
20    /// The Emscripten SDK source directory.
21    pub const DIRECTORY_EMSDK_SOURCE: &'static str = "./emsdk/";
22
23    /// Returns the path to the `llvm` stage 1 host LLVM source module directory.
24    pub fn llvm_host_module_llvm() -> anyhow::Result<PathBuf> {
25        let mut path = PathBuf::from(Self::DIRECTORY_LLVM_HOST_SOURCE);
26        path.push("llvm");
27        crate::utils::absolute_path(path).inspect(|absolute_path| {
28            log::debug!(
29                "llvm stage 1 host llvm source module: {}",
30                absolute_path.display()
31            )
32        })
33    }
34
35    /// Returns the path to the `llvm` LLVM source module directory.
36    pub fn llvm_module_llvm() -> anyhow::Result<PathBuf> {
37        let mut path = PathBuf::from(Self::DIRECTORY_LLVM_SOURCE);
38        path.push("llvm");
39        crate::utils::absolute_path(path)
40            .inspect(|absolute_path| log::debug!("llvm source module: {}", absolute_path.display()))
41    }
42
43    /// Returns the path to the MUSL source.
44    pub fn musl_source(name: &str) -> anyhow::Result<PathBuf> {
45        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
46        path.push(name);
47        crate::utils::absolute_path(path)
48            .inspect(|absolute_path| log::debug!("musl source: {}", absolute_path.display()))
49    }
50
51    /// Returns the path to the MUSL build directory.
52    pub fn musl_build(source_directory: &str) -> anyhow::Result<PathBuf> {
53        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
54        path.push(source_directory);
55        path.push("build");
56        crate::utils::absolute_path(path)
57            .inspect(|absolute_path| log::debug!("musl build: '{}'", absolute_path.display()))
58    }
59
60    /// Returns the path to the LLVM CRT build directory.
61    pub fn llvm_build_crt() -> anyhow::Result<PathBuf> {
62        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
63        path.push("build-crt");
64        crate::utils::absolute_path(path)
65            .inspect(|absolute_path| log::debug!("llvm build crt: {}", absolute_path.display()))
66    }
67
68    /// Returns the path to the LLVM host build directory.
69    pub fn llvm_build_host() -> anyhow::Result<PathBuf> {
70        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
71        path.push("build-host");
72        crate::utils::absolute_path(path)
73            .inspect(|absolute_path| log::debug!("llvm build host: {}", absolute_path.display()))
74    }
75
76    /// Returns the path to the LLVM final build directory.
77    pub fn llvm_build_final() -> anyhow::Result<PathBuf> {
78        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
79        path.push("build-final");
80        crate::utils::absolute_path(path)
81            .inspect(|absolute_path| log::debug!("llvm build final: {}", absolute_path.display()))
82    }
83
84    /// Returns the path to the MUSL target directory.
85    pub fn musl_target() -> anyhow::Result<PathBuf> {
86        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
87        path.push("target-musl");
88        crate::utils::absolute_path(path)
89            .inspect(|absolute_path| log::debug!("musl target: {}", absolute_path.display()))
90    }
91
92    /// Returns the path to the LLVM CRT target directory.
93    pub fn llvm_target_crt() -> anyhow::Result<PathBuf> {
94        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
95        path.push("target-crt");
96        crate::utils::absolute_path(path)
97            .inspect(|absolute_path| log::debug!("llvm crt target: {}", absolute_path.display()))
98    }
99
100    /// Returns the path to the LLVM host target directory.
101    pub fn llvm_target_host() -> anyhow::Result<PathBuf> {
102        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
103        path.push("target-host");
104        crate::utils::absolute_path(path)
105            .inspect(|absolute_path| log::debug!("llvm host target: {}", absolute_path.display()))
106    }
107
108    /// Returns the path to the LLVM final target directory.
109    pub fn llvm_target_final() -> anyhow::Result<PathBuf> {
110        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
111        path.push("target-final");
112        crate::utils::absolute_path(path)
113            .inspect(|absolute_path| log::debug!("llvm final target: {}", absolute_path.display()))
114    }
115
116    /// Returns the path to the LLVM compiler builtin target directory.
117    pub fn llvm_module_compiler_rt() -> anyhow::Result<PathBuf> {
118        let mut path = PathBuf::from(Self::DIRECTORY_LLVM_SOURCE);
119        path.push("compiler-rt");
120        crate::utils::absolute_path(path).inspect(|absolute_path| {
121            log::debug!("compiler-rt source dir: {}", absolute_path.display())
122        })
123    }
124
125    /// Returns the path to the LLVM compiler-rt target directory.
126    pub fn llvm_target_compiler_rt() -> anyhow::Result<PathBuf> {
127        Self::llvm_target_final()
128    }
129
130    /// Returns the path to the LLVM compiler-rt build directory.
131    pub fn llvm_build_compiler_rt() -> anyhow::Result<PathBuf> {
132        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
133        path.push("build-compiler-rt");
134        crate::utils::absolute_path(path).inspect(|absolute_path| {
135            log::debug!("llvm compiler-rt build: {}", absolute_path.display())
136        })
137    }
138
139    /// Returns the path to the LLVM target final bin path.
140    ///
141    pub fn llvm_target_final_bin(
142        target_env: crate::target_env::TargetEnv,
143    ) -> anyhow::Result<PathBuf> {
144        let mut path = Self::llvm_target_final()?;
145        path.push("bin");
146        path.push(format!("{target_env}"));
147        crate::utils::absolute_path(path).inspect(|absolute_path| {
148            log::debug!("llvm target final bin: {}", absolute_path.display())
149        })
150    }
151}