bootstrap/core/build_steps/
tool.rs

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