1 use std::env;
2 use std::ffi::{OsStr, OsString};
3 use std::fmt::Display;
4 use std::path::{Path, PathBuf};
5 use std::process::{Command, Stdio};
6
7 const OPTIONAL_COMPONENTS: &[&str] = &[
8 "x86",
9 "arm",
10 "aarch64",
11 "amdgpu",
12 "avr",
13 "loongarch",
14 "m68k",
15 "mips",
16 "powerpc",
17 "systemz",
18 "jsbackend",
19 "webassembly",
20 "msp430",
21 "sparc",
22 "nvptx",
23 "hexagon",
24 "riscv",
25 "bpf",
26 ];
27
28 const REQUIRED_COMPONENTS: &[&str] =
29 &["ipo", "bitreader", "bitwriter", "linker", "asmparser", "lto", "coverage", "instrumentation", "parts"];
30
detect_llvm_link() -> (&'static str, &'static str)31 fn detect_llvm_link() -> (&'static str, &'static str) {
32 // Force the link mode we want, preferring static by default, but
33 // possibly overridden by `configure --enable-llvm-link-shared`.
34 if tracked_env_var_os("LLVM_LINK_SHARED").is_some() {
35 ("dylib", "--link-shared")
36 } else {
37 ("static", "--link-static")
38 }
39 }
40
41 // Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
42 // break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM
43 // shared library, which means that when our freshly built llvm-config goes to load it's
44 // associated LLVM, it actually loads the compiler's LLVM. In particular when building the first
45 // compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from
46 // the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't
47 // perfect -- we might actually want to see something from Cargo's added library paths -- but
48 // for now it works.
restore_library_path()49 fn restore_library_path() {
50 let key = tracked_env_var_os("REAL_LIBRARY_PATH_VAR").expect("REAL_LIBRARY_PATH_VAR");
51 if let Some(env) = tracked_env_var_os("REAL_LIBRARY_PATH") {
52 env::set_var(&key, &env);
53 } else {
54 env::remove_var(&key);
55 }
56 }
57
58 /// Reads an environment variable and adds it to dependencies.
59 /// Supposed to be used for all variables except those set for build scripts by cargo
60 /// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts>
tracked_env_var_os<K: AsRef<OsStr> + Display>(key: K) -> Option<OsString>61 fn tracked_env_var_os<K: AsRef<OsStr> + Display>(key: K) -> Option<OsString> {
62 println!("cargo:rerun-if-env-changed={key}");
63 env::var_os(key)
64 }
65
rerun_if_changed_anything_in_dir(dir: &Path)66 fn rerun_if_changed_anything_in_dir(dir: &Path) {
67 let mut stack = dir
68 .read_dir()
69 .unwrap()
70 .map(|e| e.unwrap())
71 .filter(|e| &*e.file_name() != ".git")
72 .collect::<Vec<_>>();
73 while let Some(entry) = stack.pop() {
74 let path = entry.path();
75 if entry.file_type().unwrap().is_dir() {
76 stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
77 } else {
78 println!("cargo:rerun-if-changed={}", path.display());
79 }
80 }
81 }
82
83 #[track_caller]
output(cmd: &mut Command) -> String84 fn output(cmd: &mut Command) -> String {
85 let output = match cmd.stderr(Stdio::inherit()).output() {
86 Ok(status) => status,
87 Err(e) => {
88 println!("\n\nfailed to execute command: {cmd:?}\nerror: {e}\n\n");
89 std::process::exit(1);
90 }
91 };
92 if !output.status.success() {
93 panic!(
94 "command did not execute successfully: {:?}\n\
95 expected success, got: {}",
96 cmd, output.status
97 );
98 }
99 String::from_utf8(output.stdout).unwrap()
100 }
101
main()102 fn main() {
103 for component in REQUIRED_COMPONENTS.iter().chain(OPTIONAL_COMPONENTS.iter()) {
104 println!("cargo:rustc-check-cfg=values(llvm_component,\"{component}\")");
105 }
106
107 if tracked_env_var_os("RUST_CHECK").is_some() {
108 // If we're just running `check`, there's no need for LLVM to be built.
109 return;
110 }
111
112 restore_library_path();
113
114 let target = env::var("TARGET").expect("TARGET was not set");
115 let llvm_config =
116 tracked_env_var_os("LLVM_CONFIG").map(|x| Some(PathBuf::from(x))).unwrap_or_else(|| {
117 if let Some(dir) = tracked_env_var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
118 let to_test = dir
119 .parent()
120 .unwrap()
121 .parent()
122 .unwrap()
123 .join(&target)
124 .join("llvm/bin/llvm-config");
125 if Command::new(&to_test).output().is_ok() {
126 return Some(to_test);
127 }
128 }
129 None
130 });
131
132 if let Some(llvm_config) = &llvm_config {
133 println!("cargo:rerun-if-changed={}", llvm_config.display());
134 }
135 let llvm_config = llvm_config.unwrap_or_else(|| PathBuf::from("llvm-config"));
136
137 // Test whether we're cross-compiling LLVM. This is a pretty rare case
138 // currently where we're producing an LLVM for a different platform than
139 // what this build script is currently running on.
140 //
141 // In that case, there's no guarantee that we can actually run the target,
142 // so the build system works around this by giving us the LLVM_CONFIG for
143 // the host platform. This only really works if the host LLVM and target
144 // LLVM are compiled the same way, but for us that's typically the case.
145 //
146 // We *want* detect this cross compiling situation by asking llvm-config
147 // what its host-target is. If that's not the TARGET, then we're cross
148 // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
149 // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
150 // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
151 // tricks us into thinking we're doing a cross build when we aren't, so
152 // havoc ensues.
153 //
154 // In any case, if we're cross compiling, this generally just means that we
155 // can't trust all the output of llvm-config because it might be targeted
156 // for the host rather than the target. As a result a bunch of blocks below
157 // are gated on `if !is_crossed`
158 let target = env::var("TARGET").expect("TARGET was not set");
159 let host = env::var("HOST").expect("HOST was not set");
160 let is_crossed = target != host;
161
162 let components = output(Command::new(&llvm_config).arg("--components"));
163 let mut components = components.split_whitespace().collect::<Vec<_>>();
164 components.retain(|c| OPTIONAL_COMPONENTS.contains(c) || REQUIRED_COMPONENTS.contains(c));
165
166 for component in REQUIRED_COMPONENTS {
167 if !components.contains(component) {
168 panic!("require llvm component {component} but wasn't found");
169 }
170 }
171
172 for component in components.iter() {
173 println!("cargo:rustc-cfg=llvm_component=\"{component}\"");
174 }
175
176 // Link in our own LLVM shims, compiled with the same flags as LLVM
177 let mut cmd = Command::new(&llvm_config);
178 cmd.arg("--cxxflags");
179 let cxxflags = output(&mut cmd);
180 let mut cfg = cc::Build::new();
181 cfg.warnings(false);
182 for flag in cxxflags.split_whitespace() {
183 // Ignore flags like `-m64` when we're doing a cross build
184 if is_crossed && flag.starts_with("-m") {
185 continue;
186 }
187
188 if flag.starts_with("-flto") {
189 continue;
190 }
191
192 // -Wdate-time is not supported by the netbsd cross compiler
193 if is_crossed && target.contains("netbsd") && flag.contains("date-time") {
194 continue;
195 }
196
197 // Include path contains host directory, replace it with target
198 if is_crossed && flag.starts_with("-I") {
199 cfg.flag(&flag.replace(&host, &target));
200 continue;
201 }
202
203 cfg.flag(flag);
204 }
205
206 for component in &components {
207 let mut flag = String::from("LLVM_COMPONENT_");
208 flag.push_str(&component.to_uppercase());
209 cfg.define(&flag, None);
210 }
211
212 if tracked_env_var_os("LLVM_RUSTLLVM").is_some() {
213 cfg.define("LLVM_RUSTLLVM", None);
214 }
215
216 if tracked_env_var_os("LLVM_NDEBUG").is_some() {
217 cfg.define("NDEBUG", None);
218 cfg.debug(false);
219 }
220
221 rerun_if_changed_anything_in_dir(Path::new("llvm-wrapper"));
222 cfg.file("llvm-wrapper/PassWrapper.cpp")
223 .file("llvm-wrapper/RustWrapper.cpp")
224 .file("llvm-wrapper/ArchiveWrapper.cpp")
225 .file("llvm-wrapper/CoverageMappingWrapper.cpp")
226 .file("llvm-wrapper/SymbolWrapper.cpp")
227 .file("llvm-wrapper/Linker.cpp")
228 .cpp(true)
229 .cpp_link_stdlib(None) // we handle this below
230 .compile("llvm-wrapper");
231
232 let (llvm_kind, llvm_link_arg) = detect_llvm_link();
233
234 // Link in all LLVM libraries, if we're using the "wrong" llvm-config then
235 // we don't pick up system libs because unfortunately they're for the host
236 // of llvm-config, not the target that we're attempting to link.
237 let mut cmd = Command::new(&llvm_config);
238 cmd.arg(llvm_link_arg).arg("--libs");
239
240 if !is_crossed {
241 cmd.arg("--system-libs");
242 }
243
244 if (target.starts_with("arm") && !target.contains("freebsd"))
245 || target.starts_with("mips-")
246 || target.starts_with("mipsel-")
247 || target.starts_with("powerpc-")
248 {
249 // 32-bit targets need to link libatomic.
250 println!("cargo:rustc-link-lib=atomic");
251 } else if target.contains("windows-gnu") {
252 println!("cargo:rustc-link-lib=shell32");
253 println!("cargo:rustc-link-lib=uuid");
254 } else if target.contains("netbsd") || target.contains("haiku") || target.contains("darwin") {
255 println!("cargo:rustc-link-lib=z");
256 }
257 cmd.args(&components);
258
259 for lib in output(&mut cmd).split_whitespace() {
260 let name = if let Some(stripped) = lib.strip_prefix("-l") {
261 stripped
262 } else if let Some(stripped) = lib.strip_prefix('-') {
263 stripped
264 } else if Path::new(lib).exists() {
265 // On MSVC llvm-config will print the full name to libraries, but
266 // we're only interested in the name part
267 let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
268 name.trim_end_matches(".lib")
269 } else if lib.ends_with(".lib") {
270 // Some MSVC libraries just come up with `.lib` tacked on, so chop
271 // that off
272 lib.trim_end_matches(".lib")
273 } else {
274 continue;
275 };
276
277 // Don't need or want this library, but LLVM's CMake build system
278 // doesn't provide a way to disable it, so filter it here even though we
279 // may or may not have built it. We don't reference anything from this
280 // library and it otherwise may just pull in extra dependencies on
281 // libedit which we don't want
282 if name == "LLVMLineEditor" {
283 continue;
284 }
285
286 let kind = if name.starts_with("LLVM") { llvm_kind } else { "dylib" };
287 println!("cargo:rustc-link-lib={kind}={name}");
288 }
289
290 // LLVM ldflags
291 //
292 // If we're a cross-compile of LLVM then unfortunately we can't trust these
293 // ldflags (largely where all the LLVM libs are located). Currently just
294 // hack around this by replacing the host triple with the target and pray
295 // that those -L directories are the same!
296 let mut cmd = Command::new(&llvm_config);
297 cmd.arg(llvm_link_arg).arg("--ldflags");
298 for lib in output(&mut cmd).split_whitespace() {
299 if is_crossed {
300 if let Some(stripped) = lib.strip_prefix("-LIBPATH:") {
301 println!("cargo:rustc-link-search=native={}", stripped.replace(&host, &target));
302 } else if let Some(stripped) = lib.strip_prefix("-L") {
303 println!("cargo:rustc-link-search=native={}", stripped.replace(&host, &target));
304 }
305 } else if let Some(stripped) = lib.strip_prefix("-LIBPATH:") {
306 println!("cargo:rustc-link-search=native={stripped}");
307 } else if let Some(stripped) = lib.strip_prefix("-l") {
308 println!("cargo:rustc-link-lib={stripped}");
309 } else if let Some(stripped) = lib.strip_prefix("-L") {
310 println!("cargo:rustc-link-search=native={stripped}");
311 }
312 }
313
314 // Some LLVM linker flags (-L and -l) may be needed even when linking
315 // rustc_llvm, for example when using static libc++, we may need to
316 // manually specify the library search path and -ldl -lpthread as link
317 // dependencies.
318 let llvm_linker_flags = tracked_env_var_os("LLVM_LINKER_FLAGS");
319 if let Some(s) = llvm_linker_flags {
320 for lib in s.into_string().unwrap().split_whitespace() {
321 if let Some(stripped) = lib.strip_prefix("-l") {
322 println!("cargo:rustc-link-lib={stripped}");
323 } else if let Some(stripped) = lib.strip_prefix("-L") {
324 println!("cargo:rustc-link-search=native={stripped}");
325 }
326 }
327 }
328
329 let llvm_static_stdcpp = tracked_env_var_os("LLVM_STATIC_STDCPP");
330 let llvm_use_libcxx = tracked_env_var_os("LLVM_USE_LIBCXX");
331
332 let stdcppname = if target.contains("openbsd") {
333 if target.contains("sparc64") { "estdc++" } else { "c++" }
334 } else if target.contains("darwin")
335 || target.contains("freebsd")
336 || target.contains("windows-gnullvm")
337 || target.contains("aix")
338 {
339 "c++"
340 } else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
341 // NetBSD uses a separate library when relocation is required
342 "stdc++_p"
343 } else if llvm_use_libcxx.is_some() {
344 "c++"
345 } else {
346 "stdc++"
347 };
348
349 // RISC-V GCC erroneously requires libatomic for sub-word
350 // atomic operations. Some BSD uses Clang as its system
351 // compiler and provides no libatomic in its base system so
352 // does not want this.
353 if target.starts_with("riscv") && !target.contains("freebsd") && !target.contains("openbsd") {
354 println!("cargo:rustc-link-lib=atomic");
355 }
356
357 // C++ runtime library
358 if !target.contains("msvc") {
359 if let Some(s) = llvm_static_stdcpp {
360 assert!(!cxxflags.contains("stdlib=libc++"));
361 let path = PathBuf::from(s);
362 println!("cargo:rustc-link-search=native={}", path.parent().unwrap().display());
363 if target.contains("windows") {
364 println!("cargo:rustc-link-lib=static:-bundle={stdcppname}");
365 } else {
366 println!("cargo:rustc-link-lib=static={stdcppname}");
367 }
368 } else if cxxflags.contains("stdlib=libc++") {
369 println!("cargo:rustc-link-lib=c++");
370 } else {
371 println!("cargo:rustc-link-lib={stdcppname}");
372 }
373 }
374
375 // Libstdc++ depends on pthread which Rust doesn't link on MinGW
376 // since nothing else requires it.
377 if target.ends_with("windows-gnu") {
378 println!("cargo:rustc-link-lib=static:-bundle=pthread");
379 }
380 }
381