Skip to main content

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