bootstrap/core/build_steps/
compile.rs

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