bootstrap/core/build_steps/
compile.rs

1//! Implementation of compiling various phases of the compiler and standard
2//! library.
3//!
4//! This module contains some of the real meat in the bootstrap build system
5//! which is where Cargo is used to compile the standard library, libtest, and
6//! the compiler. This module is also responsible for assembling the sysroot as it
7//! goes along from the output of the previous stage.
8
9use std::borrow::Cow;
10use std::collections::HashSet;
11use std::ffi::OsStr;
12use std::io::BufReader;
13use std::io::prelude::*;
14use std::path::{Path, PathBuf};
15use std::time::SystemTime;
16use std::{env, fs, str};
17
18use serde_derive::Deserialize;
19#[cfg(feature = "tracing")]
20use tracing::span;
21
22use crate::core::build_steps::gcc::{Gcc, GccOutput, add_cg_gcc_cargo_flags};
23use crate::core::build_steps::tool::{RustcPrivateCompilers, SourceType, copy_lld_artifacts};
24use crate::core::build_steps::{dist, llvm};
25use crate::core::builder;
26use crate::core::builder::{
27    Builder, Cargo, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description,
28};
29use crate::core::config::{DebuginfoLevel, LlvmLibunwind, RustcLto, TargetSelection};
30use crate::utils::build_stamp;
31use crate::utils::build_stamp::BuildStamp;
32use crate::utils::exec::command;
33use crate::utils::helpers::{
34    exe, get_clang_cl_resource_dir, is_debug_info, is_dylib, symlink_dir, t, up_to_date,
35};
36use crate::{
37    CLang, CodegenBackendKind, Compiler, DependencyType, FileType, GitRepo, LLVM_TOOLS, Mode,
38    debug, trace,
39};
40
41/// Build a standard library for the given `target` using the given `build_compiler`.
42#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
43pub struct Std {
44    pub target: TargetSelection,
45    /// Compiler that builds the standard library.
46    pub build_compiler: Compiler,
47    /// Whether to build only a subset of crates in the standard library.
48    ///
49    /// This shouldn't be used from other steps; see the comment on [`Rustc`].
50    crates: Vec<String>,
51    /// When using download-rustc, we need to use a new build of `std` for running unit tests of Std itself,
52    /// but we need to use the downloaded copy of std for linking to rustdoc. Allow this to be overridden by `builder.ensure` from other steps.
53    force_recompile: bool,
54    extra_rust_args: &'static [&'static str],
55    is_for_mir_opt_tests: bool,
56}
57
58impl Std {
59    pub fn new(build_compiler: Compiler, target: TargetSelection) -> Self {
60        Self {
61            target,
62            build_compiler,
63            crates: Default::default(),
64            force_recompile: false,
65            extra_rust_args: &[],
66            is_for_mir_opt_tests: false,
67        }
68    }
69
70    pub fn force_recompile(mut self, force_recompile: bool) -> Self {
71        self.force_recompile = force_recompile;
72        self
73    }
74
75    #[expect(clippy::wrong_self_convention)]
76    pub fn is_for_mir_opt_tests(mut self, is_for_mir_opt_tests: bool) -> Self {
77        self.is_for_mir_opt_tests = is_for_mir_opt_tests;
78        self
79    }
80
81    pub fn extra_rust_args(mut self, extra_rust_args: &'static [&'static str]) -> Self {
82        self.extra_rust_args = extra_rust_args;
83        self
84    }
85
86    fn copy_extra_objects(
87        &self,
88        builder: &Builder<'_>,
89        compiler: &Compiler,
90        target: TargetSelection,
91    ) -> Vec<(PathBuf, DependencyType)> {
92        let mut deps = Vec::new();
93        if !self.is_for_mir_opt_tests {
94            deps.extend(copy_third_party_objects(builder, compiler, target));
95            deps.extend(copy_self_contained_objects(builder, compiler, target));
96        }
97        deps
98    }
99}
100
101impl Step for Std {
102    type Output = ();
103    const DEFAULT: bool = true;
104
105    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
106        run.crate_or_deps("sysroot").path("library")
107    }
108
109    fn make_run(run: RunConfig<'_>) {
110        let crates = std_crates_for_run_make(&run);
111        let builder = run.builder;
112
113        // Force compilation of the standard library from source if the `library` is modified. This allows
114        // library team to compile the standard library without needing to compile the compiler with
115        // the `rust.download-rustc=true` option.
116        let force_recompile = builder.rust_info().is_managed_git_subrepository()
117            && builder.download_rustc()
118            && builder.config.has_changes_from_upstream(&["library"]);
119
120        trace!("is managed git repo: {}", builder.rust_info().is_managed_git_subrepository());
121        trace!("download_rustc: {}", builder.download_rustc());
122        trace!(force_recompile);
123
124        run.builder.ensure(Std {
125            build_compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
126            target: run.target,
127            crates,
128            force_recompile,
129            extra_rust_args: &[],
130            is_for_mir_opt_tests: false,
131        });
132    }
133
134    /// Builds the standard library.
135    ///
136    /// This will build the standard library for a particular stage of the build
137    /// using the `compiler` targeting the `target` architecture. The artifacts
138    /// created will also be linked into the sysroot directory.
139    fn run(self, builder: &Builder<'_>) {
140        let target = self.target;
141
142        // We already have std ready to be used for stage 0.
143        if self.build_compiler.stage == 0 {
144            let compiler = self.build_compiler;
145            builder.ensure(StdLink::from_std(self, compiler));
146
147            return;
148        }
149
150        let build_compiler = if builder.download_rustc() && self.force_recompile {
151            // When there are changes in the library tree with CI-rustc, we want to build
152            // the stageN library and that requires using stageN-1 compiler.
153            builder
154                .compiler(self.build_compiler.stage.saturating_sub(1), builder.config.host_target)
155        } else {
156            self.build_compiler
157        };
158
159        // When using `download-rustc`, we already have artifacts for the host available. Don't
160        // recompile them.
161        if builder.download_rustc()
162            && builder.config.is_host_target(target)
163            && !self.force_recompile
164        {
165            let sysroot =
166                builder.ensure(Sysroot { compiler: build_compiler, force_recompile: false });
167            cp_rustc_component_to_ci_sysroot(
168                builder,
169                &sysroot,
170                builder.config.ci_rust_std_contents(),
171            );
172            return;
173        }
174
175        if builder.config.keep_stage.contains(&build_compiler.stage)
176            || builder.config.keep_stage_std.contains(&build_compiler.stage)
177        {
178            trace!(keep_stage = ?builder.config.keep_stage);
179            trace!(keep_stage_std = ?builder.config.keep_stage_std);
180
181            builder.info("WARNING: Using a potentially old libstd. This may not behave well.");
182
183            builder.ensure(StartupObjects { compiler: build_compiler, target });
184
185            self.copy_extra_objects(builder, &build_compiler, target);
186
187            builder.ensure(StdLink::from_std(self, build_compiler));
188            return;
189        }
190
191        let mut target_deps = builder.ensure(StartupObjects { compiler: build_compiler, target });
192
193        // Stage of the stdlib that we're building
194        let stage = build_compiler.stage;
195
196        // If we're building a stage2+ libstd, full bootstrap is
197        // disabled and we have a stage1 libstd already compiled for the given target,
198        // then simply uplift a previously built stage1 library.
199        if build_compiler.stage > 1
200            && !builder.config.full_bootstrap
201            // This estimates if a stage1 libstd exists for the given target. If we're not
202            // cross-compiling, it should definitely exist by the time we're building a stage2
203            // libstd.
204            // Or if we are cross-compiling, and we are building a cross-compiled rustc, then that
205            // rustc needs to link to a cross-compiled libstd, so again we should have a stage1
206            // libstd for the given target prepared.
207            // Even if we guess wrong in the cross-compiled case, the worst that should happen is
208            // that we build a fresh stage1 libstd below, and then we immediately uplift it, so we
209            // don't pay the libstd build cost twice.
210            && (target == builder.host_target || builder.config.hosts.contains(&target))
211        {
212            let build_compiler_for_std_to_uplift = builder.compiler(1, builder.host_target);
213            builder.std(build_compiler_for_std_to_uplift, target);
214
215            let msg = if build_compiler_for_std_to_uplift.host == target {
216                format!(
217                    "Uplifting library (stage{} -> stage{stage})",
218                    build_compiler_for_std_to_uplift.stage
219                )
220            } else {
221                format!(
222                    "Uplifting library (stage{}:{} -> stage{stage}:{target})",
223                    build_compiler_for_std_to_uplift.stage, build_compiler_for_std_to_uplift.host,
224                )
225            };
226
227            builder.info(&msg);
228
229            // Even if we're not building std this stage, the new sysroot must
230            // still contain the third party objects needed by various targets.
231            self.copy_extra_objects(builder, &build_compiler, target);
232
233            builder.ensure(StdLink::from_std(self, build_compiler_for_std_to_uplift));
234            return;
235        }
236
237        target_deps.extend(self.copy_extra_objects(builder, &build_compiler, target));
238
239        // We build a sysroot for mir-opt tests using the same trick that Miri does: A check build
240        // with -Zalways-encode-mir. This frees us from the need to have a target linker, and the
241        // fact that this is a check build integrates nicely with run_cargo.
242        let mut cargo = if self.is_for_mir_opt_tests {
243            trace!("building special sysroot for mir-opt tests");
244            let mut cargo = builder::Cargo::new_for_mir_opt_tests(
245                builder,
246                build_compiler,
247                Mode::Std,
248                SourceType::InTree,
249                target,
250                Kind::Check,
251            );
252            cargo.rustflag("-Zalways-encode-mir");
253            cargo.arg("--manifest-path").arg(builder.src.join("library/sysroot/Cargo.toml"));
254            cargo
255        } else {
256            trace!("building regular sysroot");
257            let mut cargo = builder::Cargo::new(
258                builder,
259                build_compiler,
260                Mode::Std,
261                SourceType::InTree,
262                target,
263                Kind::Build,
264            );
265            std_cargo(builder, target, &mut cargo);
266            for krate in &*self.crates {
267                cargo.arg("-p").arg(krate);
268            }
269            cargo
270        };
271
272        // See src/bootstrap/synthetic_targets.rs
273        if target.is_synthetic() {
274            cargo.env("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET", "1");
275        }
276        for rustflag in self.extra_rust_args.iter() {
277            cargo.rustflag(rustflag);
278        }
279
280        let _guard = builder.msg(
281            Kind::Build,
282            format_args!("library artifacts{}", crate_description(&self.crates)),
283            Mode::Std,
284            build_compiler,
285            target,
286        );
287        run_cargo(
288            builder,
289            cargo,
290            vec![],
291            &build_stamp::libstd_stamp(builder, build_compiler, target),
292            target_deps,
293            self.is_for_mir_opt_tests, // is_check
294            false,
295        );
296
297        builder.ensure(StdLink::from_std(
298            self,
299            builder.compiler(build_compiler.stage, builder.config.host_target),
300        ));
301    }
302
303    fn metadata(&self) -> Option<StepMetadata> {
304        Some(StepMetadata::build("std", self.target).built_by(self.build_compiler))
305    }
306}
307
308fn copy_and_stamp(
309    builder: &Builder<'_>,
310    libdir: &Path,
311    sourcedir: &Path,
312    name: &str,
313    target_deps: &mut Vec<(PathBuf, DependencyType)>,
314    dependency_type: DependencyType,
315) {
316    let target = libdir.join(name);
317    builder.copy_link(&sourcedir.join(name), &target, FileType::Regular);
318
319    target_deps.push((target, dependency_type));
320}
321
322fn copy_llvm_libunwind(builder: &Builder<'_>, target: TargetSelection, libdir: &Path) -> PathBuf {
323    let libunwind_path = builder.ensure(llvm::Libunwind { target });
324    let libunwind_source = libunwind_path.join("libunwind.a");
325    let libunwind_target = libdir.join("libunwind.a");
326    builder.copy_link(&libunwind_source, &libunwind_target, FileType::NativeLibrary);
327    libunwind_target
328}
329
330/// Copies third party objects needed by various targets.
331fn copy_third_party_objects(
332    builder: &Builder<'_>,
333    compiler: &Compiler,
334    target: TargetSelection,
335) -> Vec<(PathBuf, DependencyType)> {
336    let mut target_deps = vec![];
337
338    if builder.config.needs_sanitizer_runtime_built(target) && compiler.stage != 0 {
339        // The sanitizers are only copied in stage1 or above,
340        // to avoid creating dependency on LLVM.
341        target_deps.extend(
342            copy_sanitizers(builder, compiler, target)
343                .into_iter()
344                .map(|d| (d, DependencyType::Target)),
345        );
346    }
347
348    if target == "x86_64-fortanix-unknown-sgx"
349        || builder.config.llvm_libunwind(target) == LlvmLibunwind::InTree
350            && (target.contains("linux") || target.contains("fuchsia") || target.contains("aix"))
351    {
352        let libunwind_path =
353            copy_llvm_libunwind(builder, target, &builder.sysroot_target_libdir(*compiler, target));
354        target_deps.push((libunwind_path, DependencyType::Target));
355    }
356
357    target_deps
358}
359
360/// Copies third party objects needed by various targets for self-contained linkage.
361fn copy_self_contained_objects(
362    builder: &Builder<'_>,
363    compiler: &Compiler,
364    target: TargetSelection,
365) -> Vec<(PathBuf, DependencyType)> {
366    let libdir_self_contained =
367        builder.sysroot_target_libdir(*compiler, target).join("self-contained");
368    t!(fs::create_dir_all(&libdir_self_contained));
369    let mut target_deps = vec![];
370
371    // Copies the libc and CRT objects.
372    //
373    // rustc historically provides a more self-contained installation for musl targets
374    // not requiring the presence of a native musl toolchain. For example, it can fall back
375    // to using gcc from a glibc-targeting toolchain for linking.
376    // To do that we have to distribute musl startup objects as a part of Rust toolchain
377    // and link with them manually in the self-contained mode.
378    if target.needs_crt_begin_end() {
379        let srcdir = builder.musl_libdir(target).unwrap_or_else(|| {
380            panic!("Target {:?} does not have a \"musl-libdir\" key", target.triple)
381        });
382        if !target.starts_with("wasm32") {
383            for &obj in &["libc.a", "crt1.o", "Scrt1.o", "rcrt1.o", "crti.o", "crtn.o"] {
384                copy_and_stamp(
385                    builder,
386                    &libdir_self_contained,
387                    &srcdir,
388                    obj,
389                    &mut target_deps,
390                    DependencyType::TargetSelfContained,
391                );
392            }
393            let crt_path = builder.ensure(llvm::CrtBeginEnd { target });
394            for &obj in &["crtbegin.o", "crtbeginS.o", "crtend.o", "crtendS.o"] {
395                let src = crt_path.join(obj);
396                let target = libdir_self_contained.join(obj);
397                builder.copy_link(&src, &target, FileType::NativeLibrary);
398                target_deps.push((target, DependencyType::TargetSelfContained));
399            }
400        } else {
401            // For wasm32 targets, we need to copy the libc.a and crt1-command.o files from the
402            // musl-libdir, but we don't need the other files.
403            for &obj in &["libc.a", "crt1-command.o"] {
404                copy_and_stamp(
405                    builder,
406                    &libdir_self_contained,
407                    &srcdir,
408                    obj,
409                    &mut target_deps,
410                    DependencyType::TargetSelfContained,
411                );
412            }
413        }
414        if !target.starts_with("s390x") {
415            let libunwind_path = copy_llvm_libunwind(builder, target, &libdir_self_contained);
416            target_deps.push((libunwind_path, DependencyType::TargetSelfContained));
417        }
418    } else if target.contains("-wasi") {
419        let srcdir = builder.wasi_libdir(target).unwrap_or_else(|| {
420            panic!(
421                "Target {:?} does not have a \"wasi-root\" key in bootstrap.toml \
422                    or `$WASI_SDK_PATH` set",
423                target.triple
424            )
425        });
426        for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] {
427            copy_and_stamp(
428                builder,
429                &libdir_self_contained,
430                &srcdir,
431                obj,
432                &mut target_deps,
433                DependencyType::TargetSelfContained,
434            );
435        }
436    } else if target.is_windows_gnu() {
437        for obj in ["crt2.o", "dllcrt2.o"].iter() {
438            let src = compiler_file(builder, &builder.cc(target), target, CLang::C, obj);
439            let dst = libdir_self_contained.join(obj);
440            builder.copy_link(&src, &dst, FileType::NativeLibrary);
441            target_deps.push((dst, DependencyType::TargetSelfContained));
442        }
443    }
444
445    target_deps
446}
447
448/// Resolves standard library crates for `Std::run_make` for any build kind (like check, doc,
449/// build, clippy, etc.).
450pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec<String> {
451    let mut crates = run.make_run_crates(builder::Alias::Library);
452
453    // For no_std targets, we only want to check core and alloc
454    // Regardless of core/alloc being selected explicitly or via the "library" default alias,
455    // we only want to keep these two crates.
456    // The set of no_std crates should be kept in sync with what `Builder::std_cargo` does.
457    // Note: an alternative design would be to return an enum from this function (Default vs Subset)
458    // of crates. However, several steps currently pass `-p <package>` even if all crates are
459    // selected, because Cargo behaves differently in that case. To keep that behavior without
460    // making further changes, we pre-filter the no-std crates here.
461    let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);
462    if target_is_no_std {
463        crates.retain(|c| c == "core" || c == "alloc");
464    }
465    crates
466}
467
468/// Tries to find LLVM's `compiler-rt` source directory, for building `library/profiler_builtins`.
469///
470/// Normally it lives in the `src/llvm-project` submodule, but if we will be using a
471/// downloaded copy of CI LLVM, then we try to use the `compiler-rt` sources from
472/// there instead, which lets us avoid checking out the LLVM submodule.
473fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
474    // Try to use `compiler-rt` sources from downloaded CI LLVM, if possible.
475    if builder.config.llvm_from_ci {
476        // CI LLVM might not have been downloaded yet, so try to download it now.
477        builder.config.maybe_download_ci_llvm();
478        let ci_llvm_compiler_rt = builder.config.ci_llvm_root().join("compiler-rt");
479        if ci_llvm_compiler_rt.exists() {
480            return ci_llvm_compiler_rt;
481        }
482    }
483
484    // Otherwise, fall back to requiring the LLVM submodule.
485    builder.require_submodule("src/llvm-project", {
486        Some("The `build.profiler` config option requires `compiler-rt` sources from LLVM.")
487    });
488    builder.src.join("src/llvm-project/compiler-rt")
489}
490
491/// Configure cargo to compile the standard library, adding appropriate env vars
492/// and such.
493pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, cargo: &mut Cargo) {
494    // rustc already ensures that it builds with the minimum deployment
495    // target, so ideally we shouldn't need to do anything here.
496    //
497    // However, `cc` currently defaults to a higher version for backwards
498    // compatibility, which means that compiler-rt, which is built via
499    // compiler-builtins' build script, gets built with a higher deployment
500    // target. This in turn causes warnings while linking, and is generally
501    // a compatibility hazard.
502    //
503    // So, at least until https://github.com/rust-lang/cc-rs/issues/1171, or
504    // perhaps https://github.com/rust-lang/cargo/issues/13115 is resolved, we
505    // explicitly set the deployment target environment variables to avoid
506    // this issue.
507    //
508    // This place also serves as an extension point if we ever wanted to raise
509    // rustc's default deployment target while keeping the prebuilt `std` at
510    // a lower version, so it's kinda nice to have in any case.
511    if target.contains("apple") && !builder.config.dry_run() {
512        // Query rustc for the deployment target, and the associated env var.
513        // The env var is one of the standard `*_DEPLOYMENT_TARGET` vars, i.e.
514        // `MACOSX_DEPLOYMENT_TARGET`, `IPHONEOS_DEPLOYMENT_TARGET`, etc.
515        let mut cmd = command(builder.rustc(cargo.compiler()));
516        cmd.arg("--target").arg(target.rustc_target_arg());
517        cmd.arg("--print=deployment-target");
518        let output = cmd.run_capture_stdout(builder).stdout();
519
520        let (env_var, value) = output.split_once('=').unwrap();
521        // Unconditionally set the env var (if it was set in the environment
522        // already, rustc should've picked that up).
523        cargo.env(env_var.trim(), value.trim());
524
525        // Allow CI to override the deployment target for `std` on macOS.
526        //
527        // This is useful because we might want the host tooling LLVM, `rustc`
528        // and Cargo to have a different deployment target than `std` itself
529        // (currently, these two versions are the same, but in the past, we
530        // supported macOS 10.7 for user code and macOS 10.8 in host tooling).
531        //
532        // It is not necessary on the other platforms, since only macOS has
533        // support for host tooling.
534        if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
535            cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
536        }
537    }
538
539    // Paths needed by `library/profiler_builtins/build.rs`.
540    if let Some(path) = builder.config.profiler_path(target) {
541        cargo.env("LLVM_PROFILER_RT_LIB", path);
542    } else if builder.config.profiler_enabled(target) {
543        let compiler_rt = compiler_rt_for_profiler(builder);
544        // Currently this is separate from the env var used by `compiler_builtins`
545        // (below) so that adding support for CI LLVM here doesn't risk breaking
546        // the compiler builtins. But they could be unified if desired.
547        cargo.env("RUST_COMPILER_RT_FOR_PROFILER", compiler_rt);
548    }
549
550    // Determine if we're going to compile in optimized C intrinsics to
551    // the `compiler-builtins` crate. These intrinsics live in LLVM's
552    // `compiler-rt` repository.
553    //
554    // Note that this shouldn't affect the correctness of `compiler-builtins`,
555    // but only its speed. Some intrinsics in C haven't been translated to Rust
556    // yet but that's pretty rare. Other intrinsics have optimized
557    // implementations in C which have only had slower versions ported to Rust,
558    // so we favor the C version where we can, but it's not critical.
559    //
560    // If `compiler-rt` is available ensure that the `c` feature of the
561    // `compiler-builtins` crate is enabled and it's configured to learn where
562    // `compiler-rt` is located.
563    let compiler_builtins_c_feature = if builder.config.optimized_compiler_builtins(target) {
564        // NOTE: this interacts strangely with `llvm-has-rust-patches`. In that case, we enforce `submodules = false`, so this is a no-op.
565        // But, the user could still decide to manually use an in-tree submodule.
566        //
567        // NOTE: if we're using system llvm, we'll end up building a version of `compiler-rt` that doesn't match the LLVM we're linking to.
568        // That's probably ok? At least, the difference wasn't enforced before. There's a comment in
569        // the compiler_builtins build script that makes me nervous, though:
570        // https://github.com/rust-lang/compiler-builtins/blob/31ee4544dbe47903ce771270d6e3bea8654e9e50/build.rs#L575-L579
571        builder.require_submodule(
572            "src/llvm-project",
573            Some(
574                "The `build.optimized-compiler-builtins` config option \
575                 requires `compiler-rt` sources from LLVM.",
576            ),
577        );
578        let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
579        assert!(compiler_builtins_root.exists());
580        // The path to `compiler-rt` is also used by `profiler_builtins` (above),
581        // so if you're changing something here please also change that as appropriate.
582        cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);
583        " compiler-builtins-c"
584    } else {
585        ""
586    };
587
588    // `libtest` uses this to know whether or not to support
589    // `-Zunstable-options`.
590    if !builder.unstable_features() {
591        cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
592    }
593
594    let mut features = String::new();
595
596    if builder.no_std(target) == Some(true) {
597        features += " compiler-builtins-mem";
598        if !target.starts_with("bpf") {
599            features.push_str(compiler_builtins_c_feature);
600        }
601
602        // for no-std targets we only compile a few no_std crates
603        cargo
604            .args(["-p", "alloc"])
605            .arg("--manifest-path")
606            .arg(builder.src.join("library/alloc/Cargo.toml"))
607            .arg("--features")
608            .arg(features);
609    } else {
610        features += &builder.std_features(target);
611        features.push_str(compiler_builtins_c_feature);
612
613        cargo
614            .arg("--features")
615            .arg(features)
616            .arg("--manifest-path")
617            .arg(builder.src.join("library/sysroot/Cargo.toml"));
618
619        // Help the libc crate compile by assisting it in finding various
620        // sysroot native libraries.
621        if target.contains("musl")
622            && let Some(p) = builder.musl_libdir(target)
623        {
624            let root = format!("native={}", p.to_str().unwrap());
625            cargo.rustflag("-L").rustflag(&root);
626        }
627
628        if target.contains("-wasi")
629            && let Some(dir) = builder.wasi_libdir(target)
630        {
631            let root = format!("native={}", dir.to_str().unwrap());
632            cargo.rustflag("-L").rustflag(&root);
633        }
634    }
635
636    // By default, rustc uses `-Cembed-bitcode=yes`, and Cargo overrides that
637    // with `-Cembed-bitcode=no` for non-LTO builds. However, libstd must be
638    // built with bitcode so that the produced rlibs can be used for both LTO
639    // builds (which use bitcode) and non-LTO builds (which use object code).
640    // So we override the override here!
641    cargo.rustflag("-Cembed-bitcode=yes");
642
643    if builder.config.rust_lto == RustcLto::Off {
644        cargo.rustflag("-Clto=off");
645    }
646
647    // By default, rustc does not include unwind tables unless they are required
648    // for a particular target. They are not required by RISC-V targets, but
649    // compiling the standard library with them means that users can get
650    // backtraces without having to recompile the standard library themselves.
651    //
652    // This choice was discussed in https://github.com/rust-lang/rust/pull/69890
653    if target.contains("riscv") {
654        cargo.rustflag("-Cforce-unwind-tables=yes");
655    }
656
657    // Enable frame pointers by default for the library. Note that they are still controlled by a
658    // separate setting for the compiler.
659    cargo.rustflag("-Zunstable-options");
660    cargo.rustflag("-Cforce-frame-pointers=non-leaf");
661
662    let html_root =
663        format!("-Zcrate-attr=doc(html_root_url=\"{}/\")", builder.doc_rust_lang_org_channel(),);
664    cargo.rustflag(&html_root);
665    cargo.rustdocflag(&html_root);
666
667    cargo.rustdocflag("-Zcrate-attr=warn(rust_2018_idioms)");
668}
669
670/// Link all libstd rlibs/dylibs into a sysroot of `target_compiler`.
671///
672/// Links those artifacts generated by `compiler` to the `stage` compiler's
673/// sysroot for the specified `host` and `target`.
674///
675/// Note that this assumes that `compiler` has already generated the libstd
676/// libraries for `target`, and this method will find them in the relevant
677/// output directory.
678#[derive(Debug, Clone, PartialEq, Eq, Hash)]
679pub struct StdLink {
680    pub compiler: Compiler,
681    pub target_compiler: Compiler,
682    pub target: TargetSelection,
683    /// Not actually used; only present to make sure the cache invalidation is correct.
684    crates: Vec<String>,
685    /// See [`Std::force_recompile`].
686    force_recompile: bool,
687}
688
689impl StdLink {
690    pub fn from_std(std: Std, host_compiler: Compiler) -> Self {
691        Self {
692            compiler: host_compiler,
693            target_compiler: std.build_compiler,
694            target: std.target,
695            crates: std.crates,
696            force_recompile: std.force_recompile,
697        }
698    }
699}
700
701impl Step for StdLink {
702    type Output = ();
703
704    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
705        run.never()
706    }
707
708    /// Link all libstd rlibs/dylibs into the sysroot location.
709    ///
710    /// Links those artifacts generated by `compiler` to the `stage` compiler's
711    /// sysroot for the specified `host` and `target`.
712    ///
713    /// Note that this assumes that `compiler` has already generated the libstd
714    /// libraries for `target`, and this method will find them in the relevant
715    /// output directory.
716    fn run(self, builder: &Builder<'_>) {
717        let compiler = self.compiler;
718        let target_compiler = self.target_compiler;
719        let target = self.target;
720
721        // NOTE: intentionally does *not* check `target == builder.build` to avoid having to add the same check in `test::Crate`.
722        let (libdir, hostdir) = if !self.force_recompile && builder.download_rustc() {
723            // NOTE: copies part of `sysroot_libdir` to avoid having to add a new `force_recompile` argument there too
724            let lib = builder.sysroot_libdir_relative(self.compiler);
725            let sysroot = builder.ensure(crate::core::build_steps::compile::Sysroot {
726                compiler: self.compiler,
727                force_recompile: self.force_recompile,
728            });
729            let libdir = sysroot.join(lib).join("rustlib").join(target).join("lib");
730            let hostdir = sysroot.join(lib).join("rustlib").join(compiler.host).join("lib");
731            (libdir, hostdir)
732        } else {
733            let libdir = builder.sysroot_target_libdir(target_compiler, target);
734            let hostdir = builder.sysroot_target_libdir(target_compiler, compiler.host);
735            (libdir, hostdir)
736        };
737
738        let is_downloaded_beta_stage0 = builder
739            .build
740            .config
741            .initial_rustc
742            .starts_with(builder.out.join(compiler.host).join("stage0/bin"));
743
744        // Special case for stage0, to make `rustup toolchain link` and `x dist --stage 0`
745        // work for stage0-sysroot. We only do this if the stage0 compiler comes from beta,
746        // and is not set to a custom path.
747        if compiler.stage == 0 && is_downloaded_beta_stage0 {
748            // Copy bin files from stage0/bin to stage0-sysroot/bin
749            let sysroot = builder.out.join(compiler.host).join("stage0-sysroot");
750
751            let host = compiler.host;
752            let stage0_bin_dir = builder.out.join(host).join("stage0/bin");
753            let sysroot_bin_dir = sysroot.join("bin");
754            t!(fs::create_dir_all(&sysroot_bin_dir));
755            builder.cp_link_r(&stage0_bin_dir, &sysroot_bin_dir);
756
757            let stage0_lib_dir = builder.out.join(host).join("stage0/lib");
758            t!(fs::create_dir_all(sysroot.join("lib")));
759            builder.cp_link_r(&stage0_lib_dir, &sysroot.join("lib"));
760
761            // Copy codegen-backends from stage0
762            let sysroot_codegen_backends = builder.sysroot_codegen_backends(compiler);
763            t!(fs::create_dir_all(&sysroot_codegen_backends));
764            let stage0_codegen_backends = builder
765                .out
766                .join(host)
767                .join("stage0/lib/rustlib")
768                .join(host)
769                .join("codegen-backends");
770            if stage0_codegen_backends.exists() {
771                builder.cp_link_r(&stage0_codegen_backends, &sysroot_codegen_backends);
772            }
773        } else if compiler.stage == 0 {
774            let sysroot = builder.out.join(compiler.host.triple).join("stage0-sysroot");
775
776            if builder.local_rebuild {
777                // On local rebuilds this path might be a symlink to the project root,
778                // which can be read-only (e.g., on CI). So remove it before copying
779                // the stage0 lib.
780                let _ = fs::remove_dir_all(sysroot.join("lib/rustlib/src/rust"));
781            }
782
783            builder.cp_link_r(&builder.initial_sysroot.join("lib"), &sysroot.join("lib"));
784        } else {
785            if builder.download_rustc() {
786                // Ensure there are no CI-rustc std artifacts.
787                let _ = fs::remove_dir_all(&libdir);
788                let _ = fs::remove_dir_all(&hostdir);
789            }
790
791            add_to_sysroot(
792                builder,
793                &libdir,
794                &hostdir,
795                &build_stamp::libstd_stamp(builder, compiler, target),
796            );
797        }
798    }
799}
800
801/// Copies sanitizer runtime libraries into target libdir.
802fn copy_sanitizers(
803    builder: &Builder<'_>,
804    compiler: &Compiler,
805    target: TargetSelection,
806) -> Vec<PathBuf> {
807    let runtimes: Vec<llvm::SanitizerRuntime> = builder.ensure(llvm::Sanitizers { target });
808
809    if builder.config.dry_run() {
810        return Vec::new();
811    }
812
813    let mut target_deps = Vec::new();
814    let libdir = builder.sysroot_target_libdir(*compiler, target);
815
816    for runtime in &runtimes {
817        let dst = libdir.join(&runtime.name);
818        builder.copy_link(&runtime.path, &dst, FileType::NativeLibrary);
819
820        // The `aarch64-apple-ios-macabi` and `x86_64-apple-ios-macabi` are also supported for
821        // sanitizers, but they share a sanitizer runtime with `${arch}-apple-darwin`, so we do
822        // not list them here to rename and sign the runtime library.
823        if target == "x86_64-apple-darwin"
824            || target == "aarch64-apple-darwin"
825            || target == "aarch64-apple-ios"
826            || target == "aarch64-apple-ios-sim"
827            || target == "x86_64-apple-ios"
828        {
829            // Update the library’s install name to reflect that it has been renamed.
830            apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", runtime.name));
831            // Upon renaming the install name, the code signature of the file will invalidate,
832            // so we will sign it again.
833            apple_darwin_sign_file(builder, &dst);
834        }
835
836        target_deps.push(dst);
837    }
838
839    target_deps
840}
841
842fn apple_darwin_update_library_name(builder: &Builder<'_>, library_path: &Path, new_name: &str) {
843    command("install_name_tool").arg("-id").arg(new_name).arg(library_path).run(builder);
844}
845
846fn apple_darwin_sign_file(builder: &Builder<'_>, file_path: &Path) {
847    command("codesign")
848        .arg("-f") // Force to rewrite the existing signature
849        .arg("-s")
850        .arg("-")
851        .arg(file_path)
852        .run(builder);
853}
854
855#[derive(Debug, Clone, PartialEq, Eq, Hash)]
856pub struct StartupObjects {
857    pub compiler: Compiler,
858    pub target: TargetSelection,
859}
860
861impl Step for StartupObjects {
862    type Output = Vec<(PathBuf, DependencyType)>;
863
864    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
865        run.path("library/rtstartup")
866    }
867
868    fn make_run(run: RunConfig<'_>) {
869        run.builder.ensure(StartupObjects {
870            compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
871            target: run.target,
872        });
873    }
874
875    /// Builds and prepare startup objects like rsbegin.o and rsend.o
876    ///
877    /// These are primarily used on Windows right now for linking executables/dlls.
878    /// They don't require any library support as they're just plain old object
879    /// files, so we just use the nightly snapshot compiler to always build them (as
880    /// no other compilers are guaranteed to be available).
881    fn run(self, builder: &Builder<'_>) -> Vec<(PathBuf, DependencyType)> {
882        let for_compiler = self.compiler;
883        let target = self.target;
884        if !target.is_windows_gnu() {
885            return vec![];
886        }
887
888        let mut target_deps = vec![];
889
890        let src_dir = &builder.src.join("library").join("rtstartup");
891        let dst_dir = &builder.native_dir(target).join("rtstartup");
892        let sysroot_dir = &builder.sysroot_target_libdir(for_compiler, target);
893        t!(fs::create_dir_all(dst_dir));
894
895        for file in &["rsbegin", "rsend"] {
896            let src_file = &src_dir.join(file.to_string() + ".rs");
897            let dst_file = &dst_dir.join(file.to_string() + ".o");
898            if !up_to_date(src_file, dst_file) {
899                let mut cmd = command(&builder.initial_rustc);
900                cmd.env("RUSTC_BOOTSTRAP", "1");
901                if !builder.local_rebuild {
902                    // a local_rebuild compiler already has stage1 features
903                    cmd.arg("--cfg").arg("bootstrap");
904                }
905                cmd.arg("--target")
906                    .arg(target.rustc_target_arg())
907                    .arg("--emit=obj")
908                    .arg("-o")
909                    .arg(dst_file)
910                    .arg(src_file)
911                    .run(builder);
912            }
913
914            let obj = sysroot_dir.join((*file).to_string() + ".o");
915            builder.copy_link(dst_file, &obj, FileType::NativeLibrary);
916            target_deps.push((obj, DependencyType::Target));
917        }
918
919        target_deps
920    }
921}
922
923fn cp_rustc_component_to_ci_sysroot(builder: &Builder<'_>, sysroot: &Path, contents: Vec<String>) {
924    let ci_rustc_dir = builder.config.ci_rustc_dir();
925
926    for file in contents {
927        let src = ci_rustc_dir.join(&file);
928        let dst = sysroot.join(file);
929        if src.is_dir() {
930            t!(fs::create_dir_all(dst));
931        } else {
932            builder.copy_link(&src, &dst, FileType::Regular);
933        }
934    }
935}
936
937/// Represents information about a built rustc.
938#[derive(Clone, Debug)]
939pub struct BuiltRustc {
940    /// The compiler that actually built this *rustc*.
941    /// This can be different from the *build_compiler* passed to the `Rustc` step because of
942    /// uplifting.
943    pub build_compiler: Compiler,
944}
945
946/// Build rustc using the passed `build_compiler`.
947///
948/// - Makes sure that `build_compiler` has a standard library prepared for its host target,
949///   so that it can compile build scripts and proc macros when building this `rustc`.
950/// - Makes sure that `build_compiler` has a standard library prepared for `target`,
951///   so that the built `rustc` can *link to it* and use it at runtime.
952#[derive(Debug, PartialOrd, Ord, Clone, PartialEq, Eq, Hash)]
953pub struct Rustc {
954    /// The target on which rustc will run (its host).
955    pub target: TargetSelection,
956    /// The **previous** compiler used to compile this rustc.
957    pub build_compiler: Compiler,
958    /// Whether to build a subset of crates, rather than the whole compiler.
959    ///
960    /// This should only be requested by the user, not used within bootstrap itself.
961    /// Using it within bootstrap can lead to confusing situation where lints are replayed
962    /// in two different steps.
963    crates: Vec<String>,
964}
965
966impl Rustc {
967    pub fn new(build_compiler: Compiler, target: TargetSelection) -> Self {
968        Self { target, build_compiler, crates: Default::default() }
969    }
970}
971
972impl Step for Rustc {
973    type Output = BuiltRustc;
974
975    const IS_HOST: bool = true;
976    const DEFAULT: bool = false;
977
978    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
979        let mut crates = run.builder.in_tree_crates("rustc-main", None);
980        for (i, krate) in crates.iter().enumerate() {
981            // We can't allow `build rustc` as an alias for this Step, because that's reserved by `Assemble`.
982            // Ideally Assemble would use `build compiler` instead, but that seems too confusing to be worth the breaking change.
983            if krate.name == "rustc-main" {
984                crates.swap_remove(i);
985                break;
986            }
987        }
988        run.crates(crates)
989    }
990
991    fn make_run(run: RunConfig<'_>) {
992        // If only `compiler` was passed, do not run this step.
993        // Instead the `Assemble` step will take care of compiling Rustc.
994        if run.builder.paths == vec![PathBuf::from("compiler")] {
995            return;
996        }
997
998        let crates = run.cargo_crates_in_set();
999        run.builder.ensure(Rustc {
1000            build_compiler: run
1001                .builder
1002                .compiler(run.builder.top_stage.saturating_sub(1), run.build_triple()),
1003            target: run.target,
1004            crates,
1005        });
1006    }
1007
1008    /// Builds the compiler.
1009    ///
1010    /// This will build the compiler for a particular stage of the build using
1011    /// the `build_compiler` targeting the `target` architecture. The artifacts
1012    /// created will also be linked into the sysroot directory.
1013    fn run(self, builder: &Builder<'_>) -> Self::Output {
1014        let build_compiler = self.build_compiler;
1015        let target = self.target;
1016
1017        // NOTE: the ABI of the stage0 compiler is different from the ABI of the downloaded compiler,
1018        // so its artifacts can't be reused.
1019        if builder.download_rustc() && build_compiler.stage != 0 {
1020            trace!(stage = build_compiler.stage, "`download_rustc` requested");
1021
1022            let sysroot =
1023                builder.ensure(Sysroot { compiler: build_compiler, force_recompile: false });
1024            cp_rustc_component_to_ci_sysroot(
1025                builder,
1026                &sysroot,
1027                builder.config.ci_rustc_dev_contents(),
1028            );
1029            return BuiltRustc { build_compiler };
1030        }
1031
1032        // Build a standard library for `target` using the `build_compiler`.
1033        // This will be the standard library that the rustc which we build *links to*.
1034        builder.std(build_compiler, target);
1035
1036        if builder.config.keep_stage.contains(&build_compiler.stage) {
1037            trace!(stage = build_compiler.stage, "`keep-stage` requested");
1038
1039            builder.info("WARNING: Using a potentially old librustc. This may not behave well.");
1040            builder.info("WARNING: Use `--keep-stage-std` if you want to rebuild the compiler when it changes");
1041            builder.ensure(RustcLink::from_rustc(self));
1042
1043            return BuiltRustc { build_compiler };
1044        }
1045
1046        // The stage of the compiler that we're building
1047        let stage = build_compiler.stage + 1;
1048
1049        // If we are building a stage3+ compiler, and full bootstrap is disabled, and we have a
1050        // previous rustc available, we will uplift a compiler from a previous stage.
1051        if build_compiler.stage >= 2
1052            && !builder.config.full_bootstrap
1053            && (target == builder.host_target || builder.hosts.contains(&target))
1054        {
1055            // Here we need to determine the **build compiler** that built the stage that we will
1056            // be uplifting. We cannot uplift stage 1, as it has a different ABI than stage 2+,
1057            // so we always uplift the stage2 compiler (compiled with stage 1).
1058            let uplift_build_compiler = builder.compiler(1, build_compiler.host);
1059            let msg = if uplift_build_compiler.host == target {
1060                format!("Uplifting rustc (stage2 -> stage{stage})")
1061            } else {
1062                format!(
1063                    "Uplifting rustc (stage2:{} -> stage{stage}:{target})",
1064                    uplift_build_compiler.host
1065                )
1066            };
1067            builder.info(&msg);
1068
1069            // Here the compiler that built the rlibs (`uplift_build_compiler`) can be different
1070            // from the compiler whose sysroot should be modified in this step. So we need to copy
1071            // the (previously built) rlibs into the correct sysroot.
1072            builder.ensure(RustcLink::from_build_compiler_and_sysroot(
1073                // This is the compiler that actually built the rustc rlibs
1074                uplift_build_compiler,
1075                // We copy the rlibs into the sysroot of `build_compiler`
1076                build_compiler,
1077                target,
1078                self.crates,
1079            ));
1080
1081            // Here we have performed an uplift, so we return the actual build compiler that "built"
1082            // this rustc.
1083            return BuiltRustc { build_compiler: uplift_build_compiler };
1084        }
1085
1086        // Build a standard library for the current host target using the `build_compiler`.
1087        // This standard library will be used when building `rustc` for compiling
1088        // build scripts and proc macros.
1089        // If we are not cross-compiling, the Std build above will be the same one as the one we
1090        // prepare here.
1091        builder.std(
1092            builder.compiler(self.build_compiler.stage, builder.config.host_target),
1093            builder.config.host_target,
1094        );
1095
1096        let mut cargo = builder::Cargo::new(
1097            builder,
1098            build_compiler,
1099            Mode::Rustc,
1100            SourceType::InTree,
1101            target,
1102            Kind::Build,
1103        );
1104
1105        rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates);
1106
1107        // NB: all RUSTFLAGS should be added to `rustc_cargo()` so they will be
1108        // consistently applied by check/doc/test modes too.
1109
1110        for krate in &*self.crates {
1111            cargo.arg("-p").arg(krate);
1112        }
1113
1114        if builder.build.config.enable_bolt_settings && build_compiler.stage == 1 {
1115            // Relocations are required for BOLT to work.
1116            cargo.env("RUSTC_BOLT_LINK_FLAGS", "1");
1117        }
1118
1119        let _guard = builder.msg(
1120            Kind::Build,
1121            format_args!("compiler artifacts{}", crate_description(&self.crates)),
1122            Mode::Rustc,
1123            build_compiler,
1124            target,
1125        );
1126        let stamp = build_stamp::librustc_stamp(builder, build_compiler, target);
1127        run_cargo(
1128            builder,
1129            cargo,
1130            vec![],
1131            &stamp,
1132            vec![],
1133            false,
1134            true, // Only ship rustc_driver.so and .rmeta files, not all intermediate .rlib files.
1135        );
1136
1137        let target_root_dir = stamp.path().parent().unwrap();
1138        // When building `librustc_driver.so` (like `libLLVM.so`) on linux, it can contain
1139        // unexpected debuginfo from dependencies, for example from the C++ standard library used in
1140        // our LLVM wrapper. Unless we're explicitly requesting `librustc_driver` to be built with
1141        // debuginfo (via the debuginfo level of the executables using it): strip this debuginfo
1142        // away after the fact.
1143        if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None
1144            && builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None
1145        {
1146            let rustc_driver = target_root_dir.join("librustc_driver.so");
1147            strip_debug(builder, target, &rustc_driver);
1148        }
1149
1150        if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None {
1151            // Due to LTO a lot of debug info from C++ dependencies such as jemalloc can make it into
1152            // our final binaries
1153            strip_debug(builder, target, &target_root_dir.join("rustc-main"));
1154        }
1155
1156        builder.ensure(RustcLink::from_rustc(self));
1157        BuiltRustc { build_compiler }
1158    }
1159
1160    fn metadata(&self) -> Option<StepMetadata> {
1161        Some(StepMetadata::build("rustc", self.target).built_by(self.build_compiler))
1162    }
1163}
1164
1165pub fn rustc_cargo(
1166    builder: &Builder<'_>,
1167    cargo: &mut Cargo,
1168    target: TargetSelection,
1169    build_compiler: &Compiler,
1170    crates: &[String],
1171) {
1172    cargo
1173        .arg("--features")
1174        .arg(builder.rustc_features(builder.kind, target, crates))
1175        .arg("--manifest-path")
1176        .arg(builder.src.join("compiler/rustc/Cargo.toml"));
1177
1178    cargo.rustdocflag("-Zcrate-attr=warn(rust_2018_idioms)");
1179
1180    // If the rustc output is piped to e.g. `head -n1` we want the process to be killed, rather than
1181    // having an error bubble up and cause a panic.
1182    //
1183    // FIXME(jieyouxu): this flag is load-bearing for rustc to not ICE on broken pipes, because
1184    // rustc internally sometimes uses std `println!` -- but std `println!` by default will panic on
1185    // broken pipes, and uncaught panics will manifest as an ICE. The compiler *should* handle this
1186    // properly, but this flag is set in the meantime to paper over the I/O errors.
1187    //
1188    // See <https://github.com/rust-lang/rust/issues/131059> for details.
1189    //
1190    // Also see the discussion for properly handling I/O errors related to broken pipes, i.e. safe
1191    // variants of `println!` in
1192    // <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Internal.20lint.20for.20raw.20.60print!.60.20and.20.60println!.60.3F>.
1193    cargo.rustflag("-Zon-broken-pipe=kill");
1194
1195    // We want to link against registerEnzyme and in the future we want to use additional
1196    // functionality from Enzyme core. For that we need to link against Enzyme.
1197    if builder.config.llvm_enzyme {
1198        let arch = builder.build.host_target;
1199        let enzyme_dir = builder.build.out.join(arch).join("enzyme").join("lib");
1200        cargo.rustflag("-L").rustflag(enzyme_dir.to_str().expect("Invalid path"));
1201
1202        if let Some(llvm_config) = builder.llvm_config(builder.config.host_target) {
1203            let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
1204            cargo.rustflag("-l").rustflag(&format!("Enzyme-{llvm_version_major}"));
1205        }
1206    }
1207
1208    // Building with protected visibility reduces the number of dynamic relocations needed, giving
1209    // us a faster startup time. However GNU ld < 2.40 will error if we try to link a shared object
1210    // with direct references to protected symbols, so for now we only use protected symbols if
1211    // linking with LLD is enabled.
1212    if builder.build.config.lld_mode.is_used() {
1213        cargo.rustflag("-Zdefault-visibility=protected");
1214    }
1215
1216    if is_lto_stage(build_compiler) {
1217        match builder.config.rust_lto {
1218            RustcLto::Thin | RustcLto::Fat => {
1219                // Since using LTO for optimizing dylibs is currently experimental,
1220                // we need to pass -Zdylib-lto.
1221                cargo.rustflag("-Zdylib-lto");
1222                // Cargo by default passes `-Cembed-bitcode=no` and doesn't pass `-Clto` when
1223                // compiling dylibs (and their dependencies), even when LTO is enabled for the
1224                // crate. Therefore, we need to override `-Clto` and `-Cembed-bitcode` here.
1225                let lto_type = match builder.config.rust_lto {
1226                    RustcLto::Thin => "thin",
1227                    RustcLto::Fat => "fat",
1228                    _ => unreachable!(),
1229                };
1230                cargo.rustflag(&format!("-Clto={lto_type}"));
1231                cargo.rustflag("-Cembed-bitcode=yes");
1232            }
1233            RustcLto::ThinLocal => { /* Do nothing, this is the default */ }
1234            RustcLto::Off => {
1235                cargo.rustflag("-Clto=off");
1236            }
1237        }
1238    } else if builder.config.rust_lto == RustcLto::Off {
1239        cargo.rustflag("-Clto=off");
1240    }
1241
1242    // With LLD, we can use ICF (identical code folding) to reduce the executable size
1243    // of librustc_driver/rustc and to improve i-cache utilization.
1244    //
1245    // -Wl,[link options] doesn't work on MSVC. However, /OPT:ICF (technically /OPT:REF,ICF)
1246    // is already on by default in MSVC optimized builds, which is interpreted as --icf=all:
1247    // https://github.com/llvm/llvm-project/blob/3329cec2f79185bafd678f310fafadba2a8c76d2/lld/COFF/Driver.cpp#L1746
1248    // https://github.com/rust-lang/rust/blob/f22819bcce4abaff7d1246a56eec493418f9f4ee/compiler/rustc_codegen_ssa/src/back/linker.rs#L827
1249    if builder.config.lld_mode.is_used() && !build_compiler.host.is_msvc() {
1250        cargo.rustflag("-Clink-args=-Wl,--icf=all");
1251    }
1252
1253    if builder.config.rust_profile_use.is_some() && builder.config.rust_profile_generate.is_some() {
1254        panic!("Cannot use and generate PGO profiles at the same time");
1255    }
1256    let is_collecting = if let Some(path) = &builder.config.rust_profile_generate {
1257        if build_compiler.stage == 1 {
1258            cargo.rustflag(&format!("-Cprofile-generate={path}"));
1259            // Apparently necessary to avoid overflowing the counters during
1260            // a Cargo build profile
1261            cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4");
1262            true
1263        } else {
1264            false
1265        }
1266    } else if let Some(path) = &builder.config.rust_profile_use {
1267        if build_compiler.stage == 1 {
1268            cargo.rustflag(&format!("-Cprofile-use={path}"));
1269            if builder.is_verbose() {
1270                cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function");
1271            }
1272            true
1273        } else {
1274            false
1275        }
1276    } else {
1277        false
1278    };
1279    if is_collecting {
1280        // Ensure paths to Rust sources are relative, not absolute.
1281        cargo.rustflag(&format!(
1282            "-Cllvm-args=-static-func-strip-dirname-prefix={}",
1283            builder.config.src.components().count()
1284        ));
1285    }
1286
1287    // The stage0 compiler changes infrequently and does not directly depend on code
1288    // in the current working directory. Therefore, caching it with sccache should be
1289    // useful.
1290    // This is only performed for non-incremental builds, as ccache cannot deal with these.
1291    if let Some(ref ccache) = builder.config.ccache
1292        && build_compiler.stage == 0
1293        && !builder.config.incremental
1294    {
1295        cargo.env("RUSTC_WRAPPER", ccache);
1296    }
1297
1298    rustc_cargo_env(builder, cargo, target);
1299}
1300
1301pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1302    // Set some configuration variables picked up by build scripts and
1303    // the compiler alike
1304    cargo
1305        .env("CFG_RELEASE", builder.rust_release())
1306        .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
1307        .env("CFG_VERSION", builder.rust_version());
1308
1309    // Some tools like Cargo detect their own git information in build scripts. When omit-git-hash
1310    // is enabled in bootstrap.toml, we pass this environment variable to tell build scripts to avoid
1311    // detecting git information on their own.
1312    if builder.config.omit_git_hash {
1313        cargo.env("CFG_OMIT_GIT_HASH", "1");
1314    }
1315
1316    if let Some(backend) = builder.config.default_codegen_backend(target) {
1317        cargo.env("CFG_DEFAULT_CODEGEN_BACKEND", backend.name());
1318    }
1319
1320    let libdir_relative = builder.config.libdir_relative().unwrap_or_else(|| Path::new("lib"));
1321    let target_config = builder.config.target_config.get(&target);
1322
1323    cargo.env("CFG_LIBDIR_RELATIVE", libdir_relative);
1324
1325    if let Some(ref ver_date) = builder.rust_info().commit_date() {
1326        cargo.env("CFG_VER_DATE", ver_date);
1327    }
1328    if let Some(ref ver_hash) = builder.rust_info().sha() {
1329        cargo.env("CFG_VER_HASH", ver_hash);
1330    }
1331    if !builder.unstable_features() {
1332        cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
1333    }
1334
1335    // Prefer the current target's own default_linker, else a globally
1336    // specified one.
1337    if let Some(s) = target_config.and_then(|c| c.default_linker.as_ref()) {
1338        cargo.env("CFG_DEFAULT_LINKER", s);
1339    } else if let Some(ref s) = builder.config.rustc_default_linker {
1340        cargo.env("CFG_DEFAULT_LINKER", s);
1341    }
1342
1343    // Enable rustc's env var for `rust-lld` when requested.
1344    if builder.config.lld_enabled {
1345        cargo.env("CFG_USE_SELF_CONTAINED_LINKER", "1");
1346    }
1347
1348    if builder.config.rust_verify_llvm_ir {
1349        cargo.env("RUSTC_VERIFY_LLVM_IR", "1");
1350    }
1351
1352    if builder.config.llvm_enzyme {
1353        cargo.rustflag("--cfg=llvm_enzyme");
1354    }
1355
1356    // These conditionals represent a tension between three forces:
1357    // - For non-check builds, we need to define some LLVM-related environment
1358    //   variables, requiring LLVM to have been built.
1359    // - For check builds, we want to avoid building LLVM if possible.
1360    // - Check builds and non-check builds should have the same environment if
1361    //   possible, to avoid unnecessary rebuilds due to cache-busting.
1362    //
1363    // Therefore we try to avoid building LLVM for check builds, but only if
1364    // building LLVM would be expensive. If "building" LLVM is cheap
1365    // (i.e. it's already built or is downloadable), we prefer to maintain a
1366    // consistent environment between check and non-check builds.
1367    if builder.config.llvm_enabled(target) {
1368        let building_llvm_is_expensive =
1369            crate::core::build_steps::llvm::prebuilt_llvm_config(builder, target, false)
1370                .should_build();
1371
1372        let skip_llvm = (builder.kind == Kind::Check) && building_llvm_is_expensive;
1373        if !skip_llvm {
1374            rustc_llvm_env(builder, cargo, target)
1375        }
1376    }
1377
1378    // Build jemalloc on AArch64 with support for page sizes up to 64K
1379    // See: https://github.com/rust-lang/rust/pull/135081
1380    // See also the "JEMALLOC_SYS_WITH_LG_PAGE" setting in the tool build step.
1381    if builder.config.jemalloc(target)
1382        && target.starts_with("aarch64")
1383        && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none()
1384    {
1385        cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "16");
1386    }
1387}
1388
1389/// Pass down configuration from the LLVM build into the build of
1390/// rustc_llvm and rustc_codegen_llvm.
1391///
1392/// Note that this has the side-effect of _building LLVM_, which is sometimes
1393/// unwanted (e.g. for check builds).
1394fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
1395    if builder.config.is_rust_llvm(target) {
1396        cargo.env("LLVM_RUSTLLVM", "1");
1397    }
1398    if builder.config.llvm_enzyme {
1399        cargo.env("LLVM_ENZYME", "1");
1400    }
1401    let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
1402    cargo.env("LLVM_CONFIG", &llvm_config);
1403
1404    // Some LLVM linker flags (-L and -l) may be needed to link `rustc_llvm`. Its build script
1405    // expects these to be passed via the `LLVM_LINKER_FLAGS` env variable, separated by
1406    // whitespace.
1407    //
1408    // For example:
1409    // - on windows, when `clang-cl` is used with instrumentation, we need to manually add
1410    // clang's runtime library resource directory so that the profiler runtime library can be
1411    // found. This is to avoid the linker errors about undefined references to
1412    // `__llvm_profile_instrument_memop` when linking `rustc_driver`.
1413    let mut llvm_linker_flags = String::new();
1414    if builder.config.llvm_profile_generate
1415        && target.is_msvc()
1416        && let Some(ref clang_cl_path) = builder.config.llvm_clang_cl
1417    {
1418        // Add clang's runtime library directory to the search path
1419        let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
1420        llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
1421    }
1422
1423    // The config can also specify its own llvm linker flags.
1424    if let Some(ref s) = builder.config.llvm_ldflags {
1425        if !llvm_linker_flags.is_empty() {
1426            llvm_linker_flags.push(' ');
1427        }
1428        llvm_linker_flags.push_str(s);
1429    }
1430
1431    // Set the linker flags via the env var that `rustc_llvm`'s build script will read.
1432    if !llvm_linker_flags.is_empty() {
1433        cargo.env("LLVM_LINKER_FLAGS", llvm_linker_flags);
1434    }
1435
1436    // Building with a static libstdc++ is only supported on Linux and windows-gnu* right now,
1437    // not for MSVC or macOS
1438    if builder.config.llvm_static_stdcpp
1439        && !target.contains("freebsd")
1440        && !target.is_msvc()
1441        && !target.contains("apple")
1442        && !target.contains("solaris")
1443    {
1444        let libstdcxx_name =
1445            if target.contains("windows-gnullvm") { "libc++.a" } else { "libstdc++.a" };
1446        let file = compiler_file(
1447            builder,
1448            &builder.cxx(target).unwrap(),
1449            target,
1450            CLang::Cxx,
1451            libstdcxx_name,
1452        );
1453        cargo.env("LLVM_STATIC_STDCPP", file);
1454    }
1455    if builder.llvm_link_shared() {
1456        cargo.env("LLVM_LINK_SHARED", "1");
1457    }
1458    if builder.config.llvm_use_libcxx {
1459        cargo.env("LLVM_USE_LIBCXX", "1");
1460    }
1461    if builder.config.llvm_assertions {
1462        cargo.env("LLVM_ASSERTIONS", "1");
1463    }
1464}
1465
1466/// `RustcLink` copies compiler rlibs from a rustc build into a compiler sysroot.
1467/// It works with (potentially up to) three compilers:
1468/// - `build_compiler` is a compiler that built rustc rlibs
1469/// - `sysroot_compiler` is a compiler into whose sysroot we will copy the rlibs
1470///   - In most situations, `build_compiler` == `sysroot_compiler`
1471/// - `target_compiler` is the compiler whose rlibs were built. It is not represented explicitly
1472///   in this step, rather we just read the rlibs from a rustc build stamp of `build_compiler`.
1473///
1474/// This is necessary for tools using `rustc_private`, where the previous compiler will build
1475/// a tool against the next compiler.
1476/// To build a tool against a compiler, the rlibs of that compiler that it links against
1477/// must be in the sysroot of the compiler that's doing the compiling.
1478#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1479struct RustcLink {
1480    /// This compiler **built** some rustc, whose rlibs we will copy into a sysroot.
1481    build_compiler: Compiler,
1482    /// This is the compiler into whose sysroot we want to copy the built rlibs.
1483    /// In most cases, it will correspond to `build_compiler`.
1484    sysroot_compiler: Compiler,
1485    target: TargetSelection,
1486    /// Not actually used; only present to make sure the cache invalidation is correct.
1487    crates: Vec<String>,
1488}
1489
1490impl RustcLink {
1491    /// Copy rlibs from the build compiler that build this `rustc` into the sysroot of that
1492    /// build compiler.
1493    fn from_rustc(rustc: Rustc) -> Self {
1494        Self {
1495            build_compiler: rustc.build_compiler,
1496            sysroot_compiler: rustc.build_compiler,
1497            target: rustc.target,
1498            crates: rustc.crates,
1499        }
1500    }
1501
1502    /// Copy rlibs **built** by `build_compiler` into the sysroot of `sysroot_compiler`.
1503    fn from_build_compiler_and_sysroot(
1504        build_compiler: Compiler,
1505        sysroot_compiler: Compiler,
1506        target: TargetSelection,
1507        crates: Vec<String>,
1508    ) -> Self {
1509        Self { build_compiler, sysroot_compiler, target, crates }
1510    }
1511}
1512
1513impl Step for RustcLink {
1514    type Output = ();
1515
1516    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1517        run.never()
1518    }
1519
1520    /// Same as `std_link`, only for librustc
1521    fn run(self, builder: &Builder<'_>) {
1522        let build_compiler = self.build_compiler;
1523        let sysroot_compiler = self.sysroot_compiler;
1524        let target = self.target;
1525        add_to_sysroot(
1526            builder,
1527            &builder.sysroot_target_libdir(sysroot_compiler, target),
1528            &builder.sysroot_target_libdir(sysroot_compiler, sysroot_compiler.host),
1529            &build_stamp::librustc_stamp(builder, build_compiler, target),
1530        );
1531    }
1532}
1533
1534/// Output of the `compile::GccCodegenBackend` step.
1535/// It includes the path to the libgccjit library on which this backend depends.
1536#[derive(Clone)]
1537pub struct GccCodegenBackendOutput {
1538    stamp: BuildStamp,
1539    gcc: GccOutput,
1540}
1541
1542#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1543pub struct GccCodegenBackend {
1544    compilers: RustcPrivateCompilers,
1545}
1546
1547impl Step for GccCodegenBackend {
1548    type Output = GccCodegenBackendOutput;
1549
1550    const IS_HOST: bool = true;
1551
1552    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1553        run.alias("rustc_codegen_gcc").alias("cg_gcc")
1554    }
1555
1556    fn make_run(run: RunConfig<'_>) {
1557        run.builder.ensure(GccCodegenBackend {
1558            compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1559        });
1560    }
1561
1562    fn run(self, builder: &Builder<'_>) -> Self::Output {
1563        let target = self.compilers.target();
1564        let build_compiler = self.compilers.build_compiler();
1565
1566        let stamp = build_stamp::codegen_backend_stamp(
1567            builder,
1568            build_compiler,
1569            target,
1570            &CodegenBackendKind::Gcc,
1571        );
1572
1573        let gcc = builder.ensure(Gcc { target });
1574
1575        if builder.config.keep_stage.contains(&build_compiler.stage) {
1576            trace!("`keep-stage` requested");
1577            builder.info(
1578                "WARNING: Using a potentially old codegen backend. \
1579                This may not behave well.",
1580            );
1581            // Codegen backends are linked separately from this step today, so we don't do
1582            // anything here.
1583            return GccCodegenBackendOutput { stamp, gcc };
1584        }
1585
1586        let mut cargo = builder::Cargo::new(
1587            builder,
1588            build_compiler,
1589            Mode::Codegen,
1590            SourceType::InTree,
1591            target,
1592            Kind::Build,
1593        );
1594        cargo.arg("--manifest-path").arg(builder.src.join("compiler/rustc_codegen_gcc/Cargo.toml"));
1595        rustc_cargo_env(builder, &mut cargo, target);
1596
1597        add_cg_gcc_cargo_flags(&mut cargo, &gcc);
1598
1599        let _guard =
1600            builder.msg(Kind::Build, "codegen backend gcc", Mode::Codegen, build_compiler, target);
1601        let files = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false);
1602
1603        GccCodegenBackendOutput {
1604            stamp: write_codegen_backend_stamp(stamp, files, builder.config.dry_run()),
1605            gcc,
1606        }
1607    }
1608
1609    fn metadata(&self) -> Option<StepMetadata> {
1610        Some(
1611            StepMetadata::build("rustc_codegen_gcc", self.compilers.target())
1612                .built_by(self.compilers.build_compiler()),
1613        )
1614    }
1615}
1616
1617#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1618pub struct CraneliftCodegenBackend {
1619    pub compilers: RustcPrivateCompilers,
1620}
1621
1622impl Step for CraneliftCodegenBackend {
1623    type Output = BuildStamp;
1624    const IS_HOST: bool = true;
1625
1626    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1627        run.alias("rustc_codegen_cranelift").alias("cg_clif")
1628    }
1629
1630    fn make_run(run: RunConfig<'_>) {
1631        run.builder.ensure(CraneliftCodegenBackend {
1632            compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1633        });
1634    }
1635
1636    fn run(self, builder: &Builder<'_>) -> Self::Output {
1637        let target = self.compilers.target();
1638        let build_compiler = self.compilers.build_compiler();
1639
1640        let stamp = build_stamp::codegen_backend_stamp(
1641            builder,
1642            build_compiler,
1643            target,
1644            &CodegenBackendKind::Cranelift,
1645        );
1646
1647        if builder.config.keep_stage.contains(&build_compiler.stage) {
1648            trace!("`keep-stage` requested");
1649            builder.info(
1650                "WARNING: Using a potentially old codegen backend. \
1651                This may not behave well.",
1652            );
1653            // Codegen backends are linked separately from this step today, so we don't do
1654            // anything here.
1655            return stamp;
1656        }
1657
1658        let mut cargo = builder::Cargo::new(
1659            builder,
1660            build_compiler,
1661            Mode::Codegen,
1662            SourceType::InTree,
1663            target,
1664            Kind::Build,
1665        );
1666        cargo
1667            .arg("--manifest-path")
1668            .arg(builder.src.join("compiler/rustc_codegen_cranelift/Cargo.toml"));
1669        rustc_cargo_env(builder, &mut cargo, target);
1670
1671        let _guard = builder.msg(
1672            Kind::Build,
1673            "codegen backend cranelift",
1674            Mode::Codegen,
1675            build_compiler,
1676            target,
1677        );
1678        let files = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false);
1679        write_codegen_backend_stamp(stamp, files, builder.config.dry_run())
1680    }
1681
1682    fn metadata(&self) -> Option<StepMetadata> {
1683        Some(
1684            StepMetadata::build("rustc_codegen_cranelift", self.compilers.target())
1685                .built_by(self.compilers.build_compiler()),
1686        )
1687    }
1688}
1689
1690/// Write filtered `files` into the passed build stamp and returns it.
1691fn write_codegen_backend_stamp(
1692    mut stamp: BuildStamp,
1693    files: Vec<PathBuf>,
1694    dry_run: bool,
1695) -> BuildStamp {
1696    if dry_run {
1697        return stamp;
1698    }
1699
1700    let mut files = files.into_iter().filter(|f| {
1701        let filename = f.file_name().unwrap().to_str().unwrap();
1702        is_dylib(f) && filename.contains("rustc_codegen_")
1703    });
1704    let codegen_backend = match files.next() {
1705        Some(f) => f,
1706        None => panic!("no dylibs built for codegen backend?"),
1707    };
1708    if let Some(f) = files.next() {
1709        panic!("codegen backend built two dylibs:\n{}\n{}", codegen_backend.display(), f.display());
1710    }
1711
1712    let codegen_backend = codegen_backend.to_str().unwrap();
1713    stamp = stamp.add_stamp(codegen_backend);
1714    t!(stamp.write());
1715    stamp
1716}
1717
1718/// Creates the `codegen-backends` folder for a compiler that's about to be
1719/// assembled as a complete compiler.
1720///
1721/// This will take the codegen artifacts recorded in the given `stamp` and link them
1722/// into an appropriate location for `target_compiler` to be a functional
1723/// compiler.
1724fn copy_codegen_backends_to_sysroot(
1725    builder: &Builder<'_>,
1726    stamp: BuildStamp,
1727    target_compiler: Compiler,
1728) {
1729    // Note that this step is different than all the other `*Link` steps in
1730    // that it's not assembling a bunch of libraries but rather is primarily
1731    // moving the codegen backend into place. The codegen backend of rustc is
1732    // not linked into the main compiler by default but is rather dynamically
1733    // selected at runtime for inclusion.
1734    //
1735    // Here we're looking for the output dylib of the `CodegenBackend` step and
1736    // we're copying that into the `codegen-backends` folder.
1737    let dst = builder.sysroot_codegen_backends(target_compiler);
1738    t!(fs::create_dir_all(&dst), dst);
1739
1740    if builder.config.dry_run() {
1741        return;
1742    }
1743
1744    if stamp.path().exists() {
1745        let file = get_codegen_backend_file(&stamp);
1746        builder.copy_link(
1747            &file,
1748            &dst.join(normalize_codegen_backend_name(builder, &file)),
1749            FileType::NativeLibrary,
1750        );
1751    }
1752}
1753
1754/// Gets the path to a dynamic codegen backend library from its build stamp.
1755pub fn get_codegen_backend_file(stamp: &BuildStamp) -> PathBuf {
1756    PathBuf::from(t!(fs::read_to_string(stamp.path())))
1757}
1758
1759/// Normalize the name of a dynamic codegen backend library.
1760pub fn normalize_codegen_backend_name(builder: &Builder<'_>, path: &Path) -> String {
1761    let filename = path.file_name().unwrap().to_str().unwrap();
1762    // change e.g. `librustc_codegen_cranelift-xxxxxx.so` to
1763    // `librustc_codegen_cranelift-release.so`
1764    let dash = filename.find('-').unwrap();
1765    let dot = filename.find('.').unwrap();
1766    format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..])
1767}
1768
1769pub fn compiler_file(
1770    builder: &Builder<'_>,
1771    compiler: &Path,
1772    target: TargetSelection,
1773    c: CLang,
1774    file: &str,
1775) -> PathBuf {
1776    if builder.config.dry_run() {
1777        return PathBuf::new();
1778    }
1779    let mut cmd = command(compiler);
1780    cmd.args(builder.cc_handled_clags(target, c));
1781    cmd.args(builder.cc_unhandled_cflags(target, GitRepo::Rustc, c));
1782    cmd.arg(format!("-print-file-name={file}"));
1783    let out = cmd.run_capture_stdout(builder).stdout();
1784    PathBuf::from(out.trim())
1785}
1786
1787#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1788pub struct Sysroot {
1789    pub compiler: Compiler,
1790    /// See [`Std::force_recompile`].
1791    force_recompile: bool,
1792}
1793
1794impl Sysroot {
1795    pub(crate) fn new(compiler: Compiler) -> Self {
1796        Sysroot { compiler, force_recompile: false }
1797    }
1798}
1799
1800impl Step for Sysroot {
1801    type Output = PathBuf;
1802
1803    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1804        run.never()
1805    }
1806
1807    /// Returns the sysroot that `compiler` is supposed to use.
1808    /// For the stage0 compiler, this is stage0-sysroot (because of the initial std build).
1809    /// For all other stages, it's the same stage directory that the compiler lives in.
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        // In some cases(see https://github.com/rust-lang/rust/issues/109314), when the stage0
1834        // compiler relies on more recent version of LLVM than the stage0 compiler, it may not
1835        // be able to locate the correct LLVM in the sysroot. This situation typically occurs
1836        // when we upgrade LLVM version while the stage0 compiler continues to use an older version.
1837        //
1838        // Make sure to add the correct version of LLVM into the stage0 sysroot.
1839        if compiler.stage == 0 {
1840            dist::maybe_install_llvm_target(builder, compiler.host, &sysroot);
1841        }
1842
1843        // If we're downloading a compiler from CI, we can use the same compiler for all stages other than 0.
1844        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            // #102002, cleanup old toolchain folders when using download-rustc so people don't use them by accident.
1851            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            // Copy the compiler into the correct sysroot.
1861            // NOTE(#108767): We intentionally don't copy `rustc-dev` artifacts until they're requested with `builder.ensure(Rustc)`.
1862            // This fixes an issue where we'd have multiple copies of libc in the sysroot with no way to tell which to load.
1863            // There are a few quirks of bootstrap that interact to make this reliable:
1864            // 1. The order `Step`s are run is hard-coded in `builder.rs` and not configurable. This
1865            //    avoids e.g. reordering `test::UiFulldeps` before `test::Ui` and causing the latter to
1866            //    fail because of duplicate metadata.
1867            // 2. The sysroot is deleted and recreated between each invocation, so running `x test
1868            //    ui-fulldeps && x test ui` can't cause failures.
1869            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            // NOTE: we can't copy std eagerly because `stage2-test-sysroot` needs to have only the
1881            // newly compiled std, not the downloaded std.
1882            add_filtered_files("lib", builder.config.ci_rust_std_contents());
1883
1884            let filtered_extensions = [
1885                OsStr::new("rmeta"),
1886                OsStr::new("rlib"),
1887                // FIXME: this is wrong when compiler.host != build, but we don't support that today
1888                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        // Symlink the source root into the same location inside the sysroot,
1908        // where `rust-src` component would go (`$sysroot/lib/rustlib/src/rust`),
1909        // so that any tools relying on `rust-src` also work for local builds,
1910        // and also for translating the virtual `/rustc/$hash` back to the real
1911        // directory (for running tests with `rust.remap-debuginfo = true`).
1912        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        // rustc-src component is already part of CI rustc's sysroot
1936        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/// Prepare a compiler sysroot.
1958///
1959/// The sysroot may contain various things useful for running the compiler, like linkers and
1960/// linker wrappers (LLD, LLVM bitcode linker, etc.).
1961///
1962/// This will assemble a compiler in `build/$target/stage$stage`.
1963#[derive(Debug, PartialOrd, Ord, Clone, PartialEq, Eq, Hash)]
1964pub struct Assemble {
1965    /// The compiler which we will produce in this step. Assemble itself will
1966    /// take care of ensuring that the necessary prerequisites to do so exist,
1967    /// that is, this can be e.g. a stage2 compiler and Assemble will build
1968    /// the previous stages for you.
1969    pub target_compiler: Compiler,
1970}
1971
1972impl Step for Assemble {
1973    type Output = Compiler;
1974    const IS_HOST: bool = true;
1975
1976    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1977        run.path("compiler/rustc").path("compiler")
1978    }
1979
1980    fn make_run(run: RunConfig<'_>) {
1981        run.builder.ensure(Assemble {
1982            target_compiler: run.builder.compiler(run.builder.top_stage, run.target),
1983        });
1984    }
1985
1986    fn run(self, builder: &Builder<'_>) -> Compiler {
1987        let target_compiler = self.target_compiler;
1988
1989        if target_compiler.stage == 0 {
1990            trace!("stage 0 build compiler is always available, simply returning");
1991            assert_eq!(
1992                builder.config.host_target, target_compiler.host,
1993                "Cannot obtain compiler for non-native build triple at stage 0"
1994            );
1995            // The stage 0 compiler for the build triple is always pre-built.
1996            return target_compiler;
1997        }
1998
1999        // We prepend this bin directory to the user PATH when linking Rust binaries. To
2000        // avoid shadowing the system LLD we rename the LLD we provide to `rust-lld`.
2001        let libdir = builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2002        let libdir_bin = libdir.parent().unwrap().join("bin");
2003        t!(fs::create_dir_all(&libdir_bin));
2004
2005        if builder.config.llvm_enabled(target_compiler.host) {
2006            trace!("target_compiler.host" = ?target_compiler.host, "LLVM enabled");
2007
2008            let llvm::LlvmResult { llvm_config, .. } =
2009                builder.ensure(llvm::Llvm { target: target_compiler.host });
2010            if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
2011                trace!("LLVM tools enabled");
2012
2013                let llvm_bin_dir =
2014                    command(llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
2015                let llvm_bin_dir = Path::new(llvm_bin_dir.trim());
2016
2017                // Since we've already built the LLVM tools, install them to the sysroot.
2018                // This is the equivalent of installing the `llvm-tools-preview` component via
2019                // rustup, and lets developers use a locally built toolchain to
2020                // build projects that expect llvm tools to be present in the sysroot
2021                // (e.g. the `bootimage` crate).
2022
2023                #[cfg(feature = "tracing")]
2024                let _llvm_tools_span =
2025                    span!(tracing::Level::TRACE, "installing llvm tools to sysroot", ?libdir_bin)
2026                        .entered();
2027                for tool in LLVM_TOOLS {
2028                    trace!("installing `{tool}`");
2029                    let tool_exe = exe(tool, target_compiler.host);
2030                    let src_path = llvm_bin_dir.join(&tool_exe);
2031
2032                    // When using `download-ci-llvm`, some of the tools may not exist, so skip trying to copy them.
2033                    if !src_path.exists() && builder.config.llvm_from_ci {
2034                        eprintln!("{} does not exist; skipping copy", src_path.display());
2035                        continue;
2036                    }
2037
2038                    // There is a chance that these tools are being installed from an external LLVM.
2039                    // Use `Builder::resolve_symlink_and_copy` instead of `Builder::copy_link` to ensure
2040                    // we are copying the original file not the symlinked path, which causes issues for
2041                    // tarball distribution.
2042                    //
2043                    // See https://github.com/rust-lang/rust/issues/135554.
2044                    builder.resolve_symlink_and_copy(&src_path, &libdir_bin.join(&tool_exe));
2045                }
2046            }
2047        }
2048
2049        let maybe_install_llvm_bitcode_linker = || {
2050            if builder.config.llvm_bitcode_linker_enabled {
2051                trace!("llvm-bitcode-linker enabled, installing");
2052                let llvm_bitcode_linker = builder.ensure(
2053                    crate::core::build_steps::tool::LlvmBitcodeLinker::from_target_compiler(
2054                        builder,
2055                        target_compiler,
2056                    ),
2057                );
2058
2059                // Copy the llvm-bitcode-linker to the self-contained binary directory
2060                let bindir_self_contained = builder
2061                    .sysroot(target_compiler)
2062                    .join(format!("lib/rustlib/{}/bin/self-contained", target_compiler.host));
2063                let tool_exe = exe("llvm-bitcode-linker", target_compiler.host);
2064
2065                t!(fs::create_dir_all(&bindir_self_contained));
2066                builder.copy_link(
2067                    &llvm_bitcode_linker.tool_path,
2068                    &bindir_self_contained.join(tool_exe),
2069                    FileType::Executable,
2070                );
2071            }
2072        };
2073
2074        // If we're downloading a compiler from CI, we can use the same compiler for all stages other than 0.
2075        if builder.download_rustc() {
2076            trace!("`download-rustc` requested, reusing CI compiler for stage > 0");
2077
2078            builder.std(target_compiler, target_compiler.host);
2079            let sysroot =
2080                builder.ensure(Sysroot { compiler: target_compiler, force_recompile: false });
2081            // Ensure that `libLLVM.so` ends up in the newly created target directory,
2082            // so that tools using `rustc_private` can use it.
2083            dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
2084            // Lower stages use `ci-rustc-sysroot`, not stageN
2085            if target_compiler.stage == builder.top_stage {
2086                builder.info(&format!("Creating a sysroot for stage{stage} compiler (use `rustup toolchain link 'name' build/host/stage{stage}`)", stage = target_compiler.stage));
2087            }
2088
2089            // FIXME: this is incomplete, we do not copy a bunch of other stuff to the downloaded
2090            // sysroot...
2091            maybe_install_llvm_bitcode_linker();
2092
2093            return target_compiler;
2094        }
2095
2096        // Get the compiler that we'll use to bootstrap ourselves.
2097        //
2098        // Note that this is where the recursive nature of the bootstrap
2099        // happens, as this will request the previous stage's compiler on
2100        // downwards to stage 0.
2101        //
2102        // Also note that we're building a compiler for the host platform. We
2103        // only assume that we can run `build` artifacts, which means that to
2104        // produce some other architecture compiler we need to start from
2105        // `build` to get there.
2106        //
2107        // FIXME: It may be faster if we build just a stage 1 compiler and then
2108        //        use that to bootstrap this compiler forward.
2109        debug!(
2110            "ensuring build compiler is available: compiler(stage = {}, host = {:?})",
2111            target_compiler.stage - 1,
2112            builder.config.host_target,
2113        );
2114        let build_compiler =
2115            builder.compiler(target_compiler.stage - 1, builder.config.host_target);
2116
2117        // Build enzyme
2118        if builder.config.llvm_enzyme && !builder.config.dry_run() {
2119            debug!("`llvm_enzyme` requested");
2120            let enzyme_install = builder.ensure(llvm::Enzyme { target: build_compiler.host });
2121            if let Some(llvm_config) = builder.llvm_config(builder.config.host_target) {
2122                let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
2123                let lib_ext = std::env::consts::DLL_EXTENSION;
2124                let libenzyme = format!("libEnzyme-{llvm_version_major}");
2125                let src_lib =
2126                    enzyme_install.join("build/Enzyme").join(&libenzyme).with_extension(lib_ext);
2127                let libdir = builder.sysroot_target_libdir(build_compiler, build_compiler.host);
2128                let target_libdir =
2129                    builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2130                let dst_lib = libdir.join(&libenzyme).with_extension(lib_ext);
2131                let target_dst_lib = target_libdir.join(&libenzyme).with_extension(lib_ext);
2132                builder.copy_link(&src_lib, &dst_lib, FileType::NativeLibrary);
2133                builder.copy_link(&src_lib, &target_dst_lib, FileType::NativeLibrary);
2134            }
2135        }
2136
2137        // Build the libraries for this compiler to link to (i.e., the libraries
2138        // it uses at runtime).
2139        debug!(
2140            ?build_compiler,
2141            "target_compiler.host" = ?target_compiler.host,
2142            "building compiler libraries to link to"
2143        );
2144
2145        // It is possible that an uplift has happened, so we override build_compiler here.
2146        let BuiltRustc { build_compiler } =
2147            builder.ensure(Rustc::new(build_compiler, target_compiler.host));
2148
2149        let stage = target_compiler.stage;
2150        let host = target_compiler.host;
2151        let (host_info, dir_name) = if build_compiler.host == host {
2152            ("".into(), "host".into())
2153        } else {
2154            (format!(" ({host})"), host.to_string())
2155        };
2156        // NOTE: "Creating a sysroot" is somewhat inconsistent with our internal terminology, since
2157        // sysroots can temporarily be empty until we put the compiler inside. However,
2158        // `ensure(Sysroot)` isn't really something that's user facing, so there shouldn't be any
2159        // ambiguity.
2160        let msg = format!(
2161            "Creating a sysroot for stage{stage} compiler{host_info} (use `rustup toolchain link 'name' build/{dir_name}/stage{stage}`)"
2162        );
2163        builder.info(&msg);
2164
2165        // Link in all dylibs to the libdir
2166        let stamp = build_stamp::librustc_stamp(builder, build_compiler, target_compiler.host);
2167        let proc_macros = builder
2168            .read_stamp_file(&stamp)
2169            .into_iter()
2170            .filter_map(|(path, dependency_type)| {
2171                if dependency_type == DependencyType::Host {
2172                    Some(path.file_name().unwrap().to_owned().into_string().unwrap())
2173                } else {
2174                    None
2175                }
2176            })
2177            .collect::<HashSet<_>>();
2178
2179        let sysroot = builder.sysroot(target_compiler);
2180        let rustc_libdir = builder.rustc_libdir(target_compiler);
2181        t!(fs::create_dir_all(&rustc_libdir));
2182        let src_libdir = builder.sysroot_target_libdir(build_compiler, host);
2183        for f in builder.read_dir(&src_libdir) {
2184            let filename = f.file_name().into_string().unwrap();
2185
2186            let is_proc_macro = proc_macros.contains(&filename);
2187            let is_dylib_or_debug = is_dylib(&f.path()) || is_debug_info(&filename);
2188
2189            // If we link statically to stdlib, do not copy the libstd dynamic library file
2190            // FIXME: Also do this for Windows once incremental post-optimization stage0 tests
2191            // work without std.dll (see https://github.com/rust-lang/rust/pull/131188).
2192            let can_be_rustc_dynamic_dep = if builder
2193                .link_std_into_rustc_driver(target_compiler.host)
2194                && !target_compiler.host.is_windows()
2195            {
2196                let is_std = filename.starts_with("std-") || filename.starts_with("libstd-");
2197                !is_std
2198            } else {
2199                true
2200            };
2201
2202            if is_dylib_or_debug && can_be_rustc_dynamic_dep && !is_proc_macro {
2203                builder.copy_link(&f.path(), &rustc_libdir.join(&filename), FileType::Regular);
2204            }
2205        }
2206
2207        {
2208            #[cfg(feature = "tracing")]
2209            let _codegen_backend_span =
2210                span!(tracing::Level::DEBUG, "building requested codegen backends").entered();
2211
2212            for backend in builder.config.enabled_codegen_backends(target_compiler.host) {
2213                // FIXME: this is a horrible hack used to make `x check` work when other codegen
2214                // backends are enabled.
2215                // `x check` will check stage 1 rustc, which copies its rmetas to the stage0 sysroot.
2216                // Then it checks codegen backends, which correctly use these rmetas.
2217                // Then it needs to check std, but for that it needs to build stage 1 rustc.
2218                // This copies the build rmetas into the stage0 sysroot, effectively poisoning it,
2219                // because we then have both check and build rmetas in the same sysroot.
2220                // That would be fine on its own. However, when another codegen backend is enabled,
2221                // then building stage 1 rustc implies also building stage 1 codegen backend (even if
2222                // it isn't used for anything). And since that tries to use the poisoned
2223                // rmetas, it fails to build.
2224                // We don't actually need to build rustc-private codegen backends for checking std,
2225                // so instead we skip that.
2226                // Note: this would be also an issue for other rustc-private tools, but that is "solved"
2227                // by check::Std being last in the list of checked things (see
2228                // `Builder::get_step_descriptions`).
2229                if builder.kind == Kind::Check && builder.top_stage == 1 {
2230                    continue;
2231                }
2232
2233                let prepare_compilers = || {
2234                    RustcPrivateCompilers::from_build_and_target_compiler(
2235                        build_compiler,
2236                        target_compiler,
2237                    )
2238                };
2239
2240                match backend {
2241                    CodegenBackendKind::Cranelift => {
2242                        let stamp = builder
2243                            .ensure(CraneliftCodegenBackend { compilers: prepare_compilers() });
2244                        copy_codegen_backends_to_sysroot(builder, stamp, target_compiler);
2245                    }
2246                    CodegenBackendKind::Gcc => {
2247                        let output =
2248                            builder.ensure(GccCodegenBackend { compilers: prepare_compilers() });
2249                        copy_codegen_backends_to_sysroot(builder, output.stamp, target_compiler);
2250                        // Also copy libgccjit to the library sysroot, so that it is available for
2251                        // the codegen backend.
2252                        output.gcc.install_to(builder, &rustc_libdir);
2253                    }
2254                    CodegenBackendKind::Llvm | CodegenBackendKind::Custom(_) => continue,
2255                }
2256            }
2257        }
2258
2259        if builder.config.lld_enabled {
2260            let lld_wrapper =
2261                builder.ensure(crate::core::build_steps::tool::LldWrapper::for_use_by_compiler(
2262                    builder,
2263                    target_compiler,
2264                ));
2265            copy_lld_artifacts(builder, lld_wrapper, target_compiler);
2266        }
2267
2268        if builder.config.llvm_enabled(target_compiler.host) && builder.config.llvm_tools_enabled {
2269            debug!(
2270                "llvm and llvm tools enabled; copying `llvm-objcopy` as `rust-objcopy` to \
2271                workaround faulty homebrew `strip`s"
2272            );
2273
2274            // `llvm-strip` is used by rustc, which is actually just a symlink to `llvm-objcopy`, so
2275            // copy and rename `llvm-objcopy`.
2276            //
2277            // But only do so if llvm-tools are enabled, as bootstrap compiler might not contain any
2278            // LLVM tools, e.g. for cg_clif.
2279            // See <https://github.com/rust-lang/rust/issues/132719>.
2280            let src_exe = exe("llvm-objcopy", target_compiler.host);
2281            let dst_exe = exe("rust-objcopy", target_compiler.host);
2282            builder.copy_link(
2283                &libdir_bin.join(src_exe),
2284                &libdir_bin.join(dst_exe),
2285                FileType::Executable,
2286            );
2287        }
2288
2289        // In addition to `rust-lld` also install `wasm-component-ld` when
2290        // is enabled. This is used by the `wasm32-wasip2` target of Rust.
2291        if builder.tool_enabled("wasm-component-ld") {
2292            let wasm_component = builder.ensure(
2293                crate::core::build_steps::tool::WasmComponentLd::for_use_by_compiler(
2294                    builder,
2295                    target_compiler,
2296                ),
2297            );
2298            builder.copy_link(
2299                &wasm_component.tool_path,
2300                &libdir_bin.join(wasm_component.tool_path.file_name().unwrap()),
2301                FileType::Executable,
2302            );
2303        }
2304
2305        maybe_install_llvm_bitcode_linker();
2306
2307        // Ensure that `libLLVM.so` ends up in the newly build compiler directory,
2308        // so that it can be found when the newly built `rustc` is run.
2309        debug!(
2310            "target_compiler.host" = ?target_compiler.host,
2311            ?sysroot,
2312            "ensuring availability of `libLLVM.so` in compiler directory"
2313        );
2314        dist::maybe_install_llvm_runtime(builder, target_compiler.host, &sysroot);
2315        dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);
2316
2317        // Link the compiler binary itself into place
2318        let out_dir = builder.cargo_out(build_compiler, Mode::Rustc, host);
2319        let rustc = out_dir.join(exe("rustc-main", host));
2320        let bindir = sysroot.join("bin");
2321        t!(fs::create_dir_all(bindir));
2322        let compiler = builder.rustc(target_compiler);
2323        debug!(src = ?rustc, dst = ?compiler, "linking compiler binary itself");
2324        builder.copy_link(&rustc, &compiler, FileType::Executable);
2325
2326        target_compiler
2327    }
2328}
2329
2330/// Link some files into a rustc sysroot.
2331///
2332/// For a particular stage this will link the file listed in `stamp` into the
2333/// `sysroot_dst` provided.
2334#[track_caller]
2335pub fn add_to_sysroot(
2336    builder: &Builder<'_>,
2337    sysroot_dst: &Path,
2338    sysroot_host_dst: &Path,
2339    stamp: &BuildStamp,
2340) {
2341    let self_contained_dst = &sysroot_dst.join("self-contained");
2342    t!(fs::create_dir_all(sysroot_dst));
2343    t!(fs::create_dir_all(sysroot_host_dst));
2344    t!(fs::create_dir_all(self_contained_dst));
2345    for (path, dependency_type) in builder.read_stamp_file(stamp) {
2346        let dst = match dependency_type {
2347            DependencyType::Host => sysroot_host_dst,
2348            DependencyType::Target => sysroot_dst,
2349            DependencyType::TargetSelfContained => self_contained_dst,
2350        };
2351        builder.copy_link(&path, &dst.join(path.file_name().unwrap()), FileType::Regular);
2352    }
2353}
2354
2355pub fn run_cargo(
2356    builder: &Builder<'_>,
2357    cargo: Cargo,
2358    tail_args: Vec<String>,
2359    stamp: &BuildStamp,
2360    additional_target_deps: Vec<(PathBuf, DependencyType)>,
2361    is_check: bool,
2362    rlib_only_metadata: bool,
2363) -> Vec<PathBuf> {
2364    // `target_root_dir` looks like $dir/$target/release
2365    let target_root_dir = stamp.path().parent().unwrap();
2366    // `target_deps_dir` looks like $dir/$target/release/deps
2367    let target_deps_dir = target_root_dir.join("deps");
2368    // `host_root_dir` looks like $dir/release
2369    let host_root_dir = target_root_dir
2370        .parent()
2371        .unwrap() // chop off `release`
2372        .parent()
2373        .unwrap() // chop off `$target`
2374        .join(target_root_dir.file_name().unwrap());
2375
2376    // Spawn Cargo slurping up its JSON output. We'll start building up the
2377    // `deps` array of all files it generated along with a `toplevel` array of
2378    // files we need to probe for later.
2379    let mut deps = Vec::new();
2380    let mut toplevel = Vec::new();
2381    let ok = stream_cargo(builder, cargo, tail_args, &mut |msg| {
2382        let (filenames_vec, crate_types) = match msg {
2383            CargoMessage::CompilerArtifact {
2384                filenames,
2385                target: CargoTarget { crate_types },
2386                ..
2387            } => {
2388                let mut f: Vec<String> = filenames.into_iter().map(|s| s.into_owned()).collect();
2389                f.sort(); // Sort the filenames
2390                (f, crate_types)
2391            }
2392            _ => return,
2393        };
2394        for filename in filenames_vec {
2395            // Skip files like executables
2396            let mut keep = false;
2397            if filename.ends_with(".lib")
2398                || filename.ends_with(".a")
2399                || is_debug_info(&filename)
2400                || is_dylib(Path::new(&*filename))
2401            {
2402                // Always keep native libraries, rust dylibs and debuginfo
2403                keep = true;
2404            }
2405            if is_check && filename.ends_with(".rmeta") {
2406                // During check builds we need to keep crate metadata
2407                keep = true;
2408            } else if rlib_only_metadata {
2409                if filename.contains("jemalloc_sys")
2410                    || filename.contains("rustc_public_bridge")
2411                    || filename.contains("rustc_public")
2412                {
2413                    // jemalloc_sys and rustc_public_bridge are not linked into librustc_driver.so,
2414                    // so we need to distribute them as rlib to be able to use them.
2415                    keep |= filename.ends_with(".rlib");
2416                } else {
2417                    // Distribute the rest of the rustc crates as rmeta files only to reduce
2418                    // the tarball sizes by about 50%. The object files are linked into
2419                    // librustc_driver.so, so it is still possible to link against them.
2420                    keep |= filename.ends_with(".rmeta");
2421                }
2422            } else {
2423                // In all other cases keep all rlibs
2424                keep |= filename.ends_with(".rlib");
2425            }
2426
2427            if !keep {
2428                continue;
2429            }
2430
2431            let filename = Path::new(&*filename);
2432
2433            // If this was an output file in the "host dir" we don't actually
2434            // worry about it, it's not relevant for us
2435            if filename.starts_with(&host_root_dir) {
2436                // Unless it's a proc macro used in the compiler
2437                if crate_types.iter().any(|t| t == "proc-macro") {
2438                    deps.push((filename.to_path_buf(), DependencyType::Host));
2439                }
2440                continue;
2441            }
2442
2443            // If this was output in the `deps` dir then this is a precise file
2444            // name (hash included) so we start tracking it.
2445            if filename.starts_with(&target_deps_dir) {
2446                deps.push((filename.to_path_buf(), DependencyType::Target));
2447                continue;
2448            }
2449
2450            // Otherwise this was a "top level artifact" which right now doesn't
2451            // have a hash in the name, but there's a version of this file in
2452            // the `deps` folder which *does* have a hash in the name. That's
2453            // the one we'll want to we'll probe for it later.
2454            //
2455            // We do not use `Path::file_stem` or `Path::extension` here,
2456            // because some generated files may have multiple extensions e.g.
2457            // `std-<hash>.dll.lib` on Windows. The aforementioned methods only
2458            // split the file name by the last extension (`.lib`) while we need
2459            // to split by all extensions (`.dll.lib`).
2460            let expected_len = t!(filename.metadata()).len();
2461            let filename = filename.file_name().unwrap().to_str().unwrap();
2462            let mut parts = filename.splitn(2, '.');
2463            let file_stem = parts.next().unwrap().to_owned();
2464            let extension = parts.next().unwrap().to_owned();
2465
2466            toplevel.push((file_stem, extension, expected_len));
2467        }
2468    });
2469
2470    if !ok {
2471        crate::exit!(1);
2472    }
2473
2474    if builder.config.dry_run() {
2475        return Vec::new();
2476    }
2477
2478    // Ok now we need to actually find all the files listed in `toplevel`. We've
2479    // got a list of prefix/extensions and we basically just need to find the
2480    // most recent file in the `deps` folder corresponding to each one.
2481    let contents = target_deps_dir
2482        .read_dir()
2483        .unwrap_or_else(|e| panic!("Couldn't read {}: {}", target_deps_dir.display(), e))
2484        .map(|e| t!(e))
2485        .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata())))
2486        .collect::<Vec<_>>();
2487    for (prefix, extension, expected_len) in toplevel {
2488        let candidates = contents.iter().filter(|&(_, filename, meta)| {
2489            meta.len() == expected_len
2490                && filename
2491                    .strip_prefix(&prefix[..])
2492                    .map(|s| s.starts_with('-') && s.ends_with(&extension[..]))
2493                    .unwrap_or(false)
2494        });
2495        let max = candidates.max_by_key(|&(_, _, metadata)| {
2496            metadata.modified().expect("mtime should be available on all relevant OSes")
2497        });
2498        let path_to_add = match max {
2499            Some(triple) => triple.0.to_str().unwrap(),
2500            None => panic!("no output generated for {prefix:?} {extension:?}"),
2501        };
2502        if is_dylib(Path::new(path_to_add)) {
2503            let candidate = format!("{path_to_add}.lib");
2504            let candidate = PathBuf::from(candidate);
2505            if candidate.exists() {
2506                deps.push((candidate, DependencyType::Target));
2507            }
2508        }
2509        deps.push((path_to_add.into(), DependencyType::Target));
2510    }
2511
2512    deps.extend(additional_target_deps);
2513    deps.sort();
2514    let mut new_contents = Vec::new();
2515    for (dep, dependency_type) in deps.iter() {
2516        new_contents.extend(match *dependency_type {
2517            DependencyType::Host => b"h",
2518            DependencyType::Target => b"t",
2519            DependencyType::TargetSelfContained => b"s",
2520        });
2521        new_contents.extend(dep.to_str().unwrap().as_bytes());
2522        new_contents.extend(b"\0");
2523    }
2524    t!(fs::write(stamp.path(), &new_contents));
2525    deps.into_iter().map(|(d, _)| d).collect()
2526}
2527
2528pub fn stream_cargo(
2529    builder: &Builder<'_>,
2530    cargo: Cargo,
2531    tail_args: Vec<String>,
2532    cb: &mut dyn FnMut(CargoMessage<'_>),
2533) -> bool {
2534    let mut cmd = cargo.into_cmd();
2535
2536    // Instruct Cargo to give us json messages on stdout, critically leaving
2537    // stderr as piped so we can get those pretty colors.
2538    let mut message_format = if builder.config.json_output {
2539        String::from("json")
2540    } else {
2541        String::from("json-render-diagnostics")
2542    };
2543    if let Some(s) = &builder.config.rustc_error_format {
2544        message_format.push_str(",json-diagnostic-");
2545        message_format.push_str(s);
2546    }
2547    cmd.arg("--message-format").arg(message_format);
2548
2549    for arg in tail_args {
2550        cmd.arg(arg);
2551    }
2552
2553    builder.verbose(|| println!("running: {cmd:?}"));
2554
2555    let streaming_command = cmd.stream_capture_stdout(&builder.config.exec_ctx);
2556
2557    let Some(mut streaming_command) = streaming_command else {
2558        return true;
2559    };
2560
2561    // Spawn Cargo slurping up its JSON output. We'll start building up the
2562    // `deps` array of all files it generated along with a `toplevel` array of
2563    // files we need to probe for later.
2564    let stdout = BufReader::new(streaming_command.stdout.take().unwrap());
2565    for line in stdout.lines() {
2566        let line = t!(line);
2567        match serde_json::from_str::<CargoMessage<'_>>(&line) {
2568            Ok(msg) => {
2569                if builder.config.json_output {
2570                    // Forward JSON to stdout.
2571                    println!("{line}");
2572                }
2573                cb(msg)
2574            }
2575            // If this was informational, just print it out and continue
2576            Err(_) => println!("{line}"),
2577        }
2578    }
2579
2580    // Make sure Cargo actually succeeded after we read all of its stdout.
2581    let status = t!(streaming_command.wait(&builder.config.exec_ctx));
2582    if builder.is_verbose() && !status.success() {
2583        eprintln!(
2584            "command did not execute successfully: {cmd:?}\n\
2585                  expected success, got: {status}"
2586        );
2587    }
2588
2589    status.success()
2590}
2591
2592#[derive(Deserialize)]
2593pub struct CargoTarget<'a> {
2594    crate_types: Vec<Cow<'a, str>>,
2595}
2596
2597#[derive(Deserialize)]
2598#[serde(tag = "reason", rename_all = "kebab-case")]
2599pub enum CargoMessage<'a> {
2600    CompilerArtifact { filenames: Vec<Cow<'a, str>>, target: CargoTarget<'a> },
2601    BuildScriptExecuted,
2602    BuildFinished,
2603}
2604
2605pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path) {
2606    // FIXME: to make things simpler for now, limit this to the host and target where we know
2607    // `strip -g` is both available and will fix the issue, i.e. on a x64 linux host that is not
2608    // cross-compiling. Expand this to other appropriate targets in the future.
2609    if target != "x86_64-unknown-linux-gnu"
2610        || !builder.config.is_host_target(target)
2611        || !path.exists()
2612    {
2613        return;
2614    }
2615
2616    let previous_mtime = t!(t!(path.metadata()).modified());
2617    let stamp = BuildStamp::new(path.parent().unwrap())
2618        .with_prefix(path.file_name().unwrap().to_str().unwrap())
2619        .with_prefix("strip")
2620        .add_stamp(previous_mtime.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos());
2621
2622    // Running strip can be relatively expensive (~1s on librustc_driver.so), so we don't rerun it
2623    // if the file is unchanged.
2624    if !stamp.is_up_to_date() {
2625        command("strip").arg("--strip-debug").arg(path).run_capture(builder);
2626    }
2627    t!(stamp.write());
2628
2629    let file = t!(fs::File::open(path));
2630
2631    // After running `strip`, we have to set the file modification time to what it was before,
2632    // otherwise we risk Cargo invalidating its fingerprint and rebuilding the world next time
2633    // bootstrap is invoked.
2634    //
2635    // An example of this is if we run this on librustc_driver.so. In the first invocation:
2636    // - Cargo will build librustc_driver.so (mtime of 1)
2637    // - Cargo will build rustc-main (mtime of 2)
2638    // - Bootstrap will strip librustc_driver.so (changing the mtime to 3).
2639    //
2640    // In the second invocation of bootstrap, Cargo will see that the mtime of librustc_driver.so
2641    // is greater than the mtime of rustc-main, and will rebuild rustc-main. That will then cause
2642    // everything else (standard library, future stages...) to be rebuilt.
2643    t!(file.set_modified(previous_mtime));
2644}
2645
2646/// We only use LTO for stage 2+, to speed up build time of intermediate stages.
2647pub fn is_lto_stage(build_compiler: &Compiler) -> bool {
2648    build_compiler.stage != 0
2649}