1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 extern crate build_helper;
14 use std::process::Command;
16 use std::path::{PathBuf, Path};
18 use build_helper::output;
20 fn detect_llvm_link(llvm_config: &Path) -> (&'static str, Option<&'static str>) {
21 let mut version_cmd = Command::new(llvm_config);
22 version_cmd.arg("--version");
23 let version_output = output(&mut version_cmd);
24 let mut parts = version_output.split('.').take(2)
25 .filter_map(|s| s.parse::<u32>().ok());
26 if let (Some(major), Some(minor)) = (parts.next(), parts.next()) {
27 if major > 3 || (major == 3 && minor >= 9) {
28 // Force the link mode we want, preferring static by default, but
29 // possibly overridden by `configure --enable-llvm-link-shared`.
30 if env::var_os("LLVM_LINK_SHARED").is_some() {
31 return ("dylib", Some("--link-shared"));
33 return ("static", Some("--link-static"));
35 } else if major == 3 && minor == 8 {
36 // Find out LLVM's default linking mode.
37 let mut mode_cmd = Command::new(llvm_config);
38 mode_cmd.arg("--shared-mode");
39 if output(&mut mode_cmd).trim() == "shared" {
40 return ("dylib", None);
42 return ("static", None);
50 println!("cargo:rustc-cfg=cargobuild");
52 let target = env::var("TARGET").expect("TARGET was not set");
53 let llvm_config = env::var_os("LLVM_CONFIG")
56 if let Some(dir) = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
57 let to_test = dir.parent()
62 .join("llvm/bin/llvm-config");
63 if Command::new(&to_test).output().is_ok() {
67 PathBuf::from("llvm-config")
70 println!("cargo:rerun-if-changed={}", llvm_config.display());
72 // Test whether we're cross-compiling LLVM. This is a pretty rare case
73 // currently where we're producing an LLVM for a different platform than
74 // what this build script is currently running on.
76 // In that case, there's no guarantee that we can actually run the target,
77 // so the build system works around this by giving us the LLVM_CONFIG for
78 // the host platform. This only really works if the host LLVM and target
79 // LLVM are compiled the same way, but for us that's typically the case.
81 // We *want* detect this cross compiling situation by asking llvm-config
82 // what it's host-target is. If that's not the TARGET, then we're cross
83 // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
84 // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
85 // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
86 // tricks us into thinking we're doing a cross build when we aren't, so
89 // In any case, if we're cross compiling, this generally just means that we
90 // can't trust all the output of llvm-config becaues it might be targeted
91 // for the host rather than the target. As a result a bunch of blocks below
92 // are gated on `if !is_crossed`
93 let target = env::var("TARGET").expect("TARGET was not set");
94 let host = env::var("HOST").expect("HOST was not set");
95 let is_crossed = target != host;
97 let optional_components =
98 ["x86", "arm", "aarch64", "mips", "powerpc", "pnacl", "systemz", "jsbackend", "msp430"];
100 // FIXME: surely we don't need all these components, right? Stuff like mcjit
101 // or interpreter the compiler itself never uses.
102 let required_components = &["ipo",
111 let components = output(Command::new(&llvm_config).arg("--components"));
112 let mut components = components.split_whitespace().collect::<Vec<_>>();
113 components.retain(|c| optional_components.contains(c) || required_components.contains(c));
115 for component in required_components {
116 if !components.contains(component) {
117 panic!("require llvm component {} but wasn't found", component);
121 for component in components.iter() {
122 println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
125 // Link in our own LLVM shims, compiled with the same flags as LLVM
126 let mut cmd = Command::new(&llvm_config);
127 cmd.arg("--cxxflags");
128 let cxxflags = output(&mut cmd);
129 let mut cfg = gcc::Config::new();
130 for flag in cxxflags.split_whitespace() {
131 // Ignore flags like `-m64` when we're doing a cross build
132 if is_crossed && flag.starts_with("-m") {
138 for component in &components[..] {
139 let mut flag = String::from("-DLLVM_COMPONENT_");
140 flag.push_str(&component.to_uppercase());
144 if env::var_os("LLVM_RUSTLLVM").is_some() {
145 cfg.flag("-DLLVM_RUSTLLVM");
148 println!("cargo:rerun-if-changed=../rustllvm/PassWrapper.cpp");
149 println!("cargo:rerun-if-changed=../rustllvm/RustWrapper.cpp");
150 println!("cargo:rerun-if-changed=../rustllvm/ArchiveWrapper.cpp");
151 cfg.file("../rustllvm/PassWrapper.cpp")
152 .file("../rustllvm/RustWrapper.cpp")
153 .file("../rustllvm/ArchiveWrapper.cpp")
155 .cpp_link_stdlib(None) // we handle this below
156 .compile("librustllvm.a");
158 let (llvm_kind, llvm_link_arg) = detect_llvm_link(&llvm_config);
160 // Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
161 // we don't pick up system libs because unfortunately they're for the host
162 // of llvm-config, not the target that we're attempting to link.
163 let mut cmd = Command::new(&llvm_config);
166 if let Some(link_arg) = llvm_link_arg {
171 cmd.arg("--system-libs");
173 cmd.args(&components[..]);
175 for lib in output(&mut cmd).split_whitespace() {
176 let name = if lib.starts_with("-l") {
178 } else if lib.starts_with("-") {
180 } else if Path::new(lib).exists() {
181 // On MSVC llvm-config will print the full name to libraries, but
182 // we're only interested in the name part
183 let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
184 name.trim_right_matches(".lib")
185 } else if lib.ends_with(".lib") {
186 // Some MSVC libraries just come up with `.lib` tacked on, so chop
188 lib.trim_right_matches(".lib")
193 // Don't need or want this library, but LLVM's CMake build system
194 // doesn't provide a way to disable it, so filter it here even though we
195 // may or may not have built it. We don't reference anything from this
196 // library and it otherwise may just pull in extra dependencies on
197 // libedit which we don't want
198 if name == "LLVMLineEditor" {
202 let kind = if name.starts_with("LLVM") {
207 println!("cargo:rustc-link-lib={}={}", kind, name);
212 // If we're a cross-compile of LLVM then unfortunately we can't trust these
213 // ldflags (largely where all the LLVM libs are located). Currently just
214 // hack around this by replacing the host triple with the target and pray
215 // that those -L directories are the same!
216 let mut cmd = Command::new(&llvm_config);
217 cmd.arg("--ldflags");
218 for lib in output(&mut cmd).split_whitespace() {
219 if lib.starts_with("-LIBPATH:") {
220 println!("cargo:rustc-link-search=native={}", &lib[9..]);
221 } else if is_crossed {
222 if lib.starts_with("-L") {
223 println!("cargo:rustc-link-search=native={}",
224 lib[2..].replace(&host, &target));
226 } else if lib.starts_with("-l") {
227 println!("cargo:rustc-link-lib={}", &lib[2..]);
228 } else if lib.starts_with("-L") {
229 println!("cargo:rustc-link-search=native={}", &lib[2..]);
233 // C++ runtime library
234 if !target.contains("msvc") {
235 if let Some(s) = env::var_os("LLVM_STATIC_STDCPP") {
236 assert!(!cxxflags.contains("stdlib=libc++"));
237 let path = PathBuf::from(s);
238 println!("cargo:rustc-link-search=native={}",
239 path.parent().unwrap().display());
240 println!("cargo:rustc-link-lib=static=stdc++");
241 } else if cxxflags.contains("stdlib=libc++") {
242 println!("cargo:rustc-link-lib=c++");
244 println!("cargo:rustc-link-lib=stdc++");