]>
Commit | Line | Data |
---|---|---|
5bcae85e SL |
1 | //! Compilation of native dependencies like LLVM. |
2 | //! | |
3 | //! Native projects like LLVM unfortunately aren't suited just yet for | |
3b2f2976 | 4 | //! compilation in build scripts that Cargo has. This is because the |
5bcae85e SL |
5 | //! compilation takes a *very* long time but also because we don't want to |
6 | //! compile LLVM 3 times as part of a normal bootstrap (we want it cached). | |
7 | //! | |
8 | //! LLVM and compiler-rt are essentially just wired up to everything else to | |
9 | //! ensure that they're always in place if needed. | |
10 | ||
cc61c64b | 11 | use std::env; |
7cac9316 | 12 | use std::ffi::OsString; |
c30ab7b3 | 13 | use std::fs::{self, File}; |
2c00a5a8 | 14 | use std::path::{Path, PathBuf}; |
5bcae85e | 15 | use std::process::Command; |
5bcae85e | 16 | |
48663c56 | 17 | use build_helper::{output, t}; |
5bcae85e | 18 | use cmake; |
ea8adc8c | 19 | use cc; |
5bcae85e | 20 | |
9fa01778 | 21 | use crate::channel; |
0731742a | 22 | use crate::util::{self, exe}; |
8bb4bdeb | 23 | use build_helper::up_to_date; |
0731742a XL |
24 | use crate::builder::{Builder, RunConfig, ShouldRun, Step}; |
25 | use crate::cache::Interned; | |
26 | use crate::GitRepo; | |
5bcae85e | 27 | |
3b2f2976 XL |
28 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] |
29 | pub struct Llvm { | |
30 | pub target: Interned<String>, | |
31 | } | |
32 | ||
33 | impl Step for Llvm { | |
2c00a5a8 XL |
34 | type Output = PathBuf; // path to llvm-config |
35 | ||
3b2f2976 XL |
36 | const ONLY_HOSTS: bool = true; |
37 | ||
9fa01778 XL |
38 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
39 | run.path("src/llvm-project") | |
40 | .path("src/llvm-project/llvm") | |
41 | .path("src/llvm") | |
5bcae85e SL |
42 | } |
43 | ||
9fa01778 | 44 | fn make_run(run: RunConfig<'_>) { |
2c00a5a8 XL |
45 | run.builder.ensure(Llvm { |
46 | target: run.target, | |
2c00a5a8 | 47 | }); |
3b2f2976 | 48 | } |
8bb4bdeb | 49 | |
3b2f2976 | 50 | /// Compile LLVM for `target`. |
9fa01778 | 51 | fn run(self, builder: &Builder<'_>) -> PathBuf { |
3b2f2976 | 52 | let target = self.target; |
3b2f2976 XL |
53 | |
54 | // If we're using a custom LLVM bail out here, but we can only use a | |
55 | // custom LLVM for the build triple. | |
e74abb32 XL |
56 | if let Some(config) = builder.config.target_config.get(&target) { |
57 | if let Some(ref s) = config.llvm_config { | |
58 | check_llvm_version(builder, s); | |
59 | return s.to_path_buf() | |
3b2f2976 | 60 | } |
c30ab7b3 | 61 | } |
5bcae85e | 62 | |
e74abb32 XL |
63 | let llvm_info = &builder.in_tree_llvm_info; |
64 | let root = "src/llvm-project/llvm"; | |
65 | let out_dir = builder.llvm_out(target); | |
66 | let mut llvm_config_ret_dir = builder.llvm_out(builder.config.build); | |
67 | if !builder.config.build.contains("msvc") || builder.config.ninja { | |
68 | llvm_config_ret_dir.push("build"); | |
69 | } | |
70 | llvm_config_ret_dir.push("bin"); | |
532ac7d7 | 71 | |
2c00a5a8 | 72 | let build_llvm_config = llvm_config_ret_dir |
83c7162d | 73 | .join(exe("llvm-config", &*builder.config.build)); |
532ac7d7 XL |
74 | let done_stamp = out_dir.join("llvm-finished-building"); |
75 | ||
e1599b0c XL |
76 | if done_stamp.exists() { |
77 | if let Some(llvm_commit) = llvm_info.sha() { | |
532ac7d7 | 78 | let done_contents = t!(fs::read(&done_stamp)); |
476ff2be | 79 | |
532ac7d7 XL |
80 | // If LLVM was already built previously and the submodule's commit didn't change |
81 | // from the previous build, then no action is required. | |
82 | if done_contents == llvm_commit.as_bytes() { | |
e1599b0c | 83 | return build_llvm_config; |
532ac7d7 | 84 | } |
e1599b0c XL |
85 | } else { |
86 | builder.info( | |
87 | "Could not determine the LLVM submodule commit hash. \ | |
88 | Assuming that an LLVM rebuild is not necessary.", | |
89 | ); | |
90 | builder.info(&format!( | |
91 | "To force LLVM to rebuild, remove the file `{}`", | |
92 | done_stamp.display() | |
93 | )); | |
94 | return build_llvm_config; | |
3b2f2976 XL |
95 | } |
96 | } | |
8bb4bdeb | 97 | |
e74abb32 | 98 | builder.info(&format!("Building LLVM for {}", target)); |
83c7162d | 99 | let _time = util::timeit(&builder); |
3b2f2976 | 100 | t!(fs::create_dir_all(&out_dir)); |
5bcae85e | 101 | |
3b2f2976 | 102 | // http://llvm.org/docs/CMake.html |
83c7162d | 103 | let mut cfg = cmake::Config::new(builder.src.join(root)); |
8bb4bdeb | 104 | |
83c7162d | 105 | let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) { |
3b2f2976 XL |
106 | (false, _) => "Debug", |
107 | (true, false) => "Release", | |
108 | (true, true) => "RelWithDebInfo", | |
109 | }; | |
110 | ||
2c00a5a8 XL |
111 | // NOTE: remember to also update `config.toml.example` when changing the |
112 | // defaults! | |
e74abb32 XL |
113 | let llvm_targets = match &builder.config.llvm_targets { |
114 | Some(s) => s, | |
115 | None => "AArch64;ARM;Hexagon;MSP430;Mips;NVPTX;PowerPC;RISCV;\ | |
116 | Sparc;SystemZ;WebAssembly;X86", | |
3b2f2976 XL |
117 | }; |
118 | ||
e74abb32 XL |
119 | let llvm_exp_targets = match builder.config.llvm_experimental_targets { |
120 | Some(ref s) => s, | |
121 | None => "", | |
2c00a5a8 | 122 | }; |
3b2f2976 | 123 | |
83c7162d | 124 | let assertions = if builder.config.llvm_assertions {"ON"} else {"OFF"}; |
3b2f2976 | 125 | |
0531ce1d | 126 | cfg.out_dir(&out_dir) |
3b2f2976 XL |
127 | .profile(profile) |
128 | .define("LLVM_ENABLE_ASSERTIONS", assertions) | |
129 | .define("LLVM_TARGETS_TO_BUILD", llvm_targets) | |
130 | .define("LLVM_EXPERIMENTAL_TARGETS_TO_BUILD", llvm_exp_targets) | |
131 | .define("LLVM_INCLUDE_EXAMPLES", "OFF") | |
132 | .define("LLVM_INCLUDE_TESTS", "OFF") | |
133 | .define("LLVM_INCLUDE_DOCS", "OFF") | |
b7449926 | 134 | .define("LLVM_INCLUDE_BENCHMARKS", "OFF") |
3b2f2976 XL |
135 | .define("LLVM_ENABLE_ZLIB", "OFF") |
136 | .define("WITH_POLLY", "OFF") | |
137 | .define("LLVM_ENABLE_TERMINFO", "OFF") | |
138 | .define("LLVM_ENABLE_LIBEDIT", "OFF") | |
e74abb32 | 139 | .define("LLVM_ENABLE_BINDINGS", "OFF") |
416331ca | 140 | .define("LLVM_ENABLE_Z3_SOLVER", "OFF") |
83c7162d | 141 | .define("LLVM_PARALLEL_COMPILE_JOBS", builder.jobs().to_string()) |
3b2f2976 XL |
142 | .define("LLVM_TARGET_ARCH", target.split('-').next().unwrap()) |
143 | .define("LLVM_DEFAULT_TARGET_TRIPLE", target); | |
144 | ||
e74abb32 | 145 | if builder.config.llvm_thin_lto { |
48663c56 XL |
146 | cfg.define("LLVM_ENABLE_LTO", "Thin"); |
147 | if !target.contains("apple") { | |
148 | cfg.define("LLVM_ENABLE_LLD", "ON"); | |
149 | } | |
b7449926 XL |
150 | } |
151 | ||
3b2f2976 XL |
152 | // This setting makes the LLVM tools link to the dynamic LLVM library, |
153 | // which saves both memory during parallel links and overall disk space | |
b7449926 XL |
154 | // for the tools. We don't do this on every platform as it doesn't work |
155 | // equally well everywhere. | |
e74abb32 | 156 | if builder.llvm_link_tools_dynamically(target) { |
b7449926 | 157 | cfg.define("LLVM_LINK_LLVM_DYLIB", "ON"); |
8faf50e0 XL |
158 | } |
159 | ||
160 | // For distribution we want the LLVM tools to be *statically* linked to libstdc++ | |
e74abb32 XL |
161 | if builder.config.llvm_tools_enabled || builder.config.lldb_enabled { |
162 | if !target.contains("msvc") { | |
8faf50e0 XL |
163 | if target.contains("apple") { |
164 | cfg.define("CMAKE_EXE_LINKER_FLAGS", "-static-libstdc++"); | |
165 | } else { | |
166 | cfg.define("CMAKE_EXE_LINKER_FLAGS", "-Wl,-Bsymbolic -static-libstdc++"); | |
167 | } | |
168 | } | |
3b2f2976 | 169 | } |
5bcae85e | 170 | |
7cac9316 | 171 | if target.contains("msvc") { |
3b2f2976 XL |
172 | cfg.define("LLVM_USE_CRT_DEBUG", "MT"); |
173 | cfg.define("LLVM_USE_CRT_RELEASE", "MT"); | |
174 | cfg.define("LLVM_USE_CRT_RELWITHDEBINFO", "MT"); | |
175 | cfg.static_crt(true); | |
7cac9316 | 176 | } |
7cac9316 | 177 | |
3b2f2976 XL |
178 | if target.starts_with("i686") { |
179 | cfg.define("LLVM_BUILD_32_BITS", "ON"); | |
7cac9316 XL |
180 | } |
181 | ||
dc9dc135 XL |
182 | let mut enabled_llvm_projects = Vec::new(); |
183 | ||
184 | if util::forcing_clang_based_tests() { | |
185 | enabled_llvm_projects.push("clang"); | |
186 | enabled_llvm_projects.push("compiler-rt"); | |
187 | } | |
188 | ||
e74abb32 | 189 | if builder.config.lldb_enabled { |
dc9dc135 XL |
190 | enabled_llvm_projects.push("clang"); |
191 | enabled_llvm_projects.push("lldb"); | |
b7449926 XL |
192 | // For the time being, disable code signing. |
193 | cfg.define("LLDB_CODESIGN_IDENTITY", ""); | |
9fa01778 | 194 | cfg.define("LLDB_NO_DEBUGSERVER", "ON"); |
b7449926 XL |
195 | } else { |
196 | // LLDB requires libxml2; but otherwise we want it to be disabled. | |
197 | // See https://github.com/rust-lang/rust/pull/50104 | |
198 | cfg.define("LLVM_ENABLE_LIBXML2", "OFF"); | |
199 | } | |
200 | ||
dc9dc135 XL |
201 | if enabled_llvm_projects.len() > 0 { |
202 | enabled_llvm_projects.sort(); | |
203 | enabled_llvm_projects.dedup(); | |
204 | cfg.define("LLVM_ENABLE_PROJECTS", enabled_llvm_projects.join(";")); | |
205 | } | |
206 | ||
83c7162d | 207 | if let Some(num_linkers) = builder.config.llvm_link_jobs { |
3b2f2976 XL |
208 | if num_linkers > 0 { |
209 | cfg.define("LLVM_PARALLEL_LINK_JOBS", num_linkers.to_string()); | |
210 | } | |
5bcae85e | 211 | } |
5bcae85e | 212 | |
3b2f2976 | 213 | // http://llvm.org/docs/HowToCrossCompileLLVM.html |
e74abb32 | 214 | if target != builder.config.build { |
2c00a5a8 | 215 | builder.ensure(Llvm { |
83c7162d | 216 | target: builder.config.build, |
2c00a5a8 | 217 | }); |
3b2f2976 XL |
218 | // FIXME: if the llvm root for the build triple is overridden then we |
219 | // should use llvm-tblgen from there, also should verify that it | |
220 | // actually exists most of the time in normal installs of LLVM. | |
83c7162d | 221 | let host = builder.llvm_out(builder.config.build).join("bin/llvm-tblgen"); |
3b2f2976 XL |
222 | cfg.define("CMAKE_CROSSCOMPILING", "True") |
223 | .define("LLVM_TABLEGEN", &host); | |
224 | ||
225 | if target.contains("netbsd") { | |
226 | cfg.define("CMAKE_SYSTEM_NAME", "NetBSD"); | |
227 | } else if target.contains("freebsd") { | |
228 | cfg.define("CMAKE_SYSTEM_NAME", "FreeBSD"); | |
229 | } | |
7cac9316 | 230 | |
83c7162d | 231 | cfg.define("LLVM_NATIVE_BUILD", builder.llvm_out(builder.config.build).join("build")); |
3b2f2976 | 232 | } |
5bcae85e | 233 | |
b7449926 | 234 | if let Some(ref suffix) = builder.config.llvm_version_suffix { |
9fa01778 XL |
235 | // Allow version-suffix="" to not define a version suffix at all. |
236 | if !suffix.is_empty() { | |
237 | cfg.define("LLVM_VERSION_SUFFIX", suffix); | |
238 | } | |
239 | } else { | |
240 | let mut default_suffix = format!( | |
241 | "-rust-{}-{}", | |
242 | channel::CFG_RELEASE_NUM, | |
243 | builder.config.channel, | |
244 | ); | |
9fa01778 XL |
245 | if let Some(sha) = llvm_info.sha_short() { |
246 | default_suffix.push_str("-"); | |
247 | default_suffix.push_str(sha); | |
248 | } | |
249 | cfg.define("LLVM_VERSION_SUFFIX", default_suffix); | |
250 | } | |
251 | ||
252 | if let Some(ref linker) = builder.config.llvm_use_linker { | |
253 | cfg.define("LLVM_USE_LINKER", linker); | |
b7449926 XL |
254 | } |
255 | ||
532ac7d7 XL |
256 | if let Some(true) = builder.config.llvm_allow_old_toolchain { |
257 | cfg.define("LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN", "YES"); | |
258 | } | |
259 | ||
b7449926 XL |
260 | if let Some(ref python) = builder.config.python { |
261 | cfg.define("PYTHON_EXECUTABLE", python); | |
262 | } | |
263 | ||
0731742a | 264 | configure_cmake(builder, target, &mut cfg); |
3b2f2976 XL |
265 | |
266 | // FIXME: we don't actually need to build all LLVM tools and all LLVM | |
0731742a | 267 | // libraries here, e.g., we just want a few components and a few |
3b2f2976 XL |
268 | // tools. Figure out how to filter them down and only build the right |
269 | // tools and libs on all platforms. | |
83c7162d XL |
270 | |
271 | if builder.config.dry_run { | |
272 | return build_llvm_config; | |
273 | } | |
274 | ||
3b2f2976 XL |
275 | cfg.build(); |
276 | ||
e1599b0c | 277 | t!(fs::write(&done_stamp, llvm_info.sha().unwrap_or(""))); |
2c00a5a8 XL |
278 | |
279 | build_llvm_config | |
3b2f2976 | 280 | } |
5bcae85e SL |
281 | } |
282 | ||
9fa01778 | 283 | fn check_llvm_version(builder: &Builder<'_>, llvm_config: &Path) { |
83c7162d | 284 | if !builder.config.llvm_version_check { |
5bcae85e SL |
285 | return |
286 | } | |
287 | ||
83c7162d XL |
288 | if builder.config.dry_run { |
289 | return; | |
290 | } | |
291 | ||
5bcae85e SL |
292 | let mut cmd = Command::new(llvm_config); |
293 | let version = output(cmd.arg("--version")); | |
abe05a73 XL |
294 | let mut parts = version.split('.').take(2) |
295 | .filter_map(|s| s.parse::<u32>().ok()); | |
8faf50e0 | 296 | if let (Some(major), Some(_minor)) = (parts.next(), parts.next()) { |
0731742a | 297 | if major >= 6 { |
abe05a73 XL |
298 | return |
299 | } | |
5bcae85e | 300 | } |
0731742a | 301 | panic!("\n\nbad LLVM version: {}, need >=6.0\n\n", version) |
5bcae85e SL |
302 | } |
303 | ||
9fa01778 | 304 | fn configure_cmake(builder: &Builder<'_>, |
0531ce1d | 305 | target: Interned<String>, |
0731742a | 306 | cfg: &mut cmake::Config) { |
48663c56 XL |
307 | // Do not print installation messages for up-to-date files. |
308 | // LLVM and LLD builds can produce a lot of those and hit CI limits on log size. | |
309 | cfg.define("CMAKE_INSTALL_MESSAGE", "LAZY"); | |
310 | ||
83c7162d | 311 | if builder.config.ninja { |
0531ce1d XL |
312 | cfg.generator("Ninja"); |
313 | } | |
314 | cfg.target(&target) | |
83c7162d | 315 | .host(&builder.config.build); |
0531ce1d XL |
316 | |
317 | let sanitize_cc = |cc: &Path| { | |
318 | if target.contains("msvc") { | |
319 | OsString::from(cc.to_str().unwrap().replace("\\", "/")) | |
320 | } else { | |
321 | cc.as_os_str().to_owned() | |
322 | } | |
323 | }; | |
324 | ||
325 | // MSVC with CMake uses msbuild by default which doesn't respect these | |
326 | // vars that we'd otherwise configure. In that case we just skip this | |
327 | // entirely. | |
83c7162d | 328 | if target.contains("msvc") && !builder.config.ninja { |
0531ce1d XL |
329 | return |
330 | } | |
331 | ||
94b46f34 XL |
332 | let (cc, cxx) = match builder.config.llvm_clang_cl { |
333 | Some(ref cl) => (cl.as_ref(), cl.as_ref()), | |
334 | None => (builder.cc(target), builder.cxx(target).unwrap()), | |
335 | }; | |
0531ce1d XL |
336 | |
337 | // Handle msvc + ninja + ccache specially (this is what the bots use) | |
338 | if target.contains("msvc") && | |
83c7162d | 339 | builder.config.ninja && |
94b46f34 XL |
340 | builder.config.ccache.is_some() |
341 | { | |
342 | let mut wrap_cc = env::current_exe().expect("failed to get cwd"); | |
343 | wrap_cc.set_file_name("sccache-plus-cl.exe"); | |
0531ce1d | 344 | |
94b46f34 XL |
345 | cfg.define("CMAKE_C_COMPILER", sanitize_cc(&wrap_cc)) |
346 | .define("CMAKE_CXX_COMPILER", sanitize_cc(&wrap_cc)); | |
0531ce1d | 347 | cfg.env("SCCACHE_PATH", |
83c7162d | 348 | builder.config.ccache.as_ref().unwrap()) |
94b46f34 XL |
349 | .env("SCCACHE_TARGET", target) |
350 | .env("SCCACHE_CC", &cc) | |
351 | .env("SCCACHE_CXX", &cxx); | |
352 | ||
353 | // Building LLVM on MSVC can be a little ludicrous at times. We're so far | |
354 | // off the beaten path here that I'm not really sure this is even half | |
355 | // supported any more. Here we're trying to: | |
356 | // | |
357 | // * Build LLVM on MSVC | |
358 | // * Build LLVM with `clang-cl` instead of `cl.exe` | |
359 | // * Build a project with `sccache` | |
360 | // * Build for 32-bit as well | |
361 | // * Build with Ninja | |
362 | // | |
363 | // For `cl.exe` there are different binaries to compile 32/64 bit which | |
364 | // we use but for `clang-cl` there's only one which internally | |
365 | // multiplexes via flags. As a result it appears that CMake's detection | |
366 | // of a compiler's architecture and such on MSVC **doesn't** pass any | |
367 | // custom flags we pass in CMAKE_CXX_FLAGS below. This means that if we | |
368 | // use `clang-cl.exe` it's always diagnosed as a 64-bit compiler which | |
369 | // definitely causes problems since all the env vars are pointing to | |
370 | // 32-bit libraries. | |
371 | // | |
a1dfa0c6 | 372 | // To hack around this... again... we pass an argument that's |
94b46f34 XL |
373 | // unconditionally passed in the sccache shim. This'll get CMake to |
374 | // correctly diagnose it's doing a 32-bit compilation and LLVM will | |
375 | // internally configure itself appropriately. | |
376 | if builder.config.llvm_clang_cl.is_some() && target.contains("i686") { | |
377 | cfg.env("SCCACHE_EXTRA_ARGS", "-m32"); | |
378 | } | |
0531ce1d | 379 | } else { |
0731742a XL |
380 | // If ccache is configured we inform the build a little differently how |
381 | // to invoke ccache while also invoking our compilers. | |
382 | if let Some(ref ccache) = builder.config.ccache { | |
383 | cfg.define("CMAKE_C_COMPILER_LAUNCHER", ccache) | |
384 | .define("CMAKE_CXX_COMPILER_LAUNCHER", ccache); | |
385 | } | |
0531ce1d XL |
386 | cfg.define("CMAKE_C_COMPILER", sanitize_cc(cc)) |
387 | .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx)); | |
388 | } | |
389 | ||
83c7162d | 390 | cfg.build_arg("-j").build_arg(builder.jobs().to_string()); |
9fa01778 | 391 | let mut cflags = builder.cflags(target, GitRepo::Llvm).join(" "); |
dc9dc135 | 392 | if let Some(ref s) = builder.config.llvm_cflags { |
9fa01778 XL |
393 | cflags.push_str(&format!(" {}", s)); |
394 | } | |
395 | cfg.define("CMAKE_C_FLAGS", cflags); | |
b7449926 | 396 | let mut cxxflags = builder.cflags(target, GitRepo::Llvm).join(" "); |
0731742a | 397 | if builder.config.llvm_static_stdcpp && |
e74abb32 | 398 | !target.contains("msvc") && |
0731742a XL |
399 | !target.contains("netbsd") |
400 | { | |
401 | cxxflags.push_str(" -static-libstdc++"); | |
0531ce1d | 402 | } |
9fa01778 XL |
403 | if let Some(ref s) = builder.config.llvm_cxxflags { |
404 | cxxflags.push_str(&format!(" {}", s)); | |
405 | } | |
0531ce1d | 406 | cfg.define("CMAKE_CXX_FLAGS", cxxflags); |
83c7162d | 407 | if let Some(ar) = builder.ar(target) { |
0531ce1d XL |
408 | if ar.is_absolute() { |
409 | // LLVM build breaks if `CMAKE_AR` is a relative path, for some reason it | |
410 | // tries to resolve this path in the LLVM build directory. | |
411 | cfg.define("CMAKE_AR", sanitize_cc(ar)); | |
412 | } | |
413 | } | |
414 | ||
b7449926 XL |
415 | if let Some(ranlib) = builder.ranlib(target) { |
416 | if ranlib.is_absolute() { | |
417 | // LLVM build breaks if `CMAKE_RANLIB` is a relative path, for some reason it | |
418 | // tries to resolve this path in the LLVM build directory. | |
419 | cfg.define("CMAKE_RANLIB", sanitize_cc(ranlib)); | |
420 | } | |
421 | } | |
422 | ||
9fa01778 XL |
423 | if let Some(ref s) = builder.config.llvm_ldflags { |
424 | cfg.define("CMAKE_SHARED_LINKER_FLAGS", s); | |
425 | cfg.define("CMAKE_MODULE_LINKER_FLAGS", s); | |
426 | cfg.define("CMAKE_EXE_LINKER_FLAGS", s); | |
427 | } | |
428 | ||
0531ce1d | 429 | if env::var_os("SCCACHE_ERROR_LOG").is_some() { |
48663c56 | 430 | cfg.env("RUSTC_LOG", "sccache=warn"); |
0531ce1d XL |
431 | } |
432 | } | |
433 | ||
434 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] | |
435 | pub struct Lld { | |
436 | pub target: Interned<String>, | |
437 | } | |
438 | ||
439 | impl Step for Lld { | |
440 | type Output = PathBuf; | |
441 | const ONLY_HOSTS: bool = true; | |
442 | ||
9fa01778 XL |
443 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
444 | run.path("src/llvm-project/lld").path("src/tools/lld") | |
0531ce1d XL |
445 | } |
446 | ||
9fa01778 | 447 | fn make_run(run: RunConfig<'_>) { |
0531ce1d XL |
448 | run.builder.ensure(Lld { target: run.target }); |
449 | } | |
450 | ||
451 | /// Compile LLVM for `target`. | |
9fa01778 | 452 | fn run(self, builder: &Builder<'_>) -> PathBuf { |
83c7162d XL |
453 | if builder.config.dry_run { |
454 | return PathBuf::from("lld-out-dir-test-gen"); | |
455 | } | |
0531ce1d | 456 | let target = self.target; |
0531ce1d XL |
457 | |
458 | let llvm_config = builder.ensure(Llvm { | |
459 | target: self.target, | |
0531ce1d XL |
460 | }); |
461 | ||
83c7162d | 462 | let out_dir = builder.lld_out(target); |
0531ce1d XL |
463 | let done_stamp = out_dir.join("lld-finished-building"); |
464 | if done_stamp.exists() { | |
465 | return out_dir | |
466 | } | |
467 | ||
83c7162d XL |
468 | builder.info(&format!("Building LLD for {}", target)); |
469 | let _time = util::timeit(&builder); | |
0531ce1d XL |
470 | t!(fs::create_dir_all(&out_dir)); |
471 | ||
9fa01778 | 472 | let mut cfg = cmake::Config::new(builder.src.join("src/llvm-project/lld")); |
0731742a | 473 | configure_cmake(builder, target, &mut cfg); |
0531ce1d | 474 | |
94b46f34 XL |
475 | // This is an awful, awful hack. Discovered when we migrated to using |
476 | // clang-cl to compile LLVM/LLD it turns out that LLD, when built out of | |
477 | // tree, will execute `llvm-config --cmakedir` and then tell CMake about | |
478 | // that directory for later processing. Unfortunately if this path has | |
479 | // forward slashes in it (which it basically always does on Windows) | |
480 | // then CMake will hit a syntax error later on as... something isn't | |
481 | // escaped it seems? | |
482 | // | |
483 | // Instead of attempting to fix this problem in upstream CMake and/or | |
484 | // LLVM/LLD we just hack around it here. This thin wrapper will take the | |
485 | // output from llvm-config and replace all instances of `\` with `/` to | |
486 | // ensure we don't hit the same bugs with escaping. It means that you | |
487 | // can't build on a system where your paths require `\` on Windows, but | |
488 | // there's probably a lot of reasons you can't do that other than this. | |
489 | let llvm_config_shim = env::current_exe() | |
490 | .unwrap() | |
491 | .with_file_name("llvm-config-wrapper"); | |
0531ce1d XL |
492 | cfg.out_dir(&out_dir) |
493 | .profile("Release") | |
94b46f34 XL |
494 | .env("LLVM_CONFIG_REAL", llvm_config) |
495 | .define("LLVM_CONFIG_PATH", llvm_config_shim) | |
0531ce1d XL |
496 | .define("LLVM_INCLUDE_TESTS", "OFF"); |
497 | ||
498 | cfg.build(); | |
499 | ||
500 | t!(File::create(&done_stamp)); | |
501 | out_dir | |
502 | } | |
503 | } | |
504 | ||
3b2f2976 XL |
505 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
506 | pub struct TestHelpers { | |
507 | pub target: Interned<String>, | |
508 | } | |
509 | ||
510 | impl Step for TestHelpers { | |
511 | type Output = (); | |
512 | ||
9fa01778 | 513 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
0531ce1d | 514 | run.path("src/test/auxiliary/rust_test_helpers.c") |
5bcae85e SL |
515 | } |
516 | ||
9fa01778 | 517 | fn make_run(run: RunConfig<'_>) { |
3b2f2976 | 518 | run.builder.ensure(TestHelpers { target: run.target }) |
476ff2be SL |
519 | } |
520 | ||
3b2f2976 | 521 | /// Compiles the `rust_test_helpers.c` library which we used in various |
416331ca | 522 | /// `run-pass` tests for ABI testing. |
9fa01778 | 523 | fn run(self, builder: &Builder<'_>) { |
83c7162d XL |
524 | if builder.config.dry_run { |
525 | return; | |
526 | } | |
3b2f2976 | 527 | let target = self.target; |
83c7162d XL |
528 | let dst = builder.test_helpers_out(target); |
529 | let src = builder.src.join("src/test/auxiliary/rust_test_helpers.c"); | |
3b2f2976 XL |
530 | if up_to_date(&src, &dst.join("librust_test_helpers.a")) { |
531 | return | |
532 | } | |
533 | ||
8faf50e0 | 534 | builder.info("Building test helpers"); |
3b2f2976 | 535 | t!(fs::create_dir_all(&dst)); |
ea8adc8c | 536 | let mut cfg = cc::Build::new(); |
e74abb32 XL |
537 | // FIXME: Workaround for https://github.com/emscripten-core/emscripten/issues/9013 |
538 | if target.contains("emscripten") { | |
539 | cfg.pic(false); | |
540 | } | |
3b2f2976 XL |
541 | |
542 | // We may have found various cross-compilers a little differently due to our | |
543 | // extra configuration, so inform gcc of these compilers. Note, though, that | |
544 | // on MSVC we still need gcc's detection of env vars (ugh). | |
545 | if !target.contains("msvc") { | |
83c7162d | 546 | if let Some(ar) = builder.ar(target) { |
3b2f2976 XL |
547 | cfg.archiver(ar); |
548 | } | |
83c7162d | 549 | cfg.compiler(builder.cc(target)); |
3b2f2976 XL |
550 | } |
551 | ||
552 | cfg.cargo_metadata(false) | |
553 | .out_dir(&dst) | |
554 | .target(&target) | |
83c7162d | 555 | .host(&builder.config.build) |
3b2f2976 | 556 | .opt_level(0) |
ea8adc8c | 557 | .warnings(false) |
3b2f2976 | 558 | .debug(false) |
83c7162d | 559 | .file(builder.src.join("src/test/auxiliary/rust_test_helpers.c")) |
ff7c6d11 | 560 | .compile("rust_test_helpers"); |
3b2f2976 | 561 | } |
5bcae85e | 562 | } |