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