2 use std::path::{Path, PathBuf};
3 use std::process::Command;
5 use build_helper::{output, tracked_env_var_os};
7 fn detect_llvm_link() -> (&'static str, &'static str) {
8 // Force the link mode we want, preferring static by default, but
9 // possibly overridden by `configure --enable-llvm-link-shared`.
10 if tracked_env_var_os("LLVM_LINK_SHARED").is_some() {
11 ("dylib", "--link-shared")
13 ("static", "--link-static")
18 if tracked_env_var_os("RUST_CHECK").is_some() {
19 // If we're just running `check`, there's no need for LLVM to be built.
23 build_helper::restore_library_path();
25 let target = env::var("TARGET").expect("TARGET was not set");
27 tracked_env_var_os("LLVM_CONFIG").map(|x| Some(PathBuf::from(x))).unwrap_or_else(|| {
28 if let Some(dir) = tracked_env_var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
35 .join("llvm/bin/llvm-config");
36 if Command::new(&to_test).output().is_ok() {
43 if let Some(llvm_config) = &llvm_config {
44 println!("cargo:rerun-if-changed={}", llvm_config.display());
46 let llvm_config = llvm_config.unwrap_or_else(|| PathBuf::from("llvm-config"));
48 // Test whether we're cross-compiling LLVM. This is a pretty rare case
49 // currently where we're producing an LLVM for a different platform than
50 // what this build script is currently running on.
52 // In that case, there's no guarantee that we can actually run the target,
53 // so the build system works around this by giving us the LLVM_CONFIG for
54 // the host platform. This only really works if the host LLVM and target
55 // LLVM are compiled the same way, but for us that's typically the case.
57 // We *want* detect this cross compiling situation by asking llvm-config
58 // what its host-target is. If that's not the TARGET, then we're cross
59 // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
60 // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
61 // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
62 // tricks us into thinking we're doing a cross build when we aren't, so
65 // In any case, if we're cross compiling, this generally just means that we
66 // can't trust all the output of llvm-config because it might be targeted
67 // for the host rather than the target. As a result a bunch of blocks below
68 // are gated on `if !is_crossed`
69 let target = env::var("TARGET").expect("TARGET was not set");
70 let host = env::var("HOST").expect("HOST was not set");
71 let is_crossed = target != host;
73 let optional_components = &[
91 let mut version_cmd = Command::new(&llvm_config);
92 version_cmd.arg("--version");
93 let version_output = output(&mut version_cmd);
94 let mut parts = version_output.split('.').take(2).filter_map(|s| s.parse::<u32>().ok());
95 let (major, _minor) = if let (Some(major), Some(minor)) = (parts.next(), parts.next()) {
101 let required_components = &[
112 let components = output(Command::new(&llvm_config).arg("--components"));
113 let mut components = components.split_whitespace().collect::<Vec<_>>();
114 components.retain(|c| optional_components.contains(c) || required_components.contains(c));
116 for component in required_components {
117 if !components.contains(component) {
118 panic!("require llvm component {} but wasn't found", component);
122 for component in components.iter() {
123 println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
127 println!("cargo:rustc-cfg=llvm_has_msp430_asm_parser");
130 // Link in our own LLVM shims, compiled with the same flags as LLVM
131 let mut cmd = Command::new(&llvm_config);
132 cmd.arg("--cxxflags");
133 let cxxflags = output(&mut cmd);
134 let mut cfg = cc::Build::new();
136 for flag in cxxflags.split_whitespace() {
137 // Ignore flags like `-m64` when we're doing a cross build
138 if is_crossed && flag.starts_with("-m") {
142 if flag.starts_with("-flto") {
146 // -Wdate-time is not supported by the netbsd cross compiler
147 if is_crossed && target.contains("netbsd") && flag.contains("date-time") {
151 // Include path contains host directory, replace it with target
152 if is_crossed && flag.starts_with("-I") {
153 cfg.flag(&flag.replace(&host, &target));
160 for component in &components {
161 let mut flag = String::from("LLVM_COMPONENT_");
162 flag.push_str(&component.to_uppercase());
163 cfg.define(&flag, None);
166 if tracked_env_var_os("LLVM_RUSTLLVM").is_some() {
167 cfg.define("LLVM_RUSTLLVM", None);
170 if tracked_env_var_os("LLVM_NDEBUG").is_some() {
171 cfg.define("NDEBUG", None);
175 build_helper::rerun_if_changed_anything_in_dir(Path::new("llvm-wrapper"));
176 cfg.file("llvm-wrapper/PassWrapper.cpp")
177 .file("llvm-wrapper/RustWrapper.cpp")
178 .file("llvm-wrapper/ArchiveWrapper.cpp")
179 .file("llvm-wrapper/CoverageMappingWrapper.cpp")
180 .file("llvm-wrapper/Linker.cpp")
182 .cpp_link_stdlib(None) // we handle this below
183 .compile("llvm-wrapper");
185 let (llvm_kind, llvm_link_arg) = detect_llvm_link();
187 // Link in all LLVM libraries, if we're using the "wrong" llvm-config then
188 // we don't pick up system libs because unfortunately they're for the host
189 // of llvm-config, not the target that we're attempting to link.
190 let mut cmd = Command::new(&llvm_config);
191 cmd.arg(llvm_link_arg).arg("--libs");
194 cmd.arg("--system-libs");
195 } else if target.contains("windows-gnu") {
196 println!("cargo:rustc-link-lib=shell32");
197 println!("cargo:rustc-link-lib=uuid");
198 } else if target.contains("netbsd") || target.contains("haiku") {
199 println!("cargo:rustc-link-lib=z");
201 cmd.args(&components);
203 for lib in output(&mut cmd).split_whitespace() {
204 let name = if let Some(stripped) = lib.strip_prefix("-l") {
206 } else if let Some(stripped) = lib.strip_prefix('-') {
208 } else if Path::new(lib).exists() {
209 // On MSVC llvm-config will print the full name to libraries, but
210 // we're only interested in the name part
211 let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
212 name.trim_end_matches(".lib")
213 } else if lib.ends_with(".lib") {
214 // Some MSVC libraries just come up with `.lib` tacked on, so chop
216 lib.trim_end_matches(".lib")
221 // Don't need or want this library, but LLVM's CMake build system
222 // doesn't provide a way to disable it, so filter it here even though we
223 // may or may not have built it. We don't reference anything from this
224 // library and it otherwise may just pull in extra dependencies on
225 // libedit which we don't want
226 if name == "LLVMLineEditor" {
230 let kind = if name.starts_with("LLVM") { llvm_kind } else { "dylib" };
231 println!("cargo:rustc-link-lib={}={}", kind, name);
236 // If we're a cross-compile of LLVM then unfortunately we can't trust these
237 // ldflags (largely where all the LLVM libs are located). Currently just
238 // hack around this by replacing the host triple with the target and pray
239 // that those -L directories are the same!
240 let mut cmd = Command::new(&llvm_config);
241 cmd.arg(llvm_link_arg).arg("--ldflags");
242 for lib in output(&mut cmd).split_whitespace() {
244 if let Some(stripped) = lib.strip_prefix("-LIBPATH:") {
245 println!("cargo:rustc-link-search=native={}", stripped.replace(&host, &target));
246 } else if let Some(stripped) = lib.strip_prefix("-L") {
247 println!("cargo:rustc-link-search=native={}", stripped.replace(&host, &target));
249 } else if let Some(stripped) = lib.strip_prefix("-LIBPATH:") {
250 println!("cargo:rustc-link-search=native={}", stripped);
251 } else if let Some(stripped) = lib.strip_prefix("-l") {
252 println!("cargo:rustc-link-lib={}", stripped);
253 } else if let Some(stripped) = lib.strip_prefix("-L") {
254 println!("cargo:rustc-link-search=native={}", stripped);
258 // Some LLVM linker flags (-L and -l) may be needed even when linking
259 // rustc_llvm, for example when using static libc++, we may need to
260 // manually specify the library search path and -ldl -lpthread as link
262 let llvm_linker_flags = tracked_env_var_os("LLVM_LINKER_FLAGS");
263 if let Some(s) = llvm_linker_flags {
264 for lib in s.into_string().unwrap().split_whitespace() {
265 if let Some(stripped) = lib.strip_prefix("-l") {
266 println!("cargo:rustc-link-lib={}", stripped);
267 } else if let Some(stripped) = lib.strip_prefix("-L") {
268 println!("cargo:rustc-link-search=native={}", stripped);
273 let llvm_static_stdcpp = tracked_env_var_os("LLVM_STATIC_STDCPP");
274 let llvm_use_libcxx = tracked_env_var_os("LLVM_USE_LIBCXX");
276 let stdcppname = if target.contains("openbsd") {
277 if target.contains("sparc64") { "estdc++" } else { "c++" }
278 } else if target.contains("freebsd") {
280 } else if target.contains("darwin") {
282 } else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
283 // NetBSD uses a separate library when relocation is required
285 } else if llvm_use_libcxx.is_some() {
291 // RISC-V requires libatomic for sub-word atomic operations
292 if target.starts_with("riscv") {
293 println!("cargo:rustc-link-lib=atomic");
296 // C++ runtime library
297 if !target.contains("msvc") {
298 if let Some(s) = llvm_static_stdcpp {
299 assert!(!cxxflags.contains("stdlib=libc++"));
300 let path = PathBuf::from(s);
301 println!("cargo:rustc-link-search=native={}", path.parent().unwrap().display());
302 if target.contains("windows") {
303 println!("cargo:rustc-link-lib=static-nobundle={}", stdcppname);
305 println!("cargo:rustc-link-lib=static={}", stdcppname);
307 } else if cxxflags.contains("stdlib=libc++") {
308 println!("cargo:rustc-link-lib=c++");
310 println!("cargo:rustc-link-lib={}", stdcppname);
314 // Libstdc++ depends on pthread which Rust doesn't link on MinGW
315 // since nothing else requires it.
316 if target.contains("windows-gnu") {
317 println!("cargo:rustc-link-lib=static-nobundle=pthread");