Skip to main content

bootstrap/core/build_steps/
tool.rs

1//! This module handles building and managing various tools in bootstrap
2//! build system.
3//!
4//! **What It Does**
5//! - Defines how tools are built, configured and installed.
6//! - Manages tool dependencies and build steps.
7//! - Copies built tool binaries to the correct locations.
8//!
9//! Each Rust tool **MUST** utilize `ToolBuild` inside their `Step` logic,
10//! return `ToolBuildResult` and should never prepare `cargo` invocations manually.
11
12use std::ffi::OsStr;
13use std::path::{Path, PathBuf};
14use std::{env, fs};
15
16use crate::core::build_steps::compile::is_lto_stage;
17use crate::core::build_steps::toolstate::ToolState;
18use crate::core::build_steps::{compile, llvm};
19use crate::core::builder;
20use crate::core::builder::{
21    Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step, StepMetadata, cargo_profile_var,
22};
23use crate::core::config::{DebuginfoLevel, RustcLto, TargetSelection};
24use crate::utils::exec::{BootstrapCommand, command};
25use crate::utils::helpers::{add_dylib_path, exe, t};
26use crate::{Compiler, FileType, Kind, Mode};
27
28#[derive(Debug, Clone, Hash, PartialEq, Eq)]
29pub enum SourceType {
30    InTree,
31    Submodule,
32}
33
34#[derive(Debug, Clone, Hash, PartialEq, Eq)]
35pub enum ToolArtifactKind {
36    Binary,
37    Library,
38}
39
40#[derive(Debug, Clone, Hash, PartialEq, Eq)]
41struct ToolBuild {
42    /// Compiler that will build this tool.
43    build_compiler: Compiler,
44    target: TargetSelection,
45    tool: &'static str,
46    path: &'static str,
47    mode: Mode,
48    source_type: SourceType,
49    extra_features: Vec<String>,
50    /// Nightly-only features that are allowed (comma-separated list).
51    allow_features: &'static str,
52    /// Additional arguments to pass to the `cargo` invocation.
53    cargo_args: Vec<String>,
54    /// Whether the tool builds a binary or a library.
55    artifact_kind: ToolArtifactKind,
56}
57
58/// Result of the tool build process. Each `Step` in this module is responsible
59/// for using this type as `type Output = ToolBuildResult;`
60#[derive(Clone)]
61pub struct ToolBuildResult {
62    /// Artifact path of the corresponding tool that was built.
63    pub tool_path: PathBuf,
64    /// Compiler used to build the tool.
65    pub build_compiler: Compiler,
66}
67
68impl Step for ToolBuild {
69    type Output = ToolBuildResult;
70
71    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
72        run.never()
73    }
74
75    /// Builds a tool in `src/tools`
76    ///
77    /// This will build the specified tool with the specified `host` compiler in
78    /// `stage` into the normal cargo output directory.
79    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
80        let target = self.target;
81        let mut tool = self.tool;
82        let path = self.path;
83
84        match self.mode {
85            Mode::ToolRustcPrivate => {
86                // FIXME: remove this, it's only needed for download-rustc...
87                if !self.build_compiler.is_forced_compiler() && builder.download_rustc() {
88                    builder.std(self.build_compiler, self.build_compiler.host);
89                    builder.ensure(compile::Rustc::new(self.build_compiler, target));
90                }
91            }
92            Mode::ToolStd => {
93                // If compiler was forced, its artifacts should have been prepared earlier.
94                if !self.build_compiler.is_forced_compiler() {
95                    builder.std(self.build_compiler, target);
96                }
97            }
98            Mode::ToolBootstrap | Mode::ToolTarget => {} // uses downloaded stage0 compiler libs
99            _ => panic!("unexpected Mode for tool build"),
100        }
101
102        let mut cargo = prepare_tool_cargo(
103            builder,
104            self.build_compiler,
105            self.mode,
106            target,
107            Kind::Build,
108            path,
109            self.source_type,
110            &self.extra_features,
111        );
112
113        // The stage0 compiler changes infrequently and does not directly depend on code
114        // in the current working directory. Therefore, caching it with sccache should be
115        // useful.
116        // This is only performed for non-incremental builds, as ccache cannot deal with these.
117        if let Some(ref ccache) = builder.config.ccache
118            && matches!(self.mode, Mode::ToolBootstrap)
119            && !builder.config.incremental
120        {
121            cargo.env("RUSTC_WRAPPER", ccache);
122        }
123
124        // RustcPrivate tools (miri, clippy, rustfmt, rust-analyzer) and cargo
125        // could use the additional optimizations.
126        if is_lto_stage(&self.build_compiler)
127            && (self.mode == Mode::ToolRustcPrivate || self.path == "src/tools/cargo")
128        {
129            let lto = match builder.config.rust_lto {
130                RustcLto::Off => Some("off"),
131                RustcLto::Thin => Some("thin"),
132                RustcLto::Fat => Some("fat"),
133                RustcLto::ThinLocal => None,
134            };
135            if let Some(lto) = lto {
136                cargo.env(cargo_profile_var("LTO", &builder.config, self.mode), lto);
137            }
138        }
139
140        if !self.allow_features.is_empty() {
141            cargo.allow_features(self.allow_features);
142        }
143
144        cargo.args(self.cargo_args);
145
146        let _guard =
147            builder.msg(Kind::Build, self.tool, self.mode, self.build_compiler, self.target);
148
149        // we check this below
150        let build_success = compile::stream_cargo(builder, cargo, vec![], &mut |_| {});
151
152        builder.save_toolstate(
153            tool,
154            if build_success { ToolState::TestFail } else { ToolState::BuildFail },
155        );
156
157        if !build_success {
158            crate::exit!(1);
159        } else {
160            // HACK(#82501): on Windows, the tools directory gets added to PATH when running tests, and
161            // compiletest confuses HTML tidy with the in-tree tidy. Name the in-tree tidy something
162            // different so the problem doesn't come up.
163            if tool == "tidy" {
164                tool = "rust-tidy";
165            }
166            let tool_path = match self.artifact_kind {
167                ToolArtifactKind::Binary => {
168                    copy_link_tool_bin(builder, self.build_compiler, self.target, self.mode, tool)
169                }
170                ToolArtifactKind::Library => builder
171                    .cargo_out(self.build_compiler, self.mode, self.target)
172                    .join(format!("lib{tool}.rlib")),
173            };
174
175            ToolBuildResult { tool_path, build_compiler: self.build_compiler }
176        }
177    }
178}
179
180#[expect(clippy::too_many_arguments)] // FIXME: reduce the number of args and remove this.
181pub fn prepare_tool_cargo(
182    builder: &Builder<'_>,
183    compiler: Compiler,
184    mode: Mode,
185    target: TargetSelection,
186    cmd_kind: Kind,
187    path: &str,
188    source_type: SourceType,
189    extra_features: &[String],
190) -> CargoCommand {
191    let mut cargo = builder::Cargo::new(builder, compiler, mode, source_type, target, cmd_kind);
192
193    let path = PathBuf::from(path);
194    let dir = builder.src.join(&path);
195    cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
196
197    let mut features = extra_features.to_vec();
198    if builder.build.config.cargo_native_static {
199        if path.ends_with("cargo")
200            || path.ends_with("clippy")
201            || path.ends_with("miri")
202            || path.ends_with("rustfmt")
203        {
204            cargo.env("LIBZ_SYS_STATIC", "1");
205        }
206        if path.ends_with("cargo") {
207            features.push("all-static".to_string());
208        }
209    }
210
211    // build.tool.TOOL_NAME.features in bootstrap.toml allows specifying which features to enable
212    // for a specific tool. `extra_features` instead is not controlled by the toml and provides
213    // features that are always enabled for a specific tool (e.g. "in-rust-tree" for rust-analyzer).
214    // Finally, `prepare_tool_cargo` above here might add more features to adapt the build
215    // to the chosen flags (e.g. "all-static" for cargo if `cargo_native_static` is true).
216    builder
217        .config
218        .tool
219        .iter()
220        .filter(|(tool_name, _)| path.file_name().and_then(OsStr::to_str) == Some(tool_name))
221        .for_each(|(_, tool)| features.extend(tool.features.clone().unwrap_or_default()));
222
223    // clippy tests need to know about the stage sysroot. Set them consistently while building to
224    // avoid rebuilding when running tests.
225    cargo.env("SYSROOT", builder.sysroot(compiler));
226
227    // Make sure we explicitly add rustc_private libs to path centrally here so that
228    // RustcPrivate tools can pick them up.
229    if mode == Mode::ToolRustcPrivate {
230        cargo.add_rustc_lib_path(builder);
231    }
232
233    // if tools are using lzma we want to force the build script to build its
234    // own copy
235    cargo.env("LZMA_API_STATIC", "1");
236
237    // See also the "JEMALLOC_SYS_WITH_LG_PAGE" setting in the compile build step.
238    if builder.config.jemalloc(target) && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none() {
239        // Build jemalloc on AArch64 with support for page sizes up to 64K
240        // See: https://github.com/rust-lang/rust/pull/135081
241        if target.starts_with("aarch64") {
242            cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "16");
243        }
244        // Build jemalloc on LoongArch with support for page sizes up to 16K
245        else if target.starts_with("loongarch") {
246            cargo.env("JEMALLOC_SYS_WITH_LG_PAGE", "14");
247        }
248    }
249
250    // CFG_RELEASE is needed by rustfmt (and possibly other tools) which
251    // import rustc-ap-rustc_attr which requires this to be set for the
252    // `#[cfg(version(...))]` attribute.
253    cargo.env("CFG_RELEASE", builder.rust_release());
254    cargo.env("CFG_RELEASE_CHANNEL", &builder.config.channel);
255    cargo.env("CFG_VERSION", builder.rust_version());
256    cargo.env("CFG_RELEASE_NUM", &builder.version);
257    cargo.env("DOC_RUST_LANG_ORG_CHANNEL", builder.doc_rust_lang_org_channel());
258
259    if let Some(ref ver_date) = builder.rust_info().commit_date() {
260        cargo.env("CFG_VER_DATE", ver_date);
261    }
262
263    if let Some(ref ver_hash) = builder.rust_info().sha() {
264        cargo.env("CFG_VER_HASH", ver_hash);
265    }
266
267    if let Some(description) = &builder.config.description {
268        cargo.env("CFG_VER_DESCRIPTION", description);
269    }
270
271    let info = builder.config.git_info(builder.config.omit_git_hash, &dir);
272    if let Some(sha) = info.sha() {
273        cargo.env("CFG_COMMIT_HASH", sha);
274    }
275
276    if let Some(sha_short) = info.sha_short() {
277        cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
278    }
279
280    if let Some(date) = info.commit_date() {
281        cargo.env("CFG_COMMIT_DATE", date);
282    }
283
284    if !features.is_empty() {
285        cargo.arg("--features").arg(features.join(", "));
286    }
287
288    // Enable internal lints for clippy and rustdoc
289    // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
290    // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
291    //
292    // NOTE: We unconditionally set this here to avoid recompiling tools between `x check $tool`
293    // and `x test $tool` executions.
294    // See https://github.com/rust-lang/rust/issues/116538
295    cargo.rustflag("-Zunstable-options");
296
297    // NOTE: The root cause of needing `-Zon-broken-pipe=kill` in the first place is because `rustc`
298    // and `rustdoc` doesn't gracefully handle I/O errors due to usages of raw std `println!` macros
299    // which panics upon encountering broken pipes. `-Zon-broken-pipe=kill` just papers over that
300    // and stops rustc/rustdoc ICEing on e.g. `rustc --print=sysroot | false`.
301    //
302    // cargo explicitly does not want the `-Zon-broken-pipe=kill` paper because it does actually use
303    // variants of `println!` that handles I/O errors gracefully. It's also a breaking change for a
304    // spawn process not written in Rust, especially if the language default handler is not
305    // `SIG_IGN`. Thankfully cargo tests will break if we do set the flag.
306    //
307    // For the cargo discussion, see
308    // <https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Applying.20.60-Zon-broken-pipe.3Dkill.60.20flags.20in.20bootstrap.3F>.
309    //
310    // For the rustc discussion, see
311    // <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Internal.20lint.20for.20raw.20.60print!.60.20and.20.60println!.60.3F>
312    // for proper solutions.
313    if !path.ends_with("cargo") {
314        // Use an untracked env var `FORCE_ON_BROKEN_PIPE_KILL` here instead of `RUSTFLAGS`.
315        // `RUSTFLAGS` is tracked by cargo. Conditionally omitting `-Zon-broken-pipe=kill` from
316        // `RUSTFLAGS` causes unnecessary tool rebuilds due to cache invalidation from building e.g.
317        // cargo *without* `-Zon-broken-pipe=kill` but then rustdoc *with* `-Zon-broken-pipe=kill`.
318        cargo.env("FORCE_ON_BROKEN_PIPE_KILL", "-Zon-broken-pipe=kill");
319    }
320
321    cargo
322}
323
324/// Determines how to build a `ToolTarget`, i.e. which compiler should be used to compile it.
325/// The compiler stage is automatically bumped if we need to cross-compile a stage 1 tool.
326pub enum ToolTargetBuildMode {
327    /// Build the tool for the given `target` using rustc that corresponds to the top CLI
328    /// stage.
329    Build(TargetSelection),
330    /// Build the tool so that it can be attached to the sysroot of the passed compiler.
331    /// Since we always dist stage 2+, the compiler that builds the tool in this case has to be
332    /// stage 1+.
333    Dist(Compiler),
334}
335
336/// Returns compiler that is able to compile a `ToolTarget` tool with the given `mode`.
337pub(crate) fn get_tool_target_compiler(
338    builder: &Builder<'_>,
339    mode: ToolTargetBuildMode,
340) -> Compiler {
341    let (target, build_compiler_stage) = match mode {
342        ToolTargetBuildMode::Build(target) => {
343            assert!(builder.top_stage > 0);
344            // If we want to build a stage N tool, we need to compile it with stage N-1 rustc
345            (target, builder.top_stage - 1)
346        }
347        ToolTargetBuildMode::Dist(target_compiler) => {
348            assert!(target_compiler.stage > 0);
349            // If we want to dist a stage N rustc, we want to attach stage N tool to it.
350            // And to build that tool, we need to compile it with stage N-1 rustc
351            (target_compiler.host, target_compiler.stage - 1)
352        }
353    };
354
355    let compiler = if builder.host_target == target {
356        builder.compiler(build_compiler_stage, builder.host_target)
357    } else {
358        // If we are cross-compiling a stage 1 tool, we cannot do that with a stage 0 compiler,
359        // so we auto-bump the tool's stage to 2, which means we need a stage 1 compiler.
360        let build_compiler = builder.compiler(build_compiler_stage.max(1), builder.host_target);
361        // We also need the host stdlib to compile host code (proc macros/build scripts)
362        builder.std(build_compiler, builder.host_target);
363        build_compiler
364    };
365    builder.std(compiler, target);
366    compiler
367}
368
369/// Links a built tool binary with the given `name` from the build directory to the
370/// tools directory.
371fn copy_link_tool_bin(
372    builder: &Builder<'_>,
373    build_compiler: Compiler,
374    target: TargetSelection,
375    mode: Mode,
376    name: &str,
377) -> PathBuf {
378    let cargo_out = builder.cargo_out(build_compiler, mode, target).join(exe(name, target));
379    let bin = builder.tools_dir(build_compiler).join(exe(name, target));
380    builder.copy_link(&cargo_out, &bin, FileType::Executable);
381    bin
382}
383
384macro_rules! bootstrap_tool {
385    ($(
386        $name:ident, $path:expr, $tool_name:expr
387        $(,is_external_tool = $external:expr)*
388        $(,allow_features = $allow_features:expr)?
389        $(,submodules = $submodules:expr)?
390        $(,artifact_kind = $artifact_kind:expr)?
391        ;
392    )+) => {
393        #[derive(PartialEq, Eq, Clone)]
394        pub enum Tool {
395            $(
396                $name,
397            )+
398        }
399
400        impl<'a> Builder<'a> {
401            /// Ensure a tool is built, then get the path to its executable.
402            ///
403            /// The actual building, if any, will be handled via [`ToolBuild`].
404            pub fn tool_exe(&self, tool: Tool) -> PathBuf {
405                match tool {
406                    $(Tool::$name =>
407                        self.ensure($name {
408                            compiler: self.compiler(0, self.config.host_target),
409                            target: self.config.host_target,
410                        }).tool_path,
411                    )+
412                }
413            }
414        }
415
416        $(
417            #[derive(Debug, Clone, Hash, PartialEq, Eq)]
418        pub struct $name {
419            pub compiler: Compiler,
420            pub target: TargetSelection,
421        }
422
423        impl Step for $name {
424            type Output = ToolBuildResult;
425
426            fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
427                run.path($path)
428            }
429
430            fn make_run(run: RunConfig<'_>) {
431                run.builder.ensure($name {
432                    // snapshot compiler
433                    compiler: run.builder.compiler(0, run.builder.config.host_target),
434                    target: run.target,
435                });
436            }
437
438            fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
439                $(
440                    for submodule in $submodules {
441                        builder.require_submodule(submodule, None);
442                    }
443                )*
444
445                builder.ensure(ToolBuild {
446                    build_compiler: self.compiler,
447                    target: self.target,
448                    tool: $tool_name,
449                    mode: Mode::ToolBootstrap,
450                    path: $path,
451                    source_type: if false $(|| $external)* {
452                        SourceType::Submodule
453                    } else {
454                        SourceType::InTree
455                    },
456                    extra_features: vec![],
457                    allow_features: {
458                        let mut _value = "";
459                        $( _value = $allow_features; )?
460                        _value
461                    },
462                    cargo_args: vec![],
463                    artifact_kind: if false $(|| $artifact_kind == ToolArtifactKind::Library)* {
464                        ToolArtifactKind::Library
465                    } else {
466                        ToolArtifactKind::Binary
467                    }
468                })
469            }
470
471            fn metadata(&self) -> Option<StepMetadata> {
472                Some(
473                    StepMetadata::build(stringify!($name), self.target)
474                        .built_by(self.compiler)
475                )
476            }
477        }
478        )+
479    }
480}
481
482bootstrap_tool!(
483    // This is marked as an external tool because it includes dependencies
484    // from submodules. Trying to keep the lints in sync between all the repos
485    // is a bit of a pain. Unfortunately it means the rustbook source itself
486    // doesn't deny warnings, but it is a relatively small piece of code.
487    Rustbook, "src/tools/rustbook", "rustbook", is_external_tool = true, submodules = SUBMODULES_FOR_RUSTBOOK;
488    UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen";
489    Tidy, "src/tools/tidy", "tidy";
490    Linkchecker, "src/tools/linkchecker", "linkchecker";
491    CargoTest, "src/tools/cargotest", "cargotest";
492    Compiletest, "src/tools/compiletest", "compiletest";
493    RemoteTestClient, "src/tools/remote-test-client", "remote-test-client";
494    RustInstaller, "src/tools/rust-installer", "rust-installer";
495    RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
496    LintDocs, "src/tools/lint-docs", "lint-docs";
497    JsonDocCk, "src/tools/jsondocck", "jsondocck";
498    JsonDocLint, "src/tools/jsondoclint", "jsondoclint";
499    HtmlChecker, "src/tools/html-checker", "html-checker";
500    BumpStage0, "src/tools/bump-stage0", "bump-stage0";
501    ReplaceVersionPlaceholder, "src/tools/replace-version-placeholder", "replace-version-placeholder";
502    CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
503    GenerateCopyright, "src/tools/generate-copyright", "generate-copyright";
504    GenerateWindowsSys, "src/tools/generate-windows-sys", "generate-windows-sys";
505    RustdocGUITest, "src/tools/rustdoc-gui-test", "rustdoc-gui-test";
506    CoverageDump, "src/tools/coverage-dump", "coverage-dump";
507    UnicodeTableGenerator, "src/tools/unicode-table-generator", "unicode-table-generator";
508    FeaturesStatusDump, "src/tools/features-status-dump", "features-status-dump";
509    OptimizedDist, "src/tools/opt-dist", "opt-dist", submodules = &["src/tools/rustc-perf"];
510    RunMakeSupport, "src/tools/run-make-support", "run_make_support", artifact_kind = ToolArtifactKind::Library;
511    IntrinsicTest, "library/stdarch/crates/intrinsic-test", "intrinsic-test";
512);
513
514/// These are the submodules that are required for rustbook to work due to
515/// depending on mdbook plugins.
516pub static SUBMODULES_FOR_RUSTBOOK: &[&str] = &["src/doc/book", "src/doc/reference"];
517
518/// The [rustc-perf](https://github.com/rust-lang/rustc-perf) benchmark suite, which is added
519/// as a submodule at `src/tools/rustc-perf`.
520#[derive(Debug, Clone, Hash, PartialEq, Eq)]
521pub struct RustcPerf {
522    pub compiler: Compiler,
523    pub target: TargetSelection,
524}
525
526impl Step for RustcPerf {
527    /// Path to the built `collector` binary.
528    type Output = ToolBuildResult;
529
530    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
531        run.path("src/tools/rustc-perf")
532    }
533
534    fn make_run(run: RunConfig<'_>) {
535        run.builder.ensure(RustcPerf {
536            compiler: run.builder.compiler(0, run.builder.config.host_target),
537            target: run.target,
538        });
539    }
540
541    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
542        // We need to ensure the rustc-perf submodule is initialized.
543        builder.require_submodule("src/tools/rustc-perf", None);
544
545        let tool = ToolBuild {
546            build_compiler: self.compiler,
547            target: self.target,
548            tool: "collector",
549            mode: Mode::ToolBootstrap,
550            path: "src/tools/rustc-perf",
551            source_type: SourceType::Submodule,
552            extra_features: Vec::new(),
553            allow_features: "",
554            // Only build the collector package, which is used for benchmarking through
555            // a CLI.
556            cargo_args: vec!["-p".to_string(), "collector".to_string()],
557            artifact_kind: ToolArtifactKind::Binary,
558        };
559        let res = builder.ensure(tool.clone());
560        // We also need to symlink the `rustc-fake` binary to the corresponding directory,
561        // because `collector` expects it in the same directory.
562        copy_link_tool_bin(builder, tool.build_compiler, tool.target, tool.mode, "rustc-fake");
563
564        res
565    }
566}
567
568#[derive(Debug, Clone, Hash, PartialEq, Eq)]
569pub struct ErrorIndex {
570    compilers: RustcPrivateCompilers,
571}
572
573impl ErrorIndex {
574    pub fn command(builder: &Builder<'_>, compilers: RustcPrivateCompilers) -> BootstrapCommand {
575        // Error-index-generator links with the rustdoc library, so we need to add `rustc_lib_paths`
576        // for rustc_private and libLLVM.so, and `sysroot_lib` for libstd, etc.
577        let mut cmd = command(builder.ensure(ErrorIndex { compilers }).tool_path);
578
579        let target_compiler = compilers.target_compiler();
580        let mut dylib_paths = builder.rustc_lib_paths(target_compiler);
581        dylib_paths.push(builder.sysroot_target_libdir(target_compiler, target_compiler.host));
582        add_dylib_path(dylib_paths, &mut cmd);
583        cmd
584    }
585}
586
587impl Step for ErrorIndex {
588    type Output = ToolBuildResult;
589
590    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
591        run.path("src/tools/error_index_generator")
592    }
593
594    fn make_run(run: RunConfig<'_>) {
595        // NOTE: This `make_run` isn't used in normal situations, only if you
596        // manually build the tool with `x.py build
597        // src/tools/error-index-generator` which almost nobody does.
598        // Normally, `x.py test` or `x.py doc` will use the
599        // `ErrorIndex::command` function instead.
600        run.builder.ensure(ErrorIndex {
601            compilers: RustcPrivateCompilers::new(
602                run.builder,
603                run.builder.top_stage,
604                run.builder.host_target,
605            ),
606        });
607    }
608
609    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
610        builder.require_submodule(
611            "src/doc/reference",
612            Some("error_index_generator requires mdbook-spec"),
613        );
614        builder
615            .require_submodule("src/doc/book", Some("error_index_generator requires mdbook-trpl"));
616        builder.ensure(ToolBuild {
617            build_compiler: self.compilers.build_compiler,
618            target: self.compilers.target(),
619            tool: "error_index_generator",
620            mode: Mode::ToolRustcPrivate,
621            path: "src/tools/error_index_generator",
622            source_type: SourceType::InTree,
623            extra_features: Vec::new(),
624            allow_features: "",
625            cargo_args: Vec::new(),
626            artifact_kind: ToolArtifactKind::Binary,
627        })
628    }
629
630    fn metadata(&self) -> Option<StepMetadata> {
631        Some(
632            StepMetadata::build("error-index", self.compilers.target())
633                .built_by(self.compilers.build_compiler),
634        )
635    }
636}
637
638#[derive(Debug, Clone, Hash, PartialEq, Eq)]
639pub struct RemoteTestServer {
640    pub build_compiler: Compiler,
641    pub target: TargetSelection,
642}
643
644impl Step for RemoteTestServer {
645    type Output = ToolBuildResult;
646
647    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
648        run.path("src/tools/remote-test-server")
649    }
650
651    fn make_run(run: RunConfig<'_>) {
652        run.builder.ensure(RemoteTestServer {
653            build_compiler: get_tool_target_compiler(
654                run.builder,
655                ToolTargetBuildMode::Build(run.target),
656            ),
657            target: run.target,
658        });
659    }
660
661    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
662        builder.ensure(ToolBuild {
663            build_compiler: self.build_compiler,
664            target: self.target,
665            tool: "remote-test-server",
666            mode: Mode::ToolTarget,
667            path: "src/tools/remote-test-server",
668            source_type: SourceType::InTree,
669            extra_features: Vec::new(),
670            allow_features: "",
671            cargo_args: Vec::new(),
672            artifact_kind: ToolArtifactKind::Binary,
673        })
674    }
675
676    fn metadata(&self) -> Option<StepMetadata> {
677        Some(StepMetadata::build("remote-test-server", self.target).built_by(self.build_compiler))
678    }
679}
680
681/// Represents `Rustdoc` that either comes from the external stage0 sysroot or that is built
682/// locally.
683/// Rustdoc is special, because it both essentially corresponds to a `Compiler` (that can be
684/// externally provided), but also to a `ToolRustcPrivate` tool.
685#[derive(Debug, Clone, Hash, PartialEq, Eq)]
686pub struct Rustdoc {
687    /// If the stage of `target_compiler` is `0`, then rustdoc is externally provided.
688    /// Otherwise it is built locally.
689    pub target_compiler: Compiler,
690}
691
692impl Step for Rustdoc {
693    /// Path to the built rustdoc binary.
694    type Output = PathBuf;
695
696    const IS_HOST: bool = true;
697
698    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
699        run.selectors(&["src/tools/rustdoc", "src/librustdoc"])
700    }
701
702    fn is_default_step(_builder: &Builder<'_>) -> bool {
703        true
704    }
705
706    fn make_run(run: RunConfig<'_>) {
707        run.builder.ensure(Rustdoc {
708            target_compiler: run.builder.compiler(run.builder.top_stage, run.target),
709        });
710    }
711
712    fn run(self, builder: &Builder<'_>) -> Self::Output {
713        let target_compiler = self.target_compiler;
714        let target = target_compiler.host;
715
716        // If stage is 0, we use a prebuilt rustdoc from stage0
717        if target_compiler.stage == 0 {
718            if !target_compiler.is_snapshot(builder) {
719                panic!("rustdoc in stage 0 must be snapshot rustdoc");
720            }
721
722            return builder.initial_rustdoc.clone();
723        }
724
725        // If stage is higher, we build rustdoc instead
726        let bin_rustdoc = || {
727            let sysroot = builder.sysroot(target_compiler);
728            let bindir = sysroot.join("bin");
729            t!(fs::create_dir_all(&bindir));
730            let bin_rustdoc = bindir.join(exe("rustdoc", target_compiler.host));
731            let _ = fs::remove_file(&bin_rustdoc);
732            bin_rustdoc
733        };
734
735        // If CI rustc is enabled and we haven't modified the rustdoc sources,
736        // use the precompiled rustdoc from CI rustc's sysroot to speed up bootstrapping.
737        if builder.download_rustc() && builder.rust_info().is_managed_git_subrepository() {
738            let files_to_track = &["src/librustdoc", "src/tools/rustdoc", "src/rustdoc-json-types"];
739
740            // Check if unchanged
741            if !builder.config.has_changes_from_upstream(files_to_track) {
742                let precompiled_rustdoc = builder
743                    .config
744                    .ci_rustc_dir()
745                    .join("bin")
746                    .join(exe("rustdoc", target_compiler.host));
747
748                let bin_rustdoc = bin_rustdoc();
749                builder.copy_link(&precompiled_rustdoc, &bin_rustdoc, FileType::Executable);
750                return bin_rustdoc;
751            }
752        }
753
754        // The presence of `target_compiler` ensures that the necessary libraries (codegen backends,
755        // compiler libraries, ...) are built. Rustdoc does not require the presence of any
756        // libraries within sysroot_libdir (i.e., rustlib), though doctests may want it (since
757        // they'll be linked to those libraries). As such, don't explicitly `ensure` any additional
758        // libraries here. The intuition here is that If we've built a compiler, we should be able
759        // to build rustdoc.
760        //
761        let mut extra_features = Vec::new();
762        if builder.config.jemalloc(target) {
763            extra_features.push("jemalloc".to_string());
764        }
765
766        let compilers = RustcPrivateCompilers::from_target_compiler(builder, target_compiler);
767        let tool_path = builder
768            .ensure(ToolBuild {
769                build_compiler: compilers.build_compiler,
770                target,
771                // Cargo adds a number of paths to the dylib search path on windows, which results in
772                // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
773                // rustdoc a different name.
774                tool: "rustdoc_tool_binary",
775                mode: Mode::ToolRustcPrivate,
776                path: "src/tools/rustdoc",
777                source_type: SourceType::InTree,
778                extra_features,
779                allow_features: "",
780                cargo_args: Vec::new(),
781                artifact_kind: ToolArtifactKind::Binary,
782            })
783            .tool_path;
784
785        if builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None {
786            // Due to LTO a lot of debug info from C++ dependencies such as jemalloc can make it into
787            // our final binaries
788            compile::strip_debug(builder, target, &tool_path);
789        }
790        let bin_rustdoc = bin_rustdoc();
791        builder.copy_link(&tool_path, &bin_rustdoc, FileType::Executable);
792        bin_rustdoc
793    }
794
795    fn metadata(&self) -> Option<StepMetadata> {
796        Some(
797            StepMetadata::build("rustdoc", self.target_compiler.host)
798                .stage(self.target_compiler.stage),
799        )
800    }
801}
802
803/// Builds the cargo tool.
804/// Note that it can be built using a stable compiler.
805#[derive(Debug, Clone, Hash, PartialEq, Eq)]
806pub struct Cargo {
807    build_compiler: Compiler,
808    target: TargetSelection,
809}
810
811impl Cargo {
812    /// Returns `Cargo` that will be **compiled** by the passed compiler, for the given
813    /// `target`.
814    pub fn from_build_compiler(build_compiler: Compiler, target: TargetSelection) -> Self {
815        Self { build_compiler, target }
816    }
817}
818
819impl Step for Cargo {
820    type Output = ToolBuildResult;
821    const IS_HOST: bool = true;
822
823    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
824        run.path("src/tools/cargo")
825    }
826
827    fn is_default_step(builder: &Builder<'_>) -> bool {
828        builder.tool_enabled("cargo")
829    }
830
831    fn make_run(run: RunConfig<'_>) {
832        run.builder.ensure(Cargo {
833            build_compiler: get_tool_target_compiler(
834                run.builder,
835                ToolTargetBuildMode::Build(run.target),
836            ),
837            target: run.target,
838        });
839    }
840
841    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
842        builder.build.require_submodule("src/tools/cargo", None);
843
844        builder.std(self.build_compiler, builder.host_target);
845        builder.std(self.build_compiler, self.target);
846
847        builder.ensure(ToolBuild {
848            build_compiler: self.build_compiler,
849            target: self.target,
850            tool: "cargo",
851            mode: Mode::ToolTarget,
852            path: "src/tools/cargo",
853            source_type: SourceType::Submodule,
854            extra_features: Vec::new(),
855            // Cargo is compilable with a stable compiler, but since we run in bootstrap,
856            // with RUSTC_BOOTSTRAP being set, some "clever" build scripts enable specialization
857            // based on this, which breaks stuff. We thus have to explicitly allow these features
858            // here.
859            allow_features: "min_specialization,specialization",
860            cargo_args: Vec::new(),
861            artifact_kind: ToolArtifactKind::Binary,
862        })
863    }
864
865    fn metadata(&self) -> Option<StepMetadata> {
866        Some(StepMetadata::build("cargo", self.target).built_by(self.build_compiler))
867    }
868}
869
870/// Represents a built LldWrapper, the `lld-wrapper` tool itself, and a directory
871/// containing a build of LLD.
872#[derive(Clone)]
873pub struct BuiltLldWrapper {
874    tool: ToolBuildResult,
875    lld_dir: PathBuf,
876}
877
878#[derive(Debug, Clone, Hash, PartialEq, Eq)]
879pub struct LldWrapper {
880    pub build_compiler: Compiler,
881    pub target: TargetSelection,
882}
883
884impl LldWrapper {
885    /// Returns `LldWrapper` that should be **used** by the passed compiler.
886    pub fn for_use_by_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
887        Self {
888            build_compiler: get_tool_target_compiler(
889                builder,
890                ToolTargetBuildMode::Dist(target_compiler),
891            ),
892            target: target_compiler.host,
893        }
894    }
895}
896
897impl Step for LldWrapper {
898    type Output = BuiltLldWrapper;
899
900    const IS_HOST: bool = true;
901
902    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
903        run.path("src/tools/lld-wrapper")
904    }
905
906    fn make_run(run: RunConfig<'_>) {
907        run.builder.ensure(LldWrapper {
908            build_compiler: get_tool_target_compiler(
909                run.builder,
910                ToolTargetBuildMode::Build(run.target),
911            ),
912            target: run.target,
913        });
914    }
915
916    fn run(self, builder: &Builder<'_>) -> Self::Output {
917        let lld_dir = builder.ensure(llvm::Lld { target: self.target });
918        let tool = builder.ensure(ToolBuild {
919            build_compiler: self.build_compiler,
920            target: self.target,
921            tool: "lld-wrapper",
922            mode: Mode::ToolTarget,
923            path: "src/tools/lld-wrapper",
924            source_type: SourceType::InTree,
925            extra_features: Vec::new(),
926            allow_features: "",
927            cargo_args: Vec::new(),
928            artifact_kind: ToolArtifactKind::Binary,
929        });
930        BuiltLldWrapper { tool, lld_dir }
931    }
932
933    fn metadata(&self) -> Option<StepMetadata> {
934        Some(StepMetadata::build("LldWrapper", self.target).built_by(self.build_compiler))
935    }
936}
937
938pub(crate) fn copy_lld_artifacts(
939    builder: &Builder<'_>,
940    lld_wrapper: BuiltLldWrapper,
941    target_compiler: Compiler,
942) {
943    let target = target_compiler.host;
944
945    let libdir_bin = builder.sysroot_target_bindir(target_compiler, target);
946    t!(fs::create_dir_all(&libdir_bin));
947
948    let src_exe = exe("lld", target);
949    let dst_exe = exe("rust-lld", target);
950
951    builder.copy_link(
952        &lld_wrapper.lld_dir.join("bin").join(src_exe),
953        &libdir_bin.join(dst_exe),
954        FileType::Executable,
955    );
956    let self_contained_lld_dir = libdir_bin.join("gcc-ld");
957    t!(fs::create_dir_all(&self_contained_lld_dir));
958
959    for name in crate::LLD_FILE_NAMES {
960        builder.copy_link(
961            &lld_wrapper.tool.tool_path,
962            &self_contained_lld_dir.join(exe(name, target)),
963            FileType::Executable,
964        );
965    }
966}
967
968/// Builds the `wasm-component-ld` linker wrapper, which is shipped with rustc to be executed on the
969/// host platform where rustc runs.
970#[derive(Debug, Clone, Hash, PartialEq, Eq)]
971pub struct WasmComponentLd {
972    build_compiler: Compiler,
973    target: TargetSelection,
974}
975
976impl WasmComponentLd {
977    /// Returns `WasmComponentLd` that should be **used** by the passed compiler.
978    pub fn for_use_by_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
979        Self {
980            build_compiler: get_tool_target_compiler(
981                builder,
982                ToolTargetBuildMode::Dist(target_compiler),
983            ),
984            target: target_compiler.host,
985        }
986    }
987}
988
989impl Step for WasmComponentLd {
990    type Output = ToolBuildResult;
991
992    const IS_HOST: bool = true;
993
994    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
995        run.path("src/tools/wasm-component-ld")
996    }
997
998    fn make_run(run: RunConfig<'_>) {
999        run.builder.ensure(WasmComponentLd {
1000            build_compiler: get_tool_target_compiler(
1001                run.builder,
1002                ToolTargetBuildMode::Build(run.target),
1003            ),
1004            target: run.target,
1005        });
1006    }
1007
1008    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1009        builder.ensure(ToolBuild {
1010            build_compiler: self.build_compiler,
1011            target: self.target,
1012            tool: "wasm-component-ld",
1013            mode: Mode::ToolTarget,
1014            path: "src/tools/wasm-component-ld",
1015            source_type: SourceType::InTree,
1016            extra_features: vec![],
1017            allow_features: "",
1018            cargo_args: vec![],
1019            artifact_kind: ToolArtifactKind::Binary,
1020        })
1021    }
1022
1023    fn metadata(&self) -> Option<StepMetadata> {
1024        Some(StepMetadata::build("WasmComponentLd", self.target).built_by(self.build_compiler))
1025    }
1026}
1027
1028#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1029pub struct RustAnalyzer {
1030    compilers: RustcPrivateCompilers,
1031}
1032
1033impl RustAnalyzer {
1034    pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1035        Self { compilers }
1036    }
1037}
1038
1039impl RustAnalyzer {
1040    pub const ALLOW_FEATURES: &'static str = "rustc_private,proc_macro_internals,proc_macro_diagnostic,proc_macro_span,proc_macro_span_shrink,proc_macro_def_site,new_zeroed_alloc";
1041}
1042
1043impl Step for RustAnalyzer {
1044    type Output = ToolBuildResult;
1045    const IS_HOST: bool = true;
1046
1047    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1048        run.path("src/tools/rust-analyzer")
1049    }
1050
1051    fn is_default_step(builder: &Builder<'_>) -> bool {
1052        builder.tool_enabled("rust-analyzer")
1053    }
1054
1055    fn make_run(run: RunConfig<'_>) {
1056        run.builder.ensure(RustAnalyzer {
1057            compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1058        });
1059    }
1060
1061    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1062        let build_compiler = self.compilers.build_compiler;
1063        let target = self.compilers.target();
1064        builder.ensure(ToolBuild {
1065            build_compiler,
1066            target,
1067            tool: "rust-analyzer",
1068            mode: Mode::ToolRustcPrivate,
1069            path: "src/tools/rust-analyzer",
1070            extra_features: vec!["in-rust-tree".to_owned()],
1071            source_type: SourceType::InTree,
1072            allow_features: RustAnalyzer::ALLOW_FEATURES,
1073            cargo_args: Vec::new(),
1074            artifact_kind: ToolArtifactKind::Binary,
1075        })
1076    }
1077
1078    fn metadata(&self) -> Option<StepMetadata> {
1079        Some(
1080            StepMetadata::build("rust-analyzer", self.compilers.target())
1081                .built_by(self.compilers.build_compiler),
1082        )
1083    }
1084}
1085
1086#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1087pub struct RustAnalyzerProcMacroSrv {
1088    compilers: RustcPrivateCompilers,
1089}
1090
1091impl RustAnalyzerProcMacroSrv {
1092    pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1093        Self { compilers }
1094    }
1095}
1096
1097impl Step for RustAnalyzerProcMacroSrv {
1098    type Output = ToolBuildResult;
1099    const IS_HOST: bool = true;
1100
1101    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1102        // Allow building `rust-analyzer-proc-macro-srv` both as part of the `rust-analyzer` and as a stand-alone tool.
1103        run.path("src/tools/rust-analyzer")
1104            .path("src/tools/rust-analyzer/crates/proc-macro-srv-cli")
1105    }
1106
1107    fn is_default_step(builder: &Builder<'_>) -> bool {
1108        builder.tool_enabled("rust-analyzer")
1109            || builder.tool_enabled("rust-analyzer-proc-macro-srv")
1110    }
1111
1112    fn make_run(run: RunConfig<'_>) {
1113        run.builder.ensure(RustAnalyzerProcMacroSrv {
1114            compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1115        });
1116    }
1117
1118    fn run(self, builder: &Builder<'_>) -> Self::Output {
1119        let tool_result = builder.ensure(ToolBuild {
1120            build_compiler: self.compilers.build_compiler,
1121            target: self.compilers.target(),
1122            tool: "rust-analyzer-proc-macro-srv",
1123            mode: Mode::ToolRustcPrivate,
1124            path: "src/tools/rust-analyzer/crates/proc-macro-srv-cli",
1125            extra_features: vec!["in-rust-tree".to_owned()],
1126            source_type: SourceType::InTree,
1127            allow_features: RustAnalyzer::ALLOW_FEATURES,
1128            cargo_args: Vec::new(),
1129            artifact_kind: ToolArtifactKind::Binary,
1130        });
1131
1132        // Copy `rust-analyzer-proc-macro-srv` to `<sysroot>/libexec/`
1133        // so that r-a can use it.
1134        let libexec_path = builder.sysroot(self.compilers.target_compiler).join("libexec");
1135        t!(fs::create_dir_all(&libexec_path));
1136        builder.copy_link(
1137            &tool_result.tool_path,
1138            &libexec_path.join("rust-analyzer-proc-macro-srv"),
1139            FileType::Executable,
1140        );
1141
1142        tool_result
1143    }
1144
1145    fn metadata(&self) -> Option<StepMetadata> {
1146        Some(
1147            StepMetadata::build("rust-analyzer-proc-macro-srv", self.compilers.target())
1148                .built_by(self.compilers.build_compiler),
1149        )
1150    }
1151}
1152
1153#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1154pub struct LlvmBitcodeLinker {
1155    build_compiler: Compiler,
1156    target: TargetSelection,
1157}
1158
1159impl LlvmBitcodeLinker {
1160    /// Returns `LlvmBitcodeLinker` that will be **compiled** by the passed compiler, for the given
1161    /// `target`.
1162    pub fn from_build_compiler(build_compiler: Compiler, target: TargetSelection) -> Self {
1163        Self { build_compiler, target }
1164    }
1165
1166    /// Returns `LlvmBitcodeLinker` that should be **used** by the passed compiler.
1167    pub fn from_target_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
1168        Self {
1169            build_compiler: get_tool_target_compiler(
1170                builder,
1171                ToolTargetBuildMode::Dist(target_compiler),
1172            ),
1173            target: target_compiler.host,
1174        }
1175    }
1176
1177    /// Return a compiler that is able to build this tool for the given `target`.
1178    pub fn get_build_compiler_for_target(
1179        builder: &Builder<'_>,
1180        target: TargetSelection,
1181    ) -> Compiler {
1182        get_tool_target_compiler(builder, ToolTargetBuildMode::Build(target))
1183    }
1184}
1185
1186impl Step for LlvmBitcodeLinker {
1187    type Output = ToolBuildResult;
1188    const IS_HOST: bool = true;
1189
1190    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1191        run.path("src/tools/llvm-bitcode-linker")
1192    }
1193
1194    fn is_default_step(builder: &Builder<'_>) -> bool {
1195        builder.tool_enabled("llvm-bitcode-linker")
1196    }
1197
1198    fn make_run(run: RunConfig<'_>) {
1199        run.builder.ensure(LlvmBitcodeLinker {
1200            build_compiler: Self::get_build_compiler_for_target(run.builder, run.target),
1201            target: run.target,
1202        });
1203    }
1204
1205    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1206        builder.ensure(ToolBuild {
1207            build_compiler: self.build_compiler,
1208            target: self.target,
1209            tool: "llvm-bitcode-linker",
1210            mode: Mode::ToolTarget,
1211            path: "src/tools/llvm-bitcode-linker",
1212            source_type: SourceType::InTree,
1213            extra_features: vec![],
1214            allow_features: "",
1215            cargo_args: Vec::new(),
1216            artifact_kind: ToolArtifactKind::Binary,
1217        })
1218    }
1219
1220    fn metadata(&self) -> Option<StepMetadata> {
1221        Some(StepMetadata::build("LlvmBitcodeLinker", self.target).built_by(self.build_compiler))
1222    }
1223}
1224
1225#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1226pub struct LibcxxVersionTool {
1227    pub target: TargetSelection,
1228}
1229
1230#[expect(dead_code)]
1231#[derive(Debug, Clone)]
1232pub enum LibcxxVersion {
1233    Gnu(usize),
1234    Llvm(usize),
1235}
1236
1237impl Step for LibcxxVersionTool {
1238    type Output = LibcxxVersion;
1239    const IS_HOST: bool = true;
1240
1241    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1242        run.never()
1243    }
1244
1245    fn is_default_step(_builder: &Builder<'_>) -> bool {
1246        false
1247    }
1248
1249    fn run(self, builder: &Builder<'_>) -> LibcxxVersion {
1250        let out_dir = builder.out.join(self.target.to_string()).join("libcxx-version");
1251        let executable = out_dir.join(exe("libcxx-version", self.target));
1252
1253        // This is a sanity-check specific step, which means it is frequently called (when using
1254        // CI LLVM), and compiling `src/tools/libcxx-version/main.cpp` at the beginning of the bootstrap
1255        // invocation adds a fair amount of overhead to the process (see https://github.com/rust-lang/rust/issues/126423).
1256        // Therefore, we want to avoid recompiling this file unnecessarily.
1257        if !executable.exists() {
1258            if !out_dir.exists() {
1259                t!(fs::create_dir_all(&out_dir));
1260            }
1261
1262            let compiler = builder.cxx(self.target).unwrap();
1263            let mut cmd = command(compiler);
1264
1265            cmd.arg("-o")
1266                .arg(&executable)
1267                .arg(builder.src.join("src/tools/libcxx-version/main.cpp"));
1268
1269            cmd.run(builder);
1270
1271            if !executable.exists() {
1272                panic!("Something went wrong. {} is not present", executable.display());
1273            }
1274        }
1275
1276        let version_output = command(executable).run_capture_stdout(builder).stdout();
1277
1278        let version_str = version_output.split_once("version:").unwrap().1;
1279        let version = version_str.trim().parse::<usize>().unwrap();
1280
1281        if version_output.starts_with("libstdc++") {
1282            LibcxxVersion::Gnu(version)
1283        } else if version_output.starts_with("libc++") {
1284            LibcxxVersion::Llvm(version)
1285        } else {
1286            panic!("Coudln't recognize the standard library version.");
1287        }
1288    }
1289}
1290
1291#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1292pub struct BuildManifest {
1293    compiler: Compiler,
1294    target: TargetSelection,
1295}
1296
1297impl BuildManifest {
1298    pub fn new(builder: &Builder<'_>, target: TargetSelection) -> Self {
1299        BuildManifest { compiler: builder.compiler(1, builder.config.host_target), target }
1300    }
1301}
1302
1303impl Step for BuildManifest {
1304    type Output = ToolBuildResult;
1305
1306    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1307        run.path("src/tools/build-manifest")
1308    }
1309
1310    fn make_run(run: RunConfig<'_>) {
1311        run.builder.ensure(BuildManifest::new(run.builder, run.target));
1312    }
1313
1314    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1315        // Building with the beta compiler will produce a broken build-manifest that doesn't support
1316        // recently stabilized targets/hosts.
1317        assert!(self.compiler.stage != 0);
1318        builder.ensure(ToolBuild {
1319            build_compiler: self.compiler,
1320            target: self.target,
1321            tool: "build-manifest",
1322            mode: Mode::ToolStd,
1323            path: "src/tools/build-manifest",
1324            source_type: SourceType::InTree,
1325            extra_features: vec![],
1326            allow_features: "",
1327            cargo_args: vec![],
1328            artifact_kind: ToolArtifactKind::Binary,
1329        })
1330    }
1331
1332    fn metadata(&self) -> Option<StepMetadata> {
1333        Some(StepMetadata::build("build-manifest", self.target).built_by(self.compiler))
1334    }
1335}
1336
1337/// Represents which compilers are involved in the compilation of a tool
1338/// that depends on compiler internals (`rustc_private`).
1339/// Their compilation looks like this:
1340///
1341/// - `build_compiler` (stage N-1) builds `target_compiler` (stage N) to produce .rlibs
1342///     - These .rlibs are copied into the sysroot of `build_compiler`
1343/// - `build_compiler` (stage N-1) builds `<tool>` (stage N)
1344///     - `<tool>` links to .rlibs from `target_compiler`
1345///
1346/// Eventually, this could also be used for .rmetas and check builds, but so far we only deal with
1347/// normal builds here.
1348#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
1349pub struct RustcPrivateCompilers {
1350    /// Compiler that builds the tool and that builds `target_compiler`.
1351    build_compiler: Compiler,
1352    /// Compiler to which .rlib artifacts the tool links to.
1353    /// The host target of this compiler corresponds to the target of the tool.
1354    target_compiler: Compiler,
1355}
1356
1357impl RustcPrivateCompilers {
1358    /// Create compilers for a `rustc_private` tool with the given `stage` and for the given
1359    /// `target`.
1360    pub fn new(builder: &Builder<'_>, stage: u32, target: TargetSelection) -> Self {
1361        let build_compiler = Self::build_compiler_from_stage(builder, stage);
1362
1363        // This is the compiler we'll link to
1364        // FIXME: make 100% sure that `target_compiler` was indeed built with `build_compiler`...
1365        let target_compiler = builder.compiler(build_compiler.stage + 1, target);
1366
1367        Self { build_compiler, target_compiler }
1368    }
1369
1370    pub fn from_build_and_target_compiler(
1371        build_compiler: Compiler,
1372        target_compiler: Compiler,
1373    ) -> Self {
1374        Self { build_compiler, target_compiler }
1375    }
1376
1377    /// Create rustc tool compilers from the build compiler.
1378    pub fn from_build_compiler(
1379        builder: &Builder<'_>,
1380        build_compiler: Compiler,
1381        target: TargetSelection,
1382    ) -> Self {
1383        let target_compiler = builder.compiler(build_compiler.stage + 1, target);
1384        Self { build_compiler, target_compiler }
1385    }
1386
1387    /// Create rustc tool compilers from the target compiler.
1388    pub fn from_target_compiler(builder: &Builder<'_>, target_compiler: Compiler) -> Self {
1389        Self {
1390            build_compiler: Self::build_compiler_from_stage(builder, target_compiler.stage),
1391            target_compiler,
1392        }
1393    }
1394
1395    fn build_compiler_from_stage(builder: &Builder<'_>, stage: u32) -> Compiler {
1396        assert!(stage > 0);
1397
1398        if builder.download_rustc() && stage == 1 {
1399            // We shouldn't drop to stage0 compiler when using CI rustc.
1400            builder.compiler(1, builder.config.host_target)
1401        } else {
1402            builder.compiler(stage - 1, builder.config.host_target)
1403        }
1404    }
1405
1406    pub fn build_compiler(&self) -> Compiler {
1407        self.build_compiler
1408    }
1409
1410    pub fn target_compiler(&self) -> Compiler {
1411        self.target_compiler
1412    }
1413
1414    /// Target of the tool being compiled
1415    pub fn target(&self) -> TargetSelection {
1416        self.target_compiler.host
1417    }
1418}
1419
1420/// Creates a step that builds an extended `Mode::ToolRustcPrivate` tool
1421/// and installs it into the sysroot of a corresponding compiler.
1422macro_rules! tool_rustc_extended {
1423    (
1424        $name:ident {
1425            path: $path:expr,
1426            tool_name: $tool_name:expr,
1427            stable: $stable:expr
1428            $( , add_bins_to_sysroot: $add_bins_to_sysroot:expr )?
1429            $( , add_features: $add_features:expr )?
1430            $( , cargo_args: $cargo_args:expr )?
1431            $( , )?
1432        }
1433    ) => {
1434        #[derive(Debug, Clone, Hash, PartialEq, Eq)]
1435        pub struct $name {
1436            compilers: RustcPrivateCompilers,
1437        }
1438
1439        impl $name {
1440            pub fn from_compilers(compilers: RustcPrivateCompilers) -> Self {
1441                Self {
1442                    compilers,
1443                }
1444            }
1445        }
1446
1447        impl Step for $name {
1448            type Output = ToolBuildResult;
1449            const IS_HOST: bool = true;
1450
1451            fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1452                should_run_extended_rustc_tool(
1453                    run,
1454                    $path,
1455                )
1456            }
1457
1458            fn is_default_step(builder: &Builder<'_>) -> bool {
1459                extended_rustc_tool_is_default_step(
1460                    builder,
1461                    $tool_name,
1462                    $stable,
1463                )
1464            }
1465
1466            fn make_run(run: RunConfig<'_>) {
1467                run.builder.ensure($name {
1468                    compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1469                });
1470            }
1471
1472            fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1473                let Self { compilers } = self;
1474                build_extended_rustc_tool(
1475                    builder,
1476                    compilers,
1477                    $tool_name,
1478                    $path,
1479                    None $( .or(Some(&$add_bins_to_sysroot)) )?,
1480                    None $( .or(Some($add_features)) )?,
1481                    None $( .or(Some($cargo_args)) )?,
1482                )
1483            }
1484
1485            fn metadata(&self) -> Option<StepMetadata> {
1486                Some(
1487                    StepMetadata::build($tool_name, self.compilers.target())
1488                        .built_by(self.compilers.build_compiler)
1489                )
1490            }
1491        }
1492    }
1493}
1494
1495fn should_run_extended_rustc_tool<'a>(run: ShouldRun<'a>, path: &'static str) -> ShouldRun<'a> {
1496    run.path(path)
1497}
1498
1499fn extended_rustc_tool_is_default_step(
1500    builder: &Builder<'_>,
1501    tool_name: &'static str,
1502    stable: bool,
1503) -> bool {
1504    builder.config.extended
1505        && builder.config.tools.as_ref().map_or(
1506            // By default, on nightly/dev enable all tools, else only
1507            // build stable tools.
1508            stable || builder.build.unstable_features(),
1509            // If `tools` is set, search list for this tool.
1510            |tools| {
1511                tools.iter().any(|tool| match tool.as_ref() {
1512                    "clippy" => tool_name == "clippy-driver",
1513                    x => tool_name == x,
1514                })
1515            },
1516        )
1517}
1518
1519fn build_extended_rustc_tool(
1520    builder: &Builder<'_>,
1521    compilers: RustcPrivateCompilers,
1522    tool_name: &'static str,
1523    path: &'static str,
1524    add_bins_to_sysroot: Option<&[&str]>,
1525    add_features: Option<fn(&Builder<'_>, TargetSelection, &mut Vec<String>)>,
1526    cargo_args: Option<&[&'static str]>,
1527) -> ToolBuildResult {
1528    let target = compilers.target();
1529    let mut extra_features = Vec::new();
1530    if let Some(func) = add_features {
1531        func(builder, target, &mut extra_features);
1532    }
1533
1534    let build_compiler = compilers.build_compiler;
1535    let ToolBuildResult { tool_path, .. } = builder.ensure(ToolBuild {
1536        build_compiler,
1537        target,
1538        tool: tool_name,
1539        mode: Mode::ToolRustcPrivate,
1540        path,
1541        extra_features,
1542        source_type: SourceType::InTree,
1543        allow_features: "",
1544        cargo_args: cargo_args.unwrap_or_default().iter().map(|s| String::from(*s)).collect(),
1545        artifact_kind: ToolArtifactKind::Binary,
1546    });
1547
1548    let target_compiler = compilers.target_compiler;
1549    if let Some(add_bins_to_sysroot) = add_bins_to_sysroot
1550        && !add_bins_to_sysroot.is_empty()
1551    {
1552        let bindir = builder.sysroot(target_compiler).join("bin");
1553        t!(fs::create_dir_all(&bindir));
1554
1555        for add_bin in add_bins_to_sysroot {
1556            let bin_destination = bindir.join(exe(add_bin, target_compiler.host));
1557            builder.copy_link(&tool_path, &bin_destination, FileType::Executable);
1558        }
1559
1560        // Return a path into the bin dir.
1561        let path = bindir.join(exe(tool_name, target_compiler.host));
1562        ToolBuildResult { tool_path: path, build_compiler }
1563    } else {
1564        ToolBuildResult { tool_path, build_compiler }
1565    }
1566}
1567
1568tool_rustc_extended!(Cargofmt {
1569    path: "src/tools/rustfmt",
1570    tool_name: "cargo-fmt",
1571    stable: true,
1572    add_bins_to_sysroot: ["cargo-fmt"]
1573});
1574tool_rustc_extended!(CargoClippy {
1575    path: "src/tools/clippy",
1576    tool_name: "cargo-clippy",
1577    stable: true,
1578    add_bins_to_sysroot: ["cargo-clippy"]
1579});
1580tool_rustc_extended!(Clippy {
1581    path: "src/tools/clippy",
1582    tool_name: "clippy-driver",
1583    stable: true,
1584    add_bins_to_sysroot: ["clippy-driver"],
1585    add_features: |builder, target, features| {
1586        if builder.config.jemalloc(target) {
1587            features.push("jemalloc".to_string());
1588        }
1589    }
1590});
1591tool_rustc_extended!(Miri {
1592    path: "src/tools/miri",
1593    tool_name: "miri",
1594    stable: false,
1595    add_bins_to_sysroot: ["miri"],
1596    add_features: |builder, target, features| {
1597        if builder.config.jemalloc(target) {
1598            features.push("jemalloc".to_string());
1599        }
1600    },
1601    // Always compile also tests when building miri. Otherwise feature unification can cause rebuilds between building and testing miri.
1602    cargo_args: &["--all-targets"],
1603});
1604tool_rustc_extended!(CargoMiri {
1605    path: "src/tools/miri/cargo-miri",
1606    tool_name: "cargo-miri",
1607    stable: false,
1608    add_bins_to_sysroot: ["cargo-miri"]
1609});
1610tool_rustc_extended!(Rustfmt {
1611    path: "src/tools/rustfmt",
1612    tool_name: "rustfmt",
1613    stable: true,
1614    add_bins_to_sysroot: ["rustfmt"]
1615});
1616
1617pub const TEST_FLOAT_PARSE_ALLOW_FEATURES: &str = "f16,cfg_target_has_reliable_f16_f128";
1618
1619impl Builder<'_> {
1620    /// Gets a `BootstrapCommand` which is ready to run `tool` in `stage` built for
1621    /// `host`.
1622    ///
1623    /// This also ensures that the given tool is built (using [`ToolBuild`]).
1624    pub fn tool_cmd(&self, tool: Tool) -> BootstrapCommand {
1625        let mut cmd = command(self.tool_exe(tool));
1626        let compiler = self.compiler(0, self.config.host_target);
1627        let host = &compiler.host;
1628        // Prepares the `cmd` provided to be able to run the `compiler` provided.
1629        //
1630        // Notably this munges the dynamic library lookup path to point to the
1631        // right location to run `compiler`.
1632        let mut lib_paths: Vec<PathBuf> = discover_out_dirs_with_dylibs(
1633            self.cargo_out(compiler, Mode::ToolBootstrap, *host).join("build"),
1634        );
1635
1636        // On MSVC a tool may invoke a C compiler (e.g., compiletest in run-make
1637        // mode) and that C compiler may need some extra PATH modification. Do
1638        // so here.
1639        if compiler.host.is_msvc() {
1640            let curpaths = env::var_os("PATH").unwrap_or_default();
1641            let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
1642            for (k, v) in self.cc[&compiler.host].env() {
1643                if k != "PATH" {
1644                    continue;
1645                }
1646                for path in env::split_paths(v) {
1647                    if !curpaths.contains(&path) {
1648                        lib_paths.push(path);
1649                    }
1650                }
1651            }
1652        }
1653
1654        add_dylib_path(lib_paths, &mut cmd);
1655
1656        // Provide a RUSTC for this command to use.
1657        cmd.env("RUSTC", &self.initial_rustc);
1658
1659        cmd
1660    }
1661}
1662
1663/// Gets all of the `out` dirs in a given Cargo `build-dir/<profile>/build` dir.
1664fn discover_out_dirs_with_dylibs(dir: PathBuf) -> Vec<PathBuf> {
1665    if !dir.exists() {
1666        return Vec::new();
1667    }
1668    let read_dir = |path: &Path| path.read_dir().ok().into_iter().flatten().filter_map(Result::ok);
1669    let has_dylib = |path: &Path| {
1670        read_dir(path)
1671            .any(|e| e.path().extension().is_some_and(|ext| ext == std::env::consts::DLL_EXTENSION))
1672    };
1673    dir.read_dir()
1674        .unwrap_or_else(|e| panic!("Couldn't read {}: {}", dir.display(), e))
1675        .map(|e| e.unwrap())
1676        .flat_map(|e| read_dir(&e.path()))
1677        .flat_map(|e| read_dir(&e.path()))
1678        .map(|e| e.path())
1679        .filter(|path| path.ends_with("out") && has_dylib(path))
1680        .collect::<Vec<_>>()
1681}