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        // When testing bootstrap itself, it is much faster to ignore
680        // submodules. Almost all Steps work fine without their submodules.
681        if cfg!(test) && !self.config.submodules() {
682            return;
683        }
684        self.config.update_submodule(submodule);
685        let absolute_path = self.config.src.join(submodule);
686        if !absolute_path.exists() || dir_is_empty(&absolute_path) {
687            let maybe_enable = if !self.config.submodules()
688                && self.config.rust_info.is_managed_git_subrepository()
689            {
690                "\nConsider setting `build.submodules = true` or manually initializing the submodules."
691            } else {
692                ""
693            };
694            let err_hint = err_hint.map_or_else(String::new, |e| format!("\n{e}"));
695            eprintln!(
696                "submodule {submodule} does not appear to be checked out, \
697                 but it is required for this step{maybe_enable}{err_hint}"
698            );
699            exit!(1);
700        }
701    }
702
703    /// If any submodule has been initialized already, sync it unconditionally.
704    /// This avoids contributors checking in a submodule change by accident.
705    fn update_existing_submodules(&self) {
706        // Avoid running git when there isn't a git checkout, or the user has
707        // explicitly disabled submodules in `bootstrap.toml`.
708        if !self.config.submodules() {
709            return;
710        }
711        let output = helpers::git(Some(&self.src))
712            .args(["config", "--file"])
713            .arg(".gitmodules")
714            .args(["--get-regexp", "path"])
715            .run_capture(self)
716            .stdout();
717        std::thread::scope(|s| {
718            // Look for `submodule.$name.path = $path`
719            // Sample output: `submodule.src/rust-installer.path src/tools/rust-installer`
720            for line in output.lines() {
721                let submodule = line.split_once(' ').unwrap().1;
722                let config = self.config.clone();
723                s.spawn(move || {
724                    Self::update_existing_submodule(&config, submodule);
725                });
726            }
727        });
728    }
729
730    /// Updates the given submodule only if it's initialized already; nothing happens otherwise.
731    pub fn update_existing_submodule(config: &Config, submodule: &str) {
732        // Avoid running git when there isn't a git checkout.
733        if !config.submodules() {
734            return;
735        }
736
737        if config.git_info(false, Path::new(submodule)).is_managed_git_subrepository() {
738            config.update_submodule(submodule);
739        }
740    }
741
742    /// Executes the entire build, as configured by the flags and configuration.
743    #[cfg_attr(feature = "tracing", instrument(level = "debug", name = "Build::build", skip_all))]
744    pub fn build(&mut self) {
745        trace!("setting up job management");
746        unsafe {
747            crate::utils::job::setup(self);
748        }
749
750        // Handle hard-coded subcommands.
751        {
752            #[cfg(feature = "tracing")]
753            let _hardcoded_span =
754                span!(tracing::Level::DEBUG, "handling hardcoded subcommands (Format, Perf)")
755                    .entered();
756
757            match &self.config.cmd {
758                Subcommand::Format { check, all } => {
759                    return core::build_steps::format::format(
760                        &builder::Builder::new(self),
761                        *check,
762                        *all,
763                        &self.config.paths,
764                    );
765                }
766                Subcommand::Perf(args) => {
767                    return core::build_steps::perf::perf(&builder::Builder::new(self), args);
768                }
769                _cmd => {
770                    debug!(cmd = ?_cmd, "not a hardcoded subcommand; returning to normal handling");
771                }
772            }
773
774            debug!("handling subcommand normally");
775        }
776
777        if !self.config.dry_run() {
778            #[cfg(feature = "tracing")]
779            let _real_run_span = span!(tracing::Level::DEBUG, "executing real run").entered();
780
781            // We first do a dry-run. This is a sanity-check to ensure that
782            // steps don't do anything expensive in the dry-run.
783            {
784                #[cfg(feature = "tracing")]
785                let _sanity_check_span =
786                    span!(tracing::Level::DEBUG, "(1) executing dry-run sanity-check").entered();
787                self.config.set_dry_run(DryRun::SelfCheck);
788                let builder = builder::Builder::new(self);
789                builder.execute_cli();
790            }
791
792            // Actual run.
793            {
794                #[cfg(feature = "tracing")]
795                let _actual_run_span =
796                    span!(tracing::Level::DEBUG, "(2) executing actual run").entered();
797                self.config.set_dry_run(DryRun::Disabled);
798                let builder = builder::Builder::new(self);
799                builder.execute_cli();
800            }
801        } else {
802            #[cfg(feature = "tracing")]
803            let _dry_run_span = span!(tracing::Level::DEBUG, "executing dry run").entered();
804
805            let builder = builder::Builder::new(self);
806            builder.execute_cli();
807        }
808
809        #[cfg(feature = "tracing")]
810        debug!("checking for postponed test failures from `test  --no-fail-fast`");
811
812        // Check for postponed failures from `test --no-fail-fast`.
813        self.config.exec_ctx().report_failures_and_exit();
814
815        #[cfg(feature = "build-metrics")]
816        self.metrics.persist(self);
817    }
818
819    fn rust_info(&self) -> &GitInfo {
820        &self.config.rust_info
821    }
822
823    /// Gets the space-separated set of activated features for the standard library.
824    /// This can be configured with the `std-features` key in bootstrap.toml.
825    fn std_features(&self, target: TargetSelection) -> String {
826        let mut features: BTreeSet<&str> =
827            self.config.rust_std_features.iter().map(|s| s.as_str()).collect();
828
829        match self.config.llvm_libunwind(target) {
830            LlvmLibunwind::InTree => features.insert("llvm-libunwind"),
831            LlvmLibunwind::System => features.insert("system-llvm-libunwind"),
832            LlvmLibunwind::No => false,
833        };
834
835        if self.config.backtrace {
836            features.insert("backtrace");
837        }
838
839        if self.config.profiler_enabled(target) {
840            features.insert("profiler");
841        }
842
843        // If zkvm target, generate memcpy, etc.
844        if target.contains("zkvm") {
845            features.insert("compiler-builtins-mem");
846        }
847
848        if self.config.llvm_enzyme {
849            features.insert("llvm_enzyme");
850        }
851
852        features.into_iter().collect::<Vec<_>>().join(" ")
853    }
854
855    /// Gets the space-separated set of activated features for the compiler.
856    fn rustc_features(&self, kind: Kind, target: TargetSelection, crates: &[String]) -> String {
857        let possible_features_by_crates: HashSet<_> = crates
858            .iter()
859            .flat_map(|krate| &self.crates[krate].features)
860            .map(std::ops::Deref::deref)
861            .collect();
862        let check = |feature: &str| -> bool {
863            crates.is_empty() || possible_features_by_crates.contains(feature)
864        };
865        let mut features = vec![];
866        if self.config.jemalloc(target) && check("jemalloc") {
867            features.push("jemalloc");
868        }
869        if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
870            features.push("llvm");
871        }
872        if self.config.llvm_enzyme {
873            features.push("llvm_enzyme");
874        }
875        if self.config.llvm_offload {
876            features.push("llvm_offload");
877        }
878        // keep in sync with `bootstrap/compile.rs:rustc_cargo_env`
879        if self.config.rust_randomize_layout && check("rustc_randomized_layouts") {
880            features.push("rustc_randomized_layouts");
881        }
882        if self.config.compile_time_deps && kind == Kind::Check {
883            features.push("check_only");
884        }
885
886        if crates.iter().any(|c| c == "rustc_transmute") {
887            // for `x test rustc_transmute`, this feature isn't enabled automatically by a
888            // dependent crate.
889            features.push("rustc");
890        }
891
892        // If debug logging is on, then we want the default for tracing:
893        // https://github.com/tokio-rs/tracing/blob/3dd5c03d907afdf2c39444a29931833335171554/tracing/src/level_filters.rs#L26
894        // which is everything (including debug/trace/etc.)
895        // if its unset, if debug_assertions is on, then debug_logging will also be on
896        // as well as tracing *ignoring* this feature when debug_assertions is on
897        if !self.config.rust_debug_logging && check("max_level_info") {
898            features.push("max_level_info");
899        }
900
901        features.join(" ")
902    }
903
904    /// Component directory that Cargo will produce output into (e.g.
905    /// release/debug)
906    fn cargo_dir(&self, mode: Mode) -> &'static str {
907        match (mode, self.config.rust_optimize.is_release()) {
908            (Mode::Std, _) => "dist",
909            (_, true) => "release",
910            (_, false) => "debug",
911        }
912    }
913
914    fn tools_dir(&self, build_compiler: Compiler) -> PathBuf {
915        let out = self
916            .out
917            .join(build_compiler.host)
918            .join(format!("stage{}-tools-bin", build_compiler.stage + 1));
919        t!(fs::create_dir_all(&out));
920        out
921    }
922
923    /// Returns the root directory for all output generated in a particular
924    /// stage when being built with a particular build compiler.
925    ///
926    /// The mode indicates what the root directory is for.
927    fn stage_out(&self, build_compiler: Compiler, mode: Mode) -> PathBuf {
928        use std::fmt::Write;
929
930        fn bootstrap_tool() -> (Option<u32>, &'static str) {
931            (None, "bootstrap-tools")
932        }
933        fn staged_tool(build_compiler: Compiler) -> (Option<u32>, &'static str) {
934            (Some(build_compiler.stage + 1), "tools")
935        }
936
937        let (stage, suffix) = match mode {
938            // Std is special, stage N std is built with stage N rustc
939            Mode::Std => (Some(build_compiler.stage), "std"),
940            // The rest of things are built with stage N-1 rustc
941            Mode::Rustc => (Some(build_compiler.stage + 1), "rustc"),
942            Mode::Codegen => (Some(build_compiler.stage + 1), "codegen"),
943            Mode::ToolBootstrap => bootstrap_tool(),
944            Mode::ToolStd | Mode::ToolRustcPrivate => (Some(build_compiler.stage + 1), "tools"),
945            Mode::ToolTarget => {
946                // If we're not cross-compiling (the common case), share the target directory with
947                // bootstrap tools to reuse the build cache.
948                if build_compiler.stage == 0 {
949                    bootstrap_tool()
950                } else {
951                    staged_tool(build_compiler)
952                }
953            }
954        };
955        let path = self.out.join(build_compiler.host);
956        let mut dir_name = String::new();
957        if let Some(stage) = stage {
958            write!(dir_name, "stage{stage}-").unwrap();
959        }
960        dir_name.push_str(suffix);
961        path.join(dir_name)
962    }
963
964    /// Returns the root output directory for all Cargo output in a given stage,
965    /// running a particular compiler, whether or not we're building the
966    /// standard library, and targeting the specified architecture.
967    fn cargo_out(&self, build_compiler: Compiler, mode: Mode, target: TargetSelection) -> PathBuf {
968        self.stage_out(build_compiler, mode).join(target).join(self.cargo_dir(mode))
969    }
970
971    /// Root output directory of LLVM for `target`
972    ///
973    /// Note that if LLVM is configured externally then the directory returned
974    /// will likely be empty.
975    fn llvm_out(&self, target: TargetSelection) -> PathBuf {
976        if self.config.llvm_from_ci && self.config.is_host_target(target) {
977            self.config.ci_llvm_root()
978        } else {
979            self.out.join(target).join("llvm")
980        }
981    }
982
983    fn enzyme_out(&self, target: TargetSelection) -> PathBuf {
984        self.out.join(&*target.triple).join("enzyme")
985    }
986
987    fn offload_out(&self, target: TargetSelection) -> PathBuf {
988        self.out.join(&*target.triple).join("offload")
989    }
990
991    fn lld_out(&self, target: TargetSelection) -> PathBuf {
992        self.out.join(target).join("lld")
993    }
994
995    /// Output directory for all documentation for a target
996    fn doc_out(&self, target: TargetSelection) -> PathBuf {
997        self.out.join(target).join("doc")
998    }
999
1000    /// Output directory for all JSON-formatted documentation for a target
1001    fn json_doc_out(&self, target: TargetSelection) -> PathBuf {
1002        self.out.join(target).join("json-doc")
1003    }
1004
1005    fn test_out(&self, target: TargetSelection) -> PathBuf {
1006        self.out.join(target).join("test")
1007    }
1008
1009    /// Output directory for all documentation for a target
1010    fn compiler_doc_out(&self, target: TargetSelection) -> PathBuf {
1011        self.out.join(target).join("compiler-doc")
1012    }
1013
1014    /// Output directory for some generated md crate documentation for a target (temporary)
1015    fn md_doc_out(&self, target: TargetSelection) -> PathBuf {
1016        self.out.join(target).join("md-doc")
1017    }
1018
1019    /// Path to the vendored Rust crates.
1020    fn vendored_crates_path(&self) -> Option<PathBuf> {
1021        if self.config.vendor { Some(self.src.join(VENDOR_DIR)) } else { None }
1022    }
1023
1024    /// Returns the path to `FileCheck` binary for the specified target
1025    fn llvm_filecheck(&self, target: TargetSelection) -> PathBuf {
1026        let target_config = self.config.target_config.get(&target);
1027        if let Some(s) = target_config.and_then(|c| c.llvm_filecheck.as_ref()) {
1028            s.to_path_buf()
1029        } else if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
1030            let llvm_bindir = command(s).arg("--bindir").run_capture_stdout(self).stdout();
1031            let filecheck = Path::new(llvm_bindir.trim()).join(exe("FileCheck", target));
1032            if filecheck.exists() {
1033                filecheck
1034            } else {
1035                // On Fedora the system LLVM installs FileCheck in the
1036                // llvm subdirectory of the libdir.
1037                let llvm_libdir = command(s).arg("--libdir").run_capture_stdout(self).stdout();
1038                let lib_filecheck =
1039                    Path::new(llvm_libdir.trim()).join("llvm").join(exe("FileCheck", target));
1040                if lib_filecheck.exists() {
1041                    lib_filecheck
1042                } else {
1043                    // Return the most normal file name, even though
1044                    // it doesn't exist, so that any error message
1045                    // refers to that.
1046                    filecheck
1047                }
1048            }
1049        } else {
1050            let base = self.llvm_out(target).join("build");
1051            let base = if !self.ninja() && target.is_msvc() {
1052                if self.config.llvm_optimize {
1053                    if self.config.llvm_release_debuginfo {
1054                        base.join("RelWithDebInfo")
1055                    } else {
1056                        base.join("Release")
1057                    }
1058                } else {
1059                    base.join("Debug")
1060                }
1061            } else {
1062                base
1063            };
1064            base.join("bin").join(exe("FileCheck", target))
1065        }
1066    }
1067
1068    /// Directory for libraries built from C/C++ code and shared between stages.
1069    fn native_dir(&self, target: TargetSelection) -> PathBuf {
1070        self.out.join(target).join("native")
1071    }
1072
1073    /// Root output directory for rust_test_helpers library compiled for
1074    /// `target`
1075    fn test_helpers_out(&self, target: TargetSelection) -> PathBuf {
1076        self.native_dir(target).join("rust-test-helpers")
1077    }
1078
1079    /// Adds the `RUST_TEST_THREADS` env var if necessary
1080    fn add_rust_test_threads(&self, cmd: &mut BootstrapCommand) {
1081        if env::var_os("RUST_TEST_THREADS").is_none() {
1082            cmd.env("RUST_TEST_THREADS", self.jobs().to_string());
1083        }
1084    }
1085
1086    /// Returns the libdir of the snapshot compiler.
1087    fn rustc_snapshot_libdir(&self) -> PathBuf {
1088        self.rustc_snapshot_sysroot().join(libdir(self.config.host_target))
1089    }
1090
1091    /// Returns the sysroot of the snapshot compiler.
1092    fn rustc_snapshot_sysroot(&self) -> &Path {
1093        static SYSROOT_CACHE: OnceLock<PathBuf> = OnceLock::new();
1094        SYSROOT_CACHE.get_or_init(|| {
1095            command(&self.initial_rustc)
1096                .run_in_dry_run()
1097                .args(["--print", "sysroot"])
1098                .run_capture_stdout(self)
1099                .stdout()
1100                .trim()
1101                .to_owned()
1102                .into()
1103        })
1104    }
1105
1106    fn info(&self, msg: &str) {
1107        match self.config.get_dry_run() {
1108            DryRun::SelfCheck => (),
1109            DryRun::Disabled | DryRun::UserSelected => {
1110                println!("{msg}");
1111            }
1112        }
1113    }
1114
1115    /// Return a `Group` guard for a [`Step`] that:
1116    /// - Performs `action`
1117    ///   - If the action is `Kind::Test`, use [`Build::msg_test`] instead.
1118    /// - On `what`
1119    ///   - Where `what` possibly corresponds to a `mode`
1120    /// - `action` is performed with/on the given compiler (`target_and_stage`).
1121    ///   - Since for some steps it is not possible to pass a single compiler here, it is also
1122    ///     possible to pass the host and stage explicitly.
1123    /// - With a given `target`.
1124    ///
1125    /// [`Step`]: crate::core::builder::Step
1126    #[must_use = "Groups should not be dropped until the Step finishes running"]
1127    #[track_caller]
1128    fn msg(
1129        &self,
1130        action: impl Into<Kind>,
1131        what: impl Display,
1132        mode: impl Into<Option<Mode>>,
1133        target_and_stage: impl Into<TargetAndStage>,
1134        target: impl Into<Option<TargetSelection>>,
1135    ) -> Option<gha::Group> {
1136        let target_and_stage = target_and_stage.into();
1137        let action = action.into();
1138        assert!(
1139            action != Kind::Test,
1140            "Please use `Build::msg_test` instead of `Build::msg(Kind::Test)`"
1141        );
1142
1143        let actual_stage = match mode.into() {
1144            // Std has the same stage as the compiler that builds it
1145            Some(Mode::Std) => target_and_stage.stage,
1146            // Other things have stage corresponding to their build compiler + 1
1147            Some(
1148                Mode::Rustc
1149                | Mode::Codegen
1150                | Mode::ToolBootstrap
1151                | Mode::ToolTarget
1152                | Mode::ToolStd
1153                | Mode::ToolRustcPrivate,
1154            )
1155            | None => target_and_stage.stage + 1,
1156        };
1157
1158        let action = action.description();
1159        let what = what.to_string();
1160        let msg = |fmt| {
1161            let space = if !what.is_empty() { " " } else { "" };
1162            format!("{action} stage{actual_stage} {what}{space}{fmt}")
1163        };
1164        let msg = if let Some(target) = target.into() {
1165            let build_stage = target_and_stage.stage;
1166            let host = target_and_stage.target;
1167            if host == target {
1168                msg(format_args!("(stage{build_stage} -> stage{actual_stage}, {target})"))
1169            } else {
1170                msg(format_args!("(stage{build_stage}:{host} -> stage{actual_stage}:{target})"))
1171            }
1172        } else {
1173            msg(format_args!(""))
1174        };
1175        self.group(&msg)
1176    }
1177
1178    /// Return a `Group` guard for a [`Step`] that tests `what` with the given `stage` and `target`.
1179    /// Use this instead of [`Build::msg`] for test steps, because for them it is not always clear
1180    /// what exactly is a build compiler.
1181    ///
1182    /// [`Step`]: crate::core::builder::Step
1183    #[must_use = "Groups should not be dropped until the Step finishes running"]
1184    #[track_caller]
1185    fn msg_test(
1186        &self,
1187        what: impl Display,
1188        target: TargetSelection,
1189        stage: u32,
1190    ) -> Option<gha::Group> {
1191        let action = Kind::Test.description();
1192        let msg = format!("{action} stage{stage} {what} ({target})");
1193        self.group(&msg)
1194    }
1195
1196    /// Return a `Group` guard for a [`Step`] that is only built once and isn't affected by `--stage`.
1197    ///
1198    /// [`Step`]: crate::core::builder::Step
1199    #[must_use = "Groups should not be dropped until the Step finishes running"]
1200    #[track_caller]
1201    fn msg_unstaged(
1202        &self,
1203        action: impl Into<Kind>,
1204        what: impl Display,
1205        target: TargetSelection,
1206    ) -> Option<gha::Group> {
1207        let action = action.into().description();
1208        let msg = format!("{action} {what} for {target}");
1209        self.group(&msg)
1210    }
1211
1212    #[track_caller]
1213    fn group(&self, msg: &str) -> Option<gha::Group> {
1214        match self.config.get_dry_run() {
1215            DryRun::SelfCheck => None,
1216            DryRun::Disabled | DryRun::UserSelected => Some(gha::group(msg)),
1217        }
1218    }
1219
1220    /// Returns the number of parallel jobs that have been configured for this
1221    /// build.
1222    fn jobs(&self) -> u32 {
1223        self.config.jobs.unwrap_or_else(|| {
1224            std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32
1225        })
1226    }
1227
1228    fn debuginfo_map_to(&self, which: GitRepo, remap_scheme: RemapScheme) -> Option<String> {
1229        if !self.config.rust_remap_debuginfo {
1230            return None;
1231        }
1232
1233        match which {
1234            GitRepo::Rustc => {
1235                let sha = self.rust_sha().unwrap_or(&self.version);
1236
1237                match remap_scheme {
1238                    RemapScheme::Compiler => {
1239                        // For compiler sources, remap via `/rustc-dev/{sha}` to allow
1240                        // distinguishing between compiler sources vs library sources, since
1241                        // `rustc-dev` dist component places them under
1242                        // `$sysroot/lib/rustlib/rustc-src/rust` as opposed to `rust-src`'s
1243                        // `$sysroot/lib/rustlib/src/rust`.
1244                        //
1245                        // Keep this scheme in sync with `rustc_metadata::rmeta::decoder`'s
1246                        // `try_to_translate_virtual_to_real`.
1247                        Some(format!("/rustc-dev/{sha}"))
1248                    }
1249                    RemapScheme::NonCompiler => {
1250                        // For non-compiler sources, use `/rustc/{sha}` remapping scheme.
1251                        Some(format!("/rustc/{sha}"))
1252                    }
1253                }
1254            }
1255            GitRepo::Llvm => Some(String::from("/rustc/llvm")),
1256        }
1257    }
1258
1259    /// Returns the path to the C compiler for the target specified.
1260    fn cc(&self, target: TargetSelection) -> PathBuf {
1261        if self.config.dry_run() {
1262            return PathBuf::new();
1263        }
1264        self.cc[&target].path().into()
1265    }
1266
1267    /// Returns the internal `cc::Tool` for the C compiler.
1268    fn cc_tool(&self, target: TargetSelection) -> Tool {
1269        self.cc[&target].clone()
1270    }
1271
1272    /// Returns the internal `cc::Tool` for the C++ compiler.
1273    fn cxx_tool(&self, target: TargetSelection) -> Tool {
1274        self.cxx[&target].clone()
1275    }
1276
1277    /// Returns C flags that `cc-rs` thinks should be enabled for the
1278    /// specified target by default.
1279    fn cc_handled_clags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
1280        if self.config.dry_run() {
1281            return Vec::new();
1282        }
1283        let base = match c {
1284            CLang::C => self.cc[&target].clone(),
1285            CLang::Cxx => self.cxx[&target].clone(),
1286        };
1287
1288        // Filter out -O and /O (the optimization flags) that we picked up
1289        // from cc-rs, that's up to the caller to figure out.
1290        base.args()
1291            .iter()
1292            .map(|s| s.to_string_lossy().into_owned())
1293            .filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
1294            .collect::<Vec<String>>()
1295    }
1296
1297    /// Returns extra C flags that `cc-rs` doesn't handle.
1298    fn cc_unhandled_cflags(
1299        &self,
1300        target: TargetSelection,
1301        which: GitRepo,
1302        c: CLang,
1303    ) -> Vec<String> {
1304        let mut base = Vec::new();
1305
1306        // If we're compiling C++ on macOS then we add a flag indicating that
1307        // we want libc++ (more filled out than libstdc++), ensuring that
1308        // LLVM/etc are all properly compiled.
1309        if matches!(c, CLang::Cxx) && target.contains("apple-darwin") {
1310            base.push("-stdlib=libc++".into());
1311        }
1312
1313        // Work around an apparently bad MinGW / GCC optimization,
1314        // See: https://lists.llvm.org/pipermail/cfe-dev/2016-December/051980.html
1315        // See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78936
1316        if &*target.triple == "i686-pc-windows-gnu" {
1317            base.push("-fno-omit-frame-pointer".into());
1318        }
1319
1320        if let Some(map_to) = self.debuginfo_map_to(which, RemapScheme::NonCompiler) {
1321            let map = format!("{}={}", self.src.display(), map_to);
1322            let cc = self.cc(target);
1323            if cc.ends_with("clang") || cc.ends_with("gcc") {
1324                base.push(format!("-fdebug-prefix-map={map}"));
1325            } else if cc.ends_with("clang-cl.exe") {
1326                base.push("-Xclang".into());
1327                base.push(format!("-fdebug-prefix-map={map}"));
1328            }
1329        }
1330        base
1331    }
1332
1333    /// Returns the path to the `ar` archive utility for the target specified.
1334    fn ar(&self, target: TargetSelection) -> Option<PathBuf> {
1335        if self.config.dry_run() {
1336            return None;
1337        }
1338        self.ar.get(&target).cloned()
1339    }
1340
1341    /// Returns the path to the `ranlib` utility for the target specified.
1342    fn ranlib(&self, target: TargetSelection) -> Option<PathBuf> {
1343        if self.config.dry_run() {
1344            return None;
1345        }
1346        self.ranlib.get(&target).cloned()
1347    }
1348
1349    /// Returns the path to the C++ compiler for the target specified.
1350    fn cxx(&self, target: TargetSelection) -> Result<PathBuf, String> {
1351        if self.config.dry_run() {
1352            return Ok(PathBuf::new());
1353        }
1354        match self.cxx.get(&target) {
1355            Some(p) => Ok(p.path().into()),
1356            None => Err(format!("target `{target}` is not configured as a host, only as a target")),
1357        }
1358    }
1359
1360    /// Returns the path to the linker for the given target if it needs to be overridden.
1361    fn linker(&self, target: TargetSelection) -> Option<PathBuf> {
1362        if self.config.dry_run() {
1363            return Some(PathBuf::new());
1364        }
1365        if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.clone())
1366        {
1367            Some(linker)
1368        } else if target.contains("vxworks") {
1369            // need to use CXX compiler as linker to resolve the exception functions
1370            // that are only existed in CXX libraries
1371            Some(self.cxx[&target].path().into())
1372        } else if !self.config.is_host_target(target)
1373            && helpers::use_host_linker(target)
1374            && !target.is_msvc()
1375        {
1376            Some(self.cc(target))
1377        } else if self.config.bootstrap_override_lld.is_used()
1378            && self.is_lld_direct_linker(target)
1379            && self.host_target == target
1380        {
1381            match self.config.bootstrap_override_lld {
1382                BootstrapOverrideLld::SelfContained => Some(self.initial_lld.clone()),
1383                BootstrapOverrideLld::External => Some("lld".into()),
1384                BootstrapOverrideLld::None => None,
1385            }
1386        } else {
1387            None
1388        }
1389    }
1390
1391    // Is LLD configured directly through `-Clinker`?
1392    // Only MSVC targets use LLD directly at the moment.
1393    fn is_lld_direct_linker(&self, target: TargetSelection) -> bool {
1394        target.is_msvc()
1395    }
1396
1397    /// Returns if this target should statically link the C runtime, if specified
1398    fn crt_static(&self, target: TargetSelection) -> Option<bool> {
1399        if target.contains("pc-windows-msvc") {
1400            Some(true)
1401        } else {
1402            self.config.target_config.get(&target).and_then(|t| t.crt_static)
1403        }
1404    }
1405
1406    /// Returns the "musl root" for this `target`, if defined.
1407    ///
1408    /// If this is a native target (host is also musl) and no musl-root is given,
1409    /// it falls back to the system toolchain in /usr.
1410    fn musl_root(&self, target: TargetSelection) -> Option<&Path> {
1411        let configured_root = self
1412            .config
1413            .target_config
1414            .get(&target)
1415            .and_then(|t| t.musl_root.as_ref())
1416            .or(self.config.musl_root.as_ref())
1417            .map(|p| &**p);
1418
1419        if self.config.is_host_target(target) && configured_root.is_none() {
1420            Some(Path::new("/usr"))
1421        } else {
1422            configured_root
1423        }
1424    }
1425
1426    /// Returns the "musl libdir" for this `target`.
1427    fn musl_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1428        self.config
1429            .target_config
1430            .get(&target)
1431            .and_then(|t| t.musl_libdir.clone())
1432            .or_else(|| self.musl_root(target).map(|root| root.join("lib")))
1433    }
1434
1435    /// Returns the `lib` directory for the WASI target specified, if
1436    /// configured.
1437    ///
1438    /// This first consults `wasi-root` as configured in per-target
1439    /// configuration, and failing that it assumes that `$WASI_SDK_PATH` is
1440    /// set in the environment, and failing that `None` is returned.
1441    fn wasi_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1442        let configured =
1443            self.config.target_config.get(&target).and_then(|t| t.wasi_root.as_ref()).map(|p| &**p);
1444        if let Some(path) = configured {
1445            return Some(path.join("lib").join(target.to_string()));
1446        }
1447        let mut env_root = self.wasi_sdk_path.clone()?;
1448        env_root.push("share");
1449        env_root.push("wasi-sysroot");
1450        env_root.push("lib");
1451        env_root.push(target.to_string());
1452        Some(env_root)
1453    }
1454
1455    /// Returns `true` if this is a no-std `target`, if defined
1456    fn no_std(&self, target: TargetSelection) -> Option<bool> {
1457        self.config.target_config.get(&target).map(|t| t.no_std)
1458    }
1459
1460    /// Returns `true` if the target will be tested using the `remote-test-client`
1461    /// and `remote-test-server` binaries.
1462    fn remote_tested(&self, target: TargetSelection) -> bool {
1463        self.qemu_rootfs(target).is_some()
1464            || target.contains("android")
1465            || env::var_os("TEST_DEVICE_ADDR").is_some()
1466    }
1467
1468    /// Returns an optional "runner" to pass to `compiletest` when executing
1469    /// test binaries.
1470    ///
1471    /// An example of this would be a WebAssembly runtime when testing the wasm
1472    /// targets.
1473    fn runner(&self, target: TargetSelection) -> Option<String> {
1474        let configured_runner =
1475            self.config.target_config.get(&target).and_then(|t| t.runner.as_ref()).map(|p| &**p);
1476        if let Some(runner) = configured_runner {
1477            return Some(runner.to_owned());
1478        }
1479
1480        if target.starts_with("wasm") && target.contains("wasi") {
1481            self.default_wasi_runner(target)
1482        } else {
1483            None
1484        }
1485    }
1486
1487    /// When a `runner` configuration is not provided and a WASI-looking target
1488    /// is being tested this is consulted to prove the environment to see if
1489    /// there's a runtime already lying around that seems reasonable to use.
1490    fn default_wasi_runner(&self, target: TargetSelection) -> Option<String> {
1491        let mut finder = crate::core::sanity::Finder::new();
1492
1493        // Look for Wasmtime, and for its default options be sure to disable
1494        // its caching system since we're executing quite a lot of tests and
1495        // ideally shouldn't pollute the cache too much.
1496        if let Some(path) = finder.maybe_have("wasmtime")
1497            && let Ok(mut path) = path.into_os_string().into_string()
1498        {
1499            path.push_str(" run -C cache=n --dir .");
1500            // Make sure that tests have access to RUSTC_BOOTSTRAP. This (for example) is
1501            // required for libtest to work on beta/stable channels.
1502            //
1503            // NB: with Wasmtime 20 this can change to `-S inherit-env` to
1504            // inherit the entire environment rather than just this single
1505            // environment variable.
1506            path.push_str(" --env RUSTC_BOOTSTRAP");
1507
1508            if target.contains("wasip2") {
1509                path.push_str(" --wasi inherit-network --wasi allow-ip-name-lookup");
1510            }
1511
1512            return Some(path);
1513        }
1514
1515        None
1516    }
1517
1518    /// Returns whether the specified tool is configured as part of this build.
1519    ///
1520    /// This requires that both the `extended` key is set and the `tools` key is
1521    /// either unset or specifically contains the specified tool.
1522    fn tool_enabled(&self, tool: &str) -> bool {
1523        if !self.config.extended {
1524            return false;
1525        }
1526        match &self.config.tools {
1527            Some(set) => set.contains(tool),
1528            None => true,
1529        }
1530    }
1531
1532    /// Returns the root of the "rootfs" image that this target will be using,
1533    /// if one was configured.
1534    ///
1535    /// If `Some` is returned then that means that tests for this target are
1536    /// emulated with QEMU and binaries will need to be shipped to the emulator.
1537    fn qemu_rootfs(&self, target: TargetSelection) -> Option<&Path> {
1538        self.config.target_config.get(&target).and_then(|t| t.qemu_rootfs.as_ref()).map(|p| &**p)
1539    }
1540
1541    /// Temporary directory that extended error information is emitted to.
1542    fn extended_error_dir(&self) -> PathBuf {
1543        self.out.join("tmp/extended-error-metadata")
1544    }
1545
1546    /// Tests whether the `compiler` compiling for `target` should be forced to
1547    /// use a stage1 compiler instead.
1548    ///
1549    /// Currently, by default, the build system does not perform a "full
1550    /// bootstrap" by default where we compile the compiler three times.
1551    /// Instead, we compile the compiler two times. The final stage (stage2)
1552    /// just copies the libraries from the previous stage, which is what this
1553    /// method detects.
1554    ///
1555    /// Here we return `true` if:
1556    ///
1557    /// * The build isn't performing a full bootstrap
1558    /// * The `compiler` is in the final stage, 2
1559    /// * We're not cross-compiling, so the artifacts are already available in
1560    ///   stage1
1561    ///
1562    /// When all of these conditions are met the build will lift artifacts from
1563    /// the previous stage forward.
1564    fn force_use_stage1(&self, stage: u32, target: TargetSelection) -> bool {
1565        !self.config.full_bootstrap
1566            && !self.config.download_rustc()
1567            && stage >= 2
1568            && (self.hosts.contains(&target) || target == self.host_target)
1569    }
1570
1571    /// Checks whether the `compiler` compiling for `target` should be forced to
1572    /// use a stage2 compiler instead.
1573    ///
1574    /// When we download the pre-compiled version of rustc and compiler stage is >= 2,
1575    /// it should be forced to use a stage2 compiler.
1576    fn force_use_stage2(&self, stage: u32) -> bool {
1577        self.config.download_rustc() && stage >= 2
1578    }
1579
1580    /// Given `num` in the form "a.b.c" return a "release string" which
1581    /// describes the release version number.
1582    ///
1583    /// For example on nightly this returns "a.b.c-nightly", on beta it returns
1584    /// "a.b.c-beta.1" and on stable it just returns "a.b.c".
1585    fn release(&self, num: &str) -> String {
1586        match &self.config.channel[..] {
1587            "stable" => num.to_string(),
1588            "beta" => {
1589                if !self.config.omit_git_hash {
1590                    format!("{}-beta.{}", num, self.beta_prerelease_version())
1591                } else {
1592                    format!("{num}-beta")
1593                }
1594            }
1595            "nightly" => format!("{num}-nightly"),
1596            _ => format!("{num}-dev"),
1597        }
1598    }
1599
1600    fn beta_prerelease_version(&self) -> u32 {
1601        fn extract_beta_rev_from_file<P: AsRef<Path>>(version_file: P) -> Option<String> {
1602            let version = fs::read_to_string(version_file).ok()?;
1603
1604            helpers::extract_beta_rev(&version)
1605        }
1606
1607        if let Some(s) = self.prerelease_version.get() {
1608            return s;
1609        }
1610
1611        // First check if there is a version file available.
1612        // If available, we read the beta revision from that file.
1613        // This only happens when building from a source tarball when Git should not be used.
1614        let count = extract_beta_rev_from_file(self.src.join("version")).unwrap_or_else(|| {
1615            // Figure out how many merge commits happened since we branched off main.
1616            // That's our beta number!
1617            // (Note that we use a `..` range, not the `...` symmetric difference.)
1618            helpers::git(Some(&self.src))
1619                .arg("rev-list")
1620                .arg("--count")
1621                .arg("--merges")
1622                .arg(format!(
1623                    "refs/remotes/origin/{}..HEAD",
1624                    self.config.stage0_metadata.config.nightly_branch
1625                ))
1626                .run_in_dry_run()
1627                .run_capture(self)
1628                .stdout()
1629        });
1630        let n = count.trim().parse().unwrap();
1631        self.prerelease_version.set(Some(n));
1632        n
1633    }
1634
1635    /// Returns the value of `release` above for Rust itself.
1636    fn rust_release(&self) -> String {
1637        self.release(&self.version)
1638    }
1639
1640    /// Returns the "package version" for a component.
1641    ///
1642    /// The package version is typically what shows up in the names of tarballs.
1643    /// For channels like beta/nightly it's just the channel name, otherwise it's the release
1644    /// version.
1645    fn rust_package_vers(&self) -> String {
1646        match &self.config.channel[..] {
1647            "stable" => self.version.to_string(),
1648            "beta" => "beta".to_string(),
1649            "nightly" => "nightly".to_string(),
1650            _ => format!("{}-dev", self.version),
1651        }
1652    }
1653
1654    /// Returns the `version` string associated with this compiler for Rust
1655    /// itself.
1656    ///
1657    /// Note that this is a descriptive string which includes the commit date,
1658    /// sha, version, etc.
1659    fn rust_version(&self) -> String {
1660        let mut version = self.rust_info().version(self, &self.version);
1661        if let Some(ref s) = self.config.description
1662            && !s.is_empty()
1663        {
1664            version.push_str(" (");
1665            version.push_str(s);
1666            version.push(')');
1667        }
1668        version
1669    }
1670
1671    /// Returns the full commit hash.
1672    fn rust_sha(&self) -> Option<&str> {
1673        self.rust_info().sha()
1674    }
1675
1676    /// Returns the `a.b.c` version that the given package is at.
1677    fn release_num(&self, package: &str) -> String {
1678        let toml_file_name = self.src.join(format!("src/tools/{package}/Cargo.toml"));
1679        let toml = t!(fs::read_to_string(toml_file_name));
1680        for line in toml.lines() {
1681            if let Some(stripped) =
1682                line.strip_prefix("version = \"").and_then(|s| s.strip_suffix('"'))
1683            {
1684                return stripped.to_owned();
1685            }
1686        }
1687
1688        panic!("failed to find version in {package}'s Cargo.toml")
1689    }
1690
1691    /// Returns `true` if unstable features should be enabled for the compiler
1692    /// we're building.
1693    fn unstable_features(&self) -> bool {
1694        !matches!(&self.config.channel[..], "stable" | "beta")
1695    }
1696
1697    /// Returns a Vec of all the dependencies of the given root crate,
1698    /// including transitive dependencies and the root itself. Only includes
1699    /// "local" crates (those in the local source tree, not from a registry).
1700    fn in_tree_crates(&self, root: &str, target: Option<TargetSelection>) -> Vec<&Crate> {
1701        let mut ret = Vec::new();
1702        let mut list = vec![root.to_owned()];
1703        let mut visited = HashSet::new();
1704        while let Some(krate) = list.pop() {
1705            let krate = self
1706                .crates
1707                .get(&krate)
1708                .unwrap_or_else(|| panic!("metadata missing for {krate}: {:?}", self.crates));
1709            ret.push(krate);
1710            for dep in &krate.deps {
1711                if !self.crates.contains_key(dep) {
1712                    // Ignore non-workspace members.
1713                    continue;
1714                }
1715                // Don't include optional deps if their features are not
1716                // enabled. Ideally this would be computed from `cargo
1717                // metadata --features …`, but that is somewhat slow. In
1718                // the future, we may want to consider just filtering all
1719                // build and dev dependencies in metadata::build.
1720                if visited.insert(dep)
1721                    && (dep != "profiler_builtins"
1722                        || target
1723                            .map(|t| self.config.profiler_enabled(t))
1724                            .unwrap_or_else(|| self.config.any_profiler_enabled()))
1725                    && (dep != "rustc_codegen_llvm"
1726                        || self.config.hosts.iter().any(|host| self.config.llvm_enabled(*host)))
1727                {
1728                    list.push(dep.clone());
1729                }
1730            }
1731        }
1732        ret.sort_unstable_by_key(|krate| krate.name.clone()); // reproducible order needed for tests
1733        ret
1734    }
1735
1736    fn read_stamp_file(&self, stamp: &BuildStamp) -> Vec<(PathBuf, DependencyType)> {
1737        if self.config.dry_run() {
1738            return Vec::new();
1739        }
1740
1741        if !stamp.path().exists() {
1742            eprintln!(
1743                "ERROR: Unable to find the stamp file {}, did you try to keep a nonexistent build stage?",
1744                stamp.path().display()
1745            );
1746            crate::exit!(1);
1747        }
1748
1749        let mut paths = Vec::new();
1750        let contents = t!(fs::read(stamp.path()), stamp.path());
1751        // This is the method we use for extracting paths from the stamp file passed to us. See
1752        // run_cargo for more information (in compile.rs).
1753        for part in contents.split(|b| *b == 0) {
1754            if part.is_empty() {
1755                continue;
1756            }
1757            let dependency_type = match part[0] as char {
1758                'h' => DependencyType::Host,
1759                's' => DependencyType::TargetSelfContained,
1760                't' => DependencyType::Target,
1761                _ => unreachable!(),
1762            };
1763            let path = PathBuf::from(t!(str::from_utf8(&part[1..])));
1764            paths.push((path, dependency_type));
1765        }
1766        paths
1767    }
1768
1769    /// Copies a file from `src` to `dst`.
1770    ///
1771    /// If `src` is a symlink, `src` will be resolved to the actual path
1772    /// and copied to `dst` instead of the symlink itself.
1773    #[track_caller]
1774    pub fn resolve_symlink_and_copy(&self, src: &Path, dst: &Path) {
1775        self.copy_link_internal(src, dst, true);
1776    }
1777
1778    /// Links a file from `src` to `dst`.
1779    /// Attempts to use hard links if possible, falling back to copying.
1780    /// You can neither rely on this being a copy nor it being a link,
1781    /// so do not write to dst.
1782    #[track_caller]
1783    pub fn copy_link(&self, src: &Path, dst: &Path, file_type: FileType) {
1784        self.copy_link_internal(src, dst, false);
1785
1786        if file_type.could_have_split_debuginfo()
1787            && let Some(dbg_file) = split_debuginfo(src)
1788        {
1789            self.copy_link_internal(
1790                &dbg_file,
1791                &dst.with_extension(dbg_file.extension().unwrap()),
1792                false,
1793            );
1794        }
1795    }
1796
1797    #[track_caller]
1798    fn copy_link_internal(&self, src: &Path, dst: &Path, dereference_symlinks: bool) {
1799        if self.config.dry_run() {
1800            return;
1801        }
1802        if src == dst {
1803            return;
1804        }
1805
1806        #[cfg(feature = "tracing")]
1807        let _span = trace_io!("file-copy-link", ?src, ?dst);
1808
1809        if let Err(e) = fs::remove_file(dst)
1810            && cfg!(windows)
1811            && e.kind() != io::ErrorKind::NotFound
1812        {
1813            // workaround for https://github.com/rust-lang/rust/issues/127126
1814            // if removing the file fails, attempt to rename it instead.
1815            let now = t!(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH));
1816            let _ = fs::rename(dst, format!("{}-{}", dst.display(), now.as_nanos()));
1817        }
1818        let mut metadata = t!(src.symlink_metadata(), format!("src = {}", src.display()));
1819        let mut src = src.to_path_buf();
1820        if metadata.file_type().is_symlink() {
1821            if dereference_symlinks {
1822                src = t!(fs::canonicalize(src));
1823                metadata = t!(fs::metadata(&src), format!("target = {}", src.display()));
1824            } else {
1825                let link = t!(fs::read_link(src));
1826                t!(self.symlink_file(link, dst));
1827                return;
1828            }
1829        }
1830        if let Ok(()) = fs::hard_link(&src, dst) {
1831            // Attempt to "easy copy" by creating a hard link (symlinks are privileged on windows),
1832            // but if that fails just fall back to a slow `copy` operation.
1833        } else {
1834            if let Err(e) = fs::copy(&src, dst) {
1835                panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e)
1836            }
1837            t!(fs::set_permissions(dst, metadata.permissions()));
1838
1839            // Restore file times because changing permissions on e.g. Linux using `chmod` can cause
1840            // file access time to change.
1841            let file_times = fs::FileTimes::new()
1842                .set_accessed(t!(metadata.accessed()))
1843                .set_modified(t!(metadata.modified()));
1844            t!(set_file_times(dst, file_times));
1845        }
1846    }
1847
1848    /// Links the `src` directory recursively to `dst`. Both are assumed to exist
1849    /// when this function is called.
1850    /// Will attempt to use hard links if possible and fall back to copying.
1851    #[track_caller]
1852    pub fn cp_link_r(&self, src: &Path, dst: &Path) {
1853        if self.config.dry_run() {
1854            return;
1855        }
1856        for f in self.read_dir(src) {
1857            let path = f.path();
1858            let name = path.file_name().unwrap();
1859            let dst = dst.join(name);
1860            if t!(f.file_type()).is_dir() {
1861                t!(fs::create_dir_all(&dst));
1862                self.cp_link_r(&path, &dst);
1863            } else {
1864                self.copy_link(&path, &dst, FileType::Regular);
1865            }
1866        }
1867    }
1868
1869    /// Copies the `src` directory recursively to `dst`. Both are assumed to exist
1870    /// when this function is called.
1871    /// Will attempt to use hard links if possible and fall back to copying.
1872    /// Unwanted files or directories can be skipped
1873    /// by returning `false` from the filter function.
1874    #[track_caller]
1875    pub fn cp_link_filtered(&self, src: &Path, dst: &Path, filter: &dyn Fn(&Path) -> bool) {
1876        // Immediately recurse with an empty relative path
1877        self.cp_link_filtered_recurse(src, dst, Path::new(""), filter)
1878    }
1879
1880    // Inner function does the actual work
1881    #[track_caller]
1882    fn cp_link_filtered_recurse(
1883        &self,
1884        src: &Path,
1885        dst: &Path,
1886        relative: &Path,
1887        filter: &dyn Fn(&Path) -> bool,
1888    ) {
1889        for f in self.read_dir(src) {
1890            let path = f.path();
1891            let name = path.file_name().unwrap();
1892            let dst = dst.join(name);
1893            let relative = relative.join(name);
1894            // Only copy file or directory if the filter function returns true
1895            if filter(&relative) {
1896                if t!(f.file_type()).is_dir() {
1897                    let _ = fs::remove_dir_all(&dst);
1898                    self.create_dir(&dst);
1899                    self.cp_link_filtered_recurse(&path, &dst, &relative, filter);
1900                } else {
1901                    self.copy_link(&path, &dst, FileType::Regular);
1902                }
1903            }
1904        }
1905    }
1906
1907    fn copy_link_to_folder(&self, src: &Path, dest_folder: &Path) {
1908        let file_name = src.file_name().unwrap();
1909        let dest = dest_folder.join(file_name);
1910        self.copy_link(src, &dest, FileType::Regular);
1911    }
1912
1913    fn install(&self, src: &Path, dstdir: &Path, file_type: FileType) {
1914        if self.config.dry_run() {
1915            return;
1916        }
1917        let dst = dstdir.join(src.file_name().unwrap());
1918
1919        #[cfg(feature = "tracing")]
1920        let _span = trace_io!("install", ?src, ?dst);
1921
1922        t!(fs::create_dir_all(dstdir));
1923        if !src.exists() {
1924            panic!("ERROR: File \"{}\" not found!", src.display());
1925        }
1926
1927        self.copy_link_internal(src, &dst, true);
1928        chmod(&dst, file_type.perms());
1929
1930        // If this file can have debuginfo, look for split debuginfo and install it too.
1931        if file_type.could_have_split_debuginfo()
1932            && let Some(dbg_file) = split_debuginfo(src)
1933        {
1934            self.install(&dbg_file, dstdir, FileType::Regular);
1935        }
1936    }
1937
1938    fn read(&self, path: &Path) -> String {
1939        if self.config.dry_run() {
1940            return String::new();
1941        }
1942        t!(fs::read_to_string(path))
1943    }
1944
1945    #[track_caller]
1946    fn create_dir(&self, dir: &Path) {
1947        if self.config.dry_run() {
1948            return;
1949        }
1950
1951        #[cfg(feature = "tracing")]
1952        let _span = trace_io!("dir-create", ?dir);
1953
1954        t!(fs::create_dir_all(dir))
1955    }
1956
1957    fn remove_dir(&self, dir: &Path) {
1958        if self.config.dry_run() {
1959            return;
1960        }
1961
1962        #[cfg(feature = "tracing")]
1963        let _span = trace_io!("dir-remove", ?dir);
1964
1965        t!(fs::remove_dir_all(dir))
1966    }
1967
1968    /// Make sure that `dir` will be an empty existing directory after this function ends.
1969    /// If it existed before, it will be first deleted.
1970    fn clear_dir(&self, dir: &Path) {
1971        if self.config.dry_run() {
1972            return;
1973        }
1974
1975        #[cfg(feature = "tracing")]
1976        let _span = trace_io!("dir-clear", ?dir);
1977
1978        let _ = std::fs::remove_dir_all(dir);
1979        self.create_dir(dir);
1980    }
1981
1982    fn read_dir(&self, dir: &Path) -> impl Iterator<Item = fs::DirEntry> {
1983        let iter = match fs::read_dir(dir) {
1984            Ok(v) => v,
1985            Err(_) if self.config.dry_run() => return vec![].into_iter(),
1986            Err(err) => panic!("could not read dir {dir:?}: {err:?}"),
1987        };
1988        iter.map(|e| t!(e)).collect::<Vec<_>>().into_iter()
1989    }
1990
1991    fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, link: Q) -> io::Result<()> {
1992        #[cfg(unix)]
1993        use std::os::unix::fs::symlink as symlink_file;
1994        #[cfg(windows)]
1995        use std::os::windows::fs::symlink_file;
1996        if !self.config.dry_run() { symlink_file(src.as_ref(), link.as_ref()) } else { Ok(()) }
1997    }
1998
1999    /// Returns if config.ninja is enabled, and checks for ninja existence,
2000    /// exiting with a nicer error message if not.
2001    fn ninja(&self) -> bool {
2002        let mut cmd_finder = crate::core::sanity::Finder::new();
2003
2004        if self.config.ninja_in_file {
2005            // Some Linux distros rename `ninja` to `ninja-build`.
2006            // CMake can work with either binary name.
2007            if cmd_finder.maybe_have("ninja-build").is_none()
2008                && cmd_finder.maybe_have("ninja").is_none()
2009            {
2010                eprintln!(
2011                    "
2012Couldn't find required command: ninja (or ninja-build)
2013
2014You should install ninja as described at
2015<https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages>,
2016or set `ninja = false` in the `[llvm]` section of `bootstrap.toml`.
2017Alternatively, set `download-ci-llvm = true` in that `[llvm]` section
2018to download LLVM rather than building it.
2019"
2020                );
2021                exit!(1);
2022            }
2023        }
2024
2025        // If ninja isn't enabled but we're building for MSVC then we try
2026        // doubly hard to enable it. It was realized in #43767 that the msbuild
2027        // CMake generator for MSVC doesn't respect configuration options like
2028        // disabling LLVM assertions, which can often be quite important!
2029        //
2030        // In these cases we automatically enable Ninja if we find it in the
2031        // environment.
2032        if !self.config.ninja_in_file
2033            && self.config.host_target.is_msvc()
2034            && cmd_finder.maybe_have("ninja").is_some()
2035        {
2036            return true;
2037        }
2038
2039        self.config.ninja_in_file
2040    }
2041
2042    pub fn colored_stdout<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
2043        self.colored_stream_inner(StandardStream::stdout, self.config.stdout_is_tty, f)
2044    }
2045
2046    pub fn colored_stderr<R, F: FnOnce(&mut dyn WriteColor) -> R>(&self, f: F) -> R {
2047        self.colored_stream_inner(StandardStream::stderr, self.config.stderr_is_tty, f)
2048    }
2049
2050    fn colored_stream_inner<R, F, C>(&self, constructor: C, is_tty: bool, f: F) -> R
2051    where
2052        C: Fn(ColorChoice) -> StandardStream,
2053        F: FnOnce(&mut dyn WriteColor) -> R,
2054    {
2055        let choice = match self.config.color {
2056            flags::Color::Always => ColorChoice::Always,
2057            flags::Color::Never => ColorChoice::Never,
2058            flags::Color::Auto if !is_tty => ColorChoice::Never,
2059            flags::Color::Auto => ColorChoice::Auto,
2060        };
2061        let mut stream = constructor(choice);
2062        let result = f(&mut stream);
2063        stream.reset().unwrap();
2064        result
2065    }
2066
2067    pub fn exec_ctx(&self) -> &ExecutionContext {
2068        &self.config.exec_ctx
2069    }
2070
2071    pub fn report_summary(&self, path: &Path, start_time: Instant) {
2072        self.config.exec_ctx.profiler().report_summary(path, start_time);
2073    }
2074
2075    #[cfg(feature = "tracing")]
2076    pub fn report_step_graph(self, directory: &Path) {
2077        self.step_graph.into_inner().store_to_dot_files(directory);
2078    }
2079}
2080
2081impl AsRef<ExecutionContext> for Build {
2082    fn as_ref(&self) -> &ExecutionContext {
2083        &self.config.exec_ctx
2084    }
2085}
2086
2087#[cfg(unix)]
2088fn chmod(path: &Path, perms: u32) {
2089    use std::os::unix::fs::*;
2090    t!(fs::set_permissions(path, fs::Permissions::from_mode(perms)));
2091}
2092#[cfg(windows)]
2093fn chmod(_path: &Path, _perms: u32) {}
2094
2095impl Compiler {
2096    pub fn new(stage: u32, host: TargetSelection) -> Self {
2097        Self { stage, host, forced_compiler: false }
2098    }
2099
2100    pub fn forced_compiler(&mut self, forced_compiler: bool) {
2101        self.forced_compiler = forced_compiler;
2102    }
2103
2104    /// Returns `true` if this is a snapshot compiler for `build`'s configuration
2105    pub fn is_snapshot(&self, build: &Build) -> bool {
2106        self.stage == 0 && self.host == build.host_target
2107    }
2108
2109    /// Indicates whether the compiler was forced to use a specific stage.
2110    pub fn is_forced_compiler(&self) -> bool {
2111        self.forced_compiler
2112    }
2113}
2114
2115fn envify(s: &str) -> String {
2116    s.chars()
2117        .map(|c| match c {
2118            '-' => '_',
2119            c => c,
2120        })
2121        .flat_map(|c| c.to_uppercase())
2122        .collect()
2123}
2124
2125/// Ensures that the behavior dump directory is properly initialized.
2126pub fn prepare_behaviour_dump_dir(build: &Build) {
2127    static INITIALIZED: OnceLock<bool> = OnceLock::new();
2128
2129    let dump_path = build.out.join("bootstrap-shims-dump");
2130
2131    let initialized = INITIALIZED.get().unwrap_or(&false);
2132    if !initialized {
2133        // clear old dumps
2134        if dump_path.exists() {
2135            t!(fs::remove_dir_all(&dump_path));
2136        }
2137
2138        t!(fs::create_dir_all(&dump_path));
2139
2140        t!(INITIALIZED.set(true));
2141    }
2142}