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::{env, fs, str};
16
17use serde_derive::Deserialize;
18#[cfg(feature = "tracing")]
19use tracing::{instrument, span};
20
21use crate::core::build_steps::gcc::{Gcc, add_cg_gcc_cargo_flags};
22use crate::core::build_steps::tool::SourceType;
23use crate::core::build_steps::{dist, llvm};
24use crate::core::builder;
25use crate::core::builder::{
26 Builder, Cargo, Kind, PathSet, RunConfig, ShouldRun, Step, StepMetadata, TaskPath,
27 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::{CLang, Compiler, DependencyType, FileType, GitRepo, LLVM_TOOLS, Mode, debug, trace};
37
38#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
40pub struct Std {
41 pub target: TargetSelection,
42 pub compiler: Compiler,
43 crates: Vec<String>,
47 force_recompile: bool,
50 extra_rust_args: &'static [&'static str],
51 is_for_mir_opt_tests: bool,
52}
53
54impl Std {
55 pub fn new(compiler: Compiler, target: TargetSelection) -> Self {
56 Self {
57 target,
58 compiler,
59 crates: Default::default(),
60 force_recompile: false,
61 extra_rust_args: &[],
62 is_for_mir_opt_tests: false,
63 }
64 }
65
66 pub fn force_recompile(mut self, force_recompile: bool) -> Self {
67 self.force_recompile = force_recompile;
68 self
69 }
70
71 #[expect(clippy::wrong_self_convention)]
72 pub fn is_for_mir_opt_tests(mut self, is_for_mir_opt_tests: bool) -> Self {
73 self.is_for_mir_opt_tests = is_for_mir_opt_tests;
74 self
75 }
76
77 pub fn extra_rust_args(mut self, extra_rust_args: &'static [&'static str]) -> Self {
78 self.extra_rust_args = extra_rust_args;
79 self
80 }
81
82 fn copy_extra_objects(
83 &self,
84 builder: &Builder<'_>,
85 compiler: &Compiler,
86 target: TargetSelection,
87 ) -> Vec<(PathBuf, DependencyType)> {
88 let mut deps = Vec::new();
89 if !self.is_for_mir_opt_tests {
90 deps.extend(copy_third_party_objects(builder, compiler, target));
91 deps.extend(copy_self_contained_objects(builder, compiler, target));
92 }
93 deps
94 }
95}
96
97impl Step for Std {
98 type Output = ();
99 const DEFAULT: bool = true;
100
101 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
102 run.crate_or_deps("sysroot").path("library")
103 }
104
105 #[cfg_attr(feature = "tracing", instrument(level = "trace", name = "Std::make_run", skip_all))]
106 fn make_run(run: RunConfig<'_>) {
107 let crates = std_crates_for_run_make(&run);
108 let builder = run.builder;
109
110 let force_recompile = builder.rust_info().is_managed_git_subrepository()
114 && builder.download_rustc()
115 && builder.config.has_changes_from_upstream(&["library"]);
116
117 trace!("is managed git repo: {}", builder.rust_info().is_managed_git_subrepository());
118 trace!("download_rustc: {}", builder.download_rustc());
119 trace!(force_recompile);
120
121 run.builder.ensure(Std {
122 compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
123 target: run.target,
124 crates,
125 force_recompile,
126 extra_rust_args: &[],
127 is_for_mir_opt_tests: false,
128 });
129 }
130
131 #[cfg_attr(
137 feature = "tracing",
138 instrument(
139 level = "debug",
140 name = "Std::run",
141 skip_all,
142 fields(
143 target = ?self.target,
144 compiler = ?self.compiler,
145 force_recompile = self.force_recompile
146 ),
147 ),
148 )]
149 fn run(self, builder: &Builder<'_>) {
150 let target = self.target;
151
152 if self.compiler.stage == 0 {
154 let compiler = self.compiler;
155 builder.ensure(StdLink::from_std(self, compiler));
156
157 return;
158 }
159
160 let compiler = if builder.download_rustc() && self.force_recompile {
161 builder.compiler(self.compiler.stage.saturating_sub(1), builder.config.host_target)
164 } else {
165 self.compiler
166 };
167
168 if builder.download_rustc()
171 && builder.config.is_host_target(target)
172 && !self.force_recompile
173 {
174 let sysroot = builder.ensure(Sysroot { compiler, force_recompile: false });
175 cp_rustc_component_to_ci_sysroot(
176 builder,
177 &sysroot,
178 builder.config.ci_rust_std_contents(),
179 );
180 return;
181 }
182
183 if builder.config.keep_stage.contains(&compiler.stage)
184 || builder.config.keep_stage_std.contains(&compiler.stage)
185 {
186 trace!(keep_stage = ?builder.config.keep_stage);
187 trace!(keep_stage_std = ?builder.config.keep_stage_std);
188
189 builder.info("WARNING: Using a potentially old libstd. This may not behave well.");
190
191 builder.ensure(StartupObjects { compiler, target });
192
193 self.copy_extra_objects(builder, &compiler, target);
194
195 builder.ensure(StdLink::from_std(self, compiler));
196 return;
197 }
198
199 let mut target_deps = builder.ensure(StartupObjects { compiler, target });
200
201 let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
202 trace!(?compiler_to_use);
203
204 if compiler_to_use != compiler
205 && compiler.stage > 1
210 {
211 trace!(?compiler_to_use, ?compiler, "compiler != compiler_to_use, uplifting library");
212
213 builder.std(compiler_to_use, target);
214 let msg = if compiler_to_use.host == target {
215 format!(
216 "Uplifting library (stage{} -> stage{})",
217 compiler_to_use.stage, compiler.stage
218 )
219 } else {
220 format!(
221 "Uplifting library (stage{}:{} -> stage{}:{})",
222 compiler_to_use.stage, compiler_to_use.host, compiler.stage, target
223 )
224 };
225 builder.info(&msg);
226
227 self.copy_extra_objects(builder, &compiler, target);
230
231 builder.ensure(StdLink::from_std(self, compiler_to_use));
232 return;
233 }
234
235 trace!(
236 ?compiler_to_use,
237 ?compiler,
238 "compiler == compiler_to_use, handling not-cross-compile scenario"
239 );
240
241 target_deps.extend(self.copy_extra_objects(builder, &compiler, target));
242
243 let mut cargo = if self.is_for_mir_opt_tests {
247 trace!("building special sysroot for mir-opt tests");
248 let mut cargo = builder::Cargo::new_for_mir_opt_tests(
249 builder,
250 compiler,
251 Mode::Std,
252 SourceType::InTree,
253 target,
254 Kind::Check,
255 );
256 cargo.rustflag("-Zalways-encode-mir");
257 cargo.arg("--manifest-path").arg(builder.src.join("library/sysroot/Cargo.toml"));
258 cargo
259 } else {
260 trace!("building regular sysroot");
261 let mut cargo = builder::Cargo::new(
262 builder,
263 compiler,
264 Mode::Std,
265 SourceType::InTree,
266 target,
267 Kind::Build,
268 );
269 std_cargo(builder, target, compiler.stage, &mut cargo);
270 for krate in &*self.crates {
271 cargo.arg("-p").arg(krate);
272 }
273 cargo
274 };
275
276 if target.is_synthetic() {
278 cargo.env("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET", "1");
279 }
280 for rustflag in self.extra_rust_args.iter() {
281 cargo.rustflag(rustflag);
282 }
283
284 let _guard = builder.msg(
285 Kind::Build,
286 compiler.stage,
287 format_args!("library artifacts{}", crate_description(&self.crates)),
288 compiler.host,
289 target,
290 );
291 run_cargo(
292 builder,
293 cargo,
294 vec![],
295 &build_stamp::libstd_stamp(builder, compiler, target),
296 target_deps,
297 self.is_for_mir_opt_tests, false,
299 );
300
301 builder.ensure(StdLink::from_std(
302 self,
303 builder.compiler(compiler.stage, builder.config.host_target),
304 ));
305 }
306
307 fn metadata(&self) -> Option<StepMetadata> {
308 Some(StepMetadata::build("std", self.target).built_by(self.compiler))
309 }
310}
311
312fn copy_and_stamp(
313 builder: &Builder<'_>,
314 libdir: &Path,
315 sourcedir: &Path,
316 name: &str,
317 target_deps: &mut Vec<(PathBuf, DependencyType)>,
318 dependency_type: DependencyType,
319) {
320 let target = libdir.join(name);
321 builder.copy_link(&sourcedir.join(name), &target, FileType::Regular);
322
323 target_deps.push((target, dependency_type));
324}
325
326fn copy_llvm_libunwind(builder: &Builder<'_>, target: TargetSelection, libdir: &Path) -> PathBuf {
327 let libunwind_path = builder.ensure(llvm::Libunwind { target });
328 let libunwind_source = libunwind_path.join("libunwind.a");
329 let libunwind_target = libdir.join("libunwind.a");
330 builder.copy_link(&libunwind_source, &libunwind_target, FileType::NativeLibrary);
331 libunwind_target
332}
333
334fn copy_third_party_objects(
336 builder: &Builder<'_>,
337 compiler: &Compiler,
338 target: TargetSelection,
339) -> Vec<(PathBuf, DependencyType)> {
340 let mut target_deps = vec![];
341
342 if builder.config.needs_sanitizer_runtime_built(target) && compiler.stage != 0 {
343 target_deps.extend(
346 copy_sanitizers(builder, compiler, target)
347 .into_iter()
348 .map(|d| (d, DependencyType::Target)),
349 );
350 }
351
352 if target == "x86_64-fortanix-unknown-sgx"
353 || builder.config.llvm_libunwind(target) == LlvmLibunwind::InTree
354 && (target.contains("linux") || target.contains("fuchsia") || target.contains("aix"))
355 {
356 let libunwind_path =
357 copy_llvm_libunwind(builder, target, &builder.sysroot_target_libdir(*compiler, target));
358 target_deps.push((libunwind_path, DependencyType::Target));
359 }
360
361 target_deps
362}
363
364fn copy_self_contained_objects(
366 builder: &Builder<'_>,
367 compiler: &Compiler,
368 target: TargetSelection,
369) -> Vec<(PathBuf, DependencyType)> {
370 let libdir_self_contained =
371 builder.sysroot_target_libdir(*compiler, target).join("self-contained");
372 t!(fs::create_dir_all(&libdir_self_contained));
373 let mut target_deps = vec![];
374
375 if target.needs_crt_begin_end() {
383 let srcdir = builder.musl_libdir(target).unwrap_or_else(|| {
384 panic!("Target {:?} does not have a \"musl-libdir\" key", target.triple)
385 });
386 if !target.starts_with("wasm32") {
387 for &obj in &["libc.a", "crt1.o", "Scrt1.o", "rcrt1.o", "crti.o", "crtn.o"] {
388 copy_and_stamp(
389 builder,
390 &libdir_self_contained,
391 &srcdir,
392 obj,
393 &mut target_deps,
394 DependencyType::TargetSelfContained,
395 );
396 }
397 let crt_path = builder.ensure(llvm::CrtBeginEnd { target });
398 for &obj in &["crtbegin.o", "crtbeginS.o", "crtend.o", "crtendS.o"] {
399 let src = crt_path.join(obj);
400 let target = libdir_self_contained.join(obj);
401 builder.copy_link(&src, &target, FileType::NativeLibrary);
402 target_deps.push((target, DependencyType::TargetSelfContained));
403 }
404 } else {
405 for &obj in &["libc.a", "crt1-command.o"] {
408 copy_and_stamp(
409 builder,
410 &libdir_self_contained,
411 &srcdir,
412 obj,
413 &mut target_deps,
414 DependencyType::TargetSelfContained,
415 );
416 }
417 }
418 if !target.starts_with("s390x") {
419 let libunwind_path = copy_llvm_libunwind(builder, target, &libdir_self_contained);
420 target_deps.push((libunwind_path, DependencyType::TargetSelfContained));
421 }
422 } else if target.contains("-wasi") {
423 let srcdir = builder.wasi_libdir(target).unwrap_or_else(|| {
424 panic!(
425 "Target {:?} does not have a \"wasi-root\" key in bootstrap.toml \
426 or `$WASI_SDK_PATH` set",
427 target.triple
428 )
429 });
430 for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] {
431 copy_and_stamp(
432 builder,
433 &libdir_self_contained,
434 &srcdir,
435 obj,
436 &mut target_deps,
437 DependencyType::TargetSelfContained,
438 );
439 }
440 } else if target.is_windows_gnu() {
441 for obj in ["crt2.o", "dllcrt2.o"].iter() {
442 let src = compiler_file(builder, &builder.cc(target), target, CLang::C, obj);
443 let dst = libdir_self_contained.join(obj);
444 builder.copy_link(&src, &dst, FileType::NativeLibrary);
445 target_deps.push((dst, DependencyType::TargetSelfContained));
446 }
447 }
448
449 target_deps
450}
451
452pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec<String> {
454 if cfg!(test) {
456 return vec![];
457 }
458
459 let has_alias = run.paths.iter().any(|set| set.assert_single_path().path.ends_with("library"));
460 let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);
461
462 if target_is_no_std {
464 vec![]
465 }
466 else if has_alias {
468 run.make_run_crates(builder::Alias::Library)
469 } else {
470 run.cargo_crates_in_set()
471 }
472}
473
474fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
480 if builder.config.llvm_from_ci {
482 builder.config.maybe_download_ci_llvm();
484 let ci_llvm_compiler_rt = builder.config.ci_llvm_root().join("compiler-rt");
485 if ci_llvm_compiler_rt.exists() {
486 return ci_llvm_compiler_rt;
487 }
488 }
489
490 builder.require_submodule("src/llvm-project", {
492 Some("The `build.profiler` config option requires `compiler-rt` sources from LLVM.")
493 });
494 builder.src.join("src/llvm-project/compiler-rt")
495}
496
497pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, cargo: &mut Cargo) {
500 if target.contains("apple") && !builder.config.dry_run() {
518 let mut cmd = command(builder.rustc(cargo.compiler()));
522 cmd.arg("--target").arg(target.rustc_target_arg());
523 cmd.arg("--print=deployment-target");
524 let output = cmd.run_capture_stdout(builder).stdout();
525
526 let (env_var, value) = output.split_once('=').unwrap();
527 cargo.env(env_var.trim(), value.trim());
530
531 if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
541 cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
542 }
543 }
544
545 if let Some(path) = builder.config.profiler_path(target) {
547 cargo.env("LLVM_PROFILER_RT_LIB", path);
548 } else if builder.config.profiler_enabled(target) {
549 let compiler_rt = compiler_rt_for_profiler(builder);
550 cargo.env("RUST_COMPILER_RT_FOR_PROFILER", compiler_rt);
554 }
555
556 let compiler_builtins_c_feature = if builder.config.optimized_compiler_builtins(target) {
570 builder.require_submodule(
578 "src/llvm-project",
579 Some(
580 "The `build.optimized-compiler-builtins` config option \
581 requires `compiler-rt` sources from LLVM.",
582 ),
583 );
584 let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
585 assert!(compiler_builtins_root.exists());
586 cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);
589 " compiler-builtins-c"
590 } else {
591 ""
592 };
593
594 if !builder.unstable_features() {
597 cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
598 }
599
600 let mut features = String::new();
601
602 if stage != 0 && builder.config.default_codegen_backend(target).as_deref() == Some("cranelift")
603 {
604 features += "compiler-builtins-no-f16-f128 ";
605 }
606
607 if builder.no_std(target) == Some(true) {
608 features += " compiler-builtins-mem";
609 if !target.starts_with("bpf") {
610 features.push_str(compiler_builtins_c_feature);
611 }
612
613 cargo
615 .args(["-p", "alloc"])
616 .arg("--manifest-path")
617 .arg(builder.src.join("library/alloc/Cargo.toml"))
618 .arg("--features")
619 .arg(features);
620 } else {
621 features += &builder.std_features(target);
622 features.push_str(compiler_builtins_c_feature);
623
624 cargo
625 .arg("--features")
626 .arg(features)
627 .arg("--manifest-path")
628 .arg(builder.src.join("library/sysroot/Cargo.toml"));
629
630 if target.contains("musl")
633 && let Some(p) = builder.musl_libdir(target)
634 {
635 let root = format!("native={}", p.to_str().unwrap());
636 cargo.rustflag("-L").rustflag(&root);
637 }
638
639 if target.contains("-wasi")
640 && let Some(dir) = builder.wasi_libdir(target)
641 {
642 let root = format!("native={}", dir.to_str().unwrap());
643 cargo.rustflag("-L").rustflag(&root);
644 }
645 }
646
647 if stage >= 1 {
656 cargo.rustflag("-Cembed-bitcode=yes");
657 }
658 if builder.config.rust_lto == RustcLto::Off {
659 cargo.rustflag("-Clto=off");
660 }
661
662 if target.contains("riscv") {
669 cargo.rustflag("-Cforce-unwind-tables=yes");
670 }
671
672 cargo.rustflag("-Zunstable-options");
675 cargo.rustflag("-Cforce-frame-pointers=non-leaf");
676
677 let html_root =
678 format!("-Zcrate-attr=doc(html_root_url=\"{}/\")", builder.doc_rust_lang_org_channel(),);
679 cargo.rustflag(&html_root);
680 cargo.rustdocflag(&html_root);
681
682 cargo.rustdocflag("-Zcrate-attr=warn(rust_2018_idioms)");
683}
684
685#[derive(Debug, Clone, PartialEq, Eq, Hash)]
686pub struct StdLink {
687 pub compiler: Compiler,
688 pub target_compiler: Compiler,
689 pub target: TargetSelection,
690 crates: Vec<String>,
692 force_recompile: bool,
694}
695
696impl StdLink {
697 pub fn from_std(std: Std, host_compiler: Compiler) -> Self {
698 Self {
699 compiler: host_compiler,
700 target_compiler: std.compiler,
701 target: std.target,
702 crates: std.crates,
703 force_recompile: std.force_recompile,
704 }
705 }
706}
707
708impl Step for StdLink {
709 type Output = ();
710
711 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
712 run.never()
713 }
714
715 #[cfg_attr(
724 feature = "tracing",
725 instrument(
726 level = "trace",
727 name = "StdLink::run",
728 skip_all,
729 fields(
730 compiler = ?self.compiler,
731 target_compiler = ?self.target_compiler,
732 target = ?self.target
733 ),
734 ),
735 )]
736 fn run(self, builder: &Builder<'_>) {
737 let compiler = self.compiler;
738 let target_compiler = self.target_compiler;
739 let target = self.target;
740
741 let (libdir, hostdir) = if !self.force_recompile && builder.download_rustc() {
743 let lib = builder.sysroot_libdir_relative(self.compiler);
745 let sysroot = builder.ensure(crate::core::build_steps::compile::Sysroot {
746 compiler: self.compiler,
747 force_recompile: self.force_recompile,
748 });
749 let libdir = sysroot.join(lib).join("rustlib").join(target).join("lib");
750 let hostdir = sysroot.join(lib).join("rustlib").join(compiler.host).join("lib");
751 (libdir, hostdir)
752 } else {
753 let libdir = builder.sysroot_target_libdir(target_compiler, target);
754 let hostdir = builder.sysroot_target_libdir(target_compiler, compiler.host);
755 (libdir, hostdir)
756 };
757
758 let is_downloaded_beta_stage0 = builder
759 .build
760 .config
761 .initial_rustc
762 .starts_with(builder.out.join(compiler.host).join("stage0/bin"));
763
764 if compiler.stage == 0 && is_downloaded_beta_stage0 {
768 let sysroot = builder.out.join(compiler.host).join("stage0-sysroot");
770
771 let host = compiler.host;
772 let stage0_bin_dir = builder.out.join(host).join("stage0/bin");
773 let sysroot_bin_dir = sysroot.join("bin");
774 t!(fs::create_dir_all(&sysroot_bin_dir));
775 builder.cp_link_r(&stage0_bin_dir, &sysroot_bin_dir);
776
777 let stage0_lib_dir = builder.out.join(host).join("stage0/lib");
778 t!(fs::create_dir_all(sysroot.join("lib")));
779 builder.cp_link_r(&stage0_lib_dir, &sysroot.join("lib"));
780
781 let sysroot_codegen_backends = builder.sysroot_codegen_backends(compiler);
783 t!(fs::create_dir_all(&sysroot_codegen_backends));
784 let stage0_codegen_backends = builder
785 .out
786 .join(host)
787 .join("stage0/lib/rustlib")
788 .join(host)
789 .join("codegen-backends");
790 if stage0_codegen_backends.exists() {
791 builder.cp_link_r(&stage0_codegen_backends, &sysroot_codegen_backends);
792 }
793 } else if compiler.stage == 0 {
794 let sysroot = builder.out.join(compiler.host.triple).join("stage0-sysroot");
795
796 if builder.local_rebuild {
797 let _ = fs::remove_dir_all(sysroot.join("lib/rustlib/src/rust"));
801 }
802
803 builder.cp_link_r(&builder.initial_sysroot.join("lib"), &sysroot.join("lib"));
804 } else {
805 if builder.download_rustc() {
806 let _ = fs::remove_dir_all(&libdir);
808 let _ = fs::remove_dir_all(&hostdir);
809 }
810
811 add_to_sysroot(
812 builder,
813 &libdir,
814 &hostdir,
815 &build_stamp::libstd_stamp(builder, compiler, target),
816 );
817 }
818 }
819}
820
821fn copy_sanitizers(
823 builder: &Builder<'_>,
824 compiler: &Compiler,
825 target: TargetSelection,
826) -> Vec<PathBuf> {
827 let runtimes: Vec<llvm::SanitizerRuntime> = builder.ensure(llvm::Sanitizers { target });
828
829 if builder.config.dry_run() {
830 return Vec::new();
831 }
832
833 let mut target_deps = Vec::new();
834 let libdir = builder.sysroot_target_libdir(*compiler, target);
835
836 for runtime in &runtimes {
837 let dst = libdir.join(&runtime.name);
838 builder.copy_link(&runtime.path, &dst, FileType::NativeLibrary);
839
840 if target == "x86_64-apple-darwin"
844 || target == "aarch64-apple-darwin"
845 || target == "aarch64-apple-ios"
846 || target == "aarch64-apple-ios-sim"
847 || target == "x86_64-apple-ios"
848 {
849 apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", runtime.name));
851 apple_darwin_sign_file(builder, &dst);
854 }
855
856 target_deps.push(dst);
857 }
858
859 target_deps
860}
861
862fn apple_darwin_update_library_name(builder: &Builder<'_>, library_path: &Path, new_name: &str) {
863 command("install_name_tool").arg("-id").arg(new_name).arg(library_path).run(builder);
864}
865
866fn apple_darwin_sign_file(builder: &Builder<'_>, file_path: &Path) {
867 command("codesign")
868 .arg("-f") .arg("-s")
870 .arg("-")
871 .arg(file_path)
872 .run(builder);
873}
874
875#[derive(Debug, Clone, PartialEq, Eq, Hash)]
876pub struct StartupObjects {
877 pub compiler: Compiler,
878 pub target: TargetSelection,
879}
880
881impl Step for StartupObjects {
882 type Output = Vec<(PathBuf, DependencyType)>;
883
884 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
885 run.path("library/rtstartup")
886 }
887
888 fn make_run(run: RunConfig<'_>) {
889 run.builder.ensure(StartupObjects {
890 compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
891 target: run.target,
892 });
893 }
894
895 #[cfg_attr(
902 feature = "tracing",
903 instrument(
904 level = "trace",
905 name = "StartupObjects::run",
906 skip_all,
907 fields(compiler = ?self.compiler, target = ?self.target),
908 ),
909 )]
910 fn run(self, builder: &Builder<'_>) -> Vec<(PathBuf, DependencyType)> {
911 let for_compiler = self.compiler;
912 let target = self.target;
913 if !target.is_windows_gnu() {
914 return vec![];
915 }
916
917 let mut target_deps = vec![];
918
919 let src_dir = &builder.src.join("library").join("rtstartup");
920 let dst_dir = &builder.native_dir(target).join("rtstartup");
921 let sysroot_dir = &builder.sysroot_target_libdir(for_compiler, target);
922 t!(fs::create_dir_all(dst_dir));
923
924 for file in &["rsbegin", "rsend"] {
925 let src_file = &src_dir.join(file.to_string() + ".rs");
926 let dst_file = &dst_dir.join(file.to_string() + ".o");
927 if !up_to_date(src_file, dst_file) {
928 let mut cmd = command(&builder.initial_rustc);
929 cmd.env("RUSTC_BOOTSTRAP", "1");
930 if !builder.local_rebuild {
931 cmd.arg("--cfg").arg("bootstrap");
933 }
934 cmd.arg("--target")
935 .arg(target.rustc_target_arg())
936 .arg("--emit=obj")
937 .arg("-o")
938 .arg(dst_file)
939 .arg(src_file)
940 .run(builder);
941 }
942
943 let obj = sysroot_dir.join((*file).to_string() + ".o");
944 builder.copy_link(dst_file, &obj, FileType::NativeLibrary);
945 target_deps.push((obj, DependencyType::Target));
946 }
947
948 target_deps
949 }
950}
951
952fn cp_rustc_component_to_ci_sysroot(builder: &Builder<'_>, sysroot: &Path, contents: Vec<String>) {
953 let ci_rustc_dir = builder.config.ci_rustc_dir();
954
955 for file in contents {
956 let src = ci_rustc_dir.join(&file);
957 let dst = sysroot.join(file);
958 if src.is_dir() {
959 t!(fs::create_dir_all(dst));
960 } else {
961 builder.copy_link(&src, &dst, FileType::Regular);
962 }
963 }
964}
965
966#[derive(Debug, PartialOrd, Ord, Clone, PartialEq, Eq, Hash)]
973pub struct Rustc {
974 pub target: TargetSelection,
976 pub build_compiler: Compiler,
978 crates: Vec<String>,
984}
985
986impl Rustc {
987 pub fn new(build_compiler: Compiler, target: TargetSelection) -> Self {
988 Self { target, build_compiler, crates: Default::default() }
989 }
990}
991
992impl Step for Rustc {
993 type Output = u32;
1001 const ONLY_HOSTS: bool = true;
1002 const DEFAULT: bool = false;
1003
1004 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1005 let mut crates = run.builder.in_tree_crates("rustc-main", None);
1006 for (i, krate) in crates.iter().enumerate() {
1007 if krate.name == "rustc-main" {
1010 crates.swap_remove(i);
1011 break;
1012 }
1013 }
1014 run.crates(crates)
1015 }
1016
1017 fn make_run(run: RunConfig<'_>) {
1018 if run.builder.paths == vec![PathBuf::from("compiler")] {
1021 return;
1022 }
1023
1024 let crates = run.cargo_crates_in_set();
1025 run.builder.ensure(Rustc {
1026 build_compiler: run
1027 .builder
1028 .compiler(run.builder.top_stage.saturating_sub(1), run.build_triple()),
1029 target: run.target,
1030 crates,
1031 });
1032 }
1033
1034 #[cfg_attr(
1040 feature = "tracing",
1041 instrument(
1042 level = "debug",
1043 name = "Rustc::run",
1044 skip_all,
1045 fields(previous_compiler = ?self.build_compiler, target = ?self.target),
1046 ),
1047 )]
1048 fn run(self, builder: &Builder<'_>) -> u32 {
1049 let build_compiler = self.build_compiler;
1050 let target = self.target;
1051
1052 if builder.download_rustc() && build_compiler.stage != 0 {
1055 trace!(stage = build_compiler.stage, "`download_rustc` requested");
1056
1057 let sysroot =
1058 builder.ensure(Sysroot { compiler: build_compiler, force_recompile: false });
1059 cp_rustc_component_to_ci_sysroot(
1060 builder,
1061 &sysroot,
1062 builder.config.ci_rustc_dev_contents(),
1063 );
1064 return build_compiler.stage;
1065 }
1066
1067 builder.std(build_compiler, target);
1070
1071 if builder.config.keep_stage.contains(&build_compiler.stage) {
1072 trace!(stage = build_compiler.stage, "`keep-stage` requested");
1073
1074 builder.info("WARNING: Using a potentially old librustc. This may not behave well.");
1075 builder.info("WARNING: Use `--keep-stage-std` if you want to rebuild the compiler when it changes");
1076 builder.ensure(RustcLink::from_rustc(self, build_compiler));
1077
1078 return build_compiler.stage;
1079 }
1080
1081 let compiler_to_use =
1082 builder.compiler_for(build_compiler.stage, build_compiler.host, target);
1083 if compiler_to_use != build_compiler {
1084 builder.ensure(Rustc::new(compiler_to_use, target));
1085 let msg = if compiler_to_use.host == target {
1086 format!(
1087 "Uplifting rustc (stage{} -> stage{})",
1088 compiler_to_use.stage,
1089 build_compiler.stage + 1
1090 )
1091 } else {
1092 format!(
1093 "Uplifting rustc (stage{}:{} -> stage{}:{})",
1094 compiler_to_use.stage,
1095 compiler_to_use.host,
1096 build_compiler.stage + 1,
1097 target
1098 )
1099 };
1100 builder.info(&msg);
1101 builder.ensure(RustcLink::from_rustc(self, compiler_to_use));
1102 return compiler_to_use.stage;
1103 }
1104
1105 builder.std(
1111 builder.compiler(self.build_compiler.stage, builder.config.host_target),
1112 builder.config.host_target,
1113 );
1114
1115 let mut cargo = builder::Cargo::new(
1116 builder,
1117 build_compiler,
1118 Mode::Rustc,
1119 SourceType::InTree,
1120 target,
1121 Kind::Build,
1122 );
1123
1124 rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates);
1125
1126 for krate in &*self.crates {
1130 cargo.arg("-p").arg(krate);
1131 }
1132
1133 if builder.build.config.enable_bolt_settings && build_compiler.stage == 1 {
1134 cargo.env("RUSTC_BOLT_LINK_FLAGS", "1");
1136 }
1137
1138 let _guard = builder.msg_sysroot_tool(
1139 Kind::Build,
1140 build_compiler.stage,
1141 format_args!("compiler artifacts{}", crate_description(&self.crates)),
1142 build_compiler.host,
1143 target,
1144 );
1145 let stamp = build_stamp::librustc_stamp(builder, build_compiler, target);
1146 run_cargo(
1147 builder,
1148 cargo,
1149 vec![],
1150 &stamp,
1151 vec![],
1152 false,
1153 true, );
1155
1156 let target_root_dir = stamp.path().parent().unwrap();
1157 if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None
1163 && builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None
1164 {
1165 let rustc_driver = target_root_dir.join("librustc_driver.so");
1166 strip_debug(builder, target, &rustc_driver);
1167 }
1168
1169 if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None {
1170 strip_debug(builder, target, &target_root_dir.join("rustc-main"));
1173 }
1174
1175 builder.ensure(RustcLink::from_rustc(
1176 self,
1177 builder.compiler(build_compiler.stage, builder.config.host_target),
1178 ));
1179
1180 build_compiler.stage
1181 }
1182
1183 fn metadata(&self) -> Option<StepMetadata> {
1184 Some(StepMetadata::build("rustc", self.target).built_by(self.build_compiler))
1185 }
1186}
1187
1188pub fn rustc_cargo(
1189 builder: &Builder<'_>,
1190 cargo: &mut Cargo,
1191 target: TargetSelection,
1192 build_compiler: &Compiler,
1193 crates: &[String],
1194) {
1195 cargo
1196 .arg("--features")
1197 .arg(builder.rustc_features(builder.kind, target, crates))
1198 .arg("--manifest-path")
1199 .arg(builder.src.join("compiler/rustc/Cargo.toml"));
1200
1201 cargo.rustdocflag("-Zcrate-attr=warn(rust_2018_idioms)");
1202
1203 cargo.rustflag("-Zon-broken-pipe=kill");
1217
1218 if builder.config.llvm_enzyme {
1221 let arch = builder.build.host_target;
1222 let enzyme_dir = builder.build.out.join(arch).join("enzyme").join("lib");
1223 cargo.rustflag("-L").rustflag(enzyme_dir.to_str().expect("Invalid path"));
1224
1225 if let Some(llvm_config) = builder.llvm_config(builder.config.host_target) {
1226 let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
1227 cargo.rustflag("-l").rustflag(&format!("Enzyme-{llvm_version_major}"));
1228 }
1229 }
1230
1231 if builder.build.config.lld_mode.is_used() {
1236 cargo.rustflag("-Zdefault-visibility=protected");
1237 }
1238
1239 if is_lto_stage(build_compiler) {
1240 match builder.config.rust_lto {
1241 RustcLto::Thin | RustcLto::Fat => {
1242 cargo.rustflag("-Zdylib-lto");
1245 let lto_type = match builder.config.rust_lto {
1249 RustcLto::Thin => "thin",
1250 RustcLto::Fat => "fat",
1251 _ => unreachable!(),
1252 };
1253 cargo.rustflag(&format!("-Clto={lto_type}"));
1254 cargo.rustflag("-Cembed-bitcode=yes");
1255 }
1256 RustcLto::ThinLocal => { }
1257 RustcLto::Off => {
1258 cargo.rustflag("-Clto=off");
1259 }
1260 }
1261 } else if builder.config.rust_lto == RustcLto::Off {
1262 cargo.rustflag("-Clto=off");
1263 }
1264
1265 if builder.config.lld_mode.is_used() && !build_compiler.host.is_msvc() {
1273 cargo.rustflag("-Clink-args=-Wl,--icf=all");
1274 }
1275
1276 if builder.config.rust_profile_use.is_some() && builder.config.rust_profile_generate.is_some() {
1277 panic!("Cannot use and generate PGO profiles at the same time");
1278 }
1279 let is_collecting = if let Some(path) = &builder.config.rust_profile_generate {
1280 if build_compiler.stage == 1 {
1281 cargo.rustflag(&format!("-Cprofile-generate={path}"));
1282 cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4");
1285 true
1286 } else {
1287 false
1288 }
1289 } else if let Some(path) = &builder.config.rust_profile_use {
1290 if build_compiler.stage == 1 {
1291 cargo.rustflag(&format!("-Cprofile-use={path}"));
1292 if builder.is_verbose() {
1293 cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function");
1294 }
1295 true
1296 } else {
1297 false
1298 }
1299 } else {
1300 false
1301 };
1302 if is_collecting {
1303 cargo.rustflag(&format!(
1305 "-Cllvm-args=-static-func-strip-dirname-prefix={}",
1306 builder.config.src.components().count()
1307 ));
1308 }
1309
1310 if let Some(ref ccache) = builder.config.ccache
1315 && build_compiler.stage == 0
1316 && !builder.config.incremental
1317 {
1318 cargo.env("RUSTC_WRAPPER", ccache);
1319 }
1320
1321 rustc_cargo_env(builder, cargo, target, build_compiler.stage);
1322}
1323
1324pub fn rustc_cargo_env(
1325 builder: &Builder<'_>,
1326 cargo: &mut Cargo,
1327 target: TargetSelection,
1328 build_stage: u32,
1329) {
1330 cargo
1333 .env("CFG_RELEASE", builder.rust_release())
1334 .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
1335 .env("CFG_VERSION", builder.rust_version());
1336
1337 if builder.config.omit_git_hash {
1341 cargo.env("CFG_OMIT_GIT_HASH", "1");
1342 }
1343
1344 if let Some(backend) = builder.config.default_codegen_backend(target) {
1345 cargo.env("CFG_DEFAULT_CODEGEN_BACKEND", backend);
1346 }
1347
1348 let libdir_relative = builder.config.libdir_relative().unwrap_or_else(|| Path::new("lib"));
1349 let target_config = builder.config.target_config.get(&target);
1350
1351 cargo.env("CFG_LIBDIR_RELATIVE", libdir_relative);
1352
1353 if let Some(ref ver_date) = builder.rust_info().commit_date() {
1354 cargo.env("CFG_VER_DATE", ver_date);
1355 }
1356 if let Some(ref ver_hash) = builder.rust_info().sha() {
1357 cargo.env("CFG_VER_HASH", ver_hash);
1358 }
1359 if !builder.unstable_features() {
1360 cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
1361 }
1362
1363 if let Some(s) = target_config.and_then(|c| c.default_linker.as_ref()) {
1366 cargo.env("CFG_DEFAULT_LINKER", s);
1367 } else if let Some(ref s) = builder.config.rustc_default_linker {
1368 cargo.env("CFG_DEFAULT_LINKER", s);
1369 }
1370
1371 if builder.config.lld_enabled
1373 && (builder.config.channel == "dev" || builder.config.channel == "nightly")
1374 {
1375 cargo.env("CFG_USE_SELF_CONTAINED_LINKER", "1");
1376 }
1377
1378 if builder.config.rust_verify_llvm_ir {
1379 cargo.env("RUSTC_VERIFY_LLVM_IR", "1");
1380 }
1381
1382 if builder.config.llvm_enzyme {
1383 cargo.rustflag("--cfg=llvm_enzyme");
1384 }
1385
1386 if builder.config.llvm_enabled(target) {
1391 let building_is_expensive =
1392 crate::core::build_steps::llvm::prebuilt_llvm_config(builder, target, false)
1393 .should_build();
1394 let can_skip_build = builder.kind == Kind::Check && builder.top_stage == build_stage;
1396 let should_skip_build = building_is_expensive && can_skip_build;
1397 if !should_skip_build {
1398 rustc_llvm_env(builder, cargo, target)
1399 }
1400 }
1401
1402 if builder.config.jemalloc(target)
1405 && target.starts_with("aarch64")
1406 && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none()
1407 {
1408 cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "16");
1409 }
1410}
1411
1412fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1415 if builder.config.is_rust_llvm(target) {
1416 cargo.env("LLVM_RUSTLLVM", "1");
1417 }
1418 if builder.config.llvm_enzyme {
1419 cargo.env("LLVM_ENZYME", "1");
1420 }
1421 let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
1422 cargo.env("LLVM_CONFIG", &llvm_config);
1423
1424 let mut llvm_linker_flags = String::new();
1434 if builder.config.llvm_profile_generate
1435 && target.is_msvc()
1436 && let Some(ref clang_cl_path) = builder.config.llvm_clang_cl
1437 {
1438 let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
1440 llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
1441 }
1442
1443 if let Some(ref s) = builder.config.llvm_ldflags {
1445 if !llvm_linker_flags.is_empty() {
1446 llvm_linker_flags.push(' ');
1447 }
1448 llvm_linker_flags.push_str(s);
1449 }
1450
1451 if !llvm_linker_flags.is_empty() {
1453 cargo.env("LLVM_LINKER_FLAGS", llvm_linker_flags);
1454 }
1455
1456 if builder.config.llvm_static_stdcpp
1459 && !target.contains("freebsd")
1460 && !target.is_msvc()
1461 && !target.contains("apple")
1462 && !target.contains("solaris")
1463 {
1464 let libstdcxx_name =
1465 if target.contains("windows-gnullvm") { "libc++.a" } else { "libstdc++.a" };
1466 let file = compiler_file(
1467 builder,
1468 &builder.cxx(target).unwrap(),
1469 target,
1470 CLang::Cxx,
1471 libstdcxx_name,
1472 );
1473 cargo.env("LLVM_STATIC_STDCPP", file);
1474 }
1475 if builder.llvm_link_shared() {
1476 cargo.env("LLVM_LINK_SHARED", "1");
1477 }
1478 if builder.config.llvm_use_libcxx {
1479 cargo.env("LLVM_USE_LIBCXX", "1");
1480 }
1481 if builder.config.llvm_assertions {
1482 cargo.env("LLVM_ASSERTIONS", "1");
1483 }
1484}
1485
1486#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1492struct RustcLink {
1493 pub compiler: Compiler,
1495 pub previous_stage_compiler: Compiler,
1497 pub target: TargetSelection,
1498 crates: Vec<String>,
1500}
1501
1502impl RustcLink {
1503 fn from_rustc(rustc: Rustc, host_compiler: Compiler) -> Self {
1504 Self {
1505 compiler: host_compiler,
1506 previous_stage_compiler: rustc.build_compiler,
1507 target: rustc.target,
1508 crates: rustc.crates,
1509 }
1510 }
1511}
1512
1513impl Step for RustcLink {
1514 type Output = ();
1515
1516 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1517 run.never()
1518 }
1519
1520 #[cfg_attr(
1522 feature = "tracing",
1523 instrument(
1524 level = "trace",
1525 name = "RustcLink::run",
1526 skip_all,
1527 fields(
1528 compiler = ?self.compiler,
1529 previous_stage_compiler = ?self.previous_stage_compiler,
1530 target = ?self.target,
1531 ),
1532 ),
1533 )]
1534 fn run(self, builder: &Builder<'_>) {
1535 let compiler = self.compiler;
1536 let previous_stage_compiler = self.previous_stage_compiler;
1537 let target = self.target;
1538 add_to_sysroot(
1539 builder,
1540 &builder.sysroot_target_libdir(previous_stage_compiler, target),
1541 &builder.sysroot_target_libdir(previous_stage_compiler, compiler.host),
1542 &build_stamp::librustc_stamp(builder, compiler, target),
1543 );
1544 }
1545}
1546
1547#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1548pub struct CodegenBackend {
1549 pub target: TargetSelection,
1550 pub compiler: Compiler,
1551 pub backend: String,
1552}
1553
1554fn needs_codegen_config(run: &RunConfig<'_>) -> bool {
1555 let mut needs_codegen_cfg = false;
1556 for path_set in &run.paths {
1557 needs_codegen_cfg = match path_set {
1558 PathSet::Set(set) => set.iter().any(|p| is_codegen_cfg_needed(p, run)),
1559 PathSet::Suite(suite) => is_codegen_cfg_needed(suite, run),
1560 }
1561 }
1562 needs_codegen_cfg
1563}
1564
1565pub(crate) const CODEGEN_BACKEND_PREFIX: &str = "rustc_codegen_";
1566
1567fn is_codegen_cfg_needed(path: &TaskPath, run: &RunConfig<'_>) -> bool {
1568 let path = path.path.to_str().unwrap();
1569
1570 let is_explicitly_called = |p| -> bool { run.builder.paths.contains(p) };
1571 let should_enforce = run.builder.kind == Kind::Dist || run.builder.kind == Kind::Install;
1572
1573 if path.contains(CODEGEN_BACKEND_PREFIX) {
1574 let mut needs_codegen_backend_config = true;
1575 for backend in run.builder.config.codegen_backends(run.target) {
1576 if path.ends_with(&(CODEGEN_BACKEND_PREFIX.to_owned() + backend)) {
1577 needs_codegen_backend_config = false;
1578 }
1579 }
1580 if (is_explicitly_called(&PathBuf::from(path)) || should_enforce)
1581 && needs_codegen_backend_config
1582 {
1583 run.builder.info(
1584 "WARNING: no codegen-backends config matched the requested path to build a codegen backend. \
1585 HELP: add backend to codegen-backends in bootstrap.toml.",
1586 );
1587 return true;
1588 }
1589 }
1590
1591 false
1592}
1593
1594impl Step for CodegenBackend {
1595 type Output = ();
1596 const ONLY_HOSTS: bool = true;
1597 const DEFAULT: bool = true;
1599
1600 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1601 run.paths(&["compiler/rustc_codegen_cranelift", "compiler/rustc_codegen_gcc"])
1602 }
1603
1604 fn make_run(run: RunConfig<'_>) {
1605 if needs_codegen_config(&run) {
1606 return;
1607 }
1608
1609 for backend in run.builder.config.codegen_backends(run.target) {
1610 if backend == "llvm" {
1611 continue; }
1613
1614 run.builder.ensure(CodegenBackend {
1615 target: run.target,
1616 compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
1617 backend: backend.clone(),
1618 });
1619 }
1620 }
1621
1622 #[cfg_attr(
1623 feature = "tracing",
1624 instrument(
1625 level = "debug",
1626 name = "CodegenBackend::run",
1627 skip_all,
1628 fields(
1629 compiler = ?self.compiler,
1630 target = ?self.target,
1631 backend = ?self.target,
1632 ),
1633 ),
1634 )]
1635 fn run(self, builder: &Builder<'_>) {
1636 let compiler = self.compiler;
1637 let target = self.target;
1638 let backend = self.backend;
1639
1640 builder.ensure(Rustc::new(compiler, target));
1641
1642 if builder.config.keep_stage.contains(&compiler.stage) {
1643 trace!("`keep-stage` requested");
1644 builder.info(
1645 "WARNING: Using a potentially old codegen backend. \
1646 This may not behave well.",
1647 );
1648 return;
1651 }
1652
1653 let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
1654 if compiler_to_use != compiler {
1655 builder.ensure(CodegenBackend { compiler: compiler_to_use, target, backend });
1656 return;
1657 }
1658
1659 let out_dir = builder.cargo_out(compiler, Mode::Codegen, target);
1660
1661 let mut cargo = builder::Cargo::new(
1662 builder,
1663 compiler,
1664 Mode::Codegen,
1665 SourceType::InTree,
1666 target,
1667 Kind::Build,
1668 );
1669 cargo
1670 .arg("--manifest-path")
1671 .arg(builder.src.join(format!("compiler/rustc_codegen_{backend}/Cargo.toml")));
1672 rustc_cargo_env(builder, &mut cargo, target, compiler.stage);
1673
1674 if backend == "gcc" {
1678 let gcc = builder.ensure(Gcc { target });
1679 add_cg_gcc_cargo_flags(&mut cargo, &gcc);
1680 }
1681
1682 let tmp_stamp = BuildStamp::new(&out_dir).with_prefix("tmp");
1683
1684 let _guard = builder.msg_build(compiler, format_args!("codegen backend {backend}"), target);
1685 let files = run_cargo(builder, cargo, vec![], &tmp_stamp, vec![], false, false);
1686 if builder.config.dry_run() {
1687 return;
1688 }
1689 let mut files = files.into_iter().filter(|f| {
1690 let filename = f.file_name().unwrap().to_str().unwrap();
1691 is_dylib(f) && filename.contains("rustc_codegen_")
1692 });
1693 let codegen_backend = match files.next() {
1694 Some(f) => f,
1695 None => panic!("no dylibs built for codegen backend?"),
1696 };
1697 if let Some(f) = files.next() {
1698 panic!(
1699 "codegen backend built two dylibs:\n{}\n{}",
1700 codegen_backend.display(),
1701 f.display()
1702 );
1703 }
1704 let stamp = build_stamp::codegen_backend_stamp(builder, compiler, target, &backend);
1705 let codegen_backend = codegen_backend.to_str().unwrap();
1706 t!(stamp.add_stamp(codegen_backend).write());
1707 }
1708}
1709
1710fn copy_codegen_backends_to_sysroot(
1717 builder: &Builder<'_>,
1718 compiler: Compiler,
1719 target_compiler: Compiler,
1720) {
1721 let target = target_compiler.host;
1722
1723 let dst = builder.sysroot_codegen_backends(target_compiler);
1732 t!(fs::create_dir_all(&dst), dst);
1733
1734 if builder.config.dry_run() {
1735 return;
1736 }
1737
1738 for backend in builder.config.codegen_backends(target) {
1739 if backend == "llvm" {
1740 continue; }
1742
1743 let stamp = build_stamp::codegen_backend_stamp(builder, compiler, target, backend);
1744 if stamp.path().exists() {
1745 let dylib = t!(fs::read_to_string(stamp.path()));
1746 let file = Path::new(&dylib);
1747 let filename = file.file_name().unwrap().to_str().unwrap();
1748 let target_filename = {
1751 let dash = filename.find('-').unwrap();
1752 let dot = filename.find('.').unwrap();
1753 format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..])
1754 };
1755 builder.copy_link(file, &dst.join(target_filename), FileType::NativeLibrary);
1756 }
1757 }
1758}
1759
1760pub fn compiler_file(
1761 builder: &Builder<'_>,
1762 compiler: &Path,
1763 target: TargetSelection,
1764 c: CLang,
1765 file: &str,
1766) -> PathBuf {
1767 if builder.config.dry_run() {
1768 return PathBuf::new();
1769 }
1770 let mut cmd = command(compiler);
1771 cmd.args(builder.cc_handled_clags(target, c));
1772 cmd.args(builder.cc_unhandled_cflags(target, GitRepo::Rustc, c));
1773 cmd.arg(format!("-print-file-name={file}"));
1774 let out = cmd.run_capture_stdout(builder).stdout();
1775 PathBuf::from(out.trim())
1776}
1777
1778#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1779pub struct Sysroot {
1780 pub compiler: Compiler,
1781 force_recompile: bool,
1783}
1784
1785impl Sysroot {
1786 pub(crate) fn new(compiler: Compiler) -> Self {
1787 Sysroot { compiler, force_recompile: false }
1788 }
1789}
1790
1791impl Step for Sysroot {
1792 type Output = PathBuf;
1793
1794 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1795 run.never()
1796 }
1797
1798 #[cfg_attr(
1802 feature = "tracing",
1803 instrument(
1804 level = "debug",
1805 name = "Sysroot::run",
1806 skip_all,
1807 fields(compiler = ?self.compiler),
1808 ),
1809 )]
1810 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)]
1958pub struct Assemble {
1959 pub target_compiler: Compiler,
1964}
1965
1966impl Step for Assemble {
1967 type Output = Compiler;
1968 const ONLY_HOSTS: bool = true;
1969
1970 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1971 run.path("compiler/rustc").path("compiler")
1972 }
1973
1974 fn make_run(run: RunConfig<'_>) {
1975 run.builder.ensure(Assemble {
1976 target_compiler: run.builder.compiler(run.builder.top_stage, run.target),
1977 });
1978 }
1979
1980 #[cfg_attr(
1986 feature = "tracing",
1987 instrument(
1988 level = "debug",
1989 name = "Assemble::run",
1990 skip_all,
1991 fields(target_compiler = ?self.target_compiler),
1992 ),
1993 )]
1994 fn run(self, builder: &Builder<'_>) -> Compiler {
1995 let target_compiler = self.target_compiler;
1996
1997 if target_compiler.stage == 0 {
1998 trace!("stage 0 build compiler is always available, simply returning");
1999 assert_eq!(
2000 builder.config.host_target, target_compiler.host,
2001 "Cannot obtain compiler for non-native build triple at stage 0"
2002 );
2003 return target_compiler;
2005 }
2006
2007 let libdir = builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2010 let libdir_bin = libdir.parent().unwrap().join("bin");
2011 t!(fs::create_dir_all(&libdir_bin));
2012
2013 if builder.config.llvm_enabled(target_compiler.host) {
2014 trace!("target_compiler.host" = ?target_compiler.host, "LLVM enabled");
2015
2016 let llvm::LlvmResult { llvm_config, .. } =
2017 builder.ensure(llvm::Llvm { target: target_compiler.host });
2018 if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
2019 trace!("LLVM tools enabled");
2020
2021 let llvm_bin_dir =
2022 command(llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
2023 let llvm_bin_dir = Path::new(llvm_bin_dir.trim());
2024
2025 #[cfg(feature = "tracing")]
2032 let _llvm_tools_span =
2033 span!(tracing::Level::TRACE, "installing llvm tools to sysroot", ?libdir_bin)
2034 .entered();
2035 for tool in LLVM_TOOLS {
2036 trace!("installing `{tool}`");
2037 let tool_exe = exe(tool, target_compiler.host);
2038 let src_path = llvm_bin_dir.join(&tool_exe);
2039
2040 if !src_path.exists() && builder.config.llvm_from_ci {
2042 eprintln!("{} does not exist; skipping copy", src_path.display());
2043 continue;
2044 }
2045
2046 builder.resolve_symlink_and_copy(&src_path, &libdir_bin.join(&tool_exe));
2053 }
2054 }
2055 }
2056
2057 let maybe_install_llvm_bitcode_linker = |compiler| {
2058 if builder.config.llvm_bitcode_linker_enabled {
2059 trace!("llvm-bitcode-linker enabled, installing");
2060 let llvm_bitcode_linker =
2061 builder.ensure(crate::core::build_steps::tool::LlvmBitcodeLinker {
2062 compiler,
2063 target: target_compiler.host,
2064 extra_features: vec![],
2065 });
2066 let tool_exe = exe("llvm-bitcode-linker", target_compiler.host);
2067 builder.copy_link(
2068 &llvm_bitcode_linker.tool_path,
2069 &libdir_bin.join(tool_exe),
2070 FileType::Executable,
2071 );
2072 }
2073 };
2074
2075 if builder.download_rustc() {
2077 trace!("`download-rustc` requested, reusing CI compiler for stage > 0");
2078
2079 builder.std(target_compiler, target_compiler.host);
2080 let sysroot =
2081 builder.ensure(Sysroot { compiler: target_compiler, force_recompile: false });
2082 dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
2085 if target_compiler.stage == builder.top_stage {
2087 builder.info(&format!("Creating a sysroot for stage{stage} compiler (use `rustup toolchain link 'name' build/host/stage{stage}`)", stage = target_compiler.stage));
2088 }
2089
2090 let mut precompiled_compiler = target_compiler;
2091 precompiled_compiler.forced_compiler(true);
2092 maybe_install_llvm_bitcode_linker(precompiled_compiler);
2093
2094 return target_compiler;
2095 }
2096
2097 debug!(
2111 "ensuring build compiler is available: compiler(stage = {}, host = {:?})",
2112 target_compiler.stage - 1,
2113 builder.config.host_target,
2114 );
2115 let mut build_compiler =
2116 builder.compiler(target_compiler.stage - 1, builder.config.host_target);
2117
2118 if builder.config.llvm_enzyme && !builder.config.dry_run() {
2120 debug!("`llvm_enzyme` requested");
2121 let enzyme_install = builder.ensure(llvm::Enzyme { target: build_compiler.host });
2122 if let Some(llvm_config) = builder.llvm_config(builder.config.host_target) {
2123 let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
2124 let lib_ext = std::env::consts::DLL_EXTENSION;
2125 let libenzyme = format!("libEnzyme-{llvm_version_major}");
2126 let src_lib =
2127 enzyme_install.join("build/Enzyme").join(&libenzyme).with_extension(lib_ext);
2128 let libdir = builder.sysroot_target_libdir(build_compiler, build_compiler.host);
2129 let target_libdir =
2130 builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2131 let dst_lib = libdir.join(&libenzyme).with_extension(lib_ext);
2132 let target_dst_lib = target_libdir.join(&libenzyme).with_extension(lib_ext);
2133 builder.copy_link(&src_lib, &dst_lib, FileType::NativeLibrary);
2134 builder.copy_link(&src_lib, &target_dst_lib, FileType::NativeLibrary);
2135 }
2136 }
2137
2138 debug!(
2144 ?build_compiler,
2145 "target_compiler.host" = ?target_compiler.host,
2146 "building compiler libraries to link to"
2147 );
2148 let actual_stage = builder.ensure(Rustc::new(build_compiler, target_compiler.host));
2149 debug!(
2152 "(old) build_compiler.stage" = build_compiler.stage,
2153 "(adjusted) build_compiler.stage" = actual_stage,
2154 "temporarily adjusting `build_compiler.stage` to account for uplifted libraries"
2155 );
2156 build_compiler.stage = actual_stage;
2157
2158 #[cfg(feature = "tracing")]
2159 let _codegen_backend_span =
2160 span!(tracing::Level::DEBUG, "building requested codegen backends").entered();
2161 for backend in builder.config.codegen_backends(target_compiler.host) {
2162 if backend == "llvm" {
2163 debug!("llvm codegen backend is already built as part of rustc");
2164 continue; }
2166
2167 if builder.kind == Kind::Check && builder.top_stage == 1 {
2184 continue;
2185 }
2186 builder.ensure(CodegenBackend {
2187 compiler: build_compiler,
2188 target: target_compiler.host,
2189 backend: backend.clone(),
2190 });
2191 }
2192 #[cfg(feature = "tracing")]
2193 drop(_codegen_backend_span);
2194
2195 let stage = target_compiler.stage;
2196 let host = target_compiler.host;
2197 let (host_info, dir_name) = if build_compiler.host == host {
2198 ("".into(), "host".into())
2199 } else {
2200 (format!(" ({host})"), host.to_string())
2201 };
2202 let msg = format!(
2207 "Creating a sysroot for stage{stage} compiler{host_info} (use `rustup toolchain link 'name' build/{dir_name}/stage{stage}`)"
2208 );
2209 builder.info(&msg);
2210
2211 let stamp = build_stamp::librustc_stamp(builder, build_compiler, target_compiler.host);
2213 let proc_macros = builder
2214 .read_stamp_file(&stamp)
2215 .into_iter()
2216 .filter_map(|(path, dependency_type)| {
2217 if dependency_type == DependencyType::Host {
2218 Some(path.file_name().unwrap().to_owned().into_string().unwrap())
2219 } else {
2220 None
2221 }
2222 })
2223 .collect::<HashSet<_>>();
2224
2225 let sysroot = builder.sysroot(target_compiler);
2226 let rustc_libdir = builder.rustc_libdir(target_compiler);
2227 t!(fs::create_dir_all(&rustc_libdir));
2228 let src_libdir = builder.sysroot_target_libdir(build_compiler, host);
2229 for f in builder.read_dir(&src_libdir) {
2230 let filename = f.file_name().into_string().unwrap();
2231
2232 let is_proc_macro = proc_macros.contains(&filename);
2233 let is_dylib_or_debug = is_dylib(&f.path()) || is_debug_info(&filename);
2234
2235 let can_be_rustc_dynamic_dep = if builder
2239 .link_std_into_rustc_driver(target_compiler.host)
2240 && !target_compiler.host.is_windows()
2241 {
2242 let is_std = filename.starts_with("std-") || filename.starts_with("libstd-");
2243 !is_std
2244 } else {
2245 true
2246 };
2247
2248 if is_dylib_or_debug && can_be_rustc_dynamic_dep && !is_proc_macro {
2249 builder.copy_link(&f.path(), &rustc_libdir.join(&filename), FileType::Regular);
2250 }
2251 }
2252
2253 debug!("copying codegen backends to sysroot");
2254 copy_codegen_backends_to_sysroot(builder, build_compiler, target_compiler);
2255
2256 if builder.config.lld_enabled {
2257 builder.ensure(crate::core::build_steps::tool::LldWrapper {
2258 build_compiler,
2259 target_compiler,
2260 });
2261 }
2262
2263 if builder.config.llvm_enabled(target_compiler.host) && builder.config.llvm_tools_enabled {
2264 debug!(
2265 "llvm and llvm tools enabled; copying `llvm-objcopy` as `rust-objcopy` to \
2266 workaround faulty homebrew `strip`s"
2267 );
2268
2269 let src_exe = exe("llvm-objcopy", target_compiler.host);
2276 let dst_exe = exe("rust-objcopy", target_compiler.host);
2277 builder.copy_link(
2278 &libdir_bin.join(src_exe),
2279 &libdir_bin.join(dst_exe),
2280 FileType::Executable,
2281 );
2282 }
2283
2284 if builder.tool_enabled("wasm-component-ld") {
2290 let wasm_component = builder.ensure(crate::core::build_steps::tool::WasmComponentLd {
2291 compiler: build_compiler,
2292 target: target_compiler.host,
2293 });
2294 builder.copy_link(
2295 &wasm_component.tool_path,
2296 &libdir_bin.join(wasm_component.tool_path.file_name().unwrap()),
2297 FileType::Executable,
2298 );
2299 }
2300
2301 maybe_install_llvm_bitcode_linker(target_compiler);
2302
2303 debug!(
2306 "target_compiler.host" = ?target_compiler.host,
2307 ?sysroot,
2308 "ensuring availability of `libLLVM.so` in compiler directory"
2309 );
2310 dist::maybe_install_llvm_runtime(builder, target_compiler.host, &sysroot);
2311 dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
2312
2313 let out_dir = builder.cargo_out(build_compiler, Mode::Rustc, host);
2315 let rustc = out_dir.join(exe("rustc-main", host));
2316 let bindir = sysroot.join("bin");
2317 t!(fs::create_dir_all(bindir));
2318 let compiler = builder.rustc(target_compiler);
2319 debug!(src = ?rustc, dst = ?compiler, "linking compiler binary itself");
2320 builder.copy_link(&rustc, &compiler, FileType::Executable);
2321
2322 target_compiler
2323 }
2324}
2325
2326pub fn add_to_sysroot(
2331 builder: &Builder<'_>,
2332 sysroot_dst: &Path,
2333 sysroot_host_dst: &Path,
2334 stamp: &BuildStamp,
2335) {
2336 let self_contained_dst = &sysroot_dst.join("self-contained");
2337 t!(fs::create_dir_all(sysroot_dst));
2338 t!(fs::create_dir_all(sysroot_host_dst));
2339 t!(fs::create_dir_all(self_contained_dst));
2340 for (path, dependency_type) in builder.read_stamp_file(stamp) {
2341 let dst = match dependency_type {
2342 DependencyType::Host => sysroot_host_dst,
2343 DependencyType::Target => sysroot_dst,
2344 DependencyType::TargetSelfContained => self_contained_dst,
2345 };
2346 builder.copy_link(&path, &dst.join(path.file_name().unwrap()), FileType::Regular);
2347 }
2348}
2349
2350pub fn run_cargo(
2351 builder: &Builder<'_>,
2352 cargo: Cargo,
2353 tail_args: Vec<String>,
2354 stamp: &BuildStamp,
2355 additional_target_deps: Vec<(PathBuf, DependencyType)>,
2356 is_check: bool,
2357 rlib_only_metadata: bool,
2358) -> Vec<PathBuf> {
2359 let target_root_dir = stamp.path().parent().unwrap();
2361 let target_deps_dir = target_root_dir.join("deps");
2363 let host_root_dir = target_root_dir
2365 .parent()
2366 .unwrap() .parent()
2368 .unwrap() .join(target_root_dir.file_name().unwrap());
2370
2371 let mut deps = Vec::new();
2375 let mut toplevel = Vec::new();
2376 let ok = stream_cargo(builder, cargo, tail_args, &mut |msg| {
2377 let (filenames, crate_types) = match msg {
2378 CargoMessage::CompilerArtifact {
2379 filenames,
2380 target: CargoTarget { crate_types },
2381 ..
2382 } => (filenames, crate_types),
2383 _ => return,
2384 };
2385 for filename in filenames {
2386 let mut keep = false;
2388 if filename.ends_with(".lib")
2389 || filename.ends_with(".a")
2390 || is_debug_info(&filename)
2391 || is_dylib(Path::new(&*filename))
2392 {
2393 keep = true;
2395 }
2396 if is_check && filename.ends_with(".rmeta") {
2397 keep = true;
2399 } else if rlib_only_metadata {
2400 if filename.contains("jemalloc_sys")
2401 || filename.contains("rustc_smir")
2402 || filename.contains("stable_mir")
2403 {
2404 keep |= filename.ends_with(".rlib");
2407 } else {
2408 keep |= filename.ends_with(".rmeta");
2412 }
2413 } else {
2414 keep |= filename.ends_with(".rlib");
2416 }
2417
2418 if !keep {
2419 continue;
2420 }
2421
2422 let filename = Path::new(&*filename);
2423
2424 if filename.starts_with(&host_root_dir) {
2427 if crate_types.iter().any(|t| t == "proc-macro") {
2429 deps.push((filename.to_path_buf(), DependencyType::Host));
2430 }
2431 continue;
2432 }
2433
2434 if filename.starts_with(&target_deps_dir) {
2437 deps.push((filename.to_path_buf(), DependencyType::Target));
2438 continue;
2439 }
2440
2441 let expected_len = t!(filename.metadata()).len();
2452 let filename = filename.file_name().unwrap().to_str().unwrap();
2453 let mut parts = filename.splitn(2, '.');
2454 let file_stem = parts.next().unwrap().to_owned();
2455 let extension = parts.next().unwrap().to_owned();
2456
2457 toplevel.push((file_stem, extension, expected_len));
2458 }
2459 });
2460
2461 if !ok {
2462 crate::exit!(1);
2463 }
2464
2465 if builder.config.dry_run() {
2466 return Vec::new();
2467 }
2468
2469 let contents = target_deps_dir
2473 .read_dir()
2474 .unwrap_or_else(|e| panic!("Couldn't read {}: {}", target_deps_dir.display(), e))
2475 .map(|e| t!(e))
2476 .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata())))
2477 .collect::<Vec<_>>();
2478 for (prefix, extension, expected_len) in toplevel {
2479 let candidates = contents.iter().filter(|&(_, filename, meta)| {
2480 meta.len() == expected_len
2481 && filename
2482 .strip_prefix(&prefix[..])
2483 .map(|s| s.starts_with('-') && s.ends_with(&extension[..]))
2484 .unwrap_or(false)
2485 });
2486 let max = candidates.max_by_key(|&(_, _, metadata)| {
2487 metadata.modified().expect("mtime should be available on all relevant OSes")
2488 });
2489 let path_to_add = match max {
2490 Some(triple) => triple.0.to_str().unwrap(),
2491 None => panic!("no output generated for {prefix:?} {extension:?}"),
2492 };
2493 if is_dylib(Path::new(path_to_add)) {
2494 let candidate = format!("{path_to_add}.lib");
2495 let candidate = PathBuf::from(candidate);
2496 if candidate.exists() {
2497 deps.push((candidate, DependencyType::Target));
2498 }
2499 }
2500 deps.push((path_to_add.into(), DependencyType::Target));
2501 }
2502
2503 deps.extend(additional_target_deps);
2504 deps.sort();
2505 let mut new_contents = Vec::new();
2506 for (dep, dependency_type) in deps.iter() {
2507 new_contents.extend(match *dependency_type {
2508 DependencyType::Host => b"h",
2509 DependencyType::Target => b"t",
2510 DependencyType::TargetSelfContained => b"s",
2511 });
2512 new_contents.extend(dep.to_str().unwrap().as_bytes());
2513 new_contents.extend(b"\0");
2514 }
2515 t!(fs::write(stamp.path(), &new_contents));
2516 deps.into_iter().map(|(d, _)| d).collect()
2517}
2518
2519pub fn stream_cargo(
2520 builder: &Builder<'_>,
2521 cargo: Cargo,
2522 tail_args: Vec<String>,
2523 cb: &mut dyn FnMut(CargoMessage<'_>),
2524) -> bool {
2525 let mut cmd = cargo.into_cmd();
2526
2527 #[cfg(feature = "tracing")]
2528 let _run_span = crate::trace_cmd!(cmd);
2529
2530 let mut message_format = if builder.config.json_output {
2533 String::from("json")
2534 } else {
2535 String::from("json-render-diagnostics")
2536 };
2537 if let Some(s) = &builder.config.rustc_error_format {
2538 message_format.push_str(",json-diagnostic-");
2539 message_format.push_str(s);
2540 }
2541 cmd.arg("--message-format").arg(message_format);
2542
2543 for arg in tail_args {
2544 cmd.arg(arg);
2545 }
2546
2547 builder.verbose(|| println!("running: {cmd:?}"));
2548
2549 let streaming_command = cmd.stream_capture_stdout(&builder.config.exec_ctx);
2550
2551 let Some(mut streaming_command) = streaming_command else {
2552 return true;
2553 };
2554
2555 let stdout = BufReader::new(streaming_command.stdout.take().unwrap());
2559 for line in stdout.lines() {
2560 let line = t!(line);
2561 match serde_json::from_str::<CargoMessage<'_>>(&line) {
2562 Ok(msg) => {
2563 if builder.config.json_output {
2564 println!("{line}");
2566 }
2567 cb(msg)
2568 }
2569 Err(_) => println!("{line}"),
2571 }
2572 }
2573
2574 let status = t!(streaming_command.wait());
2576 if builder.is_verbose() && !status.success() {
2577 eprintln!(
2578 "command did not execute successfully: {cmd:?}\n\
2579 expected success, got: {status}"
2580 );
2581 }
2582
2583 status.success()
2584}
2585
2586#[derive(Deserialize)]
2587pub struct CargoTarget<'a> {
2588 crate_types: Vec<Cow<'a, str>>,
2589}
2590
2591#[derive(Deserialize)]
2592#[serde(tag = "reason", rename_all = "kebab-case")]
2593pub enum CargoMessage<'a> {
2594 CompilerArtifact { filenames: Vec<Cow<'a, str>>, target: CargoTarget<'a> },
2595 BuildScriptExecuted,
2596 BuildFinished,
2597}
2598
2599pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path) {
2600 if target != "x86_64-unknown-linux-gnu"
2604 || !builder.config.is_host_target(target)
2605 || !path.exists()
2606 {
2607 return;
2608 }
2609
2610 let previous_mtime = t!(t!(path.metadata()).modified());
2611 command("strip").arg("--strip-debug").arg(path).run_capture(builder);
2612
2613 let file = t!(fs::File::open(path));
2614
2615 t!(file.set_modified(previous_mtime));
2628}
2629
2630pub fn is_lto_stage(build_compiler: &Compiler) -> bool {
2632 build_compiler.stage != 0
2633}