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::toml::target::DefaultLinuxLinkerOverride;
30use crate::core::config::{
31 CompilerBuiltins, DebuginfoLevel, LlvmLibunwind, RustcLto, TargetSelection,
32};
33use crate::utils::build_stamp;
34use crate::utils::build_stamp::BuildStamp;
35use crate::utils::exec::command;
36use crate::utils::helpers::{
37 exe, get_clang_cl_resource_dir, is_debug_info, is_dylib, symlink_dir, t, up_to_date,
38};
39use crate::{
40 CLang, CodegenBackendKind, Compiler, DependencyType, FileType, GitRepo, LLVM_TOOLS, Mode,
41 debug, trace,
42};
43
44#[derive(Debug, Clone, PartialEq, Eq, Hash)]
46pub struct Std {
47 pub target: TargetSelection,
48 pub build_compiler: Compiler,
50 crates: Vec<String>,
54 force_recompile: bool,
57 extra_rust_args: &'static [&'static str],
58 is_for_mir_opt_tests: bool,
59}
60
61impl Std {
62 pub fn new(build_compiler: Compiler, target: TargetSelection) -> Self {
63 Self {
64 target,
65 build_compiler,
66 crates: Default::default(),
67 force_recompile: false,
68 extra_rust_args: &[],
69 is_for_mir_opt_tests: false,
70 }
71 }
72
73 pub fn force_recompile(mut self, force_recompile: bool) -> Self {
74 self.force_recompile = force_recompile;
75 self
76 }
77
78 #[expect(clippy::wrong_self_convention)]
79 pub fn is_for_mir_opt_tests(mut self, is_for_mir_opt_tests: bool) -> Self {
80 self.is_for_mir_opt_tests = is_for_mir_opt_tests;
81 self
82 }
83
84 pub fn extra_rust_args(mut self, extra_rust_args: &'static [&'static str]) -> Self {
85 self.extra_rust_args = extra_rust_args;
86 self
87 }
88
89 fn copy_extra_objects(
90 &self,
91 builder: &Builder<'_>,
92 compiler: &Compiler,
93 target: TargetSelection,
94 ) -> Vec<(PathBuf, DependencyType)> {
95 let mut deps = Vec::new();
96 if !self.is_for_mir_opt_tests {
97 deps.extend(copy_third_party_objects(builder, compiler, target));
98 deps.extend(copy_self_contained_objects(builder, compiler, target));
99 }
100 deps
101 }
102
103 pub fn should_be_uplifted_from_stage_1(builder: &Builder<'_>, stage: u32) -> bool {
108 stage > 1 && !builder.config.full_bootstrap
109 }
110}
111
112impl Step for Std {
113 type Output = Option<BuildStamp>;
115
116 const DEFAULT: bool = true;
117
118 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
119 run.crate_or_deps("sysroot").path("library")
120 }
121
122 fn make_run(run: RunConfig<'_>) {
123 let crates = std_crates_for_run_make(&run);
124 let builder = run.builder;
125
126 let force_recompile = builder.rust_info().is_managed_git_subrepository()
130 && builder.download_rustc()
131 && builder.config.has_changes_from_upstream(&["library"]);
132
133 trace!("is managed git repo: {}", builder.rust_info().is_managed_git_subrepository());
134 trace!("download_rustc: {}", builder.download_rustc());
135 trace!(force_recompile);
136
137 run.builder.ensure(Std {
138 build_compiler: run.builder.compiler(run.builder.top_stage, builder.host_target),
141 target: run.target,
142 crates,
143 force_recompile,
144 extra_rust_args: &[],
145 is_for_mir_opt_tests: false,
146 });
147 }
148
149 fn run(self, builder: &Builder<'_>) -> Self::Output {
155 let target = self.target;
156
157 if self.build_compiler.stage == 0
162 && !(builder.local_rebuild && target != builder.host_target)
163 {
164 let compiler = self.build_compiler;
165 builder.ensure(StdLink::from_std(self, compiler));
166
167 return None;
168 }
169
170 let build_compiler = if builder.download_rustc() && self.force_recompile {
171 builder
174 .compiler(self.build_compiler.stage.saturating_sub(1), builder.config.host_target)
175 } else {
176 self.build_compiler
177 };
178
179 if builder.download_rustc()
182 && builder.config.is_host_target(target)
183 && !self.force_recompile
184 {
185 let sysroot =
186 builder.ensure(Sysroot { compiler: build_compiler, force_recompile: false });
187 cp_rustc_component_to_ci_sysroot(
188 builder,
189 &sysroot,
190 builder.config.ci_rust_std_contents(),
191 );
192 return None;
193 }
194
195 if builder.config.keep_stage.contains(&build_compiler.stage)
196 || builder.config.keep_stage_std.contains(&build_compiler.stage)
197 {
198 trace!(keep_stage = ?builder.config.keep_stage);
199 trace!(keep_stage_std = ?builder.config.keep_stage_std);
200
201 builder.info("WARNING: Using a potentially old libstd. This may not behave well.");
202
203 builder.ensure(StartupObjects { compiler: build_compiler, target });
204
205 self.copy_extra_objects(builder, &build_compiler, target);
206
207 builder.ensure(StdLink::from_std(self, build_compiler));
208 return Some(build_stamp::libstd_stamp(builder, build_compiler, target));
209 }
210
211 let mut target_deps = builder.ensure(StartupObjects { compiler: build_compiler, target });
212
213 let stage = build_compiler.stage;
215
216 if Self::should_be_uplifted_from_stage_1(builder, build_compiler.stage) {
217 let build_compiler_for_std_to_uplift = builder.compiler(1, builder.host_target);
218 let stage_1_stamp = builder.std(build_compiler_for_std_to_uplift, target);
219
220 let msg = if build_compiler_for_std_to_uplift.host == target {
221 format!(
222 "Uplifting library (stage{} -> stage{stage})",
223 build_compiler_for_std_to_uplift.stage
224 )
225 } else {
226 format!(
227 "Uplifting library (stage{}:{} -> stage{stage}:{target})",
228 build_compiler_for_std_to_uplift.stage, build_compiler_for_std_to_uplift.host,
229 )
230 };
231
232 builder.info(&msg);
233
234 self.copy_extra_objects(builder, &build_compiler, target);
237
238 builder.ensure(StdLink::from_std(self, build_compiler_for_std_to_uplift));
239 return stage_1_stamp;
240 }
241
242 target_deps.extend(self.copy_extra_objects(builder, &build_compiler, target));
243
244 let mut cargo = if self.is_for_mir_opt_tests {
248 trace!("building special sysroot for mir-opt tests");
249 let mut cargo = builder::Cargo::new_for_mir_opt_tests(
250 builder,
251 build_compiler,
252 Mode::Std,
253 SourceType::InTree,
254 target,
255 Kind::Check,
256 );
257 cargo.rustflag("-Zalways-encode-mir");
258 cargo.arg("--manifest-path").arg(builder.src.join("library/sysroot/Cargo.toml"));
259 cargo
260 } else {
261 trace!("building regular sysroot");
262 let mut cargo = builder::Cargo::new(
263 builder,
264 build_compiler,
265 Mode::Std,
266 SourceType::InTree,
267 target,
268 Kind::Build,
269 );
270 std_cargo(builder, target, &mut cargo, &self.crates);
271 cargo
272 };
273
274 if target.is_synthetic() {
276 cargo.env("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET", "1");
277 }
278 for rustflag in self.extra_rust_args.iter() {
279 cargo.rustflag(rustflag);
280 }
281
282 let _guard = builder.msg(
283 Kind::Build,
284 format_args!("library artifacts{}", crate_description(&self.crates)),
285 Mode::Std,
286 build_compiler,
287 target,
288 );
289
290 let stamp = build_stamp::libstd_stamp(builder, build_compiler, target);
291 run_cargo(
292 builder,
293 cargo,
294 vec![],
295 &stamp,
296 target_deps,
297 self.is_for_mir_opt_tests, false,
299 );
300
301 builder.ensure(StdLink::from_std(
302 self,
303 builder.compiler(build_compiler.stage, builder.config.host_target),
304 ));
305 Some(stamp)
306 }
307
308 fn metadata(&self) -> Option<StepMetadata> {
309 Some(StepMetadata::build("std", self.target).built_by(self.build_compiler))
310 }
311}
312
313fn copy_and_stamp(
314 builder: &Builder<'_>,
315 libdir: &Path,
316 sourcedir: &Path,
317 name: &str,
318 target_deps: &mut Vec<(PathBuf, DependencyType)>,
319 dependency_type: DependencyType,
320) {
321 let target = libdir.join(name);
322 builder.copy_link(&sourcedir.join(name), &target, FileType::Regular);
323
324 target_deps.push((target, dependency_type));
325}
326
327fn copy_llvm_libunwind(builder: &Builder<'_>, target: TargetSelection, libdir: &Path) -> PathBuf {
328 let libunwind_path = builder.ensure(llvm::Libunwind { target });
329 let libunwind_source = libunwind_path.join("libunwind.a");
330 let libunwind_target = libdir.join("libunwind.a");
331 builder.copy_link(&libunwind_source, &libunwind_target, FileType::NativeLibrary);
332 libunwind_target
333}
334
335fn copy_third_party_objects(
337 builder: &Builder<'_>,
338 compiler: &Compiler,
339 target: TargetSelection,
340) -> Vec<(PathBuf, DependencyType)> {
341 let mut target_deps = vec![];
342
343 if builder.config.needs_sanitizer_runtime_built(target) && compiler.stage != 0 {
344 target_deps.extend(
347 copy_sanitizers(builder, compiler, target)
348 .into_iter()
349 .map(|d| (d, DependencyType::Target)),
350 );
351 }
352
353 if target == "x86_64-fortanix-unknown-sgx"
354 || builder.config.llvm_libunwind(target) == LlvmLibunwind::InTree
355 && (target.contains("linux") || target.contains("fuchsia") || target.contains("aix"))
356 {
357 let libunwind_path =
358 copy_llvm_libunwind(builder, target, &builder.sysroot_target_libdir(*compiler, target));
359 target_deps.push((libunwind_path, DependencyType::Target));
360 }
361
362 target_deps
363}
364
365fn copy_self_contained_objects(
367 builder: &Builder<'_>,
368 compiler: &Compiler,
369 target: TargetSelection,
370) -> Vec<(PathBuf, DependencyType)> {
371 let libdir_self_contained =
372 builder.sysroot_target_libdir(*compiler, target).join("self-contained");
373 t!(fs::create_dir_all(&libdir_self_contained));
374 let mut target_deps = vec![];
375
376 if target.needs_crt_begin_end() {
384 let srcdir = builder.musl_libdir(target).unwrap_or_else(|| {
385 panic!("Target {:?} does not have a \"musl-libdir\" key", target.triple)
386 });
387 if !target.starts_with("wasm32") {
388 for &obj in &["libc.a", "crt1.o", "Scrt1.o", "rcrt1.o", "crti.o", "crtn.o"] {
389 copy_and_stamp(
390 builder,
391 &libdir_self_contained,
392 &srcdir,
393 obj,
394 &mut target_deps,
395 DependencyType::TargetSelfContained,
396 );
397 }
398 let crt_path = builder.ensure(llvm::CrtBeginEnd { target });
399 for &obj in &["crtbegin.o", "crtbeginS.o", "crtend.o", "crtendS.o"] {
400 let src = crt_path.join(obj);
401 let target = libdir_self_contained.join(obj);
402 builder.copy_link(&src, &target, FileType::NativeLibrary);
403 target_deps.push((target, DependencyType::TargetSelfContained));
404 }
405 } else {
406 for &obj in &["libc.a", "crt1-command.o"] {
409 copy_and_stamp(
410 builder,
411 &libdir_self_contained,
412 &srcdir,
413 obj,
414 &mut target_deps,
415 DependencyType::TargetSelfContained,
416 );
417 }
418 }
419 if !target.starts_with("s390x") {
420 let libunwind_path = copy_llvm_libunwind(builder, target, &libdir_self_contained);
421 target_deps.push((libunwind_path, DependencyType::TargetSelfContained));
422 }
423 } else if target.contains("-wasi") {
424 let srcdir = builder.wasi_libdir(target).unwrap_or_else(|| {
425 panic!(
426 "Target {:?} does not have a \"wasi-root\" key in bootstrap.toml \
427 or `$WASI_SDK_PATH` set",
428 target.triple
429 )
430 });
431
432 let srcdir = if target == "wasm32-wasip3" {
436 assert!(!srcdir.exists(), "wasip3 support is in wasi-libc, this should be updated now");
437 builder.wasi_libdir(TargetSelection::from_user("wasm32-wasip2")).unwrap()
438 } else {
439 srcdir
440 };
441 for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] {
442 copy_and_stamp(
443 builder,
444 &libdir_self_contained,
445 &srcdir,
446 obj,
447 &mut target_deps,
448 DependencyType::TargetSelfContained,
449 );
450 }
451 } else if target.is_windows_gnu() {
452 for obj in ["crt2.o", "dllcrt2.o"].iter() {
453 let src = compiler_file(builder, &builder.cc(target), target, CLang::C, obj);
454 let dst = libdir_self_contained.join(obj);
455 builder.copy_link(&src, &dst, FileType::NativeLibrary);
456 target_deps.push((dst, DependencyType::TargetSelfContained));
457 }
458 }
459
460 target_deps
461}
462
463pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec<String> {
466 let mut crates = run.make_run_crates(builder::Alias::Library);
467
468 let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);
477 if target_is_no_std {
478 crates.retain(|c| c == "core" || c == "alloc");
479 }
480 crates
481}
482
483fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
489 if builder.config.llvm_from_ci {
491 builder.config.maybe_download_ci_llvm();
493 let ci_llvm_compiler_rt = builder.config.ci_llvm_root().join("compiler-rt");
494 if ci_llvm_compiler_rt.exists() {
495 return ci_llvm_compiler_rt;
496 }
497 }
498
499 builder.require_submodule("src/llvm-project", {
501 Some("The `build.profiler` config option requires `compiler-rt` sources from LLVM.")
502 });
503 builder.src.join("src/llvm-project/compiler-rt")
504}
505
506pub fn std_cargo(
509 builder: &Builder<'_>,
510 target: TargetSelection,
511 cargo: &mut Cargo,
512 crates: &[String],
513) {
514 if target.contains("apple") && !builder.config.dry_run() {
532 let mut cmd = builder.rustc_cmd(cargo.compiler());
536 cmd.arg("--target").arg(target.rustc_target_arg());
537 cmd.arg("--print=deployment-target");
538 let output = cmd.run_capture_stdout(builder).stdout();
539
540 let (env_var, value) = output.split_once('=').unwrap();
541 cargo.env(env_var.trim(), value.trim());
544
545 if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
555 cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
556 }
557 }
558
559 if let Some(path) = builder.config.profiler_path(target) {
561 cargo.env("LLVM_PROFILER_RT_LIB", path);
562 } else if builder.config.profiler_enabled(target) {
563 let compiler_rt = compiler_rt_for_profiler(builder);
564 cargo.env("RUST_COMPILER_RT_FOR_PROFILER", compiler_rt);
568 }
569
570 let compiler_builtins_c_feature = match builder.config.optimized_compiler_builtins(target) {
584 CompilerBuiltins::LinkLLVMBuiltinsLib(path) => {
585 cargo.env("LLVM_COMPILER_RT_LIB", path);
586 " compiler-builtins-c"
587 }
588 CompilerBuiltins::BuildLLVMFuncs => {
589 builder.require_submodule(
599 "src/llvm-project",
600 Some(
601 "The `build.optimized-compiler-builtins` config option \
602 requires `compiler-rt` sources from LLVM.",
603 ),
604 );
605 let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
606 if !builder.config.dry_run() {
607 assert!(compiler_builtins_root.exists());
610 }
611
612 cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);
615 " compiler-builtins-c"
616 }
617 CompilerBuiltins::BuildRustOnly => "",
618 };
619
620 if !builder.unstable_features() {
623 cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
624 }
625
626 for krate in crates {
627 cargo.args(["-p", krate]);
628 }
629
630 let mut features = String::new();
631
632 if builder.no_std(target) == Some(true) {
633 features += " compiler-builtins-mem";
634 if !target.starts_with("bpf") {
635 features.push_str(compiler_builtins_c_feature);
636 }
637
638 if crates.is_empty() {
640 cargo.args(["-p", "alloc"]);
641 }
642 cargo
643 .arg("--manifest-path")
644 .arg(builder.src.join("library/alloc/Cargo.toml"))
645 .arg("--features")
646 .arg(features);
647 } else {
648 features += &builder.std_features(target);
649 features.push_str(compiler_builtins_c_feature);
650
651 cargo
652 .arg("--features")
653 .arg(features)
654 .arg("--manifest-path")
655 .arg(builder.src.join("library/sysroot/Cargo.toml"));
656
657 if target.contains("musl")
660 && let Some(p) = builder.musl_libdir(target)
661 {
662 let root = format!("native={}", p.to_str().unwrap());
663 cargo.rustflag("-L").rustflag(&root);
664 }
665
666 if target.contains("-wasi")
667 && let Some(dir) = builder.wasi_libdir(target)
668 {
669 let root = format!("native={}", dir.to_str().unwrap());
670 cargo.rustflag("-L").rustflag(&root);
671 }
672 }
673
674 cargo.rustflag("-Cembed-bitcode=yes");
680
681 if builder.config.rust_lto == RustcLto::Off {
682 cargo.rustflag("-Clto=off");
683 }
684
685 if target.contains("riscv") {
692 cargo.rustflag("-Cforce-unwind-tables=yes");
693 }
694
695 cargo.rustflag("-Zunstable-options");
698 cargo.rustflag("-Cforce-frame-pointers=non-leaf");
699
700 let html_root =
701 format!("-Zcrate-attr=doc(html_root_url=\"{}/\")", builder.doc_rust_lang_org_channel(),);
702 cargo.rustflag(&html_root);
703 cargo.rustdocflag(&html_root);
704
705 cargo.rustdocflag("-Zcrate-attr=warn(rust_2018_idioms)");
706}
707
708#[derive(Debug, Clone, PartialEq, Eq, Hash)]
717pub struct StdLink {
718 pub compiler: Compiler,
719 pub target_compiler: Compiler,
720 pub target: TargetSelection,
721 crates: Vec<String>,
723 force_recompile: bool,
725}
726
727impl StdLink {
728 pub fn from_std(std: Std, host_compiler: Compiler) -> Self {
729 Self {
730 compiler: host_compiler,
731 target_compiler: std.build_compiler,
732 target: std.target,
733 crates: std.crates,
734 force_recompile: std.force_recompile,
735 }
736 }
737}
738
739impl Step for StdLink {
740 type Output = ();
741
742 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
743 run.never()
744 }
745
746 fn run(self, builder: &Builder<'_>) {
755 let compiler = self.compiler;
756 let target_compiler = self.target_compiler;
757 let target = self.target;
758
759 let (libdir, hostdir) = if !self.force_recompile && builder.download_rustc() {
761 let lib = builder.sysroot_libdir_relative(self.compiler);
763 let sysroot = builder.ensure(crate::core::build_steps::compile::Sysroot {
764 compiler: self.compiler,
765 force_recompile: self.force_recompile,
766 });
767 let libdir = sysroot.join(lib).join("rustlib").join(target).join("lib");
768 let hostdir = sysroot.join(lib).join("rustlib").join(compiler.host).join("lib");
769 (libdir, hostdir)
770 } else {
771 let libdir = builder.sysroot_target_libdir(target_compiler, target);
772 let hostdir = builder.sysroot_target_libdir(target_compiler, compiler.host);
773 (libdir, hostdir)
774 };
775
776 let is_downloaded_beta_stage0 = builder
777 .build
778 .config
779 .initial_rustc
780 .starts_with(builder.out.join(compiler.host).join("stage0/bin"));
781
782 if compiler.stage == 0 && is_downloaded_beta_stage0 {
786 let sysroot = builder.out.join(compiler.host).join("stage0-sysroot");
788
789 let host = compiler.host;
790 let stage0_bin_dir = builder.out.join(host).join("stage0/bin");
791 let sysroot_bin_dir = sysroot.join("bin");
792 t!(fs::create_dir_all(&sysroot_bin_dir));
793 builder.cp_link_r(&stage0_bin_dir, &sysroot_bin_dir);
794
795 let stage0_lib_dir = builder.out.join(host).join("stage0/lib");
796 t!(fs::create_dir_all(sysroot.join("lib")));
797 builder.cp_link_r(&stage0_lib_dir, &sysroot.join("lib"));
798
799 let sysroot_codegen_backends = builder.sysroot_codegen_backends(compiler);
801 t!(fs::create_dir_all(&sysroot_codegen_backends));
802 let stage0_codegen_backends = builder
803 .out
804 .join(host)
805 .join("stage0/lib/rustlib")
806 .join(host)
807 .join("codegen-backends");
808 if stage0_codegen_backends.exists() {
809 builder.cp_link_r(&stage0_codegen_backends, &sysroot_codegen_backends);
810 }
811 } else if compiler.stage == 0 {
812 let sysroot = builder.out.join(compiler.host.triple).join("stage0-sysroot");
813
814 if builder.local_rebuild {
815 let _ = fs::remove_dir_all(sysroot.join("lib/rustlib/src/rust"));
819 }
820
821 builder.cp_link_r(&builder.initial_sysroot.join("lib"), &sysroot.join("lib"));
822 } else {
823 if builder.download_rustc() {
824 let _ = fs::remove_dir_all(&libdir);
826 let _ = fs::remove_dir_all(&hostdir);
827 }
828
829 add_to_sysroot(
830 builder,
831 &libdir,
832 &hostdir,
833 &build_stamp::libstd_stamp(builder, compiler, target),
834 );
835 }
836 }
837}
838
839fn copy_sanitizers(
841 builder: &Builder<'_>,
842 compiler: &Compiler,
843 target: TargetSelection,
844) -> Vec<PathBuf> {
845 let runtimes: Vec<llvm::SanitizerRuntime> = builder.ensure(llvm::Sanitizers { target });
846
847 if builder.config.dry_run() {
848 return Vec::new();
849 }
850
851 let mut target_deps = Vec::new();
852 let libdir = builder.sysroot_target_libdir(*compiler, target);
853
854 for runtime in &runtimes {
855 let dst = libdir.join(&runtime.name);
856 builder.copy_link(&runtime.path, &dst, FileType::NativeLibrary);
857
858 if target == "x86_64-apple-darwin"
862 || target == "aarch64-apple-darwin"
863 || target == "aarch64-apple-ios"
864 || target == "aarch64-apple-ios-sim"
865 || target == "x86_64-apple-ios"
866 {
867 apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", runtime.name));
869 apple_darwin_sign_file(builder, &dst);
872 }
873
874 target_deps.push(dst);
875 }
876
877 target_deps
878}
879
880fn apple_darwin_update_library_name(builder: &Builder<'_>, library_path: &Path, new_name: &str) {
881 command("install_name_tool").arg("-id").arg(new_name).arg(library_path).run(builder);
882}
883
884fn apple_darwin_sign_file(builder: &Builder<'_>, file_path: &Path) {
885 command("codesign")
886 .arg("-f") .arg("-s")
888 .arg("-")
889 .arg(file_path)
890 .run(builder);
891}
892
893#[derive(Debug, Clone, PartialEq, Eq, Hash)]
894pub struct StartupObjects {
895 pub compiler: Compiler,
896 pub target: TargetSelection,
897}
898
899impl Step for StartupObjects {
900 type Output = Vec<(PathBuf, DependencyType)>;
901
902 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
903 run.path("library/rtstartup")
904 }
905
906 fn make_run(run: RunConfig<'_>) {
907 run.builder.ensure(StartupObjects {
908 compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
909 target: run.target,
910 });
911 }
912
913 fn run(self, builder: &Builder<'_>) -> Vec<(PathBuf, DependencyType)> {
920 let for_compiler = self.compiler;
921 let target = self.target;
922 if !target.is_windows_gnu() {
925 return vec![];
926 }
927
928 let mut target_deps = vec![];
929
930 let src_dir = &builder.src.join("library").join("rtstartup");
931 let dst_dir = &builder.native_dir(target).join("rtstartup");
932 let sysroot_dir = &builder.sysroot_target_libdir(for_compiler, target);
933 t!(fs::create_dir_all(dst_dir));
934
935 for file in &["rsbegin", "rsend"] {
936 let src_file = &src_dir.join(file.to_string() + ".rs");
937 let dst_file = &dst_dir.join(file.to_string() + ".o");
938 if !up_to_date(src_file, dst_file) {
939 let mut cmd = command(&builder.initial_rustc);
940 cmd.env("RUSTC_BOOTSTRAP", "1");
941 if !builder.local_rebuild {
942 cmd.arg("--cfg").arg("bootstrap");
944 }
945 cmd.arg("--target")
946 .arg(target.rustc_target_arg())
947 .arg("--emit=obj")
948 .arg("-o")
949 .arg(dst_file)
950 .arg(src_file)
951 .run(builder);
952 }
953
954 let obj = sysroot_dir.join((*file).to_string() + ".o");
955 builder.copy_link(dst_file, &obj, FileType::NativeLibrary);
956 target_deps.push((obj, DependencyType::Target));
957 }
958
959 target_deps
960 }
961}
962
963fn cp_rustc_component_to_ci_sysroot(builder: &Builder<'_>, sysroot: &Path, contents: Vec<String>) {
964 let ci_rustc_dir = builder.config.ci_rustc_dir();
965
966 for file in contents {
967 let src = ci_rustc_dir.join(&file);
968 let dst = sysroot.join(file);
969 if src.is_dir() {
970 t!(fs::create_dir_all(dst));
971 } else {
972 builder.copy_link(&src, &dst, FileType::Regular);
973 }
974 }
975}
976
977#[derive(Clone, Debug)]
979pub struct BuiltRustc {
980 pub build_compiler: Compiler,
984}
985
986#[derive(Debug, Clone, PartialEq, Eq, Hash)]
993pub struct Rustc {
994 pub target: TargetSelection,
996 pub build_compiler: Compiler,
998 crates: Vec<String>,
1004}
1005
1006impl Rustc {
1007 pub fn new(build_compiler: Compiler, target: TargetSelection) -> Self {
1008 Self { target, build_compiler, crates: Default::default() }
1009 }
1010}
1011
1012impl Step for Rustc {
1013 type Output = BuiltRustc;
1014
1015 const IS_HOST: bool = true;
1016 const DEFAULT: bool = false;
1017
1018 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1019 let mut crates = run.builder.in_tree_crates("rustc-main", None);
1020 for (i, krate) in crates.iter().enumerate() {
1021 if krate.name == "rustc-main" {
1024 crates.swap_remove(i);
1025 break;
1026 }
1027 }
1028 run.crates(crates)
1029 }
1030
1031 fn make_run(run: RunConfig<'_>) {
1032 if run.builder.paths == vec![PathBuf::from("compiler")] {
1035 return;
1036 }
1037
1038 let crates = run.cargo_crates_in_set();
1039 run.builder.ensure(Rustc {
1040 build_compiler: run
1041 .builder
1042 .compiler(run.builder.top_stage.saturating_sub(1), run.build_triple()),
1043 target: run.target,
1044 crates,
1045 });
1046 }
1047
1048 fn run(self, builder: &Builder<'_>) -> Self::Output {
1054 let build_compiler = self.build_compiler;
1055 let target = self.target;
1056
1057 if builder.download_rustc() && build_compiler.stage != 0 {
1060 trace!(stage = build_compiler.stage, "`download_rustc` requested");
1061
1062 let sysroot =
1063 builder.ensure(Sysroot { compiler: build_compiler, force_recompile: false });
1064 cp_rustc_component_to_ci_sysroot(
1065 builder,
1066 &sysroot,
1067 builder.config.ci_rustc_dev_contents(),
1068 );
1069 return BuiltRustc { build_compiler };
1070 }
1071
1072 builder.std(build_compiler, target);
1075
1076 if builder.config.keep_stage.contains(&build_compiler.stage) {
1077 trace!(stage = build_compiler.stage, "`keep-stage` requested");
1078
1079 builder.info("WARNING: Using a potentially old librustc. This may not behave well.");
1080 builder.info("WARNING: Use `--keep-stage-std` if you want to rebuild the compiler when it changes");
1081 builder.ensure(RustcLink::from_rustc(self));
1082
1083 return BuiltRustc { build_compiler };
1084 }
1085
1086 let stage = build_compiler.stage + 1;
1088
1089 if build_compiler.stage >= 2
1094 && !builder.config.full_bootstrap
1095 && target == builder.host_target
1096 {
1097 let uplift_build_compiler = builder.compiler(1, build_compiler.host);
1101
1102 let msg = format!("Uplifting rustc from stage2 to stage{stage})");
1103 builder.info(&msg);
1104
1105 builder.ensure(RustcLink::from_build_compiler_and_sysroot(
1109 uplift_build_compiler,
1111 build_compiler,
1113 target,
1114 self.crates,
1115 ));
1116
1117 return BuiltRustc { build_compiler: uplift_build_compiler };
1120 }
1121
1122 builder.std(
1128 builder.compiler(self.build_compiler.stage, builder.config.host_target),
1129 builder.config.host_target,
1130 );
1131
1132 let mut cargo = builder::Cargo::new(
1133 builder,
1134 build_compiler,
1135 Mode::Rustc,
1136 SourceType::InTree,
1137 target,
1138 Kind::Build,
1139 );
1140
1141 rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates);
1142
1143 for krate in &*self.crates {
1147 cargo.arg("-p").arg(krate);
1148 }
1149
1150 if builder.build.config.enable_bolt_settings && build_compiler.stage == 1 {
1151 cargo.env("RUSTC_BOLT_LINK_FLAGS", "1");
1153 }
1154
1155 let _guard = builder.msg(
1156 Kind::Build,
1157 format_args!("compiler artifacts{}", crate_description(&self.crates)),
1158 Mode::Rustc,
1159 build_compiler,
1160 target,
1161 );
1162 let stamp = build_stamp::librustc_stamp(builder, build_compiler, target);
1163 run_cargo(
1164 builder,
1165 cargo,
1166 vec![],
1167 &stamp,
1168 vec![],
1169 false,
1170 true, );
1172
1173 let target_root_dir = stamp.path().parent().unwrap();
1174 if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None
1180 && builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None
1181 {
1182 let rustc_driver = target_root_dir.join("librustc_driver.so");
1183 strip_debug(builder, target, &rustc_driver);
1184 }
1185
1186 if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None {
1187 strip_debug(builder, target, &target_root_dir.join("rustc-main"));
1190 }
1191
1192 builder.ensure(RustcLink::from_rustc(self));
1193 BuiltRustc { build_compiler }
1194 }
1195
1196 fn metadata(&self) -> Option<StepMetadata> {
1197 Some(StepMetadata::build("rustc", self.target).built_by(self.build_compiler))
1198 }
1199}
1200
1201pub fn rustc_cargo(
1202 builder: &Builder<'_>,
1203 cargo: &mut Cargo,
1204 target: TargetSelection,
1205 build_compiler: &Compiler,
1206 crates: &[String],
1207) {
1208 cargo
1209 .arg("--features")
1210 .arg(builder.rustc_features(builder.kind, target, crates))
1211 .arg("--manifest-path")
1212 .arg(builder.src.join("compiler/rustc/Cargo.toml"));
1213
1214 cargo.rustdocflag("-Zcrate-attr=warn(rust_2018_idioms)");
1215
1216 cargo.rustflag("-Zon-broken-pipe=kill");
1230
1231 if builder.config.llvm_enzyme {
1234 let arch = builder.build.host_target;
1235 let enzyme_dir = builder.build.out.join(arch).join("enzyme").join("lib");
1236 cargo.rustflag("-L").rustflag(enzyme_dir.to_str().expect("Invalid path"));
1237
1238 if let Some(llvm_config) = builder.llvm_config(builder.config.host_target) {
1239 let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
1240 cargo.rustflag("-l").rustflag(&format!("Enzyme-{llvm_version_major}"));
1241 }
1242 }
1243
1244 if builder.build.config.bootstrap_override_lld.is_used() {
1249 cargo.rustflag("-Zdefault-visibility=protected");
1250 }
1251
1252 if is_lto_stage(build_compiler) {
1253 match builder.config.rust_lto {
1254 RustcLto::Thin | RustcLto::Fat => {
1255 cargo.rustflag("-Zdylib-lto");
1258 let lto_type = match builder.config.rust_lto {
1262 RustcLto::Thin => "thin",
1263 RustcLto::Fat => "fat",
1264 _ => unreachable!(),
1265 };
1266 cargo.rustflag(&format!("-Clto={lto_type}"));
1267 cargo.rustflag("-Cembed-bitcode=yes");
1268 }
1269 RustcLto::ThinLocal => { }
1270 RustcLto::Off => {
1271 cargo.rustflag("-Clto=off");
1272 }
1273 }
1274 } else if builder.config.rust_lto == RustcLto::Off {
1275 cargo.rustflag("-Clto=off");
1276 }
1277
1278 if builder.config.bootstrap_override_lld.is_used() && !build_compiler.host.is_msvc() {
1286 cargo.rustflag("-Clink-args=-Wl,--icf=all");
1287 }
1288
1289 if builder.config.rust_profile_use.is_some() && builder.config.rust_profile_generate.is_some() {
1290 panic!("Cannot use and generate PGO profiles at the same time");
1291 }
1292 let is_collecting = if let Some(path) = &builder.config.rust_profile_generate {
1293 if build_compiler.stage == 1 {
1294 cargo.rustflag(&format!("-Cprofile-generate={path}"));
1295 cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4");
1298 true
1299 } else {
1300 false
1301 }
1302 } else if let Some(path) = &builder.config.rust_profile_use {
1303 if build_compiler.stage == 1 {
1304 cargo.rustflag(&format!("-Cprofile-use={path}"));
1305 if builder.is_verbose() {
1306 cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function");
1307 }
1308 true
1309 } else {
1310 false
1311 }
1312 } else {
1313 false
1314 };
1315 if is_collecting {
1316 cargo.rustflag(&format!(
1318 "-Cllvm-args=-static-func-strip-dirname-prefix={}",
1319 builder.config.src.components().count()
1320 ));
1321 }
1322
1323 if let Some(ref ccache) = builder.config.ccache
1328 && build_compiler.stage == 0
1329 && !builder.config.incremental
1330 {
1331 cargo.env("RUSTC_WRAPPER", ccache);
1332 }
1333
1334 rustc_cargo_env(builder, cargo, target);
1335}
1336
1337pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1338 cargo
1341 .env("CFG_RELEASE", builder.rust_release())
1342 .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
1343 .env("CFG_VERSION", builder.rust_version());
1344
1345 if builder.config.omit_git_hash {
1349 cargo.env("CFG_OMIT_GIT_HASH", "1");
1350 }
1351
1352 cargo.env("CFG_DEFAULT_CODEGEN_BACKEND", builder.config.default_codegen_backend(target).name());
1353
1354 let libdir_relative = builder.config.libdir_relative().unwrap_or_else(|| Path::new("lib"));
1355 let target_config = builder.config.target_config.get(&target);
1356
1357 cargo.env("CFG_LIBDIR_RELATIVE", libdir_relative);
1358
1359 if let Some(ref ver_date) = builder.rust_info().commit_date() {
1360 cargo.env("CFG_VER_DATE", ver_date);
1361 }
1362 if let Some(ref ver_hash) = builder.rust_info().sha() {
1363 cargo.env("CFG_VER_HASH", ver_hash);
1364 }
1365 if !builder.unstable_features() {
1366 cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
1367 }
1368
1369 if let Some(s) = target_config.and_then(|c| c.default_linker.as_ref()) {
1372 cargo.env("CFG_DEFAULT_LINKER", s);
1373 } else if let Some(ref s) = builder.config.rustc_default_linker {
1374 cargo.env("CFG_DEFAULT_LINKER", s);
1375 }
1376
1377 if let Some(linker) = target_config.map(|c| c.default_linker_linux_override) {
1379 match linker {
1380 DefaultLinuxLinkerOverride::Off => {}
1381 DefaultLinuxLinkerOverride::SelfContainedLldCc => {
1382 cargo.env("CFG_DEFAULT_LINKER_SELF_CONTAINED_LLD_CC", "1");
1383 }
1384 }
1385 }
1386
1387 if builder.config.rust_verify_llvm_ir {
1388 cargo.env("RUSTC_VERIFY_LLVM_IR", "1");
1389 }
1390
1391 if builder.config.llvm_enabled(target) {
1403 let building_llvm_is_expensive =
1404 crate::core::build_steps::llvm::prebuilt_llvm_config(builder, target, false)
1405 .should_build();
1406
1407 let skip_llvm = (builder.kind == Kind::Check) && building_llvm_is_expensive;
1408 if !skip_llvm {
1409 rustc_llvm_env(builder, cargo, target)
1410 }
1411 }
1412
1413 if builder.config.jemalloc(target) && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none() {
1415 if target.starts_with("aarch64") {
1418 cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "16");
1419 }
1420 else if target.starts_with("loongarch") {
1422 cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "14");
1423 }
1424 }
1425}
1426
1427fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1433 if builder.config.is_rust_llvm(target) {
1434 cargo.env("LLVM_RUSTLLVM", "1");
1435 }
1436 if builder.config.llvm_enzyme {
1437 cargo.env("LLVM_ENZYME", "1");
1438 }
1439 let llvm::LlvmResult { host_llvm_config, .. } = builder.ensure(llvm::Llvm { target });
1440 cargo.env("LLVM_CONFIG", &host_llvm_config);
1441
1442 let mut llvm_linker_flags = String::new();
1452 if builder.config.llvm_profile_generate
1453 && target.is_msvc()
1454 && let Some(ref clang_cl_path) = builder.config.llvm_clang_cl
1455 {
1456 let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
1458 llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
1459 }
1460
1461 if let Some(ref s) = builder.config.llvm_ldflags {
1463 if !llvm_linker_flags.is_empty() {
1464 llvm_linker_flags.push(' ');
1465 }
1466 llvm_linker_flags.push_str(s);
1467 }
1468
1469 if !llvm_linker_flags.is_empty() {
1471 cargo.env("LLVM_LINKER_FLAGS", llvm_linker_flags);
1472 }
1473
1474 if builder.config.llvm_static_stdcpp
1477 && !target.contains("freebsd")
1478 && !target.is_msvc()
1479 && !target.contains("apple")
1480 && !target.contains("solaris")
1481 {
1482 let libstdcxx_name =
1483 if target.contains("windows-gnullvm") { "libc++.a" } else { "libstdc++.a" };
1484 let file = compiler_file(
1485 builder,
1486 &builder.cxx(target).unwrap(),
1487 target,
1488 CLang::Cxx,
1489 libstdcxx_name,
1490 );
1491 cargo.env("LLVM_STATIC_STDCPP", file);
1492 }
1493 if builder.llvm_link_shared() {
1494 cargo.env("LLVM_LINK_SHARED", "1");
1495 }
1496 if builder.config.llvm_use_libcxx {
1497 cargo.env("LLVM_USE_LIBCXX", "1");
1498 }
1499 if builder.config.llvm_assertions {
1500 cargo.env("LLVM_ASSERTIONS", "1");
1501 }
1502}
1503
1504#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1517struct RustcLink {
1518 build_compiler: Compiler,
1520 sysroot_compiler: Compiler,
1523 target: TargetSelection,
1524 crates: Vec<String>,
1526}
1527
1528impl RustcLink {
1529 fn from_rustc(rustc: Rustc) -> Self {
1532 Self {
1533 build_compiler: rustc.build_compiler,
1534 sysroot_compiler: rustc.build_compiler,
1535 target: rustc.target,
1536 crates: rustc.crates,
1537 }
1538 }
1539
1540 fn from_build_compiler_and_sysroot(
1542 build_compiler: Compiler,
1543 sysroot_compiler: Compiler,
1544 target: TargetSelection,
1545 crates: Vec<String>,
1546 ) -> Self {
1547 Self { build_compiler, sysroot_compiler, target, crates }
1548 }
1549}
1550
1551impl Step for RustcLink {
1552 type Output = ();
1553
1554 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1555 run.never()
1556 }
1557
1558 fn run(self, builder: &Builder<'_>) {
1560 let build_compiler = self.build_compiler;
1561 let sysroot_compiler = self.sysroot_compiler;
1562 let target = self.target;
1563 add_to_sysroot(
1564 builder,
1565 &builder.sysroot_target_libdir(sysroot_compiler, target),
1566 &builder.sysroot_target_libdir(sysroot_compiler, sysroot_compiler.host),
1567 &build_stamp::librustc_stamp(builder, build_compiler, target),
1568 );
1569 }
1570}
1571
1572#[derive(Clone)]
1575pub struct GccCodegenBackendOutput {
1576 stamp: BuildStamp,
1577 gcc: GccOutput,
1578}
1579
1580#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1581pub struct GccCodegenBackend {
1582 compilers: RustcPrivateCompilers,
1583}
1584
1585impl Step for GccCodegenBackend {
1586 type Output = GccCodegenBackendOutput;
1587
1588 const IS_HOST: bool = true;
1589
1590 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1591 run.alias("rustc_codegen_gcc").alias("cg_gcc")
1592 }
1593
1594 fn make_run(run: RunConfig<'_>) {
1595 run.builder.ensure(GccCodegenBackend {
1596 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1597 });
1598 }
1599
1600 fn run(self, builder: &Builder<'_>) -> Self::Output {
1601 let target = self.compilers.target();
1602 let build_compiler = self.compilers.build_compiler();
1603
1604 let stamp = build_stamp::codegen_backend_stamp(
1605 builder,
1606 build_compiler,
1607 target,
1608 &CodegenBackendKind::Gcc,
1609 );
1610
1611 let gcc = builder.ensure(Gcc { target });
1612
1613 if builder.config.keep_stage.contains(&build_compiler.stage) {
1614 trace!("`keep-stage` requested");
1615 builder.info(
1616 "WARNING: Using a potentially old codegen backend. \
1617 This may not behave well.",
1618 );
1619 return GccCodegenBackendOutput { stamp, gcc };
1622 }
1623
1624 let mut cargo = builder::Cargo::new(
1625 builder,
1626 build_compiler,
1627 Mode::Codegen,
1628 SourceType::InTree,
1629 target,
1630 Kind::Build,
1631 );
1632 cargo.arg("--manifest-path").arg(builder.src.join("compiler/rustc_codegen_gcc/Cargo.toml"));
1633 rustc_cargo_env(builder, &mut cargo, target);
1634
1635 add_cg_gcc_cargo_flags(&mut cargo, &gcc);
1636
1637 let _guard =
1638 builder.msg(Kind::Build, "codegen backend gcc", Mode::Codegen, build_compiler, target);
1639 let files = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false);
1640
1641 GccCodegenBackendOutput {
1642 stamp: write_codegen_backend_stamp(stamp, files, builder.config.dry_run()),
1643 gcc,
1644 }
1645 }
1646
1647 fn metadata(&self) -> Option<StepMetadata> {
1648 Some(
1649 StepMetadata::build("rustc_codegen_gcc", self.compilers.target())
1650 .built_by(self.compilers.build_compiler()),
1651 )
1652 }
1653}
1654
1655#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1656pub struct CraneliftCodegenBackend {
1657 pub compilers: RustcPrivateCompilers,
1658}
1659
1660impl Step for CraneliftCodegenBackend {
1661 type Output = BuildStamp;
1662 const IS_HOST: bool = true;
1663
1664 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1665 run.alias("rustc_codegen_cranelift").alias("cg_clif")
1666 }
1667
1668 fn make_run(run: RunConfig<'_>) {
1669 run.builder.ensure(CraneliftCodegenBackend {
1670 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1671 });
1672 }
1673
1674 fn run(self, builder: &Builder<'_>) -> Self::Output {
1675 let target = self.compilers.target();
1676 let build_compiler = self.compilers.build_compiler();
1677
1678 let stamp = build_stamp::codegen_backend_stamp(
1679 builder,
1680 build_compiler,
1681 target,
1682 &CodegenBackendKind::Cranelift,
1683 );
1684
1685 if builder.config.keep_stage.contains(&build_compiler.stage) {
1686 trace!("`keep-stage` requested");
1687 builder.info(
1688 "WARNING: Using a potentially old codegen backend. \
1689 This may not behave well.",
1690 );
1691 return stamp;
1694 }
1695
1696 let mut cargo = builder::Cargo::new(
1697 builder,
1698 build_compiler,
1699 Mode::Codegen,
1700 SourceType::InTree,
1701 target,
1702 Kind::Build,
1703 );
1704 cargo
1705 .arg("--manifest-path")
1706 .arg(builder.src.join("compiler/rustc_codegen_cranelift/Cargo.toml"));
1707 rustc_cargo_env(builder, &mut cargo, target);
1708
1709 let _guard = builder.msg(
1710 Kind::Build,
1711 "codegen backend cranelift",
1712 Mode::Codegen,
1713 build_compiler,
1714 target,
1715 );
1716 let files = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false);
1717 write_codegen_backend_stamp(stamp, files, builder.config.dry_run())
1718 }
1719
1720 fn metadata(&self) -> Option<StepMetadata> {
1721 Some(
1722 StepMetadata::build("rustc_codegen_cranelift", self.compilers.target())
1723 .built_by(self.compilers.build_compiler()),
1724 )
1725 }
1726}
1727
1728fn write_codegen_backend_stamp(
1730 mut stamp: BuildStamp,
1731 files: Vec<PathBuf>,
1732 dry_run: bool,
1733) -> BuildStamp {
1734 if dry_run {
1735 return stamp;
1736 }
1737
1738 let mut files = files.into_iter().filter(|f| {
1739 let filename = f.file_name().unwrap().to_str().unwrap();
1740 is_dylib(f) && filename.contains("rustc_codegen_")
1741 });
1742 let codegen_backend = match files.next() {
1743 Some(f) => f,
1744 None => panic!("no dylibs built for codegen backend?"),
1745 };
1746 if let Some(f) = files.next() {
1747 panic!("codegen backend built two dylibs:\n{}\n{}", codegen_backend.display(), f.display());
1748 }
1749
1750 let codegen_backend = codegen_backend.to_str().unwrap();
1751 stamp = stamp.add_stamp(codegen_backend);
1752 t!(stamp.write());
1753 stamp
1754}
1755
1756fn copy_codegen_backends_to_sysroot(
1763 builder: &Builder<'_>,
1764 stamp: BuildStamp,
1765 target_compiler: Compiler,
1766) {
1767 let dst = builder.sysroot_codegen_backends(target_compiler);
1776 t!(fs::create_dir_all(&dst), dst);
1777
1778 if builder.config.dry_run() {
1779 return;
1780 }
1781
1782 if stamp.path().exists() {
1783 let file = get_codegen_backend_file(&stamp);
1784 builder.copy_link(
1785 &file,
1786 &dst.join(normalize_codegen_backend_name(builder, &file)),
1787 FileType::NativeLibrary,
1788 );
1789 }
1790}
1791
1792pub fn get_codegen_backend_file(stamp: &BuildStamp) -> PathBuf {
1794 PathBuf::from(t!(fs::read_to_string(stamp.path())))
1795}
1796
1797pub fn normalize_codegen_backend_name(builder: &Builder<'_>, path: &Path) -> String {
1799 let filename = path.file_name().unwrap().to_str().unwrap();
1800 let dash = filename.find('-').unwrap();
1803 let dot = filename.find('.').unwrap();
1804 format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..])
1805}
1806
1807pub fn compiler_file(
1808 builder: &Builder<'_>,
1809 compiler: &Path,
1810 target: TargetSelection,
1811 c: CLang,
1812 file: &str,
1813) -> PathBuf {
1814 if builder.config.dry_run() {
1815 return PathBuf::new();
1816 }
1817 let mut cmd = command(compiler);
1818 cmd.args(builder.cc_handled_clags(target, c));
1819 cmd.args(builder.cc_unhandled_cflags(target, GitRepo::Rustc, c));
1820 cmd.arg(format!("-print-file-name={file}"));
1821 let out = cmd.run_capture_stdout(builder).stdout();
1822 PathBuf::from(out.trim())
1823}
1824
1825#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1826pub struct Sysroot {
1827 pub compiler: Compiler,
1828 force_recompile: bool,
1830}
1831
1832impl Sysroot {
1833 pub(crate) fn new(compiler: Compiler) -> Self {
1834 Sysroot { compiler, force_recompile: false }
1835 }
1836}
1837
1838impl Step for Sysroot {
1839 type Output = PathBuf;
1840
1841 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1842 run.never()
1843 }
1844
1845 fn run(self, builder: &Builder<'_>) -> PathBuf {
1849 let compiler = self.compiler;
1850 let host_dir = builder.out.join(compiler.host);
1851
1852 let sysroot_dir = |stage| {
1853 if stage == 0 {
1854 host_dir.join("stage0-sysroot")
1855 } else if self.force_recompile && stage == compiler.stage {
1856 host_dir.join(format!("stage{stage}-test-sysroot"))
1857 } else if builder.download_rustc() && compiler.stage != builder.top_stage {
1858 host_dir.join("ci-rustc-sysroot")
1859 } else {
1860 host_dir.join(format!("stage{stage}"))
1861 }
1862 };
1863 let sysroot = sysroot_dir(compiler.stage);
1864 trace!(stage = ?compiler.stage, ?sysroot);
1865
1866 builder.do_if_verbose(|| {
1867 println!("Removing sysroot {} to avoid caching bugs", sysroot.display())
1868 });
1869 let _ = fs::remove_dir_all(&sysroot);
1870 t!(fs::create_dir_all(&sysroot));
1871
1872 if compiler.stage == 0 {
1879 dist::maybe_install_llvm_target(builder, compiler.host, &sysroot);
1880 }
1881
1882 if builder.download_rustc() && compiler.stage != 0 {
1884 assert_eq!(
1885 builder.config.host_target, compiler.host,
1886 "Cross-compiling is not yet supported with `download-rustc`",
1887 );
1888
1889 for stage in 0..=2 {
1891 if stage != compiler.stage {
1892 let dir = sysroot_dir(stage);
1893 if !dir.ends_with("ci-rustc-sysroot") {
1894 let _ = fs::remove_dir_all(dir);
1895 }
1896 }
1897 }
1898
1899 let mut filtered_files = Vec::new();
1909 let mut add_filtered_files = |suffix, contents| {
1910 for path in contents {
1911 let path = Path::new(&path);
1912 if path.parent().is_some_and(|parent| parent.ends_with(suffix)) {
1913 filtered_files.push(path.file_name().unwrap().to_owned());
1914 }
1915 }
1916 };
1917 let suffix = format!("lib/rustlib/{}/lib", compiler.host);
1918 add_filtered_files(suffix.as_str(), builder.config.ci_rustc_dev_contents());
1919 add_filtered_files("lib", builder.config.ci_rust_std_contents());
1922
1923 let filtered_extensions = [
1924 OsStr::new("rmeta"),
1925 OsStr::new("rlib"),
1926 OsStr::new(std::env::consts::DLL_EXTENSION),
1928 ];
1929 let ci_rustc_dir = builder.config.ci_rustc_dir();
1930 builder.cp_link_filtered(&ci_rustc_dir, &sysroot, &|path| {
1931 if path.extension().is_none_or(|ext| !filtered_extensions.contains(&ext)) {
1932 return true;
1933 }
1934 if !path.parent().is_none_or(|p| p.ends_with(&suffix)) {
1935 return true;
1936 }
1937 filtered_files.iter().all(|f| f != path.file_name().unwrap())
1938 });
1939 }
1940
1941 if compiler.stage != 0 {
1947 let sysroot_lib_rustlib_src = sysroot.join("lib/rustlib/src");
1948 t!(fs::create_dir_all(&sysroot_lib_rustlib_src));
1949 let sysroot_lib_rustlib_src_rust = sysroot_lib_rustlib_src.join("rust");
1950 if let Err(e) =
1951 symlink_dir(&builder.config, &builder.src, &sysroot_lib_rustlib_src_rust)
1952 {
1953 eprintln!(
1954 "ERROR: creating symbolic link `{}` to `{}` failed with {}",
1955 sysroot_lib_rustlib_src_rust.display(),
1956 builder.src.display(),
1957 e,
1958 );
1959 if builder.config.rust_remap_debuginfo {
1960 eprintln!(
1961 "ERROR: some `tests/ui` tests will fail when lacking `{}`",
1962 sysroot_lib_rustlib_src_rust.display(),
1963 );
1964 }
1965 build_helper::exit!(1);
1966 }
1967 }
1968
1969 if !builder.download_rustc() {
1971 let sysroot_lib_rustlib_rustcsrc = sysroot.join("lib/rustlib/rustc-src");
1972 t!(fs::create_dir_all(&sysroot_lib_rustlib_rustcsrc));
1973 let sysroot_lib_rustlib_rustcsrc_rust = sysroot_lib_rustlib_rustcsrc.join("rust");
1974 if let Err(e) =
1975 symlink_dir(&builder.config, &builder.src, &sysroot_lib_rustlib_rustcsrc_rust)
1976 {
1977 eprintln!(
1978 "ERROR: creating symbolic link `{}` to `{}` failed with {}",
1979 sysroot_lib_rustlib_rustcsrc_rust.display(),
1980 builder.src.display(),
1981 e,
1982 );
1983 build_helper::exit!(1);
1984 }
1985 }
1986
1987 sysroot
1988 }
1989}
1990
1991#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1998pub struct Assemble {
1999 pub target_compiler: Compiler,
2004}
2005
2006impl Step for Assemble {
2007 type Output = Compiler;
2008 const IS_HOST: bool = true;
2009
2010 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2011 run.path("compiler/rustc").path("compiler")
2012 }
2013
2014 fn make_run(run: RunConfig<'_>) {
2015 run.builder.ensure(Assemble {
2016 target_compiler: run.builder.compiler(run.builder.top_stage, run.target),
2017 });
2018 }
2019
2020 fn run(self, builder: &Builder<'_>) -> Compiler {
2021 let target_compiler = self.target_compiler;
2022
2023 if target_compiler.stage == 0 {
2024 trace!("stage 0 build compiler is always available, simply returning");
2025 assert_eq!(
2026 builder.config.host_target, target_compiler.host,
2027 "Cannot obtain compiler for non-native build triple at stage 0"
2028 );
2029 return target_compiler;
2031 }
2032
2033 let libdir = builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2036 let libdir_bin = libdir.parent().unwrap().join("bin");
2037 t!(fs::create_dir_all(&libdir_bin));
2038
2039 if builder.config.llvm_enabled(target_compiler.host) {
2040 trace!("target_compiler.host" = ?target_compiler.host, "LLVM enabled");
2041
2042 let target = target_compiler.host;
2043 let llvm::LlvmResult { host_llvm_config, .. } = builder.ensure(llvm::Llvm { target });
2044 if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
2045 trace!("LLVM tools enabled");
2046
2047 let host_llvm_bin_dir = command(&host_llvm_config)
2048 .arg("--bindir")
2049 .cached()
2050 .run_capture_stdout(builder)
2051 .stdout()
2052 .trim()
2053 .to_string();
2054
2055 let llvm_bin_dir = if target == builder.host_target {
2056 PathBuf::from(host_llvm_bin_dir)
2057 } else {
2058 let external_llvm_config = builder
2061 .config
2062 .target_config
2063 .get(&target)
2064 .and_then(|t| t.llvm_config.clone());
2065 if let Some(external_llvm_config) = external_llvm_config {
2066 external_llvm_config.parent().unwrap().to_path_buf()
2069 } else {
2070 let host_llvm_out = builder.llvm_out(builder.host_target);
2074 let target_llvm_out = builder.llvm_out(target);
2075 if let Ok(relative_path) =
2076 Path::new(&host_llvm_bin_dir).strip_prefix(host_llvm_out)
2077 {
2078 target_llvm_out.join(relative_path)
2079 } else {
2080 PathBuf::from(
2083 host_llvm_bin_dir
2084 .replace(&*builder.host_target.triple, &target.triple),
2085 )
2086 }
2087 }
2088 };
2089
2090 #[cfg(feature = "tracing")]
2097 let _llvm_tools_span =
2098 span!(tracing::Level::TRACE, "installing llvm tools to sysroot", ?libdir_bin)
2099 .entered();
2100 for tool in LLVM_TOOLS {
2101 trace!("installing `{tool}`");
2102 let tool_exe = exe(tool, target_compiler.host);
2103 let src_path = llvm_bin_dir.join(&tool_exe);
2104
2105 if !src_path.exists() && builder.config.llvm_from_ci {
2107 eprintln!("{} does not exist; skipping copy", src_path.display());
2108 continue;
2109 }
2110
2111 builder.resolve_symlink_and_copy(&src_path, &libdir_bin.join(&tool_exe));
2118 }
2119 }
2120 }
2121
2122 let maybe_install_llvm_bitcode_linker = || {
2123 if builder.config.llvm_bitcode_linker_enabled {
2124 trace!("llvm-bitcode-linker enabled, installing");
2125 let llvm_bitcode_linker = builder.ensure(
2126 crate::core::build_steps::tool::LlvmBitcodeLinker::from_target_compiler(
2127 builder,
2128 target_compiler,
2129 ),
2130 );
2131
2132 let bindir_self_contained = builder
2134 .sysroot(target_compiler)
2135 .join(format!("lib/rustlib/{}/bin/self-contained", target_compiler.host));
2136 let tool_exe = exe("llvm-bitcode-linker", target_compiler.host);
2137
2138 t!(fs::create_dir_all(&bindir_self_contained));
2139 builder.copy_link(
2140 &llvm_bitcode_linker.tool_path,
2141 &bindir_self_contained.join(tool_exe),
2142 FileType::Executable,
2143 );
2144 }
2145 };
2146
2147 if builder.download_rustc() {
2149 trace!("`download-rustc` requested, reusing CI compiler for stage > 0");
2150
2151 builder.std(target_compiler, target_compiler.host);
2152 let sysroot =
2153 builder.ensure(Sysroot { compiler: target_compiler, force_recompile: false });
2154 dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
2157 if target_compiler.stage == builder.top_stage {
2159 builder.info(&format!("Creating a sysroot for stage{stage} compiler (use `rustup toolchain link 'name' build/host/stage{stage}`)", stage = target_compiler.stage));
2160 }
2161
2162 maybe_install_llvm_bitcode_linker();
2165
2166 return target_compiler;
2167 }
2168
2169 debug!(
2183 "ensuring build compiler is available: compiler(stage = {}, host = {:?})",
2184 target_compiler.stage - 1,
2185 builder.config.host_target,
2186 );
2187 let build_compiler =
2188 builder.compiler(target_compiler.stage - 1, builder.config.host_target);
2189
2190 if builder.config.llvm_enzyme && !builder.config.dry_run() {
2192 debug!("`llvm_enzyme` requested");
2193 let enzyme_install = builder.ensure(llvm::Enzyme { target: build_compiler.host });
2194 if let Some(llvm_config) = builder.llvm_config(builder.config.host_target) {
2195 let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
2196 let lib_ext = std::env::consts::DLL_EXTENSION;
2197 let libenzyme = format!("libEnzyme-{llvm_version_major}");
2198 let src_lib =
2199 enzyme_install.join("build/Enzyme").join(&libenzyme).with_extension(lib_ext);
2200 let libdir = builder.sysroot_target_libdir(build_compiler, build_compiler.host);
2201 let target_libdir =
2202 builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2203 let dst_lib = libdir.join(&libenzyme).with_extension(lib_ext);
2204 let target_dst_lib = target_libdir.join(&libenzyme).with_extension(lib_ext);
2205 builder.copy_link(&src_lib, &dst_lib, FileType::NativeLibrary);
2206 builder.copy_link(&src_lib, &target_dst_lib, FileType::NativeLibrary);
2207 }
2208 }
2209
2210 debug!(
2213 ?build_compiler,
2214 "target_compiler.host" = ?target_compiler.host,
2215 "building compiler libraries to link to"
2216 );
2217
2218 let BuiltRustc { build_compiler } =
2220 builder.ensure(Rustc::new(build_compiler, target_compiler.host));
2221
2222 let stage = target_compiler.stage;
2223 let host = target_compiler.host;
2224 let (host_info, dir_name) = if build_compiler.host == host {
2225 ("".into(), "host".into())
2226 } else {
2227 (format!(" ({host})"), host.to_string())
2228 };
2229 let msg = format!(
2234 "Creating a sysroot for stage{stage} compiler{host_info} (use `rustup toolchain link 'name' build/{dir_name}/stage{stage}`)"
2235 );
2236 builder.info(&msg);
2237
2238 let stamp = build_stamp::librustc_stamp(builder, build_compiler, target_compiler.host);
2240 let proc_macros = builder
2241 .read_stamp_file(&stamp)
2242 .into_iter()
2243 .filter_map(|(path, dependency_type)| {
2244 if dependency_type == DependencyType::Host {
2245 Some(path.file_name().unwrap().to_owned().into_string().unwrap())
2246 } else {
2247 None
2248 }
2249 })
2250 .collect::<HashSet<_>>();
2251
2252 let sysroot = builder.sysroot(target_compiler);
2253 let rustc_libdir = builder.rustc_libdir(target_compiler);
2254 t!(fs::create_dir_all(&rustc_libdir));
2255 let src_libdir = builder.sysroot_target_libdir(build_compiler, host);
2256 for f in builder.read_dir(&src_libdir) {
2257 let filename = f.file_name().into_string().unwrap();
2258
2259 let is_proc_macro = proc_macros.contains(&filename);
2260 let is_dylib_or_debug = is_dylib(&f.path()) || is_debug_info(&filename);
2261
2262 let can_be_rustc_dynamic_dep = if builder
2266 .link_std_into_rustc_driver(target_compiler.host)
2267 && !target_compiler.host.is_windows()
2268 {
2269 let is_std = filename.starts_with("std-") || filename.starts_with("libstd-");
2270 !is_std
2271 } else {
2272 true
2273 };
2274
2275 if is_dylib_or_debug && can_be_rustc_dynamic_dep && !is_proc_macro {
2276 builder.copy_link(&f.path(), &rustc_libdir.join(&filename), FileType::Regular);
2277 }
2278 }
2279
2280 {
2281 #[cfg(feature = "tracing")]
2282 let _codegen_backend_span =
2283 span!(tracing::Level::DEBUG, "building requested codegen backends").entered();
2284
2285 for backend in builder.config.enabled_codegen_backends(target_compiler.host) {
2286 if builder.kind == Kind::Check && builder.top_stage == 1 {
2303 continue;
2304 }
2305
2306 let prepare_compilers = || {
2307 RustcPrivateCompilers::from_build_and_target_compiler(
2308 build_compiler,
2309 target_compiler,
2310 )
2311 };
2312
2313 match backend {
2314 CodegenBackendKind::Cranelift => {
2315 let stamp = builder
2316 .ensure(CraneliftCodegenBackend { compilers: prepare_compilers() });
2317 copy_codegen_backends_to_sysroot(builder, stamp, target_compiler);
2318 }
2319 CodegenBackendKind::Gcc => {
2320 let output =
2321 builder.ensure(GccCodegenBackend { compilers: prepare_compilers() });
2322 copy_codegen_backends_to_sysroot(builder, output.stamp, target_compiler);
2323 output.gcc.install_to(builder, &rustc_libdir);
2326 }
2327 CodegenBackendKind::Llvm | CodegenBackendKind::Custom(_) => continue,
2328 }
2329 }
2330 }
2331
2332 if builder.config.lld_enabled {
2333 let lld_wrapper =
2334 builder.ensure(crate::core::build_steps::tool::LldWrapper::for_use_by_compiler(
2335 builder,
2336 target_compiler,
2337 ));
2338 copy_lld_artifacts(builder, lld_wrapper, target_compiler);
2339 }
2340
2341 if builder.config.llvm_enabled(target_compiler.host) && builder.config.llvm_tools_enabled {
2342 debug!(
2343 "llvm and llvm tools enabled; copying `llvm-objcopy` as `rust-objcopy` to \
2344 workaround faulty homebrew `strip`s"
2345 );
2346
2347 let src_exe = exe("llvm-objcopy", target_compiler.host);
2354 let dst_exe = exe("rust-objcopy", target_compiler.host);
2355 builder.copy_link(
2356 &libdir_bin.join(src_exe),
2357 &libdir_bin.join(dst_exe),
2358 FileType::Executable,
2359 );
2360 }
2361
2362 if builder.tool_enabled("wasm-component-ld") {
2365 let wasm_component = builder.ensure(
2366 crate::core::build_steps::tool::WasmComponentLd::for_use_by_compiler(
2367 builder,
2368 target_compiler,
2369 ),
2370 );
2371 builder.copy_link(
2372 &wasm_component.tool_path,
2373 &libdir_bin.join(wasm_component.tool_path.file_name().unwrap()),
2374 FileType::Executable,
2375 );
2376 }
2377
2378 maybe_install_llvm_bitcode_linker();
2379
2380 debug!(
2383 "target_compiler.host" = ?target_compiler.host,
2384 ?sysroot,
2385 "ensuring availability of `libLLVM.so` in compiler directory"
2386 );
2387 dist::maybe_install_llvm_runtime(builder, target_compiler.host, &sysroot);
2388 dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
2389
2390 let out_dir = builder.cargo_out(build_compiler, Mode::Rustc, host);
2392 let rustc = out_dir.join(exe("rustc-main", host));
2393 let bindir = sysroot.join("bin");
2394 t!(fs::create_dir_all(bindir));
2395 let compiler = builder.rustc(target_compiler);
2396 debug!(src = ?rustc, dst = ?compiler, "linking compiler binary itself");
2397 builder.copy_link(&rustc, &compiler, FileType::Executable);
2398
2399 target_compiler
2400 }
2401}
2402
2403#[track_caller]
2408pub fn add_to_sysroot(
2409 builder: &Builder<'_>,
2410 sysroot_dst: &Path,
2411 sysroot_host_dst: &Path,
2412 stamp: &BuildStamp,
2413) {
2414 let self_contained_dst = &sysroot_dst.join("self-contained");
2415 t!(fs::create_dir_all(sysroot_dst));
2416 t!(fs::create_dir_all(sysroot_host_dst));
2417 t!(fs::create_dir_all(self_contained_dst));
2418 for (path, dependency_type) in builder.read_stamp_file(stamp) {
2419 let dst = match dependency_type {
2420 DependencyType::Host => sysroot_host_dst,
2421 DependencyType::Target => sysroot_dst,
2422 DependencyType::TargetSelfContained => self_contained_dst,
2423 };
2424 builder.copy_link(&path, &dst.join(path.file_name().unwrap()), FileType::Regular);
2425 }
2426}
2427
2428pub fn run_cargo(
2429 builder: &Builder<'_>,
2430 cargo: Cargo,
2431 tail_args: Vec<String>,
2432 stamp: &BuildStamp,
2433 additional_target_deps: Vec<(PathBuf, DependencyType)>,
2434 is_check: bool,
2435 rlib_only_metadata: bool,
2436) -> Vec<PathBuf> {
2437 let target_root_dir = stamp.path().parent().unwrap();
2439 let target_deps_dir = target_root_dir.join("deps");
2441 let host_root_dir = target_root_dir
2443 .parent()
2444 .unwrap() .parent()
2446 .unwrap() .join(target_root_dir.file_name().unwrap());
2448
2449 let mut deps = Vec::new();
2453 let mut toplevel = Vec::new();
2454 let ok = stream_cargo(builder, cargo, tail_args, &mut |msg| {
2455 let (filenames_vec, crate_types) = match msg {
2456 CargoMessage::CompilerArtifact {
2457 filenames,
2458 target: CargoTarget { crate_types },
2459 ..
2460 } => {
2461 let mut f: Vec<String> = filenames.into_iter().map(|s| s.into_owned()).collect();
2462 f.sort(); (f, crate_types)
2464 }
2465 _ => return,
2466 };
2467 for filename in filenames_vec {
2468 let mut keep = false;
2470 if filename.ends_with(".lib")
2471 || filename.ends_with(".a")
2472 || is_debug_info(&filename)
2473 || is_dylib(Path::new(&*filename))
2474 {
2475 keep = true;
2477 }
2478 if is_check && filename.ends_with(".rmeta") {
2479 keep = true;
2481 } else if rlib_only_metadata {
2482 if filename.contains("jemalloc_sys")
2483 || filename.contains("rustc_public_bridge")
2484 || filename.contains("rustc_public")
2485 {
2486 keep |= filename.ends_with(".rlib");
2489 } else {
2490 keep |= filename.ends_with(".rmeta");
2494 }
2495 } else {
2496 keep |= filename.ends_with(".rlib");
2498 }
2499
2500 if !keep {
2501 continue;
2502 }
2503
2504 let filename = Path::new(&*filename);
2505
2506 if filename.starts_with(&host_root_dir) {
2509 if crate_types.iter().any(|t| t == "proc-macro") {
2511 deps.push((filename.to_path_buf(), DependencyType::Host));
2512 }
2513 continue;
2514 }
2515
2516 if filename.starts_with(&target_deps_dir) {
2519 deps.push((filename.to_path_buf(), DependencyType::Target));
2520 continue;
2521 }
2522
2523 let expected_len = t!(filename.metadata()).len();
2534 let filename = filename.file_name().unwrap().to_str().unwrap();
2535 let mut parts = filename.splitn(2, '.');
2536 let file_stem = parts.next().unwrap().to_owned();
2537 let extension = parts.next().unwrap().to_owned();
2538
2539 toplevel.push((file_stem, extension, expected_len));
2540 }
2541 });
2542
2543 if !ok {
2544 crate::exit!(1);
2545 }
2546
2547 if builder.config.dry_run() {
2548 return Vec::new();
2549 }
2550
2551 let contents = target_deps_dir
2555 .read_dir()
2556 .unwrap_or_else(|e| panic!("Couldn't read {}: {}", target_deps_dir.display(), e))
2557 .map(|e| t!(e))
2558 .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata())))
2559 .collect::<Vec<_>>();
2560 for (prefix, extension, expected_len) in toplevel {
2561 let candidates = contents.iter().filter(|&(_, filename, meta)| {
2562 meta.len() == expected_len
2563 && filename
2564 .strip_prefix(&prefix[..])
2565 .map(|s| s.starts_with('-') && s.ends_with(&extension[..]))
2566 .unwrap_or(false)
2567 });
2568 let max = candidates.max_by_key(|&(_, _, metadata)| {
2569 metadata.modified().expect("mtime should be available on all relevant OSes")
2570 });
2571 let path_to_add = match max {
2572 Some(triple) => triple.0.to_str().unwrap(),
2573 None => panic!("no output generated for {prefix:?} {extension:?}"),
2574 };
2575 if is_dylib(Path::new(path_to_add)) {
2576 let candidate = format!("{path_to_add}.lib");
2577 let candidate = PathBuf::from(candidate);
2578 if candidate.exists() {
2579 deps.push((candidate, DependencyType::Target));
2580 }
2581 }
2582 deps.push((path_to_add.into(), DependencyType::Target));
2583 }
2584
2585 deps.extend(additional_target_deps);
2586 deps.sort();
2587 let mut new_contents = Vec::new();
2588 for (dep, dependency_type) in deps.iter() {
2589 new_contents.extend(match *dependency_type {
2590 DependencyType::Host => b"h",
2591 DependencyType::Target => b"t",
2592 DependencyType::TargetSelfContained => b"s",
2593 });
2594 new_contents.extend(dep.to_str().unwrap().as_bytes());
2595 new_contents.extend(b"\0");
2596 }
2597 t!(fs::write(stamp.path(), &new_contents));
2598 deps.into_iter().map(|(d, _)| d).collect()
2599}
2600
2601pub fn stream_cargo(
2602 builder: &Builder<'_>,
2603 cargo: Cargo,
2604 tail_args: Vec<String>,
2605 cb: &mut dyn FnMut(CargoMessage<'_>),
2606) -> bool {
2607 let mut cmd = cargo.into_cmd();
2608
2609 let mut message_format = if builder.config.json_output {
2612 String::from("json")
2613 } else {
2614 String::from("json-render-diagnostics")
2615 };
2616 if let Some(s) = &builder.config.rustc_error_format {
2617 message_format.push_str(",json-diagnostic-");
2618 message_format.push_str(s);
2619 }
2620 cmd.arg("--message-format").arg(message_format);
2621
2622 for arg in tail_args {
2623 cmd.arg(arg);
2624 }
2625
2626 builder.do_if_verbose(|| println!("running: {cmd:?}"));
2627
2628 let streaming_command = cmd.stream_capture_stdout(&builder.config.exec_ctx);
2629
2630 let Some(mut streaming_command) = streaming_command else {
2631 return true;
2632 };
2633
2634 let stdout = BufReader::new(streaming_command.stdout.take().unwrap());
2638 for line in stdout.lines() {
2639 let line = t!(line);
2640 match serde_json::from_str::<CargoMessage<'_>>(&line) {
2641 Ok(msg) => {
2642 if builder.config.json_output {
2643 println!("{line}");
2645 }
2646 cb(msg)
2647 }
2648 Err(_) => println!("{line}"),
2650 }
2651 }
2652
2653 let status = t!(streaming_command.wait(&builder.config.exec_ctx));
2655 if builder.is_verbose() && !status.success() {
2656 eprintln!(
2657 "command did not execute successfully: {cmd:?}\n\
2658 expected success, got: {status}"
2659 );
2660 }
2661
2662 status.success()
2663}
2664
2665#[derive(Deserialize)]
2666pub struct CargoTarget<'a> {
2667 crate_types: Vec<Cow<'a, str>>,
2668}
2669
2670#[derive(Deserialize)]
2671#[serde(tag = "reason", rename_all = "kebab-case")]
2672pub enum CargoMessage<'a> {
2673 CompilerArtifact { filenames: Vec<Cow<'a, str>>, target: CargoTarget<'a> },
2674 BuildScriptExecuted,
2675 BuildFinished,
2676}
2677
2678pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path) {
2679 if target != "x86_64-unknown-linux-gnu"
2683 || !builder.config.is_host_target(target)
2684 || !path.exists()
2685 {
2686 return;
2687 }
2688
2689 let previous_mtime = t!(t!(path.metadata()).modified());
2690 let stamp = BuildStamp::new(path.parent().unwrap())
2691 .with_prefix(path.file_name().unwrap().to_str().unwrap())
2692 .with_prefix("strip")
2693 .add_stamp(previous_mtime.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos());
2694
2695 if !stamp.is_up_to_date() {
2698 command("strip").arg("--strip-debug").arg(path).run_capture(builder);
2699 }
2700 t!(stamp.write());
2701
2702 let file = t!(fs::File::open(path));
2703
2704 t!(file.set_modified(previous_mtime));
2717}
2718
2719pub fn is_lto_stage(build_compiler: &Compiler) -> bool {
2721 build_compiler.stage != 0
2722}