]>
Commit | Line | Data |
---|---|---|
a7813a04 XL |
1 | //! Implementation of compiling various phases of the compiler and standard |
2 | //! library. | |
3 | //! | |
4 | //! This module contains some of the real meat in the rustbuild build system | |
136023e0 XL |
5 | //! which is where Cargo is used to compile the standard library, libtest, and |
6 | //! the compiler. This module is also responsible for assembling the sysroot as it | |
a7813a04 XL |
7 | //! goes along from the output of the previous stage. |
8 | ||
0531ce1d | 9 | use std::borrow::Cow; |
5869c6ff | 10 | use std::collections::HashSet; |
7cac9316 | 11 | use std::env; |
0731742a | 12 | use std::fs; |
7cac9316 | 13 | use std::io::prelude::*; |
dfeec247 | 14 | use std::io::BufReader; |
7453a54e | 15 | use std::path::{Path, PathBuf}; |
dfeec247 | 16 | use std::process::{exit, Command, Stdio}; |
7cac9316 | 17 | use std::str; |
7453a54e | 18 | |
e1599b0c | 19 | use build_helper::{output, t, up_to_date}; |
9e0c209e | 20 | use filetime::FileTime; |
48663c56 | 21 | use serde::Deserialize; |
7453a54e | 22 | |
e1599b0c | 23 | use crate::builder::Cargo; |
f035d41b XL |
24 | use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step}; |
25 | use crate::cache::{Interned, INTERNER}; | |
94222f64 | 26 | use crate::config::{LlvmLibunwind, TargetSelection}; |
dfeec247 | 27 | use crate::dist; |
0731742a | 28 | use crate::native; |
f035d41b | 29 | use crate::tool::SourceType; |
6a06907d | 30 | use crate::util::{exe, is_debug_info, is_dylib, symlink_dir}; |
a2a8927a | 31 | use crate::LLVM_TOOLS; |
5099ac24 | 32 | use crate::{CLang, Compiler, DependencyType, GitRepo, Mode}; |
3b2f2976 | 33 | |
83c7162d | 34 | #[derive(Debug, PartialOrd, Ord, Copy, Clone, PartialEq, Eq, Hash)] |
3b2f2976 | 35 | pub struct Std { |
3dfed10e | 36 | pub target: TargetSelection, |
3b2f2976 XL |
37 | pub compiler: Compiler, |
38 | } | |
39 | ||
40 | impl Step for Std { | |
41 | type Output = (); | |
42 | const DEFAULT: bool = true; | |
43 | ||
9fa01778 | 44 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
6a06907d XL |
45 | // When downloading stage1, the standard library has already been copied to the sysroot, so |
46 | // there's no need to rebuild it. | |
47 | let download_rustc = run.builder.config.download_rustc; | |
48 | run.all_krates("test").default_condition(!download_rustc) | |
3b2f2976 XL |
49 | } |
50 | ||
9fa01778 | 51 | fn make_run(run: RunConfig<'_>) { |
3b2f2976 | 52 | run.builder.ensure(Std { |
1b1a35ee | 53 | compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()), |
3b2f2976 XL |
54 | target: run.target, |
55 | }); | |
56 | } | |
57 | ||
9fa01778 | 58 | /// Builds the standard library. |
3b2f2976 XL |
59 | /// |
60 | /// This will build the standard library for a particular stage of the build | |
61 | /// using the `compiler` targeting the `target` architecture. The artifacts | |
62 | /// created will also be linked into the sysroot directory. | |
9fa01778 | 63 | fn run(self, builder: &Builder<'_>) { |
3b2f2976 XL |
64 | let target = self.target; |
65 | let compiler = self.compiler; | |
66 | ||
6a06907d XL |
67 | // These artifacts were already copied (in `impl Step for Sysroot`). |
68 | // Don't recompile them. | |
cdc7bbd5 XL |
69 | // NOTE: the ABI of the beta compiler is different from the ABI of the downloaded compiler, |
70 | // so its artifacts can't be reused. | |
71 | if builder.config.download_rustc && compiler.stage != 0 { | |
6a06907d XL |
72 | return; |
73 | } | |
74 | ||
1b1a35ee XL |
75 | if builder.config.keep_stage.contains(&compiler.stage) |
76 | || builder.config.keep_stage_std.contains(&compiler.stage) | |
77 | { | |
8faf50e0 | 78 | builder.info("Warning: Using a potentially old libstd. This may not behave well."); |
dfeec247 | 79 | builder.ensure(StdLink { compiler, target_compiler: compiler, target }); |
8faf50e0 XL |
80 | return; |
81 | } | |
82 | ||
136023e0 XL |
83 | builder.update_submodule(&Path::new("library").join("stdarch")); |
84 | ||
e74abb32 | 85 | let mut target_deps = builder.ensure(StartupObjects { compiler, target }); |
3b2f2976 | 86 | |
dc9dc135 XL |
87 | let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target); |
88 | if compiler_to_use != compiler { | |
dfeec247 | 89 | builder.ensure(Std { compiler: compiler_to_use, target }); |
dc9dc135 | 90 | builder.info(&format!("Uplifting stage1 std ({} -> {})", compiler_to_use.host, target)); |
3b2f2976 XL |
91 | |
92 | // Even if we're not building std this stage, the new sysroot must | |
0731742a XL |
93 | // still contain the third party objects needed by various targets. |
94 | copy_third_party_objects(builder, &compiler, target); | |
f035d41b | 95 | copy_self_contained_objects(builder, &compiler, target); |
3b2f2976 XL |
96 | |
97 | builder.ensure(StdLink { | |
dc9dc135 | 98 | compiler: compiler_to_use, |
3b2f2976 XL |
99 | target_compiler: compiler, |
100 | target, | |
101 | }); | |
102 | return; | |
103 | } | |
104 | ||
f035d41b XL |
105 | target_deps.extend(copy_third_party_objects(builder, &compiler, target)); |
106 | target_deps.extend(copy_self_contained_objects(builder, &compiler, target)); | |
3b2f2976 | 107 | |
f035d41b | 108 | let mut cargo = builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "build"); |
f9f354fc | 109 | std_cargo(builder, target, compiler.stage, &mut cargo); |
0531ce1d | 110 | |
dfeec247 XL |
111 | builder.info(&format!( |
112 | "Building stage{} std artifacts ({} -> {})", | |
113 | compiler.stage, &compiler.host, target | |
114 | )); | |
115 | run_cargo( | |
116 | builder, | |
117 | cargo, | |
118 | vec![], | |
119 | &libstd_stamp(builder, compiler, target), | |
120 | target_deps, | |
121 | false, | |
122 | ); | |
3b2f2976 XL |
123 | |
124 | builder.ensure(StdLink { | |
83c7162d | 125 | compiler: builder.compiler(compiler.stage, builder.config.build), |
3b2f2976 XL |
126 | target_compiler: compiler, |
127 | target, | |
128 | }); | |
129 | } | |
130 | } | |
131 | ||
f035d41b XL |
132 | fn copy_and_stamp( |
133 | builder: &Builder<'_>, | |
134 | libdir: &Path, | |
135 | sourcedir: &Path, | |
136 | name: &str, | |
137 | target_deps: &mut Vec<(PathBuf, DependencyType)>, | |
138 | dependency_type: DependencyType, | |
139 | ) { | |
140 | let target = libdir.join(name); | |
141 | builder.copy(&sourcedir.join(name), &target); | |
142 | ||
143 | target_deps.push((target, dependency_type)); | |
144 | } | |
145 | ||
94222f64 XL |
146 | fn copy_llvm_libunwind(builder: &Builder<'_>, target: TargetSelection, libdir: &Path) -> PathBuf { |
147 | let libunwind_path = builder.ensure(native::Libunwind { target }); | |
148 | let libunwind_source = libunwind_path.join("libunwind.a"); | |
149 | let libunwind_target = libdir.join("libunwind.a"); | |
150 | builder.copy(&libunwind_source, &libunwind_target); | |
151 | libunwind_target | |
152 | } | |
153 | ||
60c5eb7d | 154 | /// Copies third party objects needed by various targets. |
dfeec247 XL |
155 | fn copy_third_party_objects( |
156 | builder: &Builder<'_>, | |
157 | compiler: &Compiler, | |
3dfed10e | 158 | target: TargetSelection, |
f035d41b | 159 | ) -> Vec<(PathBuf, DependencyType)> { |
e74abb32 XL |
160 | let mut target_deps = vec![]; |
161 | ||
f035d41b XL |
162 | // FIXME: remove this in 2021 |
163 | if target == "x86_64-fortanix-unknown-sgx" { | |
164 | if env::var_os("X86_FORTANIX_SGX_LIBS").is_some() { | |
165 | builder.info("Warning: X86_FORTANIX_SGX_LIBS environment variable is ignored, libunwind is now compiled as part of rustbuild"); | |
166 | } | |
167 | } | |
168 | ||
29967ef6 | 169 | if builder.config.sanitizers_enabled(target) && compiler.stage != 0 { |
f035d41b XL |
170 | // The sanitizers are only copied in stage1 or above, |
171 | // to avoid creating dependency on LLVM. | |
172 | target_deps.extend( | |
173 | copy_sanitizers(builder, &compiler, target) | |
174 | .into_iter() | |
175 | .map(|d| (d, DependencyType::Target)), | |
176 | ); | |
177 | } | |
178 | ||
94222f64 XL |
179 | if target == "x86_64-fortanix-unknown-sgx" |
180 | || builder.config.llvm_libunwind == LlvmLibunwind::InTree | |
181 | && (target.contains("linux") || target.contains("fuchsia")) | |
182 | { | |
183 | let libunwind_path = | |
184 | copy_llvm_libunwind(builder, target, &builder.sysroot_libdir(*compiler, target)); | |
185 | target_deps.push((libunwind_path, DependencyType::Target)); | |
186 | } | |
187 | ||
f035d41b XL |
188 | target_deps |
189 | } | |
190 | ||
191 | /// Copies third party objects needed by various targets for self-contained linkage. | |
192 | fn copy_self_contained_objects( | |
193 | builder: &Builder<'_>, | |
194 | compiler: &Compiler, | |
3dfed10e | 195 | target: TargetSelection, |
f035d41b | 196 | ) -> Vec<(PathBuf, DependencyType)> { |
3dfed10e | 197 | let libdir_self_contained = builder.sysroot_libdir(*compiler, target).join("self-contained"); |
f035d41b XL |
198 | t!(fs::create_dir_all(&libdir_self_contained)); |
199 | let mut target_deps = vec![]; | |
e74abb32 | 200 | |
3c0e092e | 201 | // Copies the libc and CRT objects. |
0731742a | 202 | // |
f9f354fc XL |
203 | // rustc historically provides a more self-contained installation for musl targets |
204 | // not requiring the presence of a native musl toolchain. For example, it can fall back | |
205 | // to using gcc from a glibc-targeting toolchain for linking. | |
206 | // To do that we have to distribute musl startup objects as a part of Rust toolchain | |
207 | // and link with them manually in the self-contained mode. | |
0731742a | 208 | if target.contains("musl") { |
6a06907d XL |
209 | let srcdir = builder.musl_libdir(target).unwrap_or_else(|| { |
210 | panic!("Target {:?} does not have a \"musl-libdir\" key", target.triple) | |
211 | }); | |
3c0e092e | 212 | for &obj in &["libc.a", "crt1.o", "Scrt1.o", "rcrt1.o", "crti.o", "crtn.o"] { |
f035d41b XL |
213 | copy_and_stamp( |
214 | builder, | |
215 | &libdir_self_contained, | |
216 | &srcdir, | |
217 | obj, | |
218 | &mut target_deps, | |
219 | DependencyType::TargetSelfContained, | |
220 | ); | |
0731742a | 221 | } |
cdc7bbd5 | 222 | let crt_path = builder.ensure(native::CrtBeginEnd { target }); |
6a06907d | 223 | for &obj in &["crtbegin.o", "crtbeginS.o", "crtend.o", "crtendS.o"] { |
cdc7bbd5 | 224 | let src = crt_path.join(obj); |
6a06907d XL |
225 | let target = libdir_self_contained.join(obj); |
226 | builder.copy(&src, &target); | |
227 | target_deps.push((target, DependencyType::TargetSelfContained)); | |
228 | } | |
94222f64 | 229 | |
5099ac24 FG |
230 | if !target.starts_with("s390x") { |
231 | let libunwind_path = copy_llvm_libunwind(builder, target, &libdir_self_contained); | |
232 | target_deps.push((libunwind_path, DependencyType::TargetSelfContained)); | |
233 | } | |
532ac7d7 | 234 | } else if target.ends_with("-wasi") { |
6a06907d XL |
235 | let srcdir = builder |
236 | .wasi_root(target) | |
237 | .unwrap_or_else(|| { | |
238 | panic!("Target {:?} does not have a \"wasi-root\" key", target.triple) | |
239 | }) | |
240 | .join("lib/wasm32-wasi"); | |
3c0e092e | 241 | for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] { |
5869c6ff XL |
242 | copy_and_stamp( |
243 | builder, | |
244 | &libdir_self_contained, | |
245 | &srcdir, | |
246 | obj, | |
247 | &mut target_deps, | |
248 | DependencyType::TargetSelfContained, | |
249 | ); | |
250 | } | |
f035d41b XL |
251 | } else if target.contains("windows-gnu") { |
252 | for obj in ["crt2.o", "dllcrt2.o"].iter() { | |
5099ac24 | 253 | let src = compiler_file(builder, builder.cc(target), target, CLang::C, obj); |
f035d41b XL |
254 | let target = libdir_self_contained.join(obj); |
255 | builder.copy(&src, &target); | |
256 | target_deps.push((target, DependencyType::TargetSelfContained)); | |
257 | } | |
dfeec247 XL |
258 | } |
259 | ||
e74abb32 | 260 | target_deps |
3b2f2976 XL |
261 | } |
262 | ||
263 | /// Configure cargo to compile the standard library, adding appropriate env vars | |
264 | /// and such. | |
3dfed10e | 265 | pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, cargo: &mut Cargo) { |
041b39d2 | 266 | if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") { |
8bb4bdeb XL |
267 | cargo.env("MACOSX_DEPLOYMENT_TARGET", target); |
268 | } | |
269 | ||
dc9dc135 XL |
270 | // Determine if we're going to compile in optimized C intrinsics to |
271 | // the `compiler-builtins` crate. These intrinsics live in LLVM's | |
272 | // `compiler-rt` repository, but our `src/llvm-project` submodule isn't | |
273 | // always checked out, so we need to conditionally look for this. (e.g. if | |
274 | // an external LLVM is used we skip the LLVM submodule checkout). | |
275 | // | |
276 | // Note that this shouldn't affect the correctness of `compiler-builtins`, | |
277 | // but only its speed. Some intrinsics in C haven't been translated to Rust | |
278 | // yet but that's pretty rare. Other intrinsics have optimized | |
279 | // implementations in C which have only had slower versions ported to Rust, | |
280 | // so we favor the C version where we can, but it's not critical. | |
281 | // | |
282 | // If `compiler-rt` is available ensure that the `c` feature of the | |
283 | // `compiler-builtins` crate is enabled and it's configured to learn where | |
284 | // `compiler-rt` is located. | |
285 | let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt"); | |
286 | let compiler_builtins_c_feature = if compiler_builtins_root.exists() { | |
ba9703b0 XL |
287 | // Note that `libprofiler_builtins/build.rs` also computes this so if |
288 | // you're changing something here please also change that. | |
dc9dc135 | 289 | cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root); |
29967ef6 | 290 | " compiler-builtins-c" |
dc9dc135 | 291 | } else { |
29967ef6 | 292 | "" |
dc9dc135 XL |
293 | }; |
294 | ||
83c7162d | 295 | if builder.no_std(target) == Some(true) { |
dc9dc135 | 296 | let mut features = "compiler-builtins-mem".to_string(); |
17df50a5 XL |
297 | if !target.starts_with("bpf") { |
298 | features.push_str(compiler_builtins_c_feature); | |
299 | } | |
dc9dc135 | 300 | |
83c7162d | 301 | // for no-std targets we only compile a few no_std crates |
0731742a | 302 | cargo |
83c7162d | 303 | .args(&["-p", "alloc"]) |
83c7162d | 304 | .arg("--manifest-path") |
3dfed10e | 305 | .arg(builder.src.join("library/alloc/Cargo.toml")) |
0731742a | 306 | .arg("--features") |
29967ef6 | 307 | .arg(features); |
83c7162d | 308 | } else { |
29967ef6 XL |
309 | let mut features = builder.std_features(target); |
310 | features.push_str(compiler_builtins_c_feature); | |
8bb4bdeb | 311 | |
dfeec247 XL |
312 | cargo |
313 | .arg("--features") | |
314 | .arg(features) | |
83c7162d | 315 | .arg("--manifest-path") |
3dfed10e | 316 | .arg(builder.src.join("library/test/Cargo.toml")); |
7453a54e | 317 | |
e1599b0c XL |
318 | // Help the libc crate compile by assisting it in finding various |
319 | // sysroot native libraries. | |
83c7162d | 320 | if target.contains("musl") { |
f035d41b XL |
321 | if let Some(p) = builder.musl_libdir(target) { |
322 | let root = format!("native={}", p.to_str().unwrap()); | |
e1599b0c | 323 | cargo.rustflag("-L").rustflag(&root); |
83c7162d | 324 | } |
7453a54e | 325 | } |
532ac7d7 XL |
326 | |
327 | if target.ends_with("-wasi") { | |
328 | if let Some(p) = builder.wasi_root(target) { | |
e1599b0c XL |
329 | let root = format!("native={}/lib/wasm32-wasi", p.to_str().unwrap()); |
330 | cargo.rustflag("-L").rustflag(&root); | |
532ac7d7 XL |
331 | } |
332 | } | |
7453a54e | 333 | } |
f9f354fc XL |
334 | |
335 | // By default, rustc uses `-Cembed-bitcode=yes`, and Cargo overrides that | |
336 | // with `-Cembed-bitcode=no` for non-LTO builds. However, libstd must be | |
337 | // built with bitcode so that the produced rlibs can be used for both LTO | |
338 | // builds (which use bitcode) and non-LTO builds (which use object code). | |
339 | // So we override the override here! | |
340 | // | |
341 | // But we don't bother for the stage 0 compiler because it's never used | |
342 | // with LTO. | |
343 | if stage >= 1 { | |
344 | cargo.rustflag("-Cembed-bitcode=yes"); | |
345 | } | |
f035d41b XL |
346 | |
347 | // By default, rustc does not include unwind tables unless they are required | |
348 | // for a particular target. They are not required by RISC-V targets, but | |
349 | // compiling the standard library with them means that users can get | |
350 | // backtraces without having to recompile the standard library themselves. | |
351 | // | |
352 | // This choice was discussed in https://github.com/rust-lang/rust/pull/69890 | |
353 | if target.contains("riscv") { | |
354 | cargo.rustflag("-Cforce-unwind-tables=yes"); | |
355 | } | |
17df50a5 XL |
356 | |
357 | let html_root = | |
358 | format!("-Zcrate-attr=doc(html_root_url=\"{}/\")", builder.doc_rust_lang_org_channel(),); | |
359 | cargo.rustflag(&html_root); | |
360 | cargo.rustdocflag(&html_root); | |
7453a54e SL |
361 | } |
362 | ||
3b2f2976 XL |
363 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
364 | struct StdLink { | |
365 | pub compiler: Compiler, | |
366 | pub target_compiler: Compiler, | |
3dfed10e | 367 | pub target: TargetSelection, |
54a0048b SL |
368 | } |
369 | ||
3b2f2976 XL |
370 | impl Step for StdLink { |
371 | type Output = (); | |
372 | ||
9fa01778 | 373 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
3b2f2976 XL |
374 | run.never() |
375 | } | |
376 | ||
377 | /// Link all libstd rlibs/dylibs into the sysroot location. | |
378 | /// | |
a1dfa0c6 | 379 | /// Links those artifacts generated by `compiler` to the `stage` compiler's |
3b2f2976 XL |
380 | /// sysroot for the specified `host` and `target`. |
381 | /// | |
382 | /// Note that this assumes that `compiler` has already generated the libstd | |
383 | /// libraries for `target`, and this method will find them in the relevant | |
384 | /// output directory. | |
9fa01778 | 385 | fn run(self, builder: &Builder<'_>) { |
3b2f2976 XL |
386 | let compiler = self.compiler; |
387 | let target_compiler = self.target_compiler; | |
388 | let target = self.target; | |
dfeec247 XL |
389 | builder.info(&format!( |
390 | "Copying stage{} std from stage{} ({} -> {} / {})", | |
391 | target_compiler.stage, compiler.stage, &compiler.host, target_compiler.host, target | |
392 | )); | |
3b2f2976 | 393 | let libdir = builder.sysroot_libdir(target_compiler, target); |
532ac7d7 XL |
394 | let hostdir = builder.sysroot_libdir(target_compiler, compiler.host); |
395 | add_to_sysroot(builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target)); | |
54a0048b | 396 | } |
7453a54e SL |
397 | } |
398 | ||
dfeec247 XL |
399 | /// Copies sanitizer runtime libraries into target libdir. |
400 | fn copy_sanitizers( | |
9fa01778 | 401 | builder: &Builder<'_>, |
dfeec247 | 402 | compiler: &Compiler, |
3dfed10e | 403 | target: TargetSelection, |
dfeec247 XL |
404 | ) -> Vec<PathBuf> { |
405 | let runtimes: Vec<native::SanitizerRuntime> = builder.ensure(native::Sanitizers { target }); | |
406 | ||
407 | if builder.config.dry_run { | |
408 | return Vec::new(); | |
409 | } | |
410 | ||
411 | let mut target_deps = Vec::new(); | |
412 | let libdir = builder.sysroot_libdir(*compiler, target); | |
413 | ||
414 | for runtime in &runtimes { | |
415 | let dst = libdir.join(&runtime.name); | |
416 | builder.copy(&runtime.path, &dst); | |
417 | ||
5869c6ff XL |
418 | if target == "x86_64-apple-darwin" || target == "aarch64-apple-darwin" { |
419 | // Update the library’s install name to reflect that it has has been renamed. | |
420 | apple_darwin_update_library_name(&dst, &format!("@rpath/{}", &runtime.name)); | |
421 | // Upon renaming the install name, the code signature of the file will invalidate, | |
422 | // so we will sign it again. | |
423 | apple_darwin_sign_file(&dst); | |
dfeec247 XL |
424 | } |
425 | ||
426 | target_deps.push(dst); | |
7cac9316 | 427 | } |
dfeec247 XL |
428 | |
429 | target_deps | |
7cac9316 XL |
430 | } |
431 | ||
5869c6ff XL |
432 | fn apple_darwin_update_library_name(library_path: &Path, new_name: &str) { |
433 | let status = Command::new("install_name_tool") | |
434 | .arg("-id") | |
435 | .arg(new_name) | |
436 | .arg(library_path) | |
437 | .status() | |
438 | .expect("failed to execute `install_name_tool`"); | |
439 | assert!(status.success()); | |
440 | } | |
441 | ||
442 | fn apple_darwin_sign_file(file_path: &Path) { | |
443 | let status = Command::new("codesign") | |
444 | .arg("-f") // Force to rewrite the existing signature | |
445 | .arg("-s") | |
446 | .arg("-") | |
447 | .arg(file_path) | |
448 | .status() | |
449 | .expect("failed to execute `codesign`"); | |
450 | assert!(status.success()); | |
451 | } | |
452 | ||
3b2f2976 XL |
453 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
454 | pub struct StartupObjects { | |
455 | pub compiler: Compiler, | |
3dfed10e | 456 | pub target: TargetSelection, |
3b2f2976 XL |
457 | } |
458 | ||
459 | impl Step for StartupObjects { | |
f035d41b | 460 | type Output = Vec<(PathBuf, DependencyType)>; |
3b2f2976 | 461 | |
9fa01778 | 462 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
3dfed10e | 463 | run.path("library/rtstartup") |
3b2f2976 XL |
464 | } |
465 | ||
9fa01778 | 466 | fn make_run(run: RunConfig<'_>) { |
3b2f2976 | 467 | run.builder.ensure(StartupObjects { |
1b1a35ee | 468 | compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()), |
3b2f2976 XL |
469 | target: run.target, |
470 | }); | |
471 | } | |
472 | ||
9fa01778 | 473 | /// Builds and prepare startup objects like rsbegin.o and rsend.o |
3b2f2976 XL |
474 | /// |
475 | /// These are primarily used on Windows right now for linking executables/dlls. | |
476 | /// They don't require any library support as they're just plain old object | |
477 | /// files, so we just use the nightly snapshot compiler to always build them (as | |
478 | /// no other compilers are guaranteed to be available). | |
f035d41b | 479 | fn run(self, builder: &Builder<'_>) -> Vec<(PathBuf, DependencyType)> { |
3b2f2976 XL |
480 | let for_compiler = self.compiler; |
481 | let target = self.target; | |
416331ca | 482 | if !target.contains("windows-gnu") { |
dfeec247 | 483 | return vec![]; |
8bb4bdeb | 484 | } |
7453a54e | 485 | |
e74abb32 XL |
486 | let mut target_deps = vec![]; |
487 | ||
3dfed10e | 488 | let src_dir = &builder.src.join("library").join("rtstartup"); |
83c7162d | 489 | let dst_dir = &builder.native_dir(target).join("rtstartup"); |
3b2f2976 XL |
490 | let sysroot_dir = &builder.sysroot_libdir(for_compiler, target); |
491 | t!(fs::create_dir_all(dst_dir)); | |
492 | ||
493 | for file in &["rsbegin", "rsend"] { | |
494 | let src_file = &src_dir.join(file.to_string() + ".rs"); | |
495 | let dst_file = &dst_dir.join(file.to_string() + ".o"); | |
496 | if !up_to_date(src_file, dst_file) { | |
83c7162d | 497 | let mut cmd = Command::new(&builder.initial_rustc); |
cdc7bbd5 XL |
498 | cmd.env("RUSTC_BOOTSTRAP", "1"); |
499 | if !builder.local_rebuild { | |
500 | // a local_rebuild compiler already has stage1 features | |
501 | cmd.arg("--cfg").arg("bootstrap"); | |
502 | } | |
dfeec247 | 503 | builder.run( |
cdc7bbd5 | 504 | cmd.arg("--target") |
3dfed10e | 505 | .arg(target.rustc_target_arg()) |
dfeec247 XL |
506 | .arg("--emit=obj") |
507 | .arg("-o") | |
508 | .arg(dst_file) | |
509 | .arg(src_file), | |
510 | ); | |
3b2f2976 XL |
511 | } |
512 | ||
74b04a01 | 513 | let target = sysroot_dir.join((*file).to_string() + ".o"); |
e74abb32 | 514 | builder.copy(dst_file, &target); |
f035d41b | 515 | target_deps.push((target, DependencyType::Target)); |
3b2f2976 | 516 | } |
e74abb32 XL |
517 | |
518 | target_deps | |
7453a54e | 519 | } |
3b2f2976 XL |
520 | } |
521 | ||
83c7162d | 522 | #[derive(Debug, PartialOrd, Ord, Copy, Clone, PartialEq, Eq, Hash)] |
3b2f2976 | 523 | pub struct Rustc { |
3dfed10e | 524 | pub target: TargetSelection, |
83c7162d | 525 | pub compiler: Compiler, |
3b2f2976 XL |
526 | } |
527 | ||
528 | impl Step for Rustc { | |
529 | type Output = (); | |
530 | const ONLY_HOSTS: bool = true; | |
3dfed10e | 531 | const DEFAULT: bool = false; |
3b2f2976 | 532 | |
9fa01778 | 533 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
c295e0f8 | 534 | run.never() |
3b2f2976 XL |
535 | } |
536 | ||
9fa01778 | 537 | fn make_run(run: RunConfig<'_>) { |
3b2f2976 | 538 | run.builder.ensure(Rustc { |
1b1a35ee | 539 | compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()), |
3b2f2976 XL |
540 | target: run.target, |
541 | }); | |
542 | } | |
543 | ||
9fa01778 | 544 | /// Builds the compiler. |
3b2f2976 XL |
545 | /// |
546 | /// This will build the compiler for a particular stage of the build using | |
547 | /// the `compiler` targeting the `target` architecture. The artifacts | |
548 | /// created will also be linked into the sysroot directory. | |
9fa01778 | 549 | fn run(self, builder: &Builder<'_>) { |
3b2f2976 XL |
550 | let compiler = self.compiler; |
551 | let target = self.target; | |
552 | ||
cdc7bbd5 XL |
553 | // NOTE: the ABI of the beta compiler is different from the ABI of the downloaded compiler, |
554 | // so its artifacts can't be reused. | |
555 | if builder.config.download_rustc && compiler.stage != 0 { | |
6a06907d XL |
556 | // Copy the existing artifacts instead of rebuilding them. |
557 | // NOTE: this path is only taken for tools linking to rustc-dev. | |
558 | builder.ensure(Sysroot { compiler }); | |
559 | return; | |
560 | } | |
561 | ||
e1599b0c | 562 | builder.ensure(Std { compiler, target }); |
3b2f2976 | 563 | |
8faf50e0 XL |
564 | if builder.config.keep_stage.contains(&compiler.stage) { |
565 | builder.info("Warning: Using a potentially old librustc. This may not behave well."); | |
1b1a35ee | 566 | builder.info("Warning: Use `--keep-stage-std` if you want to rebuild the compiler when it changes"); |
dfeec247 | 567 | builder.ensure(RustcLink { compiler, target_compiler: compiler, target }); |
8faf50e0 XL |
568 | return; |
569 | } | |
570 | ||
dc9dc135 XL |
571 | let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target); |
572 | if compiler_to_use != compiler { | |
dfeec247 XL |
573 | builder.ensure(Rustc { compiler: compiler_to_use, target }); |
574 | builder | |
575 | .info(&format!("Uplifting stage1 rustc ({} -> {})", builder.config.build, target)); | |
3b2f2976 | 576 | builder.ensure(RustcLink { |
dc9dc135 | 577 | compiler: compiler_to_use, |
3b2f2976 XL |
578 | target_compiler: compiler, |
579 | target, | |
580 | }); | |
581 | return; | |
582 | } | |
583 | ||
532ac7d7 | 584 | // Ensure that build scripts and proc macros have a std / libproc_macro to link against. |
e1599b0c | 585 | builder.ensure(Std { |
83c7162d XL |
586 | compiler: builder.compiler(self.compiler.stage, builder.config.build), |
587 | target: builder.config.build, | |
3b2f2976 | 588 | }); |
0531ce1d | 589 | |
f035d41b | 590 | let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "build"); |
60c5eb7d | 591 | rustc_cargo(builder, &mut cargo, target); |
3b2f2976 | 592 | |
fc512014 XL |
593 | if builder.config.rust_profile_use.is_some() |
594 | && builder.config.rust_profile_generate.is_some() | |
595 | { | |
596 | panic!("Cannot use and generate PGO profiles at the same time"); | |
597 | } | |
598 | ||
599 | let is_collecting = if let Some(path) = &builder.config.rust_profile_generate { | |
600 | if compiler.stage == 1 { | |
601 | cargo.rustflag(&format!("-Cprofile-generate={}", path)); | |
602 | // Apparently necessary to avoid overflowing the counters during | |
603 | // a Cargo build profile | |
604 | cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4"); | |
605 | true | |
606 | } else { | |
607 | false | |
608 | } | |
609 | } else if let Some(path) = &builder.config.rust_profile_use { | |
610 | if compiler.stage == 1 { | |
611 | cargo.rustflag(&format!("-Cprofile-use={}", path)); | |
612 | cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function"); | |
613 | true | |
614 | } else { | |
615 | false | |
616 | } | |
617 | } else { | |
618 | false | |
619 | }; | |
620 | if is_collecting { | |
621 | // Ensure paths to Rust sources are relative, not absolute. | |
622 | cargo.rustflag(&format!( | |
623 | "-Cllvm-args=-static-func-strip-dirname-prefix={}", | |
624 | builder.config.src.components().count() | |
625 | )); | |
626 | } | |
627 | ||
dfeec247 XL |
628 | builder.info(&format!( |
629 | "Building stage{} compiler artifacts ({} -> {})", | |
630 | compiler.stage, &compiler.host, target | |
631 | )); | |
632 | run_cargo( | |
633 | builder, | |
634 | cargo, | |
635 | vec![], | |
636 | &librustc_stamp(builder, compiler, target), | |
637 | vec![], | |
638 | false, | |
639 | ); | |
3b2f2976 XL |
640 | |
641 | builder.ensure(RustcLink { | |
83c7162d | 642 | compiler: builder.compiler(compiler.stage, builder.config.build), |
3b2f2976 XL |
643 | target_compiler: compiler, |
644 | target, | |
645 | }); | |
646 | } | |
647 | } | |
648 | ||
3dfed10e | 649 | pub fn rustc_cargo(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) { |
dfeec247 XL |
650 | cargo |
651 | .arg("--features") | |
652 | .arg(builder.rustc_features()) | |
653 | .arg("--manifest-path") | |
1b1a35ee | 654 | .arg(builder.src.join("compiler/rustc/Cargo.toml")); |
60c5eb7d | 655 | rustc_cargo_env(builder, cargo, target); |
2c00a5a8 | 656 | } |
7453a54e | 657 | |
3dfed10e | 658 | pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) { |
7453a54e SL |
659 | // Set some configuration variables picked up by build scripts and |
660 | // the compiler alike | |
dfeec247 XL |
661 | cargo |
662 | .env("CFG_RELEASE", builder.rust_release()) | |
663 | .env("CFG_RELEASE_CHANNEL", &builder.config.channel) | |
17df50a5 | 664 | .env("CFG_VERSION", builder.rust_version()); |
32a655c1 | 665 | |
74b04a01 | 666 | let libdir_relative = builder.config.libdir_relative().unwrap_or_else(|| Path::new("lib")); |
a2a8927a XL |
667 | let target_config = builder.config.target_config.get(&target); |
668 | ||
2c00a5a8 | 669 | cargo.env("CFG_LIBDIR_RELATIVE", libdir_relative); |
7453a54e | 670 | |
83c7162d | 671 | if let Some(ref ver_date) = builder.rust_info.commit_date() { |
7453a54e SL |
672 | cargo.env("CFG_VER_DATE", ver_date); |
673 | } | |
83c7162d | 674 | if let Some(ref ver_hash) = builder.rust_info.sha() { |
7453a54e SL |
675 | cargo.env("CFG_VER_HASH", ver_hash); |
676 | } | |
83c7162d | 677 | if !builder.unstable_features() { |
7453a54e SL |
678 | cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1"); |
679 | } | |
a2a8927a XL |
680 | |
681 | // Prefer the current target's own default_linker, else a globally | |
682 | // specified one. | |
683 | if let Some(s) = target_config.and_then(|c| c.default_linker.as_ref()) { | |
684 | cargo.env("CFG_DEFAULT_LINKER", s); | |
685 | } else if let Some(ref s) = builder.config.rustc_default_linker { | |
7453a54e SL |
686 | cargo.env("CFG_DEFAULT_LINKER", s); |
687 | } | |
a2a8927a | 688 | |
9fa01778 | 689 | if builder.config.rustc_parallel { |
e1599b0c | 690 | cargo.rustflag("--cfg=parallel_compiler"); |
17df50a5 | 691 | cargo.rustdocflag("--cfg=parallel_compiler"); |
ff7c6d11 | 692 | } |
0bf4aa26 XL |
693 | if builder.config.rust_verify_llvm_ir { |
694 | cargo.env("RUSTC_VERIFY_LLVM_IR", "1"); | |
695 | } | |
60c5eb7d XL |
696 | |
697 | // Pass down configuration from the LLVM build into the build of | |
1b1a35ee | 698 | // rustc_llvm and rustc_codegen_llvm. |
60c5eb7d XL |
699 | // |
700 | // Note that this is disabled if LLVM itself is disabled or we're in a check | |
f9f354fc XL |
701 | // build. If we are in a check build we still go ahead here presuming we've |
702 | // detected that LLVM is alreay built and good to go which helps prevent | |
703 | // busting caches (e.g. like #71152). | |
704 | if builder.config.llvm_enabled() | |
705 | && (builder.kind != Kind::Check | |
706 | || crate::native::prebuilt_llvm_config(builder, target).is_ok()) | |
707 | { | |
60c5eb7d XL |
708 | if builder.is_rust_llvm(target) { |
709 | cargo.env("LLVM_RUSTLLVM", "1"); | |
710 | } | |
711 | let llvm_config = builder.ensure(native::Llvm { target }); | |
712 | cargo.env("LLVM_CONFIG", &llvm_config); | |
60c5eb7d XL |
713 | if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) { |
714 | cargo.env("CFG_LLVM_ROOT", s); | |
715 | } | |
1b1a35ee | 716 | // Some LLVM linker flags (-L and -l) may be needed to link rustc_llvm. |
60c5eb7d XL |
717 | if let Some(ref s) = builder.config.llvm_ldflags { |
718 | cargo.env("LLVM_LINKER_FLAGS", s); | |
719 | } | |
720 | // Building with a static libstdc++ is only supported on linux right now, | |
721 | // not for MSVC or macOS | |
dfeec247 XL |
722 | if builder.config.llvm_static_stdcpp |
723 | && !target.contains("freebsd") | |
724 | && !target.contains("msvc") | |
725 | && !target.contains("apple") | |
726 | { | |
5099ac24 FG |
727 | let file = compiler_file( |
728 | builder, | |
729 | builder.cxx(target).unwrap(), | |
730 | target, | |
731 | CLang::Cxx, | |
732 | "libstdc++.a", | |
733 | ); | |
60c5eb7d XL |
734 | cargo.env("LLVM_STATIC_STDCPP", file); |
735 | } | |
1b1a35ee | 736 | if builder.config.llvm_link_shared { |
60c5eb7d XL |
737 | cargo.env("LLVM_LINK_SHARED", "1"); |
738 | } | |
739 | if builder.config.llvm_use_libcxx { | |
740 | cargo.env("LLVM_USE_LIBCXX", "1"); | |
741 | } | |
742 | if builder.config.llvm_optimize && !builder.config.llvm_release_debuginfo { | |
743 | cargo.env("LLVM_NDEBUG", "1"); | |
744 | } | |
745 | } | |
7453a54e SL |
746 | } |
747 | ||
3b2f2976 XL |
748 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
749 | struct RustcLink { | |
750 | pub compiler: Compiler, | |
751 | pub target_compiler: Compiler, | |
3dfed10e | 752 | pub target: TargetSelection, |
3b2f2976 XL |
753 | } |
754 | ||
755 | impl Step for RustcLink { | |
756 | type Output = (); | |
757 | ||
9fa01778 | 758 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
3b2f2976 XL |
759 | run.never() |
760 | } | |
761 | ||
762 | /// Same as `std_link`, only for librustc | |
9fa01778 | 763 | fn run(self, builder: &Builder<'_>) { |
3b2f2976 XL |
764 | let compiler = self.compiler; |
765 | let target_compiler = self.target_compiler; | |
766 | let target = self.target; | |
dfeec247 XL |
767 | builder.info(&format!( |
768 | "Copying stage{} rustc from stage{} ({} -> {} / {})", | |
769 | target_compiler.stage, compiler.stage, &compiler.host, target_compiler.host, target | |
770 | )); | |
532ac7d7 XL |
771 | add_to_sysroot( |
772 | builder, | |
773 | &builder.sysroot_libdir(target_compiler, target), | |
774 | &builder.sysroot_libdir(target_compiler, compiler.host), | |
dfeec247 | 775 | &librustc_stamp(builder, compiler, target), |
532ac7d7 | 776 | ); |
3b2f2976 | 777 | } |
7453a54e SL |
778 | } |
779 | ||
29967ef6 XL |
780 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
781 | pub struct CodegenBackend { | |
782 | pub target: TargetSelection, | |
783 | pub compiler: Compiler, | |
784 | pub backend: Interned<String>, | |
785 | } | |
786 | ||
787 | impl Step for CodegenBackend { | |
788 | type Output = (); | |
789 | const ONLY_HOSTS: bool = true; | |
790 | // Only the backends specified in the `codegen-backends` entry of `config.toml` are built. | |
791 | const DEFAULT: bool = true; | |
792 | ||
793 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { | |
794 | run.path("compiler/rustc_codegen_cranelift") | |
795 | } | |
796 | ||
797 | fn make_run(run: RunConfig<'_>) { | |
798 | for &backend in &run.builder.config.rust_codegen_backends { | |
799 | if backend == "llvm" { | |
800 | continue; // Already built as part of rustc | |
801 | } | |
802 | ||
803 | run.builder.ensure(CodegenBackend { | |
804 | target: run.target, | |
805 | compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()), | |
806 | backend, | |
807 | }); | |
808 | } | |
809 | } | |
810 | ||
811 | fn run(self, builder: &Builder<'_>) { | |
812 | let compiler = self.compiler; | |
813 | let target = self.target; | |
814 | let backend = self.backend; | |
815 | ||
816 | builder.ensure(Rustc { compiler, target }); | |
817 | ||
818 | if builder.config.keep_stage.contains(&compiler.stage) { | |
819 | builder.info( | |
820 | "Warning: Using a potentially old codegen backend. \ | |
821 | This may not behave well.", | |
822 | ); | |
823 | // Codegen backends are linked separately from this step today, so we don't do | |
824 | // anything here. | |
825 | return; | |
826 | } | |
827 | ||
828 | let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target); | |
829 | if compiler_to_use != compiler { | |
830 | builder.ensure(CodegenBackend { compiler: compiler_to_use, target, backend }); | |
831 | return; | |
832 | } | |
833 | ||
834 | let out_dir = builder.cargo_out(compiler, Mode::Codegen, target); | |
835 | ||
c295e0f8 | 836 | let mut cargo = builder.cargo(compiler, Mode::Codegen, SourceType::InTree, target, "build"); |
29967ef6 XL |
837 | cargo |
838 | .arg("--manifest-path") | |
839 | .arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend))); | |
840 | rustc_cargo_env(builder, &mut cargo, target); | |
841 | ||
842 | let tmp_stamp = out_dir.join(".tmp.stamp"); | |
843 | ||
94222f64 XL |
844 | builder.info(&format!( |
845 | "Building stage{} codegen backend {} ({} -> {})", | |
846 | compiler.stage, backend, &compiler.host, target | |
847 | )); | |
29967ef6 XL |
848 | let files = run_cargo(builder, cargo, vec![], &tmp_stamp, vec![], false); |
849 | if builder.config.dry_run { | |
850 | return; | |
851 | } | |
852 | let mut files = files.into_iter().filter(|f| { | |
853 | let filename = f.file_name().unwrap().to_str().unwrap(); | |
854 | is_dylib(filename) && filename.contains("rustc_codegen_") | |
855 | }); | |
856 | let codegen_backend = match files.next() { | |
857 | Some(f) => f, | |
858 | None => panic!("no dylibs built for codegen backend?"), | |
859 | }; | |
860 | if let Some(f) = files.next() { | |
861 | panic!( | |
862 | "codegen backend built two dylibs:\n{}\n{}", | |
863 | codegen_backend.display(), | |
864 | f.display() | |
865 | ); | |
866 | } | |
867 | let stamp = codegen_backend_stamp(builder, compiler, target, backend); | |
868 | let codegen_backend = codegen_backend.to_str().unwrap(); | |
869 | t!(fs::write(&stamp, &codegen_backend)); | |
870 | } | |
871 | } | |
872 | ||
873 | /// Creates the `codegen-backends` folder for a compiler that's about to be | |
874 | /// assembled as a complete compiler. | |
875 | /// | |
876 | /// This will take the codegen artifacts produced by `compiler` and link them | |
877 | /// into an appropriate location for `target_compiler` to be a functional | |
878 | /// compiler. | |
879 | fn copy_codegen_backends_to_sysroot( | |
880 | builder: &Builder<'_>, | |
881 | compiler: Compiler, | |
882 | target_compiler: Compiler, | |
883 | ) { | |
884 | let target = target_compiler.host; | |
885 | ||
886 | // Note that this step is different than all the other `*Link` steps in | |
887 | // that it's not assembling a bunch of libraries but rather is primarily | |
888 | // moving the codegen backend into place. The codegen backend of rustc is | |
889 | // not linked into the main compiler by default but is rather dynamically | |
890 | // selected at runtime for inclusion. | |
891 | // | |
892 | // Here we're looking for the output dylib of the `CodegenBackend` step and | |
893 | // we're copying that into the `codegen-backends` folder. | |
894 | let dst = builder.sysroot_codegen_backends(target_compiler); | |
fc512014 | 895 | t!(fs::create_dir_all(&dst), dst); |
29967ef6 XL |
896 | |
897 | if builder.config.dry_run { | |
898 | return; | |
899 | } | |
900 | ||
901 | for backend in builder.config.rust_codegen_backends.iter() { | |
902 | if backend == "llvm" { | |
903 | continue; // Already built as part of rustc | |
904 | } | |
905 | ||
906 | let stamp = codegen_backend_stamp(builder, compiler, target, *backend); | |
907 | let dylib = t!(fs::read_to_string(&stamp)); | |
908 | let file = Path::new(&dylib); | |
909 | let filename = file.file_name().unwrap().to_str().unwrap(); | |
910 | // change `librustc_codegen_cranelift-xxxxxx.so` to | |
911 | // `librustc_codegen_cranelift-release.so` | |
912 | let target_filename = { | |
913 | let dash = filename.find('-').unwrap(); | |
914 | let dot = filename.find('.').unwrap(); | |
915 | format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..]) | |
916 | }; | |
917 | builder.copy(&file, &dst.join(target_filename)); | |
918 | } | |
919 | } | |
920 | ||
7453a54e SL |
921 | /// Cargo's output path for the standard library in a given stage, compiled |
922 | /// by a particular compiler for the specified target. | |
3dfed10e | 923 | pub fn libstd_stamp(builder: &Builder<'_>, compiler: Compiler, target: TargetSelection) -> PathBuf { |
94b46f34 | 924 | builder.cargo_out(compiler, Mode::Std, target).join(".libstd.stamp") |
7453a54e SL |
925 | } |
926 | ||
cc61c64b XL |
927 | /// Cargo's output path for librustc in a given stage, compiled by a particular |
928 | /// compiler for the specified target. | |
9fa01778 XL |
929 | pub fn librustc_stamp( |
930 | builder: &Builder<'_>, | |
931 | compiler: Compiler, | |
3dfed10e | 932 | target: TargetSelection, |
9fa01778 | 933 | ) -> PathBuf { |
94b46f34 | 934 | builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc.stamp") |
cc61c64b XL |
935 | } |
936 | ||
29967ef6 XL |
937 | /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular |
938 | /// compiler for the specified target and backend. | |
939 | fn codegen_backend_stamp( | |
940 | builder: &Builder<'_>, | |
941 | compiler: Compiler, | |
942 | target: TargetSelection, | |
943 | backend: Interned<String>, | |
944 | ) -> PathBuf { | |
945 | builder | |
946 | .cargo_out(compiler, Mode::Codegen, target) | |
947 | .join(format!(".librustc_codegen_{}.stamp", backend)) | |
948 | } | |
949 | ||
9fa01778 XL |
950 | pub fn compiler_file( |
951 | builder: &Builder<'_>, | |
952 | compiler: &Path, | |
3dfed10e | 953 | target: TargetSelection, |
5099ac24 | 954 | c: CLang, |
9fa01778 XL |
955 | file: &str, |
956 | ) -> PathBuf { | |
2c00a5a8 | 957 | let mut cmd = Command::new(compiler); |
5099ac24 | 958 | cmd.args(builder.cflags(target, GitRepo::Rustc, c)); |
2c00a5a8 XL |
959 | cmd.arg(format!("-print-file-name={}", file)); |
960 | let out = output(&mut cmd); | |
54a0048b | 961 | PathBuf::from(out.trim()) |
7453a54e SL |
962 | } |
963 | ||
3b2f2976 XL |
964 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
965 | pub struct Sysroot { | |
966 | pub compiler: Compiler, | |
32a655c1 SL |
967 | } |
968 | ||
3b2f2976 XL |
969 | impl Step for Sysroot { |
970 | type Output = Interned<PathBuf>; | |
971 | ||
9fa01778 | 972 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
3b2f2976 | 973 | run.never() |
7453a54e SL |
974 | } |
975 | ||
3b2f2976 XL |
976 | /// Returns the sysroot for the `compiler` specified that *this build system |
977 | /// generates*. | |
978 | /// | |
979 | /// That is, the sysroot for the stage0 compiler is not what the compiler | |
980 | /// thinks it is by default, but it's the same as the default for stages | |
981 | /// 1-3. | |
9fa01778 | 982 | fn run(self, builder: &Builder<'_>) -> Interned<PathBuf> { |
3b2f2976 XL |
983 | let compiler = self.compiler; |
984 | let sysroot = if compiler.stage == 0 { | |
3dfed10e | 985 | builder.out.join(&compiler.host.triple).join("stage0-sysroot") |
3b2f2976 | 986 | } else { |
3dfed10e | 987 | builder.out.join(&compiler.host.triple).join(format!("stage{}", compiler.stage)) |
3b2f2976 XL |
988 | }; |
989 | let _ = fs::remove_dir_all(&sysroot); | |
990 | t!(fs::create_dir_all(&sysroot)); | |
ba9703b0 | 991 | |
6a06907d | 992 | // If we're downloading a compiler from CI, we can use the same compiler for all stages other than 0. |
cdc7bbd5 | 993 | if builder.config.download_rustc && compiler.stage != 0 { |
6a06907d XL |
994 | assert_eq!( |
995 | builder.config.build, compiler.host, | |
996 | "Cross-compiling is not yet supported with `download-rustc`", | |
997 | ); | |
998 | // Copy the compiler into the correct sysroot. | |
cdc7bbd5 XL |
999 | let ci_rustc_dir = |
1000 | builder.config.out.join(&*builder.config.build.triple).join("ci-rustc"); | |
1001 | builder.cp_r(&ci_rustc_dir, &sysroot); | |
6a06907d XL |
1002 | return INTERNER.intern_path(sysroot); |
1003 | } | |
1004 | ||
ba9703b0 XL |
1005 | // Symlink the source root into the same location inside the sysroot, |
1006 | // where `rust-src` component would go (`$sysroot/lib/rustlib/src/rust`), | |
1007 | // so that any tools relying on `rust-src` also work for local builds, | |
1008 | // and also for translating the virtual `/rustc/$hash` back to the real | |
1009 | // directory (for running tests with `rust.remap-debuginfo = true`). | |
1010 | let sysroot_lib_rustlib_src = sysroot.join("lib/rustlib/src"); | |
1011 | t!(fs::create_dir_all(&sysroot_lib_rustlib_src)); | |
1012 | let sysroot_lib_rustlib_src_rust = sysroot_lib_rustlib_src.join("rust"); | |
1013 | if let Err(e) = symlink_dir(&builder.config, &builder.src, &sysroot_lib_rustlib_src_rust) { | |
1014 | eprintln!( | |
1015 | "warning: creating symbolic link `{}` to `{}` failed with {}", | |
1016 | sysroot_lib_rustlib_src_rust.display(), | |
1017 | builder.src.display(), | |
1018 | e, | |
1019 | ); | |
1020 | if builder.config.rust_remap_debuginfo { | |
1021 | eprintln!( | |
1022 | "warning: some `src/test/ui` tests will fail when lacking `{}`", | |
1023 | sysroot_lib_rustlib_src_rust.display(), | |
1024 | ); | |
1025 | } | |
1026 | } | |
1027 | ||
3b2f2976 XL |
1028 | INTERNER.intern_path(sysroot) |
1029 | } | |
1030 | } | |
1031 | ||
83c7162d | 1032 | #[derive(Debug, Copy, PartialOrd, Ord, Clone, PartialEq, Eq, Hash)] |
3b2f2976 XL |
1033 | pub struct Assemble { |
1034 | /// The compiler which we will produce in this step. Assemble itself will | |
1035 | /// take care of ensuring that the necessary prerequisites to do so exist, | |
1036 | /// that is, this target can be a stage2 compiler and Assemble will build | |
1037 | /// previous stages for you. | |
1038 | pub target_compiler: Compiler, | |
1039 | } | |
7453a54e | 1040 | |
3b2f2976 XL |
1041 | impl Step for Assemble { |
1042 | type Output = Compiler; | |
c295e0f8 | 1043 | const ONLY_HOSTS: bool = true; |
7453a54e | 1044 | |
9fa01778 | 1045 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
c295e0f8 XL |
1046 | run.path("compiler/rustc") |
1047 | } | |
1048 | ||
1049 | fn make_run(run: RunConfig<'_>) { | |
1050 | run.builder.ensure(Assemble { | |
1051 | target_compiler: run.builder.compiler(run.builder.top_stage + 1, run.target), | |
1052 | }); | |
3b2f2976 XL |
1053 | } |
1054 | ||
1055 | /// Prepare a new compiler from the artifacts in `stage` | |
1056 | /// | |
1057 | /// This will assemble a compiler in `build/$host/stage$stage`. The compiler | |
83c7162d | 1058 | /// must have been previously produced by the `stage - 1` builder.build |
3b2f2976 | 1059 | /// compiler. |
9fa01778 | 1060 | fn run(self, builder: &Builder<'_>) -> Compiler { |
3b2f2976 XL |
1061 | let target_compiler = self.target_compiler; |
1062 | ||
1063 | if target_compiler.stage == 0 { | |
dfeec247 XL |
1064 | assert_eq!( |
1065 | builder.config.build, target_compiler.host, | |
1066 | "Cannot obtain compiler for non-native build triple at stage 0" | |
1067 | ); | |
3b2f2976 XL |
1068 | // The stage 0 compiler for the build triple is always pre-built. |
1069 | return target_compiler; | |
1070 | } | |
1071 | ||
1072 | // Get the compiler that we'll use to bootstrap ourselves. | |
2c00a5a8 XL |
1073 | // |
1074 | // Note that this is where the recursive nature of the bootstrap | |
1075 | // happens, as this will request the previous stage's compiler on | |
1076 | // downwards to stage 0. | |
1077 | // | |
1078 | // Also note that we're building a compiler for the host platform. We | |
1079 | // only assume that we can run `build` artifacts, which means that to | |
1080 | // produce some other architecture compiler we need to start from | |
1081 | // `build` to get there. | |
1082 | // | |
2c00a5a8 XL |
1083 | // FIXME: It may be faster if we build just a stage 1 compiler and then |
1084 | // use that to bootstrap this compiler forward. | |
dfeec247 | 1085 | let build_compiler = builder.compiler(target_compiler.stage - 1, builder.config.build); |
3b2f2976 | 1086 | |
6a06907d XL |
1087 | // If we're downloading a compiler from CI, we can use the same compiler for all stages other than 0. |
1088 | if builder.config.download_rustc { | |
1089 | builder.ensure(Sysroot { compiler: target_compiler }); | |
1090 | return target_compiler; | |
1091 | } | |
1092 | ||
3b2f2976 XL |
1093 | // Build the libraries for this compiler to link to (i.e., the libraries |
1094 | // it uses at runtime). NOTE: Crates the target compiler compiles don't | |
1095 | // link to these. (FIXME: Is that correct? It seems to be correct most | |
1096 | // of the time but I think we do link to these for stage2/bin compilers | |
1097 | // when not performing a full bootstrap). | |
dfeec247 | 1098 | builder.ensure(Rustc { compiler: build_compiler, target: target_compiler.host }); |
3b2f2976 | 1099 | |
29967ef6 XL |
1100 | for &backend in builder.config.rust_codegen_backends.iter() { |
1101 | if backend == "llvm" { | |
1102 | continue; // Already built as part of rustc | |
1103 | } | |
1104 | ||
1105 | builder.ensure(CodegenBackend { | |
1106 | compiler: build_compiler, | |
1107 | target: target_compiler.host, | |
1108 | backend, | |
1109 | }); | |
1110 | } | |
1111 | ||
83c7162d | 1112 | let lld_install = if builder.config.lld_enabled { |
dfeec247 | 1113 | Some(builder.ensure(native::Lld { target: target_compiler.host })) |
0531ce1d XL |
1114 | } else { |
1115 | None | |
1116 | }; | |
1117 | ||
3b2f2976 XL |
1118 | let stage = target_compiler.stage; |
1119 | let host = target_compiler.host; | |
83c7162d | 1120 | builder.info(&format!("Assembling stage{} compiler ({})", stage, host)); |
3b2f2976 XL |
1121 | |
1122 | // Link in all dylibs to the libdir | |
5869c6ff XL |
1123 | let stamp = librustc_stamp(builder, build_compiler, target_compiler.host); |
1124 | let proc_macros = builder | |
1125 | .read_stamp_file(&stamp) | |
1126 | .into_iter() | |
1127 | .filter_map(|(path, dependency_type)| { | |
1128 | if dependency_type == DependencyType::Host { | |
1129 | Some(path.file_name().unwrap().to_owned().into_string().unwrap()) | |
1130 | } else { | |
1131 | None | |
1132 | } | |
1133 | }) | |
1134 | .collect::<HashSet<_>>(); | |
1135 | ||
3b2f2976 | 1136 | let sysroot = builder.sysroot(target_compiler); |
532ac7d7 XL |
1137 | let rustc_libdir = builder.rustc_libdir(target_compiler); |
1138 | t!(fs::create_dir_all(&rustc_libdir)); | |
3b2f2976 | 1139 | let src_libdir = builder.sysroot_libdir(build_compiler, host); |
83c7162d | 1140 | for f in builder.read_dir(&src_libdir) { |
3b2f2976 | 1141 | let filename = f.file_name().into_string().unwrap(); |
6a06907d XL |
1142 | if (is_dylib(&filename) || is_debug_info(&filename)) && !proc_macros.contains(&filename) |
1143 | { | |
532ac7d7 | 1144 | builder.copy(&f.path(), &rustc_libdir.join(&filename)); |
3b2f2976 XL |
1145 | } |
1146 | } | |
1147 | ||
29967ef6 XL |
1148 | copy_codegen_backends_to_sysroot(builder, build_compiler, target_compiler); |
1149 | ||
fc512014 XL |
1150 | // We prepend this bin directory to the user PATH when linking Rust binaries. To |
1151 | // avoid shadowing the system LLD we rename the LLD we provide to `rust-lld`. | |
60c5eb7d | 1152 | let libdir = builder.sysroot_libdir(target_compiler, target_compiler.host); |
fc512014 XL |
1153 | let libdir_bin = libdir.parent().unwrap().join("bin"); |
1154 | t!(fs::create_dir_all(&libdir_bin)); | |
0531ce1d | 1155 | if let Some(lld_install) = lld_install { |
3dfed10e XL |
1156 | let src_exe = exe("lld", target_compiler.host); |
1157 | let dst_exe = exe("rust-lld", target_compiler.host); | |
fc512014 | 1158 | builder.copy(&lld_install.join("bin").join(&src_exe), &libdir_bin.join(&dst_exe)); |
17df50a5 XL |
1159 | // for `-Z gcc-ld=lld` |
1160 | let gcc_ld_dir = libdir_bin.join("gcc-ld"); | |
1161 | t!(fs::create_dir(&gcc_ld_dir)); | |
dc3f5686 XL |
1162 | for flavor in ["ld", "ld64"] { |
1163 | let lld_wrapper_exe = builder.ensure(crate::tool::LldWrapper { | |
1164 | compiler: build_compiler, | |
1165 | target: target_compiler.host, | |
1166 | flavor_feature: flavor, | |
1167 | }); | |
1168 | builder.copy(&lld_wrapper_exe, &gcc_ld_dir.join(exe(flavor, target_compiler.host))); | |
1169 | } | |
fc512014 XL |
1170 | } |
1171 | ||
5869c6ff | 1172 | if builder.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm")) { |
fc512014 XL |
1173 | let llvm_config_bin = builder.ensure(native::Llvm { target: target_compiler.host }); |
1174 | if !builder.config.dry_run { | |
1175 | let llvm_bin_dir = output(Command::new(llvm_config_bin).arg("--bindir")); | |
1176 | let llvm_bin_dir = Path::new(llvm_bin_dir.trim()); | |
a2a8927a XL |
1177 | |
1178 | // Since we've already built the LLVM tools, install them to the sysroot. | |
1179 | // This is the equivalent of installing the `llvm-tools-preview` component via | |
1180 | // rustup, and lets developers use a locally built toolchain to | |
1181 | // build projects that expect llvm tools to be present in the sysroot | |
1182 | // (e.g. the `bootimage` crate). | |
1183 | for tool in LLVM_TOOLS { | |
1184 | let tool_exe = exe(tool, target_compiler.host); | |
1185 | let src_path = llvm_bin_dir.join(&tool_exe); | |
1186 | // When using `donwload-ci-llvm`, some of the tools | |
1187 | // may not exist, so skip trying to copy them. | |
1188 | if src_path.exists() { | |
1189 | builder.copy(&src_path, &libdir_bin.join(&tool_exe)); | |
1190 | } | |
1191 | } | |
fc512014 | 1192 | } |
0531ce1d | 1193 | } |
3b2f2976 | 1194 | |
60c5eb7d XL |
1195 | // Ensure that `libLLVM.so` ends up in the newly build compiler directory, |
1196 | // so that it can be found when the newly built `rustc` is run. | |
f9f354fc XL |
1197 | dist::maybe_install_llvm_runtime(builder, target_compiler.host, &sysroot); |
1198 | dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot); | |
0731742a | 1199 | |
3b2f2976 | 1200 | // Link the compiler binary itself into place |
94b46f34 | 1201 | let out_dir = builder.cargo_out(build_compiler, Mode::Rustc, host); |
1b1a35ee | 1202 | let rustc = out_dir.join(exe("rustc-main", host)); |
3b2f2976 XL |
1203 | let bindir = sysroot.join("bin"); |
1204 | t!(fs::create_dir_all(&bindir)); | |
1205 | let compiler = builder.rustc(target_compiler); | |
83c7162d | 1206 | builder.copy(&rustc, &compiler); |
3b2f2976 XL |
1207 | |
1208 | target_compiler | |
7453a54e SL |
1209 | } |
1210 | } | |
1211 | ||
1212 | /// Link some files into a rustc sysroot. | |
1213 | /// | |
7cac9316 XL |
1214 | /// For a particular stage this will link the file listed in `stamp` into the |
1215 | /// `sysroot_dst` provided. | |
532ac7d7 XL |
1216 | pub fn add_to_sysroot( |
1217 | builder: &Builder<'_>, | |
1218 | sysroot_dst: &Path, | |
1219 | sysroot_host_dst: &Path, | |
dfeec247 | 1220 | stamp: &Path, |
532ac7d7 | 1221 | ) { |
f035d41b | 1222 | let self_contained_dst = &sysroot_dst.join("self-contained"); |
7cac9316 | 1223 | t!(fs::create_dir_all(&sysroot_dst)); |
532ac7d7 | 1224 | t!(fs::create_dir_all(&sysroot_host_dst)); |
f035d41b XL |
1225 | t!(fs::create_dir_all(&self_contained_dst)); |
1226 | for (path, dependency_type) in builder.read_stamp_file(stamp) { | |
1227 | let dst = match dependency_type { | |
1228 | DependencyType::Host => sysroot_host_dst, | |
1229 | DependencyType::Target => sysroot_dst, | |
1230 | DependencyType::TargetSelfContained => self_contained_dst, | |
1231 | }; | |
1232 | builder.copy(&path, &dst.join(path.file_name().unwrap())); | |
7453a54e SL |
1233 | } |
1234 | } | |
54a0048b | 1235 | |
dfeec247 XL |
1236 | pub fn run_cargo( |
1237 | builder: &Builder<'_>, | |
1238 | cargo: Cargo, | |
1239 | tail_args: Vec<String>, | |
1240 | stamp: &Path, | |
f035d41b | 1241 | additional_target_deps: Vec<(PathBuf, DependencyType)>, |
dfeec247 XL |
1242 | is_check: bool, |
1243 | ) -> Vec<PathBuf> { | |
83c7162d XL |
1244 | if builder.config.dry_run { |
1245 | return Vec::new(); | |
1246 | } | |
1247 | ||
7cac9316 XL |
1248 | // `target_root_dir` looks like $dir/$target/release |
1249 | let target_root_dir = stamp.parent().unwrap(); | |
1250 | // `target_deps_dir` looks like $dir/$target/release/deps | |
1251 | let target_deps_dir = target_root_dir.join("deps"); | |
1252 | // `host_root_dir` looks like $dir/release | |
dfeec247 XL |
1253 | let host_root_dir = target_root_dir |
1254 | .parent() | |
1255 | .unwrap() // chop off `release` | |
1256 | .parent() | |
1257 | .unwrap() // chop off `$target` | |
1258 | .join(target_root_dir.file_name().unwrap()); | |
7cac9316 XL |
1259 | |
1260 | // Spawn Cargo slurping up its JSON output. We'll start building up the | |
1261 | // `deps` array of all files it generated along with a `toplevel` array of | |
1262 | // files we need to probe for later. | |
1263 | let mut deps = Vec::new(); | |
1264 | let mut toplevel = Vec::new(); | |
dc9dc135 | 1265 | let ok = stream_cargo(builder, cargo, tail_args, &mut |msg| { |
532ac7d7 XL |
1266 | let (filenames, crate_types) = match msg { |
1267 | CargoMessage::CompilerArtifact { | |
1268 | filenames, | |
dfeec247 | 1269 | target: CargoTarget { crate_types }, |
532ac7d7 XL |
1270 | .. |
1271 | } => (filenames, crate_types), | |
0531ce1d | 1272 | _ => return, |
7cac9316 | 1273 | }; |
0531ce1d | 1274 | for filename in filenames { |
7cac9316 | 1275 | // Skip files like executables |
74b04a01 XL |
1276 | if !(filename.ends_with(".rlib") |
1277 | || filename.ends_with(".lib") | |
1278 | || filename.ends_with(".a") | |
6a06907d | 1279 | || is_debug_info(&filename) |
74b04a01 XL |
1280 | || is_dylib(&filename) |
1281 | || (is_check && filename.ends_with(".rmeta"))) | |
dfeec247 | 1282 | { |
8faf50e0 | 1283 | continue; |
7cac9316 XL |
1284 | } |
1285 | ||
0531ce1d | 1286 | let filename = Path::new(&*filename); |
7cac9316 XL |
1287 | |
1288 | // If this was an output file in the "host dir" we don't actually | |
532ac7d7 | 1289 | // worry about it, it's not relevant for us |
7cac9316 | 1290 | if filename.starts_with(&host_root_dir) { |
532ac7d7 XL |
1291 | // Unless it's a proc macro used in the compiler |
1292 | if crate_types.iter().any(|t| t == "proc-macro") { | |
f035d41b | 1293 | deps.push((filename.to_path_buf(), DependencyType::Host)); |
532ac7d7 | 1294 | } |
8faf50e0 | 1295 | continue; |
041b39d2 | 1296 | } |
7cac9316 XL |
1297 | |
1298 | // If this was output in the `deps` dir then this is a precise file | |
1299 | // name (hash included) so we start tracking it. | |
041b39d2 | 1300 | if filename.starts_with(&target_deps_dir) { |
f035d41b | 1301 | deps.push((filename.to_path_buf(), DependencyType::Target)); |
8faf50e0 | 1302 | continue; |
041b39d2 | 1303 | } |
7cac9316 XL |
1304 | |
1305 | // Otherwise this was a "top level artifact" which right now doesn't | |
1306 | // have a hash in the name, but there's a version of this file in | |
1307 | // the `deps` folder which *does* have a hash in the name. That's | |
1308 | // the one we'll want to we'll probe for it later. | |
abe05a73 XL |
1309 | // |
1310 | // We do not use `Path::file_stem` or `Path::extension` here, | |
1311 | // because some generated files may have multiple extensions e.g. | |
1312 | // `std-<hash>.dll.lib` on Windows. The aforementioned methods only | |
1313 | // split the file name by the last extension (`.lib`) while we need | |
1314 | // to split by all extensions (`.dll.lib`). | |
1315 | let expected_len = t!(filename.metadata()).len(); | |
1316 | let filename = filename.file_name().unwrap().to_str().unwrap(); | |
1317 | let mut parts = filename.splitn(2, '.'); | |
1318 | let file_stem = parts.next().unwrap().to_owned(); | |
1319 | let extension = parts.next().unwrap().to_owned(); | |
1320 | ||
1321 | toplevel.push((file_stem, extension, expected_len)); | |
7cac9316 | 1322 | } |
0531ce1d | 1323 | }); |
7cac9316 | 1324 | |
0531ce1d | 1325 | if !ok { |
a1dfa0c6 | 1326 | exit(1); |
7cac9316 XL |
1327 | } |
1328 | ||
1329 | // Ok now we need to actually find all the files listed in `toplevel`. We've | |
1330 | // got a list of prefix/extensions and we basically just need to find the | |
1331 | // most recent file in the `deps` folder corresponding to each one. | |
1332 | let contents = t!(target_deps_dir.read_dir()) | |
1333 | .map(|e| t!(e)) | |
1334 | .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata()))) | |
1335 | .collect::<Vec<_>>(); | |
abe05a73 XL |
1336 | for (prefix, extension, expected_len) in toplevel { |
1337 | let candidates = contents.iter().filter(|&&(_, ref filename, ref meta)| { | |
f035d41b XL |
1338 | meta.len() == expected_len |
1339 | && filename | |
1340 | .strip_prefix(&prefix[..]) | |
1341 | .map(|s| s.starts_with('-') && s.ends_with(&extension[..])) | |
1342 | .unwrap_or(false) | |
7cac9316 | 1343 | }); |
dfeec247 XL |
1344 | let max = candidates |
1345 | .max_by_key(|&&(_, _, ref metadata)| FileTime::from_last_modification_time(metadata)); | |
7cac9316 XL |
1346 | let path_to_add = match max { |
1347 | Some(triple) => triple.0.to_str().unwrap(), | |
1348 | None => panic!("no output generated for {:?} {:?}", prefix, extension), | |
1349 | }; | |
1350 | if is_dylib(path_to_add) { | |
1351 | let candidate = format!("{}.lib", path_to_add); | |
1352 | let candidate = PathBuf::from(candidate); | |
1353 | if candidate.exists() { | |
f035d41b | 1354 | deps.push((candidate, DependencyType::Target)); |
7cac9316 XL |
1355 | } |
1356 | } | |
f035d41b | 1357 | deps.push((path_to_add.into(), DependencyType::Target)); |
7cac9316 XL |
1358 | } |
1359 | ||
f035d41b | 1360 | deps.extend(additional_target_deps); |
7cac9316 | 1361 | deps.sort(); |
7cac9316 | 1362 | let mut new_contents = Vec::new(); |
f035d41b XL |
1363 | for (dep, dependency_type) in deps.iter() { |
1364 | new_contents.extend(match *dependency_type { | |
1365 | DependencyType::Host => b"h", | |
1366 | DependencyType::Target => b"t", | |
1367 | DependencyType::TargetSelfContained => b"s", | |
1368 | }); | |
7cac9316 XL |
1369 | new_contents.extend(dep.to_str().unwrap().as_bytes()); |
1370 | new_contents.extend(b"\0"); | |
1371 | } | |
0731742a | 1372 | t!(fs::write(&stamp, &new_contents)); |
532ac7d7 | 1373 | deps.into_iter().map(|(d, _)| d).collect() |
9e0c209e | 1374 | } |
0531ce1d XL |
1375 | |
1376 | pub fn stream_cargo( | |
9fa01778 | 1377 | builder: &Builder<'_>, |
e1599b0c | 1378 | cargo: Cargo, |
dc9dc135 | 1379 | tail_args: Vec<String>, |
9fa01778 | 1380 | cb: &mut dyn FnMut(CargoMessage<'_>), |
0531ce1d | 1381 | ) -> bool { |
e1599b0c | 1382 | let mut cargo = Command::from(cargo); |
83c7162d XL |
1383 | if builder.config.dry_run { |
1384 | return true; | |
1385 | } | |
0531ce1d XL |
1386 | // Instruct Cargo to give us json messages on stdout, critically leaving |
1387 | // stderr as piped so we can get those pretty colors. | |
ba9703b0 XL |
1388 | let mut message_format = if builder.config.json_output { |
1389 | String::from("json") | |
1390 | } else { | |
1391 | String::from("json-render-diagnostics") | |
1392 | }; | |
dfeec247 | 1393 | if let Some(s) = &builder.config.rustc_error_format { |
e1599b0c XL |
1394 | message_format.push_str(",json-diagnostic-"); |
1395 | message_format.push_str(s); | |
1396 | } | |
1397 | cargo.arg("--message-format").arg(message_format).stdout(Stdio::piped()); | |
0531ce1d | 1398 | |
dc9dc135 XL |
1399 | for arg in tail_args { |
1400 | cargo.arg(arg); | |
1401 | } | |
1402 | ||
83c7162d | 1403 | builder.verbose(&format!("running: {:?}", cargo)); |
0531ce1d XL |
1404 | let mut child = match cargo.spawn() { |
1405 | Ok(child) => child, | |
1406 | Err(e) => panic!("failed to execute command: {:?}\nerror: {}", cargo, e), | |
1407 | }; | |
1408 | ||
1409 | // Spawn Cargo slurping up its JSON output. We'll start building up the | |
1410 | // `deps` array of all files it generated along with a `toplevel` array of | |
1411 | // files we need to probe for later. | |
1412 | let stdout = BufReader::new(child.stdout.take().unwrap()); | |
1413 | for line in stdout.lines() { | |
1414 | let line = t!(line); | |
9fa01778 | 1415 | match serde_json::from_str::<CargoMessage<'_>>(&line) { |
f035d41b XL |
1416 | Ok(msg) => { |
1417 | if builder.config.json_output { | |
1418 | // Forward JSON to stdout. | |
1419 | println!("{}", line); | |
1420 | } | |
1421 | cb(msg) | |
1422 | } | |
0531ce1d | 1423 | // If this was informational, just print it out and continue |
dfeec247 | 1424 | Err(_) => println!("{}", line), |
0531ce1d XL |
1425 | } |
1426 | } | |
1427 | ||
1428 | // Make sure Cargo actually succeeded after we read all of its stdout. | |
1429 | let status = t!(child.wait()); | |
136023e0 | 1430 | if builder.is_verbose() && !status.success() { |
dfeec247 XL |
1431 | eprintln!( |
1432 | "command did not execute successfully: {:?}\n\ | |
0531ce1d | 1433 | expected success, got: {}", |
dfeec247 XL |
1434 | cargo, status |
1435 | ); | |
0531ce1d XL |
1436 | } |
1437 | status.success() | |
1438 | } | |
1439 | ||
532ac7d7 XL |
1440 | #[derive(Deserialize)] |
1441 | pub struct CargoTarget<'a> { | |
1442 | crate_types: Vec<Cow<'a, str>>, | |
1443 | } | |
1444 | ||
0531ce1d XL |
1445 | #[derive(Deserialize)] |
1446 | #[serde(tag = "reason", rename_all = "kebab-case")] | |
1447 | pub enum CargoMessage<'a> { | |
1448 | CompilerArtifact { | |
1449 | package_id: Cow<'a, str>, | |
1450 | features: Vec<Cow<'a, str>>, | |
1451 | filenames: Vec<Cow<'a, str>>, | |
532ac7d7 | 1452 | target: CargoTarget<'a>, |
0531ce1d XL |
1453 | }, |
1454 | BuildScriptExecuted { | |
1455 | package_id: Cow<'a, str>, | |
dc9dc135 | 1456 | }, |
f9f354fc XL |
1457 | BuildFinished { |
1458 | success: bool, | |
1459 | }, | |
dc9dc135 | 1460 | } |