1 use std::process::Command;
3 use std::path::{PathBuf, Path};
5 use build_helper::output;
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 env::var_os("LLVM_LINK_SHARED").is_some() {
11 ("dylib", "--link-shared")
13 ("static", "--link-static")
18 if env::var_os("RUST_CHECK").is_some() {
19 // If we're just running `check`, there's no need for LLVM to be built.
20 println!("cargo:rerun-if-env-changed=RUST_CHECK");
24 build_helper::restore_library_path();
26 let target = env::var("TARGET").expect("TARGET was not set");
27 let llvm_config = env::var_os("LLVM_CONFIG")
30 if let Some(dir) = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
31 let to_test = dir.parent()
36 .join("llvm/bin/llvm-config");
37 if Command::new(&to_test).output().is_ok() {
41 PathBuf::from("llvm-config")
44 println!("cargo:rerun-if-changed={}", llvm_config.display());
45 println!("cargo:rerun-if-env-changed=LLVM_CONFIG");
47 // Test whether we're cross-compiling LLVM. This is a pretty rare case
48 // currently where we're producing an LLVM for a different platform than
49 // what this build script is currently running on.
51 // In that case, there's no guarantee that we can actually run the target,
52 // so the build system works around this by giving us the LLVM_CONFIG for
53 // the host platform. This only really works if the host LLVM and target
54 // LLVM are compiled the same way, but for us that's typically the case.
56 // We *want* detect this cross compiling situation by asking llvm-config
57 // what it's host-target is. If that's not the TARGET, then we're cross
58 // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
59 // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
60 // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
61 // tricks us into thinking we're doing a cross build when we aren't, so
64 // In any case, if we're cross compiling, this generally just means that we
65 // can't trust all the output of llvm-config becaues it might be targeted
66 // for the host rather than the target. As a result a bunch of blocks below
67 // are gated on `if !is_crossed`
68 let target = env::var("TARGET").expect("TARGET was not set");
69 let host = env::var("HOST").expect("HOST was not set");
70 let is_crossed = target != host;
72 let mut optional_components =
73 vec!["x86", "arm", "aarch64", "amdgpu", "mips", "powerpc",
74 "systemz", "jsbackend", "webassembly", "msp430", "sparc", "nvptx",
77 let mut version_cmd = Command::new(&llvm_config);
78 version_cmd.arg("--version");
79 let version_output = output(&mut version_cmd);
80 let mut parts = version_output.split('.').take(2)
81 .filter_map(|s| s.parse::<u32>().ok());
83 if let (Some(major), Some(minor)) = (parts.next(), parts.next()) {
90 optional_components.push("riscv");
93 let required_components = &["ipo",
101 let components = output(Command::new(&llvm_config).arg("--components"));
102 let mut components = components.split_whitespace().collect::<Vec<_>>();
103 components.retain(|c| optional_components.contains(c) || required_components.contains(c));
105 for component in required_components {
106 if !components.contains(component) {
107 panic!("require llvm component {} but wasn't found", component);
111 for component in components.iter() {
112 println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
116 println!("cargo:rustc-cfg=llvm_has_msp430_asm_parser");
119 // Link in our own LLVM shims, compiled with the same flags as LLVM
120 let mut cmd = Command::new(&llvm_config);
121 cmd.arg("--cxxflags");
122 let cxxflags = output(&mut cmd);
123 let mut cfg = cc::Build::new();
125 for flag in cxxflags.split_whitespace() {
126 // Ignore flags like `-m64` when we're doing a cross build
127 if is_crossed && flag.starts_with("-m") {
131 if flag.starts_with("-flto") {
135 // -Wdate-time is not supported by the netbsd cross compiler
136 if is_crossed && target.contains("netbsd") && flag.contains("date-time") {
143 for component in &components {
144 let mut flag = String::from("LLVM_COMPONENT_");
145 flag.push_str(&component.to_uppercase());
146 cfg.define(&flag, None);
149 println!("cargo:rerun-if-changed-env=LLVM_RUSTLLVM");
150 if env::var_os("LLVM_RUSTLLVM").is_some() {
151 cfg.define("LLVM_RUSTLLVM", None);
154 build_helper::rerun_if_changed_anything_in_dir(Path::new("../rustllvm"));
155 cfg.file("../rustllvm/PassWrapper.cpp")
156 .file("../rustllvm/RustWrapper.cpp")
157 .file("../rustllvm/ArchiveWrapper.cpp")
158 .file("../rustllvm/Linker.cpp")
160 .cpp_link_stdlib(None) // we handle this below
161 .compile("rustllvm");
163 let (llvm_kind, llvm_link_arg) = detect_llvm_link();
165 // Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
166 // we don't pick up system libs because unfortunately they're for the host
167 // of llvm-config, not the target that we're attempting to link.
168 let mut cmd = Command::new(&llvm_config);
169 cmd.arg(llvm_link_arg).arg("--libs");
172 cmd.arg("--system-libs");
174 cmd.args(&components);
176 for lib in output(&mut cmd).split_whitespace() {
177 let name = if lib.starts_with("-l") {
179 } else if lib.starts_with("-") {
181 } else if Path::new(lib).exists() {
182 // On MSVC llvm-config will print the full name to libraries, but
183 // we're only interested in the name part
184 let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
185 name.trim_end_matches(".lib")
186 } else if lib.ends_with(".lib") {
187 // Some MSVC libraries just come up with `.lib` tacked on, so chop
189 lib.trim_end_matches(".lib")
194 // Don't need or want this library, but LLVM's CMake build system
195 // doesn't provide a way to disable it, so filter it here even though we
196 // may or may not have built it. We don't reference anything from this
197 // library and it otherwise may just pull in extra dependencies on
198 // libedit which we don't want
199 if name == "LLVMLineEditor" {
203 let kind = if name.starts_with("LLVM") {
208 println!("cargo:rustc-link-lib={}={}", kind, name);
213 // If we're a cross-compile of LLVM then unfortunately we can't trust these
214 // ldflags (largely where all the LLVM libs are located). Currently just
215 // hack around this by replacing the host triple with the target and pray
216 // that those -L directories are the same!
217 let mut cmd = Command::new(&llvm_config);
218 cmd.arg(llvm_link_arg).arg("--ldflags");
219 for lib in output(&mut cmd).split_whitespace() {
220 if lib.starts_with("-LIBPATH:") {
221 println!("cargo:rustc-link-search=native={}", &lib[9..]);
222 } else if is_crossed {
223 if lib.starts_with("-L") {
224 println!("cargo:rustc-link-search=native={}",
225 lib[2..].replace(&host, &target));
227 } else if lib.starts_with("-l") {
228 println!("cargo:rustc-link-lib={}", &lib[2..]);
229 } else if lib.starts_with("-L") {
230 println!("cargo:rustc-link-search=native={}", &lib[2..]);
234 // Some LLVM linker flags (-L and -l) may be needed even when linking
235 // librustc_llvm, for example when using static libc++, we may need to
236 // manually specify the library search path and -ldl -lpthread as link
238 let llvm_linker_flags = env::var_os("LLVM_LINKER_FLAGS");
239 if let Some(s) = llvm_linker_flags {
240 for lib in s.into_string().unwrap().split_whitespace() {
241 if lib.starts_with("-l") {
242 println!("cargo:rustc-link-lib={}", &lib[2..]);
243 } else if lib.starts_with("-L") {
244 println!("cargo:rustc-link-search=native={}", &lib[2..]);
249 let llvm_static_stdcpp = env::var_os("LLVM_STATIC_STDCPP");
250 let llvm_use_libcxx = env::var_os("LLVM_USE_LIBCXX");
252 let stdcppname = if target.contains("openbsd") {
253 // llvm-config on OpenBSD doesn't mention stdlib=libc++
255 } else if target.contains("freebsd") {
257 } else if target.contains("darwin") {
259 } else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
260 // NetBSD uses a separate library when relocation is required
262 } else if llvm_use_libcxx.is_some() {
268 // C++ runtime library
269 if !target.contains("msvc") {
270 if let Some(s) = llvm_static_stdcpp {
271 assert!(!cxxflags.contains("stdlib=libc++"));
272 let path = PathBuf::from(s);
273 println!("cargo:rustc-link-search=native={}",
274 path.parent().unwrap().display());
275 println!("cargo:rustc-link-lib=static={}", stdcppname);
276 } else if cxxflags.contains("stdlib=libc++") {
277 println!("cargo:rustc-link-lib=c++");
279 println!("cargo:rustc-link-lib={}", stdcppname);
283 // LLVM requires symbols from this library, but apparently they're not printed
284 // during llvm-config?
285 if target.contains("windows-gnu") {
286 println!("cargo:rustc-link-lib=static-nobundle=gcc_s");
287 println!("cargo:rustc-link-lib=static-nobundle=pthread");
288 println!("cargo:rustc-link-lib=dylib=uuid");