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