]> git.lizzy.rs Git - rust.git/blob - src/librustc_llvm/build.rs
libpanic_abort => 2018
[rust.git] / src / librustc_llvm / build.rs
1 extern crate cc;
2 extern crate build_helper;
3
4 use std::process::Command;
5 use std::env;
6 use std::path::{PathBuf, Path};
7
8 use build_helper::output;
9
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")
15     } else {
16         ("static", "--link-static")
17     }
18 }
19
20 fn main() {
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");
24         return;
25     }
26
27     build_helper::restore_library_path();
28
29     let target = env::var("TARGET").expect("TARGET was not set");
30     let llvm_config = env::var_os("LLVM_CONFIG")
31         .map(PathBuf::from)
32         .unwrap_or_else(|| {
33             if let Some(dir) = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
34                 let to_test = dir.parent()
35                     .unwrap()
36                     .parent()
37                     .unwrap()
38                     .join(&target)
39                     .join("llvm/bin/llvm-config");
40                 if Command::new(&to_test).output().is_ok() {
41                     return to_test;
42                 }
43             }
44             PathBuf::from("llvm-config")
45         });
46
47     println!("cargo:rerun-if-changed={}", llvm_config.display());
48     println!("cargo:rerun-if-env-changed=LLVM_CONFIG");
49
50     // Test whether we're cross-compiling LLVM. This is a pretty rare case
51     // currently where we're producing an LLVM for a different platform than
52     // what this build script is currently running on.
53     //
54     // In that case, there's no guarantee that we can actually run the target,
55     // so the build system works around this by giving us the LLVM_CONFIG for
56     // the host platform. This only really works if the host LLVM and target
57     // LLVM are compiled the same way, but for us that's typically the case.
58     //
59     // We *want* detect this cross compiling situation by asking llvm-config
60     // what it's host-target is. If that's not the TARGET, then we're cross
61     // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
62     // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
63     // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
64     // tricks us into thinking we're doing a cross build when we aren't, so
65     // havoc ensues.
66     //
67     // In any case, if we're cross compiling, this generally just means that we
68     // can't trust all the output of llvm-config becaues it might be targeted
69     // for the host rather than the target. As a result a bunch of blocks below
70     // are gated on `if !is_crossed`
71     let target = env::var("TARGET").expect("TARGET was not set");
72     let host = env::var("HOST").expect("HOST was not set");
73     let is_crossed = target != host;
74
75     let mut optional_components =
76         vec!["x86", "arm", "aarch64", "amdgpu", "mips", "powerpc",
77              "systemz", "jsbackend", "webassembly", "msp430", "sparc", "nvptx"];
78
79     let mut version_cmd = Command::new(&llvm_config);
80     version_cmd.arg("--version");
81     let version_output = output(&mut version_cmd);
82     let mut parts = version_output.split('.').take(2)
83         .filter_map(|s| s.parse::<u32>().ok());
84     let (major, _minor) =
85         if let (Some(major), Some(minor)) = (parts.next(), parts.next()) {
86             (major, minor)
87         } else {
88             (3, 9)
89         };
90
91     if major > 3 {
92         optional_components.push("hexagon");
93     }
94
95     if major > 6 {
96         optional_components.push("riscv");
97     }
98
99     // FIXME: surely we don't need all these components, right? Stuff like mcjit
100     //        or interpreter the compiler itself never uses.
101     let required_components = &["ipo",
102                                 "bitreader",
103                                 "bitwriter",
104                                 "linker",
105                                 "asmparser",
106                                 "mcjit",
107                                 "lto",
108                                 "interpreter",
109                                 "instrumentation"];
110
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));
114
115     for component in required_components {
116         if !components.contains(component) {
117             panic!("require llvm component {} but wasn't found", component);
118         }
119     }
120
121     for component in components.iter() {
122         println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
123     }
124
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 = cc::Build::new();
130     cfg.warnings(false);
131     for flag in cxxflags.split_whitespace() {
132         // Ignore flags like `-m64` when we're doing a cross build
133         if is_crossed && flag.starts_with("-m") {
134             continue;
135         }
136
137         if flag.starts_with("-flto") {
138             continue;
139         }
140
141         // -Wdate-time is not supported by the netbsd cross compiler
142         if is_crossed && target.contains("netbsd") && flag.contains("date-time") {
143             continue;
144         }
145
146         cfg.flag(flag);
147     }
148
149     for component in &components {
150         let mut flag = String::from("LLVM_COMPONENT_");
151         flag.push_str(&component.to_uppercase());
152         cfg.define(&flag, None);
153     }
154
155     println!("cargo:rerun-if-changed-env=LLVM_RUSTLLVM");
156     if env::var_os("LLVM_RUSTLLVM").is_some() {
157         cfg.define("LLVM_RUSTLLVM", None);
158     }
159
160     build_helper::rerun_if_changed_anything_in_dir(Path::new("../rustllvm"));
161     cfg.file("../rustllvm/PassWrapper.cpp")
162        .file("../rustllvm/RustWrapper.cpp")
163        .file("../rustllvm/ArchiveWrapper.cpp")
164        .file("../rustllvm/Linker.cpp")
165        .cpp(true)
166        .cpp_link_stdlib(None) // we handle this below
167        .compile("rustllvm");
168
169     let (llvm_kind, llvm_link_arg) = detect_llvm_link();
170
171     // Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
172     // we don't pick up system libs because unfortunately they're for the host
173     // of llvm-config, not the target that we're attempting to link.
174     let mut cmd = Command::new(&llvm_config);
175     cmd.arg(llvm_link_arg).arg("--libs");
176
177     if !is_crossed {
178         cmd.arg("--system-libs");
179     }
180     cmd.args(&components);
181
182     for lib in output(&mut cmd).split_whitespace() {
183         let name = if lib.starts_with("-l") {
184             &lib[2..]
185         } else if lib.starts_with("-") {
186             &lib[1..]
187         } else if Path::new(lib).exists() {
188             // On MSVC llvm-config will print the full name to libraries, but
189             // we're only interested in the name part
190             let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
191             name.trim_end_matches(".lib")
192         } else if lib.ends_with(".lib") {
193             // Some MSVC libraries just come up with `.lib` tacked on, so chop
194             // that off
195             lib.trim_end_matches(".lib")
196         } else {
197             continue;
198         };
199
200         // Don't need or want this library, but LLVM's CMake build system
201         // doesn't provide a way to disable it, so filter it here even though we
202         // may or may not have built it. We don't reference anything from this
203         // library and it otherwise may just pull in extra dependencies on
204         // libedit which we don't want
205         if name == "LLVMLineEditor" {
206             continue;
207         }
208
209         let kind = if name.starts_with("LLVM") {
210             llvm_kind
211         } else {
212             "dylib"
213         };
214         println!("cargo:rustc-link-lib={}={}", kind, name);
215     }
216
217     // LLVM ldflags
218     //
219     // If we're a cross-compile of LLVM then unfortunately we can't trust these
220     // ldflags (largely where all the LLVM libs are located). Currently just
221     // hack around this by replacing the host triple with the target and pray
222     // that those -L directories are the same!
223     let mut cmd = Command::new(&llvm_config);
224     cmd.arg(llvm_link_arg).arg("--ldflags");
225     for lib in output(&mut cmd).split_whitespace() {
226         if lib.starts_with("-LIBPATH:") {
227             println!("cargo:rustc-link-search=native={}", &lib[9..]);
228         } else if is_crossed {
229             if lib.starts_with("-L") {
230                 println!("cargo:rustc-link-search=native={}",
231                          lib[2..].replace(&host, &target));
232             }
233         } else if lib.starts_with("-l") {
234             println!("cargo:rustc-link-lib={}", &lib[2..]);
235         } else if lib.starts_with("-L") {
236             println!("cargo:rustc-link-search=native={}", &lib[2..]);
237         }
238     }
239
240     let llvm_static_stdcpp = env::var_os("LLVM_STATIC_STDCPP");
241     let llvm_use_libcxx = env::var_os("LLVM_USE_LIBCXX");
242
243     let stdcppname = if target.contains("openbsd") {
244         // llvm-config on OpenBSD doesn't mention stdlib=libc++
245         "c++"
246     } else if target.contains("freebsd") {
247         "c++"
248     } else if target.contains("darwin") {
249         "c++"
250     } else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
251         // NetBSD uses a separate library when relocation is required
252         "stdc++_pic"
253     } else if llvm_use_libcxx.is_some() {
254         "c++"
255     } else {
256         "stdc++"
257     };
258
259     // C++ runtime library
260     if !target.contains("msvc") {
261         if let Some(s) = llvm_static_stdcpp {
262             assert!(!cxxflags.contains("stdlib=libc++"));
263             let path = PathBuf::from(s);
264             println!("cargo:rustc-link-search=native={}",
265                      path.parent().unwrap().display());
266             println!("cargo:rustc-link-lib=static={}", stdcppname);
267         } else if cxxflags.contains("stdlib=libc++") {
268             println!("cargo:rustc-link-lib=c++");
269         } else {
270             println!("cargo:rustc-link-lib={}", stdcppname);
271         }
272     }
273
274     // LLVM requires symbols from this library, but apparently they're not printed
275     // during llvm-config?
276     if target.contains("windows-gnu") {
277         println!("cargo:rustc-link-lib=static-nobundle=gcc_s");
278         println!("cargo:rustc-link-lib=static-nobundle=pthread");
279         println!("cargo:rustc-link-lib=dylib=uuid");
280     }
281 }