]> git.lizzy.rs Git - rust.git/blob - src/librustc_llvm/build.rs
Fix font color for help button in ayu and dark themes
[rust.git] / src / librustc_llvm / build.rs
1 use std::env;
2 use std::path::{Path, PathBuf};
3 use std::process::Command;
4
5 use build_helper::{output, tracked_env_var_os};
6
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")
12     } else {
13         ("static", "--link-static")
14     }
15 }
16
17 fn main() {
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.
20         return;
21     }
22
23     build_helper::restore_library_path();
24
25     let target = env::var("TARGET").expect("TARGET was not set");
26     let llvm_config =
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) {
29                 let to_test = dir
30                     .parent()
31                     .unwrap()
32                     .parent()
33                     .unwrap()
34                     .join(&target)
35                     .join("llvm/bin/llvm-config");
36                 if Command::new(&to_test).output().is_ok() {
37                     return Some(to_test);
38                 }
39             }
40             None
41         });
42
43     if let Some(llvm_config) = &llvm_config {
44         println!("cargo:rerun-if-changed={}", llvm_config.display());
45     }
46     let llvm_config = llvm_config.unwrap_or_else(|| PathBuf::from("llvm-config"));
47
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.
51     //
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.
56     //
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
63     // havoc ensues.
64     //
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;
72
73     let mut optional_components = vec![
74         "x86",
75         "arm",
76         "aarch64",
77         "amdgpu",
78         "avr",
79         "mips",
80         "powerpc",
81         "systemz",
82         "jsbackend",
83         "webassembly",
84         "msp430",
85         "sparc",
86         "nvptx",
87         "hexagon",
88     ];
89
90     let mut version_cmd = Command::new(&llvm_config);
91     version_cmd.arg("--version");
92     let version_output = output(&mut version_cmd);
93     let mut parts = version_output.split('.').take(2).filter_map(|s| s.parse::<u32>().ok());
94     let (major, _minor) = if let (Some(major), Some(minor)) = (parts.next(), parts.next()) {
95         (major, minor)
96     } else {
97         (6, 0)
98     };
99
100     if major > 6 {
101         optional_components.push("riscv");
102     }
103
104     let required_components = &[
105         "ipo",
106         "bitreader",
107         "bitwriter",
108         "linker",
109         "asmparser",
110         "lto",
111         "coverage",
112         "instrumentation",
113     ];
114
115     let components = output(Command::new(&llvm_config).arg("--components"));
116     let mut components = components.split_whitespace().collect::<Vec<_>>();
117     components.retain(|c| optional_components.contains(c) || required_components.contains(c));
118
119     for component in required_components {
120         if !components.contains(component) {
121             panic!("require llvm component {} but wasn't found", component);
122         }
123     }
124
125     for component in components.iter() {
126         println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
127     }
128
129     if major >= 9 {
130         println!("cargo:rustc-cfg=llvm_has_msp430_asm_parser");
131     }
132
133     // Link in our own LLVM shims, compiled with the same flags as LLVM
134     let mut cmd = Command::new(&llvm_config);
135     cmd.arg("--cxxflags");
136     let cxxflags = output(&mut cmd);
137     let mut cfg = cc::Build::new();
138     cfg.warnings(false);
139     for flag in cxxflags.split_whitespace() {
140         // Ignore flags like `-m64` when we're doing a cross build
141         if is_crossed && flag.starts_with("-m") {
142             continue;
143         }
144
145         if flag.starts_with("-flto") {
146             continue;
147         }
148
149         // -Wdate-time is not supported by the netbsd cross compiler
150         if is_crossed && target.contains("netbsd") && flag.contains("date-time") {
151             continue;
152         }
153
154         cfg.flag(flag);
155     }
156
157     for component in &components {
158         let mut flag = String::from("LLVM_COMPONENT_");
159         flag.push_str(&component.to_uppercase());
160         cfg.define(&flag, None);
161     }
162
163     if tracked_env_var_os("LLVM_RUSTLLVM").is_some() {
164         cfg.define("LLVM_RUSTLLVM", None);
165     }
166
167     if tracked_env_var_os("LLVM_NDEBUG").is_some() {
168         cfg.define("NDEBUG", None);
169         cfg.debug(false);
170     }
171
172     build_helper::rerun_if_changed_anything_in_dir(Path::new("../rustllvm"));
173     cfg.file("../rustllvm/PassWrapper.cpp")
174         .file("../rustllvm/RustWrapper.cpp")
175         .file("../rustllvm/ArchiveWrapper.cpp")
176         .file("../rustllvm/CoverageMappingWrapper.cpp")
177         .file("../rustllvm/Linker.cpp")
178         .cpp(true)
179         .cpp_link_stdlib(None) // we handle this below
180         .compile("rustllvm");
181
182     let (llvm_kind, llvm_link_arg) = detect_llvm_link();
183
184     // Link in all LLVM libraries, if we're using the "wrong" llvm-config then
185     // we don't pick up system libs because unfortunately they're for the host
186     // of llvm-config, not the target that we're attempting to link.
187     let mut cmd = Command::new(&llvm_config);
188     cmd.arg(llvm_link_arg).arg("--libs");
189
190     if !is_crossed {
191         cmd.arg("--system-libs");
192     }
193     cmd.args(&components);
194
195     for lib in output(&mut cmd).split_whitespace() {
196         let name = if lib.starts_with("-l") {
197             &lib[2..]
198         } else if lib.starts_with('-') {
199             &lib[1..]
200         } else if Path::new(lib).exists() {
201             // On MSVC llvm-config will print the full name to libraries, but
202             // we're only interested in the name part
203             let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
204             name.trim_end_matches(".lib")
205         } else if lib.ends_with(".lib") {
206             // Some MSVC libraries just come up with `.lib` tacked on, so chop
207             // that off
208             lib.trim_end_matches(".lib")
209         } else {
210             continue;
211         };
212
213         // Don't need or want this library, but LLVM's CMake build system
214         // doesn't provide a way to disable it, so filter it here even though we
215         // may or may not have built it. We don't reference anything from this
216         // library and it otherwise may just pull in extra dependencies on
217         // libedit which we don't want
218         if name == "LLVMLineEditor" {
219             continue;
220         }
221
222         let kind = if name.starts_with("LLVM") { llvm_kind } else { "dylib" };
223         println!("cargo:rustc-link-lib={}={}", kind, name);
224     }
225
226     // LLVM ldflags
227     //
228     // If we're a cross-compile of LLVM then unfortunately we can't trust these
229     // ldflags (largely where all the LLVM libs are located). Currently just
230     // hack around this by replacing the host triple with the target and pray
231     // that those -L directories are the same!
232     let mut cmd = Command::new(&llvm_config);
233     cmd.arg(llvm_link_arg).arg("--ldflags");
234     for lib in output(&mut cmd).split_whitespace() {
235         if is_crossed {
236             if lib.starts_with("-LIBPATH:") {
237                 println!("cargo:rustc-link-search=native={}", lib[9..].replace(&host, &target));
238             } else if lib.starts_with("-L") {
239                 println!("cargo:rustc-link-search=native={}", lib[2..].replace(&host, &target));
240             }
241         } else if lib.starts_with("-LIBPATH:") {
242             println!("cargo:rustc-link-search=native={}", &lib[9..]);
243         } else if lib.starts_with("-l") {
244             println!("cargo:rustc-link-lib={}", &lib[2..]);
245         } else if lib.starts_with("-L") {
246             println!("cargo:rustc-link-search=native={}", &lib[2..]);
247         }
248     }
249
250     // Some LLVM linker flags (-L and -l) may be needed even when linking
251     // librustc_llvm, for example when using static libc++, we may need to
252     // manually specify the library search path and -ldl -lpthread as link
253     // dependencies.
254     let llvm_linker_flags = tracked_env_var_os("LLVM_LINKER_FLAGS");
255     if let Some(s) = llvm_linker_flags {
256         for lib in s.into_string().unwrap().split_whitespace() {
257             if lib.starts_with("-l") {
258                 println!("cargo:rustc-link-lib={}", &lib[2..]);
259             } else if lib.starts_with("-L") {
260                 println!("cargo:rustc-link-search=native={}", &lib[2..]);
261             }
262         }
263     }
264
265     let llvm_static_stdcpp = tracked_env_var_os("LLVM_STATIC_STDCPP");
266     let llvm_use_libcxx = tracked_env_var_os("LLVM_USE_LIBCXX");
267
268     let stdcppname = if target.contains("openbsd") {
269         if target.contains("sparc64") { "estdc++" } else { "c++" }
270     } else if target.contains("freebsd") {
271         "c++"
272     } else if target.contains("darwin") {
273         "c++"
274     } else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
275         // NetBSD uses a separate library when relocation is required
276         "stdc++_pic"
277     } else if llvm_use_libcxx.is_some() {
278         "c++"
279     } else {
280         "stdc++"
281     };
282
283     // RISC-V requires libatomic for sub-word atomic operations
284     if target.starts_with("riscv") {
285         println!("cargo:rustc-link-lib=atomic");
286     }
287
288     // C++ runtime library
289     if !target.contains("msvc") {
290         if let Some(s) = llvm_static_stdcpp {
291             assert!(!cxxflags.contains("stdlib=libc++"));
292             let path = PathBuf::from(s);
293             println!("cargo:rustc-link-search=native={}", path.parent().unwrap().display());
294             if target.contains("windows") {
295                 println!("cargo:rustc-link-lib=static-nobundle={}", stdcppname);
296             } else {
297                 println!("cargo:rustc-link-lib=static={}", stdcppname);
298             }
299         } else if cxxflags.contains("stdlib=libc++") {
300             println!("cargo:rustc-link-lib=c++");
301         } else {
302             println!("cargo:rustc-link-lib={}", stdcppname);
303         }
304     }
305
306     // Libstdc++ depends on pthread which Rust doesn't link on MinGW
307     // since nothing else requires it.
308     if target.contains("windows-gnu") {
309         println!("cargo:rustc-link-lib=static-nobundle=pthread");
310     }
311 }