bootstrap/core/build_steps/
tool.rs

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