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