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