2 extern crate build_helper;
4 use std::process::Command;
6 use std::path::{PathBuf, Path};
8 use build_helper::output;
10 fn detect_llvm_link() -> (&'static str, &'static str) {
11 // Force the link mode we want, preferring static by default, but
12 // possibly overridden by `configure --enable-llvm-link-shared`.
13 if env::var_os("LLVM_LINK_SHARED").is_some() {
14 ("dylib", "--link-shared")
16 ("static", "--link-static")
21 if env::var_os("RUST_CHECK").is_some() {
22 // If we're just running `check`, there's no need for LLVM to be built.
23 println!("cargo:rerun-if-env-changed=RUST_CHECK");
27 let target = env::var("TARGET").expect("TARGET was not set");
28 let llvm_config = env::var_os("LLVM_CONFIG")
31 if let Some(dir) = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
32 let to_test = dir.parent()
37 .join("llvm/bin/llvm-config");
38 if Command::new(&to_test).output().is_ok() {
42 PathBuf::from("llvm-config")
45 println!("cargo:rerun-if-changed={}", llvm_config.display());
46 println!("cargo:rerun-if-env-changed=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 it's 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 becaues 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 mut optional_components =
74 vec!["x86", "arm", "aarch64", "amdgpu", "mips", "powerpc",
75 "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("hexagon");
94 optional_components.push("riscv");
97 // FIXME: surely we don't need all these components, right? Stuff like mcjit
98 // or interpreter the compiler itself never uses.
99 let required_components = &["ipo",
109 let components = output(Command::new(&llvm_config).arg("--components"));
110 let mut components = components.split_whitespace().collect::<Vec<_>>();
111 components.retain(|c| optional_components.contains(c) || required_components.contains(c));
113 for component in required_components {
114 if !components.contains(component) {
115 panic!("require llvm component {} but wasn't found", component);
119 for component in components.iter() {
120 println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
123 // Link in our own LLVM shims, compiled with the same flags as LLVM
124 let mut cmd = Command::new(&llvm_config);
125 cmd.arg("--cxxflags");
126 let cxxflags = output(&mut cmd);
127 let mut cfg = cc::Build::new();
129 for flag in cxxflags.split_whitespace() {
130 // Ignore flags like `-m64` when we're doing a cross build
131 if is_crossed && flag.starts_with("-m") {
135 if flag.starts_with("-flto") {
139 // -Wdate-time is not supported by the netbsd cross compiler
140 if is_crossed && target.contains("netbsd") && flag.contains("date-time") {
147 for component in &components {
148 let mut flag = String::from("LLVM_COMPONENT_");
149 flag.push_str(&component.to_uppercase());
150 cfg.define(&flag, None);
153 println!("cargo:rerun-if-changed-env=LLVM_RUSTLLVM");
154 if env::var_os("LLVM_RUSTLLVM").is_some() {
155 cfg.define("LLVM_RUSTLLVM", None);
158 build_helper::rerun_if_changed_anything_in_dir(Path::new("../rustllvm"));
159 cfg.file("../rustllvm/PassWrapper.cpp")
160 .file("../rustllvm/RustWrapper.cpp")
161 .file("../rustllvm/ArchiveWrapper.cpp")
162 .file("../rustllvm/Linker.cpp")
164 .cpp_link_stdlib(None) // we handle this below
165 .compile("rustllvm");
167 let (llvm_kind, llvm_link_arg) = detect_llvm_link();
169 // Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
170 // we don't pick up system libs because unfortunately they're for the host
171 // of llvm-config, not the target that we're attempting to link.
172 let mut cmd = Command::new(&llvm_config);
173 cmd.arg(llvm_link_arg).arg("--libs");
176 cmd.arg("--system-libs");
178 cmd.args(&components);
180 for lib in output(&mut cmd).split_whitespace() {
181 let name = if lib.starts_with("-l") {
183 } else if lib.starts_with("-") {
185 } else if Path::new(lib).exists() {
186 // On MSVC llvm-config will print the full name to libraries, but
187 // we're only interested in the name part
188 let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
189 name.trim_end_matches(".lib")
190 } else if lib.ends_with(".lib") {
191 // Some MSVC libraries just come up with `.lib` tacked on, so chop
193 lib.trim_end_matches(".lib")
198 // Don't need or want this library, but LLVM's CMake build system
199 // doesn't provide a way to disable it, so filter it here even though we
200 // may or may not have built it. We don't reference anything from this
201 // library and it otherwise may just pull in extra dependencies on
202 // libedit which we don't want
203 if name == "LLVMLineEditor" {
207 let kind = if name.starts_with("LLVM") {
212 println!("cargo:rustc-link-lib={}={}", kind, name);
217 // If we're a cross-compile of LLVM then unfortunately we can't trust these
218 // ldflags (largely where all the LLVM libs are located). Currently just
219 // hack around this by replacing the host triple with the target and pray
220 // that those -L directories are the same!
221 let mut cmd = Command::new(&llvm_config);
222 cmd.arg(llvm_link_arg).arg("--ldflags");
223 for lib in output(&mut cmd).split_whitespace() {
224 if lib.starts_with("-LIBPATH:") {
225 println!("cargo:rustc-link-search=native={}", &lib[9..]);
226 } else if is_crossed {
227 if lib.starts_with("-L") {
228 println!("cargo:rustc-link-search=native={}",
229 lib[2..].replace(&host, &target));
231 } else if lib.starts_with("-l") {
232 println!("cargo:rustc-link-lib={}", &lib[2..]);
233 } else if lib.starts_with("-L") {
234 println!("cargo:rustc-link-search=native={}", &lib[2..]);
238 let llvm_static_stdcpp = env::var_os("LLVM_STATIC_STDCPP");
239 let llvm_use_libcxx = env::var_os("LLVM_USE_LIBCXX");
241 let stdcppname = if target.contains("openbsd") {
242 // llvm-config on OpenBSD doesn't mention stdlib=libc++
244 } else if target.contains("freebsd") {
246 } else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
247 // NetBSD uses a separate library when relocation is required
249 } else if llvm_use_libcxx.is_some() {
255 // C++ runtime library
256 if !target.contains("msvc") {
257 if let Some(s) = llvm_static_stdcpp {
258 assert!(!cxxflags.contains("stdlib=libc++"));
259 let path = PathBuf::from(s);
260 println!("cargo:rustc-link-search=native={}",
261 path.parent().unwrap().display());
262 println!("cargo:rustc-link-lib=static={}", stdcppname);
263 } else if cxxflags.contains("stdlib=libc++") {
264 println!("cargo:rustc-link-lib=c++");
266 println!("cargo:rustc-link-lib={}", stdcppname);
270 // LLVM requires symbols from this library, but apparently they're not printed
271 // during llvm-config?
272 if target.contains("windows-gnu") {
273 println!("cargo:rustc-link-lib=static-nobundle=gcc_s");
274 println!("cargo:rustc-link-lib=static-nobundle=pthread");
275 println!("cargo:rustc-link-lib=dylib=uuid");