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