Skip to main content

bootstrap/
lib.rs

1//! Implementation of bootstrap, the Rust build system.
2//!
3//! This module, and its descendants, are the implementation of the Rust build
4//! system. Most of this build system is backed by Cargo but the outer layer
5//! here serves as the ability to orchestrate calling Cargo, sequencing Cargo
6//! builds, building artifacts like LLVM, etc. The goals of bootstrap are:
7//!
8//! * To be an easily understandable, easily extensible, and maintainable build
9//!   system.
10//! * Leverage standard tools in the Rust ecosystem to build the compiler, aka
11//!   crates.io and Cargo.
12//! * A standard interface to build across all platforms, including MSVC
13//!
14//! ## Further information
15//!
16//! More documentation can be found in each respective module below, and you can
17//! also check out the `src/bootstrap/README.md` file for more information.
18#![cfg_attr(test, allow(unused))]
19
20use std::cell::Cell;
21use std::collections::{BTreeSet, HashMap, HashSet};
22use std::fmt::Display;
23use std::path::{Path, PathBuf};
24use std::sync::OnceLock;
25use std::time::{Instant, SystemTime};
26use std::{env, fs, io, str};
27
28use build_helper::ci::gha;
29use build_helper::exit;
30use cc::Tool;
31use termcolor::{ColorChoice, StandardStream, WriteColor};
32use utils::build_stamp::BuildStamp;
33use utils::channel::GitInfo;
34use utils::exec::ExecutionContext;
35
36use crate::core::builder;
37use crate::core::builder::Kind;
38use crate::core::config::{BootstrapOverrideLld, DryRun, LlvmLibunwind, TargetSelection, flags};
39use crate::utils::exec::{BootstrapCommand, command};
40use crate::utils::helpers::{self, dir_is_empty, exe, libdir, set_file_times, split_debuginfo};
41
42mod core;
43mod utils;
44
45pub use core::builder::PathSet;
46#[cfg(feature = "tracing")]
47pub use core::builder::STEP_SPAN_TARGET;
48pub use core::config::flags::{Flags, Subcommand};
49pub use core::config::{ChangeId, Config};
50
51#[cfg(feature = "tracing")]
52use tracing::{instrument, span};
53pub use utils::change_tracker::{
54    CONFIG_CHANGE_HISTORY, find_recent_config_change_ids, human_readable_changes,
55};
56pub use utils::helpers::{PanicTracker, symlink_dir};
57#[cfg(feature = "tracing")]
58pub use utils::tracing::setup_tracing;
59
60use crate::core::build_steps::vendor::VENDOR_DIR;
61
62const LLVM_TOOLS: &[&str] = &[
63    "llvm-cov",      // used to generate coverage report
64    "llvm-nm",       // used to inspect binaries; it shows symbol names, their sizes and visibility
65    "llvm-objcopy",  // used to transform ELFs into binary format which flashing tools consume
66    "llvm-objdump",  // used to disassemble programs
67    "llvm-profdata", // used to inspect and merge files generated by profiles
68    "llvm-readobj",  // used to get information from ELFs/objects that the other tools don't provide
69    "llvm-size",     // used to prints the size of the linker sections of a program
70    "llvm-strip",    // used to discard symbols from binary files to reduce their size
71    "llvm-ar",       // used for creating and modifying archive files
72    "llvm-as",       // used to convert LLVM assembly to LLVM bitcode
73    "llvm-dis",      // used to disassemble LLVM bitcode
74    "llvm-link",     // Used to link LLVM bitcode
75    "llc",           // used to compile LLVM bytecode
76    "opt",           // used to optimize LLVM bytecode
77];
78
79/// LLD file names for all flavors.
80const LLD_FILE_NAMES: &[&str] = &["ld.lld", "ld64.lld", "lld-link", "wasm-ld"];
81
82/// Extra `--check-cfg` to add when building the compiler or tools
83/// (Mode restriction, config name, config values (if any))
84#[expect(clippy::type_complexity)] // It's fine for hard-coded list and type is explained above.
85const EXTRA_CHECK_CFGS: &[(Option<Mode>, &str, Option<&[&'static str]>)] = &[
86    (Some(Mode::Rustc), "bootstrap", None),
87    (Some(Mode::Codegen), "bootstrap", None),
88    (Some(Mode::ToolRustcPrivate), "bootstrap", None),
89    (Some(Mode::ToolStd), "bootstrap", None),
90    (Some(Mode::ToolRustcPrivate), "rust_analyzer", None),
91    (Some(Mode::ToolStd), "rust_analyzer", None),
92    // Any library specific cfgs like `target_os`, `target_arch` should be put in
93    // priority the `[lints.rust.unexpected_cfgs.check-cfg]` table
94    // in the appropriate `library/{std,alloc,core}/Cargo.toml`
95];
96
97/// A structure representing a Rust compiler.
98///
99/// Each compiler has a `stage` that it is associated with and a `host` that
100/// corresponds to the platform the compiler runs on. This structure is used as
101/// a parameter to many methods below.
102#[derive(Eq, PartialOrd, Ord, Clone, Copy, Debug)]
103pub struct Compiler {
104    stage: u32,
105    host: TargetSelection,
106    /// Indicates whether the compiler was forced to use a specific stage.
107    /// This field is ignored in `Hash` and `PartialEq` implementations as only the `stage`
108    /// and `host` fields are relevant for those.
109    forced_compiler: bool,
110}
111
112impl std::hash::Hash for Compiler {
113    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
114        self.stage.hash(state);
115        self.host.hash(state);
116    }
117}
118
119impl PartialEq for Compiler {
120    fn eq(&self, other: &Self) -> bool {
121        self.stage == other.stage && self.host == other.host
122    }
123}
124
125/// Represents a codegen backend.
126#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
127pub enum CodegenBackendKind {
128    #[default]
129    Llvm,
130    Cranelift,
131    Gcc,
132    Custom(String),
133}
134
135impl CodegenBackendKind {
136    /// Name of the codegen backend, as identified in the `compiler` directory
137    /// (`rustc_codegen_<name>`).
138    pub fn name(&self) -> &str {
139        match self {
140            CodegenBackendKind::Llvm => "llvm",
141            CodegenBackendKind::Cranelift => "cranelift",
142            CodegenBackendKind::Gcc => "gcc",
143            CodegenBackendKind::Custom(name) => name,
144        }
145    }
146
147    /// Name of the codegen backend's crate, e.g. `rustc_codegen_cranelift`.
148    pub fn crate_name(&self) -> String {
149        format!("rustc_codegen_{}", self.name())
150    }
151
152    pub fn is_llvm(&self) -> bool {
153        matches!(self, Self::Llvm)
154    }
155
156    pub fn is_cranelift(&self) -> bool {
157        matches!(self, Self::Cranelift)
158    }
159
160    pub fn is_gcc(&self) -> bool {
161        matches!(self, Self::Gcc)
162    }
163}
164
165impl std::str::FromStr for CodegenBackendKind {
166    type Err = &'static str;
167
168    fn from_str(s: &str) -> Result<Self, Self::Err> {
169        match s.to_lowercase().as_str() {
170            "" => Err("Invalid empty backend name"),
171            "gcc" => Ok(Self::Gcc),
172            "llvm" => Ok(Self::Llvm),
173            "cranelift" => Ok(Self::Cranelift),
174            _ => Ok(Self::Custom(s.to_string())),
175        }
176    }
177}
178
179#[derive(PartialEq, Eq, Copy, Clone, Debug)]
180pub enum TestTarget {
181    /// Run unit, integration and doc tests (default).
182    Default,
183    /// Run unit, integration, doc tests, examples, bins, benchmarks (no doc tests).
184    AllTargets,
185    /// Only run doc tests.
186    DocOnly,
187    /// Only run unit and integration tests.
188    Tests,
189}
190
191impl TestTarget {
192    fn runs_doctests(&self) -> bool {
193        matches!(self, TestTarget::DocOnly | TestTarget::Default)
194    }
195}
196
197pub enum GitRepo {
198    Rustc,
199    Llvm,
200}
201
202/// Global configuration for the build system.
203///
204/// This structure transitively contains all configuration for the build system.
205/// All filesystem-encoded configuration is in `config`, all flags are in
206/// `flags`, and then parsed or probed information is listed in the keys below.
207///
208/// This structure is a parameter of almost all methods in the build system,
209/// although most functions are implemented as free functions rather than
210/// methods specifically on this structure itself (to make it easier to
211/// organize).
212pub struct Build {
213    /// User-specified configuration from `bootstrap.toml`.
214    config: Config,
215
216    // Version information
217    version: String,
218
219    // Properties derived from the above configuration
220    src: PathBuf,
221    out: PathBuf,
222    bootstrap_out: PathBuf,
223    cargo_info: GitInfo,
224    rust_analyzer_info: GitInfo,
225    clippy_info: GitInfo,
226    miri_info: GitInfo,
227    rustfmt_info: GitInfo,
228    enzyme_info: GitInfo,
229    in_tree_llvm_info: GitInfo,
230    in_tree_gcc_info: GitInfo,
231    local_rebuild: bool,
232    fail_fast: bool,
233    test_target: TestTarget,
234    verbosity: usize,
235
236    /// Build triple for the pre-compiled snapshot compiler.
237    host_target: TargetSelection,
238    /// Which triples to produce a compiler toolchain for.
239    hosts: Vec<TargetSelection>,
240    /// Which triples to build libraries (core/alloc/std/test/proc_macro) for.
241    targets: Vec<TargetSelection>,
242
243    initial_rustc: PathBuf,
244    initial_rustdoc: PathBuf,
245    initial_cargo: PathBuf,
246    initial_lld: PathBuf,
247    initial_relative_libdir: PathBuf,
248    initial_sysroot: PathBuf,
249
250    // Runtime state filled in later on
251    // C/C++ compilers and archiver for all targets
252    cc: HashMap<TargetSelection, cc::Tool>,
253    cxx: HashMap<TargetSelection, cc::Tool>,
254    ar: HashMap<TargetSelection, PathBuf>,
255    ranlib: HashMap<TargetSelection, PathBuf>,
256    wasi_sdk_path: Option<PathBuf>,
257
258    // Miscellaneous
259    // allow bidirectional lookups: both name -> path and path -> name
260    crates: HashMap<String, Crate>,
261    crate_paths: HashMap<PathBuf, String>,
262    is_sudo: bool,
263    prerelease_version: Cell<Option<u32>>,
264
265    #[cfg(feature = "build-metrics")]
266    metrics: crate::utils::metrics::BuildMetrics,
267
268    #[cfg(feature = "tracing")]
269    step_graph: std::cell::RefCell<crate::utils::step_graph::StepGraph>,
270}
271
272#[derive(Debug, Clone)]
273struct Crate {
274    name: String,
275    deps: HashSet<String>,
276    path: PathBuf,
277    features: Vec<String>,
278}
279
280impl Crate {
281    fn local_path(&self, build: &Build) -> PathBuf {
282        self.path.strip_prefix(&build.config.src).unwrap().into()
283    }
284}
285
286/// When building Rust various objects are handled differently.
287#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
288pub enum DependencyType {
289    /// Libraries originating from proc-macros.
290    Host,
291    /// Typical Rust libraries.
292    Target,
293    /// Non Rust libraries and objects shipped to ease usage of certain targets.
294    TargetSelfContained,
295}
296
297/// The various "modes" of invoking Cargo.
298///
299/// These entries currently correspond to the various output directories of the
300/// build system, with each mod generating output in a different directory.
301#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
302pub enum Mode {
303    /// Build the standard library, placing output in the "stageN-std" directory.
304    Std,
305
306    /// Build librustc, and compiler libraries, placing output in the "stageN-rustc" directory.
307    Rustc,
308
309    /// Build a codegen backend for rustc, placing the output in the "stageN-codegen" directory.
310    Codegen,
311
312    /// Build a tool, placing output in the "bootstrap-tools"
313    /// directory. This is for miscellaneous sets of tools that extend
314    /// bootstrap.
315    ///
316    /// These tools are intended to be only executed on the host system that
317    /// invokes bootstrap, and they thus cannot be cross-compiled.
318    ///
319    /// They are always built using the stage0 compiler, and they
320    /// can be compiled with stable Rust.
321    ///
322    /// These tools also essentially do not participate in staging.
323    ToolBootstrap,
324
325    /// Build a cross-compilable helper tool. These tools do not depend on unstable features or
326    /// compiler internals, but they might be cross-compilable (so we cannot build them using the
327    /// stage0 compiler, unlike `ToolBootstrap`).
328    ///
329    /// Some of these tools are also shipped in our `dist` archives.
330    /// While we could compile them using the stage0 compiler when not cross-compiling, we instead
331    /// use the in-tree compiler (and std) to build them, so that we can ship e.g. std security
332    /// fixes and avoid depending fully on stage0 for the artifacts that we ship.
333    ///
334    /// This mode is used e.g. for linkers and linker tools invoked by rustc on its host target.
335    ToolTarget,
336
337    /// Build a tool which uses the locally built std, placing output in the
338    /// "stageN-tools" directory. Its usage is quite rare; historically it was
339    /// needed by compiletest, but now it is mainly used by `test-float-parse`.
340    ToolStd,
341
342    /// Build a tool which uses the `rustc_private` mechanism, and thus
343    /// the locally built rustc rlib artifacts,
344    /// placing the output in the "stageN-tools" directory. This is used for
345    /// everything that links to rustc as a library, such as rustdoc, clippy,
346    /// rustfmt, miri, etc.
347    ToolRustcPrivate,
348}
349
350impl Mode {
351    pub fn must_support_dlopen(&self) -> bool {
352        match self {
353            Mode::Std | Mode::Codegen => true,
354            Mode::ToolBootstrap
355            | Mode::ToolRustcPrivate
356            | Mode::ToolStd
357            | Mode::ToolTarget
358            | Mode::Rustc => false,
359        }
360    }
361}
362
363/// When `rust.rust_remap_debuginfo` is requested, the compiler needs to know how to
364/// opportunistically unremap compiler vs non-compiler sources. We use two schemes,
365/// [`RemapScheme::Compiler`] and [`RemapScheme::NonCompiler`].
366pub enum RemapScheme {
367    /// The [`RemapScheme::Compiler`] scheme will remap to `/rustc-dev/{hash}`.
368    Compiler,
369    /// The [`RemapScheme::NonCompiler`] scheme will remap to `/rustc/{hash}`.
370    NonCompiler,
371}
372
373#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
374pub enum CLang {
375    C,
376    Cxx,
377}
378
379#[derive(Debug, Clone, Copy, PartialEq, Eq)]
380pub enum FileType {
381    /// An executable binary file (like a `.exe`).
382    Executable,
383    /// A native, binary library file (like a `.so`, `.dll`, `.a`, `.lib` or `.o`).
384    NativeLibrary,
385    /// An executable (non-binary) script file (like a `.py` or `.sh`).
386    Script,
387    /// Any other regular file that is non-executable.
388    Regular,
389}
390
391impl FileType {
392    /// Get Unix permissions appropriate for this file type.
393    pub fn perms(self) -> u32 {
394        match self {
395            FileType::Executable | FileType::Script => 0o755,
396            FileType::Regular | FileType::NativeLibrary => 0o644,
397        }
398    }
399
400    pub fn could_have_split_debuginfo(self) -> bool {
401        match self {
402            FileType::Executable | FileType::NativeLibrary => true,
403            FileType::Script | FileType::Regular => false,
404        }
405    }
406}
407
408macro_rules! forward {
409    ( $( $fn:ident( $($param:ident: $ty:ty),* ) $( -> $ret:ty)? ),+ $(,)? ) => {
410        impl Build {
411            $( fn $fn(&self, $($param: $ty),* ) $( -> $ret)? {
412                self.config.$fn( $($param),* )
413            } )+
414        }
415    }
416}
417
418forward! {
419    do_if_verbose(f: impl Fn()),
420    is_verbose() -> bool,
421    create(path: &Path, s: &str),
422    remove(f: &Path),
423    tempdir() -> PathBuf,
424    llvm_link_shared() -> bool,
425    download_rustc() -> bool,
426}
427
428/// An alternative way of specifying what target and stage is involved in some bootstrap activity.
429/// Ideally using a `Compiler` directly should be preferred.
430struct TargetAndStage {
431    target: TargetSelection,
432    stage: u32,
433}
434
435impl From<(TargetSelection, u32)> for TargetAndStage {
436    fn from((target, stage): (TargetSelection, u32)) -> Self {
437        Self { target, stage }
438    }
439}
440
441impl From<Compiler> for TargetAndStage {
442    fn from(compiler: Compiler) -> Self {
443        Self { target: compiler.host, stage: compiler.stage }
444    }
445}
446
447impl Build {
448    /// Creates a new set of build configuration from the `flags` on the command
449    /// line and the filesystem `config`.
450    ///
451    /// By default all build output will be placed in the current directory.
452    pub fn new(mut config: Config) -> Build {
453        let src = config.src.clone();
454        let out = config.out.clone();
455
456        #[cfg(unix)]
457        // keep this consistent with the equivalent check in x.py:
458        // https://github.com/rust-lang/rust/blob/a8a33cf27166d3eabaffc58ed3799e054af3b0c6/src/bootstrap/bootstrap.py#L796-L797
459        let is_sudo = match env::var_os("SUDO_USER") {
460            Some(_sudo_user) => {
461                // SAFETY: getuid() system call is always successful and no return value is reserved
462                // to indicate an error.
463                //
464                // For more context, see https://man7.org/linux/man-pages/man2/geteuid.2.html
465                let uid = unsafe { libc::getuid() };
466                uid == 0
467            }
468            None => false,
469        };
470        #[cfg(not(unix))]
471        let is_sudo = false;
472
473        let rust_info = config.rust_info.clone();
474        let cargo_info = config.cargo_info.clone();
475        let rust_analyzer_info = config.rust_analyzer_info.clone();
476        let clippy_info = config.clippy_info.clone();
477        let miri_info = config.miri_info.clone();
478        let rustfmt_info = config.rustfmt_info.clone();
479        let enzyme_info = config.enzyme_info.clone();
480        let in_tree_llvm_info = config.in_tree_llvm_info.clone();
481        let in_tree_gcc_info = config.in_tree_gcc_info.clone();
482
483        let initial_target_libdir = command(&config.initial_rustc)
484            .run_in_dry_run()
485            .args(["--print", "target-libdir"])
486            .run_capture_stdout(&config)
487            .stdout()
488            .trim()
489            .to_owned();
490
491        let initial_target_dir = Path::new(&initial_target_libdir)
492            .parent()
493            .unwrap_or_else(|| panic!("{initial_target_libdir} has no parent"));
494
495        let initial_lld = initial_target_dir.join("bin").join("rust-lld");
496
497        let initial_relative_libdir = if cfg!(test) {
498            // On tests, bootstrap uses the shim rustc, not the one from the stage0 toolchain.
499            PathBuf::default()
500        } else {
501            let ancestor = initial_target_dir.ancestors().nth(2).unwrap_or_else(|| {
502                panic!("Not enough ancestors for {}", initial_target_dir.display())
503            });
504
505            ancestor
506                .strip_prefix(&config.initial_sysroot)
507                .unwrap_or_else(|_| {
508                    panic!(
509                        "Couldn’t resolve the initial relative libdir from {}",
510                        initial_target_dir.display()
511                    )
512                })
513                .to_path_buf()
514        };
515
516        let version = std::fs::read_to_string(src.join("src").join("version"))
517            .expect("failed to read src/version");
518        let version = version.trim();
519
520        let mut bootstrap_out = std::env::current_exe()
521            .expect("could not determine path to running process")
522            .parent()
523            .unwrap()
524            .to_path_buf();
525        // Since bootstrap is hardlink to deps/bootstrap-*, Solaris can sometimes give
526        // path with deps/ which is bad and needs to be avoided.
527        if bootstrap_out.ends_with("deps") {
528            bootstrap_out.pop();
529        }
530        if !bootstrap_out.join(exe("rustc", config.host_target)).exists() && !cfg!(test) {
531            // this restriction can be lifted whenever https://github.com/rust-lang/rfcs/pull/3028 is implemented
532            panic!(
533                "`rustc` not found in {}, run `cargo build --bins` before `cargo run`",
534                bootstrap_out.display()
535            )
536        }
537
538        if rust_info.is_from_tarball() && config.description.is_none() {
539            config.description = Some("built from a source tarball".to_owned());
540        }
541
542        let mut build = Build {
543            initial_lld,
544            initial_relative_libdir,
545            initial_rustc: config.initial_rustc.clone(),
546            initial_rustdoc: config.initial_rustdoc.clone(),
547            initial_cargo: config.initial_cargo.clone(),
548            initial_sysroot: config.initial_sysroot.clone(),
549            local_rebuild: config.local_rebuild,
550            fail_fast: config.cmd.fail_fast(),
551            test_target: config.cmd.test_target(),
552            verbosity: config.exec_ctx.verbosity as usize,
553
554            host_target: config.host_target,
555            hosts: config.hosts.clone(),
556            targets: config.targets.clone(),
557
558            config,
559            version: version.to_string(),
560            src,
561            out,
562            bootstrap_out,
563
564            cargo_info,
565            rust_analyzer_info,
566            clippy_info,
567            miri_info,
568            rustfmt_info,
569            enzyme_info,
570            in_tree_llvm_info,
571            in_tree_gcc_info,
572            cc: HashMap::new(),
573            cxx: HashMap::new(),
574            ar: HashMap::new(),
575            ranlib: HashMap::new(),
576            wasi_sdk_path: env::var_os("WASI_SDK_PATH").map(PathBuf::from),
577            crates: HashMap::new(),
578            crate_paths: HashMap::new(),
579            is_sudo,
580            prerelease_version: Cell::new(None),
581
582            #[cfg(feature = "build-metrics")]
583            metrics: crate::utils::metrics::BuildMetrics::init(),
584
585            #[cfg(feature = "tracing")]
586            step_graph: std::cell::RefCell::new(crate::utils::step_graph::StepGraph::default()),
587        };
588
589        // If local-rust is the same major.minor as the current version, then force a
590        // local-rebuild
591        let local_version_verbose = command(&build.initial_rustc)
592            .run_in_dry_run()
593            .args(["--version", "--verbose"])
594            .run_capture_stdout(&build)
595            .stdout();
596        let local_release = local_version_verbose
597            .lines()
598            .filter_map(|x| x.strip_prefix("release:"))
599            .next()
600            .unwrap()
601            .trim();
602        if local_release.split('.').take(2).eq(version.split('.').take(2)) {
603            build.do_if_verbose(|| println!("auto-detected local-rebuild {local_release}"));
604            build.local_rebuild = true;
605        }
606
607        build.do_if_verbose(|| println!("finding compilers"));
608        utils::cc_detect::fill_compilers(&mut build);
609        // When running `setup`, the profile is about to change, so any requirements we have now may
610        // be different on the next invocation. Don't check for them until the next time x.py is
611        // run. This is ok because `setup` never runs any build commands, so it won't fail if commands are missing.
612        //
613        // Similarly, for `setup` we don't actually need submodules or cargo metadata.
614        if !matches!(build.config.cmd, Subcommand::Setup { .. }) {
615            build.do_if_verbose(|| println!("running sanity check"));
616            crate::core::sanity::check(&mut build);
617
618            // Make sure we update these before gathering metadata so we don't get an error about missing
619            // Cargo.toml files.
620            let rust_submodules = ["library/backtrace"];
621            for s in rust_submodules {
622                build.require_submodule(
623                    s,
624                    Some(
625                        "The submodule is required for the standard library \
626                         and the main Cargo workspace.",
627                    ),
628                );
629            }
630            // Now, update all existing submodules.
631            build.update_existing_submodules();
632
633            build.do_if_verbose(|| println!("learning about cargo"));
634            crate::core::metadata::build(&mut build);
635        }
636
637        // Create symbolic link to use host sysroot from a consistent path (e.g., in the rust-analyzer config file).
638        let build_triple = build.out.join(build.host_target);
639        t!(fs::create_dir_all(&build_triple));
640        let host = build.out.join("host");
641        if host.is_symlink() {
642            // Left over from a previous build; overwrite it.
643            // This matters if `build.build` has changed between invocations.
644            #[cfg(windows)]
645            t!(fs::remove_dir(&host));
646            #[cfg(not(windows))]
647            t!(fs::remove_file(&host));
648        }
649        t!(
650            symlink_dir(&build.config, &build_triple, &host),
651            format!("symlink_dir({} => {}) failed", host.display(), build_triple.display())
652        );
653
654        build
655    }
656
657    /// Updates a submodule, and exits with a failure if submodule management
658    /// is disabled and the submodule does not exist.
659    ///
660    /// The given submodule name should be its path relative to the root of
661    /// the main repository.
662    ///
663    /// The given `err_hint` will be shown to the user if the submodule is not
664    /// checked out and submodule management is disabled.
665    #[cfg_attr(
666        feature = "tracing",
667        instrument(
668            level = "trace",
669            name = "Build::require_submodule",
670            skip_all,
671            fields(submodule = submodule),
672        ),
673    )]
674    pub fn require_submodule(&self, submodule: &str, err_hint: Option<&str>) {
675        if self.rust_info().is_from_tarball() {
676            return;
677        }
678
679        if self.config.dry_run() {
680            return;
681        }
682
683        // When testing bootstrap itself, it is much faster to ignore
684        // submodules. Almost all Steps work fine without their submodules.
685        if cfg!(test) && !self.config.submodules() {
686            return;
687        }
688        self.config.update_submodule(submodule);
689        let absolute_path = self.config.src.join(submodule);
690        if !absolute_path.exists() || dir_is_empty(&absolute_path) {
691            let maybe_enable = if !self.config.submodules()
692                && self.config.rust_info.is_managed_git_subrepository()
693            {
694                "\nConsider setting `build.submodules = true` or manually initializing the submodules."
695            } else {
696                ""
697            };
698            let err_hint = err_hint.map_or_else(String::new, |e| format!("\n{e}"));
699            eprintln!(
700                "submodule {submodule} does not appear to be checked out, \
701                 but it is required for this step{maybe_enable}{err_hint}"
702            );
703            exit!(1);
704        }
705    }
706
707    /// If any submodule has been initialized already, sync it unconditionally.
708    /// This avoids contributors checking in a submodule change by accident.
709    fn update_existing_submodules(&self) {
710        // Avoid running git when there isn't a git checkout, or the user has
711        // explicitly disabled submodules in `bootstrap.toml`.
712        if !self.config.submodules() {
713            return;
714        }
715        let output = helpers::git(Some(&self.src))
716            .args(["config", "--file"])
717            .arg(".gitmodules")
718            .args(["--get-regexp", "path"])
719            .run_capture(self)
720            .stdout();
721        std::thread::scope(|s| {
722            // Look for `submodule.$name.path = $path`
723            // Sample output: `submodule.src/rust-installer.path src/tools/rust-installer`
724            for line in output.lines() {
725                let submodule = line.split_once(' ').unwrap().1;
726                let config = self.config.clone();
727                s.spawn(move || {
728                    Self::update_existing_submodule(&config, submodule);
729                });
730            }
731        });
732    }
733
734    /// Updates the given submodule only if it's initialized already; nothing happens otherwise.
735    pub fn update_existing_submodule(config: &Config, submodule: &str) {
736        // Avoid running git when there isn't a git checkout.
737        if !config.submodules() {
738            return;
739        }
740
741        if config.git_info(false, Path::new(submodule)).is_managed_git_subrepository() {
742            config.update_submodule(submodule);
743        }
744    }
745
746    /// Executes the entire build, as configured by the flags and configuration.
747    #[cfg_attr(feature = "tracing", instrument(level = "debug", name = "Build::build", skip_all))]
748    pub fn build(&mut self) {
749        trace!("setting up job management");
750        unsafe {
751            crate::utils::job::setup(self);
752        }
753
754        // Handle hard-coded subcommands.
755        {
756            #[cfg(feature = "tracing")]
757            let _hardcoded_span =
758                span!(tracing::Level::DEBUG, "handling hardcoded subcommands (Format, Perf)")
759                    .entered();
760
761            match &self.config.cmd {
762                Subcommand::Format { check, all } => {
763                    return core::build_steps::format::format(
764                        &builder::Builder::new(self),
765                        *check,
766                        *all,
767                        &self.config.paths,
768                    );
769                }
770                Subcommand::Perf(args) => {
771                    return core::build_steps::perf::perf(&builder::Builder::new(self), args);
772                }
773                _cmd => {
774                    debug!(cmd = ?_cmd, "not a hardcoded subcommand; returning to normal handling");
775                }
776            }
777
778            debug!("handling subcommand normally");
779        }
780
781        if !self.config.dry_run() {
782            #[cfg(feature = "tracing")]
783            let _real_run_span = span!(tracing::Level::DEBUG, "executing real run").entered();
784
785            // We first do a dry-run. This is a sanity-check to ensure that
786            // steps don't do anything expensive in the dry-run.
787            {
788                #[cfg(feature = "tracing")]
789                let _sanity_check_span =
790                    span!(tracing::Level::DEBUG, "(1) executing dry-run sanity-check").entered();
791                self.config.set_dry_run(DryRun::SelfCheck);
792                let builder = builder::Builder::new(self);
793                builder.execute_cli();
794            }
795
796            // Actual run.
797            {
798                #[cfg(feature = "tracing")]
799                let _actual_run_span =
800                    span!(tracing::Level::DEBUG, "(2) executing actual run").entered();
801                self.config.set_dry_run(DryRun::Disabled);
802                let builder = builder::Builder::new(self);
803                builder.execute_cli();
804            }
805        } else {
806            #[cfg(feature = "tracing")]
807            let _dry_run_span = span!(tracing::Level::DEBUG, "executing dry run").entered();
808
809            let builder = builder::Builder::new(self);
810            builder.execute_cli();
811        }
812
813        #[cfg(feature = "tracing")]
814        debug!("checking for postponed test failures from `test  --no-fail-fast`");
815
816        // Check for postponed failures from `test --no-fail-fast`.
817        self.config.exec_ctx().report_failures_and_exit();
818
819        #[cfg(feature = "build-metrics")]
820        self.metrics.persist(self);
821    }
822
823    fn rust_info(&self) -> &GitInfo {
824        &self.config.rust_info
825    }
826
827    /// Gets the space-separated set of activated features for the standard library.
828    /// This can be configured with the `std-features` key in bootstrap.toml.
829    fn std_features(&self, target: TargetSelection) -> String {
830        let mut features: BTreeSet<&str> =
831            self.config.rust_std_features.iter().map(|s| s.as_str()).collect();
832
833        match self.config.llvm_libunwind(target) {
834            LlvmLibunwind::InTree => features.insert("llvm-libunwind"),
835            LlvmLibunwind::System => features.insert("system-llvm-libunwind"),
836            LlvmLibunwind::No => false,
837        };
838
839        if self.config.backtrace {
840            features.insert("backtrace");
841        }
842
843        if self.config.profiler_enabled(target) {
844            features.insert("profiler");
845        }
846
847        // If zkvm target, generate memcpy, etc.
848        if target.contains("zkvm") {
849            features.insert("compiler-builtins-mem");
850        }
851
852        if self.config.llvm_enzyme {
853            features.insert("llvm_enzyme");
854        }
855
856        features.into_iter().collect::<Vec<_>>().join(" ")
857    }
858
859    /// Gets the space-separated set of activated features for the compiler.
860    fn rustc_features(&self, kind: Kind, target: TargetSelection, crates: &[String]) -> String {
861        let possible_features_by_crates: HashSet<_> = crates
862            .iter()
863            .flat_map(|krate| &self.crates[krate].features)
864            .map(std::ops::Deref::deref)
865            .collect();
866        let check = |feature: &str| -> bool {
867            crates.is_empty() || possible_features_by_crates.contains(feature)
868        };
869        let mut features = vec![];
870        if self.config.jemalloc(target) && check("jemalloc") {
871            features.push("jemalloc");
872        }
873        if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
874            features.push("llvm");
875        }
876        if self.config.llvm_enzyme {
877            features.push("llvm_enzyme");
878        }
879        if self.config.llvm_offload {
880            features.push("llvm_offload");
881        }
882        // keep in sync with `bootstrap/compile.rs:rustc_cargo_env`
883        if self.config.rust_randomize_layout && check("rustc_randomized_layouts") {
884            features.push("rustc_randomized_layouts");
885        }
886        if self.config.compile_time_deps && kind == Kind::Check {
887            features.push("check_only");
888        }
889
890        if crates.iter().any(|c| c == "rustc_transmute") {
891            // for `x test rustc_transmute`, this feature isn't enabled automatically by a
892            // dependent crate.
893            features.push("rustc");
894        }
895
896        // If debug logging is on, then we want the default for tracing:
897        // https://github.com/tokio-rs/tracing/blob/3dd5c03d907afdf2c39444a29931833335171554/tracing/src/level_filters.rs#L26
898        // which is everything (including debug/trace/etc.)
899        // if its unset, if debug_assertions is on, then debug_logging will also be on
900        // as well as tracing *ignoring* this feature when debug_assertions is on
901        if !self.config.rust_debug_logging && check("max_level_info") {
902            features.push("max_level_info");
903        }
904
905        features.join(" ")
906    }
907
908    /// Component directory that Cargo will produce output into (e.g.
909    /// release/debug)
910    fn cargo_dir(&self, mode: Mode) -> &'static str {
911        match (mode, self.config.rust_optimize.is_release()) {
912            (Mode::Std, _) => "dist",
913            (_, true) => "release",
914            (_, false) => "debug",
915        }
916    }
917
918    fn tools_dir(&self, build_compiler: Compiler) -> PathBuf {
919        let out = self
920            .out
921            .join(build_compiler.host)
922            .join(format!("stage{}-tools-bin", build_compiler.stage + 1));
923        t!(fs::create_dir_all(&out));
924        out
925    }
926
927    /// Returns the root directory for all output generated in a particular
928    /// stage when being built with a particular build compiler.
929    ///
930    /// The mode indicates what the root directory is for.
931    fn stage_out(&self, build_compiler: Compiler, mode: Mode) -> PathBuf {
932        use std::fmt::Write;
933
934        fn bootstrap_tool() -> (Option<u32>, &'static str) {
935            (None, "bootstrap-tools")
936        }
937        fn staged_tool(build_compiler: Compiler) -> (Option<u32>, &'static str) {
938            (Some(build_compiler.stage + 1), "tools")
939        }
940
941        let (stage, suffix) = match mode {
942            // Std is special, stage N std is built with stage N rustc
943            Mode::Std => (Some(build_compiler.stage), "std"),
944            // The rest of things are built with stage N-1 rustc
945            Mode::Rustc => (Some(build_compiler.stage + 1), "rustc"),
946            Mode::Codegen => (Some(build_compiler.stage + 1), "codegen"),
947            Mode::ToolBootstrap => bootstrap_tool(),
948            Mode::ToolStd | Mode::ToolRustcPrivate => (Some(build_compiler.stage + 1), "tools"),
949            Mode::ToolTarget => {
950                // If we're not cross-compiling (the common case), share the target directory with
951                // bootstrap tools to reuse the build cache.
952                if build_compiler.stage == 0 {
953                    bootstrap_tool()
954                } else {
955                    staged_tool(build_compiler)
956                }
957            }
958        };
959        let path = self.out.join(build_compiler.host);
960        let mut dir_name = String::new();
961        if let Some(stage) = stage {
962            write!(dir_name, "stage{stage}-").unwrap();
963        }
964        dir_name.push_str(suffix);
965        path.join(dir_name)
966    }
967
968    /// Returns the root output directory for all Cargo output in a given stage,
969    /// running a particular compiler, whether or not we're building the
970    /// standard library, and targeting the specified architecture.
971    fn cargo_out(&self, build_compiler: Compiler, mode: Mode, target: TargetSelection) -> PathBuf {
972        self.stage_out(build_compiler, mode).join(target).join(self.cargo_dir(mode))
973    }
974
975    /// Root output directory of LLVM for `target`
976    ///
977    /// Note that if LLVM is configured externally then the directory returned
978    /// will likely be empty.
979    fn llvm_out(&self, target: TargetSelection) -> PathBuf {
980        if self.config.llvm_from_ci && self.config.is_host_target(target) {
981            self.config.ci_llvm_root()
982        } else {
983            self.out.join(target).join("llvm")
984        }
985    }
986
987    fn enzyme_out(&self, target: TargetSelection) -> PathBuf {
988        self.out.join(&*target.triple).join("enzyme")
989    }
990
991    fn offload_out(&self, target: TargetSelection) -> PathBuf {
992        self.out.join(&*target.triple).join("offload")
993    }
994
995    fn lld_out(&self, target: TargetSelection) -> PathBuf {
996        self.out.join(target).join("lld")
997    }
998
999    /// Output directory for all documentation for a target
1000    fn doc_out(&self, target: TargetSelection) -> PathBuf {
1001        self.out.join(target).join("doc")
1002    }
1003
1004    /// Output directory for all JSON-formatted documentation for a target
1005    fn json_doc_out(&self, target: TargetSelection) -> PathBuf {
1006        self.out.join(target).join("json-doc")
1007    }
1008
1009    fn test_out(&self, target: TargetSelection) -> PathBuf {
1010        self.out.join(target).join("test")
1011    }
1012
1013    /// Output directory for all documentation for a target
1014    fn compiler_doc_out(&self, target: TargetSelection) -> PathBuf {
1015        self.out.join(target).join("compiler-doc")
1016    }
1017
1018    /// Output directory for some generated md crate documentation for a target (temporary)
1019    fn md_doc_out(&self, target: TargetSelection) -> PathBuf {
1020        self.out.join(target).join("md-doc")
1021    }
1022
1023    /// Path to the vendored Rust crates.
1024    fn vendored_crates_path(&self) -> Option<PathBuf> {
1025        if self.config.vendor { Some(self.src.join(VENDOR_DIR)) } else { None }
1026    }
1027
1028    /// Returns the path to `FileCheck` binary for the specified target
1029    fn llvm_filecheck(&self, target: TargetSelection) -> PathBuf {
1030        let target_config = self.config.target_config.get(&target);
1031        if let Some(s) = target_config.and_then(|c| c.llvm_filecheck.as_ref()) {
1032            s.to_path_buf()
1033        } else if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
1034            let llvm_bindir = command(s).arg("--bindir").run_capture_stdout(self).stdout();
1035            let filecheck = Path::new(llvm_bindir.trim()).join(exe("FileCheck", target));
1036            if filecheck.exists() {
1037                filecheck
1038            } else {
1039                // On Fedora the system LLVM installs FileCheck in the
1040                // llvm subdirectory of the libdir.
1041                let llvm_libdir = command(s).arg("--libdir").run_capture_stdout(self).stdout();
1042                let lib_filecheck =
1043                    Path::new(llvm_libdir.trim()).join("llvm").join(exe("FileCheck", target));
1044                if lib_filecheck.exists() {
1045                    lib_filecheck
1046                } else {
1047                    // Return the most normal file name, even though
1048                    // it doesn't exist, so that any error message
1049                    // refers to that.
1050                    filecheck
1051                }
1052            }
1053        } else {
1054            let base = self.llvm_out(target).join("build");
1055            let base = if !self.ninja() && target.is_msvc() {
1056                if self.config.llvm_optimize {
1057                    if self.config.llvm_release_debuginfo {
1058                        base.join("RelWithDebInfo")
1059                    } else {
1060                        base.join("Release")
1061                    }
1062                } else {
1063                    base.join("Debug")
1064                }
1065            } else {
1066                base
1067            };
1068            base.join("bin").join(exe("FileCheck", target))
1069        }
1070    }
1071
1072    /// Directory for libraries built from C/C++ code and shared between stages.
1073    fn native_dir(&self, target: TargetSelection) -> PathBuf {
1074        self.out.join(target).join("native")
1075    }
1076
1077    /// Root output directory for rust_test_helpers library compiled for
1078    /// `target`
1079    fn test_helpers_out(&self, target: TargetSelection) -> PathBuf {
1080        self.native_dir(target).join("rust-test-helpers")
1081    }
1082
1083    /// Adds the `RUST_TEST_THREADS` env var if necessary
1084    fn add_rust_test_threads(&self, cmd: &mut BootstrapCommand) {
1085        if env::var_os("RUST_TEST_THREADS").is_none() {
1086            cmd.env("RUST_TEST_THREADS", self.jobs().to_string());
1087        }
1088    }
1089
1090    /// Returns the libdir of the snapshot compiler.
1091    fn rustc_snapshot_libdir(&self) -> PathBuf {
1092        self.rustc_snapshot_sysroot().join(libdir(self.config.host_target))
1093    }
1094
1095    /// Returns the sysroot of the snapshot compiler.
1096    fn rustc_snapshot_sysroot(&self) -> &Path {
1097        static SYSROOT_CACHE: OnceLock<PathBuf> = OnceLock::new();
1098        SYSROOT_CACHE.get_or_init(|| {
1099            command(&self.initial_rustc)
1100                .run_in_dry_run()
1101                .args(["--print", "sysroot"])
1102                .run_capture_stdout(self)
1103                .stdout()
1104                .trim()
1105                .to_owned()
1106                .into()
1107        })
1108    }
1109
1110    fn info(&self, msg: &str) {
1111        match self.config.get_dry_run() {
1112            DryRun::SelfCheck => (),
1113            DryRun::Disabled | DryRun::UserSelected => {
1114                println!("{msg}");
1115            }
1116        }
1117    }
1118
1119    /// Return a `Group` guard for a [`Step`] that:
1120    /// - Performs `action`
1121    ///   - If the action is `Kind::Test`, use [`Build::msg_test`] instead.
1122    /// - On `what`
1123    ///   - Where `what` possibly corresponds to a `mode`
1124    /// - `action` is performed with/on the given compiler (`target_and_stage`).
1125    ///   - Since for some steps it is not possible to pass a single compiler here, it is also
1126    ///     possible to pass the host and stage explicitly.
1127    /// - With a given `target`.
1128    ///
1129    /// [`Step`]: crate::core::builder::Step
1130    #[must_use = "Groups should not be dropped until the Step finishes running"]
1131    #[track_caller]
1132    fn msg(
1133        &self,
1134        action: impl Into<Kind>,
1135        what: impl Display,
1136        mode: impl Into<Option<Mode>>,
1137        target_and_stage: impl Into<TargetAndStage>,
1138        target: impl Into<Option<TargetSelection>>,
1139    ) -> Option<gha::Group> {
1140        let target_and_stage = target_and_stage.into();
1141        let action = action.into();
1142        assert!(
1143            action != Kind::Test,
1144            "Please use `Build::msg_test` instead of `Build::msg(Kind::Test)`"
1145        );
1146
1147        let actual_stage = match mode.into() {
1148            // Std has the same stage as the compiler that builds it
1149            Some(Mode::Std) => target_and_stage.stage,
1150            // Other things have stage corresponding to their build compiler + 1
1151            Some(
1152                Mode::Rustc
1153                | Mode::Codegen
1154                | Mode::ToolBootstrap
1155                | Mode::ToolTarget
1156                | Mode::ToolStd
1157                | Mode::ToolRustcPrivate,
1158            )
1159            | None => target_and_stage.stage + 1,
1160        };
1161
1162        let action = action.description();
1163        let what = what.to_string();
1164        let msg = |fmt| {
1165            let space = if !what.is_empty() { " " } else { "" };
1166            format!("{action} stage{actual_stage} {what}{space}{fmt}")
1167        };
1168        let msg = if let Some(target) = target.into() {
1169            let build_stage = target_and_stage.stage;
1170            let host = target_and_stage.target;
1171            if host == target {
1172                msg(format_args!("(stage{build_stage} -> stage{actual_stage}, {target})"))
1173            } else {
1174                msg(format_args!("(stage{build_stage}:{host} -> stage{actual_stage}:{target})"))
1175            }
1176        } else {
1177            msg(format_args!(""))
1178        };
1179        self.group(&msg)
1180    }
1181
1182    /// Return a `Group` guard for a [`Step`] that tests `what` with the given `stage` and `target`.
1183    /// Use this instead of [`Build::msg`] for test steps, because for them it is not always clear
1184    /// what exactly is a build compiler.
1185    ///
1186    /// [`Step`]: crate::core::builder::Step
1187    #[must_use = "Groups should not be dropped until the Step finishes running"]
1188    #[track_caller]
1189    fn msg_test(
1190        &self,
1191        what: impl Display,
1192        target: TargetSelection,
1193        stage: u32,
1194    ) -> Option<gha::Group> {
1195        let action = Kind::Test.description();
1196        let msg = format!("{action} stage{stage} {what} ({target})");
1197        self.group(&msg)
1198    }
1199
1200    /// Return a `Group` guard for a [`Step`] that is only built once and isn't affected by `--stage`.
1201    ///
1202    /// [`Step`]: crate::core::builder::Step
1203    #[must_use = "Groups should not be dropped until the Step finishes running"]
1204    #[track_caller]
1205    fn msg_unstaged(
1206        &self,
1207        action: impl Into<Kind>,
1208        what: impl Display,
1209        target: TargetSelection,
1210    ) -> Option<gha::Group> {
1211        let action = action.into().description();
1212        let msg = format!("{action} {what} for {target}");
1213        self.group(&msg)
1214    }
1215
1216    #[track_caller]
1217    fn group(&self, msg: &str) -> Option<gha::Group> {
1218        match self.config.get_dry_run() {
1219            DryRun::SelfCheck => None,
1220            DryRun::Disabled | DryRun::UserSelected => Some(gha::group(msg)),
1221        }
1222    }
1223
1224    /// Returns the number of parallel jobs that have been configured for this
1225    /// build.
1226    fn jobs(&self) -> u32 {
1227        self.config.jobs.unwrap_or_else(|| {
1228            std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32
1229        })
1230    }
1231
1232    fn debuginfo_map_to(&self, which: GitRepo, remap_scheme: RemapScheme) -> Option<String> {
1233        if !self.config.rust_remap_debuginfo {
1234            return None;
1235        }
1236
1237        match which {
1238            GitRepo::Rustc => {
1239                let sha = self.rust_sha().unwrap_or(&self.version);
1240
1241                match remap_scheme {
1242                    RemapScheme::Compiler => {
1243                        // For compiler sources, remap via `/rustc-dev/{sha}` to allow
1244                        // distinguishing between compiler sources vs library sources, since
1245                        // `rustc-dev` dist component places them under
1246                        // `$sysroot/lib/rustlib/rustc-src/rust` as opposed to `rust-src`'s
1247                        // `$sysroot/lib/rustlib/src/rust`.
1248                        //
1249                        // Keep this scheme in sync with `rustc_metadata::rmeta::decoder`'s
1250                        // `try_to_translate_virtual_to_real`.
1251                        Some(format!("/rustc-dev/{sha}"))
1252                    }
1253                    RemapScheme::NonCompiler => {
1254                        // For non-compiler sources, use `/rustc/{sha}` remapping scheme.
1255                        Some(format!("/rustc/{sha}"))
1256                    }
1257                }
1258            }
1259            GitRepo::Llvm => Some(String::from("/rustc/llvm")),
1260        }
1261    }
1262
1263    /// Returns the path to the C compiler for the target specified.
1264    fn cc(&self, target: TargetSelection) -> PathBuf {
1265        if self.config.dry_run() {
1266            return PathBuf::new();
1267        }
1268        self.cc[&target].path().into()
1269    }
1270
1271    /// Returns the internal `cc::Tool` for the C compiler.
1272    fn cc_tool(&self, target: TargetSelection) -> Tool {
1273        self.cc[&target].clone()
1274    }
1275
1276    /// Returns the internal `cc::Tool` for the C++ compiler.
1277    fn cxx_tool(&self, target: TargetSelection) -> Tool {
1278        self.cxx[&target].clone()
1279    }
1280
1281    /// Returns C flags that `cc-rs` thinks should be enabled for the
1282    /// specified target by default.
1283    fn cc_handled_clags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
1284        if self.config.dry_run() {
1285            return Vec::new();
1286        }
1287        let base = match c {
1288            CLang::C => self.cc[&target].clone(),
1289            CLang::Cxx => self.cxx[&target].clone(),
1290        };
1291
1292        // Filter out -O and /O (the optimization flags) that we picked up
1293        // from cc-rs, that's up to the caller to figure out.
1294        base.args()
1295            .iter()
1296            .map(|s| s.to_string_lossy().into_owned())
1297            .filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
1298            .collect::<Vec<String>>()
1299    }
1300
1301    /// Returns extra C flags that `cc-rs` doesn't handle.
1302    fn cc_unhandled_cflags(
1303        &self,
1304        target: TargetSelection,
1305        which: GitRepo,
1306        c: CLang,
1307    ) -> Vec<String> {
1308        let mut base = Vec::new();
1309
1310        // If we're compiling C++ on macOS then we add a flag indicating that
1311        // we want libc++ (more filled out than libstdc++), ensuring that
1312        // LLVM/etc are all properly compiled.
1313        if matches!(c, CLang::Cxx) && target.contains("apple-darwin") {
1314            base.push("-stdlib=libc++".into());
1315        }
1316
1317        // Work around an apparently bad MinGW / GCC optimization,
1318        // See: https://lists.llvm.org/pipermail/cfe-dev/2016-December/051980.html
1319        // See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78936
1320        if &*target.triple == "i686-pc-windows-gnu" {
1321            base.push("-fno-omit-frame-pointer".into());
1322        }
1323
1324        if let Some(map_to) = self.debuginfo_map_to(which, RemapScheme::NonCompiler) {
1325            let map = format!("{}={}", self.src.display(), map_to);
1326            let cc = self.cc(target);
1327            if cc.ends_with("clang") || cc.ends_with("gcc") {
1328                base.push(format!("-fdebug-prefix-map={map}"));
1329            } else if cc.ends_with("clang-cl.exe") {
1330                base.push("-Xclang".into());
1331                base.push(format!("-fdebug-prefix-map={map}"));
1332            }
1333        }
1334        base
1335    }
1336
1337    /// Returns the path to the `ar` archive utility for the target specified.
1338    fn ar(&self, target: TargetSelection) -> Option<PathBuf> {
1339        if self.config.dry_run() {
1340            return None;
1341        }
1342        self.ar.get(&target).cloned()
1343    }
1344
1345    /// Returns the path to the `ranlib` utility for the target specified.
1346    fn ranlib(&self, target: TargetSelection) -> Option<PathBuf> {
1347        if self.config.dry_run() {
1348            return None;
1349        }
1350        self.ranlib.get(&target).cloned()
1351    }
1352
1353    /// Returns the path to the C++ compiler for the target specified.
1354    fn cxx(&self, target: TargetSelection) -> Result<PathBuf, String> {
1355        if self.config.dry_run() {
1356            return Ok(PathBuf::new());
1357        }
1358        match self.cxx.get(&target) {
1359            Some(p) => Ok(p.path().into()),
1360            None => Err(format!("target `{target}` is not configured as a host, only as a target")),
1361        }
1362    }
1363
1364    /// Returns the path to the linker for the given target if it needs to be overridden.
1365    fn linker(&self, target: TargetSelection) -> Option<PathBuf> {
1366        if self.config.dry_run() {
1367            return Some(PathBuf::new());
1368        }
1369        if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.clone())
1370        {
1371            Some(linker)
1372        } else if target.contains("vxworks") {
1373            // need to use CXX compiler as linker to resolve the exception functions
1374            // that are only existed in CXX libraries
1375            Some(self.cxx[&target].path().into())
1376        } else if !self.config.is_host_target(target)
1377            && helpers::use_host_linker(target)
1378            && !target.is_msvc()
1379        {
1380            Some(self.cc(target))
1381        } else if self.config.bootstrap_override_lld.is_used()
1382            && self.is_lld_direct_linker(target)
1383            && self.host_target == target
1384        {
1385            match self.config.bootstrap_override_lld {
1386                BootstrapOverrideLld::SelfContained => Some(self.initial_lld.clone()),
1387                BootstrapOverrideLld::External => Some("lld".into()),
1388                BootstrapOverrideLld::None => None,
1389            }
1390        } else {
1391            None
1392        }
1393    }
1394
1395    // Is LLD configured directly through `-Clinker`?
1396    // Only MSVC targets use LLD directly at the moment.
1397    fn is_lld_direct_linker(&self, target: TargetSelection) -> bool {
1398        target.is_msvc()
1399    }
1400
1401    /// Returns if this target should statically link the C runtime, if specified
1402    fn crt_static(&self, target: TargetSelection) -> Option<bool> {
1403        if target.contains("pc-windows-msvc") {
1404            Some(true)
1405        } else {
1406            self.config.target_config.get(&target).and_then(|t| t.crt_static)
1407        }
1408    }
1409
1410    /// Returns the "musl root" for this `target`, if defined.
1411    ///
1412    /// If this is a native target (host is also musl) and no musl-root is given,
1413    /// it falls back to the system toolchain in /usr.
1414    fn musl_root(&self, target: TargetSelection) -> Option<&Path> {
1415        let configured_root = self
1416            .config
1417            .target_config
1418            .get(&target)
1419            .and_then(|t| t.musl_root.as_ref())
1420            .or(self.config.musl_root.as_ref())
1421            .map(|p| &**p);
1422
1423        if self.config.is_host_target(target) && configured_root.is_none() {
1424            Some(Path::new("/usr"))
1425        } else {
1426            configured_root
1427        }
1428    }
1429
1430    /// Returns the "musl libdir" for this `target`.
1431    fn musl_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1432        self.config
1433            .target_config
1434            .get(&target)
1435            .and_then(|t| t.musl_libdir.clone())
1436            .or_else(|| self.musl_root(target).map(|root| root.join("lib")))
1437    }
1438
1439    /// Returns the `lib` directory for the WASI target specified, if
1440    /// configured.
1441    ///
1442    /// This first consults `wasi-root` as configured in per-target
1443    /// configuration, and failing that it assumes that `$WASI_SDK_PATH` is
1444    /// set in the environment, and failing that `None` is returned.
1445    fn wasi_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1446        let configured =
1447            self.config.target_config.get(&target).and_then(|t| t.wasi_root.as_ref()).map(|p| &**p);
1448        if let Some(path) = configured {
1449            return Some(path.join("lib").join(target.to_string()));
1450        }
1451        let mut env_root = self.wasi_sdk_path.clone()?;
1452        env_root.push("share");
1453        env_root.push("wasi-sysroot");
1454        env_root.push("lib");
1455        env_root.push(target.to_string());
1456        Some(env_root)
1457    }
1458
1459    /// Returns `true` if this is a no-std `target`, if defined
1460    fn no_std(&self, target: TargetSelection) -> Option<bool> {
1461        self.config.target_config.get(&target).map(|t| t.no_std)
1462    }
1463
1464    /// Returns `true` if the target will be tested using the `remote-test-client`
1465    /// and `remote-test-server` binaries.
1466    fn remote_tested(&self, target: TargetSelection) -> bool {
1467        self.qemu_rootfs(target).is_some()
1468            || target.contains("android")
1469            || env::var_os("TEST_DEVICE_ADDR").is_some()
1470    }
1471
1472    /// Returns an optional "runner" to pass to `compiletest` when executing
1473    /// test binaries.
1474    ///
1475    /// An example of this would be a WebAssembly runtime when testing the wasm
1476    /// targets.
1477    fn runner(&self, target: TargetSelection) -> Option<String> {
1478        let configured_runner =
1479            self.config.target_config.get(&target).and_then(|t| t.runner.as_ref()).map(|p| &**p);
1480        if let Some(runner) = configured_runner {
1481            return Some(runner.to_owned());
1482        }
1483
1484        if target.starts_with("wasm") && target.contains("wasi") {
1485            self.default_wasi_runner(target)
1486        } else {
1487            None
1488        }
1489    }
1490
1491    /// When a `runner` configuration is not provided and a WASI-looking target
1492    /// is being tested this is consulted to prove the environment to see if
1493    /// there's a runtime already lying around that seems reasonable to use.
1494    fn default_wasi_runner(&self, target: TargetSelection) -> Option<String> {
1495        let mut finder = crate::core::sanity::Finder::new();
1496
1497        // Look for Wasmtime, and for its default options be sure to disable
1498        // its caching system since we're executing quite a lot of tests and
1499        // ideally shouldn't pollute the cache too much.
1500        if let Some(path) = finder.maybe_have("wasmtime")
1501            && let Ok(mut path) = path.into_os_string().into_string()
1502        {
1503            path.push_str(" run -C cache=n --dir .");
1504            // Make sure that tests have access to RUSTC_BOOTSTRAP. This (for example) is
1505            // required for libtest to work on beta/stable channels.
1506            //
1507            // NB: with Wasmtime 20 this can change to `-S inherit-env` to
1508            // inherit the entire environment rather than just this single
1509            // environment variable.
1510            path.push_str(" --env RUSTC_BOOTSTRAP");
1511
1512            if target.contains("wasip2") {
1513                path.push_str(" --wasi inherit-network --wasi allow-ip-name-lookup");
1514            }
1515
1516            return Some(path);
1517        }
1518
1519        None
1520    }
1521
1522    /// Returns whether the specified tool is configured as part of this build.
1523    ///
1524    /// This requires that both the `extended` key is set and the `tools` key is
1525    /// either unset or specifically contains the specified tool.
1526    fn tool_enabled(&self, tool: &str) -> bool {
1527        if !self.config.extended {
1528            return false;
1529        }
1530        match &self.config.tools {
1531            Some(set) => set.contains(tool),
1532            None => true,
1533        }
1534    }
1535
1536    /// Returns the root of the "rootfs" image that this target will be using,
1537    /// if one was configured.
1538    ///
1539    /// If `Some` is returned then that means that tests for this target are
1540    /// emulated with QEMU and binaries will need to be shipped to the emulator.
1541    fn qemu_rootfs(&self, target: TargetSelection) -> Option<&Path> {
1542        self.config.target_config.get(&target).and_then(|t| t.qemu_rootfs.as_ref()).map(|p| &**p)
1543    }
1544
1545    /// Temporary directory that extended error information is emitted to.
1546    fn extended_error_dir(&self) -> PathBuf {
1547        self.out.join("tmp/extended-error-metadata")
1548    }
1549
1550    /// Tests whether the `compiler` compiling for `target` should be forced to
1551    /// use a stage1 compiler instead.
1552    ///
1553    /// Currently, by default, the build system does not perform a "full
1554    /// bootstrap" by default where we compile the compiler three times.
1555    /// Instead, we compile the compiler two times. The final stage (stage2)
1556    /// just copies the libraries from the previous stage, which is what this
1557    /// method detects.
1558    ///
1559    /// Here we return `true` if:
1560    ///
1561    /// * The build isn't performing a full bootstrap
1562    /// * The `compiler` is in the final stage, 2
1563    /// * We're not cross-compiling, so the artifacts are already available in
1564    ///   stage1
1565    ///
1566    /// When all of these conditions are met the build will lift artifacts from
1567    /// the previous stage forward.
1568    fn force_use_stage1(&self, stage: u32, target: TargetSelection) -> bool {
1569        !self.config.full_bootstrap
1570            && !self.config.download_rustc()
1571            && stage >= 2
1572            && (self.hosts.contains(&target) || target == self.host_target)
1573    }
1574
1575    /// Checks whether the `compiler` compiling for `target` should be forced to
1576    /// use a stage2 compiler instead.
1577    ///
1578    /// When we download the pre-compiled version of rustc and compiler stage is >= 2,
1579    /// it should be forced to use a stage2 compiler.
1580    fn force_use_stage2(&self, stage: u32) -> bool {
1581        self.config.download_rustc() && stage >= 2
1582    }
1583
1584    /// Given `num` in the form "a.b.c" return a "release string" which
1585    /// describes the release version number.
1586    ///
1587    /// For example on nightly this returns "a.b.c-nightly", on beta it returns
1588    /// "a.b.c-beta.1" and on stable it just returns "a.b.c".
1589    fn release(&self, num: &str) -> String {
1590        match &self.config.channel[..] {
1591            "stable" => num.to_string(),
1592            "beta" => {
1593                if !self.config.omit_git_hash {
1594                    format!("{}-beta.{}", num, self.beta_prerelease_version())
1595                } else {
1596                    format!("{num}-beta")
1597                }
1598            }
1599            "nightly" => format!("{num}-nightly"),
1600            _ => format!("{num}-dev"),
1601        }
1602    }
1603
1604    fn beta_prerelease_version(&self) -> u32 {
1605        fn extract_beta_rev_from_file<P: AsRef<Path>>(version_file: P) -> Option<String> {
1606            let version = fs::read_to_string(version_file).ok()?;
1607
1608            helpers::extract_beta_rev(&version)
1609        }
1610
1611        if let Some(s) = self.prerelease_version.get() {
1612            return s;
1613        }
1614
1615        // First check if there is a version file available.
1616        // If available, we read the beta revision from that file.
1617        // This only happens when building from a source tarball when Git should not be used.
1618        let count = extract_beta_rev_from_file(self.src.join("version")).unwrap_or_else(|| {
1619            // Figure out how many merge commits happened since we branched off main.
1620            // That's our beta number!
1621            // (Note that we use a `..` range, not the `...` symmetric difference.)
1622            helpers::git(Some(&self.src))
1623                .arg("rev-list")
1624                .arg("--count")
1625                .arg("--merges")
1626                .arg(format!(
1627                    "refs/remotes/origin/{}..HEAD",
1628                    self.config.stage0_metadata.config.nightly_branch
1629                ))
1630                .run_in_dry_run()
1631                .run_capture(self)
1632                .stdout()
1633        });
1634        let n = count.trim().parse().unwrap();
1635        self.prerelease_version.set(Some(n));
1636        n
1637    }
1638
1639    /// Returns the value of `release` above for Rust itself.
1640    fn rust_release(&self) -> String {
1641        self.release(&self.version)
1642    }
1643
1644    /// Returns the "package version" for a component.
1645    ///
1646    /// The package version is typically what shows up in the names of tarballs.
1647    /// For channels like beta/nightly it's just the channel name, otherwise it's the release
1648    /// version.
1649    fn rust_package_vers(&self) -> String {
1650        match &self.config.channel[..] {
1651            "stable" => self.version.to_string(),
1652            "beta" => "beta".to_string(),
1653            "nightly" => "nightly".to_string(),
1654            _ => format!("{}-dev", self.version),
1655        }
1656    }
1657
1658    /// Returns the `version` string associated with this compiler for Rust
1659    /// itself.
1660    ///
1661    /// Note that this is a descriptive string which includes the commit date,
1662    /// sha, version, etc.
1663    fn rust_version(&self) -> String {
1664        let mut version = self.rust_info().version(self, &self.version);
1665        if let Some(ref s) = self.config.description
1666            && !s.is_empty()
1667        {
1668            version.push_str(" (");
1669            version.push_str(s);
1670            version.push(')');
1671        }
1672        version
1673    }
1674
1675    /// Returns the full commit hash.
1676    fn rust_sha(&self) -> Option<&str> {
1677        self.rust_info().sha()
1678    }
1679
1680    /// Returns the `a.b.c` version that the given package is at.
1681    fn release_num(&self, package: &str) -> String {
1682        let toml_file_name = self.src.join(format!("src/tools/{package}/Cargo.toml"));
1683        let toml = t!(fs::read_to_string(toml_file_name));
1684        for line in toml.lines() {
1685            if let Some(stripped) =
1686                line.strip_prefix("version = \"").and_then(|s| s.strip_suffix('"'))
1687            {
1688                return stripped.to_owned();
1689            }
1690        }
1691
1692        panic!("failed to find version in {package}'s Cargo.toml")
1693    }
1694
1695    /// Returns `true` if unstable features should be enabled for the compiler
1696    /// we're building.
1697    fn unstable_features(&self) -> bool {
1698        !matches!(&self.config.channel[..], "stable" | "beta")
1699    }
1700
1701    /// Returns a Vec of all the dependencies of the given root crate,
1702    /// including transitive dependencies and the root itself. Only includes
1703    /// "local" crates (those in the local source tree, not from a registry).
1704    fn in_tree_crates(&self, root: &str, target: Option<TargetSelection>) -> Vec<&Crate> {
1705        let mut ret = Vec::new();
1706        let mut list = vec![root.to_owned()];
1707        let mut visited = HashSet::new();
1708        while let Some(krate) = list.pop() {
1709            let krate = self
1710                .crates
1711                .get(&krate)
1712                .unwrap_or_else(|| panic!("metadata missing for {krate}: {:?}", self.crates));
1713            ret.push(krate);
1714            for dep in &krate.deps {
1715                if !self.crates.contains_key(dep) {
1716                    // Ignore non-workspace members.
1717                    continue;
1718                }
1719                // Don't include optional deps if their features are not
1720                // enabled. Ideally this would be computed from `cargo
1721                // metadata --features …`, but that is somewhat slow. In
1722                // the future, we may want to consider just filtering all
1723                // build and dev dependencies in metadata::build.
1724                if visited.insert(dep)
1725                    && (dep != "profiler_builtins"
1726                        || target
1727                            .map(|t| self.config.profiler_enabled(t))
1728                            .unwrap_or_else(|| self.config.any_profiler_enabled()))
1729                    && (dep != "rustc_codegen_llvm"
1730                        || self.config.hosts.iter().any(|host| self.config.llvm_enabled(*host)))
1731                {
1732                    list.push(dep.clone());
1733                }
1734            }
1735        }
1736        ret.sort_unstable_by_key(|krate| krate.name.clone()); // reproducible order needed for tests
1737        ret
1738    }
1739
1740    fn read_stamp_file(&self, stamp: &BuildStamp) -> Vec<(PathBuf, DependencyType)> {
1741        if self.config.dry_run() {
1742            return Vec::new();
1743        }
1744
1745        if !stamp.path().exists() {
1746            eprintln!(
1747                "ERROR: Unable to find the stamp file {}, did you try to keep a nonexistent build stage?",
1748                stamp.path().display()
1749            );
1750            crate::exit!(1);
1751        }
1752
1753        let mut paths = Vec::new();
1754        let contents = t!(fs::read(stamp.path()), stamp.path());
1755        // This is the method we use for extracting paths from the stamp file passed to us. See
1756        // run_cargo for more information (in compile.rs).
1757        for part in contents.split(|b| *b == 0) {
1758            if part.is_empty() {
1759                continue;
1760            }
1761            let dependency_type = match part[0] as char {
1762                'h' => DependencyType::Host,
1763                's' => DependencyType::TargetSelfContained,
1764                't' => DependencyType::Target,
1765                _ => unreachable!(),
1766            };
1767            let path = PathBuf::from(t!(str::from_utf8(&part[1..])));
1768            paths.push((path, dependency_type));
1769        }
1770        paths
1771    }
1772
1773    /// Copies a file from `src` to `dst`.
1774    ///
1775    /// If `src` is a symlink, `src` will be resolved to the actual path
1776    /// and copied to `dst` instead of the symlink itself.
1777    #[track_caller]
1778    pub fn resolve_symlink_and_copy(&self, src: &Path, dst: &Path) {
1779        self.copy_link_internal(src, dst, true);
1780    }
1781
1782    /// Links a file from `src` to `dst`.
1783    /// Attempts to use hard links if possible, falling back to copying.
1784    /// You can neither rely on this being a copy nor it being a link,
1785    /// so do not write to dst.
1786    #[track_caller]
1787    pub fn copy_link(&self, src: &Path, dst: &Path, file_type: FileType) {
1788        self.copy_link_internal(src, dst, false);
1789
1790        if file_type.could_have_split_debuginfo()
1791            && let Some(dbg_file) = split_debuginfo(src)
1792        {
1793            self.copy_link_internal(
1794                &dbg_file,
1795                &dst.with_extension(dbg_file.extension().unwrap()),
1796                false,
1797            );
1798        }
1799    }
1800
1801    #[track_caller]
1802    fn copy_link_internal(&self, src: &Path, dst: &Path, dereference_symlinks: bool) {
1803        if self.config.dry_run() {
1804            return;
1805        }
1806        if src == dst {
1807            return;
1808        }
1809
1810        #[cfg(feature = "tracing")]
1811        let _span = trace_io!("file-copy-link", ?src, ?dst);
1812
1813        if let Err(e) = fs::remove_file(dst)
1814            && cfg!(windows)
1815            && e.kind() != io::ErrorKind::NotFound
1816        {
1817            // workaround for https://github.com/rust-lang/rust/issues/127126
1818            // if removing the file fails, attempt to rename it instead.
1819            let now = t!(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH));
1820            let _ = fs::rename(dst, format!("{}-{}", dst.display(), now.as_nanos()));
1821        }
1822        let mut metadata = t!(src.symlink_metadata(), format!("src = {}", src.display()));
1823        let mut src = src.to_path_buf();
1824        if metadata.file_type().is_symlink() {
1825            if dereference_symlinks {
1826                src = t!(fs::canonicalize(src));
1827                metadata = t!(fs::metadata(&src), format!("target = {}", src.display()));
1828            } else {
1829                let link = t!(fs::read_link(src));
1830                t!(self.symlink_file(link, dst));
1831                return;
1832            }
1833        }
1834        if let Ok(()) = fs::hard_link(&src, dst) {
1835            // Attempt to "easy copy" by creating a hard link (symlinks are privileged on windows),
1836            // but if that fails just fall back to a slow `copy` operation.
1837        } else {
1838            if let Err(e) = fs::copy(&src, dst) {
1839                panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
1840            }
1841            t!(fs::set_permissions(dst, metadata.permissions()));
1842
1843            // Restore file times because changing permissions on e.g. Linux using `chmod` can cause
1844            // file access time to change.
1845            let file_times = fs::FileTimes::new()
1846                .set_accessed(t!(metadata.accessed()))
1847                .set_modified(t!(metadata.modified()));
1848            t!(set_file_times(dst, file_times));
1849        }
1850    }
1851
1852    /// Links the `src` directory recursively to `dst`. Both are assumed to exist
1853    /// when this function is called.
1854    /// Will attempt to use hard links if possible and fall back to copying.
1855    #[track_caller]
1856    pub fn cp_link_r(&self, src: &Path, dst: &Path) {
1857        if self.config.dry_run() {
1858            return;
1859        }
1860        for f in self.read_dir(src) {
1861            let path = f.path();
1862            let name = path.file_name().unwrap();
1863            let dst = dst.join(name);
1864            if t!(f.file_type()).is_dir() {
1865                t!(fs::create_dir_all(&dst));
1866                self.cp_link_r(&path, &dst);
1867            } else {
1868                self.copy_link(&path, &dst, FileType::Regular);
1869            }
1870        }
1871    }
1872
1873    /// Copies the `src` directory recursively to `dst`. Both are assumed to exist
1874    /// when this function is called.
1875    /// Will attempt to use hard links if possible and fall back to copying.
1876    /// Unwanted files or directories can be skipped
1877    /// by returning `false` from the filter function.
1878    #[track_caller]
1879    pub fn cp_link_filtered(&self, src: &Path, dst: &Path, filter: &dyn Fn(&Path) -> bool) {
1880        // Immediately recurse with an empty relative path
1881        self.cp_link_filtered_recurse(src, dst, Path::new(""), filter)
1882    }
1883
1884    // Inner function does the actual work
1885    #[track_caller]
1886    fn cp_link_filtered_recurse(
1887        &self,
1888        src: &Path,
1889        dst: &Path,
1890        relative: &Path,
1891        filter: &dyn Fn(&Path) -> bool,
1892    ) {
1893        for f in self.read_dir(src) {
1894            let path = f.path();
1895            let name = path.file_name().unwrap();
1896            let dst = dst.join(name);
1897            let relative = relative.join(name);
1898            // Only copy file or directory if the filter function returns true
1899            if filter(&relative) {
1900                if t!(f.file_type()).is_dir() {
1901                    let _ = fs::remove_dir_all(&dst);
1902                    self.create_dir(&dst);
1903                    self.cp_link_filtered_recurse(&path, &dst, &relative, filter);
1904                } else {
1905                    self.copy_link(&path, &dst, FileType::Regular);
1906                }
1907            }
1908        }
1909    }
1910
1911    fn copy_link_to_folder(&self, src: &Path, dest_folder: &Path) {
1912        let file_name = src.file_name().unwrap();
1913        let dest = dest_folder.join(file_name);
1914        self.copy_link(src, &dest, FileType::Regular);
1915    }
1916
1917    fn install(&self, src: &Path, dstdir: &Path, file_type: FileType) {
1918        if self.config.dry_run() {
1919            return;
1920        }
1921        let dst = dstdir.join(src.file_name().unwrap());
1922
1923        #[cfg(feature = "tracing")]
1924        let _span = trace_io!("install", ?src, ?dst);
1925
1926        t!(fs::create_dir_all(dstdir));
1927        if !src.exists() {
1928            panic!("ERROR: File \"{}\" not found!", src.display());
1929        }
1930
1931        self.copy_link_internal(src, &dst, true);
1932        chmod(&dst, file_type.perms());
1933
1934        // If this file can have debuginfo, look for split debuginfo and install it too.
1935        if file_type.could_have_split_debuginfo()
1936            && let Some(dbg_file) = split_debuginfo(src)
1937        {
1938            self.install(&dbg_file, dstdir, FileType::Regular);
1939        }
1940    }
1941
1942    fn read(&self, path: &Path) -> String {
1943        if self.config.dry_run() {
1944            return String::new();
1945        }
1946        t!(fs::read_to_string(path))
1947    }
1948
1949    #[track_caller]
1950    fn create_dir(&self, dir: &Path) {
1951        if self.config.dry_run() {
1952            return;
1953        }
1954
1955        #[cfg(feature = "tracing")]
1956        let _span = trace_io!("dir-create", ?dir);
1957
1958        t!(fs::create_dir_all(dir))
1959    }
1960
1961    fn remove_dir(&self, dir: &Path) {
1962        if self.config.dry_run() {
1963            return;
1964        }
1965
1966        #[cfg(feature = "tracing")]
1967        let _span = trace_io!("dir-remove", ?dir);
1968
1969        t!(fs::remove_dir_all(dir))
1970    }
1971
1972    /// Make sure that `dir` will be an empty existing directory after this function ends.
1973    /// If it existed before, it will be first deleted.
1974    fn clear_dir(&self, dir: &Path) {
1975        if self.config.dry_run() {
1976            return;
1977        }
1978
1979        #[cfg(feature = "tracing")]
1980        let _span = trace_io!("dir-clear", ?dir);
1981
1982        let _ = std::fs::remove_dir_all(dir);
1983        self.create_dir(dir);
1984    }
1985
1986    fn read_dir(&self, dir: &Path) -> impl Iterator<Item = fs::DirEntry> {
1987        let iter = match fs::read_dir(dir) {
1988            Ok(v) => v,
1989            Err(_) if self.config.dry_run() => return vec![].into_iter(),
1990            Err(err) => panic!("could not read dir {dir:?}: {err:?}"),
1991        };
1992        iter.map(|e| t!(e)).collect::<Vec<_>>().into_iter()
1993    }
1994
1995    fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, link: Q) -> io::Result<()> {
1996        #[cfg(unix)]
1997        use std::os::unix::fs::symlink as symlink_file;
1998        #[cfg(windows)]
1999        use std::os::windows::fs::symlink_file;
2000        if !self.config.dry_run() { symlink_file(src.as_ref(), link.as_ref()) } else { Ok(()) }
2001    }
2002
2003    /// Returns if config.ninja is enabled, and checks for ninja existence,
2004    /// exiting with a nicer error message if not.
2005    fn ninja(&self) -> bool {
2006        let mut cmd_finder = crate::core::sanity::Finder::new();
2007
2008        if self.config.ninja_in_file {
2009            // Some Linux distros rename `ninja` to `ninja-build`.
2010            // CMake can work with either binary name.
2011            if cmd_finder.maybe_have("ninja-build").is_none()
2012                && cmd_finder.maybe_have("ninja").is_none()
2013            {
2014                eprintln!(
2015                    "
2016Couldn't find required command: ninja (or ninja-build)
2017
2018You should install ninja as described at
2019<https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages>,
2020or set `ninja = false` in the `[llvm]` section of `bootstrap.toml`.
2021Alternatively, set `download-ci-llvm = true` in that `[llvm]` section
2022to download LLVM rather than building it.
2023"
2024                );
2025                exit!(1);
2026            }
2027        }
2028
2029        // If ninja isn't enabled but we're building for MSVC then we try
2030        // doubly hard to enable it. It was realized in #43767 that the msbuild
2031        // CMake generator for MSVC doesn't respect configuration options like
2032        // disabling LLVM assertions, which can often be quite important!
2033        //
2034        // In these cases we automatically enable Ninja if we find it in the
2035        // environment.
2036        if !self.config.ninja_in_file
2037            && self.config.host_target.is_msvc()
2038            && cmd_finder.maybe_have("ninja").is_some()
2039        {
2040            return true;
2041        }
2042
2043        self.config.ninja_in_file
2044    }
2045
2046    pub fn colored_stdout<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
2047        self.colored_stream_inner(StandardStream::stdout, self.config.stdout_is_tty, f)
2048    }
2049
2050    pub fn colored_stderr<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
2051        self.colored_stream_inner(StandardStream::stderr, self.config.stderr_is_tty, f)
2052    }
2053
2054    fn colored_stream_inner<R, F, C>(&self, constructor: C, is_tty: bool, f: F) -> R
2055    where
2056        C: Fn(ColorChoice) -> StandardStream,
2057        F: FnOnce(&mut dyn WriteColor) -> R,
2058    {
2059        let choice = match self.config.color {
2060            flags::Color::Always => ColorChoice::Always,
2061            flags::Color::Never => ColorChoice::Never,
2062            flags::Color::Auto if !is_tty => ColorChoice::Never,
2063            flags::Color::Auto => ColorChoice::Auto,
2064        };
2065        let mut stream = constructor(choice);
2066        let result = f(&mut stream);
2067        stream.reset().unwrap();
2068        result
2069    }
2070
2071    pub fn exec_ctx(&self) -> &ExecutionContext {
2072        &self.config.exec_ctx
2073    }
2074
2075    pub fn report_summary(&self, path: &Path, start_time: Instant) {
2076        self.config.exec_ctx.profiler().report_summary(path, start_time);
2077    }
2078
2079    #[cfg(feature = "tracing")]
2080    pub fn report_step_graph(self, directory: &Path) {
2081        self.step_graph.into_inner().store_to_dot_files(directory);
2082    }
2083}
2084
2085impl AsRef<ExecutionContext> for Build {
2086    fn as_ref(&self) -> &ExecutionContext {
2087        &self.config.exec_ctx
2088    }
2089}
2090
2091#[cfg(unix)]
2092fn chmod(path: &Path, perms: u32) {
2093    use std::os::unix::fs::*;
2094    t!(fs::set_permissions(path, fs::Permissions::from_mode(perms)));
2095}
2096#[cfg(windows)]
2097fn chmod(_path: &Path, _perms: u32) {}
2098
2099impl Compiler {
2100    pub fn new(stage: u32, host: TargetSelection) -> Self {
2101        Self { stage, host, forced_compiler: false }
2102    }
2103
2104    pub fn forced_compiler(&mut self, forced_compiler: bool) {
2105        self.forced_compiler = forced_compiler;
2106    }
2107
2108    /// Returns `true` if this is a snapshot compiler for `build`'s configuration
2109    pub fn is_snapshot(&self, build: &Build) -> bool {
2110        self.stage == 0 && self.host == build.host_target
2111    }
2112
2113    /// Indicates whether the compiler was forced to use a specific stage.
2114    pub fn is_forced_compiler(&self) -> bool {
2115        self.forced_compiler
2116    }
2117}
2118
2119fn envify(s: &str) -> String {
2120    s.chars()
2121        .map(|c| match c {
2122            '-' => '_',
2123            c => c,
2124        })
2125        .flat_map(|c| c.to_uppercase())
2126        .collect()
2127}
2128
2129/// Ensures that the behavior dump directory is properly initialized.
2130pub fn prepare_behaviour_dump_dir(build: &Build) {
2131    static INITIALIZED: OnceLock<bool> = OnceLock::new();
2132
2133    let dump_path = build.out.join("bootstrap-shims-dump");
2134
2135    let initialized = INITIALIZED.get().unwrap_or(&false);
2136    if !initialized {
2137        // clear old dumps
2138        if dump_path.exists() {
2139            t!(fs::remove_dir_all(&dump_path));
2140        }
2141
2142        t!(fs::create_dir_all(&dump_path));
2143
2144        t!(INITIALIZED.set(true));
2145    }
2146}