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