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::path::PathBuf;
13use std::{env, fs};
14
15#[cfg(feature = "tracing")]
16use tracing::instrument;
17
18use crate::core::build_steps::compile::is_lto_stage;
19use crate::core::build_steps::toolstate::ToolState;
20use crate::core::build_steps::{compile, llvm};
21use crate::core::builder;
22use crate::core::builder::{
23    Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step, cargo_profile_var,
24};
25use crate::core::config::{DebuginfoLevel, RustcLto, TargetSelection};
26use crate::utils::channel::GitInfo;
27use crate::utils::exec::{BootstrapCommand, command};
28use crate::utils::helpers::{add_dylib_path, exe, t};
29use crate::{Compiler, FileType, Kind, Mode, gha};
30
31#[derive(Debug, Clone, Hash, PartialEq, Eq)]
32pub enum SourceType {
33    InTree,
34    Submodule,
35}
36
37#[derive(Debug, Clone, Hash, PartialEq, Eq)]
38pub enum ToolArtifactKind {
39    Binary,
40    Library,
41}
42
43#[derive(Debug, Clone, Hash, PartialEq, Eq)]
44struct ToolBuild {
45    compiler: Compiler,
46    target: TargetSelection,
47    tool: &'static str,
48    path: &'static str,
49    mode: Mode,
50    source_type: SourceType,
51    extra_features: Vec<String>,
52    /// Nightly-only features that are allowed (comma-separated list).
53    allow_features: &'static str,
54    /// Additional arguments to pass to the `cargo` invocation.
55    cargo_args: Vec<String>,
56    /// Whether the tool builds a binary or a library.
57    artifact_kind: ToolArtifactKind,
58}
59
60impl Builder<'_> {
61    #[track_caller]
62    pub(crate) fn msg_tool(
63        &self,
64        kind: Kind,
65        mode: Mode,
66        tool: &str,
67        build_stage: u32,
68        host: &TargetSelection,
69        target: &TargetSelection,
70    ) -> Option<gha::Group> {
71        match mode {
72            // depends on compiler stage, different to host compiler
73            Mode::ToolRustc => self.msg_sysroot_tool(
74                kind,
75                build_stage,
76                format_args!("tool {tool}"),
77                *host,
78                *target,
79            ),
80            // doesn't depend on compiler, same as host compiler
81            _ => self.msg(Kind::Build, build_stage, format_args!("tool {tool}"), *host, *target),
82        }
83    }
84}
85
86/// Result of the tool build process. Each `Step` in this module is responsible
87/// for using this type as `type Output = ToolBuildResult;`
88#[derive(Clone)]
89pub struct ToolBuildResult {
90    /// Artifact path of the corresponding tool that was built.
91    pub tool_path: PathBuf,
92    /// Compiler used to build the tool. For non-`ToolRustc` tools this is equal to `target_compiler`.
93    /// For `ToolRustc` this is one stage before of the `target_compiler`.
94    pub build_compiler: Compiler,
95    /// Target compiler passed to `Step`.
96    pub target_compiler: Compiler,
97}
98
99impl Step for ToolBuild {
100    type Output = ToolBuildResult;
101
102    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
103        run.never()
104    }
105
106    /// Builds a tool in `src/tools`
107    ///
108    /// This will build the specified tool with the specified `host` compiler in
109    /// `stage` into the normal cargo output directory.
110    fn run(mut self, builder: &Builder<'_>) -> ToolBuildResult {
111        let target = self.target;
112        let mut tool = self.tool;
113        let path = self.path;
114
115        let target_compiler = self.compiler;
116        self.compiler = if self.mode == Mode::ToolRustc {
117            get_tool_rustc_compiler(builder, self.compiler)
118        } else {
119            self.compiler
120        };
121
122        match self.mode {
123            Mode::ToolRustc => {
124                // If compiler was forced, its artifacts should be prepared earlier.
125                if !self.compiler.is_forced_compiler() {
126                    builder.ensure(compile::Std::new(self.compiler, self.compiler.host));
127                    builder.ensure(compile::Rustc::new(self.compiler, target));
128                }
129            }
130            Mode::ToolStd => {
131                // If compiler was forced, its artifacts should be prepared earlier.
132                if !self.compiler.is_forced_compiler() {
133                    builder.ensure(compile::Std::new(self.compiler, target))
134                }
135            }
136            Mode::ToolBootstrap => {} // uses downloaded stage0 compiler libs
137            _ => panic!("unexpected Mode for tool build"),
138        }
139
140        let mut cargo = prepare_tool_cargo(
141            builder,
142            self.compiler,
143            self.mode,
144            target,
145            Kind::Build,
146            path,
147            self.source_type,
148            &self.extra_features,
149        );
150
151        // Rustc tools (miri, clippy, cargo, rustfmt, rust-analyzer)
152        // could use the additional optimizations.
153        if self.mode == Mode::ToolRustc && is_lto_stage(&self.compiler) {
154            let lto = match builder.config.rust_lto {
155                RustcLto::Off => Some("off"),
156                RustcLto::Thin => Some("thin"),
157                RustcLto::Fat => Some("fat"),
158                RustcLto::ThinLocal => None,
159            };
160            if let Some(lto) = lto {
161                cargo.env(cargo_profile_var("LTO", &builder.config), lto);
162            }
163        }
164
165        if !self.allow_features.is_empty() {
166            cargo.allow_features(self.allow_features);
167        }
168
169        cargo.args(self.cargo_args);
170
171        let _guard = builder.msg_tool(
172            Kind::Build,
173            self.mode,
174            self.tool,
175            self.compiler.stage,
176            &self.compiler.host,
177            &self.target,
178        );
179
180        // we check this below
181        let build_success = compile::stream_cargo(builder, cargo, vec![], &mut |_| {});
182
183        builder.save_toolstate(
184            tool,
185            if build_success { ToolState::TestFail } else { ToolState::BuildFail },
186        );
187
188        if !build_success {
189            crate::exit!(1);
190        } else {
191            // HACK(#82501): on Windows, the tools directory gets added to PATH when running tests, and
192            // compiletest confuses HTML tidy with the in-tree tidy. Name the in-tree tidy something
193            // different so the problem doesn't come up.
194            if tool == "tidy" {
195                tool = "rust-tidy";
196            }
197            let tool_path = match self.artifact_kind {
198                ToolArtifactKind::Binary => {
199                    copy_link_tool_bin(builder, self.compiler, self.target, self.mode, tool)
200                }
201                ToolArtifactKind::Library => builder
202                    .cargo_out(self.compiler, self.mode, self.target)
203                    .join(format!("lib{tool}.rlib")),
204            };
205
206            ToolBuildResult { tool_path, build_compiler: self.compiler, target_compiler }
207        }
208    }
209}
210
211#[expect(clippy::too_many_arguments)] // FIXME: reduce the number of args and remove this.
212pub fn prepare_tool_cargo(
213    builder: &Builder<'_>,
214    compiler: Compiler,
215    mode: Mode,
216    target: TargetSelection,
217    cmd_kind: Kind,
218    path: &str,
219    source_type: SourceType,
220    extra_features: &[String],
221) -> CargoCommand {
222    let mut cargo = builder::Cargo::new(builder, compiler, mode, source_type, target, cmd_kind);
223
224    let dir = builder.src.join(path);
225    cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));
226
227    let mut features = extra_features.to_vec();
228    if builder.build.config.cargo_native_static {
229        if path.ends_with("cargo")
230            || path.ends_with("clippy")
231            || path.ends_with("miri")
232            || path.ends_with("rustfmt")
233        {
234            cargo.env("LIBZ_SYS_STATIC", "1");
235        }
236        if path.ends_with("cargo") {
237            features.push("all-static".to_string());
238        }
239    }
240
241    // clippy tests need to know about the stage sysroot. Set them consistently while building to
242    // avoid rebuilding when running tests.
243    cargo.env("SYSROOT", builder.sysroot(compiler));
244
245    // if tools are using lzma we want to force the build script to build its
246    // own copy
247    cargo.env("LZMA_API_STATIC", "1");
248
249    // CFG_RELEASE is needed by rustfmt (and possibly other tools) which
250    // import rustc-ap-rustc_attr which requires this to be set for the
251    // `#[cfg(version(...))]` attribute.
252    cargo.env("CFG_RELEASE", builder.rust_release());
253    cargo.env("CFG_RELEASE_CHANNEL", &builder.config.channel);
254    cargo.env("CFG_VERSION", builder.rust_version());
255    cargo.env("CFG_RELEASE_NUM", &builder.version);
256    cargo.env("DOC_RUST_LANG_ORG_CHANNEL", builder.doc_rust_lang_org_channel());
257
258    if let Some(ref ver_date) = builder.rust_info().commit_date() {
259        cargo.env("CFG_VER_DATE", ver_date);
260    }
261
262    if let Some(ref ver_hash) = builder.rust_info().sha() {
263        cargo.env("CFG_VER_HASH", ver_hash);
264    }
265
266    if let Some(description) = &builder.config.description {
267        cargo.env("CFG_VER_DESCRIPTION", description);
268    }
269
270    let info = GitInfo::new(builder.config.omit_git_hash, &dir);
271    if let Some(sha) = info.sha() {
272        cargo.env("CFG_COMMIT_HASH", sha);
273    }
274
275    if let Some(sha_short) = info.sha_short() {
276        cargo.env("CFG_SHORT_COMMIT_HASH", sha_short);
277    }
278
279    if let Some(date) = info.commit_date() {
280        cargo.env("CFG_COMMIT_DATE", date);
281    }
282
283    if !features.is_empty() {
284        cargo.arg("--features").arg(features.join(", "));
285    }
286
287    // Enable internal lints for clippy and rustdoc
288    // NOTE: this doesn't enable lints for any other tools unless they explicitly add `#![warn(rustc::internal)]`
289    // See https://github.com/rust-lang/rust/pull/80573#issuecomment-754010776
290    //
291    // NOTE: We unconditionally set this here to avoid recompiling tools between `x check $tool`
292    // and `x test $tool` executions.
293    // See https://github.com/rust-lang/rust/issues/116538
294    cargo.rustflag("-Zunstable-options");
295
296    // NOTE: The root cause of needing `-Zon-broken-pipe=kill` in the first place is because `rustc`
297    // and `rustdoc` doesn't gracefully handle I/O errors due to usages of raw std `println!` macros
298    // which panics upon encountering broken pipes. `-Zon-broken-pipe=kill` just papers over that
299    // and stops rustc/rustdoc ICEing on e.g. `rustc --print=sysroot | false`.
300    //
301    // cargo explicitly does not want the `-Zon-broken-pipe=kill` paper because it does actually use
302    // variants of `println!` that handles I/O errors gracefully. It's also a breaking change for a
303    // spawn process not written in Rust, especially if the language default handler is not
304    // `SIG_IGN`. Thankfully cargo tests will break if we do set the flag.
305    //
306    // For the cargo discussion, see
307    // <https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Applying.20.60-Zon-broken-pipe.3Dkill.60.20flags.20in.20bootstrap.3F>.
308    //
309    // For the rustc discussion, see
310    // <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Internal.20lint.20for.20raw.20.60print!.60.20and.20.60println!.60.3F>
311    // for proper solutions.
312    if !path.ends_with("cargo") {
313        // Use an untracked env var `FORCE_ON_BROKEN_PIPE_KILL` here instead of `RUSTFLAGS`.
314        // `RUSTFLAGS` is tracked by cargo. Conditionally omitting `-Zon-broken-pipe=kill` from
315        // `RUSTFLAGS` causes unnecessary tool rebuilds due to cache invalidation from building e.g.
316        // cargo *without* `-Zon-broken-pipe=kill` but then rustdoc *with* `-Zon-broken-pipe=kill`.
317        cargo.env("FORCE_ON_BROKEN_PIPE_KILL", "-Zon-broken-pipe=kill");
318    }
319
320    cargo
321}
322
323/// Handle stage-off logic for `ToolRustc` tools when necessary.
324pub(crate) fn get_tool_rustc_compiler(
325    builder: &Builder<'_>,
326    target_compiler: Compiler,
327) -> Compiler {
328    if target_compiler.is_forced_compiler() {
329        return target_compiler;
330    }
331
332    if builder.download_rustc() && target_compiler.stage > 0 {
333        // We already have the stage N compiler, we don't need to cut the stage.
334        return builder.compiler(target_compiler.stage, builder.config.build);
335    }
336
337    // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
338    // we'd have stageN/bin/rustc and stageN/bin/$rustc_tool be effectively different stage
339    // compilers, which isn't what we want. Rustc tools should be linked in the same way as the
340    // compiler it's paired with, so it must be built with the previous stage compiler.
341    builder.compiler(target_compiler.stage.saturating_sub(1), builder.config.build)
342}
343
344/// Links a built tool binary with the given `name` from the build directory to the
345/// tools directory.
346fn copy_link_tool_bin(
347    builder: &Builder<'_>,
348    compiler: Compiler,
349    target: TargetSelection,
350    mode: Mode,
351    name: &str,
352) -> PathBuf {
353    let cargo_out = builder.cargo_out(compiler, mode, target).join(exe(name, target));
354    let bin = builder.tools_dir(compiler).join(exe(name, target));
355    builder.copy_link(&cargo_out, &bin, FileType::Executable);
356    bin
357}
358
359macro_rules! bootstrap_tool {
360    ($(
361        $name:ident, $path:expr, $tool_name:expr
362        $(,is_external_tool = $external:expr)*
363        $(,is_unstable_tool = $unstable:expr)*
364        $(,allow_features = $allow_features:expr)?
365        $(,submodules = $submodules:expr)?
366        $(,artifact_kind = $artifact_kind:expr)?
367        ;
368    )+) => {
369        #[derive(PartialEq, Eq, Clone)]
370        #[allow(dead_code)]
371        pub enum Tool {
372            $(
373                $name,
374            )+
375        }
376
377        impl<'a> Builder<'a> {
378            pub fn tool_exe(&self, tool: Tool) -> PathBuf {
379                match tool {
380                    $(Tool::$name =>
381                        self.ensure($name {
382                            compiler: self.compiler(0, self.config.build),
383                            target: self.config.build,
384                        }).tool_path,
385                    )+
386                }
387            }
388        }
389
390        $(
391            #[derive(Debug, Clone, Hash, PartialEq, Eq)]
392        pub struct $name {
393            pub compiler: Compiler,
394            pub target: TargetSelection,
395        }
396
397        impl Step for $name {
398            type Output = ToolBuildResult;
399
400            fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
401                run.path($path)
402            }
403
404            fn make_run(run: RunConfig<'_>) {
405                run.builder.ensure($name {
406                    // snapshot compiler
407                    compiler: run.builder.compiler(0, run.builder.config.build),
408                    target: run.target,
409                });
410            }
411
412            #[cfg_attr(
413                feature = "tracing",
414                instrument(
415                    level = "debug",
416                    name = $tool_name,
417                    skip_all,
418                ),
419            )]
420            fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
421                $(
422                    for submodule in $submodules {
423                        builder.require_submodule(submodule, None);
424                    }
425                )*
426
427                let is_unstable = false $(|| $unstable)*;
428                let compiletest_wants_stage0 = $tool_name == "compiletest" && builder.config.compiletest_use_stage0_libtest;
429
430                builder.ensure(ToolBuild {
431                    compiler: self.compiler,
432                    target: self.target,
433                    tool: $tool_name,
434                    mode: if is_unstable && !compiletest_wants_stage0 {
435                        // use in-tree libraries for unstable features
436                        Mode::ToolStd
437                    } else {
438                        Mode::ToolBootstrap
439                    },
440                    path: $path,
441                    source_type: if false $(|| $external)* {
442                        SourceType::Submodule
443                    } else {
444                        SourceType::InTree
445                    },
446                    extra_features: vec![],
447                    allow_features: {
448                        let mut _value = "";
449                        $( _value = $allow_features; )?
450                        _value
451                    },
452                    cargo_args: vec![],
453                    artifact_kind: if false $(|| $artifact_kind == ToolArtifactKind::Library)* {
454                        ToolArtifactKind::Library
455                    } else {
456                        ToolArtifactKind::Binary
457                    }
458                })
459            }
460        }
461        )+
462    }
463}
464
465pub(crate) const COMPILETEST_ALLOW_FEATURES: &str = "internal_output_capture";
466
467bootstrap_tool!(
468    // This is marked as an external tool because it includes dependencies
469    // from submodules. Trying to keep the lints in sync between all the repos
470    // is a bit of a pain. Unfortunately it means the rustbook source itself
471    // doesn't deny warnings, but it is a relatively small piece of code.
472    Rustbook, "src/tools/rustbook", "rustbook", is_external_tool = true, submodules = SUBMODULES_FOR_RUSTBOOK;
473    UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen";
474    Tidy, "src/tools/tidy", "tidy";
475    Linkchecker, "src/tools/linkchecker", "linkchecker";
476    CargoTest, "src/tools/cargotest", "cargotest";
477    Compiletest, "src/tools/compiletest", "compiletest", is_unstable_tool = true, allow_features = COMPILETEST_ALLOW_FEATURES;
478    BuildManifest, "src/tools/build-manifest", "build-manifest";
479    RemoteTestClient, "src/tools/remote-test-client", "remote-test-client";
480    RustInstaller, "src/tools/rust-installer", "rust-installer";
481    RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
482    LintDocs, "src/tools/lint-docs", "lint-docs";
483    JsonDocCk, "src/tools/jsondocck", "jsondocck";
484    JsonDocLint, "src/tools/jsondoclint", "jsondoclint";
485    HtmlChecker, "src/tools/html-checker", "html-checker";
486    BumpStage0, "src/tools/bump-stage0", "bump-stage0";
487    ReplaceVersionPlaceholder, "src/tools/replace-version-placeholder", "replace-version-placeholder";
488    CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
489    GenerateCopyright, "src/tools/generate-copyright", "generate-copyright";
490    SuggestTests, "src/tools/suggest-tests", "suggest-tests";
491    GenerateWindowsSys, "src/tools/generate-windows-sys", "generate-windows-sys";
492    // rustdoc-gui-test has a crate dependency on compiletest, so it needs the same unstable features.
493    RustdocGUITest, "src/tools/rustdoc-gui-test", "rustdoc-gui-test", is_unstable_tool = true, allow_features = COMPILETEST_ALLOW_FEATURES;
494    CoverageDump, "src/tools/coverage-dump", "coverage-dump";
495    WasmComponentLd, "src/tools/wasm-component-ld", "wasm-component-ld", is_unstable_tool = true, allow_features = "min_specialization";
496    UnicodeTableGenerator, "src/tools/unicode-table-generator", "unicode-table-generator";
497    FeaturesStatusDump, "src/tools/features-status-dump", "features-status-dump";
498    OptimizedDist, "src/tools/opt-dist", "opt-dist", submodules = &["src/tools/rustc-perf"];
499    RunMakeSupport, "src/tools/run-make-support", "run_make_support", artifact_kind = ToolArtifactKind::Library;
500);
501
502/// These are the submodules that are required for rustbook to work due to
503/// depending on mdbook plugins.
504pub static SUBMODULES_FOR_RUSTBOOK: &[&str] = &["src/doc/book", "src/doc/reference"];
505
506/// The [rustc-perf](https://github.com/rust-lang/rustc-perf) benchmark suite, which is added
507/// as a submodule at `src/tools/rustc-perf`.
508#[derive(Debug, Clone, Hash, PartialEq, Eq)]
509pub struct RustcPerf {
510    pub compiler: Compiler,
511    pub target: TargetSelection,
512}
513
514impl Step for RustcPerf {
515    /// Path to the built `collector` binary.
516    type Output = ToolBuildResult;
517
518    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
519        run.path("src/tools/rustc-perf")
520    }
521
522    fn make_run(run: RunConfig<'_>) {
523        run.builder.ensure(RustcPerf {
524            compiler: run.builder.compiler(0, run.builder.config.build),
525            target: run.target,
526        });
527    }
528
529    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
530        // We need to ensure the rustc-perf submodule is initialized.
531        builder.require_submodule("src/tools/rustc-perf", None);
532
533        let tool = ToolBuild {
534            compiler: self.compiler,
535            target: self.target,
536            tool: "collector",
537            mode: Mode::ToolBootstrap,
538            path: "src/tools/rustc-perf",
539            source_type: SourceType::Submodule,
540            extra_features: Vec::new(),
541            allow_features: "",
542            // Only build the collector package, which is used for benchmarking through
543            // a CLI.
544            cargo_args: vec!["-p".to_string(), "collector".to_string()],
545            artifact_kind: ToolArtifactKind::Binary,
546        };
547        let res = builder.ensure(tool.clone());
548        // We also need to symlink the `rustc-fake` binary to the corresponding directory,
549        // because `collector` expects it in the same directory.
550        copy_link_tool_bin(builder, tool.compiler, tool.target, tool.mode, "rustc-fake");
551
552        res
553    }
554}
555
556#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
557pub struct ErrorIndex {
558    pub compiler: Compiler,
559}
560
561impl ErrorIndex {
562    pub fn command(builder: &Builder<'_>) -> BootstrapCommand {
563        // Error-index-generator links with the rustdoc library, so we need to add `rustc_lib_paths`
564        // for rustc_private and libLLVM.so, and `sysroot_lib` for libstd, etc.
565        let host = builder.config.build;
566        let compiler = builder.compiler_for(builder.top_stage, host, host);
567        let mut cmd = command(builder.ensure(ErrorIndex { compiler }).tool_path);
568        let mut dylib_paths = builder.rustc_lib_paths(compiler);
569        dylib_paths.push(PathBuf::from(&builder.sysroot_target_libdir(compiler, compiler.host)));
570        add_dylib_path(dylib_paths, &mut cmd);
571        cmd
572    }
573}
574
575impl Step for ErrorIndex {
576    type Output = ToolBuildResult;
577
578    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
579        run.path("src/tools/error_index_generator")
580    }
581
582    fn make_run(run: RunConfig<'_>) {
583        // NOTE: This `make_run` isn't used in normal situations, only if you
584        // manually build the tool with `x.py build
585        // src/tools/error-index-generator` which almost nobody does.
586        // Normally, `x.py test` or `x.py doc` will use the
587        // `ErrorIndex::command` function instead.
588        let compiler = run.builder.compiler(run.builder.top_stage, run.builder.config.build);
589        run.builder.ensure(ErrorIndex { compiler });
590    }
591
592    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
593        builder.ensure(ToolBuild {
594            compiler: self.compiler,
595            target: self.compiler.host,
596            tool: "error_index_generator",
597            mode: Mode::ToolRustc,
598            path: "src/tools/error_index_generator",
599            source_type: SourceType::InTree,
600            extra_features: Vec::new(),
601            allow_features: "",
602            cargo_args: Vec::new(),
603            artifact_kind: ToolArtifactKind::Binary,
604        })
605    }
606}
607
608#[derive(Debug, Clone, Hash, PartialEq, Eq)]
609pub struct RemoteTestServer {
610    pub compiler: Compiler,
611    pub target: TargetSelection,
612}
613
614impl Step for RemoteTestServer {
615    type Output = ToolBuildResult;
616
617    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
618        run.path("src/tools/remote-test-server")
619    }
620
621    fn make_run(run: RunConfig<'_>) {
622        run.builder.ensure(RemoteTestServer {
623            compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
624            target: run.target,
625        });
626    }
627
628    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
629        builder.ensure(ToolBuild {
630            compiler: self.compiler,
631            target: self.target,
632            tool: "remote-test-server",
633            mode: Mode::ToolStd,
634            path: "src/tools/remote-test-server",
635            source_type: SourceType::InTree,
636            extra_features: Vec::new(),
637            allow_features: "",
638            cargo_args: Vec::new(),
639            artifact_kind: ToolArtifactKind::Binary,
640        })
641    }
642}
643
644#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
645pub struct Rustdoc {
646    /// This should only ever be 0 or 2.
647    /// We sometimes want to reference the "bootstrap" rustdoc, which is why this option is here.
648    pub compiler: Compiler,
649}
650
651impl Step for Rustdoc {
652    type Output = ToolBuildResult;
653    const DEFAULT: bool = true;
654    const ONLY_HOSTS: bool = true;
655
656    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
657        run.path("src/tools/rustdoc").path("src/librustdoc")
658    }
659
660    fn make_run(run: RunConfig<'_>) {
661        run.builder
662            .ensure(Rustdoc { compiler: run.builder.compiler(run.builder.top_stage, run.target) });
663    }
664
665    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
666        let target_compiler = self.compiler;
667        let target = target_compiler.host;
668
669        if target_compiler.stage == 0 {
670            if !target_compiler.is_snapshot(builder) {
671                panic!("rustdoc in stage 0 must be snapshot rustdoc");
672            }
673
674            return ToolBuildResult {
675                tool_path: builder.initial_rustdoc.clone(),
676                build_compiler: target_compiler,
677                target_compiler,
678            };
679        }
680
681        let bin_rustdoc = || {
682            let sysroot = builder.sysroot(target_compiler);
683            let bindir = sysroot.join("bin");
684            t!(fs::create_dir_all(&bindir));
685            let bin_rustdoc = bindir.join(exe("rustdoc", target_compiler.host));
686            let _ = fs::remove_file(&bin_rustdoc);
687            bin_rustdoc
688        };
689
690        // If CI rustc is enabled and we haven't modified the rustdoc sources,
691        // use the precompiled rustdoc from CI rustc's sysroot to speed up bootstrapping.
692        if builder.download_rustc()
693            && target_compiler.stage > 0
694            && builder.rust_info().is_managed_git_subrepository()
695        {
696            let files_to_track = &["src/librustdoc", "src/tools/rustdoc"];
697
698            // Check if unchanged
699            if !builder.config.has_changes_from_upstream(files_to_track) {
700                let precompiled_rustdoc = builder
701                    .config
702                    .ci_rustc_dir()
703                    .join("bin")
704                    .join(exe("rustdoc", target_compiler.host));
705
706                let bin_rustdoc = bin_rustdoc();
707                builder.copy_link(&precompiled_rustdoc, &bin_rustdoc, FileType::Executable);
708
709                return ToolBuildResult {
710                    tool_path: bin_rustdoc,
711                    build_compiler: target_compiler,
712                    target_compiler,
713                };
714            }
715        }
716
717        // The presence of `target_compiler` ensures that the necessary libraries (codegen backends,
718        // compiler libraries, ...) are built. Rustdoc does not require the presence of any
719        // libraries within sysroot_libdir (i.e., rustlib), though doctests may want it (since
720        // they'll be linked to those libraries). As such, don't explicitly `ensure` any additional
721        // libraries here. The intuition here is that If we've built a compiler, we should be able
722        // to build rustdoc.
723        //
724        let mut extra_features = Vec::new();
725        if builder.config.jemalloc(target) {
726            extra_features.push("jemalloc".to_string());
727        }
728
729        let ToolBuildResult { tool_path, build_compiler, target_compiler } =
730            builder.ensure(ToolBuild {
731                compiler: target_compiler,
732                target,
733                // Cargo adds a number of paths to the dylib search path on windows, which results in
734                // the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
735                // rustdoc a different name.
736                tool: "rustdoc_tool_binary",
737                mode: Mode::ToolRustc,
738                path: "src/tools/rustdoc",
739                source_type: SourceType::InTree,
740                extra_features,
741                allow_features: "",
742                cargo_args: Vec::new(),
743                artifact_kind: ToolArtifactKind::Binary,
744            });
745
746        // don't create a stage0-sysroot/bin directory.
747        if target_compiler.stage > 0 {
748            if builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None {
749                // Due to LTO a lot of debug info from C++ dependencies such as jemalloc can make it into
750                // our final binaries
751                compile::strip_debug(builder, target, &tool_path);
752            }
753            let bin_rustdoc = bin_rustdoc();
754            builder.copy_link(&tool_path, &bin_rustdoc, FileType::Executable);
755            ToolBuildResult { tool_path: bin_rustdoc, build_compiler, target_compiler }
756        } else {
757            ToolBuildResult { tool_path, build_compiler, target_compiler }
758        }
759    }
760}
761
762#[derive(Debug, Clone, Hash, PartialEq, Eq)]
763pub struct Cargo {
764    pub compiler: Compiler,
765    pub target: TargetSelection,
766}
767
768impl Step for Cargo {
769    type Output = ToolBuildResult;
770    const DEFAULT: bool = true;
771    const ONLY_HOSTS: bool = true;
772
773    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
774        let builder = run.builder;
775        run.path("src/tools/cargo").default_condition(builder.tool_enabled("cargo"))
776    }
777
778    fn make_run(run: RunConfig<'_>) {
779        run.builder.ensure(Cargo {
780            compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
781            target: run.target,
782        });
783    }
784
785    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
786        builder.build.require_submodule("src/tools/cargo", None);
787
788        builder.ensure(ToolBuild {
789            compiler: self.compiler,
790            target: self.target,
791            tool: "cargo",
792            mode: Mode::ToolRustc,
793            path: "src/tools/cargo",
794            source_type: SourceType::Submodule,
795            extra_features: Vec::new(),
796            allow_features: "",
797            cargo_args: Vec::new(),
798            artifact_kind: ToolArtifactKind::Binary,
799        })
800    }
801}
802
803#[derive(Debug, Clone, Hash, PartialEq, Eq)]
804pub struct LldWrapper {
805    pub build_compiler: Compiler,
806    pub target_compiler: Compiler,
807}
808
809impl Step for LldWrapper {
810    type Output = ToolBuildResult;
811
812    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
813        run.never()
814    }
815
816    #[cfg_attr(
817        feature = "tracing",
818        instrument(
819            level = "debug",
820            name = "LldWrapper::run",
821            skip_all,
822            fields(build_compiler = ?self.build_compiler, target_compiler = ?self.target_compiler),
823        ),
824    )]
825    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
826        if builder.config.dry_run() {
827            return ToolBuildResult {
828                tool_path: Default::default(),
829                build_compiler: self.build_compiler,
830                target_compiler: self.target_compiler,
831            };
832        }
833
834        let target = self.target_compiler.host;
835
836        let tool_result = builder.ensure(ToolBuild {
837            compiler: self.build_compiler,
838            target,
839            tool: "lld-wrapper",
840            mode: Mode::ToolStd,
841            path: "src/tools/lld-wrapper",
842            source_type: SourceType::InTree,
843            extra_features: Vec::new(),
844            allow_features: "",
845            cargo_args: Vec::new(),
846            artifact_kind: ToolArtifactKind::Binary,
847        });
848
849        let libdir_bin = builder.sysroot_target_bindir(self.target_compiler, target);
850        t!(fs::create_dir_all(&libdir_bin));
851
852        let lld_install = builder.ensure(llvm::Lld { target });
853        let src_exe = exe("lld", target);
854        let dst_exe = exe("rust-lld", target);
855
856        builder.copy_link(
857            &lld_install.join("bin").join(src_exe),
858            &libdir_bin.join(dst_exe),
859            FileType::Executable,
860        );
861        let self_contained_lld_dir = libdir_bin.join("gcc-ld");
862        t!(fs::create_dir_all(&self_contained_lld_dir));
863
864        for name in crate::LLD_FILE_NAMES {
865            builder.copy_link(
866                &tool_result.tool_path,
867                &self_contained_lld_dir.join(exe(name, target)),
868                FileType::Executable,
869            );
870        }
871
872        tool_result
873    }
874}
875
876#[derive(Debug, Clone, Hash, PartialEq, Eq)]
877pub struct RustAnalyzer {
878    pub compiler: Compiler,
879    pub target: TargetSelection,
880}
881
882impl RustAnalyzer {
883    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";
884}
885
886impl Step for RustAnalyzer {
887    type Output = ToolBuildResult;
888    const DEFAULT: bool = true;
889    const ONLY_HOSTS: bool = true;
890
891    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
892        let builder = run.builder;
893        run.path("src/tools/rust-analyzer").default_condition(builder.tool_enabled("rust-analyzer"))
894    }
895
896    fn make_run(run: RunConfig<'_>) {
897        run.builder.ensure(RustAnalyzer {
898            compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
899            target: run.target,
900        });
901    }
902
903    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
904        builder.ensure(ToolBuild {
905            compiler: self.compiler,
906            target: self.target,
907            tool: "rust-analyzer",
908            mode: Mode::ToolRustc,
909            path: "src/tools/rust-analyzer",
910            extra_features: vec!["in-rust-tree".to_owned()],
911            source_type: SourceType::InTree,
912            allow_features: RustAnalyzer::ALLOW_FEATURES,
913            cargo_args: Vec::new(),
914            artifact_kind: ToolArtifactKind::Binary,
915        })
916    }
917}
918
919#[derive(Debug, Clone, Hash, PartialEq, Eq)]
920pub struct RustAnalyzerProcMacroSrv {
921    pub compiler: Compiler,
922    pub target: TargetSelection,
923}
924
925impl Step for RustAnalyzerProcMacroSrv {
926    type Output = Option<ToolBuildResult>;
927    const DEFAULT: bool = true;
928    const ONLY_HOSTS: bool = true;
929
930    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
931        let builder = run.builder;
932        // Allow building `rust-analyzer-proc-macro-srv` both as part of the `rust-analyzer` and as a stand-alone tool.
933        run.path("src/tools/rust-analyzer")
934            .path("src/tools/rust-analyzer/crates/proc-macro-srv-cli")
935            .default_condition(
936                builder.tool_enabled("rust-analyzer")
937                    || builder.tool_enabled("rust-analyzer-proc-macro-srv"),
938            )
939    }
940
941    fn make_run(run: RunConfig<'_>) {
942        run.builder.ensure(RustAnalyzerProcMacroSrv {
943            compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
944            target: run.target,
945        });
946    }
947
948    fn run(self, builder: &Builder<'_>) -> Option<ToolBuildResult> {
949        let tool_result = builder.ensure(ToolBuild {
950            compiler: self.compiler,
951            target: self.target,
952            tool: "rust-analyzer-proc-macro-srv",
953            mode: Mode::ToolRustc,
954            path: "src/tools/rust-analyzer/crates/proc-macro-srv-cli",
955            extra_features: vec!["in-rust-tree".to_owned()],
956            source_type: SourceType::InTree,
957            allow_features: RustAnalyzer::ALLOW_FEATURES,
958            cargo_args: Vec::new(),
959            artifact_kind: ToolArtifactKind::Binary,
960        });
961
962        // Copy `rust-analyzer-proc-macro-srv` to `<sysroot>/libexec/`
963        // so that r-a can use it.
964        let libexec_path = builder.sysroot(self.compiler).join("libexec");
965        t!(fs::create_dir_all(&libexec_path));
966        builder.copy_link(
967            &tool_result.tool_path,
968            &libexec_path.join("rust-analyzer-proc-macro-srv"),
969            FileType::Executable,
970        );
971
972        Some(tool_result)
973    }
974}
975
976#[derive(Debug, Clone, Hash, PartialEq, Eq)]
977pub struct LlvmBitcodeLinker {
978    pub compiler: Compiler,
979    pub target: TargetSelection,
980    pub extra_features: Vec<String>,
981}
982
983impl Step for LlvmBitcodeLinker {
984    type Output = ToolBuildResult;
985    const DEFAULT: bool = true;
986    const ONLY_HOSTS: bool = true;
987
988    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
989        let builder = run.builder;
990        run.path("src/tools/llvm-bitcode-linker")
991            .default_condition(builder.tool_enabled("llvm-bitcode-linker"))
992    }
993
994    fn make_run(run: RunConfig<'_>) {
995        run.builder.ensure(LlvmBitcodeLinker {
996            compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
997            extra_features: Vec::new(),
998            target: run.target,
999        });
1000    }
1001
1002    #[cfg_attr(
1003        feature = "tracing",
1004        instrument(level = "debug", name = "LlvmBitcodeLinker::run", skip_all)
1005    )]
1006    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1007        let tool_result = builder.ensure(ToolBuild {
1008            compiler: self.compiler,
1009            target: self.target,
1010            tool: "llvm-bitcode-linker",
1011            mode: Mode::ToolRustc,
1012            path: "src/tools/llvm-bitcode-linker",
1013            source_type: SourceType::InTree,
1014            extra_features: self.extra_features,
1015            allow_features: "",
1016            cargo_args: Vec::new(),
1017            artifact_kind: ToolArtifactKind::Binary,
1018        });
1019
1020        if tool_result.target_compiler.stage > 0 {
1021            let bindir_self_contained = builder
1022                .sysroot(tool_result.target_compiler)
1023                .join(format!("lib/rustlib/{}/bin/self-contained", self.target.triple));
1024            t!(fs::create_dir_all(&bindir_self_contained));
1025            let bin_destination = bindir_self_contained
1026                .join(exe("llvm-bitcode-linker", tool_result.target_compiler.host));
1027            builder.copy_link(&tool_result.tool_path, &bin_destination, FileType::Executable);
1028            ToolBuildResult {
1029                tool_path: bin_destination,
1030                build_compiler: tool_result.build_compiler,
1031                target_compiler: tool_result.target_compiler,
1032            }
1033        } else {
1034            tool_result
1035        }
1036    }
1037}
1038
1039#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1040pub struct LibcxxVersionTool {
1041    pub target: TargetSelection,
1042}
1043
1044#[expect(dead_code)]
1045#[derive(Debug, Clone)]
1046pub enum LibcxxVersion {
1047    Gnu(usize),
1048    Llvm(usize),
1049}
1050
1051impl Step for LibcxxVersionTool {
1052    type Output = LibcxxVersion;
1053    const DEFAULT: bool = false;
1054    const ONLY_HOSTS: bool = true;
1055
1056    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1057        run.never()
1058    }
1059
1060    fn run(self, builder: &Builder<'_>) -> LibcxxVersion {
1061        let out_dir = builder.out.join(self.target.to_string()).join("libcxx-version");
1062        let executable = out_dir.join(exe("libcxx-version", self.target));
1063
1064        // This is a sanity-check specific step, which means it is frequently called (when using
1065        // CI LLVM), and compiling `src/tools/libcxx-version/main.cpp` at the beginning of the bootstrap
1066        // invocation adds a fair amount of overhead to the process (see https://github.com/rust-lang/rust/issues/126423).
1067        // Therefore, we want to avoid recompiling this file unnecessarily.
1068        if !executable.exists() {
1069            if !out_dir.exists() {
1070                t!(fs::create_dir_all(&out_dir));
1071            }
1072
1073            let compiler = builder.cxx(self.target).unwrap();
1074            let mut cmd = command(compiler);
1075
1076            cmd.arg("-o")
1077                .arg(&executable)
1078                .arg(builder.src.join("src/tools/libcxx-version/main.cpp"));
1079
1080            cmd.run(builder);
1081
1082            if !executable.exists() {
1083                panic!("Something went wrong. {} is not present", executable.display());
1084            }
1085        }
1086
1087        let version_output = command(executable).run_capture_stdout(builder).stdout();
1088
1089        let version_str = version_output.split_once("version:").unwrap().1;
1090        let version = version_str.trim().parse::<usize>().unwrap();
1091
1092        if version_output.starts_with("libstdc++") {
1093            LibcxxVersion::Gnu(version)
1094        } else if version_output.starts_with("libc++") {
1095            LibcxxVersion::Llvm(version)
1096        } else {
1097            panic!("Coudln't recognize the standard library version.");
1098        }
1099    }
1100}
1101
1102macro_rules! tool_extended {
1103    (
1104        $name:ident {
1105            path: $path:expr,
1106            tool_name: $tool_name:expr,
1107            stable: $stable:expr
1108            $( , add_bins_to_sysroot: $add_bins_to_sysroot:expr )?
1109            $( , )?
1110        }
1111    ) => {
1112        #[derive(Debug, Clone, Hash, PartialEq, Eq)]
1113        pub struct $name {
1114            pub compiler: Compiler,
1115            pub target: TargetSelection,
1116        }
1117
1118        impl Step for $name {
1119            type Output = ToolBuildResult;
1120            const DEFAULT: bool = true; // Overridden by `should_run_tool_build_step`
1121            const ONLY_HOSTS: bool = true;
1122
1123            fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1124                should_run_tool_build_step(
1125                    run,
1126                    $tool_name,
1127                    $path,
1128                    $stable,
1129                )
1130            }
1131
1132            fn make_run(run: RunConfig<'_>) {
1133                run.builder.ensure($name {
1134                    compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
1135                    target: run.target,
1136                });
1137            }
1138
1139            fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1140                let Self { compiler, target } = self;
1141                run_tool_build_step(
1142                    builder,
1143                    compiler,
1144                    target,
1145                    $tool_name,
1146                    $path,
1147                    None $( .or(Some(&$add_bins_to_sysroot)) )?,
1148                )
1149            }
1150        }
1151    }
1152}
1153
1154fn should_run_tool_build_step<'a>(
1155    run: ShouldRun<'a>,
1156    tool_name: &'static str,
1157    path: &'static str,
1158    stable: bool,
1159) -> ShouldRun<'a> {
1160    let builder = run.builder;
1161    run.path(path).default_condition(
1162        builder.config.extended
1163            && builder.config.tools.as_ref().map_or(
1164                // By default, on nightly/dev enable all tools, else only
1165                // build stable tools.
1166                stable || builder.build.unstable_features(),
1167                // If `tools` is set, search list for this tool.
1168                |tools| {
1169                    tools.iter().any(|tool| match tool.as_ref() {
1170                        "clippy" => tool_name == "clippy-driver",
1171                        x => tool_name == x,
1172                    })
1173                },
1174            ),
1175    )
1176}
1177
1178fn run_tool_build_step(
1179    builder: &Builder<'_>,
1180    compiler: Compiler,
1181    target: TargetSelection,
1182    tool_name: &'static str,
1183    path: &'static str,
1184    add_bins_to_sysroot: Option<&[&str]>,
1185) -> ToolBuildResult {
1186    let ToolBuildResult { tool_path, build_compiler, target_compiler } =
1187        builder.ensure(ToolBuild {
1188            compiler,
1189            target,
1190            tool: tool_name,
1191            mode: Mode::ToolRustc,
1192            path,
1193            extra_features: vec![],
1194            source_type: SourceType::InTree,
1195            allow_features: "",
1196            cargo_args: vec![],
1197            artifact_kind: ToolArtifactKind::Binary,
1198        });
1199
1200    if let Some(add_bins_to_sysroot) = add_bins_to_sysroot
1201        && !add_bins_to_sysroot.is_empty()
1202        && target_compiler.stage > 0
1203    {
1204        let bindir = builder.sysroot(target_compiler).join("bin");
1205        t!(fs::create_dir_all(&bindir));
1206
1207        for add_bin in add_bins_to_sysroot {
1208            let bin_destination = bindir.join(exe(add_bin, target_compiler.host));
1209            builder.copy_link(&tool_path, &bin_destination, FileType::Executable);
1210        }
1211
1212        // Return a path into the bin dir.
1213        let path = bindir.join(exe(tool_name, target_compiler.host));
1214        ToolBuildResult { tool_path: path, build_compiler, target_compiler }
1215    } else {
1216        ToolBuildResult { tool_path, build_compiler, target_compiler }
1217    }
1218}
1219
1220tool_extended!(Cargofmt {
1221    path: "src/tools/rustfmt",
1222    tool_name: "cargo-fmt",
1223    stable: true,
1224    add_bins_to_sysroot: ["cargo-fmt"]
1225});
1226tool_extended!(CargoClippy {
1227    path: "src/tools/clippy",
1228    tool_name: "cargo-clippy",
1229    stable: true,
1230    add_bins_to_sysroot: ["cargo-clippy"]
1231});
1232tool_extended!(Clippy {
1233    path: "src/tools/clippy",
1234    tool_name: "clippy-driver",
1235    stable: true,
1236    add_bins_to_sysroot: ["clippy-driver"]
1237});
1238tool_extended!(Miri {
1239    path: "src/tools/miri",
1240    tool_name: "miri",
1241    stable: false,
1242    add_bins_to_sysroot: ["miri"]
1243});
1244tool_extended!(CargoMiri {
1245    path: "src/tools/miri/cargo-miri",
1246    tool_name: "cargo-miri",
1247    stable: false,
1248    add_bins_to_sysroot: ["cargo-miri"]
1249});
1250tool_extended!(Rustfmt {
1251    path: "src/tools/rustfmt",
1252    tool_name: "rustfmt",
1253    stable: true,
1254    add_bins_to_sysroot: ["rustfmt"]
1255});
1256
1257#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1258pub struct TestFloatParse {
1259    pub host: TargetSelection,
1260}
1261
1262impl TestFloatParse {
1263    pub const ALLOW_FEATURES: &'static str = "f16,cfg_target_has_reliable_f16_f128";
1264}
1265
1266impl Step for TestFloatParse {
1267    type Output = ToolBuildResult;
1268    const ONLY_HOSTS: bool = true;
1269    const DEFAULT: bool = false;
1270
1271    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1272        run.path("src/etc/test-float-parse")
1273    }
1274
1275    fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1276        let bootstrap_host = builder.config.build;
1277        let compiler = builder.compiler(builder.top_stage, bootstrap_host);
1278
1279        builder.ensure(ToolBuild {
1280            compiler,
1281            target: bootstrap_host,
1282            tool: "test-float-parse",
1283            mode: Mode::ToolStd,
1284            path: "src/etc/test-float-parse",
1285            source_type: SourceType::InTree,
1286            extra_features: Vec::new(),
1287            allow_features: Self::ALLOW_FEATURES,
1288            cargo_args: Vec::new(),
1289            artifact_kind: ToolArtifactKind::Binary,
1290        })
1291    }
1292}
1293
1294impl Builder<'_> {
1295    /// Gets a `BootstrapCommand` which is ready to run `tool` in `stage` built for
1296    /// `host`.
1297    pub fn tool_cmd(&self, tool: Tool) -> BootstrapCommand {
1298        let mut cmd = command(self.tool_exe(tool));
1299        let compiler = self.compiler(0, self.config.build);
1300        let host = &compiler.host;
1301        // Prepares the `cmd` provided to be able to run the `compiler` provided.
1302        //
1303        // Notably this munges the dynamic library lookup path to point to the
1304        // right location to run `compiler`.
1305        let mut lib_paths: Vec<PathBuf> = vec![
1306            self.build.rustc_snapshot_libdir(),
1307            self.cargo_out(compiler, Mode::ToolBootstrap, *host).join("deps"),
1308        ];
1309
1310        // On MSVC a tool may invoke a C compiler (e.g., compiletest in run-make
1311        // mode) and that C compiler may need some extra PATH modification. Do
1312        // so here.
1313        if compiler.host.is_msvc() {
1314            let curpaths = env::var_os("PATH").unwrap_or_default();
1315            let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
1316            for (k, v) in self.cc.borrow()[&compiler.host].env() {
1317                if k != "PATH" {
1318                    continue;
1319                }
1320                for path in env::split_paths(v) {
1321                    if !curpaths.contains(&path) {
1322                        lib_paths.push(path);
1323                    }
1324                }
1325            }
1326        }
1327
1328        add_dylib_path(lib_paths, &mut cmd);
1329
1330        // Provide a RUSTC for this command to use.
1331        cmd.env("RUSTC", &self.initial_rustc);
1332
1333        cmd
1334    }
1335}