Skip to main content

bootstrap/core/builder/
mod.rs

1use std::any::{Any, type_name};
2use std::cell::{Cell, RefCell};
3use std::collections::BTreeSet;
4use std::fmt::{Debug, Write};
5use std::hash::Hash;
6use std::ops::Deref;
7use std::path::{Path, PathBuf};
8use std::sync::OnceLock;
9use std::time::{Duration, Instant};
10use std::{env, fs, iter};
11
12use clap::ValueEnum;
13#[cfg(feature = "tracing")]
14use tracing::instrument;
15
16pub(crate) use self::cargo::{Cargo, apply_pgo, cargo_profile_var};
17use crate::core::build_steps::compile::{Std, StdLink, looks_like_codegen_backend};
18use crate::core::build_steps::tool::RustcPrivateCompilers;
19use crate::core::build_steps::{
20    check, clean, clippy, compile, dist, doc, gcc, install, llvm, run, setup, test, tool, vendor,
21};
22use crate::core::builder::step_stack::StepRecord;
23pub use crate::core::builder::step_stack::StepStack;
24use crate::core::compiler::Compiler;
25use crate::core::config::flags::Subcommand;
26use crate::core::config::{DryRun, TargetSelection};
27use crate::core::metadata::Crate;
28use crate::core::session::Session;
29use crate::trace;
30use crate::utils::build_stamp::BuildStamp;
31use crate::utils::cache::Cache;
32use crate::utils::exec::{BootstrapCommand, ExecutionContext, command};
33use crate::utils::helpers::{self, LldThreads, add_dylib_path, exe, libdir, linker_args, t};
34use crate::utils::tracing::format_location;
35
36mod cargo;
37mod cli_paths;
38mod step_stack;
39#[cfg(test)]
40mod tests;
41
42/// Builds and performs different [`Self::kind`]s of stuff and actions, taking
43/// into account build configuration from e.g. bootstrap.toml.
44pub(crate) struct Builder<'a> {
45    /// Build configuration from e.g. bootstrap.toml.
46    pub sess: &'a Session,
47
48    /// The stage to use. Either implicitly determined based on subcommand, or
49    /// explicitly specified with `--stage N`. Normally this is the stage we
50    /// use, but sometimes we want to run steps with a lower stage than this.
51    pub top_stage: u32,
52
53    /// What to build or what action to perform.
54    pub kind: Kind,
55
56    /// A cache of outputs of [`Step`]s so we can avoid running steps we already
57    /// ran.
58    cache: Cache,
59
60    /// A stack of [`Step`]s to run before we can run this builder. The output
61    /// of steps is cached in [`Self::cache`].
62    stack: RefCell<Vec<Box<dyn AnyDebug>>>,
63
64    /// The total amount of time we spent running [`Step`]s in [`Self::stack`].
65    time_spent_on_dependencies: Cell<Duration>,
66
67    /// The paths passed on the command line. Used by steps to figure out what
68    /// to do. For example: with `./x check foo bar` we get `paths=["foo",
69    /// "bar"]`.
70    pub paths: Vec<PathBuf>,
71
72    /// Cached list of submodules from self.sess.src.
73    submodule_paths_cache: OnceLock<Vec<String>>,
74
75    /// When enabled by tests, this causes the top-level steps that _would_ be
76    /// executed to be logged instead. Used by snapshot tests of command-line
77    /// paths-to-steps handling.
78    #[expect(clippy::type_complexity)]
79    log_cli_step_for_tests:
80        Option<Box<dyn Fn(&CommandLineStepDescription, &[PathSet], &[TargetSelection])>>,
81}
82
83impl Deref for Builder<'_> {
84    type Target = Session;
85
86    fn deref(&self) -> &Self::Target {
87        self.sess
88    }
89}
90
91/// This trait is similar to `Any`, except that it also exposes the underlying
92/// type's [`Debug`] implementation.
93///
94/// (Trying to debug-print `dyn Any` results in the unhelpful `"Any { .. }"`.)
95pub trait AnyDebug: Any + Debug {}
96impl<T: Any + Debug> AnyDebug for T {}
97impl dyn AnyDebug {
98    /// Equivalent to `<dyn Any>::downcast_ref`.
99    fn downcast_ref<T: Any>(&self) -> Option<&T> {
100        (self as &dyn Any).downcast_ref()
101    }
102
103    // Feel free to add other `dyn Any` methods as necessary.
104}
105
106/// A unit of work within bootstrap that is cached to avoid redundant execution.
107/// Steps can be performed via [`Builder::ensure`].
108///
109/// Historically, steps also participated in command-line processing.
110/// That responsibility has been split off into the larger [`CommandLineStep`] trait,
111/// which helper steps don't need to implement.
112pub(crate) trait Step: 'static + Clone + Debug + PartialEq + Eq + Hash {
113    /// Result type of [`Step::run`]. Stored in the step cache for later lookup.
114    type Output: Clone;
115
116    /// Executes this step.
117    ///
118    /// Called by [`Builder::ensure`] if no cached result was found for this step.
119    fn run(self, builder: &Builder<'_>) -> Self::Output;
120
121    /// Returns metadata of the step, for tests.
122    #[cfg_attr(not(any(test, feature = "tracing")), expect(dead_code))]
123    fn metadata(&self) -> Option<StepMetadata> {
124        None
125    }
126}
127
128/// Every [`CommandLineStep`] is also a [`Step`].
129impl<S: CommandLineStep> Step for S {
130    type Output = <S as CommandLineStep>::Output;
131
132    fn run(self, builder: &Builder<'_>) -> Self::Output {
133        <S as CommandLineStep>::run(self, builder)
134    }
135
136    fn metadata(&self) -> Option<StepMetadata> {
137        <S as CommandLineStep>::metadata(self)
138    }
139}
140
141/// A [`Step`] that can be selected by command-line arguments.
142///
143/// A blanket impl allows every [`CommandLineStep`] to be used as a [`Step`].
144/// This is arguably nicer than having it be a subtrait, because it avoids the
145/// need for two separate `impl` blocks per command-line-step type.
146pub(crate) trait CommandLineStep: 'static + Clone + Debug + PartialEq + Eq + Hash {
147    /// Result type of [`Step::run`].
148    type Output: Clone;
149
150    /// If this value is true, then the values of `run.target` passed to the `make_run` function of
151    /// this Step will be determined based on the `--host` flag.
152    /// If this value is false, then they will be determined based on the `--target` flag.
153    ///
154    /// A corollary of the above is that if this is set to true, then the step will be skipped if
155    /// `--target` was specified, but `--host` was explicitly set to '' (empty string).
156    const IS_HOST: bool = false;
157
158    /// Called to allow steps to register the command-line paths that should
159    /// cause them to run.
160    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_>;
161
162    /// Should this step run when the user invokes bootstrap with a subcommand
163    /// but no paths/aliases?
164    ///
165    /// For example, `./x test` runs all default test steps, and `./x dist`
166    /// runs all default dist steps.
167    ///
168    /// Most steps are always default or always non-default, and just return
169    /// true or false. But some steps are conditionally default, based on
170    /// bootstrap config or the availability of ambient tools.
171    ///
172    /// If the underlying check should not be performed repeatedly
173    /// (e.g. because it probes command-line tools),
174    /// consider memoizing its outcome via a field in the builder.
175    fn is_default_step(_builder: &Builder<'_>) -> bool {
176        false
177    }
178
179    /// Called directly by the bootstrap `Step` handler when not triggered indirectly by other `Step`s using [`Builder::ensure`].
180    /// For example, `./x.py test bootstrap` runs this for `test::Bootstrap`. Similarly, `./x.py test` runs it for every step
181    /// that is listed by the `describe` macro in [`Builder::get_step_descriptions`].
182    fn make_run(_run: RunConfig<'_>);
183
184    /// Used as the implementation of [`Step::run`].
185    fn run(self, builder: &Builder<'_>) -> Self::Output;
186
187    /// Used as the implementation of [`Step::metadata`].
188    fn metadata(&self) -> Option<StepMetadata> {
189        None
190    }
191}
192
193/// Metadata that describes an executed step, mostly for testing and tracing.
194#[derive(Clone, Debug, PartialEq, Eq)]
195pub(crate) struct StepMetadata {
196    name: String,
197    kind: Kind,
198    target: TargetSelection,
199    built_by: Option<Compiler>,
200    stage: Option<u32>,
201    /// Additional opaque string printed in the metadata
202    metadata: Option<String>,
203}
204
205impl StepMetadata {
206    pub fn build(name: &str, target: TargetSelection) -> Self {
207        Self::new(name, target, Kind::Build)
208    }
209
210    pub fn check(name: &str, target: TargetSelection) -> Self {
211        Self::new(name, target, Kind::Check)
212    }
213
214    pub fn clippy(name: &str, target: TargetSelection) -> Self {
215        Self::new(name, target, Kind::Clippy)
216    }
217
218    pub fn doc(name: &str, target: TargetSelection) -> Self {
219        Self::new(name, target, Kind::Doc)
220    }
221
222    pub fn dist(name: &str, target: TargetSelection) -> Self {
223        Self::new(name, target, Kind::Dist)
224    }
225
226    pub fn test(name: &str, target: TargetSelection) -> Self {
227        Self::new(name, target, Kind::Test)
228    }
229
230    pub fn run(name: &str, target: TargetSelection) -> Self {
231        Self::new(name, target, Kind::Run)
232    }
233
234    pub fn new(name: &str, target: TargetSelection, kind: Kind) -> Self {
235        Self { name: name.to_string(), kind, target, built_by: None, stage: None, metadata: None }
236    }
237
238    pub fn built_by(mut self, compiler: Compiler) -> Self {
239        self.built_by = Some(compiler);
240        self
241    }
242
243    pub fn stage(mut self, stage: u32) -> Self {
244        self.stage = Some(stage);
245        self
246    }
247
248    pub fn with_metadata(mut self, metadata: String) -> Self {
249        self.metadata = Some(metadata);
250        self
251    }
252
253    #[cfg_attr(not(any(test, feature = "tracing")), expect(dead_code))]
254    pub(crate) fn get_stage(&self) -> Option<u32> {
255        self.stage.or(self
256            .built_by
257            // For std, its stage corresponds to the stage of the compiler that builds it.
258            // For everything else, a stage N things gets built by a stage N-1 compiler.
259            .map(|compiler| if self.name == "std" { compiler.stage } else { compiler.stage + 1 }))
260    }
261
262    #[cfg_attr(not(feature = "tracing"), expect(dead_code))]
263    pub(crate) fn get_name(&self) -> &str {
264        &self.name
265    }
266
267    #[cfg_attr(not(feature = "tracing"), expect(dead_code))]
268    pub(crate) fn get_target(&self) -> TargetSelection {
269        self.target
270    }
271}
272
273pub struct RunConfig<'a> {
274    pub builder: &'a Builder<'a>,
275    pub target: TargetSelection,
276    pub paths: Vec<PathSet>,
277}
278
279impl RunConfig<'_> {
280    pub fn build_triple(&self) -> TargetSelection {
281        self.builder.sess.host_target
282    }
283
284    /// Return a list of crate names selected by `run.paths`.
285    #[track_caller]
286    pub fn cargo_crates_in_set(&self) -> Vec<String> {
287        let mut crates = Vec::new();
288        for krate in &self.paths {
289            let path = &krate.assert_single_path().path;
290
291            let crate_name = self
292                .builder
293                .crate_paths
294                .get(path)
295                .unwrap_or_else(|| panic!("missing crate for path {}", path.display()));
296
297            crates.push(crate_name.to_string());
298        }
299        crates
300    }
301
302    /// Given an `alias` selected by the `Step` and the paths passed on the command line,
303    /// return a list of the crates that should be built.
304    ///
305    /// Normally, people will pass *just* `library` if they pass it.
306    /// But it's possible (although strange) to pass something like `library std core`.
307    /// Build all crates anyway, as if they hadn't passed the other args.
308    pub fn make_run_crates(&self, alias: Alias) -> Vec<String> {
309        let has_alias =
310            self.paths.iter().any(|set| set.assert_single_path().path.ends_with(alias.as_str()));
311        if !has_alias {
312            return self.cargo_crates_in_set();
313        }
314
315        let crates = match alias {
316            Alias::Library => self.builder.in_tree_crates("sysroot", Some(self.target)),
317            Alias::Compiler => self.builder.in_tree_crates("rustc-main", Some(self.target)),
318        };
319
320        crates.into_iter().map(|krate| krate.name.to_string()).collect()
321    }
322}
323
324#[derive(Debug, Copy, Clone)]
325pub enum Alias {
326    Library,
327    Compiler,
328}
329
330impl Alias {
331    fn as_str(self) -> &'static str {
332        match self {
333            Alias::Library => "library",
334            Alias::Compiler => "compiler",
335        }
336    }
337}
338
339/// A description of the crates in this set, suitable for passing to `builder.info`.
340///
341/// `crates` should be generated by [`RunConfig::cargo_crates_in_set`].
342pub fn crate_description(crates: &[impl AsRef<str>]) -> String {
343    if crates.is_empty() {
344        return "".into();
345    }
346
347    let mut descr = String::from("{");
348    descr.push_str(crates[0].as_ref());
349    for krate in &crates[1..] {
350        descr.push_str(", ");
351        descr.push_str(krate.as_ref());
352    }
353    descr.push('}');
354    descr
355}
356
357struct CommandLineStepDescription {
358    is_host: bool,
359    should_run: fn(ShouldRun<'_>) -> ShouldRun<'_>,
360    is_default_step_fn: fn(&Builder<'_>) -> bool,
361    make_run: fn(RunConfig<'_>),
362    name: &'static str,
363
364    /// Kind that was passed to [`CommandLineStepDescription::from`].
365    #[cfg_attr(not(test), expect(dead_code, reason = "currently only needed by tests"))]
366    kind: Kind,
367}
368
369#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
370pub struct TaskPath {
371    pub path: PathBuf,
372}
373
374impl Debug for TaskPath {
375    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
376        write!(f, "{}", self.path.display())
377    }
378}
379
380/// Collection of paths used to match a task rule.
381#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
382pub enum PathSet {
383    /// A collection of individual paths or aliases.
384    ///
385    /// These are generally matched as a path suffix. For example, a
386    /// command-line value of `std` will match if `library/std` is in the
387    /// set.
388    ///
389    /// NOTE: the paths within a set should all select the same unit of work.
390    /// For example, `src/librustdoc` and `src/tools/rustdoc` should be in the same set,
391    /// but `library/core` and `library/std` generally should not, unless there's no way (for that Step)
392    /// to build them separately.
393    Set(BTreeSet<TaskPath>),
394    /// A "suite" of paths.
395    ///
396    /// These can match as a path suffix (like `Set`), or as a prefix. For
397    /// example, a command-line value of `tests/ui/abi/variadic-ffi.rs`
398    /// will match `tests/ui`. A command-line value of `ui` would also
399    /// match `tests/ui`.
400    Suite(TaskPath),
401}
402
403impl PathSet {
404    fn one<P: Into<PathBuf>>(path: P) -> PathSet {
405        let mut set = BTreeSet::new();
406        set.insert(TaskPath { path: path.into() });
407        PathSet::Set(set)
408    }
409
410    fn has(&self, needle: &Path) -> bool {
411        match self {
412            PathSet::Set(set) => set.iter().any(|p| Self::check(p, needle)),
413            PathSet::Suite(suite) => Self::check(suite, needle),
414        }
415    }
416
417    // internal use only
418    fn check(p: &TaskPath, needle: &Path) -> bool {
419        // This order is important for retro-compatibility, as `starts_with` was introduced later.
420        p.path.ends_with(needle) || p.path.starts_with(needle)
421    }
422
423    /// A convenience wrapper for Steps which know they have no aliases and all their sets contain only a single path.
424    ///
425    /// This can be used with [`ShouldRun::crate_or_deps`], [`ShouldRun::path`], or [`ShouldRun::alias`].
426    #[track_caller]
427    pub fn assert_single_path(&self) -> &TaskPath {
428        match self {
429            PathSet::Set(set) => {
430                assert_eq!(set.len(), 1, "called assert_single_path on multiple paths");
431                set.iter().next().unwrap()
432            }
433            PathSet::Suite(_) => unreachable!("called assert_single_path on a Suite path"),
434        }
435    }
436}
437
438impl CommandLineStepDescription {
439    fn from<S: CommandLineStep>(kind: Kind) -> CommandLineStepDescription {
440        CommandLineStepDescription {
441            is_host: S::IS_HOST,
442            should_run: S::should_run,
443            is_default_step_fn: S::is_default_step,
444            make_run: S::make_run,
445            name: std::any::type_name::<S>(),
446            kind,
447        }
448    }
449
450    fn maybe_run(&self, builder: &Builder<'_>, mut pathsets: Vec<PathSet>) {
451        pathsets.retain(|set| !self.is_excluded(builder, set));
452
453        if pathsets.is_empty() {
454            return;
455        }
456
457        // Determine the targets participating in this rule.
458        let targets = if self.is_host { &builder.hosts } else { &builder.targets };
459
460        // Log the step that's about to run, for snapshot tests.
461        if let Some(ref log_cli_step) = builder.log_cli_step_for_tests {
462            log_cli_step(self, &pathsets, targets);
463            // Return so that the step won't actually run in snapshot tests.
464            return;
465        }
466
467        for target in targets {
468            let run = RunConfig { builder, paths: pathsets.clone(), target: *target };
469            (self.make_run)(run);
470        }
471    }
472
473    fn is_excluded(&self, builder: &Builder<'_>, pathset: &PathSet) -> bool {
474        if builder.config.skip.iter().any(|e| pathset.has(e)) {
475            if !matches!(builder.config.get_dry_run(), DryRun::SelfCheck) {
476                println!("Skipping {pathset:?} because it is excluded");
477            }
478            return true;
479        }
480
481        if !builder.config.skip.is_empty()
482            && !matches!(builder.config.get_dry_run(), DryRun::SelfCheck)
483        {
484            builder.do_if_verbose(|| {
485                println!(
486                    "{:?} not skipped for {:?} -- not in {:?}",
487                    pathset, self.name, builder.config.skip
488                )
489            });
490        }
491        false
492    }
493}
494
495/// Builder that allows steps to register command-line paths/aliases that
496/// should cause those steps to be run.
497///
498/// For example, if the user invokes `./x test compiler` or `./x doc unstable-book`,
499/// this allows bootstrap to determine what steps "compiler" or "unstable-book"
500/// correspond to.
501pub struct ShouldRun<'a> {
502    pub builder: &'a Builder<'a>,
503
504    // use a BTreeSet to maintain sort order
505    paths: BTreeSet<PathSet>,
506}
507
508impl<'a> ShouldRun<'a> {
509    fn new(builder: &'a Builder<'_>) -> ShouldRun<'a> {
510        ShouldRun { builder, paths: BTreeSet::new() }
511    }
512
513    /// The corresponding step should run if the bootstrap command-line selects
514    /// the given crate or any of its (local) dependencies.
515    ///
516    /// Delegates to [`Self::crate_or_deps_filtered`] with a filter that accepts all crates.
517    pub(crate) fn crate_or_deps(self, root_crate_name: &str) -> Self {
518        self.crate_or_deps_filtered(root_crate_name, |_: &Crate| true)
519    }
520
521    /// The corresponding step should run if the bootstrap command-line selects
522    /// the given crate or any of its (local) dependencies, not counting any
523    /// crates rejected by the given filter function.
524    ///
525    /// `make_run` will be called a single time with all matching command-line paths.
526    pub(crate) fn crate_or_deps_filtered(
527        mut self,
528        root_crate_name: &str,
529        crate_filter_fn: impl Fn(&Crate) -> bool,
530    ) -> Self {
531        let crates = self.builder.in_tree_crates(root_crate_name, None);
532        for krate in crates {
533            if !crate_filter_fn(krate) {
534                continue;
535            }
536
537            let path = krate.local_path(self.builder);
538            self.paths.insert(PathSet::one(path));
539        }
540        self
541    }
542
543    // single alias, which does not correspond to any on-disk path
544    pub fn alias(self, alias: &str) -> Self {
545        self.assert_valid_alias(alias);
546        self.alias_without_assert(alias)
547    }
548
549    /// Like [`Self::alias`], but does not assert the absence of a path with the same name.
550    ///
551    /// Needed by [`setup::Profile`], which registers aliases named `compiler` and `library`
552    /// that happen to coincide with directory names.
553    pub fn alias_without_assert(mut self, alias: &str) -> Self {
554        self.paths.insert(PathSet::Set(iter::once(TaskPath { path: alias.into() }).collect()));
555        self
556    }
557
558    fn assert_valid_alias(&self, alias: &str) {
559        assert!(
560            !self.builder.src.join(alias).exists(),
561            "use `builder.path()` for real paths: {alias}"
562        );
563    }
564
565    fn assert_valid_path(&self, path: &str) {
566        let submodules_paths = self.builder.submodule_paths();
567
568        // assert only if `p` isn't submodule
569        if !submodules_paths.iter().any(|sm_p| path.contains(sm_p)) {
570            assert!(
571                self.builder.src.join(path).exists(),
572                "`should_run.path` should correspond to a real on-disk path - use `alias` if there is no relevant path: {path}"
573            );
574        }
575    }
576
577    /// A single path
578    ///
579    /// Must be an on-disk path; use [`alias`][Self::alias] for names that do not
580    /// correspond to on-disk paths.
581    pub fn path(mut self, path: &str) -> Self {
582        self.assert_valid_path(path);
583
584        let task = TaskPath { path: path.into() };
585        self.paths.insert(PathSet::Set(BTreeSet::from_iter([task])));
586        self
587    }
588
589    /// Registers a path, and an alias that is treated as equivalent to that path.
590    pub fn path_with_alias(mut self, path: &str, alias: &str) -> Self {
591        self.assert_valid_path(path);
592        self.assert_valid_alias(alias);
593
594        let set = [path, alias]
595            .into_iter()
596            .map(|p| TaskPath { path: PathBuf::from(p) })
597            .collect::<BTreeSet<_>>();
598        self.paths.insert(PathSet::Set(set));
599        self
600    }
601
602    /// Multiple on-disk paths that should select the same unit of work.
603    pub fn multi_path(mut self, paths: &[&str]) -> Self {
604        let mut set = BTreeSet::new();
605        for path in paths {
606            self.assert_valid_path(path);
607            set.insert(TaskPath { path: (*path).into() });
608        }
609        self.paths.insert(PathSet::Set(set));
610        self
611    }
612
613    pub fn suite_path(mut self, suite: &str) -> Self {
614        self.paths.insert(PathSet::Suite(TaskPath { path: suite.into() }));
615        self
616    }
617
618    /// When the corresponding step is run "by default" (without explicit command-line paths),
619    /// act as though the user had explicitly specified these paths.
620    fn default_pathsets(&self) -> Vec<PathSet> {
621        self.paths.iter().cloned().collect::<Vec<_>>()
622    }
623}
624
625#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord, ValueEnum)]
626pub enum Kind {
627    #[value(alias = "b")]
628    Build,
629    #[value(alias = "c")]
630    Check,
631    Clippy,
632    Fix,
633    Format,
634    #[value(alias = "t")]
635    Test,
636    Miri,
637    MiriSetup,
638    MiriTest,
639    Bench,
640    #[value(alias = "d")]
641    Doc,
642    Clean,
643    Dist,
644    Install,
645    #[value(alias = "r")]
646    Run,
647    Setup,
648    Vendor,
649    Perf,
650}
651
652impl Kind {
653    pub fn as_str(&self) -> &'static str {
654        match self {
655            Kind::Build => "build",
656            Kind::Check => "check",
657            Kind::Clippy => "clippy",
658            Kind::Fix => "fix",
659            Kind::Format => "fmt",
660            Kind::Test => "test",
661            Kind::Miri => "miri",
662            Kind::MiriSetup => panic!("`as_str` is not supported for `Kind::MiriSetup`."),
663            Kind::MiriTest => panic!("`as_str` is not supported for `Kind::MiriTest`."),
664            Kind::Bench => "bench",
665            Kind::Doc => "doc",
666            Kind::Clean => "clean",
667            Kind::Dist => "dist",
668            Kind::Install => "install",
669            Kind::Run => "run",
670            Kind::Setup => "setup",
671            Kind::Vendor => "vendor",
672            Kind::Perf => "perf",
673        }
674    }
675
676    pub fn description(&self) -> String {
677        match self {
678            Kind::Test => "Testing",
679            Kind::Bench => "Benchmarking",
680            Kind::Doc => "Documenting",
681            Kind::Run => "Running",
682            Kind::Clippy => "Linting",
683            Kind::Perf => "Profiling & benchmarking",
684            _ => {
685                let title_letter = self.as_str()[0..1].to_ascii_uppercase();
686                return format!("{title_letter}{}ing", &self.as_str()[1..]);
687            }
688        }
689        .to_owned()
690    }
691
692    /// Is this a command similar to check, which only runs the compiler frontend and doesn't
693    /// build code for the target? (it can still build code for the host, i.e. proc macros).
694    pub fn is_check_like(&self) -> bool {
695        match self {
696            Kind::Check | Kind::Clippy | Kind::Fix | Kind::Doc => true,
697            Kind::Build
698            | Kind::Format
699            | Kind::Test
700            | Kind::Miri
701            | Kind::MiriSetup
702            | Kind::MiriTest
703            | Kind::Bench
704            | Kind::Clean
705            | Kind::Dist
706            | Kind::Install
707            | Kind::Run
708            | Kind::Setup
709            | Kind::Vendor
710            | Kind::Perf => false,
711        }
712    }
713}
714
715#[derive(Debug, Clone, Hash, PartialEq, Eq)]
716struct Libdir {
717    compiler: Compiler,
718    target: TargetSelection,
719}
720
721impl Step for Libdir {
722    type Output = PathBuf;
723
724    fn run(self, builder: &Builder<'_>) -> PathBuf {
725        let relative_sysroot_libdir = builder.sysroot_libdir_relative(self.compiler);
726        let sysroot = builder.sysroot(self.compiler).join(relative_sysroot_libdir).join("rustlib");
727
728        if !builder.config.dry_run() {
729            // Avoid deleting the `rustlib/` directory we just copied (in `impl CommandLineStep for
730            // Sysroot`).
731            if !builder.download_rustc() {
732                let sysroot_target_libdir = sysroot.join(self.target).join("lib");
733                builder.do_if_verbose(|| {
734                    eprintln!(
735                        "Removing sysroot {} to avoid caching bugs",
736                        sysroot_target_libdir.display()
737                    )
738                });
739                let _ = fs::remove_dir_all(&sysroot_target_libdir);
740                t!(fs::create_dir_all(&sysroot_target_libdir));
741            }
742
743            if self.compiler.stage == 0 {
744                // The stage 0 compiler for the build triple is always pre-built. Ensure that
745                // `libLLVM.so` ends up in the target libdir, so that ui-fulldeps tests can use
746                // it when run.
747                dist::maybe_install_llvm_target(
748                    builder,
749                    self.compiler.host,
750                    &builder.sysroot(self.compiler),
751                );
752            }
753        }
754
755        sysroot
756    }
757}
758
759#[cfg(feature = "tracing")]
760pub const STEP_SPAN_TARGET: &str = "STEP";
761
762impl<'a> Builder<'a> {
763    fn get_step_descriptions(kind: Kind) -> Vec<CommandLineStepDescription> {
764        macro_rules! describe {
765            ($($rule:ty),+ $(,)?) => {{
766                vec![$(CommandLineStepDescription::from::<$rule>(kind)),+]
767            }};
768        }
769        match kind {
770            Kind::Build => describe!(
771                compile::Std,
772                compile::Rustc,
773                compile::Assemble,
774                compile::CraneliftCodegenBackend,
775                compile::GccCodegenBackend,
776                compile::StartupObjects,
777                tool::BuildManifest,
778                tool::Rustbook,
779                tool::ErrorIndex,
780                tool::UnstableBookGen,
781                tool::Tidy,
782                tool::Linkchecker,
783                tool::CargoTest,
784                tool::Compiletest,
785                tool::RemoteTestServer,
786                tool::RemoteTestClient,
787                tool::RustInstaller,
788                tool::FeaturesStatusDump,
789                tool::Cargo,
790                tool::RustAnalyzer,
791                tool::RustAnalyzerProcMacroSrv,
792                tool::Rustdoc,
793                tool::Clippy,
794                tool::CargoClippy,
795                llvm::Llvm,
796                gcc::Gcc,
797                llvm::Sanitizers,
798                tool::Rustfmt,
799                tool::Cargofmt,
800                tool::Miri,
801                tool::CargoMiri,
802                llvm::Lld,
803                llvm::Enzyme,
804                llvm::RustOffload,
805                llvm::CrtBeginEnd,
806                tool::RustdocGUITest,
807                tool::OptimizedDist,
808                tool::CoverageDump,
809                tool::LlvmBitcodeLinker,
810                tool::RustcPerf,
811                tool::WasmComponentLd,
812                tool::LldWrapper
813            ),
814            Kind::Clippy => describe!(
815                clippy::Std,
816                clippy::Rustc,
817                clippy::Bootstrap,
818                clippy::BuildHelper,
819                clippy::BuildManifest,
820                clippy::CargoMiri,
821                clippy::Clippy,
822                clippy::CodegenGcc,
823                clippy::CollectLicenseMetadata,
824                clippy::Compiletest,
825                clippy::CoverageDump,
826                clippy::Jsondocck,
827                clippy::Jsondoclint,
828                clippy::LintDocs,
829                clippy::LlvmBitcodeLinker,
830                clippy::Miri,
831                clippy::MiroptTestTools,
832                clippy::OptDist,
833                clippy::RemoteTestClient,
834                clippy::RemoteTestServer,
835                clippy::RustAnalyzer,
836                clippy::Rustdoc,
837                clippy::Rustfmt,
838                clippy::RustInstaller,
839                clippy::TestFloatParse,
840                clippy::Tidy,
841                clippy::CI,
842            ),
843            Kind::Check | Kind::Fix => describe!(
844                check::Rustc,
845                check::Rustdoc,
846                check::CraneliftCodegenBackend,
847                check::GccCodegenBackend,
848                check::Clippy,
849                check::Miri,
850                check::CargoMiri,
851                check::Priroda,
852                check::MiroptTestTools,
853                check::Rustfmt,
854                check::RustAnalyzer,
855                check::TestFloatParse,
856                check::Bootstrap,
857                check::RunMakeSupport,
858                check::Compiletest,
859                check::RustdocGuiTest,
860                check::FeaturesStatusDump,
861                check::CoverageDump,
862                check::Linkchecker,
863                check::BumpStage0,
864                check::Tidy,
865                // This has special staging logic, it may run on stage 1 while others run on stage 0.
866                // It takes quite some time to build stage 1, so put this at the end.
867                //
868                // FIXME: This also helps bootstrap to not interfere with stage 0 builds. We should probably fix
869                // that issue somewhere else, but we still want to keep `check::Std` at the end so that the
870                // quicker steps run before this.
871                check::Std,
872            ),
873            Kind::Test => describe!(
874                crate::core::build_steps::toolstate::ToolStateCheck,
875                test::Tidy,
876                test::BootstrapPy,
877                test::Bootstrap,
878                test::Ui,
879                test::Crashes,
880                test::Coverage,
881                test::CoverageModeAlias,
882                test::MirOpt,
883                test::CodegenLlvm,
884                test::CodegenUnits,
885                test::AssemblyLlvm,
886                test::Incremental,
887                test::Debuginfo,
888                test::UiFullDeps,
889                test::RustdocHtml,
890                test::CoverageRunRustdoc,
891                test::Pretty,
892                test::CodegenCranelift,
893                test::CodegenGCC,
894                test::Crate,
895                test::CrateLibrustc,
896                test::CrateRustdoc,
897                test::CrateRustdocJsonTypes,
898                test::CrateBootstrap,
899                test::RemoteTestClientTests,
900                test::Linkcheck,
901                test::TierCheck,
902                test::Cargotest,
903                test::Cargo,
904                test::RustAnalyzer,
905                test::ErrorIndex,
906                test::Distcheck,
907                test::Nomicon,
908                test::Reference,
909                test::RustdocBook,
910                test::RustByExample,
911                test::TheBook,
912                test::UnstableBook,
913                test::RustcBook,
914                test::LintDocs,
915                test::EmbeddedBook,
916                test::EditionGuide,
917                test::Rustfmt,
918                test::Miri,
919                test::CargoMiri,
920                test::Priroda,
921                test::Clippy,
922                test::CompiletestTest,
923                test::StdarchVerify,
924                test::CrateRunMakeSupport,
925                test::CrateBuildHelper,
926                test::RustdocJSStd,
927                test::RustdocJSNotStd,
928                test::RustdocGUI,
929                test::RustdocTheme,
930                test::RustdocUi,
931                test::RustdocJson,
932                test::HtmlCheck,
933                test::RustInstaller,
934                test::TestFloatParse,
935                test::CollectLicenseMetadata,
936                test::RunMake,
937                test::RunMakeCargo,
938                test::BuildStd,
939                test::StdSemverCheck,
940                test::IntrinsicTest,
941            ),
942            Kind::Miri => describe!(test::Crate),
943            Kind::Bench => describe!(test::Crate, test::CrateLibrustc, test::CrateRustdoc),
944            Kind::Doc => describe!(
945                doc::UnstableBook,
946                doc::UnstableBookGen,
947                doc::TheBook,
948                doc::Standalone,
949                doc::Std,
950                doc::Rustc,
951                doc::Rustdoc,
952                doc::Rustfmt,
953                doc::ErrorIndex,
954                doc::Nomicon,
955                doc::Reference,
956                doc::RustdocBook,
957                doc::RustByExample,
958                doc::RustcBook,
959                doc::Cargo,
960                doc::CargoBook,
961                doc::Clippy,
962                doc::ClippyBook,
963                doc::Miri,
964                doc::EmbeddedBook,
965                doc::EditionGuide,
966                doc::StyleGuide,
967                doc::Tidy,
968                doc::Bootstrap,
969                doc::Releases,
970                doc::RunMakeSupport,
971                doc::BuildHelper,
972                doc::Compiletest,
973            ),
974            Kind::Dist => describe!(
975                dist::Docs,
976                dist::RustcDocs,
977                dist::JsonDocs,
978                dist::Mingw,
979                dist::Rustc,
980                dist::CraneliftCodegenBackend,
981                dist::GccCodegenBackend,
982                dist::Std,
983                dist::RustcDev,
984                dist::Analysis,
985                dist::Src,
986                dist::Cargo,
987                dist::RustAnalyzer,
988                dist::Rustfmt,
989                dist::Clippy,
990                dist::Miri,
991                dist::LlvmTools,
992                dist::LlvmBitcodeLinker,
993                dist::RustDev,
994                dist::Enzyme,
995                dist::Offload,
996                dist::Bootstrap,
997                dist::Extended,
998                // It seems that PlainSourceTarball somehow changes how some of the tools
999                // perceive their dependencies (see #93033) which would invalidate fingerprints
1000                // and force us to rebuild tools after vendoring dependencies.
1001                // To work around this, create the Tarball after building all the tools.
1002                dist::PlainSourceTarball,
1003                dist::PlainSourceTarballGpl,
1004                dist::BuildManifest,
1005                dist::ReproducibleArtifacts,
1006                dist::GccDev,
1007                dist::Gcc
1008            ),
1009            Kind::Install => describe!(
1010                install::Docs,
1011                install::Std,
1012                // During the Rust compiler (rustc) installation process, we copy the entire sysroot binary
1013                // path (build/host/stage2/bin). Since the building tools also make their copy in the sysroot
1014                // binary path, we must install rustc before the tools. Otherwise, the rust-installer will
1015                // install the same binaries twice for each tool, leaving backup files (*.old) as a result.
1016                install::Rustc,
1017                install::RustcDev,
1018                install::Cargo,
1019                install::RustAnalyzer,
1020                install::Rustfmt,
1021                install::Clippy,
1022                install::Miri,
1023                install::LlvmTools,
1024                install::Src,
1025                install::RustcCodegenCranelift,
1026                install::LlvmBitcodeLinker
1027            ),
1028            Kind::Run => describe!(
1029                run::BuildManifest,
1030                run::BumpStage0,
1031                run::ReplaceVersionPlaceholder,
1032                run::Miri,
1033                run::CollectLicenseMetadata,
1034                run::GenerateCopyright,
1035                run::GenerateWindowsSys,
1036                run::GenerateCompletions,
1037                run::UnicodeTableGenerator,
1038                run::FeaturesStatusDump,
1039                run::CyclicStep,
1040                run::CoverageDump,
1041                run::Rustfmt,
1042                run::GenerateHelp,
1043            ),
1044            Kind::Setup => {
1045                describe!(setup::Profile, setup::Hook, setup::Link, setup::Editor)
1046            }
1047            Kind::Clean => describe!(clean::CleanAll, clean::Rustc, clean::Std),
1048            Kind::Vendor => describe!(vendor::Vendor),
1049            // special-cased in Session::build()
1050            Kind::Format | Kind::Perf => vec![],
1051            Kind::MiriTest | Kind::MiriSetup => unreachable!(),
1052        }
1053    }
1054
1055    pub fn get_help(sess: &Session, kind: Kind) -> Option<String> {
1056        let step_descriptions = Builder::get_step_descriptions(kind);
1057        if step_descriptions.is_empty() {
1058            return None;
1059        }
1060
1061        let builder = Self::new_internal(sess, kind, vec![]);
1062        let builder = &builder;
1063
1064        let mut should_run = ShouldRun::new(builder);
1065        for desc in step_descriptions {
1066            should_run = (desc.should_run)(should_run);
1067        }
1068        let mut help = String::from("Available paths:\n");
1069        let mut add_path = |path: &Path| {
1070            t!(write!(help, "    ./x.py {} {}\n", kind.as_str(), path.display()));
1071        };
1072        for pathset in should_run.paths {
1073            match pathset {
1074                PathSet::Set(set) => {
1075                    for path in set {
1076                        add_path(&path.path);
1077                    }
1078                }
1079                PathSet::Suite(path) => {
1080                    add_path(&path.path.join("..."));
1081                }
1082            }
1083        }
1084        Some(help)
1085    }
1086
1087    fn new_internal(sess: &Session, kind: Kind, paths: Vec<PathBuf>) -> Builder<'_> {
1088        Builder {
1089            sess,
1090            top_stage: sess.config.stage,
1091            kind,
1092            cache: Cache::new(),
1093            stack: RefCell::new(Vec::new()),
1094            time_spent_on_dependencies: Cell::new(Duration::new(0, 0)),
1095            paths,
1096            submodule_paths_cache: Default::default(),
1097            log_cli_step_for_tests: None,
1098        }
1099    }
1100
1101    pub fn new(sess: &Session) -> Builder<'_> {
1102        let paths = &sess.config.paths;
1103        let (kind, paths) = match sess.config.cmd {
1104            Subcommand::Build { .. } => (Kind::Build, &paths[..]),
1105            Subcommand::Check { .. } => (Kind::Check, &paths[..]),
1106            Subcommand::Clippy { .. } => (Kind::Clippy, &paths[..]),
1107            Subcommand::Fix { .. } => (Kind::Fix, &paths[..]),
1108            Subcommand::Doc { .. } => (Kind::Doc, &paths[..]),
1109            Subcommand::Test { .. } => (Kind::Test, &paths[..]),
1110            Subcommand::Miri { .. } => (Kind::Miri, &paths[..]),
1111            Subcommand::Bench { .. } => (Kind::Bench, &paths[..]),
1112            Subcommand::Dist => (Kind::Dist, &paths[..]),
1113            Subcommand::Install => (Kind::Install, &paths[..]),
1114            Subcommand::Run { .. } => (Kind::Run, &paths[..]),
1115            Subcommand::Clean { .. } => (Kind::Clean, &paths[..]),
1116            Subcommand::Format { .. } => (Kind::Format, &[][..]),
1117            Subcommand::Setup { profile: ref path } => (
1118                Kind::Setup,
1119                path.as_ref().map_or([].as_slice(), |path| std::slice::from_ref(path)),
1120            ),
1121            Subcommand::Vendor { .. } => (Kind::Vendor, &paths[..]),
1122            Subcommand::Perf { .. } => (Kind::Perf, &paths[..]),
1123        };
1124
1125        StepStack::with_current(|stack| stack.clear());
1126        Self::new_internal(sess, kind, paths.to_owned())
1127    }
1128
1129    pub fn execute_cli(&self) {
1130        self.run_step_descriptions(&Builder::get_step_descriptions(self.kind), &self.paths);
1131    }
1132
1133    /// Run all default documentation steps to build documentation.
1134    pub fn run_default_doc_steps(&self) {
1135        // It's important that we don't just call `run_step_descriptions` here,
1136        // because that would cause `--skip` handling for actual command-line
1137        // arguments to inappropriately skip these steps.
1138        //
1139        // This function is nevertheless a bit of a hack, to work around the
1140        // fact that we don't have a good way to simulate `./x doc` without
1141        // also simulating parts of command-line selector handling.
1142
1143        for desc in &Builder::get_step_descriptions(Kind::Doc) {
1144            if !(desc.is_default_step_fn)(self) {
1145                continue;
1146            }
1147
1148            let should_run = (desc.should_run)(ShouldRun::new(self));
1149            let default_pathsets = should_run.default_pathsets();
1150
1151            let targets = if desc.is_host { &self.hosts } else { &self.targets };
1152            for &target in targets {
1153                let run = RunConfig { builder: self, target, paths: default_pathsets.clone() };
1154                (desc.make_run)(run);
1155            }
1156        }
1157    }
1158
1159    pub fn doc_rust_lang_org_channel(&self) -> String {
1160        let channel = match &*self.config.channel {
1161            "stable" => &self.version,
1162            "beta" => "beta",
1163            "nightly" | "dev" => "nightly",
1164            // custom build of rustdoc maybe? link to the latest stable docs just in case
1165            _ => "stable",
1166        };
1167
1168        format!("https://doc.rust-lang.org/{channel}")
1169    }
1170
1171    fn run_step_descriptions(&self, v: &[CommandLineStepDescription], paths: &[PathBuf]) {
1172        cli_paths::match_paths_to_steps_and_run(self, v, paths);
1173    }
1174
1175    /// Obtain a compiler at a given stage and for a given host (i.e., this is the target that the
1176    /// compiler will run on, *not* the target it will build code for). Explicitly does not take
1177    /// `Compiler` since all `Compiler` instances are meant to be obtained through this function,
1178    /// since it ensures that they are valid (i.e., built and assembled).
1179    #[track_caller]
1180    #[cfg_attr(
1181        feature = "tracing",
1182        instrument(
1183            level = "trace",
1184            name = "Builder::compiler",
1185            target = "COMPILER",
1186            skip_all,
1187            fields(
1188                stage = stage,
1189                host = ?host,
1190            ),
1191        ),
1192    )]
1193    pub fn compiler(&self, stage: u32, host: TargetSelection) -> Compiler {
1194        self.ensure(compile::Assemble { target_compiler: Compiler::new(stage, host) })
1195    }
1196
1197    /// This function can be used to provide a build compiler for building
1198    /// the standard library, in order to avoid unnecessary rustc builds in case where std uplifting
1199    /// would happen anyway.
1200    ///
1201    /// This is an important optimization mainly for CI.
1202    ///
1203    /// Normally, to build stage N libstd, we need stage N rustc.
1204    /// However, if we know that we will uplift libstd from stage 1 anyway, building the stage N
1205    /// rustc can be wasteful.
1206    /// In particular, if we do a cross-compiling dist stage 2 build from target1 to target2,
1207    /// we need:
1208    /// - stage 2 libstd for target2 (uplifted from stage 1, where it was built by target1 rustc)
1209    /// - stage 2 rustc for target2
1210    ///
1211    /// However, without this optimization, we would also build stage 2 rustc for **target1**,
1212    /// which is completely wasteful.
1213    #[track_caller]
1214    pub fn compiler_for_std(&self, stage: u32) -> Compiler {
1215        if compile::Std::should_be_uplifted_from_stage_1(self, stage) {
1216            self.compiler(1, self.host_target)
1217        } else {
1218            self.compiler(stage, self.host_target)
1219        }
1220    }
1221
1222    /// Similar to `compiler`, except handles the full-bootstrap option to
1223    /// silently use the stage1 compiler instead of a stage2 compiler if one is
1224    /// requested.
1225    ///
1226    /// Note that this does *not* have the side effect of creating
1227    /// `compiler(stage, host)`, unlike `compiler` above which does have such
1228    /// a side effect. The returned compiler here can only be used to compile
1229    /// new artifacts, it can't be used to rely on the presence of a particular
1230    /// sysroot.
1231    ///
1232    /// See `force_use_stage1` and `force_use_stage2` for documentation on what each argument is.
1233    #[track_caller]
1234    #[cfg_attr(
1235        feature = "tracing",
1236        instrument(
1237            level = "trace",
1238            name = "Builder::compiler_for",
1239            target = "COMPILER_FOR",
1240            skip_all,
1241            fields(
1242                stage = stage,
1243                host = ?host,
1244                target = ?target,
1245            ),
1246        ),
1247    )]
1248    /// FIXME: This function is unnecessary (and dangerous, see <https://github.com/rust-lang/rust/issues/137469>).
1249    /// We already have uplifting logic for the compiler, so remove this.
1250    pub fn compiler_for(
1251        &self,
1252        stage: u32,
1253        host: TargetSelection,
1254        target: TargetSelection,
1255    ) -> Compiler {
1256        let mut resolved_compiler = if self.sess.force_use_stage2(stage) {
1257            trace!(target: "COMPILER_FOR", ?stage, "force_use_stage2");
1258            self.compiler(2, self.config.host_target)
1259        } else if self.sess.force_use_stage1(stage, target) {
1260            trace!(target: "COMPILER_FOR", ?stage, "force_use_stage1");
1261            self.compiler(1, self.config.host_target)
1262        } else {
1263            trace!(target: "COMPILER_FOR", ?stage, ?host, "no force, fallback to `compiler()`");
1264            self.compiler(stage, host)
1265        };
1266
1267        if stage != resolved_compiler.stage {
1268            resolved_compiler.forced_compiler(true);
1269        }
1270
1271        trace!(target: "COMPILER_FOR", ?resolved_compiler);
1272        resolved_compiler
1273    }
1274
1275    /// Obtain a standard library for the given target that will be built by the passed compiler.
1276    /// The standard library will be linked to the sysroot of the passed compiler.
1277    ///
1278    /// Prefer using this method rather than manually invoking `Std::new`.
1279    ///
1280    /// Returns an optional build stamp, if libstd was indeed built.
1281    #[track_caller]
1282    #[cfg_attr(
1283        feature = "tracing",
1284        instrument(
1285            level = "trace",
1286            name = "Builder::std",
1287            target = "STD",
1288            skip_all,
1289            fields(
1290                compiler = ?compiler,
1291                target = ?target,
1292            ),
1293        ),
1294    )]
1295    pub fn std(&self, compiler: Compiler, target: TargetSelection) -> Option<BuildStamp> {
1296        // FIXME: make the `Std` step return some type-level "proof" that std was indeed built,
1297        // and then require passing that to all Cargo invocations that we do.
1298
1299        // The "stage 0" std is almost always precompiled and comes with the stage0 compiler, so we
1300        // have special logic for it, to avoid creating needless and confusing Std steps that don't
1301        // actually build anything.
1302        // We only allow building the stage0 stdlib if we do a local rebuild, so the stage0 compiler
1303        // actually comes from in-tree sources, and we're cross-compiling, so the stage0 for the
1304        // given `target` is not available.
1305        if compiler.stage == 0 {
1306            if target != compiler.host {
1307                if self.local_rebuild {
1308                    self.ensure(Std::new(compiler, target))
1309                } else {
1310                    panic!(
1311                        r"It is not possible to build the standard library for `{target}` using the stage0 compiler.
1312You have to build a stage1 compiler for `{}` first, and then use it to build a standard library for `{target}`.
1313Alternatively, you can set `build.local-rebuild=true` and use a stage0 compiler built from in-tree sources.
1314",
1315                        compiler.host
1316                    )
1317                }
1318            } else {
1319                // We still need to link the prebuilt standard library into the ephemeral stage0 sysroot
1320                self.ensure(StdLink::from_std(Std::new(compiler, target), compiler));
1321                None
1322            }
1323        } else {
1324            // This step both compiles the std and links it into the compiler's sysroot.
1325            // Yes, it's quite magical and side-effecty.. would be nice to refactor later.
1326            self.ensure(Std::new(compiler, target))
1327        }
1328    }
1329
1330    #[track_caller]
1331    pub fn sysroot(&self, compiler: Compiler) -> PathBuf {
1332        self.ensure(compile::Sysroot::new(compiler))
1333    }
1334
1335    /// Returns the bindir for a compiler's sysroot.
1336    #[track_caller]
1337    pub fn sysroot_target_bindir(&self, compiler: Compiler, target: TargetSelection) -> PathBuf {
1338        self.ensure(Libdir { compiler, target }).join(target).join("bin")
1339    }
1340
1341    /// Returns the libdir where the standard library and other artifacts are
1342    /// found for a compiler's sysroot.
1343    #[track_caller]
1344    pub fn sysroot_target_libdir(&self, compiler: Compiler, target: TargetSelection) -> PathBuf {
1345        self.ensure(Libdir { compiler, target }).join(target).join("lib")
1346    }
1347
1348    pub fn sysroot_codegen_backends(&self, compiler: Compiler) -> PathBuf {
1349        self.sysroot_target_libdir(compiler, compiler.host).with_file_name("codegen-backends")
1350    }
1351
1352    /// Returns the compiler's libdir where it stores the dynamic libraries that
1353    /// it itself links against.
1354    ///
1355    /// For example this returns `<sysroot>/lib` on Unix and `<sysroot>/bin` on
1356    /// Windows.
1357    pub fn rustc_libdir(&self, compiler: Compiler) -> PathBuf {
1358        if compiler.is_snapshot(self) {
1359            self.rustc_snapshot_libdir()
1360        } else {
1361            match self.config.libdir_relative() {
1362                Some(relative_libdir) if compiler.stage >= 1 => {
1363                    self.sysroot(compiler).join(relative_libdir)
1364                }
1365                _ => self.sysroot(compiler).join(libdir(compiler.host)),
1366            }
1367        }
1368    }
1369
1370    /// Returns the compiler's relative libdir where it stores the dynamic libraries that
1371    /// it itself links against.
1372    ///
1373    /// For example this returns `lib` on Unix and `bin` on
1374    /// Windows.
1375    pub fn libdir_relative(&self, compiler: Compiler) -> &Path {
1376        if compiler.is_snapshot(self) {
1377            libdir(self.config.host_target).as_ref()
1378        } else {
1379            match self.config.libdir_relative() {
1380                Some(relative_libdir) if compiler.stage >= 1 => relative_libdir,
1381                _ => libdir(compiler.host).as_ref(),
1382            }
1383        }
1384    }
1385
1386    /// Returns the compiler's relative libdir where the standard library and other artifacts are
1387    /// found for a compiler's sysroot.
1388    ///
1389    /// For example this returns `lib` on Unix and Windows.
1390    pub fn sysroot_libdir_relative(&self, compiler: Compiler) -> &Path {
1391        match self.config.libdir_relative() {
1392            Some(relative_libdir) if compiler.stage >= 1 => relative_libdir,
1393            _ if compiler.stage == 0 => &self.sess.initial_relative_libdir,
1394            _ => Path::new("lib"),
1395        }
1396    }
1397
1398    pub fn rustc_lib_paths(&self, compiler: Compiler) -> Vec<PathBuf> {
1399        let mut dylib_dirs = vec![self.rustc_libdir(compiler)];
1400
1401        // Ensure that the downloaded LLVM libraries can be found.
1402        if self.config.llvm_ci_mode.download_from_ci() {
1403            let ci_llvm_lib = self.out.join(compiler.host).join("ci-llvm").join("lib");
1404            dylib_dirs.push(ci_llvm_lib);
1405        }
1406
1407        dylib_dirs
1408    }
1409
1410    /// Adds the compiler's directory of dynamic libraries to `cmd`'s dynamic
1411    /// library lookup path.
1412    pub fn add_rustc_lib_path(&self, compiler: Compiler, cmd: &mut BootstrapCommand) {
1413        // Windows doesn't need dylib path munging because the dlls for the
1414        // compiler live next to the compiler and the system will find them
1415        // automatically.
1416        if cfg!(any(windows, target_os = "cygwin")) {
1417            return;
1418        }
1419
1420        add_dylib_path(self.rustc_lib_paths(compiler), cmd);
1421    }
1422
1423    /// Gets a path to the compiler specified.
1424    pub fn rustc(&self, compiler: Compiler) -> PathBuf {
1425        if compiler.is_snapshot(self) {
1426            self.initial_rustc.clone()
1427        } else {
1428            self.sysroot(compiler).join("bin").join(exe("rustc", compiler.host))
1429        }
1430    }
1431
1432    /// Gets a command to run the compiler specified, including the dynamic library
1433    /// path in case the executable has not been build with `rpath` enabled.
1434    pub fn rustc_cmd(&self, compiler: Compiler) -> BootstrapCommand {
1435        let mut cmd = command(self.rustc(compiler));
1436        self.add_rustc_lib_path(compiler, &mut cmd);
1437        cmd
1438    }
1439
1440    /// Gets the paths to all of the compiler's codegen backends.
1441    fn codegen_backends(&self, compiler: Compiler) -> impl Iterator<Item = PathBuf> {
1442        fs::read_dir(self.sysroot_codegen_backends(compiler))
1443            .into_iter()
1444            .flatten()
1445            .filter_map(Result::ok)
1446            .filter(|path| looks_like_codegen_backend(&path.path()))
1447            .map(|entry| entry.path())
1448    }
1449
1450    /// Returns a path to `Rustdoc` that "belongs" to the `target_compiler`.
1451    /// It can be either a stage0 rustdoc or a locally built rustdoc that *links* to
1452    /// `target_compiler`.
1453    #[track_caller]
1454    pub fn rustdoc_for_compiler(&self, target_compiler: Compiler) -> PathBuf {
1455        self.ensure(tool::Rustdoc { target_compiler })
1456    }
1457
1458    pub fn cargo_miri_cmd(&self, run_compiler: Compiler) -> BootstrapCommand {
1459        assert!(run_compiler.stage > 0, "miri can not be invoked at stage 0");
1460
1461        let compilers = RustcPrivateCompilers::new(self, run_compiler.stage, self.sess.host_target);
1462        assert_eq!(run_compiler, compilers.target_compiler());
1463
1464        // Prepare the tools
1465        let miri = self.ensure(tool::Miri::from_compilers(compilers));
1466        let cargo_miri = self.ensure(tool::CargoMiri::from_compilers(compilers));
1467        // Invoke cargo-miri, make sure it can find miri and cargo.
1468        let mut cmd = command(cargo_miri.tool_path);
1469        cmd.env("MIRI", &miri.tool_path);
1470        cmd.env("CARGO", &self.initial_cargo);
1471        // Need to add the `run_compiler` libs. Those are the libs produces *by* `build_compiler`
1472        // in `tool::ToolBuild` step, so they match the Miri we just built. However this means they
1473        // are actually living one stage up, i.e. we are running `stage1-tools-bin/miri` with the
1474        // libraries in `stage1/lib`. This is an unfortunate off-by-1 caused (possibly) by the fact
1475        // that Miri doesn't have an "assemble" step like rustc does that would cross the stage boundary.
1476        // We can't use `add_rustc_lib_path` as that's a NOP on Windows but we do need these libraries
1477        // added to the PATH due to the stage mismatch.
1478        // Also see https://github.com/rust-lang/rust/pull/123192#issuecomment-2028901503.
1479        add_dylib_path(self.rustc_lib_paths(run_compiler), &mut cmd);
1480        cmd
1481    }
1482
1483    /// Create a Cargo command for running Clippy.
1484    /// The used Clippy is (or in the case of stage 0, already was) built using `build_compiler`.
1485    pub fn cargo_clippy_cmd(&self, build_compiler: Compiler) -> BootstrapCommand {
1486        if build_compiler.stage == 0 {
1487            let cargo_clippy =
1488                self.config.external_cargo_clippy.clone().unwrap_or_else(|| {
1489                    self.sess.config.download_clippy(&self.sess.initial_sysroot)
1490                });
1491
1492            let mut cmd = command(cargo_clippy);
1493            cmd.env("CARGO", &self.initial_cargo);
1494            return cmd;
1495        }
1496
1497        // If we're linting something with build_compiler stage N, we want to build Clippy stage N
1498        // and use that to lint it. That is why we use the `build_compiler` as the target compiler
1499        // for RustcPrivateCompilers. We will use build compiler stage N-1 to build Clippy stage N.
1500        let compilers = RustcPrivateCompilers::from_target_compiler(self, build_compiler);
1501
1502        let _ = self.ensure(tool::Clippy::from_compilers(compilers));
1503        let cargo_clippy = self.ensure(tool::CargoClippy::from_compilers(compilers));
1504        let mut dylib_path = helpers::dylib_path();
1505        dylib_path.insert(0, self.sysroot(build_compiler).join("lib"));
1506
1507        let mut cmd = command(cargo_clippy.tool_path);
1508        cmd.env(helpers::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1509        cmd.env("CARGO", &self.initial_cargo);
1510        cmd
1511    }
1512
1513    pub fn rustdoc_cmd(&self, compiler: Compiler) -> BootstrapCommand {
1514        let mut cmd = command(self.bootstrap_out.join("rustdoc"));
1515        cmd.env("RUSTC_STAGE", compiler.stage.to_string())
1516            .env("RUSTC_SYSROOT", self.sysroot(compiler))
1517            // Note that this is *not* the sysroot_libdir because rustdoc must be linked
1518            // equivalently to rustc.
1519            .env("RUSTDOC_LIBDIR", self.rustc_libdir(compiler))
1520            .env("CFG_RELEASE_CHANNEL", &self.config.channel)
1521            .env("RUSTDOC_REAL", self.rustdoc_for_compiler(compiler))
1522            .env("RUSTC_BOOTSTRAP", "1");
1523
1524        cmd.arg("-Wrustdoc::invalid_codeblock_attributes");
1525
1526        if self.config.deny_warnings {
1527            cmd.arg("-Dwarnings");
1528        }
1529        cmd.arg("-Znormalize-docs");
1530        cmd.args(linker_args(self, compiler.host, LldThreads::Yes));
1531        cmd
1532    }
1533
1534    /// Returns true is LLVM is enabled for the given target and we are supposed to build it.
1535    ///
1536    /// Note that this returns false if LLVM is disabled, or if we're in a
1537    /// check build or dry-run, where there's no need to build all of LLVM.
1538    pub fn is_llvm_enabled_for(&self, target: TargetSelection) -> bool {
1539        self.config.llvm_enabled(target) && self.kind != Kind::Check && !self.config.dry_run()
1540    }
1541
1542    /// Return the `llvm-config` for the host target, so that it is executable.
1543    pub fn host_llvm_config(&self) -> PathBuf {
1544        self.ensure(llvm::Llvm { target: self.host_target }).llvm_config().to_owned()
1545    }
1546
1547    /// Updates all submodules, and exits with an error if submodule
1548    /// management is disabled and the submodule does not exist.
1549    pub fn require_and_update_all_submodules(&self) {
1550        for submodule in self.submodule_paths() {
1551            self.require_submodule(submodule, None);
1552        }
1553    }
1554
1555    /// Get all submodules from the src directory.
1556    pub fn submodule_paths(&self) -> &[String] {
1557        self.submodule_paths_cache.get_or_init(|| build_helper::util::parse_gitmodules(&self.src))
1558    }
1559
1560    /// Ensure that a given step is built, returning its output. This will
1561    /// cache the step, so it is safe (and good!) to call this as often as
1562    /// needed to ensure that all dependencies are built.
1563    #[track_caller]
1564    pub(crate) fn ensure<S: Step>(&'a self, step: S) -> S::Output {
1565        {
1566            let mut stack = self.stack.borrow_mut();
1567            for stack_step in stack.iter() {
1568                // should skip
1569                if stack_step.downcast_ref::<S>().is_none_or(|stack_step| *stack_step != step) {
1570                    continue;
1571                }
1572                let mut out = String::new();
1573                out += &format!("\n\nCycle in build detected when adding {step:?}\n");
1574                for el in stack.iter().rev() {
1575                    out += &format!("\t{el:?}\n");
1576                }
1577                panic!("{}", out);
1578            }
1579            if let Some(out) = self.cache.get(&step) {
1580                #[cfg(feature = "tracing")]
1581                {
1582                    if let Some(parent) = stack.last() {
1583                        let mut graph = self.sess.step_graph.borrow_mut();
1584                        graph.register_cached_step(&step, parent, self.config.dry_run());
1585                    }
1586                }
1587                return out;
1588            }
1589
1590            #[cfg(feature = "tracing")]
1591            {
1592                let parent = stack.last();
1593                let mut graph = self.sess.step_graph.borrow_mut();
1594                graph.register_step_execution(&step, parent, self.config.dry_run());
1595            }
1596
1597            // The location has to be gathered in this function, to be correctly propagated with
1598            // #[track_caller].
1599            let location = format_location(*std::panic::Location::caller());
1600            StepStack::with_current(|stack| {
1601                stack.push(StepRecord { info: pretty_print_step(&step), location });
1602            });
1603            stack.push(Box::new(step.clone()));
1604        }
1605
1606        #[cfg(feature = "build-metrics")]
1607        self.metrics.enter_step(&step, self);
1608
1609        if self.config.print_step_timings && !self.config.dry_run() {
1610            println!("[TIMING:start] {}", pretty_print_step(&step));
1611        }
1612
1613        let (out, dur) = {
1614            let start = Instant::now();
1615            let zero = Duration::new(0, 0);
1616            let parent = self.time_spent_on_dependencies.replace(zero);
1617
1618            #[cfg(feature = "tracing")]
1619            let _span = {
1620                // Keep the target and field names synchronized with `setup_tracing`.
1621                let span = tracing::info_span!(
1622                    target: STEP_SPAN_TARGET,
1623                    // We cannot use a dynamic name here, so instead we record the actual step name
1624                    // in the step_name field.
1625                    "step",
1626                    step_name = pretty_step_name::<S>(),
1627                    args = step_debug_args(&step),
1628                    location = format_location(*std::panic::Location::caller())
1629                );
1630                span.entered()
1631            };
1632
1633            let out = step.clone().run(self);
1634            let dur = start.elapsed();
1635            let deps = self.time_spent_on_dependencies.replace(parent + dur);
1636            (out, dur.saturating_sub(deps))
1637        };
1638
1639        if self.config.print_step_timings && !self.config.dry_run() {
1640            println!(
1641                "[TIMING:end] {} -- {}.{:03}",
1642                pretty_print_step(&step),
1643                dur.as_secs(),
1644                dur.subsec_millis()
1645            );
1646        }
1647
1648        #[cfg(feature = "build-metrics")]
1649        self.metrics.exit_step(self);
1650
1651        {
1652            let mut stack = self.stack.borrow_mut();
1653            let cur_step = stack.pop().expect("step stack empty");
1654            assert_eq!(cur_step.downcast_ref(), Some(&step));
1655
1656            StepStack::with_current(|stack| {
1657                stack.pop();
1658            });
1659        }
1660        self.cache.put(step, out.clone());
1661        out
1662    }
1663
1664    /// Ensure that a given step is built *only if it's supposed to be built by default*, returning
1665    /// its output. This will cache the step, so it's safe (and good!) to call this as often as
1666    /// needed to ensure that all dependencies are build.
1667    pub(crate) fn ensure_if_default<T, S: CommandLineStep<Output = T>>(
1668        &'a self,
1669        step: S,
1670        kind: Kind,
1671    ) -> Option<S::Output> {
1672        let desc = CommandLineStepDescription::from::<S>(kind);
1673        let should_run = (desc.should_run)(ShouldRun::new(self));
1674
1675        // Avoid running steps contained in --skip
1676        for pathset in &should_run.paths {
1677            if desc.is_excluded(self, pathset) {
1678                return None;
1679            }
1680        }
1681
1682        // Only execute if it's supposed to run as default
1683        if (desc.is_default_step_fn)(self) { Some(self.ensure(step)) } else { None }
1684    }
1685
1686    /// Checks if any of the "should_run" paths is in the `Builder` paths.
1687    pub(crate) fn was_invoked_explicitly<S: CommandLineStep>(&'a self, kind: Kind) -> bool {
1688        let desc = CommandLineStepDescription::from::<S>(kind);
1689        let should_run = (desc.should_run)(ShouldRun::new(self));
1690
1691        for path in &self.paths {
1692            if should_run.paths.iter().any(|s| s.has(path))
1693                && !desc.is_excluded(self, &PathSet::Suite(TaskPath { path: path.clone() }))
1694            {
1695                return true;
1696            }
1697        }
1698
1699        false
1700    }
1701
1702    pub(crate) fn maybe_open_in_browser<S: CommandLineStep>(&self, path: impl AsRef<Path>) {
1703        if self.was_invoked_explicitly::<S>(Kind::Doc) {
1704            self.open_in_browser(path);
1705        } else {
1706            self.info(&format!("Doc path: {}", path.as_ref().display()));
1707        }
1708    }
1709
1710    pub(crate) fn open_in_browser(&self, path: impl AsRef<Path>) {
1711        let path = path.as_ref();
1712
1713        if self.config.dry_run() || !self.config.cmd.open() {
1714            self.info(&format!("Doc path: {}", path.display()));
1715            return;
1716        }
1717
1718        self.info(&format!("Opening doc {}", path.display()));
1719        if let Err(err) = opener::open(path) {
1720            self.info(&format!("{err}\n"));
1721        }
1722    }
1723
1724    pub fn exec_ctx(&self) -> &ExecutionContext {
1725        &self.config.exec_ctx
1726    }
1727}
1728
1729/// Return qualified step name, e.g. `compile::Rustc`.
1730pub fn pretty_step_name<S: Step>() -> String {
1731    // Normalize step type path to only keep the module and the type name
1732    let path = type_name::<S>().rsplit("::").take(2).collect::<Vec<_>>();
1733    path.into_iter().rev().collect::<Vec<_>>().join("::")
1734}
1735
1736/// Renders `step` using its `Debug` implementation and extract the field arguments out of it.
1737fn step_debug_args<S: Step>(step: &S) -> String {
1738    let step_dbg_repr = format!("{step:?}");
1739
1740    // Some steps do not have any arguments, so they do not have the braces
1741    match (step_dbg_repr.find('{'), step_dbg_repr.rfind('}')) {
1742        (Some(brace_start), Some(brace_end)) => {
1743            step_dbg_repr[brace_start + 1..brace_end - 1].trim().to_string()
1744        }
1745        _ => String::new(),
1746    }
1747}
1748
1749fn pretty_print_step<S: Step>(step: &S) -> String {
1750    format!("{} {{ {} }}", pretty_step_name::<S>(), step_debug_args(step))
1751}
1752
1753impl<'a> AsRef<ExecutionContext> for Builder<'a> {
1754    fn as_ref(&self) -> &ExecutionContext {
1755        self.exec_ctx()
1756    }
1757}