1use std::borrow::Cow;
10use std::collections::HashSet;
11use std::ffi::OsStr;
12use std::io::BufReader;
13use std::io::prelude::*;
14use std::path::{Path, PathBuf};
15use std::time::SystemTime;
16use std::{env, fs, str};
17
18use serde_derive::Deserialize;
19#[cfg(feature = "tracing")]
20use tracing::span;
21
22use crate::core::build_steps::gcc::{Gcc, GccOutput, add_cg_gcc_cargo_flags};
23use crate::core::build_steps::tool::{RustcPrivateCompilers, SourceType, copy_lld_artifacts};
24use crate::core::build_steps::{dist, llvm};
25use crate::core::builder;
26use crate::core::builder::{
27 Builder, Cargo, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description,
28};
29use crate::core::config::{DebuginfoLevel, LlvmLibunwind, RustcLto, TargetSelection};
30use crate::utils::build_stamp;
31use crate::utils::build_stamp::BuildStamp;
32use crate::utils::exec::command;
33use crate::utils::helpers::{
34 exe, get_clang_cl_resource_dir, is_debug_info, is_dylib, symlink_dir, t, up_to_date,
35};
36use crate::{
37 CLang, CodegenBackendKind, Compiler, DependencyType, FileType, GitRepo, LLVM_TOOLS, Mode,
38 debug, trace,
39};
40
41#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
43pub struct Std {
44 pub target: TargetSelection,
45 pub build_compiler: Compiler,
47 crates: Vec<String>,
51 force_recompile: bool,
54 extra_rust_args: &'static [&'static str],
55 is_for_mir_opt_tests: bool,
56}
57
58impl Std {
59 pub fn new(build_compiler: Compiler, target: TargetSelection) -> Self {
60 Self {
61 target,
62 build_compiler,
63 crates: Default::default(),
64 force_recompile: false,
65 extra_rust_args: &[],
66 is_for_mir_opt_tests: false,
67 }
68 }
69
70 pub fn force_recompile(mut self, force_recompile: bool) -> Self {
71 self.force_recompile = force_recompile;
72 self
73 }
74
75 #[expect(clippy::wrong_self_convention)]
76 pub fn is_for_mir_opt_tests(mut self, is_for_mir_opt_tests: bool) -> Self {
77 self.is_for_mir_opt_tests = is_for_mir_opt_tests;
78 self
79 }
80
81 pub fn extra_rust_args(mut self, extra_rust_args: &'static [&'static str]) -> Self {
82 self.extra_rust_args = extra_rust_args;
83 self
84 }
85
86 fn copy_extra_objects(
87 &self,
88 builder: &Builder<'_>,
89 compiler: &Compiler,
90 target: TargetSelection,
91 ) -> Vec<(PathBuf, DependencyType)> {
92 let mut deps = Vec::new();
93 if !self.is_for_mir_opt_tests {
94 deps.extend(copy_third_party_objects(builder, compiler, target));
95 deps.extend(copy_self_contained_objects(builder, compiler, target));
96 }
97 deps
98 }
99}
100
101impl Step for Std {
102 type Output = ();
103 const DEFAULT: bool = true;
104
105 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
106 run.crate_or_deps("sysroot").path("library")
107 }
108
109 fn make_run(run: RunConfig<'_>) {
110 let crates = std_crates_for_run_make(&run);
111 let builder = run.builder;
112
113 let force_recompile = builder.rust_info().is_managed_git_subrepository()
117 && builder.download_rustc()
118 && builder.config.has_changes_from_upstream(&["library"]);
119
120 trace!("is managed git repo: {}", builder.rust_info().is_managed_git_subrepository());
121 trace!("download_rustc: {}", builder.download_rustc());
122 trace!(force_recompile);
123
124 run.builder.ensure(Std {
125 build_compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
126 target: run.target,
127 crates,
128 force_recompile,
129 extra_rust_args: &[],
130 is_for_mir_opt_tests: false,
131 });
132 }
133
134 fn run(self, builder: &Builder<'_>) {
140 let target = self.target;
141
142 if self.build_compiler.stage == 0 {
144 let compiler = self.build_compiler;
145 builder.ensure(StdLink::from_std(self, compiler));
146
147 return;
148 }
149
150 let build_compiler = if builder.download_rustc() && self.force_recompile {
151 builder
154 .compiler(self.build_compiler.stage.saturating_sub(1), builder.config.host_target)
155 } else {
156 self.build_compiler
157 };
158
159 if builder.download_rustc()
162 && builder.config.is_host_target(target)
163 && !self.force_recompile
164 {
165 let sysroot =
166 builder.ensure(Sysroot { compiler: build_compiler, force_recompile: false });
167 cp_rustc_component_to_ci_sysroot(
168 builder,
169 &sysroot,
170 builder.config.ci_rust_std_contents(),
171 );
172 return;
173 }
174
175 if builder.config.keep_stage.contains(&build_compiler.stage)
176 || builder.config.keep_stage_std.contains(&build_compiler.stage)
177 {
178 trace!(keep_stage = ?builder.config.keep_stage);
179 trace!(keep_stage_std = ?builder.config.keep_stage_std);
180
181 builder.info("WARNING: Using a potentially old libstd. This may not behave well.");
182
183 builder.ensure(StartupObjects { compiler: build_compiler, target });
184
185 self.copy_extra_objects(builder, &build_compiler, target);
186
187 builder.ensure(StdLink::from_std(self, build_compiler));
188 return;
189 }
190
191 let mut target_deps = builder.ensure(StartupObjects { compiler: build_compiler, target });
192
193 let stage = build_compiler.stage;
195
196 if build_compiler.stage > 1
200 && !builder.config.full_bootstrap
201 && (target == builder.host_target || builder.config.hosts.contains(&target))
211 {
212 let build_compiler_for_std_to_uplift = builder.compiler(1, builder.host_target);
213 builder.std(build_compiler_for_std_to_uplift, target);
214
215 let msg = if build_compiler_for_std_to_uplift.host == target {
216 format!(
217 "Uplifting library (stage{} -> stage{stage})",
218 build_compiler_for_std_to_uplift.stage
219 )
220 } else {
221 format!(
222 "Uplifting library (stage{}:{} -> stage{stage}:{target})",
223 build_compiler_for_std_to_uplift.stage, build_compiler_for_std_to_uplift.host,
224 )
225 };
226
227 builder.info(&msg);
228
229 self.copy_extra_objects(builder, &build_compiler, target);
232
233 builder.ensure(StdLink::from_std(self, build_compiler_for_std_to_uplift));
234 return;
235 }
236
237 target_deps.extend(self.copy_extra_objects(builder, &build_compiler, target));
238
239 let mut cargo = if self.is_for_mir_opt_tests {
243 trace!("building special sysroot for mir-opt tests");
244 let mut cargo = builder::Cargo::new_for_mir_opt_tests(
245 builder,
246 build_compiler,
247 Mode::Std,
248 SourceType::InTree,
249 target,
250 Kind::Check,
251 );
252 cargo.rustflag("-Zalways-encode-mir");
253 cargo.arg("--manifest-path").arg(builder.src.join("library/sysroot/Cargo.toml"));
254 cargo
255 } else {
256 trace!("building regular sysroot");
257 let mut cargo = builder::Cargo::new(
258 builder,
259 build_compiler,
260 Mode::Std,
261 SourceType::InTree,
262 target,
263 Kind::Build,
264 );
265 std_cargo(builder, target, &mut cargo);
266 for krate in &*self.crates {
267 cargo.arg("-p").arg(krate);
268 }
269 cargo
270 };
271
272 if target.is_synthetic() {
274 cargo.env("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET", "1");
275 }
276 for rustflag in self.extra_rust_args.iter() {
277 cargo.rustflag(rustflag);
278 }
279
280 let _guard = builder.msg(
281 Kind::Build,
282 format_args!("library artifacts{}", crate_description(&self.crates)),
283 Mode::Std,
284 build_compiler,
285 target,
286 );
287 run_cargo(
288 builder,
289 cargo,
290 vec![],
291 &build_stamp::libstd_stamp(builder, build_compiler, target),
292 target_deps,
293 self.is_for_mir_opt_tests, false,
295 );
296
297 builder.ensure(StdLink::from_std(
298 self,
299 builder.compiler(build_compiler.stage, builder.config.host_target),
300 ));
301 }
302
303 fn metadata(&self) -> Option<StepMetadata> {
304 Some(StepMetadata::build("std", self.target).built_by(self.build_compiler))
305 }
306}
307
308fn copy_and_stamp(
309 builder: &Builder<'_>,
310 libdir: &Path,
311 sourcedir: &Path,
312 name: &str,
313 target_deps: &mut Vec<(PathBuf, DependencyType)>,
314 dependency_type: DependencyType,
315) {
316 let target = libdir.join(name);
317 builder.copy_link(&sourcedir.join(name), &target, FileType::Regular);
318
319 target_deps.push((target, dependency_type));
320}
321
322fn copy_llvm_libunwind(builder: &Builder<'_>, target: TargetSelection, libdir: &Path) -> PathBuf {
323 let libunwind_path = builder.ensure(llvm::Libunwind { target });
324 let libunwind_source = libunwind_path.join("libunwind.a");
325 let libunwind_target = libdir.join("libunwind.a");
326 builder.copy_link(&libunwind_source, &libunwind_target, FileType::NativeLibrary);
327 libunwind_target
328}
329
330fn copy_third_party_objects(
332 builder: &Builder<'_>,
333 compiler: &Compiler,
334 target: TargetSelection,
335) -> Vec<(PathBuf, DependencyType)> {
336 let mut target_deps = vec![];
337
338 if builder.config.needs_sanitizer_runtime_built(target) && compiler.stage != 0 {
339 target_deps.extend(
342 copy_sanitizers(builder, compiler, target)
343 .into_iter()
344 .map(|d| (d, DependencyType::Target)),
345 );
346 }
347
348 if target == "x86_64-fortanix-unknown-sgx"
349 || builder.config.llvm_libunwind(target) == LlvmLibunwind::InTree
350 && (target.contains("linux") || target.contains("fuchsia") || target.contains("aix"))
351 {
352 let libunwind_path =
353 copy_llvm_libunwind(builder, target, &builder.sysroot_target_libdir(*compiler, target));
354 target_deps.push((libunwind_path, DependencyType::Target));
355 }
356
357 target_deps
358}
359
360fn copy_self_contained_objects(
362 builder: &Builder<'_>,
363 compiler: &Compiler,
364 target: TargetSelection,
365) -> Vec<(PathBuf, DependencyType)> {
366 let libdir_self_contained =
367 builder.sysroot_target_libdir(*compiler, target).join("self-contained");
368 t!(fs::create_dir_all(&libdir_self_contained));
369 let mut target_deps = vec![];
370
371 if target.needs_crt_begin_end() {
379 let srcdir = builder.musl_libdir(target).unwrap_or_else(|| {
380 panic!("Target {:?} does not have a \"musl-libdir\" key", target.triple)
381 });
382 if !target.starts_with("wasm32") {
383 for &obj in &["libc.a", "crt1.o", "Scrt1.o", "rcrt1.o", "crti.o", "crtn.o"] {
384 copy_and_stamp(
385 builder,
386 &libdir_self_contained,
387 &srcdir,
388 obj,
389 &mut target_deps,
390 DependencyType::TargetSelfContained,
391 );
392 }
393 let crt_path = builder.ensure(llvm::CrtBeginEnd { target });
394 for &obj in &["crtbegin.o", "crtbeginS.o", "crtend.o", "crtendS.o"] {
395 let src = crt_path.join(obj);
396 let target = libdir_self_contained.join(obj);
397 builder.copy_link(&src, &target, FileType::NativeLibrary);
398 target_deps.push((target, DependencyType::TargetSelfContained));
399 }
400 } else {
401 for &obj in &["libc.a", "crt1-command.o"] {
404 copy_and_stamp(
405 builder,
406 &libdir_self_contained,
407 &srcdir,
408 obj,
409 &mut target_deps,
410 DependencyType::TargetSelfContained,
411 );
412 }
413 }
414 if !target.starts_with("s390x") {
415 let libunwind_path = copy_llvm_libunwind(builder, target, &libdir_self_contained);
416 target_deps.push((libunwind_path, DependencyType::TargetSelfContained));
417 }
418 } else if target.contains("-wasi") {
419 let srcdir = builder.wasi_libdir(target).unwrap_or_else(|| {
420 panic!(
421 "Target {:?} does not have a \"wasi-root\" key in bootstrap.toml \
422 or `$WASI_SDK_PATH` set",
423 target.triple
424 )
425 });
426 for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] {
427 copy_and_stamp(
428 builder,
429 &libdir_self_contained,
430 &srcdir,
431 obj,
432 &mut target_deps,
433 DependencyType::TargetSelfContained,
434 );
435 }
436 } else if target.is_windows_gnu() {
437 for obj in ["crt2.o", "dllcrt2.o"].iter() {
438 let src = compiler_file(builder, &builder.cc(target), target, CLang::C, obj);
439 let dst = libdir_self_contained.join(obj);
440 builder.copy_link(&src, &dst, FileType::NativeLibrary);
441 target_deps.push((dst, DependencyType::TargetSelfContained));
442 }
443 }
444
445 target_deps
446}
447
448pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec<String> {
451 let mut crates = run.make_run_crates(builder::Alias::Library);
452
453 let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);
462 if target_is_no_std {
463 crates.retain(|c| c == "core" || c == "alloc");
464 }
465 crates
466}
467
468fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
474 if builder.config.llvm_from_ci {
476 builder.config.maybe_download_ci_llvm();
478 let ci_llvm_compiler_rt = builder.config.ci_llvm_root().join("compiler-rt");
479 if ci_llvm_compiler_rt.exists() {
480 return ci_llvm_compiler_rt;
481 }
482 }
483
484 builder.require_submodule("src/llvm-project", {
486 Some("The `build.profiler` config option requires `compiler-rt` sources from LLVM.")
487 });
488 builder.src.join("src/llvm-project/compiler-rt")
489}
490
491pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, cargo: &mut Cargo) {
494 if target.contains("apple") && !builder.config.dry_run() {
512 let mut cmd = command(builder.rustc(cargo.compiler()));
516 cmd.arg("--target").arg(target.rustc_target_arg());
517 cmd.arg("--print=deployment-target");
518 let output = cmd.run_capture_stdout(builder).stdout();
519
520 let (env_var, value) = output.split_once('=').unwrap();
521 cargo.env(env_var.trim(), value.trim());
524
525 if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
535 cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
536 }
537 }
538
539 if let Some(path) = builder.config.profiler_path(target) {
541 cargo.env("LLVM_PROFILER_RT_LIB", path);
542 } else if builder.config.profiler_enabled(target) {
543 let compiler_rt = compiler_rt_for_profiler(builder);
544 cargo.env("RUST_COMPILER_RT_FOR_PROFILER", compiler_rt);
548 }
549
550 let compiler_builtins_c_feature = if builder.config.optimized_compiler_builtins(target) {
564 builder.require_submodule(
572 "src/llvm-project",
573 Some(
574 "The `build.optimized-compiler-builtins` config option \
575 requires `compiler-rt` sources from LLVM.",
576 ),
577 );
578 let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
579 assert!(compiler_builtins_root.exists());
580 cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);
583 " compiler-builtins-c"
584 } else {
585 ""
586 };
587
588 if !builder.unstable_features() {
591 cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
592 }
593
594 let mut features = String::new();
595
596 if builder.no_std(target) == Some(true) {
597 features += " compiler-builtins-mem";
598 if !target.starts_with("bpf") {
599 features.push_str(compiler_builtins_c_feature);
600 }
601
602 cargo
604 .args(["-p", "alloc"])
605 .arg("--manifest-path")
606 .arg(builder.src.join("library/alloc/Cargo.toml"))
607 .arg("--features")
608 .arg(features);
609 } else {
610 features += &builder.std_features(target);
611 features.push_str(compiler_builtins_c_feature);
612
613 cargo
614 .arg("--features")
615 .arg(features)
616 .arg("--manifest-path")
617 .arg(builder.src.join("library/sysroot/Cargo.toml"));
618
619 if target.contains("musl")
622 && let Some(p) = builder.musl_libdir(target)
623 {
624 let root = format!("native={}", p.to_str().unwrap());
625 cargo.rustflag("-L").rustflag(&root);
626 }
627
628 if target.contains("-wasi")
629 && let Some(dir) = builder.wasi_libdir(target)
630 {
631 let root = format!("native={}", dir.to_str().unwrap());
632 cargo.rustflag("-L").rustflag(&root);
633 }
634 }
635
636 cargo.rustflag("-Cembed-bitcode=yes");
642
643 if builder.config.rust_lto == RustcLto::Off {
644 cargo.rustflag("-Clto=off");
645 }
646
647 if target.contains("riscv") {
654 cargo.rustflag("-Cforce-unwind-tables=yes");
655 }
656
657 cargo.rustflag("-Zunstable-options");
660 cargo.rustflag("-Cforce-frame-pointers=non-leaf");
661
662 let html_root =
663 format!("-Zcrate-attr=doc(html_root_url=\"{}/\")", builder.doc_rust_lang_org_channel(),);
664 cargo.rustflag(&html_root);
665 cargo.rustdocflag(&html_root);
666
667 cargo.rustdocflag("-Zcrate-attr=warn(rust_2018_idioms)");
668}
669
670#[derive(Debug, Clone, PartialEq, Eq, Hash)]
679pub struct StdLink {
680 pub compiler: Compiler,
681 pub target_compiler: Compiler,
682 pub target: TargetSelection,
683 crates: Vec<String>,
685 force_recompile: bool,
687}
688
689impl StdLink {
690 pub fn from_std(std: Std, host_compiler: Compiler) -> Self {
691 Self {
692 compiler: host_compiler,
693 target_compiler: std.build_compiler,
694 target: std.target,
695 crates: std.crates,
696 force_recompile: std.force_recompile,
697 }
698 }
699}
700
701impl Step for StdLink {
702 type Output = ();
703
704 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
705 run.never()
706 }
707
708 fn run(self, builder: &Builder<'_>) {
717 let compiler = self.compiler;
718 let target_compiler = self.target_compiler;
719 let target = self.target;
720
721 let (libdir, hostdir) = if !self.force_recompile && builder.download_rustc() {
723 let lib = builder.sysroot_libdir_relative(self.compiler);
725 let sysroot = builder.ensure(crate::core::build_steps::compile::Sysroot {
726 compiler: self.compiler,
727 force_recompile: self.force_recompile,
728 });
729 let libdir = sysroot.join(lib).join("rustlib").join(target).join("lib");
730 let hostdir = sysroot.join(lib).join("rustlib").join(compiler.host).join("lib");
731 (libdir, hostdir)
732 } else {
733 let libdir = builder.sysroot_target_libdir(target_compiler, target);
734 let hostdir = builder.sysroot_target_libdir(target_compiler, compiler.host);
735 (libdir, hostdir)
736 };
737
738 let is_downloaded_beta_stage0 = builder
739 .build
740 .config
741 .initial_rustc
742 .starts_with(builder.out.join(compiler.host).join("stage0/bin"));
743
744 if compiler.stage == 0 && is_downloaded_beta_stage0 {
748 let sysroot = builder.out.join(compiler.host).join("stage0-sysroot");
750
751 let host = compiler.host;
752 let stage0_bin_dir = builder.out.join(host).join("stage0/bin");
753 let sysroot_bin_dir = sysroot.join("bin");
754 t!(fs::create_dir_all(&sysroot_bin_dir));
755 builder.cp_link_r(&stage0_bin_dir, &sysroot_bin_dir);
756
757 let stage0_lib_dir = builder.out.join(host).join("stage0/lib");
758 t!(fs::create_dir_all(sysroot.join("lib")));
759 builder.cp_link_r(&stage0_lib_dir, &sysroot.join("lib"));
760
761 let sysroot_codegen_backends = builder.sysroot_codegen_backends(compiler);
763 t!(fs::create_dir_all(&sysroot_codegen_backends));
764 let stage0_codegen_backends = builder
765 .out
766 .join(host)
767 .join("stage0/lib/rustlib")
768 .join(host)
769 .join("codegen-backends");
770 if stage0_codegen_backends.exists() {
771 builder.cp_link_r(&stage0_codegen_backends, &sysroot_codegen_backends);
772 }
773 } else if compiler.stage == 0 {
774 let sysroot = builder.out.join(compiler.host.triple).join("stage0-sysroot");
775
776 if builder.local_rebuild {
777 let _ = fs::remove_dir_all(sysroot.join("lib/rustlib/src/rust"));
781 }
782
783 builder.cp_link_r(&builder.initial_sysroot.join("lib"), &sysroot.join("lib"));
784 } else {
785 if builder.download_rustc() {
786 let _ = fs::remove_dir_all(&libdir);
788 let _ = fs::remove_dir_all(&hostdir);
789 }
790
791 add_to_sysroot(
792 builder,
793 &libdir,
794 &hostdir,
795 &build_stamp::libstd_stamp(builder, compiler, target),
796 );
797 }
798 }
799}
800
801fn copy_sanitizers(
803 builder: &Builder<'_>,
804 compiler: &Compiler,
805 target: TargetSelection,
806) -> Vec<PathBuf> {
807 let runtimes: Vec<llvm::SanitizerRuntime> = builder.ensure(llvm::Sanitizers { target });
808
809 if builder.config.dry_run() {
810 return Vec::new();
811 }
812
813 let mut target_deps = Vec::new();
814 let libdir = builder.sysroot_target_libdir(*compiler, target);
815
816 for runtime in &runtimes {
817 let dst = libdir.join(&runtime.name);
818 builder.copy_link(&runtime.path, &dst, FileType::NativeLibrary);
819
820 if target == "x86_64-apple-darwin"
824 || target == "aarch64-apple-darwin"
825 || target == "aarch64-apple-ios"
826 || target == "aarch64-apple-ios-sim"
827 || target == "x86_64-apple-ios"
828 {
829 apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", runtime.name));
831 apple_darwin_sign_file(builder, &dst);
834 }
835
836 target_deps.push(dst);
837 }
838
839 target_deps
840}
841
842fn apple_darwin_update_library_name(builder: &Builder<'_>, library_path: &Path, new_name: &str) {
843 command("install_name_tool").arg("-id").arg(new_name).arg(library_path).run(builder);
844}
845
846fn apple_darwin_sign_file(builder: &Builder<'_>, file_path: &Path) {
847 command("codesign")
848 .arg("-f") .arg("-s")
850 .arg("-")
851 .arg(file_path)
852 .run(builder);
853}
854
855#[derive(Debug, Clone, PartialEq, Eq, Hash)]
856pub struct StartupObjects {
857 pub compiler: Compiler,
858 pub target: TargetSelection,
859}
860
861impl Step for StartupObjects {
862 type Output = Vec<(PathBuf, DependencyType)>;
863
864 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
865 run.path("library/rtstartup")
866 }
867
868 fn make_run(run: RunConfig<'_>) {
869 run.builder.ensure(StartupObjects {
870 compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
871 target: run.target,
872 });
873 }
874
875 fn run(self, builder: &Builder<'_>) -> Vec<(PathBuf, DependencyType)> {
882 let for_compiler = self.compiler;
883 let target = self.target;
884 if !target.is_windows_gnu() {
885 return vec![];
886 }
887
888 let mut target_deps = vec![];
889
890 let src_dir = &builder.src.join("library").join("rtstartup");
891 let dst_dir = &builder.native_dir(target).join("rtstartup");
892 let sysroot_dir = &builder.sysroot_target_libdir(for_compiler, target);
893 t!(fs::create_dir_all(dst_dir));
894
895 for file in &["rsbegin", "rsend"] {
896 let src_file = &src_dir.join(file.to_string() + ".rs");
897 let dst_file = &dst_dir.join(file.to_string() + ".o");
898 if !up_to_date(src_file, dst_file) {
899 let mut cmd = command(&builder.initial_rustc);
900 cmd.env("RUSTC_BOOTSTRAP", "1");
901 if !builder.local_rebuild {
902 cmd.arg("--cfg").arg("bootstrap");
904 }
905 cmd.arg("--target")
906 .arg(target.rustc_target_arg())
907 .arg("--emit=obj")
908 .arg("-o")
909 .arg(dst_file)
910 .arg(src_file)
911 .run(builder);
912 }
913
914 let obj = sysroot_dir.join((*file).to_string() + ".o");
915 builder.copy_link(dst_file, &obj, FileType::NativeLibrary);
916 target_deps.push((obj, DependencyType::Target));
917 }
918
919 target_deps
920 }
921}
922
923fn cp_rustc_component_to_ci_sysroot(builder: &Builder<'_>, sysroot: &Path, contents: Vec<String>) {
924 let ci_rustc_dir = builder.config.ci_rustc_dir();
925
926 for file in contents {
927 let src = ci_rustc_dir.join(&file);
928 let dst = sysroot.join(file);
929 if src.is_dir() {
930 t!(fs::create_dir_all(dst));
931 } else {
932 builder.copy_link(&src, &dst, FileType::Regular);
933 }
934 }
935}
936
937#[derive(Clone, Debug)]
939pub struct BuiltRustc {
940 pub build_compiler: Compiler,
944}
945
946#[derive(Debug, PartialOrd, Ord, Clone, PartialEq, Eq, Hash)]
953pub struct Rustc {
954 pub target: TargetSelection,
956 pub build_compiler: Compiler,
958 crates: Vec<String>,
964}
965
966impl Rustc {
967 pub fn new(build_compiler: Compiler, target: TargetSelection) -> Self {
968 Self { target, build_compiler, crates: Default::default() }
969 }
970}
971
972impl Step for Rustc {
973 type Output = BuiltRustc;
974
975 const IS_HOST: bool = true;
976 const DEFAULT: bool = false;
977
978 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
979 let mut crates = run.builder.in_tree_crates("rustc-main", None);
980 for (i, krate) in crates.iter().enumerate() {
981 if krate.name == "rustc-main" {
984 crates.swap_remove(i);
985 break;
986 }
987 }
988 run.crates(crates)
989 }
990
991 fn make_run(run: RunConfig<'_>) {
992 if run.builder.paths == vec![PathBuf::from("compiler")] {
995 return;
996 }
997
998 let crates = run.cargo_crates_in_set();
999 run.builder.ensure(Rustc {
1000 build_compiler: run
1001 .builder
1002 .compiler(run.builder.top_stage.saturating_sub(1), run.build_triple()),
1003 target: run.target,
1004 crates,
1005 });
1006 }
1007
1008 fn run(self, builder: &Builder<'_>) -> Self::Output {
1014 let build_compiler = self.build_compiler;
1015 let target = self.target;
1016
1017 if builder.download_rustc() && build_compiler.stage != 0 {
1020 trace!(stage = build_compiler.stage, "`download_rustc` requested");
1021
1022 let sysroot =
1023 builder.ensure(Sysroot { compiler: build_compiler, force_recompile: false });
1024 cp_rustc_component_to_ci_sysroot(
1025 builder,
1026 &sysroot,
1027 builder.config.ci_rustc_dev_contents(),
1028 );
1029 return BuiltRustc { build_compiler };
1030 }
1031
1032 builder.std(build_compiler, target);
1035
1036 if builder.config.keep_stage.contains(&build_compiler.stage) {
1037 trace!(stage = build_compiler.stage, "`keep-stage` requested");
1038
1039 builder.info("WARNING: Using a potentially old librustc. This may not behave well.");
1040 builder.info("WARNING: Use `--keep-stage-std` if you want to rebuild the compiler when it changes");
1041 builder.ensure(RustcLink::from_rustc(self));
1042
1043 return BuiltRustc { build_compiler };
1044 }
1045
1046 let stage = build_compiler.stage + 1;
1048
1049 if build_compiler.stage >= 2
1052 && !builder.config.full_bootstrap
1053 && (target == builder.host_target || builder.hosts.contains(&target))
1054 {
1055 let uplift_build_compiler = builder.compiler(1, build_compiler.host);
1059 let msg = if uplift_build_compiler.host == target {
1060 format!("Uplifting rustc (stage2 -> stage{stage})")
1061 } else {
1062 format!(
1063 "Uplifting rustc (stage2:{} -> stage{stage}:{target})",
1064 uplift_build_compiler.host
1065 )
1066 };
1067 builder.info(&msg);
1068
1069 builder.ensure(RustcLink::from_build_compiler_and_sysroot(
1073 uplift_build_compiler,
1075 build_compiler,
1077 target,
1078 self.crates,
1079 ));
1080
1081 return BuiltRustc { build_compiler: uplift_build_compiler };
1084 }
1085
1086 builder.std(
1092 builder.compiler(self.build_compiler.stage, builder.config.host_target),
1093 builder.config.host_target,
1094 );
1095
1096 let mut cargo = builder::Cargo::new(
1097 builder,
1098 build_compiler,
1099 Mode::Rustc,
1100 SourceType::InTree,
1101 target,
1102 Kind::Build,
1103 );
1104
1105 rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates);
1106
1107 for krate in &*self.crates {
1111 cargo.arg("-p").arg(krate);
1112 }
1113
1114 if builder.build.config.enable_bolt_settings && build_compiler.stage == 1 {
1115 cargo.env("RUSTC_BOLT_LINK_FLAGS", "1");
1117 }
1118
1119 let _guard = builder.msg(
1120 Kind::Build,
1121 format_args!("compiler artifacts{}", crate_description(&self.crates)),
1122 Mode::Rustc,
1123 build_compiler,
1124 target,
1125 );
1126 let stamp = build_stamp::librustc_stamp(builder, build_compiler, target);
1127 run_cargo(
1128 builder,
1129 cargo,
1130 vec![],
1131 &stamp,
1132 vec![],
1133 false,
1134 true, );
1136
1137 let target_root_dir = stamp.path().parent().unwrap();
1138 if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None
1144 && builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None
1145 {
1146 let rustc_driver = target_root_dir.join("librustc_driver.so");
1147 strip_debug(builder, target, &rustc_driver);
1148 }
1149
1150 if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None {
1151 strip_debug(builder, target, &target_root_dir.join("rustc-main"));
1154 }
1155
1156 builder.ensure(RustcLink::from_rustc(self));
1157 BuiltRustc { build_compiler }
1158 }
1159
1160 fn metadata(&self) -> Option<StepMetadata> {
1161 Some(StepMetadata::build("rustc", self.target).built_by(self.build_compiler))
1162 }
1163}
1164
1165pub fn rustc_cargo(
1166 builder: &Builder<'_>,
1167 cargo: &mut Cargo,
1168 target: TargetSelection,
1169 build_compiler: &Compiler,
1170 crates: &[String],
1171) {
1172 cargo
1173 .arg("--features")
1174 .arg(builder.rustc_features(builder.kind, target, crates))
1175 .arg("--manifest-path")
1176 .arg(builder.src.join("compiler/rustc/Cargo.toml"));
1177
1178 cargo.rustdocflag("-Zcrate-attr=warn(rust_2018_idioms)");
1179
1180 cargo.rustflag("-Zon-broken-pipe=kill");
1194
1195 if builder.config.llvm_enzyme {
1198 let arch = builder.build.host_target;
1199 let enzyme_dir = builder.build.out.join(arch).join("enzyme").join("lib");
1200 cargo.rustflag("-L").rustflag(enzyme_dir.to_str().expect("Invalid path"));
1201
1202 if let Some(llvm_config) = builder.llvm_config(builder.config.host_target) {
1203 let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
1204 cargo.rustflag("-l").rustflag(&format!("Enzyme-{llvm_version_major}"));
1205 }
1206 }
1207
1208 if builder.build.config.lld_mode.is_used() {
1213 cargo.rustflag("-Zdefault-visibility=protected");
1214 }
1215
1216 if is_lto_stage(build_compiler) {
1217 match builder.config.rust_lto {
1218 RustcLto::Thin | RustcLto::Fat => {
1219 cargo.rustflag("-Zdylib-lto");
1222 let lto_type = match builder.config.rust_lto {
1226 RustcLto::Thin => "thin",
1227 RustcLto::Fat => "fat",
1228 _ => unreachable!(),
1229 };
1230 cargo.rustflag(&format!("-Clto={lto_type}"));
1231 cargo.rustflag("-Cembed-bitcode=yes");
1232 }
1233 RustcLto::ThinLocal => { }
1234 RustcLto::Off => {
1235 cargo.rustflag("-Clto=off");
1236 }
1237 }
1238 } else if builder.config.rust_lto == RustcLto::Off {
1239 cargo.rustflag("-Clto=off");
1240 }
1241
1242 if builder.config.lld_mode.is_used() && !build_compiler.host.is_msvc() {
1250 cargo.rustflag("-Clink-args=-Wl,--icf=all");
1251 }
1252
1253 if builder.config.rust_profile_use.is_some() && builder.config.rust_profile_generate.is_some() {
1254 panic!("Cannot use and generate PGO profiles at the same time");
1255 }
1256 let is_collecting = if let Some(path) = &builder.config.rust_profile_generate {
1257 if build_compiler.stage == 1 {
1258 cargo.rustflag(&format!("-Cprofile-generate={path}"));
1259 cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4");
1262 true
1263 } else {
1264 false
1265 }
1266 } else if let Some(path) = &builder.config.rust_profile_use {
1267 if build_compiler.stage == 1 {
1268 cargo.rustflag(&format!("-Cprofile-use={path}"));
1269 if builder.is_verbose() {
1270 cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function");
1271 }
1272 true
1273 } else {
1274 false
1275 }
1276 } else {
1277 false
1278 };
1279 if is_collecting {
1280 cargo.rustflag(&format!(
1282 "-Cllvm-args=-static-func-strip-dirname-prefix={}",
1283 builder.config.src.components().count()
1284 ));
1285 }
1286
1287 if let Some(ref ccache) = builder.config.ccache
1292 && build_compiler.stage == 0
1293 && !builder.config.incremental
1294 {
1295 cargo.env("RUSTC_WRAPPER", ccache);
1296 }
1297
1298 rustc_cargo_env(builder, cargo, target);
1299}
1300
1301pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1302 cargo
1305 .env("CFG_RELEASE", builder.rust_release())
1306 .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
1307 .env("CFG_VERSION", builder.rust_version());
1308
1309 if builder.config.omit_git_hash {
1313 cargo.env("CFG_OMIT_GIT_HASH", "1");
1314 }
1315
1316 if let Some(backend) = builder.config.default_codegen_backend(target) {
1317 cargo.env("CFG_DEFAULT_CODEGEN_BACKEND", backend.name());
1318 }
1319
1320 let libdir_relative = builder.config.libdir_relative().unwrap_or_else(|| Path::new("lib"));
1321 let target_config = builder.config.target_config.get(&target);
1322
1323 cargo.env("CFG_LIBDIR_RELATIVE", libdir_relative);
1324
1325 if let Some(ref ver_date) = builder.rust_info().commit_date() {
1326 cargo.env("CFG_VER_DATE", ver_date);
1327 }
1328 if let Some(ref ver_hash) = builder.rust_info().sha() {
1329 cargo.env("CFG_VER_HASH", ver_hash);
1330 }
1331 if !builder.unstable_features() {
1332 cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
1333 }
1334
1335 if let Some(s) = target_config.and_then(|c| c.default_linker.as_ref()) {
1338 cargo.env("CFG_DEFAULT_LINKER", s);
1339 } else if let Some(ref s) = builder.config.rustc_default_linker {
1340 cargo.env("CFG_DEFAULT_LINKER", s);
1341 }
1342
1343 if builder.config.lld_enabled {
1345 cargo.env("CFG_USE_SELF_CONTAINED_LINKER", "1");
1346 }
1347
1348 if builder.config.rust_verify_llvm_ir {
1349 cargo.env("RUSTC_VERIFY_LLVM_IR", "1");
1350 }
1351
1352 if builder.config.llvm_enzyme {
1353 cargo.rustflag("--cfg=llvm_enzyme");
1354 }
1355
1356 if builder.config.llvm_enabled(target) {
1368 let building_llvm_is_expensive =
1369 crate::core::build_steps::llvm::prebuilt_llvm_config(builder, target, false)
1370 .should_build();
1371
1372 let skip_llvm = (builder.kind == Kind::Check) && building_llvm_is_expensive;
1373 if !skip_llvm {
1374 rustc_llvm_env(builder, cargo, target)
1375 }
1376 }
1377
1378 if builder.config.jemalloc(target)
1382 && target.starts_with("aarch64")
1383 && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none()
1384 {
1385 cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "16");
1386 }
1387}
1388
1389fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1395 if builder.config.is_rust_llvm(target) {
1396 cargo.env("LLVM_RUSTLLVM", "1");
1397 }
1398 if builder.config.llvm_enzyme {
1399 cargo.env("LLVM_ENZYME", "1");
1400 }
1401 let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
1402 cargo.env("LLVM_CONFIG", &llvm_config);
1403
1404 let mut llvm_linker_flags = String::new();
1414 if builder.config.llvm_profile_generate
1415 && target.is_msvc()
1416 && let Some(ref clang_cl_path) = builder.config.llvm_clang_cl
1417 {
1418 let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
1420 llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
1421 }
1422
1423 if let Some(ref s) = builder.config.llvm_ldflags {
1425 if !llvm_linker_flags.is_empty() {
1426 llvm_linker_flags.push(' ');
1427 }
1428 llvm_linker_flags.push_str(s);
1429 }
1430
1431 if !llvm_linker_flags.is_empty() {
1433 cargo.env("LLVM_LINKER_FLAGS", llvm_linker_flags);
1434 }
1435
1436 if builder.config.llvm_static_stdcpp
1439 && !target.contains("freebsd")
1440 && !target.is_msvc()
1441 && !target.contains("apple")
1442 && !target.contains("solaris")
1443 {
1444 let libstdcxx_name =
1445 if target.contains("windows-gnullvm") { "libc++.a" } else { "libstdc++.a" };
1446 let file = compiler_file(
1447 builder,
1448 &builder.cxx(target).unwrap(),
1449 target,
1450 CLang::Cxx,
1451 libstdcxx_name,
1452 );
1453 cargo.env("LLVM_STATIC_STDCPP", file);
1454 }
1455 if builder.llvm_link_shared() {
1456 cargo.env("LLVM_LINK_SHARED", "1");
1457 }
1458 if builder.config.llvm_use_libcxx {
1459 cargo.env("LLVM_USE_LIBCXX", "1");
1460 }
1461 if builder.config.llvm_assertions {
1462 cargo.env("LLVM_ASSERTIONS", "1");
1463 }
1464}
1465
1466#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1479struct RustcLink {
1480 build_compiler: Compiler,
1482 sysroot_compiler: Compiler,
1485 target: TargetSelection,
1486 crates: Vec<String>,
1488}
1489
1490impl RustcLink {
1491 fn from_rustc(rustc: Rustc) -> Self {
1494 Self {
1495 build_compiler: rustc.build_compiler,
1496 sysroot_compiler: rustc.build_compiler,
1497 target: rustc.target,
1498 crates: rustc.crates,
1499 }
1500 }
1501
1502 fn from_build_compiler_and_sysroot(
1504 build_compiler: Compiler,
1505 sysroot_compiler: Compiler,
1506 target: TargetSelection,
1507 crates: Vec<String>,
1508 ) -> Self {
1509 Self { build_compiler, sysroot_compiler, target, crates }
1510 }
1511}
1512
1513impl Step for RustcLink {
1514 type Output = ();
1515
1516 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1517 run.never()
1518 }
1519
1520 fn run(self, builder: &Builder<'_>) {
1522 let build_compiler = self.build_compiler;
1523 let sysroot_compiler = self.sysroot_compiler;
1524 let target = self.target;
1525 add_to_sysroot(
1526 builder,
1527 &builder.sysroot_target_libdir(sysroot_compiler, target),
1528 &builder.sysroot_target_libdir(sysroot_compiler, sysroot_compiler.host),
1529 &build_stamp::librustc_stamp(builder, build_compiler, target),
1530 );
1531 }
1532}
1533
1534#[derive(Clone)]
1537pub struct GccCodegenBackendOutput {
1538 stamp: BuildStamp,
1539 gcc: GccOutput,
1540}
1541
1542#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1543pub struct GccCodegenBackend {
1544 compilers: RustcPrivateCompilers,
1545}
1546
1547impl Step for GccCodegenBackend {
1548 type Output = GccCodegenBackendOutput;
1549
1550 const IS_HOST: bool = true;
1551
1552 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1553 run.alias("rustc_codegen_gcc").alias("cg_gcc")
1554 }
1555
1556 fn make_run(run: RunConfig<'_>) {
1557 run.builder.ensure(GccCodegenBackend {
1558 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1559 });
1560 }
1561
1562 fn run(self, builder: &Builder<'_>) -> Self::Output {
1563 let target = self.compilers.target();
1564 let build_compiler = self.compilers.build_compiler();
1565
1566 let stamp = build_stamp::codegen_backend_stamp(
1567 builder,
1568 build_compiler,
1569 target,
1570 &CodegenBackendKind::Gcc,
1571 );
1572
1573 let gcc = builder.ensure(Gcc { target });
1574
1575 if builder.config.keep_stage.contains(&build_compiler.stage) {
1576 trace!("`keep-stage` requested");
1577 builder.info(
1578 "WARNING: Using a potentially old codegen backend. \
1579 This may not behave well.",
1580 );
1581 return GccCodegenBackendOutput { stamp, gcc };
1584 }
1585
1586 let mut cargo = builder::Cargo::new(
1587 builder,
1588 build_compiler,
1589 Mode::Codegen,
1590 SourceType::InTree,
1591 target,
1592 Kind::Build,
1593 );
1594 cargo.arg("--manifest-path").arg(builder.src.join("compiler/rustc_codegen_gcc/Cargo.toml"));
1595 rustc_cargo_env(builder, &mut cargo, target);
1596
1597 add_cg_gcc_cargo_flags(&mut cargo, &gcc);
1598
1599 let _guard =
1600 builder.msg(Kind::Build, "codegen backend gcc", Mode::Codegen, build_compiler, target);
1601 let files = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false);
1602
1603 GccCodegenBackendOutput {
1604 stamp: write_codegen_backend_stamp(stamp, files, builder.config.dry_run()),
1605 gcc,
1606 }
1607 }
1608
1609 fn metadata(&self) -> Option<StepMetadata> {
1610 Some(
1611 StepMetadata::build("rustc_codegen_gcc", self.compilers.target())
1612 .built_by(self.compilers.build_compiler()),
1613 )
1614 }
1615}
1616
1617#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1618pub struct CraneliftCodegenBackend {
1619 pub compilers: RustcPrivateCompilers,
1620}
1621
1622impl Step for CraneliftCodegenBackend {
1623 type Output = BuildStamp;
1624 const IS_HOST: bool = true;
1625
1626 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1627 run.alias("rustc_codegen_cranelift").alias("cg_clif")
1628 }
1629
1630 fn make_run(run: RunConfig<'_>) {
1631 run.builder.ensure(CraneliftCodegenBackend {
1632 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1633 });
1634 }
1635
1636 fn run(self, builder: &Builder<'_>) -> Self::Output {
1637 let target = self.compilers.target();
1638 let build_compiler = self.compilers.build_compiler();
1639
1640 let stamp = build_stamp::codegen_backend_stamp(
1641 builder,
1642 build_compiler,
1643 target,
1644 &CodegenBackendKind::Cranelift,
1645 );
1646
1647 if builder.config.keep_stage.contains(&build_compiler.stage) {
1648 trace!("`keep-stage` requested");
1649 builder.info(
1650 "WARNING: Using a potentially old codegen backend. \
1651 This may not behave well.",
1652 );
1653 return stamp;
1656 }
1657
1658 let mut cargo = builder::Cargo::new(
1659 builder,
1660 build_compiler,
1661 Mode::Codegen,
1662 SourceType::InTree,
1663 target,
1664 Kind::Build,
1665 );
1666 cargo
1667 .arg("--manifest-path")
1668 .arg(builder.src.join("compiler/rustc_codegen_cranelift/Cargo.toml"));
1669 rustc_cargo_env(builder, &mut cargo, target);
1670
1671 let _guard = builder.msg(
1672 Kind::Build,
1673 "codegen backend cranelift",
1674 Mode::Codegen,
1675 build_compiler,
1676 target,
1677 );
1678 let files = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false);
1679 write_codegen_backend_stamp(stamp, files, builder.config.dry_run())
1680 }
1681
1682 fn metadata(&self) -> Option<StepMetadata> {
1683 Some(
1684 StepMetadata::build("rustc_codegen_cranelift", self.compilers.target())
1685 .built_by(self.compilers.build_compiler()),
1686 )
1687 }
1688}
1689
1690fn write_codegen_backend_stamp(
1692 mut stamp: BuildStamp,
1693 files: Vec<PathBuf>,
1694 dry_run: bool,
1695) -> BuildStamp {
1696 if dry_run {
1697 return stamp;
1698 }
1699
1700 let mut files = files.into_iter().filter(|f| {
1701 let filename = f.file_name().unwrap().to_str().unwrap();
1702 is_dylib(f) && filename.contains("rustc_codegen_")
1703 });
1704 let codegen_backend = match files.next() {
1705 Some(f) => f,
1706 None => panic!("no dylibs built for codegen backend?"),
1707 };
1708 if let Some(f) = files.next() {
1709 panic!("codegen backend built two dylibs:\n{}\n{}", codegen_backend.display(), f.display());
1710 }
1711
1712 let codegen_backend = codegen_backend.to_str().unwrap();
1713 stamp = stamp.add_stamp(codegen_backend);
1714 t!(stamp.write());
1715 stamp
1716}
1717
1718fn copy_codegen_backends_to_sysroot(
1725 builder: &Builder<'_>,
1726 stamp: BuildStamp,
1727 target_compiler: Compiler,
1728) {
1729 let dst = builder.sysroot_codegen_backends(target_compiler);
1738 t!(fs::create_dir_all(&dst), dst);
1739
1740 if builder.config.dry_run() {
1741 return;
1742 }
1743
1744 if stamp.path().exists() {
1745 let file = get_codegen_backend_file(&stamp);
1746 builder.copy_link(
1747 &file,
1748 &dst.join(normalize_codegen_backend_name(builder, &file)),
1749 FileType::NativeLibrary,
1750 );
1751 }
1752}
1753
1754pub fn get_codegen_backend_file(stamp: &BuildStamp) -> PathBuf {
1756 PathBuf::from(t!(fs::read_to_string(stamp.path())))
1757}
1758
1759pub fn normalize_codegen_backend_name(builder: &Builder<'_>, path: &Path) -> String {
1761 let filename = path.file_name().unwrap().to_str().unwrap();
1762 let dash = filename.find('-').unwrap();
1765 let dot = filename.find('.').unwrap();
1766 format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..])
1767}
1768
1769pub fn compiler_file(
1770 builder: &Builder<'_>,
1771 compiler: &Path,
1772 target: TargetSelection,
1773 c: CLang,
1774 file: &str,
1775) -> PathBuf {
1776 if builder.config.dry_run() {
1777 return PathBuf::new();
1778 }
1779 let mut cmd = command(compiler);
1780 cmd.args(builder.cc_handled_clags(target, c));
1781 cmd.args(builder.cc_unhandled_cflags(target, GitRepo::Rustc, c));
1782 cmd.arg(format!("-print-file-name={file}"));
1783 let out = cmd.run_capture_stdout(builder).stdout();
1784 PathBuf::from(out.trim())
1785}
1786
1787#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1788pub struct Sysroot {
1789 pub compiler: Compiler,
1790 force_recompile: bool,
1792}
1793
1794impl Sysroot {
1795 pub(crate) fn new(compiler: Compiler) -> Self {
1796 Sysroot { compiler, force_recompile: false }
1797 }
1798}
1799
1800impl Step for Sysroot {
1801 type Output = PathBuf;
1802
1803 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1804 run.never()
1805 }
1806
1807 fn run(self, builder: &Builder<'_>) -> PathBuf {
1811 let compiler = self.compiler;
1812 let host_dir = builder.out.join(compiler.host);
1813
1814 let sysroot_dir = |stage| {
1815 if stage == 0 {
1816 host_dir.join("stage0-sysroot")
1817 } else if self.force_recompile && stage == compiler.stage {
1818 host_dir.join(format!("stage{stage}-test-sysroot"))
1819 } else if builder.download_rustc() && compiler.stage != builder.top_stage {
1820 host_dir.join("ci-rustc-sysroot")
1821 } else {
1822 host_dir.join(format!("stage{stage}"))
1823 }
1824 };
1825 let sysroot = sysroot_dir(compiler.stage);
1826 trace!(stage = ?compiler.stage, ?sysroot);
1827
1828 builder
1829 .verbose(|| println!("Removing sysroot {} to avoid caching bugs", sysroot.display()));
1830 let _ = fs::remove_dir_all(&sysroot);
1831 t!(fs::create_dir_all(&sysroot));
1832
1833 if compiler.stage == 0 {
1840 dist::maybe_install_llvm_target(builder, compiler.host, &sysroot);
1841 }
1842
1843 if builder.download_rustc() && compiler.stage != 0 {
1845 assert_eq!(
1846 builder.config.host_target, compiler.host,
1847 "Cross-compiling is not yet supported with `download-rustc`",
1848 );
1849
1850 for stage in 0..=2 {
1852 if stage != compiler.stage {
1853 let dir = sysroot_dir(stage);
1854 if !dir.ends_with("ci-rustc-sysroot") {
1855 let _ = fs::remove_dir_all(dir);
1856 }
1857 }
1858 }
1859
1860 let mut filtered_files = Vec::new();
1870 let mut add_filtered_files = |suffix, contents| {
1871 for path in contents {
1872 let path = Path::new(&path);
1873 if path.parent().is_some_and(|parent| parent.ends_with(suffix)) {
1874 filtered_files.push(path.file_name().unwrap().to_owned());
1875 }
1876 }
1877 };
1878 let suffix = format!("lib/rustlib/{}/lib", compiler.host);
1879 add_filtered_files(suffix.as_str(), builder.config.ci_rustc_dev_contents());
1880 add_filtered_files("lib", builder.config.ci_rust_std_contents());
1883
1884 let filtered_extensions = [
1885 OsStr::new("rmeta"),
1886 OsStr::new("rlib"),
1887 OsStr::new(std::env::consts::DLL_EXTENSION),
1889 ];
1890 let ci_rustc_dir = builder.config.ci_rustc_dir();
1891 builder.cp_link_filtered(&ci_rustc_dir, &sysroot, &|path| {
1892 if path.extension().is_none_or(|ext| !filtered_extensions.contains(&ext)) {
1893 return true;
1894 }
1895 if !path.parent().is_none_or(|p| p.ends_with(&suffix)) {
1896 return true;
1897 }
1898 if !filtered_files.iter().all(|f| f != path.file_name().unwrap()) {
1899 builder.verbose_than(1, || println!("ignoring {}", path.display()));
1900 false
1901 } else {
1902 true
1903 }
1904 });
1905 }
1906
1907 if compiler.stage != 0 {
1913 let sysroot_lib_rustlib_src = sysroot.join("lib/rustlib/src");
1914 t!(fs::create_dir_all(&sysroot_lib_rustlib_src));
1915 let sysroot_lib_rustlib_src_rust = sysroot_lib_rustlib_src.join("rust");
1916 if let Err(e) =
1917 symlink_dir(&builder.config, &builder.src, &sysroot_lib_rustlib_src_rust)
1918 {
1919 eprintln!(
1920 "ERROR: creating symbolic link `{}` to `{}` failed with {}",
1921 sysroot_lib_rustlib_src_rust.display(),
1922 builder.src.display(),
1923 e,
1924 );
1925 if builder.config.rust_remap_debuginfo {
1926 eprintln!(
1927 "ERROR: some `tests/ui` tests will fail when lacking `{}`",
1928 sysroot_lib_rustlib_src_rust.display(),
1929 );
1930 }
1931 build_helper::exit!(1);
1932 }
1933 }
1934
1935 if !builder.download_rustc() {
1937 let sysroot_lib_rustlib_rustcsrc = sysroot.join("lib/rustlib/rustc-src");
1938 t!(fs::create_dir_all(&sysroot_lib_rustlib_rustcsrc));
1939 let sysroot_lib_rustlib_rustcsrc_rust = sysroot_lib_rustlib_rustcsrc.join("rust");
1940 if let Err(e) =
1941 symlink_dir(&builder.config, &builder.src, &sysroot_lib_rustlib_rustcsrc_rust)
1942 {
1943 eprintln!(
1944 "ERROR: creating symbolic link `{}` to `{}` failed with {}",
1945 sysroot_lib_rustlib_rustcsrc_rust.display(),
1946 builder.src.display(),
1947 e,
1948 );
1949 build_helper::exit!(1);
1950 }
1951 }
1952
1953 sysroot
1954 }
1955}
1956
1957#[derive(Debug, PartialOrd, Ord, Clone, PartialEq, Eq, Hash)]
1964pub struct Assemble {
1965 pub target_compiler: Compiler,
1970}
1971
1972impl Step for Assemble {
1973 type Output = Compiler;
1974 const IS_HOST: bool = true;
1975
1976 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1977 run.path("compiler/rustc").path("compiler")
1978 }
1979
1980 fn make_run(run: RunConfig<'_>) {
1981 run.builder.ensure(Assemble {
1982 target_compiler: run.builder.compiler(run.builder.top_stage, run.target),
1983 });
1984 }
1985
1986 fn run(self, builder: &Builder<'_>) -> Compiler {
1987 let target_compiler = self.target_compiler;
1988
1989 if target_compiler.stage == 0 {
1990 trace!("stage 0 build compiler is always available, simply returning");
1991 assert_eq!(
1992 builder.config.host_target, target_compiler.host,
1993 "Cannot obtain compiler for non-native build triple at stage 0"
1994 );
1995 return target_compiler;
1997 }
1998
1999 let libdir = builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2002 let libdir_bin = libdir.parent().unwrap().join("bin");
2003 t!(fs::create_dir_all(&libdir_bin));
2004
2005 if builder.config.llvm_enabled(target_compiler.host) {
2006 trace!("target_compiler.host" = ?target_compiler.host, "LLVM enabled");
2007
2008 let llvm::LlvmResult { llvm_config, .. } =
2009 builder.ensure(llvm::Llvm { target: target_compiler.host });
2010 if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
2011 trace!("LLVM tools enabled");
2012
2013 let llvm_bin_dir =
2014 command(llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
2015 let llvm_bin_dir = Path::new(llvm_bin_dir.trim());
2016
2017 #[cfg(feature = "tracing")]
2024 let _llvm_tools_span =
2025 span!(tracing::Level::TRACE, "installing llvm tools to sysroot", ?libdir_bin)
2026 .entered();
2027 for tool in LLVM_TOOLS {
2028 trace!("installing `{tool}`");
2029 let tool_exe = exe(tool, target_compiler.host);
2030 let src_path = llvm_bin_dir.join(&tool_exe);
2031
2032 if !src_path.exists() && builder.config.llvm_from_ci {
2034 eprintln!("{} does not exist; skipping copy", src_path.display());
2035 continue;
2036 }
2037
2038 builder.resolve_symlink_and_copy(&src_path, &libdir_bin.join(&tool_exe));
2045 }
2046 }
2047 }
2048
2049 let maybe_install_llvm_bitcode_linker = || {
2050 if builder.config.llvm_bitcode_linker_enabled {
2051 trace!("llvm-bitcode-linker enabled, installing");
2052 let llvm_bitcode_linker = builder.ensure(
2053 crate::core::build_steps::tool::LlvmBitcodeLinker::from_target_compiler(
2054 builder,
2055 target_compiler,
2056 ),
2057 );
2058
2059 let bindir_self_contained = builder
2061 .sysroot(target_compiler)
2062 .join(format!("lib/rustlib/{}/bin/self-contained", target_compiler.host));
2063 let tool_exe = exe("llvm-bitcode-linker", target_compiler.host);
2064
2065 t!(fs::create_dir_all(&bindir_self_contained));
2066 builder.copy_link(
2067 &llvm_bitcode_linker.tool_path,
2068 &bindir_self_contained.join(tool_exe),
2069 FileType::Executable,
2070 );
2071 }
2072 };
2073
2074 if builder.download_rustc() {
2076 trace!("`download-rustc` requested, reusing CI compiler for stage > 0");
2077
2078 builder.std(target_compiler, target_compiler.host);
2079 let sysroot =
2080 builder.ensure(Sysroot { compiler: target_compiler, force_recompile: false });
2081 dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
2084 if target_compiler.stage == builder.top_stage {
2086 builder.info(&format!("Creating a sysroot for stage{stage} compiler (use `rustup toolchain link 'name' build/host/stage{stage}`)", stage = target_compiler.stage));
2087 }
2088
2089 maybe_install_llvm_bitcode_linker();
2092
2093 return target_compiler;
2094 }
2095
2096 debug!(
2110 "ensuring build compiler is available: compiler(stage = {}, host = {:?})",
2111 target_compiler.stage - 1,
2112 builder.config.host_target,
2113 );
2114 let build_compiler =
2115 builder.compiler(target_compiler.stage - 1, builder.config.host_target);
2116
2117 if builder.config.llvm_enzyme && !builder.config.dry_run() {
2119 debug!("`llvm_enzyme` requested");
2120 let enzyme_install = builder.ensure(llvm::Enzyme { target: build_compiler.host });
2121 if let Some(llvm_config) = builder.llvm_config(builder.config.host_target) {
2122 let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
2123 let lib_ext = std::env::consts::DLL_EXTENSION;
2124 let libenzyme = format!("libEnzyme-{llvm_version_major}");
2125 let src_lib =
2126 enzyme_install.join("build/Enzyme").join(&libenzyme).with_extension(lib_ext);
2127 let libdir = builder.sysroot_target_libdir(build_compiler, build_compiler.host);
2128 let target_libdir =
2129 builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2130 let dst_lib = libdir.join(&libenzyme).with_extension(lib_ext);
2131 let target_dst_lib = target_libdir.join(&libenzyme).with_extension(lib_ext);
2132 builder.copy_link(&src_lib, &dst_lib, FileType::NativeLibrary);
2133 builder.copy_link(&src_lib, &target_dst_lib, FileType::NativeLibrary);
2134 }
2135 }
2136
2137 debug!(
2140 ?build_compiler,
2141 "target_compiler.host" = ?target_compiler.host,
2142 "building compiler libraries to link to"
2143 );
2144
2145 let BuiltRustc { build_compiler } =
2147 builder.ensure(Rustc::new(build_compiler, target_compiler.host));
2148
2149 let stage = target_compiler.stage;
2150 let host = target_compiler.host;
2151 let (host_info, dir_name) = if build_compiler.host == host {
2152 ("".into(), "host".into())
2153 } else {
2154 (format!(" ({host})"), host.to_string())
2155 };
2156 let msg = format!(
2161 "Creating a sysroot for stage{stage} compiler{host_info} (use `rustup toolchain link 'name' build/{dir_name}/stage{stage}`)"
2162 );
2163 builder.info(&msg);
2164
2165 let stamp = build_stamp::librustc_stamp(builder, build_compiler, target_compiler.host);
2167 let proc_macros = builder
2168 .read_stamp_file(&stamp)
2169 .into_iter()
2170 .filter_map(|(path, dependency_type)| {
2171 if dependency_type == DependencyType::Host {
2172 Some(path.file_name().unwrap().to_owned().into_string().unwrap())
2173 } else {
2174 None
2175 }
2176 })
2177 .collect::<HashSet<_>>();
2178
2179 let sysroot = builder.sysroot(target_compiler);
2180 let rustc_libdir = builder.rustc_libdir(target_compiler);
2181 t!(fs::create_dir_all(&rustc_libdir));
2182 let src_libdir = builder.sysroot_target_libdir(build_compiler, host);
2183 for f in builder.read_dir(&src_libdir) {
2184 let filename = f.file_name().into_string().unwrap();
2185
2186 let is_proc_macro = proc_macros.contains(&filename);
2187 let is_dylib_or_debug = is_dylib(&f.path()) || is_debug_info(&filename);
2188
2189 let can_be_rustc_dynamic_dep = if builder
2193 .link_std_into_rustc_driver(target_compiler.host)
2194 && !target_compiler.host.is_windows()
2195 {
2196 let is_std = filename.starts_with("std-") || filename.starts_with("libstd-");
2197 !is_std
2198 } else {
2199 true
2200 };
2201
2202 if is_dylib_or_debug && can_be_rustc_dynamic_dep && !is_proc_macro {
2203 builder.copy_link(&f.path(), &rustc_libdir.join(&filename), FileType::Regular);
2204 }
2205 }
2206
2207 {
2208 #[cfg(feature = "tracing")]
2209 let _codegen_backend_span =
2210 span!(tracing::Level::DEBUG, "building requested codegen backends").entered();
2211
2212 for backend in builder.config.enabled_codegen_backends(target_compiler.host) {
2213 if builder.kind == Kind::Check && builder.top_stage == 1 {
2230 continue;
2231 }
2232
2233 let prepare_compilers = || {
2234 RustcPrivateCompilers::from_build_and_target_compiler(
2235 build_compiler,
2236 target_compiler,
2237 )
2238 };
2239
2240 match backend {
2241 CodegenBackendKind::Cranelift => {
2242 let stamp = builder
2243 .ensure(CraneliftCodegenBackend { compilers: prepare_compilers() });
2244 copy_codegen_backends_to_sysroot(builder, stamp, target_compiler);
2245 }
2246 CodegenBackendKind::Gcc => {
2247 let output =
2248 builder.ensure(GccCodegenBackend { compilers: prepare_compilers() });
2249 copy_codegen_backends_to_sysroot(builder, output.stamp, target_compiler);
2250 output.gcc.install_to(builder, &rustc_libdir);
2253 }
2254 CodegenBackendKind::Llvm | CodegenBackendKind::Custom(_) => continue,
2255 }
2256 }
2257 }
2258
2259 if builder.config.lld_enabled {
2260 let lld_wrapper =
2261 builder.ensure(crate::core::build_steps::tool::LldWrapper::for_use_by_compiler(
2262 builder,
2263 target_compiler,
2264 ));
2265 copy_lld_artifacts(builder, lld_wrapper, target_compiler);
2266 }
2267
2268 if builder.config.llvm_enabled(target_compiler.host) && builder.config.llvm_tools_enabled {
2269 debug!(
2270 "llvm and llvm tools enabled; copying `llvm-objcopy` as `rust-objcopy` to \
2271 workaround faulty homebrew `strip`s"
2272 );
2273
2274 let src_exe = exe("llvm-objcopy", target_compiler.host);
2281 let dst_exe = exe("rust-objcopy", target_compiler.host);
2282 builder.copy_link(
2283 &libdir_bin.join(src_exe),
2284 &libdir_bin.join(dst_exe),
2285 FileType::Executable,
2286 );
2287 }
2288
2289 if builder.tool_enabled("wasm-component-ld") {
2292 let wasm_component = builder.ensure(
2293 crate::core::build_steps::tool::WasmComponentLd::for_use_by_compiler(
2294 builder,
2295 target_compiler,
2296 ),
2297 );
2298 builder.copy_link(
2299 &wasm_component.tool_path,
2300 &libdir_bin.join(wasm_component.tool_path.file_name().unwrap()),
2301 FileType::Executable,
2302 );
2303 }
2304
2305 maybe_install_llvm_bitcode_linker();
2306
2307 debug!(
2310 "target_compiler.host" = ?target_compiler.host,
2311 ?sysroot,
2312 "ensuring availability of `libLLVM.so` in compiler directory"
2313 );
2314 dist::maybe_install_llvm_runtime(builder, target_compiler.host, &sysroot);
2315 dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
2316
2317 let out_dir = builder.cargo_out(build_compiler, Mode::Rustc, host);
2319 let rustc = out_dir.join(exe("rustc-main", host));
2320 let bindir = sysroot.join("bin");
2321 t!(fs::create_dir_all(bindir));
2322 let compiler = builder.rustc(target_compiler);
2323 debug!(src = ?rustc, dst = ?compiler, "linking compiler binary itself");
2324 builder.copy_link(&rustc, &compiler, FileType::Executable);
2325
2326 target_compiler
2327 }
2328}
2329
2330#[track_caller]
2335pub fn add_to_sysroot(
2336 builder: &Builder<'_>,
2337 sysroot_dst: &Path,
2338 sysroot_host_dst: &Path,
2339 stamp: &BuildStamp,
2340) {
2341 let self_contained_dst = &sysroot_dst.join("self-contained");
2342 t!(fs::create_dir_all(sysroot_dst));
2343 t!(fs::create_dir_all(sysroot_host_dst));
2344 t!(fs::create_dir_all(self_contained_dst));
2345 for (path, dependency_type) in builder.read_stamp_file(stamp) {
2346 let dst = match dependency_type {
2347 DependencyType::Host => sysroot_host_dst,
2348 DependencyType::Target => sysroot_dst,
2349 DependencyType::TargetSelfContained => self_contained_dst,
2350 };
2351 builder.copy_link(&path, &dst.join(path.file_name().unwrap()), FileType::Regular);
2352 }
2353}
2354
2355pub fn run_cargo(
2356 builder: &Builder<'_>,
2357 cargo: Cargo,
2358 tail_args: Vec<String>,
2359 stamp: &BuildStamp,
2360 additional_target_deps: Vec<(PathBuf, DependencyType)>,
2361 is_check: bool,
2362 rlib_only_metadata: bool,
2363) -> Vec<PathBuf> {
2364 let target_root_dir = stamp.path().parent().unwrap();
2366 let target_deps_dir = target_root_dir.join("deps");
2368 let host_root_dir = target_root_dir
2370 .parent()
2371 .unwrap() .parent()
2373 .unwrap() .join(target_root_dir.file_name().unwrap());
2375
2376 let mut deps = Vec::new();
2380 let mut toplevel = Vec::new();
2381 let ok = stream_cargo(builder, cargo, tail_args, &mut |msg| {
2382 let (filenames_vec, crate_types) = match msg {
2383 CargoMessage::CompilerArtifact {
2384 filenames,
2385 target: CargoTarget { crate_types },
2386 ..
2387 } => {
2388 let mut f: Vec<String> = filenames.into_iter().map(|s| s.into_owned()).collect();
2389 f.sort(); (f, crate_types)
2391 }
2392 _ => return,
2393 };
2394 for filename in filenames_vec {
2395 let mut keep = false;
2397 if filename.ends_with(".lib")
2398 || filename.ends_with(".a")
2399 || is_debug_info(&filename)
2400 || is_dylib(Path::new(&*filename))
2401 {
2402 keep = true;
2404 }
2405 if is_check && filename.ends_with(".rmeta") {
2406 keep = true;
2408 } else if rlib_only_metadata {
2409 if filename.contains("jemalloc_sys")
2410 || filename.contains("rustc_public_bridge")
2411 || filename.contains("rustc_public")
2412 {
2413 keep |= filename.ends_with(".rlib");
2416 } else {
2417 keep |= filename.ends_with(".rmeta");
2421 }
2422 } else {
2423 keep |= filename.ends_with(".rlib");
2425 }
2426
2427 if !keep {
2428 continue;
2429 }
2430
2431 let filename = Path::new(&*filename);
2432
2433 if filename.starts_with(&host_root_dir) {
2436 if crate_types.iter().any(|t| t == "proc-macro") {
2438 deps.push((filename.to_path_buf(), DependencyType::Host));
2439 }
2440 continue;
2441 }
2442
2443 if filename.starts_with(&target_deps_dir) {
2446 deps.push((filename.to_path_buf(), DependencyType::Target));
2447 continue;
2448 }
2449
2450 let expected_len = t!(filename.metadata()).len();
2461 let filename = filename.file_name().unwrap().to_str().unwrap();
2462 let mut parts = filename.splitn(2, '.');
2463 let file_stem = parts.next().unwrap().to_owned();
2464 let extension = parts.next().unwrap().to_owned();
2465
2466 toplevel.push((file_stem, extension, expected_len));
2467 }
2468 });
2469
2470 if !ok {
2471 crate::exit!(1);
2472 }
2473
2474 if builder.config.dry_run() {
2475 return Vec::new();
2476 }
2477
2478 let contents = target_deps_dir
2482 .read_dir()
2483 .unwrap_or_else(|e| panic!("Couldn't read {}: {}", target_deps_dir.display(), e))
2484 .map(|e| t!(e))
2485 .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata())))
2486 .collect::<Vec<_>>();
2487 for (prefix, extension, expected_len) in toplevel {
2488 let candidates = contents.iter().filter(|&(_, filename, meta)| {
2489 meta.len() == expected_len
2490 && filename
2491 .strip_prefix(&prefix[..])
2492 .map(|s| s.starts_with('-') && s.ends_with(&extension[..]))
2493 .unwrap_or(false)
2494 });
2495 let max = candidates.max_by_key(|&(_, _, metadata)| {
2496 metadata.modified().expect("mtime should be available on all relevant OSes")
2497 });
2498 let path_to_add = match max {
2499 Some(triple) => triple.0.to_str().unwrap(),
2500 None => panic!("no output generated for {prefix:?} {extension:?}"),
2501 };
2502 if is_dylib(Path::new(path_to_add)) {
2503 let candidate = format!("{path_to_add}.lib");
2504 let candidate = PathBuf::from(candidate);
2505 if candidate.exists() {
2506 deps.push((candidate, DependencyType::Target));
2507 }
2508 }
2509 deps.push((path_to_add.into(), DependencyType::Target));
2510 }
2511
2512 deps.extend(additional_target_deps);
2513 deps.sort();
2514 let mut new_contents = Vec::new();
2515 for (dep, dependency_type) in deps.iter() {
2516 new_contents.extend(match *dependency_type {
2517 DependencyType::Host => b"h",
2518 DependencyType::Target => b"t",
2519 DependencyType::TargetSelfContained => b"s",
2520 });
2521 new_contents.extend(dep.to_str().unwrap().as_bytes());
2522 new_contents.extend(b"\0");
2523 }
2524 t!(fs::write(stamp.path(), &new_contents));
2525 deps.into_iter().map(|(d, _)| d).collect()
2526}
2527
2528pub fn stream_cargo(
2529 builder: &Builder<'_>,
2530 cargo: Cargo,
2531 tail_args: Vec<String>,
2532 cb: &mut dyn FnMut(CargoMessage<'_>),
2533) -> bool {
2534 let mut cmd = cargo.into_cmd();
2535
2536 let mut message_format = if builder.config.json_output {
2539 String::from("json")
2540 } else {
2541 String::from("json-render-diagnostics")
2542 };
2543 if let Some(s) = &builder.config.rustc_error_format {
2544 message_format.push_str(",json-diagnostic-");
2545 message_format.push_str(s);
2546 }
2547 cmd.arg("--message-format").arg(message_format);
2548
2549 for arg in tail_args {
2550 cmd.arg(arg);
2551 }
2552
2553 builder.verbose(|| println!("running: {cmd:?}"));
2554
2555 let streaming_command = cmd.stream_capture_stdout(&builder.config.exec_ctx);
2556
2557 let Some(mut streaming_command) = streaming_command else {
2558 return true;
2559 };
2560
2561 let stdout = BufReader::new(streaming_command.stdout.take().unwrap());
2565 for line in stdout.lines() {
2566 let line = t!(line);
2567 match serde_json::from_str::<CargoMessage<'_>>(&line) {
2568 Ok(msg) => {
2569 if builder.config.json_output {
2570 println!("{line}");
2572 }
2573 cb(msg)
2574 }
2575 Err(_) => println!("{line}"),
2577 }
2578 }
2579
2580 let status = t!(streaming_command.wait(&builder.config.exec_ctx));
2582 if builder.is_verbose() && !status.success() {
2583 eprintln!(
2584 "command did not execute successfully: {cmd:?}\n\
2585 expected success, got: {status}"
2586 );
2587 }
2588
2589 status.success()
2590}
2591
2592#[derive(Deserialize)]
2593pub struct CargoTarget<'a> {
2594 crate_types: Vec<Cow<'a, str>>,
2595}
2596
2597#[derive(Deserialize)]
2598#[serde(tag = "reason", rename_all = "kebab-case")]
2599pub enum CargoMessage<'a> {
2600 CompilerArtifact { filenames: Vec<Cow<'a, str>>, target: CargoTarget<'a> },
2601 BuildScriptExecuted,
2602 BuildFinished,
2603}
2604
2605pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path) {
2606 if target != "x86_64-unknown-linux-gnu"
2610 || !builder.config.is_host_target(target)
2611 || !path.exists()
2612 {
2613 return;
2614 }
2615
2616 let previous_mtime = t!(t!(path.metadata()).modified());
2617 let stamp = BuildStamp::new(path.parent().unwrap())
2618 .with_prefix(path.file_name().unwrap().to_str().unwrap())
2619 .with_prefix("strip")
2620 .add_stamp(previous_mtime.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos());
2621
2622 if !stamp.is_up_to_date() {
2625 command("strip").arg("--strip-debug").arg(path).run_capture(builder);
2626 }
2627 t!(stamp.write());
2628
2629 let file = t!(fs::File::open(path));
2630
2631 t!(file.set_modified(previous_mtime));
2644}
2645
2646pub fn is_lto_stage(build_compiler: &Compiler) -> bool {
2648 build_compiler.stage != 0
2649}