Skip to main content

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    /// The directory containing patches to apply to the LLVM source.
24    pub const DIRECTORY_PATCHES: &'static str = "./patches/llvm/";
25
26    /// Returns the path to the `llvm` stage 1 host LLVM source module directory.
27    pub fn llvm_host_module_llvm() -> anyhow::Result<PathBuf> {
28        let mut path = PathBuf::from(Self::DIRECTORY_LLVM_HOST_SOURCE);
29        path.push("llvm");
30        crate::utils::absolute_path(path).inspect(|absolute_path| {
31            log::debug!(
32                "llvm stage 1 host llvm source module: {}",
33                absolute_path.display()
34            )
35        })
36    }
37
38    /// Returns the path to the `llvm` LLVM source module directory.
39    pub fn llvm_module_llvm() -> anyhow::Result<PathBuf> {
40        let mut path = PathBuf::from(Self::DIRECTORY_LLVM_SOURCE);
41        path.push("llvm");
42        crate::utils::absolute_path(path)
43            .inspect(|absolute_path| log::debug!("llvm source module: {}", absolute_path.display()))
44    }
45
46    /// Returns the path to the MUSL source.
47    pub fn musl_source(name: &str) -> anyhow::Result<PathBuf> {
48        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
49        path.push(name);
50        crate::utils::absolute_path(path)
51            .inspect(|absolute_path| log::debug!("musl source: {}", absolute_path.display()))
52    }
53
54    /// Returns the path to the MUSL build directory.
55    pub fn musl_build(source_directory: &str) -> anyhow::Result<PathBuf> {
56        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
57        path.push(source_directory);
58        path.push("build");
59        crate::utils::absolute_path(path)
60            .inspect(|absolute_path| log::debug!("musl build: '{}'", absolute_path.display()))
61    }
62
63    /// Returns the path to the LLVM CRT build directory.
64    pub fn llvm_build_crt() -> anyhow::Result<PathBuf> {
65        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
66        path.push("build-crt");
67        crate::utils::absolute_path(path)
68            .inspect(|absolute_path| log::debug!("llvm build crt: {}", absolute_path.display()))
69    }
70
71    /// Returns the path to the LLVM host build directory.
72    pub fn llvm_build_host() -> anyhow::Result<PathBuf> {
73        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
74        path.push("build-host");
75        crate::utils::absolute_path(path)
76            .inspect(|absolute_path| log::debug!("llvm build host: {}", absolute_path.display()))
77    }
78
79    /// Returns the path to the LLVM final build directory.
80    pub fn llvm_build_final() -> anyhow::Result<PathBuf> {
81        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
82        path.push("build-final");
83        crate::utils::absolute_path(path)
84            .inspect(|absolute_path| log::debug!("llvm build final: {}", absolute_path.display()))
85    }
86
87    /// Returns the path to the MUSL target directory.
88    pub fn musl_target() -> anyhow::Result<PathBuf> {
89        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
90        path.push("target-musl");
91        crate::utils::absolute_path(path)
92            .inspect(|absolute_path| log::debug!("musl target: {}", absolute_path.display()))
93    }
94
95    /// Returns the path to the LLVM CRT target directory.
96    pub fn llvm_target_crt() -> anyhow::Result<PathBuf> {
97        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
98        path.push("target-crt");
99        crate::utils::absolute_path(path)
100            .inspect(|absolute_path| log::debug!("llvm crt target: {}", absolute_path.display()))
101    }
102
103    /// Returns the path to the LLVM host target directory.
104    pub fn llvm_target_host() -> anyhow::Result<PathBuf> {
105        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
106        path.push("target-host");
107        crate::utils::absolute_path(path)
108            .inspect(|absolute_path| log::debug!("llvm host target: {}", absolute_path.display()))
109    }
110
111    /// Returns the path to the LLVM final target directory.
112    pub fn llvm_target_final() -> anyhow::Result<PathBuf> {
113        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
114        path.push("target-final");
115        crate::utils::absolute_path(path)
116            .inspect(|absolute_path| log::debug!("llvm final target: {}", absolute_path.display()))
117    }
118
119    /// Returns the path to the LLVM compiler builtin target directory.
120    pub fn llvm_module_compiler_rt() -> anyhow::Result<PathBuf> {
121        let mut path = PathBuf::from(Self::DIRECTORY_LLVM_SOURCE);
122        path.push("compiler-rt");
123        crate::utils::absolute_path(path).inspect(|absolute_path| {
124            log::debug!("compiler-rt source dir: {}", absolute_path.display())
125        })
126    }
127
128    /// Returns the path to the LLVM compiler-rt target directory.
129    pub fn llvm_target_compiler_rt() -> anyhow::Result<PathBuf> {
130        Self::llvm_target_final()
131    }
132
133    /// Returns the path to the LLVM compiler-rt build directory.
134    pub fn llvm_build_compiler_rt() -> anyhow::Result<PathBuf> {
135        let mut path = PathBuf::from(DIRECTORY_LLVM_TARGET.get().unwrap());
136        path.push("build-compiler-rt");
137        crate::utils::absolute_path(path).inspect(|absolute_path| {
138            log::debug!("llvm compiler-rt build: {}", absolute_path.display())
139        })
140    }
141
142    /// Returns the path to the LLVM target final bin path.
143    ///
144    pub fn llvm_target_final_bin(
145        target_env: crate::target_env::TargetEnv,
146    ) -> anyhow::Result<PathBuf> {
147        let mut path = Self::llvm_target_final()?;
148        path.push("bin");
149        path.push(format!("{target_env}"));
150        crate::utils::absolute_path(path).inspect(|absolute_path| {
151            log::debug!("llvm target final bin: {}", absolute_path.display())
152        })
153    }
154}