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