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