Skip to main content

bootstrap/core/build_steps/
test.rs

1//! Build-and-run steps for `./x.py test` test fixtures
2//!
3//! `./x.py test` (aka [`Kind::Test`]) is currently allowed to reach build steps in other modules.
4//! However, this contains ~all test parts we expect people to be able to build and run locally.
5
6// (This file should be split up, but having tidy block all changes is not helpful.)
7// ignore-tidy-file-filelength
8
9use std::collections::HashSet;
10use std::env::split_paths;
11use std::ffi::{OsStr, OsString};
12use std::path::{Path, PathBuf};
13use std::process::Command;
14use std::{env, fs, iter};
15
16use build_helper::git::get_closest_upstream_commit;
17
18use crate::core::backend::CodegenBackendKind;
19use crate::core::build_steps::compile::{ArtifactKeepMode, Std, run_cargo};
20use crate::core::build_steps::doc::{DocumentationFormat, prepare_doc_compiler};
21use crate::core::build_steps::format::InternalRustfmt;
22use crate::core::build_steps::gcc::{Gcc, GccTargetPair, add_cg_gcc_cargo_flags};
23use crate::core::build_steps::llvm::get_llvm_version;
24use crate::core::build_steps::run::{get_completion_paths, get_help_path};
25use crate::core::build_steps::synthetic_targets::MirOptPanicAbortSyntheticTarget;
26use crate::core::build_steps::test::compiletest::CompiletestMode;
27use crate::core::build_steps::test::failed_tests::{RecordFailedTests, SetupFailedTestsFile};
28use crate::core::build_steps::tool::{
29    self, RustcPrivateCompilers, SourceType, TEST_FLOAT_PARSE_ALLOW_FEATURES, Tool,
30    ToolTargetBuildMode, get_tool_target_compiler,
31};
32use crate::core::build_steps::toolstate::ToolState;
33use crate::core::build_steps::{compile, dist, llvm};
34use crate::core::builder::{
35    self, Alias, Builder, CommandLineStep, Kind, RunConfig, ShouldRun, Step, StepMetadata,
36    crate_description,
37};
38use crate::core::compiler::Compiler;
39use crate::core::config::TargetSelection;
40use crate::core::config::flags::{Subcommand, get_completion, top_level_help};
41use crate::core::session::{CLang, Mode};
42use crate::core::{android, debuggers};
43use crate::utils::build_stamp::{self, BuildStamp};
44use crate::utils::exec::{BootstrapCommand, command};
45use crate::utils::helpers::{
46    self, LldThreads, TestFilterCategory, add_dylib_path, add_rustdoc_cargo_linker_args,
47    dylib_path, dylib_path_var, envify, linker_args, linker_flags, t,
48    target_supports_cranelift_backend, up_to_date,
49};
50use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
51mod compiletest;
52pub mod failed_tests;
53
54#[derive(PartialEq, Eq, Copy, Clone, Debug)]
55pub enum TestTarget {
56    /// Run unit, integration and doc tests (default).
57    Default,
58    /// Run unit, integration, doc tests, examples, bins, benchmarks (no doc tests).
59    AllTargets,
60    /// Only run doc tests.
61    DocOnly,
62    /// Only run unit and integration tests.
63    Tests,
64}
65
66impl TestTarget {
67    pub(crate) fn runs_doctests(&self) -> bool {
68        matches!(self, TestTarget::DocOnly | TestTarget::Default)
69    }
70}
71
72/// Runs `cargo test` on various internal tools used by bootstrap.
73#[derive(Debug, Clone, PartialEq, Eq, Hash)]
74pub struct CrateBootstrap {
75    path: PathBuf,
76    host: TargetSelection,
77}
78
79impl CommandLineStep for CrateBootstrap {
80    type Output = ();
81    const IS_HOST: bool = true;
82
83    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
84        // This step is responsible for several different tool paths.
85        //
86        // By default, it will test all of them, but requesting specific tools on the command-line
87        // (e.g. `./x test src/tools/coverage-dump`) will test only the specified tools.
88        run.path("src/tools/jsondoclint")
89            .path("src/tools/replace-version-placeholder")
90            .path("src/tools/coverage-dump")
91            // We want `./x test tidy` to _run_ the tidy tool, not its tests.
92            // So we need a separate alias to test the tidy tool itself.
93            .alias("tidyselftest")
94    }
95
96    fn is_default_step(_builder: &Builder<'_>) -> bool {
97        true
98    }
99
100    fn make_run(run: RunConfig<'_>) {
101        // Create and ensure a separate instance of this step for each path
102        // that was selected on the command-line (or selected by default).
103        for path in run.paths {
104            let path = path.assert_single_path().path.clone();
105            run.builder.ensure(CrateBootstrap { host: run.target, path });
106        }
107    }
108
109    fn run(self, builder: &Builder<'_>) {
110        let bootstrap_host = builder.config.host_target;
111        let compiler = builder.compiler(0, bootstrap_host);
112        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
113        let mut path = self.path.to_str().unwrap();
114
115        // Map alias `tidyselftest` back to the actual crate path of tidy.
116        if path == "tidyselftest" {
117            path = "src/tools/tidy";
118        }
119
120        let cargo = tool::prepare_tool_cargo(
121            builder,
122            compiler,
123            Mode::ToolBootstrap,
124            bootstrap_host,
125            Kind::Test,
126            path,
127            SourceType::InTree,
128            &[],
129        );
130
131        let crate_name = path.rsplit_once('/').unwrap().1;
132        run_cargo_test(cargo, &[], &[], crate_name, bootstrap_host, builder, record_failed_tests);
133    }
134
135    fn metadata(&self) -> Option<StepMetadata> {
136        Some(
137            StepMetadata::test("crate-bootstrap", self.host)
138                .with_metadata(self.path.as_path().to_string_lossy().to_string()),
139        )
140    }
141}
142
143#[derive(Debug, Clone, PartialEq, Eq, Hash)]
144pub struct Linkcheck {
145    host: TargetSelection,
146}
147
148impl CommandLineStep for Linkcheck {
149    type Output = ();
150    const IS_HOST: bool = true;
151
152    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
153        run.path("src/tools/linkchecker")
154    }
155
156    fn is_default_step(builder: &Builder<'_>) -> bool {
157        builder.config.docs
158    }
159
160    fn make_run(run: RunConfig<'_>) {
161        run.builder.ensure(Linkcheck { host: run.target });
162    }
163
164    /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
165    ///
166    /// This tool in `src/tools` will verify the validity of all our links in the
167    /// documentation to ensure we don't have a bunch of dead ones.
168    fn run(self, builder: &Builder<'_>) {
169        let host = self.host;
170        let hosts = &builder.hosts;
171        let targets = &builder.targets;
172
173        // if we have different hosts and targets, some things may be built for
174        // the host (e.g. rustc) and others for the target (e.g. std). The
175        // documentation built for each will contain broken links to
176        // docs built for the other platform (e.g. rustc linking to cargo)
177        if (hosts != targets) && !hosts.is_empty() && !targets.is_empty() {
178            panic!(
179                "Linkcheck currently does not support builds with different hosts and targets.
180You can skip linkcheck with --skip src/tools/linkchecker"
181            );
182        }
183
184        builder.info(&format!("Linkcheck ({host})"));
185
186        // Test the linkchecker itself.
187        let bootstrap_host = builder.config.host_target;
188        let compiler = builder.compiler(0, bootstrap_host);
189        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
190
191        let cargo = tool::prepare_tool_cargo(
192            builder,
193            compiler,
194            Mode::ToolBootstrap,
195            bootstrap_host,
196            Kind::Test,
197            "src/tools/linkchecker",
198            SourceType::InTree,
199            &[],
200        );
201        run_cargo_test(
202            cargo,
203            &[],
204            &[],
205            "linkchecker self tests",
206            bootstrap_host,
207            builder,
208            record_failed_tests,
209        );
210
211        if !builder.test_target.runs_doctests() {
212            return;
213        }
214
215        // Build all the default documentation.
216        builder.run_default_doc_steps();
217
218        // Build the linkchecker before calling `msg`, since GHA doesn't support nested groups.
219        let linkchecker = builder.tool_cmd(Tool::Linkchecker);
220
221        // Run the linkchecker.
222        let _guard = builder.msg_test("Linkcheck", bootstrap_host, 1);
223        let _time = helpers::timeit(builder);
224        linkchecker.delay_failure().arg(builder.out.join(host).join("doc")).run(builder);
225    }
226
227    fn metadata(&self) -> Option<StepMetadata> {
228        Some(StepMetadata::test("link-check", self.host))
229    }
230}
231
232fn check_if_tidy_is_installed(builder: &Builder<'_>) -> bool {
233    command("tidy")
234        .allow_failure()
235        .arg("--version")
236        // Cache the output to avoid running this command more than once (per builder).
237        .cached()
238        .run_capture_stdout(builder)
239        .is_success()
240}
241
242#[derive(Debug, Clone, PartialEq, Eq, Hash)]
243pub struct HtmlCheck {
244    target: TargetSelection,
245}
246
247impl CommandLineStep for HtmlCheck {
248    type Output = ();
249    const IS_HOST: bool = true;
250
251    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
252        run.path("src/tools/html-checker")
253    }
254
255    fn is_default_step(builder: &Builder<'_>) -> bool {
256        check_if_tidy_is_installed(builder)
257    }
258
259    fn make_run(run: RunConfig<'_>) {
260        run.builder.ensure(HtmlCheck { target: run.target });
261    }
262
263    fn run(self, builder: &Builder<'_>) {
264        if !check_if_tidy_is_installed(builder) {
265            eprintln!("not running HTML-check tool because `tidy` is missing");
266            eprintln!(
267                "You need the HTML tidy tool https://www.html-tidy.org/, this tool is *not* part of the rust project and needs to be installed separately, for example via your package manager."
268            );
269            panic!("Cannot run html-check tests");
270        }
271        // Ensure that a few different kinds of documentation are available.
272        builder.run_default_doc_steps();
273        builder.ensure(crate::core::build_steps::doc::Rustc::for_stage(
274            builder,
275            builder.top_stage,
276            self.target,
277        ));
278
279        builder
280            .tool_cmd(Tool::HtmlChecker)
281            .delay_failure()
282            .arg(builder.doc_out(self.target))
283            .run(builder);
284    }
285
286    fn metadata(&self) -> Option<StepMetadata> {
287        Some(StepMetadata::test("html-check", self.target))
288    }
289}
290
291/// Builds cargo and then runs the `src/tools/cargotest` tool, which checks out
292/// some representative crate repositories and runs `cargo test` on them, in
293/// order to test cargo.
294#[derive(Debug, Clone, PartialEq, Eq, Hash)]
295pub struct Cargotest {
296    build_compiler: Compiler,
297    host: TargetSelection,
298}
299
300impl CommandLineStep for Cargotest {
301    type Output = ();
302    const IS_HOST: bool = true;
303
304    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
305        run.path("src/tools/cargotest")
306    }
307
308    fn make_run(run: RunConfig<'_>) {
309        if run.builder.top_stage == 0 {
310            eprintln!(
311                "ERROR: running cargotest with stage 0 is currently unsupported. Use at least stage 1."
312            );
313            helpers::exit_process(1);
314        }
315        // We want to build cargo stage N (where N == top_stage), and rustc stage N,
316        // and test both of these together.
317        // So we need to get a build compiler stage N-1 to build the stage N components.
318        run.builder.ensure(Cargotest {
319            build_compiler: run.builder.compiler(run.builder.top_stage - 1, run.target),
320            host: run.target,
321        });
322    }
323
324    /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler.
325    ///
326    /// This tool in `src/tools` will check out a few Rust projects and run `cargo
327    /// test` to ensure that we don't regress the test suites there.
328    fn run(self, builder: &Builder<'_>) {
329        // cargotest's staging has several pieces:
330        // consider ./x test cargotest --stage=2.
331        //
332        // The test goal is to exercise a (stage 2 cargo, stage 2 rustc) pair through a stage 2
333        // cargotest tool.
334        // To produce the stage 2 cargo and cargotest, we need to do so with the stage 1 rustc and std.
335        // Importantly, the stage 2 rustc being tested (`tested_compiler`) via stage 2 cargotest is
336        // the rustc built by an earlier stage 1 rustc (the build_compiler). These are two different
337        // compilers!
338        let cargo =
339            builder.ensure(tool::Cargo::from_build_compiler(self.build_compiler, self.host));
340        let tested_compiler = builder.compiler(self.build_compiler.stage + 1, self.host);
341        builder.std(tested_compiler, self.host);
342
343        // Note that this is a short, cryptic, and not scoped directory name. This
344        // is currently to minimize the length of path on Windows where we otherwise
345        // quickly run into path name limit constraints.
346        let out_dir = builder.out.join("ct");
347        t!(fs::create_dir_all(&out_dir));
348
349        let _time = helpers::timeit(builder);
350        let mut cmd = builder.tool_cmd(Tool::CargoTest);
351        cmd.arg(&cargo.tool_path)
352            .arg(&out_dir)
353            .args(builder.config.test_args())
354            .env("RUSTC", builder.rustc(tested_compiler))
355            .env("RUSTDOC", builder.rustdoc_for_compiler(tested_compiler));
356        add_rustdoc_cargo_linker_args(&mut cmd, builder, tested_compiler.host, LldThreads::No);
357        cmd.delay_failure().run(builder);
358    }
359
360    fn metadata(&self) -> Option<StepMetadata> {
361        Some(StepMetadata::test("cargotest", self.host).stage(self.build_compiler.stage + 1))
362    }
363}
364
365/// Runs `cargo test` for cargo itself.
366/// We label these tests as "cargo self-tests".
367#[derive(Debug, Clone, PartialEq, Eq, Hash)]
368pub struct Cargo {
369    build_compiler: Compiler,
370    host: TargetSelection,
371}
372
373impl Cargo {
374    const CRATE_PATH: &str = "src/tools/cargo";
375}
376
377impl CommandLineStep for Cargo {
378    type Output = ();
379    const IS_HOST: bool = true;
380
381    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
382        run.path(Self::CRATE_PATH)
383    }
384
385    fn make_run(run: RunConfig<'_>) {
386        run.builder.ensure(Cargo {
387            build_compiler: get_tool_target_compiler(
388                run.builder,
389                ToolTargetBuildMode::Build(run.target),
390            ),
391            host: run.target,
392        });
393    }
394
395    /// Runs `cargo test` for `cargo` packaged with Rust.
396    fn run(self, builder: &Builder<'_>) {
397        // When we do a "stage 1 cargo self-test", it means that we test the stage 1 rustc
398        // using stage 1 cargo. So we actually build cargo using the stage 0 compiler, and then
399        // run its tests against the stage 1 compiler (called `tested_compiler` below).
400        builder.ensure(tool::Cargo::from_build_compiler(self.build_compiler, self.host));
401        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
402
403        let tested_compiler = builder.compiler(self.build_compiler.stage + 1, self.host);
404        builder.std(tested_compiler, self.host);
405        // We also need to build rustdoc for cargo tests
406        // It will be located in the bindir of `tested_compiler`, so we don't need to explicitly
407        // pass its path to Cargo.
408        builder.rustdoc_for_compiler(tested_compiler);
409
410        let cargo = tool::prepare_tool_cargo(
411            builder,
412            self.build_compiler,
413            Mode::ToolTarget,
414            self.host,
415            Kind::Test,
416            Self::CRATE_PATH,
417            SourceType::Submodule,
418            &[],
419        );
420
421        // NOTE: can't use `run_cargo_test` because we need to overwrite `PATH`
422        let mut cargo = prepare_cargo_test(cargo, &[], &[], self.host, builder);
423
424        // Don't run cross-compile tests, we may not have cross-compiled libstd libs
425        // available.
426        cargo.env("CFG_DISABLE_CROSS_TESTS", "1");
427        // Forcibly disable tests using nightly features since any changes to
428        // those features won't be able to land.
429        cargo.env("CARGO_TEST_DISABLE_NIGHTLY", "1");
430
431        // Configure PATH to find the right rustc. NB. we have to use PATH
432        // and not RUSTC because the Cargo test suite has tests that will
433        // fail if rustc is not spelled `rustc`.
434        cargo.env("PATH", bin_path_for_cargo(builder, tested_compiler));
435
436        // The `cargo` command configured above has dylib dir path set to the `build_compiler`'s
437        // libdir. That causes issues in cargo test, because the programs that cargo compiles are
438        // incorrectly picking that libdir, even though they should be picking the
439        // `tested_compiler`'s libdir. We thus have to override the precedence here.
440        let mut existing_dylib_paths = cargo
441            .get_envs()
442            .find(|(k, _)| *k == OsStr::new(dylib_path_var()))
443            .and_then(|(_, v)| v)
444            .map(|value| split_paths(value).collect::<Vec<PathBuf>>())
445            .unwrap_or_default();
446        existing_dylib_paths.insert(0, builder.rustc_libdir(tested_compiler));
447        add_dylib_path(existing_dylib_paths, &mut cargo);
448
449        // Cargo's test suite uses `CARGO_RUSTC_CURRENT_DIR` to determine the path that `file!` is
450        // relative to. Cargo no longer sets this env var, so we have to do that. This has to be the
451        // same value as `-Zroot-dir`.
452        cargo.env("CARGO_RUSTC_CURRENT_DIR", builder.src.display().to_string());
453
454        #[cfg(feature = "build-metrics")]
455        builder.metrics.begin_test_suite(
456            build_helper::metrics::TestSuiteMetadata::CargoPackage {
457                crates: vec!["cargo".into()],
458                target: self.host.triple.to_string(),
459                host: self.host.triple.to_string(),
460                stage: self.build_compiler.stage + 1,
461            },
462            builder,
463        );
464
465        let _time = helpers::timeit(builder);
466        add_flags_and_try_run_tests(builder, &mut cargo, record_failed_tests);
467    }
468
469    fn metadata(&self) -> Option<StepMetadata> {
470        Some(StepMetadata::test("cargo", self.host).built_by(self.build_compiler))
471    }
472}
473
474#[derive(Debug, Clone, PartialEq, Eq, Hash)]
475pub struct RustAnalyzer {
476    compilers: RustcPrivateCompilers,
477}
478
479impl CommandLineStep for RustAnalyzer {
480    type Output = ();
481    const IS_HOST: bool = true;
482
483    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
484        run.path("src/tools/rust-analyzer")
485    }
486
487    fn is_default_step(_builder: &Builder<'_>) -> bool {
488        true
489    }
490
491    fn make_run(run: RunConfig<'_>) {
492        run.builder.ensure(Self {
493            compilers: RustcPrivateCompilers::new(
494                run.builder,
495                run.builder.top_stage,
496                run.builder.host_target,
497            ),
498        });
499    }
500
501    /// Runs `cargo test` for rust-analyzer
502    fn run(self, builder: &Builder<'_>) {
503        let build_compiler = self.compilers.build_compiler();
504        let target = self.compilers.target();
505        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
506
507        // NOTE: rust-analyzer repo currently (as of 2025-12-11) does not run tests against 32-bit
508        // targets, so we also don't run them in rust-lang/rust CI (because that will just mean that
509        // subtree syncs will keep getting 32-bit-specific failures that are not observed in
510        // rust-analyzer repo CI).
511        //
512        // Some 32-bit specific failures include e.g. target pointer width specific hashes.
513
514        // FIXME: eventually, we should probably reduce the amount of target tuple substring
515        // matching in bootstrap.
516        if target.starts_with("i686") {
517            return;
518        }
519
520        let suite = "src/tools/rust-analyzer";
521        let mut cargo = tool::prepare_tool_cargo(
522            builder,
523            build_compiler,
524            Mode::ToolRustcPrivate,
525            target,
526            Kind::Test,
527            suite,
528            SourceType::InTree,
529            &["in-rust-tree".to_owned()],
530        );
531        cargo.allow_features(tool::RustAnalyzer::ALLOW_FEATURES);
532
533        // N.B. it turns out _setting_ `CARGO_WORKSPACE_DIR` actually somehow breaks `expect-test`,
534        // even though previously we actually needed to set that hack to allow `expect-test` to
535        // correctly discover the r-a workspace instead of the outer r-l/r workspace.
536
537        // FIXME: RA's test suite tries to write to the source directory, that can't work in Rust CI
538        // without properly wiring up the writable test dir.
539        cargo.env("SKIP_SLOW_TESTS", "1");
540
541        // NOTE: we need to skip `src/tools/rust-analyzer/xtask` as they seem to exercise rustup /
542        // stable rustfmt.
543        //
544        // NOTE: you can only skip a specific workspace package via `--exclude=...` if you *also*
545        // specify `--workspace`.
546        cargo.arg("--workspace");
547        cargo.arg("--exclude=xtask");
548
549        if build_compiler.stage == 0 {
550            // This builds a proc macro against the bootstrap libproc_macro, which is not ABI
551            // compatible with the ABI proc-macro-srv expects to load.
552            cargo.arg("--exclude=proc-macro-srv");
553            cargo.arg("--exclude=proc-macro-srv-cli");
554        }
555
556        let mut skip_tests = vec![];
557
558        // NOTE: the following test skips is a bit cheeky in that it assumes there are no
559        // identically named tests across different r-a packages, where we want to run the
560        // identically named test in one package but not another. If we want to support that use
561        // case, we'd have to run the r-a tests in two batches (with one excluding the package that
562        // we *don't* want to run the test for, and the other batch including).
563
564        // Across all platforms.
565        skip_tests.extend_from_slice(&[
566            // FIXME: this test wants to find a `rustc`. We need to provide it with a path to staged
567            // in-tree `rustc`, but setting `RUSTC` env var requires some reworking of bootstrap.
568            "tests::smoke_test_real_sysroot_cargo",
569            // NOTE: part of `smol-str` test suite; this tries to access a stable rustfmt from the
570            // environment, which is not something we want to do.
571            "check_code_formatting",
572        ]);
573
574        let skip_tests = skip_tests.iter().map(|name| format!("--skip={name}")).collect::<Vec<_>>();
575        let skip_tests = skip_tests.iter().map(|s| s.as_str()).collect::<Vec<_>>();
576
577        cargo.add_rustc_lib_path(builder);
578        run_cargo_test(
579            cargo,
580            skip_tests.as_slice(),
581            &[],
582            "rust-analyzer",
583            target,
584            builder,
585            record_failed_tests,
586        );
587    }
588
589    fn metadata(&self) -> Option<StepMetadata> {
590        Some(
591            StepMetadata::test("rust-analyzer", self.compilers.target())
592                .built_by(self.compilers.build_compiler()),
593        )
594    }
595}
596
597/// Runs `cargo test` for rustfmt.
598#[derive(Debug, Clone, PartialEq, Eq, Hash)]
599pub struct Rustfmt {
600    compilers: RustcPrivateCompilers,
601}
602
603impl CommandLineStep for Rustfmt {
604    type Output = ();
605    const IS_HOST: bool = true;
606
607    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
608        run.path("src/tools/rustfmt")
609    }
610
611    fn make_run(run: RunConfig<'_>) {
612        run.builder.ensure(Rustfmt {
613            compilers: RustcPrivateCompilers::new(
614                run.builder,
615                run.builder.top_stage,
616                run.builder.host_target,
617            ),
618        });
619    }
620
621    /// Runs `cargo test` for rustfmt.
622    fn run(self, builder: &Builder<'_>) {
623        let build_compiler = self.compilers.build_compiler();
624        let target = self.compilers.target();
625        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
626
627        // FIXME(#156525): `compile::Sysroot::run` intentionally do not copy `rustc-dev` artifacts
628        // until they're requested with `builder.ensure(Rustc)`, relevant for `download-rustc`
629        // flows.
630        builder.ensure(compile::Rustc::new(build_compiler, target));
631
632        let mut cargo = tool::prepare_tool_cargo(
633            builder,
634            build_compiler,
635            Mode::ToolRustcPrivate,
636            target,
637            Kind::Test,
638            "src/tools/rustfmt",
639            SourceType::InTree,
640            &[],
641        );
642
643        let dir = testdir(builder, target);
644        t!(fs::create_dir_all(&dir));
645        cargo.env("RUSTFMT_TEST_DIR", dir);
646
647        cargo.add_rustc_lib_path(builder);
648
649        run_cargo_test(cargo, &[], &[], "rustfmt", target, builder, record_failed_tests);
650    }
651
652    fn metadata(&self) -> Option<StepMetadata> {
653        Some(
654            StepMetadata::test("rustfmt", self.compilers.target())
655                .built_by(self.compilers.build_compiler()),
656        )
657    }
658}
659
660#[derive(Debug, Clone, PartialEq, Eq, Hash)]
661pub struct Miri {
662    target: TargetSelection,
663}
664
665impl Miri {
666    /// Run `cargo miri setup` for the given target, return where the Miri sysroot was put.
667    pub fn build_miri_sysroot(
668        builder: &Builder<'_>,
669        compiler: Compiler,
670        target: TargetSelection,
671    ) -> PathBuf {
672        let miri_sysroot = builder.out.join(compiler.host).join("miri-sysroot");
673        let mut cargo = builder::Cargo::new(
674            builder,
675            compiler,
676            Mode::Std,
677            SourceType::Submodule,
678            target,
679            Kind::MiriSetup,
680        );
681
682        // Tell `cargo miri setup` where to find the sources.
683        cargo.env("MIRI_LIB_SRC", builder.src.join("library"));
684        // Tell it where to put the sysroot.
685        cargo.env("MIRI_SYSROOT", &miri_sysroot);
686
687        let mut cargo = BootstrapCommand::from(cargo);
688        let _guard =
689            builder.msg(Kind::Build, "miri sysroot", Mode::ToolRustcPrivate, compiler, target);
690        cargo.run(builder);
691
692        // # Determine where Miri put its sysroot.
693        // To this end, we run `cargo miri setup --print-sysroot` and capture the output.
694        // (We do this separately from the above so that when the setup actually
695        // happens we get some output.)
696        // We re-use the `cargo` from above.
697        cargo.arg("--print-sysroot");
698
699        builder.do_if_verbose(|| println!("running: {cargo:?}"));
700        let stdout = cargo.run_capture_stdout(builder).stdout();
701        // Output is "<sysroot>\n".
702        let sysroot = stdout.trim_end();
703        builder.do_if_verbose(|| println!("`cargo miri setup --print-sysroot` said: {sysroot:?}"));
704        PathBuf::from(sysroot)
705    }
706}
707
708impl CommandLineStep for Miri {
709    type Output = ();
710
711    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
712        run.path("src/tools/miri")
713    }
714
715    fn make_run(run: RunConfig<'_>) {
716        run.builder.ensure(Miri { target: run.target });
717    }
718
719    /// Runs `cargo test` for miri.
720    fn run(self, builder: &Builder<'_>) {
721        let host = builder.sess.host_target;
722        let target = self.target;
723        let stage = builder.top_stage;
724        if stage == 0 {
725            eprintln!("miri cannot be tested at stage 0");
726            std::process::exit(1);
727        }
728
729        // This compiler runs on the host, we'll just use it for the target.
730        let compilers = RustcPrivateCompilers::new(builder, stage, host);
731
732        // Build our tools.
733        let miri = builder.ensure(tool::Miri::from_compilers(compilers));
734        // the ui tests also assume cargo-miri has been built
735        builder.ensure(tool::CargoMiri::from_compilers(compilers));
736
737        let target_compiler = compilers.target_compiler();
738
739        // We also need sysroots, for Miri and for the host (the latter for build scripts).
740        // This is for the tests so everything is done with the target compiler.
741        let miri_sysroot = Miri::build_miri_sysroot(builder, target_compiler, target);
742        builder.std(target_compiler, host);
743        let host_sysroot = builder.sysroot(target_compiler);
744
745        // Miri has its own "target dir" for ui test dependencies. Make sure it gets cleared when
746        // the sysroot gets rebuilt, to avoid "found possibly newer version of crate `std`" errors.
747        if !builder.config.dry_run() {
748            // This has to match `CARGO_TARGET_TMPDIR` in Miri's `ui.rs`.
749            // This means we need `host` here as that's the target `ui.rs` is built for.
750            let ui_test_dep_dir = builder
751                .stage_out(miri.build_compiler, Mode::ToolStd)
752                .join(host)
753                .join("tmp")
754                .join("miri_ui");
755            // The mtime of `miri_sysroot` changes when the sysroot gets rebuilt (also see
756            // <https://github.com/RalfJung/rustc-build-sysroot/commit/10ebcf60b80fe2c3dc765af0ff19fdc0da4b7466>).
757            // We can hence use that directly as a signal to clear the ui test dir.
758            build_stamp::clear_if_dirty(builder, &ui_test_dep_dir, &miri_sysroot);
759        }
760
761        // Run `cargo test`.
762        // This is with the Miri crate, so it uses the host compiler.
763        let mut cargo = tool::prepare_tool_cargo(
764            builder,
765            miri.build_compiler,
766            Mode::ToolRustcPrivate,
767            host,
768            Kind::Test,
769            "src/tools/miri",
770            SourceType::InTree,
771            &[],
772        );
773
774        cargo.add_rustc_lib_path(builder);
775
776        // We can NOT use `run_cargo_test` since Miri's integration tests do not use the usual test
777        // harness and therefore do not understand the flags added by `add_flags_and_try_run_test`.
778        let mut cargo = prepare_cargo_test(cargo, &[], &[], host, builder);
779
780        // miri tests need to know about the stage sysroot
781        cargo.env("MIRI_SYSROOT", &miri_sysroot);
782        cargo.env("MIRI_HOST_SYSROOT", &host_sysroot);
783
784        // Set the target.
785        cargo.env("MIRI_TEST_TARGET", target.rustc_target_arg());
786
787        {
788            let _guard = builder.msg_test("miri", target, target_compiler.stage);
789            let _time = helpers::timeit(builder);
790            cargo.run(builder);
791        }
792    }
793}
794
795/// Runs `cargo miri test` to demonstrate that `src/tools/miri/cargo-miri`
796/// works and that libtest works under miri.
797#[derive(Debug, Clone, PartialEq, Eq, Hash)]
798pub struct CargoMiri {
799    target: TargetSelection,
800}
801
802impl CommandLineStep for CargoMiri {
803    type Output = ();
804
805    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
806        run.path("src/tools/miri/cargo-miri")
807    }
808
809    fn make_run(run: RunConfig<'_>) {
810        run.builder.ensure(CargoMiri { target: run.target });
811    }
812
813    /// Tests `cargo miri test`.
814    fn run(self, builder: &Builder<'_>) {
815        let host = builder.sess.host_target;
816        let target = self.target;
817        let stage = builder.top_stage;
818        if stage == 0 {
819            eprintln!("cargo-miri cannot be tested at stage 0");
820            std::process::exit(1);
821        }
822
823        // This compiler runs on the host, we'll just use it for the target.
824        let build_compiler = builder.compiler(stage, host);
825
826        // Run `cargo miri test`.
827        // This is just a smoke test (Miri's own CI invokes this in a bunch of different ways and ensures
828        // that we get the desired output), but that is sufficient to make sure that the libtest harness
829        // itself executes properly under Miri, and that all the logic in `cargo-miri` does not explode.
830        let mut cargo = tool::prepare_tool_cargo(
831            builder,
832            build_compiler,
833            Mode::ToolStd, // it's unclear what to use here, we're not building anything just doing a smoke test!
834            target,
835            Kind::MiriTest,
836            "src/tools/miri/test-cargo-miri",
837            SourceType::Submodule,
838            &[],
839        );
840        // Run subcrate tests as well.
841        cargo.arg("--workspace");
842        // Some tests need isolation disabled.
843        cargo.env("MIRIFLAGS", "-Zmiri-disable-isolation");
844
845        // If we are testing stage 2+ cargo miri, make sure that it works with the in-tree cargo.
846        // We want to do this *somewhere* to ensure that Miri + nightly cargo actually works.
847        if stage >= 2 {
848            let built_cargo = builder
849                .ensure(tool::Cargo::from_build_compiler(
850                    // Build stage 1 cargo here, we don't need it to be built in any special way,
851                    // just that it is built from in-tree sources.
852                    builder.compiler(0, builder.host_target),
853                    builder.host_target,
854                ))
855                .tool_path;
856            cargo.env("CARGO", built_cargo);
857        }
858
859        // We're not using `prepare_cargo_test` so we have to do this ourselves.
860        // (We're not using that as the test-cargo-miri crate is not known to bootstrap.)
861        match builder.test_target {
862            TestTarget::AllTargets => {
863                cargo.args(["--lib", "--bins", "--examples", "--tests", "--benches"])
864            }
865            TestTarget::Default => &mut cargo,
866            TestTarget::DocOnly => cargo.arg("--doc"),
867            TestTarget::Tests => cargo.arg("--tests"),
868        };
869        cargo.arg("--").args(builder.config.test_args());
870
871        // Finally, run everything.
872        let mut cargo = BootstrapCommand::from(cargo);
873        {
874            let _guard = builder.msg_test("cargo-miri", target, stage);
875            let _time = helpers::timeit(builder);
876            cargo.run(builder);
877        }
878    }
879}
880
881#[derive(Debug, Clone, PartialEq, Eq, Hash)]
882pub struct Priroda {
883    target: TargetSelection,
884}
885
886impl CommandLineStep for Priroda {
887    type Output = ();
888
889    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
890        run.path("src/tools/miri/priroda")
891    }
892
893    fn make_run(run: RunConfig<'_>) {
894        run.builder.ensure(Priroda { target: run.target });
895    }
896
897    /// Runs `cargo test` for priroda, reusing the Miri sysroot and binary.
898    fn run(self, builder: &Builder<'_>) {
899        let host = builder.sess.host_target;
900        let target = self.target;
901        let stage = builder.top_stage;
902
903        // Priroda tests run under Miri, so reuse the Miri binary and sysroot.
904        let compilers = RustcPrivateCompilers::new(builder, stage, host);
905        let miri = builder.ensure(tool::Miri::from_compilers(compilers));
906        let target_compiler = compilers.target_compiler();
907
908        let miri_sysroot = Miri::build_miri_sysroot(builder, target_compiler, target);
909        builder.std(target_compiler, host);
910        let host_sysroot = builder.sysroot(target_compiler);
911
912        let mut cargo = tool::prepare_tool_cargo(
913            builder,
914            miri.build_compiler,
915            Mode::ToolRustcPrivate,
916            host,
917            Kind::Test,
918            "src/tools/miri",
919            SourceType::InTree,
920            &["priroda".into()],
921        );
922        cargo.args(["--test", "priroda"]);
923
924        cargo.add_rustc_lib_path(builder);
925
926        let mut cargo = prepare_cargo_test(cargo, &[], &[], host, builder);
927
928        cargo.env("MIRI_SYSROOT", &miri_sysroot);
929        cargo.env("MIRI_HOST_SYSROOT", &host_sysroot);
930        cargo.env("MIRI_TEST_TARGET", target.rustc_target_arg());
931
932        {
933            let _guard = builder.msg_test("priroda", target, target_compiler.stage);
934            let _time = helpers::timeit(builder);
935            cargo.run(builder);
936        }
937    }
938}
939
940#[derive(Debug, Clone, PartialEq, Eq, Hash)]
941pub struct CompiletestTest {
942    host: TargetSelection,
943}
944
945impl CommandLineStep for CompiletestTest {
946    type Output = ();
947
948    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
949        run.path("src/tools/compiletest")
950    }
951
952    fn make_run(run: RunConfig<'_>) {
953        run.builder.ensure(CompiletestTest { host: run.target });
954    }
955
956    /// Runs `cargo test` for compiletest.
957    fn run(self, builder: &Builder<'_>) {
958        let host = self.host;
959        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
960
961        // Now that compiletest uses only stable Rust, building it always uses
962        // the stage 0 compiler. However, some of its unit tests need to be able
963        // to query information from an in-tree compiler, so we treat `--stage`
964        // as selecting the stage of that secondary compiler.
965
966        if builder.top_stage == 0 && !builder.config.compiletest_allow_stage0 {
967            eprintln!("\
968ERROR: `--stage 0` causes compiletest to query information from the stage0 (precompiled) compiler, instead of the in-tree compiler, which can cause some tests to fail inappropriately
969NOTE: if you're sure you want to do this, please open an issue as to why. In the meantime, you can override this with `--set build.compiletest-allow-stage0=true`."
970            );
971            helpers::exit_process(1);
972        }
973
974        let bootstrap_compiler = builder.compiler(0, host);
975        let staged_compiler = builder.compiler(builder.top_stage, host);
976
977        let mut cargo = tool::prepare_tool_cargo(
978            builder,
979            bootstrap_compiler,
980            Mode::ToolBootstrap,
981            host,
982            Kind::Test,
983            "src/tools/compiletest",
984            SourceType::InTree,
985            &[],
986        );
987
988        // Used for `compiletest` self-tests to have the path to the *staged* compiler. Getting this
989        // right is important, as `compiletest` is intended to only support one target spec JSON
990        // format, namely that of the staged compiler.
991        cargo.env("TEST_RUSTC", builder.rustc(staged_compiler));
992
993        run_cargo_test(
994            cargo,
995            &[],
996            &[],
997            "compiletest self test",
998            host,
999            builder,
1000            record_failed_tests,
1001        );
1002    }
1003}
1004
1005/// Runs `library/stdarch/crates/stdarch-verify`'s tests which cross-check the
1006/// `core::arch` intrinsics for x86, Arm, and MIPS against the corresponding
1007/// vendor references (signatures, target features, and `assert_instr` mappings).
1008#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1009pub struct StdarchVerify;
1010
1011impl CommandLineStep for StdarchVerify {
1012    type Output = ();
1013    const IS_HOST: bool = true;
1014
1015    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1016        run.path("library/stdarch/crates/stdarch-verify")
1017    }
1018
1019    fn is_default_step(_builder: &Builder<'_>) -> bool {
1020        true
1021    }
1022
1023    fn make_run(run: RunConfig<'_>) {
1024        let builder = run.builder;
1025        if builder.remote_tested(run.target) {
1026            builder.info("remote testing is not supported by stdarch-verify. skipping");
1027            return;
1028        }
1029        builder.ensure(StdarchVerify);
1030    }
1031
1032    fn run(self, builder: &Builder<'_>) {
1033        let host = builder.config.host_target;
1034        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
1035        let build_compiler = builder.compiler(0, host);
1036
1037        let cargo = tool::prepare_tool_cargo(
1038            builder,
1039            build_compiler,
1040            Mode::ToolBootstrap,
1041            host,
1042            Kind::Test,
1043            "library/stdarch/crates/stdarch-verify",
1044            SourceType::InTree,
1045            &[],
1046        );
1047
1048        run_cargo_test(
1049            cargo,
1050            &[],
1051            &["stdarch-verify".to_string()],
1052            Some("stdarch-verify"),
1053            host,
1054            builder,
1055            record_failed_tests,
1056        );
1057    }
1058}
1059
1060/// Runs stdarch's intrinsic-test binary crate to verify that Rust's `core::arch`
1061/// SIMD intrinsics produce the same results as their C counterparts.
1062///
1063/// First runs the `intrinsic-test` binary, which generates C wrapper programs
1064/// and a Rust Cargo workspace. Then runs `cargo test` on that workspace
1065/// which compiles both versions and compares their outputs on random inputs.
1066///
1067/// On `x86_64`, it requires a very recent version of GCC (e.g. GCC 15+)
1068/// as well as the Intel SDE emulator to successfully run the tests.
1069#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1070pub struct IntrinsicTest {
1071    host: TargetSelection,
1072}
1073
1074impl CommandLineStep for IntrinsicTest {
1075    type Output = ();
1076    const IS_HOST: bool = true;
1077
1078    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1079        run.alias("intrinsic-test")
1080    }
1081
1082    fn is_default_step(_builder: &Builder<'_>) -> bool {
1083        true
1084    }
1085
1086    fn make_run(run: RunConfig<'_>) {
1087        let target = run.target;
1088        let builder = run.builder;
1089
1090        let is_explicit =
1091            builder.config.paths.iter().any(|p| p.to_string_lossy() == "intrinsic-test");
1092
1093        if target.contains("x86_64-unknown-linux") && builder.config.sde.is_none() && is_explicit {
1094            panic!(
1095                "SDE is required to run intrinsic-test. Please configure `build.sde` in config.toml."
1096            );
1097        }
1098
1099        builder.ensure(IntrinsicTest { host: target });
1100    }
1101
1102    fn run(self, builder: &Builder<'_>) {
1103        let host = self.host;
1104        if cfg!(test)
1105            || (!host.contains("aarch64-unknown-linux") && !host.contains("x86_64-unknown-linux"))
1106        {
1107            builder.info(&format!("Skipping intrinsic-test, as it is not available for {host}"));
1108            return;
1109        }
1110        // intrinsic-test shells out to `cargo` and `rustfmt` make bootstrap's
1111        // managed binaries findable by prepending their dirs to PATH.
1112        let Some(rustfmt_path) = builder.ensure(InternalRustfmt) else {
1113            eprintln!(
1114                "WARNING: intrinsic-test skipped because rustfmt is required but not available on this channel"
1115            );
1116            return;
1117        };
1118
1119        let (input_file, skip_file, cflags, sde_runner) = if host.contains("x86_64-unknown-linux") {
1120            let Some(sde) = &builder.config.sde else {
1121                builder.info("Skipping intrinsic-test because `build.sde` is not configured");
1122                return;
1123            };
1124
1125            let cpuid_def =
1126                builder.src.join("library/stdarch/ci/docker/x86_64-unknown-linux-gnu/cpuid.def");
1127            let sde_runner = format!(
1128                "{} -cpuid-in {} -rtm-mode full -tsx --",
1129                sde.display(),
1130                cpuid_def.display()
1131            );
1132
1133            (
1134                builder.src.join("library/stdarch/intrinsics_data/x86-intel.xml"),
1135                [
1136                    builder
1137                        .src
1138                        .join("library/stdarch/crates/intrinsic-test/missing_x86_common.txt"),
1139                    builder.src.join("library/stdarch/crates/intrinsic-test/missing_x86_gcc.txt"),
1140                ],
1141                "-I/usr/include/x86_64-linux-gnu/",
1142                Some(sde_runner),
1143            )
1144        } else if host.contains("aarch64-unknown-linux") {
1145            (
1146                builder.src.join("library/stdarch/intrinsics_data/arm_intrinsics.json"),
1147                [
1148                    builder
1149                        .src
1150                        .join("library/stdarch/crates/intrinsic-test/missing_aarch64_common.txt"),
1151                    builder
1152                        .src
1153                        .join("library/stdarch/crates/intrinsic-test/missing_aarch64_gcc.txt"),
1154                ],
1155                "-I/usr/aarch64-linux-gnu/include/",
1156                None,
1157            )
1158        } else {
1159            panic!("intrinsic-test only supports aarch64/x86_64 Linux, got {host}");
1160        };
1161
1162        let out_dir = builder.out.join(host).join("intrinsic-test");
1163        t!(fs::create_dir_all(&out_dir));
1164
1165        let crates_link = out_dir.join("crates");
1166        if !crates_link.exists() {
1167            t!(
1168                helpers::symlink_dir(
1169                    &builder.config,
1170                    &builder.src.join("library/stdarch/crates"),
1171                    &crates_link
1172                ),
1173                format!("failed to symlink stdarch crates into {}", crates_link.display())
1174            );
1175        }
1176
1177        let mut cmd = builder.tool_cmd(Tool::IntrinsicTest);
1178        cmd.current_dir(&out_dir);
1179        cmd.arg(&input_file);
1180        cmd.arg("--target").arg(&*host.triple);
1181        for skip in &skip_file {
1182            cmd.arg("--skip").arg(skip);
1183        }
1184        cmd.arg("--sample-percentage").arg("100");
1185        cmd.arg("--cc-arg-style").arg("gcc");
1186        cmd.env("CC", builder.cc(host));
1187        cmd.env("CFLAGS", cflags);
1188
1189        let mut path_dirs: Vec<PathBuf> = Vec::new();
1190        if let Some(cargo_dir) = builder.initial_cargo.parent() {
1191            path_dirs.push(cargo_dir.to_path_buf());
1192        }
1193        if let Some(rustfmt_dir) = rustfmt_path.parent() {
1194            path_dirs.push(rustfmt_dir.to_path_buf());
1195        }
1196        let old_path = env::var_os("PATH").unwrap_or_default();
1197        let new_path = env::join_paths(path_dirs.into_iter().chain(env::split_paths(&old_path)))
1198            .expect("could not build PATH for intrinsic-test");
1199        cmd.env("PATH", new_path);
1200        cmd.run(builder);
1201
1202        let tested_compiler = builder.compiler(builder.top_stage, host);
1203        builder.std(tested_compiler, host);
1204        let rustc = builder.rustc(tested_compiler);
1205
1206        let manifest = out_dir.join("rust_programs/Cargo.toml");
1207        let mut cargo = command(&builder.initial_cargo);
1208        cargo.arg("test");
1209        cargo.arg("--tests");
1210        cargo.arg("--manifest-path").arg(&manifest);
1211        cargo.arg("--target").arg(&*host.triple);
1212        cargo.arg("--profile").arg("release");
1213        cargo.env("CC", builder.cc(host));
1214        cargo.env("CFLAGS", cflags);
1215        cargo.env("RUSTC", rustc);
1216        cargo.env("RUSTC_BOOTSTRAP", "1");
1217        if let Some(runner) = sde_runner {
1218            cargo.env("CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER", runner);
1219        }
1220        cargo.run(builder);
1221    }
1222
1223    fn metadata(&self) -> Option<StepMetadata> {
1224        Some(StepMetadata::test("intrinsic-test", self.host))
1225    }
1226}
1227
1228/// Runs stdarch's gen-checks (arm, loongarch, hexagon) to
1229/// verify the committed `core_arch` files are up to date with their specs.
1230/// With `--bless`, regenerates and writes them back instead.
1231#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1232pub struct StdarchGenCheck {
1233    host: TargetSelection,
1234}
1235
1236impl CommandLineStep for StdarchGenCheck {
1237    type Output = ();
1238    const IS_HOST: bool = true;
1239
1240    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1241        run.alias("stdarch-gen-check")
1242    }
1243
1244    fn is_default_step(_builder: &Builder<'_>) -> bool {
1245        true
1246    }
1247
1248    fn make_run(run: RunConfig<'_>) {
1249        run.builder.ensure(StdarchGenCheck { host: run.target });
1250    }
1251
1252    fn run(self, builder: &Builder<'_>) {
1253        let stdarch_root = builder.src.join("library/stdarch");
1254
1255        // `--bless` regenerates and writes back into the tree otherwise just check.
1256        let mode = if builder.config.cmd.bless() { "bless" } else { "check" };
1257
1258        // Generators shell out to `rustfmt`. Skip this step if bootstrap has none for this channel.
1259        let Some(rustfmt_path) = builder.ensure(InternalRustfmt) else {
1260            eprintln!(
1261                "WARNING: stdarch-gen-check skipped because rustfmt is required but not available on this channel"
1262            );
1263            return;
1264        };
1265
1266        // Keep cargo's build artifacts out of the (possibly read-only) source tree.
1267        let cargo_target_dir = builder.out.join("stdarch-gen-check").join("target");
1268
1269        let manifest = stdarch_root.join("Cargo.toml");
1270        let arm_spec = stdarch_root.join("crates/stdarch-gen-arm/spec");
1271        let core_arch_src = stdarch_root.join("crates/core_arch/src");
1272
1273        // `stdarch-gen-common` runs each generator into a temp dir and diffs/blesses
1274        // against the committed files itself, driven by STDARCH_GEN_MODE.
1275        let run_gen = |selector: &str, pkg: &str, args: &[&OsStr]| {
1276            let mut cmd = command(&builder.initial_cargo);
1277            // Note: it is important to run this command from this directory, so that Cargo doesn't
1278            // pick up library/.cargo/config.toml, which may point to vendored sources (which
1279            // currently do not contain dependencies from stdarch).
1280            cmd.current_dir(&builder.src);
1281            cmd.arg("run")
1282                .arg("--manifest-path")
1283                .arg(&manifest)
1284                .arg(selector)
1285                .arg(pkg)
1286                .arg("--release")
1287                .arg("--")
1288                .args(args)
1289                .arg("--rustfmt-path")
1290                .arg(&rustfmt_path);
1291            // RUSTC_BOOTSTRAP=1 allow nightly features when building tools against stage0.
1292            cmd.env("RUSTC_BOOTSTRAP", "1");
1293            cmd.env("RUSTC", &builder.initial_rustc);
1294            cmd.env("CARGO_TARGET_DIR", &cargo_target_dir);
1295            cmd.env("STDARCH_GEN_MODE", mode);
1296            cmd.run(builder);
1297        };
1298
1299        run_gen("--bin", "stdarch-gen-arm", &[arm_spec.as_os_str(), core_arch_src.as_os_str()]);
1300        run_gen("-p", "stdarch-gen-loongarch", &[OsStr::new("lsx")]);
1301        run_gen("-p", "stdarch-gen-loongarch", &[OsStr::new("lasx")]);
1302        run_gen("-p", "stdarch-gen-hexagon", &[]);
1303    }
1304}
1305
1306#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1307pub struct Clippy {
1308    compilers: RustcPrivateCompilers,
1309}
1310
1311impl CommandLineStep for Clippy {
1312    type Output = ();
1313    const IS_HOST: bool = true;
1314
1315    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1316        run.suite_path("src/tools/clippy/tests").path("src/tools/clippy")
1317    }
1318
1319    fn is_default_step(_builder: &Builder<'_>) -> bool {
1320        false
1321    }
1322
1323    fn make_run(run: RunConfig<'_>) {
1324        run.builder.ensure(Clippy {
1325            compilers: RustcPrivateCompilers::new(
1326                run.builder,
1327                run.builder.top_stage,
1328                run.builder.host_target,
1329            ),
1330        });
1331    }
1332
1333    /// Runs `cargo test` for clippy.
1334    fn run(self, builder: &Builder<'_>) {
1335        let target = self.compilers.target();
1336
1337        // We need to carefully distinguish the compiler that builds clippy, and the compiler
1338        // that is linked into the clippy being tested. `target_compiler` is the latter,
1339        // and it must also be used by clippy's test runner to build tests and their dependencies.
1340        let target_compiler = self.compilers.target_compiler();
1341        let build_compiler = self.compilers.build_compiler();
1342
1343        // FIXME(#156525): `compile::Sysroot::run` intentionally do not copy `rustc-dev` artifacts
1344        // until they're requested with `builder.ensure(Rustc)`, relevant for `download-rustc`
1345        // flows.
1346        builder.ensure(compile::Rustc::new(build_compiler, target));
1347
1348        let mut cargo = tool::prepare_tool_cargo(
1349            builder,
1350            build_compiler,
1351            Mode::ToolRustcPrivate,
1352            target,
1353            Kind::Test,
1354            "src/tools/clippy",
1355            SourceType::InTree,
1356            &[],
1357        );
1358
1359        cargo.env("RUSTC_TEST_SUITE", builder.rustc(build_compiler));
1360        cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(build_compiler));
1361        let host_libs = builder
1362            .stage_out(build_compiler, Mode::ToolRustcPrivate)
1363            .join(builder.cargo_dir(Mode::ToolRustcPrivate));
1364        cargo.env("HOST_LIBS", host_libs);
1365
1366        // Build the standard library that the tests can use.
1367        builder.std(target_compiler, target);
1368        cargo.env("TEST_SYSROOT", builder.sysroot(target_compiler));
1369        cargo.env("TEST_RUSTC", builder.rustc(target_compiler));
1370        cargo.env("TEST_RUSTC_LIB", builder.rustc_libdir(target_compiler));
1371
1372        // Collect paths of tests to run
1373        'partially_test: {
1374            let paths = &builder.config.paths[..];
1375            let mut test_names = Vec::new();
1376            for path in paths {
1377                match helpers::is_valid_test_suite_arg(path, "src/tools/clippy/tests", builder) {
1378                    TestFilterCategory::Arg(path) => {
1379                        test_names.push(path);
1380                    }
1381                    TestFilterCategory::Fullsuite => {
1382                        // When src/tools/clippy is called directly, all tests should be run.
1383                        break 'partially_test;
1384                    }
1385                    TestFilterCategory::Uninteresting => {}
1386                }
1387            }
1388            cargo.env("TESTNAME", test_names.join(","));
1389        }
1390
1391        cargo.add_rustc_lib_path(builder);
1392        let cargo = prepare_cargo_test(cargo, &[], &[], target, builder);
1393
1394        let _guard = builder.msg_test("clippy", target, target_compiler.stage);
1395
1396        // Clippy reports errors if it blessed the outputs
1397        if cargo.allow_failure().run(builder) {
1398            // The tests succeeded; nothing to do.
1399            return;
1400        }
1401
1402        if !builder.config.cmd.bless() {
1403            helpers::exit_process(1);
1404        }
1405    }
1406
1407    fn metadata(&self) -> Option<StepMetadata> {
1408        Some(
1409            StepMetadata::test("clippy", self.compilers.target())
1410                .built_by(self.compilers.build_compiler()),
1411        )
1412    }
1413}
1414
1415fn bin_path_for_cargo(builder: &Builder<'_>, compiler: Compiler) -> OsString {
1416    let path = builder.sysroot(compiler).join("bin");
1417    let old_path = env::var_os("PATH").unwrap_or_default();
1418    env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("")
1419}
1420
1421/// Run the rustdoc-themes tool to test a given compiler.
1422#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1423pub struct RustdocTheme {
1424    /// The compiler (more accurately, its rustdoc) that we test.
1425    test_compiler: Compiler,
1426}
1427
1428impl CommandLineStep for RustdocTheme {
1429    type Output = ();
1430    const IS_HOST: bool = true;
1431
1432    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1433        run.path("src/tools/rustdoc-themes")
1434    }
1435
1436    fn is_default_step(_builder: &Builder<'_>) -> bool {
1437        true
1438    }
1439
1440    fn make_run(run: RunConfig<'_>) {
1441        let test_compiler = run.builder.compiler(run.builder.top_stage, run.target);
1442
1443        run.builder.ensure(RustdocTheme { test_compiler });
1444    }
1445
1446    fn run(self, builder: &Builder<'_>) {
1447        let rustdoc = builder.bootstrap_out.join("rustdoc");
1448        let mut cmd = builder.tool_cmd(Tool::RustdocTheme);
1449        cmd.arg(rustdoc.to_str().unwrap())
1450            .arg(builder.src.join("src/librustdoc/html/static/css/rustdoc.css").to_str().unwrap())
1451            .env("RUSTC_STAGE", self.test_compiler.stage.to_string())
1452            .env("RUSTC_SYSROOT", builder.sysroot(self.test_compiler))
1453            .env(
1454                "RUSTDOC_LIBDIR",
1455                builder.sysroot_target_libdir(self.test_compiler, self.test_compiler.host),
1456            )
1457            .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
1458            .env("RUSTDOC_REAL", builder.rustdoc_for_compiler(self.test_compiler))
1459            .env("RUSTC_BOOTSTRAP", "1");
1460        cmd.args(linker_args(builder, self.test_compiler.host, LldThreads::No));
1461
1462        cmd.delay_failure().run(builder);
1463    }
1464
1465    fn metadata(&self) -> Option<StepMetadata> {
1466        Some(
1467            StepMetadata::test("rustdoc-theme", self.test_compiler.host)
1468                .stage(self.test_compiler.stage),
1469        )
1470    }
1471}
1472
1473/// Test rustdoc JS for the standard library.
1474#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1475pub struct RustdocJSStd {
1476    /// Compiler that will build the standary library.
1477    build_compiler: Compiler,
1478    target: TargetSelection,
1479}
1480
1481impl CommandLineStep for RustdocJSStd {
1482    type Output = ();
1483    const IS_HOST: bool = true;
1484
1485    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1486        run.suite_path("tests/rustdoc-js-std")
1487    }
1488
1489    fn is_default_step(builder: &Builder<'_>) -> bool {
1490        builder.config.nodejs.is_some()
1491    }
1492
1493    fn make_run(run: RunConfig<'_>) {
1494        run.builder.ensure(RustdocJSStd {
1495            build_compiler: run.builder.compiler(run.builder.top_stage, run.builder.host_target),
1496            target: run.target,
1497        });
1498    }
1499
1500    fn run(self, builder: &Builder<'_>) {
1501        let nodejs =
1502            builder.config.nodejs.as_ref().expect("need nodejs to run rustdoc-js-std tests");
1503        let mut command = command(nodejs);
1504        command
1505            .arg(builder.src.join("src/tools/rustdoc-js/tester.js"))
1506            .arg("--crate-name")
1507            .arg("std")
1508            .arg("--resource-suffix")
1509            .arg(&builder.version)
1510            .arg("--doc-folder")
1511            .arg(builder.doc_out(self.target))
1512            .arg("--test-folder")
1513            .arg(builder.src.join("tests/rustdoc-js-std"));
1514
1515        let full_suite = builder.paths.iter().any(|path| {
1516            matches!(
1517                helpers::is_valid_test_suite_arg(path, "tests/rustdoc-js-std", builder),
1518                TestFilterCategory::Fullsuite
1519            )
1520        });
1521
1522        // If we have to also run the full suite, don't worry about the individual arguments.
1523        // They will be covered by running the entire suite
1524        if !full_suite {
1525            for path in &builder.paths {
1526                if let TestFilterCategory::Arg(p) =
1527                    helpers::is_valid_test_suite_arg(path, "tests/rustdoc-js-std", builder)
1528                {
1529                    if !p.ends_with(".js") {
1530                        eprintln!("A non-js file was given: `{}`", path.display());
1531                        panic!("Cannot run rustdoc-js-std tests");
1532                    }
1533                    command.arg("--test-file").arg(path);
1534                }
1535            }
1536        }
1537
1538        builder.ensure(crate::core::build_steps::doc::Std::from_build_compiler(
1539            self.build_compiler,
1540            self.target,
1541            DocumentationFormat::Html,
1542        ));
1543        let _guard = builder.msg_test("rustdoc-js-std", self.target, self.build_compiler.stage);
1544        command.run(builder);
1545    }
1546
1547    fn metadata(&self) -> Option<StepMetadata> {
1548        Some(StepMetadata::test("rustdoc-js-std", self.target).stage(self.build_compiler.stage))
1549    }
1550}
1551
1552#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1553pub struct RustdocJSNotStd {
1554    pub target: TargetSelection,
1555    pub compiler: Compiler,
1556}
1557
1558impl CommandLineStep for RustdocJSNotStd {
1559    type Output = ();
1560    const IS_HOST: bool = true;
1561
1562    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1563        run.suite_path("tests/rustdoc-js")
1564    }
1565
1566    fn is_default_step(builder: &Builder<'_>) -> bool {
1567        builder.config.nodejs.is_some()
1568    }
1569
1570    fn make_run(run: RunConfig<'_>) {
1571        let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
1572        run.builder.ensure(RustdocJSNotStd { target: run.target, compiler });
1573    }
1574
1575    fn run(self, builder: &Builder<'_>) {
1576        builder.ensure(Compiletest {
1577            test_compiler: self.compiler,
1578            target: self.target,
1579            mode: CompiletestMode::RustdocJs,
1580            suite: "rustdoc-js",
1581            path: "tests/rustdoc-js",
1582            compare_mode: None,
1583        });
1584    }
1585}
1586
1587fn get_browser_ui_test_version_inner(
1588    builder: &Builder<'_>,
1589    yarn: &Path,
1590    global: bool,
1591) -> Option<String> {
1592    let mut command = command(yarn);
1593    command
1594        .arg("--cwd")
1595        .arg(&builder.sess.out)
1596        .arg("list")
1597        .arg("--parseable")
1598        .arg("--long")
1599        .arg("--depth=0");
1600    if global {
1601        command.arg("--global");
1602    }
1603    // Cache the command output so that `test::RustdocGUI` only performs these
1604    // command-line probes once.
1605    let lines = command.allow_failure().cached().run_capture(builder).stdout();
1606    lines
1607        .lines()
1608        .find_map(|l| l.split(':').nth(1)?.strip_prefix("browser-ui-test@"))
1609        .map(|v| v.to_owned())
1610}
1611
1612fn get_browser_ui_test_version(builder: &Builder<'_>) -> Option<String> {
1613    let yarn = builder.config.yarn.as_deref()?;
1614    get_browser_ui_test_version_inner(builder, yarn, false)
1615        .or_else(|| get_browser_ui_test_version_inner(builder, yarn, true))
1616}
1617
1618/// Run GUI tests on a given rustdoc.
1619#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1620pub struct RustdocGUI {
1621    /// The compiler whose rustdoc we are testing.
1622    test_compiler: Compiler,
1623    target: TargetSelection,
1624}
1625
1626impl CommandLineStep for RustdocGUI {
1627    type Output = ();
1628    const IS_HOST: bool = true;
1629
1630    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1631        run.suite_path("tests/rustdoc-gui")
1632    }
1633
1634    fn is_default_step(builder: &Builder<'_>) -> bool {
1635        builder.config.nodejs.is_some()
1636            && builder.test_target != TestTarget::DocOnly
1637            && get_browser_ui_test_version(builder).is_some()
1638    }
1639
1640    fn make_run(run: RunConfig<'_>) {
1641        let test_compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
1642        run.builder.ensure(RustdocGUI { test_compiler, target: run.target });
1643    }
1644
1645    fn run(self, builder: &Builder<'_>) {
1646        builder.std(self.test_compiler, self.target);
1647        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
1648
1649        let mut cmd = builder.tool_cmd(Tool::RustdocGUITest);
1650
1651        let out_dir = builder.out.join(self.target).join("test").join("rustdoc-gui");
1652        build_stamp::clear_if_dirty(
1653            builder,
1654            &out_dir,
1655            &builder.rustdoc_for_compiler(self.test_compiler),
1656        );
1657
1658        if let Some(src) = builder.config.src.to_str() {
1659            cmd.arg("--rust-src").arg(src);
1660        }
1661
1662        if let Some(out_dir) = out_dir.to_str() {
1663            cmd.arg("--out-dir").arg(out_dir);
1664        }
1665
1666        if let Some(initial_cargo) = builder.sess.initial_cargo.to_str() {
1667            cmd.arg("--initial-cargo").arg(initial_cargo);
1668        }
1669
1670        cmd.arg("--jobs").arg(builder.jobs().to_string());
1671
1672        cmd.env("RUSTDOC", builder.rustdoc_for_compiler(self.test_compiler))
1673            .env("RUSTC", builder.rustc(self.test_compiler));
1674
1675        add_rustdoc_cargo_linker_args(&mut cmd, builder, self.test_compiler.host, LldThreads::No);
1676
1677        let full_suite = builder.paths.iter().any(|path| {
1678            matches!(
1679                helpers::is_valid_test_suite_arg(path, "tests/rustdoc-js-std", builder),
1680                TestFilterCategory::Fullsuite
1681            )
1682        });
1683
1684        // If we have to also run the full suite, don't worry about the individual arguments.
1685        // They will be covered by running the entire suite
1686        if !full_suite {
1687            for path in &builder.paths {
1688                if let TestFilterCategory::Arg(p) =
1689                    helpers::is_valid_test_suite_arg(path, "tests/rustdoc-gui", builder)
1690                {
1691                    if !p.ends_with(".goml") {
1692                        eprintln!("A non-goml file was given: `{}`", path.display());
1693                        panic!("Cannot run rustdoc-gui tests");
1694                    }
1695                    if let Some(name) = path.file_name().and_then(|f| f.to_str()) {
1696                        cmd.arg("--goml-file").arg(name);
1697                    }
1698                }
1699            }
1700        }
1701
1702        for test_arg in builder.config.test_args() {
1703            cmd.arg("--test-arg").arg(test_arg);
1704        }
1705
1706        if let Some(ref nodejs) = builder.config.nodejs {
1707            cmd.arg("--nodejs").arg(nodejs);
1708        }
1709
1710        if let Some(ref yarn) = builder.config.yarn {
1711            cmd.arg("--yarn").arg(yarn);
1712        }
1713
1714        let _time = helpers::timeit(builder);
1715        let _guard = builder.msg_test("rustdoc-gui", self.target, self.test_compiler.stage);
1716        try_run_tests(builder, &mut cmd, true, record_failed_tests);
1717    }
1718
1719    fn metadata(&self) -> Option<StepMetadata> {
1720        Some(StepMetadata::test("rustdoc-gui", self.target).stage(self.test_compiler.stage))
1721    }
1722}
1723
1724/// Runs `src/tools/tidy` and `cargo fmt --check` to detect various style
1725/// problems in the repository.
1726///
1727/// (To run the tidy tool's internal tests, use the alias "tidyselftest" instead.)
1728#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1729pub struct Tidy;
1730
1731impl CommandLineStep for Tidy {
1732    type Output = ();
1733    const IS_HOST: bool = true;
1734
1735    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1736        run.path("src/tools/tidy")
1737    }
1738
1739    fn is_default_step(builder: &Builder<'_>) -> bool {
1740        builder.test_target != TestTarget::DocOnly
1741    }
1742
1743    fn make_run(run: RunConfig<'_>) {
1744        run.builder.ensure(Tidy);
1745    }
1746
1747    /// Runs the `tidy` tool.
1748    ///
1749    /// This tool in `src/tools` checks up on various bits and pieces of style and
1750    /// otherwise just implements a few lint-like checks that are specific to the
1751    /// compiler itself.
1752    ///
1753    /// Once tidy passes, this step also runs `fmt --check` if tests are being run
1754    /// for the `dev` or `nightly` channels.
1755    fn run(self, builder: &Builder<'_>) {
1756        let mut cmd = builder.tool_cmd(Tool::Tidy);
1757        cmd.arg(format!("--root-path={}", builder.src.display()));
1758        cmd.arg(format!("--cargo-path={}", builder.initial_cargo.display()));
1759        cmd.arg(format!("--output-dir={}", builder.out.display()));
1760        // Tidy is heavily IO constrained. Still respect `-j`, but use a higher limit if `jobs` hasn't been configured.
1761        let jobs = builder.config.jobs.unwrap_or_else(|| {
1762            8 * std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) as u32
1763        });
1764        cmd.arg(format!("--concurrency={jobs}"));
1765        // pass the path to the yarn command used for installing js deps.
1766        if let Some(yarn) = &builder.config.yarn {
1767            cmd.arg(format!("--npm-path={}", yarn.display()));
1768        } else {
1769            cmd.arg("--npm-path=yarn");
1770        }
1771        if builder.is_verbose() {
1772            cmd.arg("--verbose");
1773        }
1774        if builder.config.cmd.bless() {
1775            cmd.arg("--bless");
1776        }
1777        if builder.config.is_running_on_ci() {
1778            cmd.arg("--ci=true");
1779        }
1780        if let Some(s) =
1781            builder.config.cmd.extra_checks().or(builder.config.tidy_extra_checks.as_deref())
1782        {
1783            cmd.arg(format!("--extra-checks={s}"));
1784        }
1785        let mut args = std::env::args_os();
1786        if args.any(|arg| arg == OsStr::new("--")) {
1787            cmd.arg("--");
1788            cmd.args(args);
1789        }
1790
1791        if builder.config.channel == "dev" || builder.config.channel == "nightly" {
1792            if !builder.config.json_output {
1793                builder.info("fmt check");
1794
1795                // Note: this actually sets up or downloads rustfmt, so running this step here is
1796                // load-bearing
1797                let Some(rustfmt) = builder.ensure(InternalRustfmt) else {
1798                    let inferred_rustfmt_dir = builder.initial_sysroot.join("bin");
1799                    eprintln!(
1800                        "\
1801ERROR: no `rustfmt` binary found in {PATH}
1802INFO: `rust.channel` is currently set to \"{CHAN}\"
1803HELP: if you are testing a beta branch, set `rust.channel` to \"beta\" in the `bootstrap.toml` file
1804HELP: to skip test's attempt to check tidiness, pass `--skip src/tools/tidy` to `x.py test`",
1805                        PATH = inferred_rustfmt_dir.display(),
1806                        CHAN = builder.config.channel,
1807                    );
1808                    helpers::exit_process(1);
1809                };
1810                let all = false;
1811                crate::core::build_steps::format::format(
1812                    builder,
1813                    rustfmt,
1814                    !builder.config.cmd.bless(),
1815                    all,
1816                    &[],
1817                );
1818            } else {
1819                eprintln!(
1820                    "WARNING: `--json-output` is not supported on rustfmt, formatting will be skipped"
1821                );
1822            }
1823        }
1824
1825        builder.info("tidy check");
1826        let ctx = builder.as_ref();
1827        let mut cmd = cmd.delay_failure();
1828        // spawn child in background so we can do additional work in parallel
1829        let tidy = cmd.start(ctx);
1830
1831        builder.info("x.py completions check");
1832        let completion_paths = get_completion_paths(builder);
1833        if builder.config.cmd.bless() {
1834            builder.ensure(crate::core::build_steps::run::GenerateCompletions);
1835        } else if completion_paths
1836            .into_iter()
1837            .any(|(shell, path)| get_completion(shell, &path).is_some())
1838        {
1839            eprintln!(
1840                "x.py completions were changed; run `x.py run generate-completions` to update them"
1841            );
1842            helpers::exit_process(1);
1843        }
1844
1845        // now wait for the child to complete
1846        tidy.wait_for_output(ctx);
1847
1848        builder.info("x.py help check");
1849        if builder.config.cmd.bless() {
1850            builder.ensure(crate::core::build_steps::run::GenerateHelp);
1851        } else {
1852            let help_path = get_help_path(builder);
1853            let cur_help = std::fs::read_to_string(&help_path).unwrap_or_else(|err| {
1854                eprintln!("couldn't read {}: {}", help_path.display(), err);
1855                helpers::exit_process(1);
1856            });
1857            let new_help = top_level_help();
1858
1859            if new_help != cur_help {
1860                eprintln!("x.py help was changed; run `x.py run generate-help` to update it");
1861                helpers::exit_process(1);
1862            }
1863        }
1864    }
1865
1866    fn metadata(&self) -> Option<StepMetadata> {
1867        Some(StepMetadata::test("tidy", TargetSelection::default()))
1868    }
1869}
1870
1871/// Runs `cargo test` on the `src/tools/run-make-support` crate.
1872/// That crate is used by run-make tests.
1873#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1874pub struct CrateRunMakeSupport {
1875    host: TargetSelection,
1876}
1877
1878impl CommandLineStep for CrateRunMakeSupport {
1879    type Output = ();
1880    const IS_HOST: bool = true;
1881
1882    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1883        run.path("src/tools/run-make-support")
1884    }
1885
1886    fn make_run(run: RunConfig<'_>) {
1887        run.builder.ensure(CrateRunMakeSupport { host: run.target });
1888    }
1889
1890    /// Runs `cargo test` for run-make-support.
1891    fn run(self, builder: &Builder<'_>) {
1892        let host = self.host;
1893        let compiler = builder.compiler(0, host);
1894        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
1895
1896        let mut cargo = tool::prepare_tool_cargo(
1897            builder,
1898            compiler,
1899            Mode::ToolBootstrap,
1900            host,
1901            Kind::Test,
1902            "src/tools/run-make-support",
1903            SourceType::InTree,
1904            &[],
1905        );
1906        cargo.allow_features("test");
1907        run_cargo_test(
1908            cargo,
1909            &[],
1910            &[],
1911            "run-make-support self test",
1912            host,
1913            builder,
1914            record_failed_tests,
1915        );
1916    }
1917}
1918
1919#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1920pub struct CrateBuildHelper {
1921    host: TargetSelection,
1922}
1923
1924impl CommandLineStep for CrateBuildHelper {
1925    type Output = ();
1926    const IS_HOST: bool = true;
1927
1928    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1929        run.path("src/build_helper")
1930    }
1931
1932    fn make_run(run: RunConfig<'_>) {
1933        run.builder.ensure(CrateBuildHelper { host: run.target });
1934    }
1935
1936    /// Runs `cargo test` for build_helper.
1937    fn run(self, builder: &Builder<'_>) {
1938        let host = self.host;
1939        let compiler = builder.compiler(0, host);
1940        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
1941
1942        let mut cargo = tool::prepare_tool_cargo(
1943            builder,
1944            compiler,
1945            Mode::ToolBootstrap,
1946            host,
1947            Kind::Test,
1948            "src/build_helper",
1949            SourceType::InTree,
1950            &[],
1951        );
1952        cargo.allow_features("test");
1953        run_cargo_test(
1954            cargo,
1955            &[],
1956            &[],
1957            "build_helper self test",
1958            host,
1959            builder,
1960            record_failed_tests,
1961        );
1962    }
1963}
1964
1965fn testdir(builder: &Builder<'_>, host: TargetSelection) -> PathBuf {
1966    builder.out.join(host).join("test")
1967}
1968
1969/// Declares a test step that invokes compiletest on a particular test suite.
1970macro_rules! test {
1971    (
1972        $( #[$attr:meta] )* // allow docstrings and attributes
1973        $name:ident {
1974            path: $path:expr,
1975            mode: $mode:expr,
1976            suite: $suite:expr,
1977            default: $default:expr
1978            $( , IS_HOST: $IS_HOST:expr )? // default: false
1979            $( , compare_mode: $compare_mode:expr )? // default: None
1980            $( , )? // optional trailing comma
1981        }
1982    ) => {
1983        $( #[$attr] )*
1984        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
1985        pub struct $name {
1986            test_compiler: Compiler,
1987            target: TargetSelection,
1988        }
1989
1990        impl CommandLineStep for $name {
1991            type Output = ();
1992            const IS_HOST: bool = (const {
1993                #[allow(unused_assignments, unused_mut)]
1994                let mut value = false;
1995                $( value = $IS_HOST; )?
1996                value
1997            });
1998
1999            fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2000                run.suite_path($path)
2001            }
2002
2003            fn is_default_step(_builder: &Builder<'_>) -> bool {
2004                const { $default }
2005            }
2006
2007            fn make_run(run: RunConfig<'_>) {
2008                let test_compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
2009
2010                run.builder.ensure($name { test_compiler, target: run.target });
2011            }
2012
2013            fn run(self, builder: &Builder<'_>) {
2014                builder.ensure(Compiletest {
2015                    test_compiler: self.test_compiler,
2016                    target: self.target,
2017                    mode: const { $mode },
2018                    suite: $suite,
2019                    path: $path,
2020                    compare_mode: (const {
2021                        #[allow(unused_assignments, unused_mut)]
2022                        let mut value = None;
2023                        $( value = $compare_mode; )?
2024                        value
2025                    }),
2026                })
2027            }
2028        }
2029    };
2030}
2031
2032test!(Ui { path: "tests/ui", mode: CompiletestMode::Ui, suite: "ui", default: true });
2033
2034test!(Crashes {
2035    path: "tests/crashes",
2036    mode: CompiletestMode::Crashes,
2037    suite: "crashes",
2038    default: true,
2039});
2040
2041test!(CodegenLlvm {
2042    path: "tests/codegen-llvm",
2043    mode: CompiletestMode::Codegen,
2044    suite: "codegen-llvm",
2045    default: true
2046});
2047
2048test!(CodegenUnits {
2049    path: "tests/codegen-units",
2050    mode: CompiletestMode::CodegenUnits,
2051    suite: "codegen-units",
2052    default: true,
2053});
2054
2055test!(Incremental {
2056    path: "tests/incremental",
2057    mode: CompiletestMode::Incremental,
2058    suite: "incremental",
2059    default: true,
2060});
2061
2062test!(Debuginfo {
2063    path: "tests/debuginfo",
2064    mode: CompiletestMode::Debuginfo,
2065    suite: "debuginfo",
2066    default: true,
2067    compare_mode: Some("split-dwarf"),
2068});
2069
2070test!(UiFullDeps {
2071    path: "tests/ui-fulldeps",
2072    mode: CompiletestMode::Ui,
2073    suite: "ui-fulldeps",
2074    default: true,
2075    IS_HOST: true,
2076});
2077
2078test!(RustdocHtml {
2079    path: "tests/rustdoc-html",
2080    mode: CompiletestMode::RustdocHtml,
2081    suite: "rustdoc-html",
2082    default: true,
2083    IS_HOST: true,
2084});
2085test!(RustdocUi {
2086    path: "tests/rustdoc-ui",
2087    mode: CompiletestMode::Ui,
2088    suite: "rustdoc-ui",
2089    default: true,
2090    IS_HOST: true,
2091});
2092
2093test!(RustdocJson {
2094    path: "tests/rustdoc-json",
2095    mode: CompiletestMode::RustdocJson,
2096    suite: "rustdoc-json",
2097    default: true,
2098    IS_HOST: true,
2099});
2100
2101test!(Pretty {
2102    path: "tests/pretty",
2103    mode: CompiletestMode::Pretty,
2104    suite: "pretty",
2105    default: true,
2106    IS_HOST: true,
2107});
2108
2109test!(RunMake {
2110    path: "tests/run-make",
2111    mode: CompiletestMode::RunMake,
2112    suite: "run-make",
2113    default: true,
2114});
2115test!(RunMakeCargo {
2116    path: "tests/run-make-cargo",
2117    mode: CompiletestMode::RunMake,
2118    suite: "run-make-cargo",
2119    default: true
2120});
2121test!(BuildStd {
2122    path: "tests/build-std",
2123    mode: CompiletestMode::RunMake,
2124    suite: "build-std",
2125    default: false
2126});
2127
2128test!(AssemblyGcc {
2129    path: "tests/assembly-gcc",
2130    mode: CompiletestMode::Assembly,
2131    suite: "assembly-gcc",
2132    default: true
2133});
2134test!(AssemblyLlvm {
2135    path: "tests/assembly-llvm",
2136    mode: CompiletestMode::Assembly,
2137    suite: "assembly-llvm",
2138    default: true
2139});
2140
2141/// Runs the coverage test suite at `tests/coverage` in some or all of the
2142/// coverage test modes.
2143#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2144pub struct Coverage {
2145    pub compiler: Compiler,
2146    pub target: TargetSelection,
2147    pub(crate) mode: CompiletestMode,
2148}
2149
2150impl Coverage {
2151    const PATH: &'static str = "tests/coverage";
2152    const SUITE: &'static str = "coverage";
2153    const ALL_MODES: &[CompiletestMode] =
2154        &[CompiletestMode::CoverageMap, CompiletestMode::CoverageRun];
2155
2156    fn new(run: &RunConfig<'_>, mode: CompiletestMode) -> Self {
2157        let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
2158        let target = run.target;
2159        Coverage { compiler, target, mode }
2160    }
2161}
2162
2163impl CommandLineStep for Coverage {
2164    type Output = ();
2165    /// Compiletest will automatically skip the "coverage-run" tests if necessary.
2166    const IS_HOST: bool = false;
2167
2168    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2169        // Handle these invocation styles:
2170        // - `./x test` (including coverage tests)
2171        // - `./x test coverage`
2172        // - `./x test tests/coverage`
2173        // - `./x test tests/coverage/trivial.rs`
2174        // - `./x test tests/coverage/trivial.rs --skip=coverage-run`
2175        run.suite_path(Coverage::PATH)
2176    }
2177
2178    fn is_default_step(_builder: &Builder<'_>) -> bool {
2179        true
2180    }
2181
2182    fn make_run(run: RunConfig<'_>) {
2183        // Run the tests in all coverage-test modes, but skip any modes that
2184        // were explicitly skipped on the command-line (e.g. `--skip=coverage-run`).
2185        // FIXME(Zalathar): Integrate this into central skip handling somehow?
2186        for &mode in Coverage::ALL_MODES {
2187            if !run.builder.config.skip.iter().any(|skip| skip == Path::new(mode.as_str())) {
2188                run.builder.ensure(Coverage::new(&run, mode));
2189            }
2190        }
2191    }
2192
2193    fn run(self, builder: &Builder<'_>) {
2194        let Self { compiler, target, mode } = self;
2195        // Like other compiletest suite test steps, delegate to an internal
2196        // compiletest task to actually run the tests.
2197        builder.ensure(Compiletest {
2198            test_compiler: compiler,
2199            target,
2200            mode,
2201            suite: Self::SUITE,
2202            path: Self::PATH,
2203            compare_mode: None,
2204        });
2205    }
2206}
2207
2208/// Registers the `coverage-map` and `coverage-run` aliases, which are then
2209/// forwarded to the [`Coverage`] step.
2210///
2211/// If the aliases were registered by [`Coverage`] directly, they would also
2212/// be treated as implied command-line arguments when run by default.
2213/// That would cause things like `./x test --skip=tests` to still run coverage
2214/// tests, which is undesirable.
2215#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2216pub enum CoverageModeAlias {}
2217
2218impl CommandLineStep for CoverageModeAlias {
2219    type Output = ();
2220
2221    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2222        // Register the aliases "coverage-map" and "coverage-run", to handle
2223        // these invocation styles:
2224        // - `./x test coverage-map`
2225        // - `./x test coverage-run -- tests/coverage/trivial.rs`
2226        Coverage::ALL_MODES.iter().fold(run, |run, mode| run.alias(mode.as_str()))
2227    }
2228
2229    fn is_default_step(_builder: &Builder<'_>) -> bool {
2230        false
2231    }
2232
2233    fn make_run(run: RunConfig<'_>) {
2234        for path in &run.paths {
2235            let single_path = &path.assert_single_path().path;
2236            for &mode in Coverage::ALL_MODES {
2237                if single_path == Path::new(mode.as_str()) {
2238                    // Instead of creating an intermediate `CoverageModeAlias`
2239                    // step instance, delegate straight to `Coverage`.
2240                    run.builder.ensure(Coverage::new(&run, mode));
2241                }
2242            }
2243        }
2244    }
2245
2246    fn run(self, _builder: &Builder<'_>) {
2247        unreachable!("never instantiated; `make_run` creates a Coverage step instead");
2248    }
2249}
2250
2251test!(CoverageRunRustdoc {
2252    path: "tests/coverage-run-rustdoc",
2253    mode: CompiletestMode::CoverageRun,
2254    suite: "coverage-run-rustdoc",
2255    default: true,
2256    IS_HOST: true,
2257});
2258
2259// For the mir-opt suite we do not use macros, as we need custom behavior when blessing.
2260#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2261pub struct MirOpt {
2262    pub compiler: Compiler,
2263    pub target: TargetSelection,
2264}
2265
2266impl CommandLineStep for MirOpt {
2267    type Output = ();
2268
2269    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2270        run.suite_path("tests/mir-opt")
2271    }
2272
2273    fn is_default_step(_builder: &Builder<'_>) -> bool {
2274        true
2275    }
2276
2277    fn make_run(run: RunConfig<'_>) {
2278        let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
2279        run.builder.ensure(MirOpt { compiler, target: run.target });
2280    }
2281
2282    fn run(self, builder: &Builder<'_>) {
2283        let run = |target| {
2284            builder.ensure(Compiletest {
2285                test_compiler: self.compiler,
2286                target,
2287                mode: CompiletestMode::MirOpt,
2288                suite: "mir-opt",
2289                path: "tests/mir-opt",
2290                compare_mode: None,
2291            })
2292        };
2293
2294        run(self.target);
2295
2296        // Run more targets with `--bless`. But we always run the host target first, since some
2297        // tests use very specific `only` clauses that are not covered by the target set below.
2298        if builder.config.cmd.bless() {
2299            // All that we really need to do is cover all combinations of 32/64-bit and unwind/abort,
2300            // but while we're at it we might as well flex our cross-compilation support. This
2301            // selection covers all our tier 1 operating systems and architectures using only tier
2302            // 1 targets.
2303
2304            for target in ["aarch64-unknown-linux-gnu", "i686-pc-windows-msvc"] {
2305                run(TargetSelection::from_user(target));
2306            }
2307
2308            for target in ["x86_64-apple-darwin", "i686-unknown-linux-musl"] {
2309                let target = TargetSelection::from_user(target);
2310                let panic_abort_target = builder.ensure(MirOptPanicAbortSyntheticTarget {
2311                    compiler: self.compiler,
2312                    base: target,
2313                });
2314                run(panic_abort_target);
2315            }
2316        }
2317    }
2318}
2319
2320/// Executes the `compiletest` tool to run a suite of tests.
2321///
2322/// Compiles all tests with `test_compiler` for `target` with the specified
2323/// compiletest `mode` and `suite` arguments. For example `mode` can be
2324/// "mir-opt" and `suite` can be something like "debuginfo".
2325#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2326struct Compiletest {
2327    /// The compiler that we're testing.
2328    test_compiler: Compiler,
2329    target: TargetSelection,
2330    mode: CompiletestMode,
2331    suite: &'static str,
2332    path: &'static str,
2333    compare_mode: Option<&'static str>,
2334}
2335
2336impl Step for Compiletest {
2337    type Output = ();
2338
2339    fn run(self, builder: &Builder<'_>) {
2340        if builder.test_target == TestTarget::DocOnly {
2341            return;
2342        }
2343
2344        if builder.top_stage == 0 && !builder.config.compiletest_allow_stage0 {
2345            eprintln!("\
2346ERROR: `--stage 0` runs compiletest on the stage0 (precompiled) compiler, not your local changes, and will almost always cause tests to fail
2347HELP: to test the compiler or standard library, omit the stage or explicitly use `--stage 1` instead
2348NOTE: if you're sure you want to do this, please open an issue as to why. In the meantime, you can override this with `--set build.compiletest-allow-stage0=true`."
2349            );
2350            helpers::exit_process(1);
2351        }
2352
2353        let mut test_compiler = self.test_compiler;
2354        let target = self.target;
2355        let mode = self.mode;
2356        let suite = self.suite;
2357        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
2358
2359        // Path for test suite
2360        let suite_path = self.path;
2361
2362        // Skip codegen tests if they aren't enabled in configuration.
2363        if !builder.config.codegen_tests && mode == CompiletestMode::Codegen {
2364            return;
2365        }
2366
2367        // Support stage 1 ui-fulldeps. This is somewhat complicated: ui-fulldeps tests for the most
2368        // part test the *API* of the compiler, not how it compiles a given file. As a result, we
2369        // can run them against the stage 1 sources as long as we build them with the stage 0
2370        // bootstrap compiler.
2371        // NOTE: Only stage 1 is special cased because we need the rustc_private artifacts to match the
2372        // running compiler in stage 2 when plugins run.
2373        let query_compiler;
2374        let (stage, stage_id) = if suite == "ui-fulldeps" && test_compiler.stage == 1 {
2375            builder.info("Warning: running ui-fulldeps tests in stage 1 might cause failures");
2376
2377            // Even when using the stage 0 compiler, we also need to provide the stage 1 compiler
2378            // so that compiletest can query it for target information.
2379            query_compiler = Some(test_compiler);
2380            // At stage 0 (stage - 1) we are using the stage0 compiler. Using `self.target` can lead
2381            // finding an incorrect compiler path on cross-targets, as the stage 0 is always equal to
2382            // `build.build` in the configuration.
2383            let build = builder.sess.host_target;
2384            test_compiler = builder.compiler(test_compiler.stage - 1, build);
2385            let test_stage = test_compiler.stage + 1;
2386            (test_stage, format!("stage{test_stage}-{build}"))
2387        } else {
2388            query_compiler = None;
2389            let stage = test_compiler.stage;
2390            (stage, format!("stage{stage}-{target}"))
2391        };
2392
2393        if suite.ends_with("fulldeps") {
2394            builder.ensure(compile::Rustc::new(test_compiler, target));
2395        }
2396
2397        let lint_driver_compiler = query_compiler.unwrap_or(test_compiler);
2398        if suite == "ui-fulldeps" {
2399            builder.std(lint_driver_compiler, target);
2400            builder.std(lint_driver_compiler, lint_driver_compiler.host);
2401        }
2402
2403        // Build the standard library for wasm32-wasip2 (current target for wasm proc macros).
2404        if builder.config.wasm_proc_macros {
2405            builder.ensure(compile::Std::new(
2406                test_compiler,
2407                TargetSelection::from_user("wasm32-wasip2"),
2408            ));
2409        }
2410
2411        if suite == "debuginfo" {
2412            builder.ensure(dist::DebuggerScripts {
2413                sysroot: builder.sysroot(test_compiler).to_path_buf(),
2414                target,
2415            });
2416        }
2417
2418        // ensure that `libproc_macro` is available on the host.
2419        if suite == "mir-opt" {
2420            builder.ensure(
2421                compile::Std::new(test_compiler, test_compiler.host).is_for_mir_opt_tests(true),
2422            );
2423        } else {
2424            builder.std(test_compiler, test_compiler.host);
2425        }
2426
2427        let mut cmd = builder.tool_cmd(Tool::Compiletest);
2428
2429        if mode == CompiletestMode::RunMake {
2430            // Find .rlib and .rmeta files of the run-make-support library, and pass them to
2431            // compiletest
2432            let output = builder.tool(Tool::RunMakeSupport);
2433            let find = |extension: &str| -> Option<&PathBuf> {
2434                output.artifacts.iter().find_map(|p| {
2435                    // We want librun_make_support .rlib and .rmeta files
2436                    // They can be in separate directories, because Cargo currently uplifts the
2437                    // .rlib file when using -Zembed-metadata=no, but it doesn't uplift the
2438                    // .rmeta file
2439                    let filename = p.file_name()?.to_str()?;
2440                    if !filename.starts_with("librun_make_support") {
2441                        return None;
2442                    }
2443
2444                    if extension == p.extension()? { Some(p) } else { None }
2445                })
2446            };
2447            if !builder.config.dry_run() {
2448                let rlib =
2449                    find("rlib").expect(".rlib not found when compiling librun_make_support");
2450                cmd.arg("--run-make-support-rlib").arg(rlib);
2451
2452                // .rmeta might not be found if we're not using -Zembed-metadata=no
2453                if let Some(rmeta) = find("rmeta") {
2454                    cmd.arg("--run-make-support-rmeta").arg(rmeta);
2455                }
2456            }
2457        }
2458
2459        if suite == "mir-opt" {
2460            builder.ensure(compile::Std::new(test_compiler, target).is_for_mir_opt_tests(true));
2461        } else {
2462            builder.std(test_compiler, target);
2463        }
2464
2465        builder.ensure(RemoteCopyLibs { build_compiler: test_compiler, target });
2466
2467        // compiletest currently has... a lot of arguments, so let's just pass all
2468        // of them!
2469
2470        cmd.arg("--stage").arg(stage.to_string());
2471        cmd.arg("--stage-id").arg(stage_id);
2472
2473        cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(test_compiler));
2474        cmd.arg("--run-lib-path").arg(builder.sysroot_target_libdir(test_compiler, target));
2475        cmd.arg("--rustc-path").arg(builder.rustc(test_compiler));
2476        if suite == "ui-fulldeps" {
2477            let lint_driver: PathBuf = builder
2478                .ensure(tool::CompiletestLintDriver {
2479                    compiler: test_compiler,
2480                    target: test_compiler.host,
2481                })
2482                .tool_path;
2483            cmd.arg("--rustc-lint-driver-path").arg(lint_driver);
2484            cmd.arg("--rustc-lint-driver-lib-path").arg(builder.rustc_libdir(lint_driver_compiler));
2485            cmd.arg("--rustc-lint-driver-sysroot").arg(builder.sysroot(lint_driver_compiler));
2486        }
2487        if let Some(query_compiler) = query_compiler {
2488            cmd.arg("--query-rustc-path").arg(builder.rustc(query_compiler));
2489            cmd.arg("--query-rustc-lib-path").arg(builder.rustc_libdir(query_compiler));
2490        }
2491
2492        // Minicore auxiliary lib for `no_core` tests that need `core` stubs in cross-compilation
2493        // scenarios.
2494        cmd.arg("--minicore-path")
2495            .arg(builder.src.join("tests").join("auxiliary").join("minicore.rs"));
2496
2497        let is_rustdoc = suite == "rustdoc-ui" || suite == "rustdoc-js";
2498
2499        if builder.config.wasm_proc_macros {
2500            cmd.arg("--wasm-proc-macros");
2501        }
2502
2503        // There are (potentially) 2 `cargo`s to consider:
2504        //
2505        // - A "bootstrap" cargo, which is the same cargo used to build bootstrap itself, and is
2506        //   used to build the `run-make` test recipes and the `run-make-support` test library. All
2507        //   of these may not use unstable rustc/cargo features.
2508        // - An in-tree cargo, which should be considered as under test. The `run-make-cargo` test
2509        //   suite is intended to support the use case of testing the "toolchain" (that is, at the
2510        //   minimum the interaction between in-tree cargo + rustc) together.
2511        //
2512        // For build time and iteration purposes, we partition `run-make` tests which needs an
2513        // in-tree cargo (a smaller subset) versus `run-make` tests that do not into two test
2514        // suites, `run-make` and `run-make-cargo`. That way, contributors who do not need to run
2515        // the `run-make` tests that need in-tree cargo do not need to spend time building in-tree
2516        // cargo.
2517        if mode == CompiletestMode::RunMake {
2518            // We need to pass the compiler that was used to compile run-make-support,
2519            // because we have to use the same compiler to compile rmake.rs recipes.
2520            let stage0_rustc_path = builder.compiler(0, test_compiler.host);
2521            cmd.arg("--stage0-rustc-path").arg(builder.rustc(stage0_rustc_path));
2522
2523            if matches!(suite, "run-make-cargo" | "build-std") {
2524                let cargo_path = if test_compiler.stage == 0 {
2525                    // If we're using `--stage 0`, we should provide the bootstrap cargo.
2526                    builder.initial_cargo.clone()
2527                } else {
2528                    builder
2529                        .ensure(tool::Cargo::from_build_compiler(
2530                            builder.compiler(test_compiler.stage - 1, test_compiler.host),
2531                            test_compiler.host,
2532                        ))
2533                        .tool_path
2534                };
2535
2536                cmd.arg("--cargo-path").arg(cargo_path);
2537            }
2538        }
2539
2540        // Avoid depending on rustdoc when we don't need it.
2541        if matches!(
2542            mode,
2543            CompiletestMode::RunMake
2544                | CompiletestMode::RustdocHtml
2545                | CompiletestMode::RustdocJs
2546                | CompiletestMode::RustdocJson
2547        ) || matches!(suite, "rustdoc-ui" | "coverage-run-rustdoc")
2548        {
2549            cmd.arg("--rustdoc-path").arg(builder.rustdoc_for_compiler(test_compiler));
2550        }
2551
2552        if mode == CompiletestMode::RustdocJson {
2553            // Use the stage0 compiler for jsondocck
2554            let json_compiler = builder.compiler(0, builder.host_target);
2555            cmd.arg("--jsondocck-path")
2556                .arg(builder.ensure(tool::JsonDocCk { compiler: json_compiler, target }).tool_path);
2557            cmd.arg("--jsondoclint-path").arg(
2558                builder.ensure(tool::JsonDocLint { compiler: json_compiler, target }).tool_path,
2559            );
2560        }
2561
2562        if matches!(mode, CompiletestMode::CoverageMap | CompiletestMode::CoverageRun) {
2563            let coverage_dump = builder.tool_exe(Tool::CoverageDump);
2564            cmd.arg("--coverage-dump-path").arg(coverage_dump);
2565        }
2566
2567        cmd.arg("--src-root").arg(&builder.src);
2568        cmd.arg("--src-test-suite-root").arg(builder.src.join("tests").join(suite));
2569
2570        // N.B. it's important to distinguish between the *root* build directory, the *host* build
2571        // directory immediately under the root build directory, and the test-suite-specific build
2572        // directory.
2573        cmd.arg("--build-root").arg(&builder.out);
2574        cmd.arg("--build-test-suite-root").arg(testdir(builder, test_compiler.host).join(suite));
2575
2576        // When top stage is 0, that means that we're testing an externally provided compiler.
2577        // In that case we need to use its specific sysroot for tests to pass.
2578        // Note: DO NOT check if test_compiler.stage is 0, because the test compiler can be stage 0
2579        // even if the top stage is 1 (when we run the ui-fulldeps suite).
2580        let sysroot = if builder.top_stage == 0 {
2581            builder.initial_sysroot.clone()
2582        } else {
2583            builder.sysroot(test_compiler)
2584        };
2585
2586        cmd.arg("--sysroot-base").arg(sysroot);
2587
2588        cmd.arg("--suite").arg(suite);
2589        cmd.arg("--mode").arg(mode.as_str());
2590        cmd.arg("--target").arg(target.rustc_target_arg());
2591        cmd.arg("--host").arg(&*test_compiler.host.triple);
2592
2593        let filecheck = builder.ensure(llvm::FileCheck { target: builder.config.host_target });
2594        cmd.arg("--llvm-filecheck").arg(filecheck);
2595
2596        if let Some(codegen_backend) = builder.config.cmd.test_codegen_backend() {
2597            if !builder
2598                .config
2599                .enabled_codegen_backends(test_compiler.host)
2600                .contains(codegen_backend)
2601            {
2602                eprintln!(
2603                    "\
2604ERROR: No configured backend named `{name}`
2605HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?}]`",
2606                    name = codegen_backend.name(),
2607                );
2608                helpers::exit_process(1);
2609            }
2610
2611            if let CodegenBackendKind::Gcc = codegen_backend
2612                && builder.config.rustc_debug_assertions
2613            {
2614                eprintln!(
2615                    r#"WARNING: Running tests with the GCC codegen backend while rustc debug assertions are enabled. This might lead to test failures.
2616Please disable assertions with `rust.debug-assertions = false`.
2617        "#
2618                );
2619            }
2620
2621            // Tells compiletest that we want to use this codegen in particular and to override
2622            // the default one.
2623            cmd.arg("--override-codegen-backend").arg(codegen_backend.name());
2624            // Tells compiletest which codegen backend to use.
2625            // It is used to e.g. ignore tests that don't support that codegen backend.
2626            cmd.arg("--default-codegen-backend").arg(codegen_backend.name());
2627        } else {
2628            // Tells compiletest which codegen backend to use.
2629            // It is used to e.g. ignore tests that don't support that codegen backend.
2630            cmd.arg("--default-codegen-backend")
2631                .arg(builder.config.default_codegen_backend(test_compiler.host).name());
2632        }
2633        if builder.config.cmd.bypass_ignore_backends() {
2634            cmd.arg("--bypass-ignore-backends");
2635        }
2636
2637        if builder.sess.config.llvm_enzyme {
2638            cmd.arg("--has-enzyme");
2639        }
2640
2641        if builder.sess.config.llvm_offload {
2642            cmd.arg("--has-offload");
2643        }
2644
2645        if builder.sess.config.llvm_tpde {
2646            cmd.arg("--has-tpde");
2647        }
2648
2649        if builder.config.cmd.bless() {
2650            cmd.arg("--bless");
2651        }
2652
2653        if builder.config.cmd.force_rerun() {
2654            cmd.arg("--force-rerun");
2655        }
2656
2657        if builder.config.cmd.no_capture() {
2658            cmd.arg("--no-capture");
2659        }
2660
2661        let compare_mode =
2662            builder.config.cmd.compare_mode().or_else(|| {
2663                if builder.config.test_compare_mode { self.compare_mode } else { None }
2664            });
2665
2666        if let Some(ref pass) = builder.config.cmd.pass() {
2667            cmd.arg("--pass");
2668            cmd.arg(pass);
2669        }
2670
2671        if let Some(ref run) = builder.config.cmd.run() {
2672            cmd.arg("--run");
2673            cmd.arg(run);
2674        }
2675
2676        if let Some(ref nodejs) = builder.config.nodejs {
2677            cmd.arg("--nodejs").arg(nodejs);
2678        } else if mode == CompiletestMode::RustdocJs {
2679            panic!("need nodejs to run rustdoc-js suite");
2680        }
2681        if builder.config.rust_optimize_tests {
2682            cmd.arg("--optimize-tests");
2683        }
2684        if !builder.config.docs_minification {
2685            cmd.arg("--disable-minification");
2686        }
2687        if builder.config.rust_randomize_layout {
2688            cmd.arg("--rust-randomized-layout");
2689        }
2690        if builder.config.cmd.only_modified() {
2691            cmd.arg("--only-modified");
2692        }
2693        if let Some(compiletest_diff_tool) = &builder.config.compiletest_diff_tool {
2694            cmd.arg("--compiletest-diff-tool").arg(compiletest_diff_tool);
2695        }
2696
2697        let mut flags = if is_rustdoc { Vec::new() } else { vec!["-Crpath".to_string()] };
2698        flags.push(format!(
2699            "-Cdebuginfo={}",
2700            if mode == CompiletestMode::Codegen {
2701                // codegen tests typically check LLVM IR and are sensitive to additional debuginfo.
2702                // So do not apply `rust.debuginfo-level-tests` for codegen tests.
2703                if builder.config.rust_debuginfo_level_tests
2704                    != crate::core::config::DebuginfoLevel::None
2705                {
2706                    println!(
2707                        "NOTE: ignoring `rust.debuginfo-level-tests={}` for codegen tests",
2708                        builder.config.rust_debuginfo_level_tests
2709                    );
2710                }
2711                crate::core::config::DebuginfoLevel::None
2712            } else {
2713                builder.config.rust_debuginfo_level_tests
2714            }
2715        ));
2716        flags.extend(builder.config.cmd.compiletest_rustc_args().iter().map(|s| s.to_string()));
2717
2718        if suite != "mir-opt" {
2719            if let Some(linker) = builder.linker(target) {
2720                cmd.arg("--target-linker").arg(linker);
2721            }
2722            if let Some(linker) = builder.linker(test_compiler.host) {
2723                cmd.arg("--host-linker").arg(linker);
2724            }
2725        }
2726
2727        // FIXME(136096): on macOS, we get linker warnings about duplicate `-lm` flags.
2728        if suite == "ui-fulldeps" && target.ends_with("darwin") {
2729            flags.push("-Alinker_messages".into());
2730        }
2731
2732        let mut hostflags = flags.clone();
2733        hostflags.extend(linker_flags(builder, test_compiler.host, LldThreads::No));
2734
2735        let mut targetflags = flags;
2736
2737        // Provide `rust_test_helpers` for both host and target.
2738        if suite == "ui" || suite == "incremental" {
2739            let host_test_helpers = builder.ensure(TestHelpers { target: test_compiler.host });
2740            let target_helpers = builder.ensure(TestHelpers { target });
2741            hostflags.push(format!("-Lnative={}", host_test_helpers.display()));
2742            targetflags.push(format!("-Lnative={}", target_helpers.display()));
2743            if target.is_pauthtest() {
2744                // For the pauthtest target, embed an rpath to the directory containing the helper
2745                // dynamic library.
2746                targetflags.push(format!("-Clink-arg=-Wl,-rpath,{}", target_helpers.display()));
2747            }
2748        }
2749
2750        for flag in hostflags {
2751            cmd.arg("--host-rustcflags").arg(flag);
2752        }
2753        for flag in targetflags {
2754            cmd.arg("--target-rustcflags").arg(flag);
2755        }
2756        if target.is_synthetic() {
2757            cmd.arg("--target-rustcflags").arg("-Zunstable-options");
2758        }
2759
2760        cmd.arg("--python").arg(
2761            builder.config.python.as_ref().expect("python is required for running rustdoc tests"),
2762        );
2763
2764        // Discover and set some flags related to running tests on Android targets.
2765        let android = android::discover_android(builder, target);
2766        if let Some(android::Android { adb_path, adb_test_dir, android_cross_path }) = &android {
2767            cmd.arg("--adb-path").arg(adb_path);
2768            cmd.arg("--adb-test-dir").arg(adb_test_dir);
2769            cmd.arg("--android-cross-path").arg(android_cross_path);
2770        }
2771
2772        if mode == CompiletestMode::Debuginfo {
2773            if let Some(debuggers::Cdb { cdb }) = debuggers::discover_cdb(target) {
2774                cmd.arg("--cdb").arg(cdb);
2775            }
2776
2777            if let Some(debuggers::Gdb { gdb }) = debuggers::discover_gdb(builder, android.as_ref())
2778            {
2779                cmd.arg("--gdb").arg(gdb);
2780            }
2781
2782            if let Some(debuggers::Lldb { lldb_exe, lldb_version }) =
2783                debuggers::discover_lldb(builder)
2784            {
2785                cmd.arg("--lldb").arg(lldb_exe);
2786                cmd.arg("--lldb-version").arg(lldb_version);
2787            }
2788        }
2789
2790        if helpers::forcing_clang_based_tests() {
2791            let llvm = builder.ensure(llvm::Llvm { target });
2792            let clang_exe = llvm.root_dir().join("bin").join("clang");
2793            cmd.arg("--run-clang-based-tests-with").arg(clang_exe);
2794        }
2795
2796        for exclude in &builder.config.skip {
2797            cmd.arg("--skip");
2798            cmd.arg(exclude);
2799        }
2800
2801        // Get paths from cmd args
2802        let mut paths = match &builder.config.cmd {
2803            Subcommand::Test { .. } => &builder.config.paths[..],
2804            _ => &[],
2805        };
2806
2807        // in rustdoc-js mode, allow filters to be rs files or js files.
2808        // use a late-initialized Vec to avoid cloning for other modes.
2809        let mut paths_v;
2810        if mode == CompiletestMode::RustdocJs {
2811            paths_v = paths.to_vec();
2812            for p in &mut paths_v {
2813                if let Some(ext) = p.extension()
2814                    && ext == "js"
2815                {
2816                    p.set_extension("rs");
2817                }
2818            }
2819            paths = &paths_v;
2820        }
2821
2822        // Get test-args by striping suite path
2823        let mut test_args = Vec::new();
2824        for p in paths {
2825            match helpers::is_valid_test_suite_arg(p, suite_path, builder) {
2826                TestFilterCategory::Fullsuite => {
2827                    // If we also have to run the full suite, don't append _any_ test args here,
2828                    // clear the list instead and break out.
2829                    // That way none of the more specific paths make it into test_args,
2830                    // since running the whole suite will run the specific ones anyway.
2831                    test_args.clear();
2832                    break;
2833                }
2834                TestFilterCategory::Arg(a) => test_args.push(a),
2835                TestFilterCategory::Uninteresting => {}
2836            }
2837        }
2838
2839        test_args.append(&mut builder.config.test_args());
2840
2841        // On Windows, replace forward slashes in test-args by backslashes
2842        // so the correct filters are passed to libtest
2843        if cfg!(windows) {
2844            let test_args_win: Vec<String> =
2845                test_args.iter().map(|s| s.replace('/', "\\")).collect();
2846            cmd.args(&test_args_win);
2847        } else {
2848            cmd.args(&test_args);
2849        }
2850
2851        if builder.is_verbose() {
2852            cmd.arg("--verbose");
2853        }
2854
2855        if builder.config.cmd.verbose_run_make_subprocess_output() {
2856            cmd.arg("--verbose-run-make-subprocess-output");
2857        }
2858
2859        if builder.config.rustc_debug_assertions {
2860            cmd.arg("--with-rustc-debug-assertions");
2861        }
2862
2863        if builder.config.std_debug_assertions {
2864            cmd.arg("--with-std-debug-assertions");
2865        }
2866
2867        if builder.config.rust_remap_debuginfo {
2868            cmd.arg("--with-std-remap-debuginfo");
2869        }
2870
2871        cmd.arg("--jobs").arg(builder.jobs().to_string());
2872
2873        let mut llvm_components_passed = false;
2874        let mut copts_passed = false;
2875        if builder.config.llvm_enabled(test_compiler.host) {
2876            let llvm_output = builder.ensure(llvm::Llvm { target: builder.config.host_target });
2877            if !builder.config.dry_run() {
2878                let llvm_version = get_llvm_version(builder, llvm_output.llvm_config());
2879                let llvm_components = command(llvm_output.llvm_config())
2880                    .cached()
2881                    .arg("--components")
2882                    .run_capture_stdout(builder)
2883                    .stdout();
2884                // Remove trailing newline from llvm-config output.
2885                cmd.arg("--llvm-version")
2886                    .arg(llvm_version.trim())
2887                    .arg("--llvm-components")
2888                    .arg(llvm_components.trim());
2889                llvm_components_passed = true;
2890            }
2891            if !builder.config.is_rust_llvm(&llvm_output, target) {
2892                cmd.arg("--system-llvm");
2893            }
2894
2895            // Tests that use compiler libraries may inherit the `-lLLVM` link
2896            // requirement, but the `-L` library path is not propagated across
2897            // separate compilations. We can add LLVM's library path to the
2898            // rustc args as a workaround.
2899            if !builder.config.dry_run() && suite.ends_with("fulldeps") {
2900                let llvm_libdir = command(llvm_output.llvm_config())
2901                    .cached()
2902                    .arg("--libdir")
2903                    .run_capture_stdout(builder)
2904                    .stdout();
2905                let link_llvm = if target.is_msvc() {
2906                    format!("-Clink-arg=-LIBPATH:{llvm_libdir}")
2907                } else {
2908                    format!("-Clink-arg=-L{llvm_libdir}")
2909                };
2910                cmd.arg("--host-rustcflags").arg(link_llvm);
2911            }
2912
2913            if !builder.config.dry_run()
2914                && matches!(mode, CompiletestMode::RunMake | CompiletestMode::CoverageRun)
2915            {
2916                // The llvm/bin directory contains many useful cross-platform
2917                // tools. Pass the path to run-make tests so they can use them.
2918                // (The coverage-run tests also need these tools to process
2919                // coverage reports.)
2920                let llvm_bin_path = llvm_output
2921                    .llvm_config()
2922                    .parent()
2923                    .expect("Expected llvm-config to be contained in directory");
2924                assert!(llvm_bin_path.is_dir());
2925                cmd.arg("--llvm-bin-dir").arg(llvm_bin_path);
2926            }
2927
2928            if !builder.config.dry_run() && mode == CompiletestMode::RunMake {
2929                // If LLD is available, add it to the PATH
2930                if builder.config.lld_enabled {
2931                    let lld_install_root =
2932                        builder.ensure(llvm::Lld { target: builder.config.host_target });
2933
2934                    let lld_bin_path = lld_install_root.join("bin");
2935
2936                    let old_path = env::var_os("PATH").unwrap_or_default();
2937                    let new_path = env::join_paths(
2938                        std::iter::once(lld_bin_path).chain(env::split_paths(&old_path)),
2939                    )
2940                    .expect("Could not add LLD bin path to PATH");
2941                    cmd.env("PATH", new_path);
2942                }
2943            }
2944        }
2945
2946        // Only pass correct values for these flags for the `run-make` suite as it
2947        // requires that a C++ compiler was configured which isn't always the case.
2948        if !builder.config.dry_run() && mode == CompiletestMode::RunMake {
2949            let mut cflags = builder.cc_handled_cflags(target, CLang::C);
2950            cflags.extend(builder.cc_unhandled_cflags(target, CLang::C));
2951            let mut cxxflags = builder.cc_handled_cflags(target, CLang::Cxx);
2952            cxxflags.extend(builder.cc_unhandled_cflags(target, CLang::Cxx));
2953            cmd.arg("--cc")
2954                .arg(builder.cc(target))
2955                .arg("--cxx")
2956                .arg(builder.cxx(target).unwrap())
2957                .arg("--cflags")
2958                .arg(cflags.join(" "))
2959                .arg("--cxxflags")
2960                .arg(cxxflags.join(" "));
2961            copts_passed = true;
2962            if let Some(ar) = builder.ar(target) {
2963                cmd.arg("--ar").arg(ar);
2964            }
2965        }
2966
2967        if !llvm_components_passed {
2968            cmd.arg("--llvm-components").arg("");
2969        }
2970        if !copts_passed {
2971            cmd.arg("--cc")
2972                .arg("")
2973                .arg("--cxx")
2974                .arg("")
2975                .arg("--cflags")
2976                .arg("")
2977                .arg("--cxxflags")
2978                .arg("");
2979        }
2980
2981        if builder.remote_tested(target) {
2982            cmd.arg("--remote-test-client").arg(builder.tool_exe(Tool::RemoteTestClient));
2983        } else if let Some(tool) = builder.runner(target) {
2984            cmd.arg("--runner").arg(tool);
2985        }
2986
2987        if suite != "mir-opt" {
2988            // Running a C compiler on MSVC requires a few env vars to be set, to be
2989            // sure to set them here.
2990            //
2991            // Note that if we encounter `PATH` we make sure to append to our own `PATH`
2992            // rather than stomp over it.
2993            if !builder.config.dry_run() && target.is_msvc() {
2994                for (k, v) in builder.cc[&target].env() {
2995                    if k != "PATH" {
2996                        cmd.env(k, v);
2997                    }
2998                }
2999            }
3000        }
3001
3002        // Special setup to enable running with sanitizers on MSVC.
3003        if !builder.config.dry_run()
3004            && target.contains("msvc")
3005            && builder.config.sanitizers_enabled(target)
3006        {
3007            // Ignore interception failures: not all dlls in the process will have been built with
3008            // address sanitizer enabled (e.g., ntdll.dll).
3009            cmd.env("ASAN_WIN_CONTINUE_ON_INTERCEPTION_FAILURE", "1");
3010            // Add the address sanitizer runtime to the PATH - it is located next to cl.exe.
3011            let asan_runtime_path = builder.cc[&target].path().parent().unwrap().to_path_buf();
3012            let old_path = cmd
3013                .get_envs()
3014                .find_map(|(k, v)| (k == "PATH").then_some(v))
3015                .flatten()
3016                .map_or_else(|| env::var_os("PATH").unwrap_or_default(), |v| v.to_owned());
3017            let new_path = env::join_paths(
3018                env::split_paths(&old_path).chain(std::iter::once(asan_runtime_path)),
3019            )
3020            .expect("Could not add ASAN runtime path to PATH");
3021            cmd.env("PATH", new_path);
3022        }
3023
3024        // Some UI tests trigger behavior in rustc where it reads $CARGO and changes behavior if it exists.
3025        // To make the tests work that rely on it not being set, make sure it is not set.
3026        cmd.env_remove("CARGO");
3027
3028        cmd.env("RUSTC_BOOTSTRAP", "1");
3029        // Override the rustc version used in symbol hashes to reduce the amount of normalization
3030        // needed when diffing test output.
3031        cmd.env("RUSTC_FORCE_RUSTC_VERSION", "compiletest");
3032        cmd.env("DOC_RUST_LANG_ORG_CHANNEL", builder.doc_rust_lang_org_channel());
3033        builder.add_rust_test_threads(&mut cmd);
3034
3035        if builder.config.sanitizers_enabled(target) {
3036            cmd.env("RUSTC_SANITIZER_SUPPORT", "1");
3037        }
3038
3039        if builder.config.profiler_enabled(target) {
3040            cmd.arg("--profiler-runtime");
3041        }
3042
3043        cmd.env("RUST_TEST_TMPDIR", builder.tempdir());
3044
3045        if builder.config.cmd.rustfix_coverage() {
3046            cmd.arg("--rustfix-coverage");
3047        }
3048
3049        cmd.arg("--channel").arg(&builder.config.channel);
3050
3051        if !builder.config.omit_git_hash {
3052            cmd.arg("--git-hash");
3053        }
3054
3055        let git_config = builder.config.git_config();
3056        cmd.arg("--nightly-branch").arg(git_config.nightly_branch);
3057        cmd.arg("--git-merge-commit-email").arg(git_config.git_merge_commit_email);
3058
3059        #[cfg(feature = "build-metrics")]
3060        builder.metrics.begin_test_suite(
3061            build_helper::metrics::TestSuiteMetadata::Compiletest {
3062                suite: suite.into(),
3063                mode: mode.to_string(),
3064                compare_mode: None,
3065                target: self.target.triple.to_string(),
3066                host: self.test_compiler.host.triple.to_string(),
3067                stage: self.test_compiler.stage,
3068            },
3069            builder,
3070        );
3071
3072        let _group = builder.msg_test(
3073            format!("with compiletest suite={suite} mode={mode}"),
3074            target,
3075            test_compiler.stage,
3076        );
3077        try_run_tests(builder, &mut cmd, false, record_failed_tests.clone());
3078
3079        if let Some(compare_mode) = compare_mode {
3080            cmd.arg("--compare-mode").arg(compare_mode);
3081
3082            #[cfg(feature = "build-metrics")]
3083            builder.metrics.begin_test_suite(
3084                build_helper::metrics::TestSuiteMetadata::Compiletest {
3085                    suite: suite.into(),
3086                    mode: mode.to_string(),
3087                    compare_mode: Some(compare_mode.into()),
3088                    target: self.target.triple.to_string(),
3089                    host: self.test_compiler.host.triple.to_string(),
3090                    stage: self.test_compiler.stage,
3091                },
3092                builder,
3093            );
3094
3095            builder.info(&format!(
3096                "Check compiletest suite={} mode={} compare_mode={} ({} -> {})",
3097                suite, mode, compare_mode, test_compiler.host, target
3098            ));
3099            let _time = helpers::timeit(builder);
3100            try_run_tests(builder, &mut cmd, false, record_failed_tests);
3101        }
3102    }
3103
3104    fn metadata(&self) -> Option<StepMetadata> {
3105        Some(
3106            StepMetadata::test(&format!("compiletest-{}", self.suite), self.target)
3107                .stage(self.test_compiler.stage),
3108        )
3109    }
3110}
3111
3112/// Runs the documentation tests for a book in `src/doc` using the `rustdoc` of `test_compiler`.
3113#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3114struct BookTest {
3115    test_compiler: Compiler,
3116    path: PathBuf,
3117    name: &'static str,
3118    is_ext_doc: bool,
3119    dependencies: Vec<&'static str>,
3120}
3121
3122impl Step for BookTest {
3123    type Output = ();
3124
3125    fn run(self, builder: &Builder<'_>) {
3126        // External docs are different from local because:
3127        // - Some books need pre-processing by mdbook before being tested.
3128        // - They need to save their state to toolstate.
3129        // - They are only tested on the "checktools" builders.
3130        //
3131        // The local docs are tested by default, and we don't want to pay the
3132        // cost of building mdbook, so they use `rustdoc --test` directly.
3133        // Also, the unstable book is special because SUMMARY.md is generated,
3134        // so it is easier to just run `rustdoc` on its files.
3135        if self.is_ext_doc {
3136            self.run_ext_doc(builder);
3137        } else {
3138            self.run_local_doc(builder);
3139        }
3140    }
3141}
3142
3143impl BookTest {
3144    /// This runs the equivalent of `mdbook test` (via the rustbook wrapper)
3145    /// which in turn runs `rustdoc --test` on each file in the book.
3146    fn run_ext_doc(self, builder: &Builder<'_>) {
3147        let test_compiler = self.test_compiler;
3148
3149        builder.std(test_compiler, test_compiler.host);
3150
3151        // mdbook just executes a binary named "rustdoc", so we need to update
3152        // PATH so that it points to our rustdoc.
3153        let mut rustdoc_path = builder.rustdoc_for_compiler(test_compiler);
3154        rustdoc_path.pop();
3155        let old_path = env::var_os("PATH").unwrap_or_default();
3156        let new_path = env::join_paths(iter::once(rustdoc_path).chain(env::split_paths(&old_path)))
3157            .expect("could not add rustdoc to PATH");
3158
3159        let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
3160        let path = builder.src.join(&self.path);
3161        // Books often have feature-gated example text.
3162        rustbook_cmd.env("RUSTC_BOOTSTRAP", "1");
3163        rustbook_cmd.env("PATH", new_path).arg("test").arg(path);
3164
3165        // Books may also need to build dependencies. For example, `TheBook` has
3166        // code samples which use the `trpl` crate. For the `rustdoc` invocation
3167        // to find them them successfully, they need to be built first and their
3168        // paths used to generate the
3169        let libs = if !self.dependencies.is_empty() {
3170            let mut lib_paths = vec![];
3171            for dep in self.dependencies {
3172                let mode = Mode::ToolRustcPrivate;
3173                let target = builder.config.host_target;
3174                let cargo = tool::prepare_tool_cargo(
3175                    builder,
3176                    test_compiler,
3177                    mode,
3178                    target,
3179                    Kind::Build,
3180                    dep,
3181                    SourceType::Submodule,
3182                    &[],
3183                );
3184
3185                let stamp = BuildStamp::new(&builder.cargo_out(test_compiler, mode, target))
3186                    .with_prefix(PathBuf::from(dep).file_name().and_then(|v| v.to_str()).unwrap());
3187
3188                let output_paths = run_cargo(
3189                    builder,
3190                    cargo,
3191                    vec![],
3192                    &stamp,
3193                    vec![],
3194                    ArtifactKeepMode::BothRlibAndRmeta,
3195                );
3196                let directories = output_paths
3197                    .into_iter()
3198                    .filter_map(|p| p.parent().map(ToOwned::to_owned))
3199                    .fold(HashSet::new(), |mut set, dir| {
3200                        set.insert(dir);
3201                        set
3202                    });
3203
3204                lib_paths.extend(directories);
3205            }
3206            lib_paths
3207        } else {
3208            vec![]
3209        };
3210
3211        if !libs.is_empty() {
3212            let paths = libs
3213                .into_iter()
3214                .map(|path| path.into_os_string())
3215                .collect::<Vec<OsString>>()
3216                .join(OsStr::new(","));
3217            rustbook_cmd.args([OsString::from("--library-path"), paths]);
3218        }
3219
3220        builder.add_rust_test_threads(&mut rustbook_cmd);
3221        let _guard = builder.msg_test(
3222            format_args!("mdbook {}", self.path.display()),
3223            test_compiler.host,
3224            test_compiler.stage,
3225        );
3226        let _time = helpers::timeit(builder);
3227        let toolstate = if rustbook_cmd.delay_failure().run(builder) {
3228            ToolState::TestPass
3229        } else {
3230            ToolState::TestFail
3231        };
3232        builder.save_toolstate(self.name, toolstate);
3233    }
3234
3235    /// This runs `rustdoc --test` on all `.md` files in the path.
3236    fn run_local_doc(self, builder: &Builder<'_>) {
3237        let test_compiler = self.test_compiler;
3238        let host = self.test_compiler.host;
3239
3240        builder.std(test_compiler, host);
3241
3242        let _guard = builder.msg_test(
3243            format!("book {}", self.name),
3244            test_compiler.host,
3245            test_compiler.stage,
3246        );
3247
3248        // Do a breadth-first traversal of the `src/doc` directory and just run
3249        // tests for all files that end in `*.md`
3250        let mut stack = vec![builder.src.join(self.path)];
3251        let _time = helpers::timeit(builder);
3252        let mut files = Vec::new();
3253        while let Some(p) = stack.pop() {
3254            if p.is_dir() {
3255                stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
3256                continue;
3257            }
3258
3259            if p.extension().and_then(|s| s.to_str()) != Some("md") {
3260                continue;
3261            }
3262
3263            files.push(p);
3264        }
3265
3266        files.sort();
3267
3268        for file in files {
3269            markdown_test(builder, test_compiler, &file);
3270        }
3271    }
3272}
3273
3274macro_rules! test_book {
3275    ($(
3276        $name:ident, $path:expr, $book_name:expr,
3277        default=$default:expr
3278        $(,submodules = $submodules:expr)?
3279        $(,dependencies=$dependencies:expr)?
3280        ;
3281    )+) => {
3282        $(
3283            #[derive(Debug, Clone, PartialEq, Eq, Hash)]
3284            pub struct $name {
3285                test_compiler: Compiler,
3286            }
3287
3288            impl CommandLineStep for $name {
3289                type Output = ();
3290                const IS_HOST: bool = true;
3291
3292                fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
3293                    run.path($path)
3294                }
3295
3296                fn is_default_step(_builder: &Builder<'_>) -> bool {
3297                    const { $default }
3298                }
3299
3300                fn make_run(run: RunConfig<'_>) {
3301                    run.builder.ensure($name {
3302                        test_compiler: run.builder.compiler(run.builder.top_stage, run.target),
3303                    });
3304                }
3305
3306                fn run(self, builder: &Builder<'_>) {
3307                    $(
3308                        for submodule in $submodules {
3309                            builder.require_submodule(submodule, None);
3310                        }
3311                    )*
3312
3313                    let dependencies = vec![];
3314                    $(
3315                        let mut dependencies = dependencies;
3316                        for dep in $dependencies {
3317                            dependencies.push(dep);
3318                        }
3319                    )?
3320
3321                    builder.ensure(BookTest {
3322                        test_compiler: self.test_compiler,
3323                        path: PathBuf::from($path),
3324                        name: $book_name,
3325                        is_ext_doc: !$default,
3326                        dependencies,
3327                    });
3328                }
3329            }
3330        )+
3331    }
3332}
3333
3334test_book!(
3335    Nomicon, "src/doc/nomicon", "nomicon", default=false, submodules=["src/doc/nomicon"];
3336    Reference, "src/doc/reference", "reference", default=false, submodules=["src/doc/reference"];
3337    RustdocBook, "src/doc/rustdoc", "rustdoc", default=true;
3338    RustcBook, "src/doc/rustc", "rustc", default=true;
3339    RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false, submodules=["src/doc/rust-by-example"];
3340    EmbeddedBook, "src/doc/embedded-book", "embedded-book", default=false, submodules=["src/doc/embedded-book"];
3341    TheBook, "src/doc/book", "book", default=false, submodules=["src/doc/book"], dependencies=["src/doc/book/packages/trpl"];
3342    UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
3343    EditionGuide, "src/doc/edition-guide", "edition-guide", default=false, submodules=["src/doc/edition-guide"];
3344);
3345
3346#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3347pub struct ErrorIndex {
3348    compilers: RustcPrivateCompilers,
3349}
3350
3351impl CommandLineStep for ErrorIndex {
3352    type Output = ();
3353    const IS_HOST: bool = true;
3354
3355    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
3356        // Also add `error-index` here since that is what appears in the error message
3357        // when this fails.
3358        run.path("src/tools/error_index_generator").alias("error-index")
3359    }
3360
3361    fn is_default_step(_builder: &Builder<'_>) -> bool {
3362        true
3363    }
3364
3365    fn make_run(run: RunConfig<'_>) {
3366        // error_index_generator depends on librustdoc. Use the compiler that
3367        // is normally used to build rustdoc for other tests (like compiletest
3368        // tests in tests/rustdoc-html) so that it shares the same artifacts.
3369        let compilers = RustcPrivateCompilers::new(
3370            run.builder,
3371            run.builder.top_stage,
3372            run.builder.config.host_target,
3373        );
3374        run.builder.ensure(ErrorIndex { compilers });
3375    }
3376
3377    /// Runs the error index generator tool to execute the tests located in the error
3378    /// index.
3379    ///
3380    /// The `error_index_generator` tool lives in `src/tools` and is used to
3381    /// generate a markdown file from the error indexes of the code base which is
3382    /// then passed to `rustdoc --test`.
3383    fn run(self, builder: &Builder<'_>) {
3384        // The compiler that we are testing
3385        let target_compiler = self.compilers.target_compiler();
3386
3387        let dir = testdir(builder, target_compiler.host);
3388        t!(fs::create_dir_all(&dir));
3389        let output = dir.join("error-index.md");
3390
3391        let mut tool = tool::ErrorIndex::command(builder, self.compilers);
3392        tool.arg("markdown").arg(&output);
3393
3394        let guard = builder.msg_test("error-index", target_compiler.host, target_compiler.stage);
3395        let _time = helpers::timeit(builder);
3396        tool.run_capture(builder);
3397        drop(guard);
3398        // The tests themselves need to link to std, so make sure it is
3399        // available.
3400        builder.std(target_compiler, target_compiler.host);
3401        markdown_test(builder, target_compiler, &output);
3402    }
3403}
3404
3405fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) -> bool {
3406    if let Ok(contents) = fs::read_to_string(markdown)
3407        && !contents.contains("```")
3408    {
3409        return true;
3410    }
3411
3412    builder.do_if_verbose(|| println!("doc tests for: {}", markdown.display()));
3413    let mut cmd = builder.rustdoc_cmd(compiler);
3414    builder.add_rust_test_threads(&mut cmd);
3415    // FIXME(#160895): While the new solver is enabled by default on nightly,
3416    // we don't want to use it in our tests for now.
3417    cmd.arg("-Znext-solver=coherence");
3418    // allow for unstable options such as new editions
3419    cmd.arg("-Z");
3420    cmd.arg("unstable-options");
3421    cmd.arg("--test");
3422    cmd.arg(markdown);
3423    cmd.env("RUSTC_BOOTSTRAP", "1");
3424
3425    let test_args = builder.config.test_args().join(" ");
3426    cmd.arg("--test-args").arg(test_args);
3427
3428    cmd = cmd.delay_failure();
3429    if !builder.config.verbose_tests {
3430        cmd.run_capture(builder).is_success()
3431    } else {
3432        cmd.run(builder)
3433    }
3434}
3435
3436/// Runs `cargo test` for the compiler crates in `compiler/`.
3437///
3438/// (This step does not test `rustc_codegen_cranelift` or `rustc_codegen_gcc`,
3439/// which have their own separate test steps.)
3440#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3441pub struct CrateLibrustc {
3442    /// The compiler that will run unit tests and doctests on the in-tree rustc source.
3443    build_compiler: Compiler,
3444    target: TargetSelection,
3445    crates: Vec<String>,
3446}
3447
3448impl CommandLineStep for CrateLibrustc {
3449    type Output = ();
3450    const IS_HOST: bool = true;
3451
3452    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
3453        run.crate_or_deps("rustc-main").path("compiler")
3454    }
3455
3456    fn is_default_step(_builder: &Builder<'_>) -> bool {
3457        true
3458    }
3459
3460    fn make_run(run: RunConfig<'_>) {
3461        let builder = run.builder;
3462        let host = run.build_triple();
3463        let build_compiler = builder.compiler(builder.top_stage - 1, host);
3464        let crates = run.make_run_crates(Alias::Compiler);
3465
3466        builder.ensure(CrateLibrustc { build_compiler, target: run.target, crates });
3467    }
3468
3469    fn run(self, builder: &Builder<'_>) {
3470        builder.std(self.build_compiler, self.target);
3471
3472        // To actually run the tests, delegate to a copy of the `Crate` step.
3473        builder.ensure(Crate {
3474            build_compiler: self.build_compiler,
3475            target: self.target,
3476            mode: Mode::Rustc,
3477            crates: self.crates,
3478        });
3479    }
3480
3481    fn metadata(&self) -> Option<StepMetadata> {
3482        Some(StepMetadata::test("CrateLibrustc", self.target).built_by(self.build_compiler))
3483    }
3484}
3485
3486/// Given a `cargo test` subcommand, add the appropriate flags and run it.
3487///
3488/// Returns whether the test succeeded.
3489fn run_cargo_test<'a>(
3490    mut cargo: builder::Cargo,
3491    libtest_args: &[&str],
3492    crates: &[String],
3493    description: impl Into<Option<&'a str>>,
3494    target: TargetSelection,
3495    builder: &Builder<'_>,
3496    record_failed_tests: RecordFailedTests,
3497) -> bool {
3498    let compiler = cargo.compiler();
3499    let stage = match cargo.mode() {
3500        Mode::Std => compiler.stage,
3501        _ => compiler.stage + 1,
3502    };
3503
3504    // FIXME(#160895): While the new solver is enabled by default on nightly,
3505    // we don't want to use it in our tests for now.
3506    cargo.rustdocflag("-Znext-solver=coherence");
3507
3508    let mut cargo = prepare_cargo_test(cargo, libtest_args, crates, target, builder);
3509    let _time = helpers::timeit(builder);
3510
3511    let _group = description.into().and_then(|what| builder.msg_test(what, target, stage));
3512
3513    #[cfg(feature = "build-metrics")]
3514    builder.metrics.begin_test_suite(
3515        build_helper::metrics::TestSuiteMetadata::CargoPackage {
3516            crates: crates.iter().map(|c| c.to_string()).collect(),
3517            target: target.triple.to_string(),
3518            host: compiler.host.triple.to_string(),
3519            stage: compiler.stage,
3520        },
3521        builder,
3522    );
3523    add_flags_and_try_run_tests(builder, &mut cargo, record_failed_tests)
3524}
3525
3526/// Given a `cargo test` subcommand, pass it the appropriate test flags given a `builder`.
3527fn prepare_cargo_test(
3528    cargo: builder::Cargo,
3529    libtest_args: &[&str],
3530    crates: &[String],
3531    target: TargetSelection,
3532    builder: &Builder<'_>,
3533) -> BootstrapCommand {
3534    let compiler = cargo.compiler();
3535    let mut cargo: BootstrapCommand = cargo.into();
3536
3537    // Propagate `--bless` if it has not already been set/unset
3538    // Any tools that want to use this should bless if `RUSTC_BLESS` is set to
3539    // anything other than `0`.
3540    if builder.config.cmd.bless() && !cargo.get_envs().any(|v| v.0 == "RUSTC_BLESS") {
3541        cargo.env("RUSTC_BLESS", "Gesundheit");
3542    }
3543
3544    // Pass in some standard flags then iterate over the graph we've discovered
3545    // in `cargo metadata` with the maps above and figure out what `-p`
3546    // arguments need to get passed.
3547    if builder.kind == Kind::Test && !builder.fail_fast {
3548        cargo.arg("--no-fail-fast");
3549    }
3550
3551    if builder.config.json_output {
3552        cargo.arg("--message-format=json");
3553    }
3554
3555    match builder.test_target {
3556        TestTarget::AllTargets => cargo.args(["--bins", "--examples", "--tests", "--benches"]),
3557        TestTarget::Default => &mut cargo,
3558        TestTarget::DocOnly => cargo.arg("--doc"),
3559        TestTarget::Tests => cargo.arg("--tests"),
3560    };
3561
3562    for krate in crates {
3563        cargo.arg("-p").arg(krate);
3564    }
3565
3566    cargo.arg("--").args(builder.config.test_args()).args(libtest_args);
3567    if !builder.config.verbose_tests {
3568        cargo.arg("--quiet");
3569    }
3570
3571    // The tests are going to run with the *target* libraries, so we need to
3572    // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
3573    //
3574    // Note that to run the compiler we need to run with the *host* libraries,
3575    // but our wrapper scripts arrange for that to be the case anyway.
3576    //
3577    // We skip everything on Miri as then this overwrites the libdir set up
3578    // by `Cargo::new` and that actually makes things go wrong.
3579    if builder.kind != Kind::Miri {
3580        let mut dylib_paths = builder.rustc_lib_paths(compiler);
3581        dylib_paths.push(builder.sysroot_target_libdir(compiler, target));
3582        helpers::add_dylib_path(dylib_paths, &mut cargo);
3583    }
3584
3585    add_cargo_target_runner(&mut cargo, target, builder);
3586
3587    cargo
3588}
3589
3590/// Tells cargo how to execute binaries built for `target`, e.g. under an emulator or on a remote
3591/// device.
3592fn add_cargo_target_runner(
3593    cargo: &mut BootstrapCommand,
3594    target: TargetSelection,
3595    builder: &Builder<'_>,
3596) {
3597    if builder.remote_tested(target) {
3598        cargo.env(
3599            format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
3600            format!("{} run 0", builder.tool_exe(Tool::RemoteTestClient).display()),
3601        );
3602    } else if let Some(tool) = builder.runner(target) {
3603        cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)), tool);
3604    }
3605}
3606
3607/// Runs `cargo test` for standard library crates.
3608///
3609/// (Also used internally to run `cargo test` for compiler crates.)
3610///
3611/// FIXME(Zalathar): Try to split this into two separate steps: a user-visible
3612/// step for testing standard library crates, and an internal step used for both
3613/// library crates and compiler crates.
3614#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3615pub struct Crate {
3616    /// The compiler that will *build* libstd or rustc in test mode.
3617    build_compiler: Compiler,
3618    target: TargetSelection,
3619    mode: Mode,
3620    crates: Vec<String>,
3621}
3622
3623impl CommandLineStep for Crate {
3624    type Output = ();
3625
3626    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
3627        run.crate_or_deps("sysroot").crate_or_deps("coretests").crate_or_deps("alloctests")
3628    }
3629
3630    fn is_default_step(_builder: &Builder<'_>) -> bool {
3631        true
3632    }
3633
3634    fn make_run(run: RunConfig<'_>) {
3635        let builder = run.builder;
3636        let host = run.build_triple();
3637        let build_compiler = builder.compiler(builder.top_stage, host);
3638        let crates = run
3639            .paths
3640            .iter()
3641            .map(|p| builder.crate_paths[&p.assert_single_path().path].clone())
3642            .collect();
3643
3644        builder.ensure(Crate { build_compiler, target: run.target, mode: Mode::Std, crates });
3645    }
3646
3647    /// Runs all unit tests plus documentation tests for a given crate defined
3648    /// by a `Cargo.toml` (single manifest)
3649    ///
3650    /// This is what runs tests for crates like the standard library, compiler, etc.
3651    /// It essentially is the driver for running `cargo test`.
3652    ///
3653    /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
3654    /// arguments, and those arguments are discovered from `cargo metadata`.
3655    fn run(self, builder: &Builder<'_>) {
3656        let build_compiler = self.build_compiler;
3657        let target = self.target;
3658        let mode = self.mode;
3659
3660        // Prepare sysroot
3661        // See [field@compile::Std::force_recompile].
3662        builder.ensure(Std::new(build_compiler, build_compiler.host).force_recompile(true));
3663        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
3664
3665        let mut cargo = if builder.kind == Kind::Miri {
3666            if builder.top_stage == 0 {
3667                eprintln!("ERROR: `x.py miri` requires stage 1 or higher");
3668                std::process::exit(1);
3669            }
3670
3671            // Build `cargo miri test` command
3672            // (Implicitly prepares target sysroot)
3673            let mut cargo = builder::Cargo::new(
3674                builder,
3675                build_compiler,
3676                mode,
3677                SourceType::InTree,
3678                target,
3679                Kind::MiriTest,
3680            );
3681            // This hack helps bootstrap run standard library tests in Miri. The issue is as
3682            // follows: when running `cargo miri test` on libcore, cargo builds a local copy of core
3683            // and makes it a dependency of the integration test crate. This copy duplicates all the
3684            // lang items, so the build fails. (Regular testing avoids this because the sysroot is a
3685            // literal copy of what `cargo build` produces, but since Miri builds its own sysroot
3686            // this does not work for us.) So we need to make it so that the locally built libcore
3687            // contains all the items from `core`, but does not re-define them -- we want to replace
3688            // the entire crate but a re-export of the sysroot crate. We do this by swapping out the
3689            // source file: if `MIRI_REPLACE_LIBRS_IF_NOT_TEST` is set and we are building a
3690            // `lib.rs` file, and a `lib.miri.rs` file exists in the same folder, we build that
3691            // instead. But crucially we only do that for the library, not the test builds.
3692            cargo.env("MIRI_REPLACE_LIBRS_IF_NOT_TEST", "1");
3693            // std needs to be built with `-Zforce-unstable-if-unmarked`. For some reason the builder
3694            // does not set this directly, but relies on the rustc wrapper to set it, and we are not using
3695            // the wrapper -- hence we have to set it ourselves.
3696            cargo.rustflag("-Zforce-unstable-if-unmarked");
3697            // Miri is told to invoke the libtest runner and bootstrap sets unstable flags
3698            // for that runner. That only works when RUSTC_BOOTSTRAP is set. Bootstrap sets
3699            // that flag but Miri by default does not forward the host environment to the test.
3700            // Here we set up MIRIFLAGS to forward that env var.
3701            cargo.env(
3702                "MIRIFLAGS",
3703                format!(
3704                    "{} -Zmiri-env-forward=RUSTC_BOOTSTRAP",
3705                    env::var("MIRIFLAGS").unwrap_or_default()
3706                ),
3707            );
3708            cargo
3709        } else {
3710            // Also prepare a sysroot for the target.
3711            if !builder.config.is_host_target(target) {
3712                builder.ensure(compile::Std::new(build_compiler, target).force_recompile(true));
3713                builder.ensure(RemoteCopyLibs { build_compiler, target });
3714            }
3715
3716            // Build `cargo test` command
3717            builder::Cargo::new(
3718                builder,
3719                build_compiler,
3720                mode,
3721                SourceType::InTree,
3722                target,
3723                builder.kind,
3724            )
3725        };
3726
3727        match mode {
3728            Mode::Std => {
3729                if builder.kind == Kind::Miri {
3730                    // We can't use `std_cargo` as that uses `optimized-compiler-builtins` which
3731                    // needs host tools for the given target. This is similar to what `compile::Std`
3732                    // does when `is_for_mir_opt_tests` is true. There's probably a chance for
3733                    // de-duplication here... `std_cargo` should support a mode that avoids needing
3734                    // host tools.
3735                    cargo
3736                        .arg("--manifest-path")
3737                        .arg(builder.src.join("library/sysroot/Cargo.toml"));
3738                } else {
3739                    compile::std_cargo(builder, target, &mut cargo, &[]);
3740                }
3741            }
3742            Mode::Rustc => {
3743                compile::rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates);
3744            }
3745            _ => panic!("can only test libraries"),
3746        };
3747
3748        let mut crates = self.crates.clone();
3749        // The core and alloc crates can't directly be tested. We
3750        // could silently ignore them, but adding their own test
3751        // crates is less confusing for users. We still keep core and
3752        // alloc themself for doctests
3753        if crates.iter().any(|crate_| crate_ == "core") {
3754            crates.push("coretests".to_owned());
3755        }
3756        if crates.iter().any(|crate_| crate_ == "alloc") {
3757            crates.push("alloctests".to_owned());
3758        };
3759        let mut description = crate_description(&self.crates);
3760        if builder.kind == Kind::Miri {
3761            if !description.is_empty() {
3762                description.push(' ');
3763            }
3764            description.push_str("in Miri");
3765        }
3766        run_cargo_test(cargo, &[], &crates, &*description, target, builder, record_failed_tests);
3767    }
3768}
3769
3770/// Test compiler-builtins via its testcrate.
3771#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3772pub struct CompilerBuiltins {
3773    compiler: Compiler,
3774    target: TargetSelection,
3775}
3776
3777impl CommandLineStep for CompilerBuiltins {
3778    type Output = ();
3779
3780    fn is_default_step(_builder: &Builder<'_>) -> bool {
3781        true
3782    }
3783
3784    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
3785        run.alias("compiler-builtins")
3786    }
3787
3788    fn make_run(run: RunConfig<'_>) {
3789        let builder = run.builder;
3790        let host = run.build_triple();
3791        let compiler = builder.compiler(builder.top_stage, host);
3792
3793        builder.ensure(CompilerBuiltins { compiler, target: run.target });
3794    }
3795
3796    fn run(self, builder: &Builder<'_>) -> Self::Output {
3797        let compiler = self.compiler;
3798        let target = self.target;
3799        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
3800
3801        builder.ensure(compile::Std::new(compiler, compiler.host));
3802        builder.ensure(compile::Std::new(compiler, target));
3803
3804        const COMPILER_BUILTINS_ALLOW_FEATURES: &[&str] = &[
3805            // tidy-alphabetical-start
3806            "abi_custom",
3807            "asm_experimental_arch",
3808            "cfg_target_has_atomic",
3809            "compiler_builtins",
3810            "compiler_builtins_lib",
3811            "complex_numbers",
3812            "core_intrinsics",
3813            "decl_macro",
3814            "f16",
3815            "f128",
3816            "lang_items",
3817            "linkage",
3818            "macro_metavar_expr_concat",
3819            "repr_simd",
3820            "rustc_attrs",
3821            "rustc_private",
3822            "wasm_numeric_instr",
3823            // tidy-alphabetical-end
3824        ];
3825
3826        let make_cargo = |f: fn(&mut builder::Cargo) -> &mut builder::Cargo| {
3827            let mut cargo = builder::Cargo::new(
3828                builder,
3829                compiler,
3830                Mode::ToolStd,
3831                SourceType::InTree,
3832                target,
3833                Kind::Test,
3834            );
3835            // We have to run from the root directory to make offline builds work.
3836            // We have to use the combined vendored dependencies
3837            // from the `default_paths_to_vendor` function, rather than only the vendored
3838            // dependencies of the standard library, which do not contain compiler-builtins
3839            // dependencies.
3840            cargo.arg("--manifest-path");
3841            cargo.arg(builder.src.join("library").join("compiler-builtins").join("Cargo.toml"));
3842            cargo.allow_features(&COMPILER_BUILTINS_ALLOW_FEATURES.join(","));
3843            f(&mut cargo);
3844            cargo
3845        };
3846
3847        // Most tests are in the builtins-test crate.
3848        let crates = ["compiler_builtins".to_string(), "builtins-test".to_string()];
3849        let cargo = make_cargo(|c| c);
3850        run_cargo_test(
3851            cargo,
3852            &[],
3853            &crates,
3854            "compiler-builtins",
3855            target,
3856            builder,
3857            record_failed_tests.clone(),
3858        );
3859        let cargo = make_cargo(|c| c.arg("--features=c"));
3860        run_cargo_test(
3861            cargo,
3862            &[],
3863            &crates,
3864            "compiler-builtins --features c",
3865            target,
3866            builder,
3867            record_failed_tests,
3868        );
3869    }
3870}
3871
3872/// Run cargo tests for the rustdoc crate.
3873/// Rustdoc is special in various ways, which is why this step is different from `Crate`.
3874#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3875pub struct CrateRustdoc {
3876    host: TargetSelection,
3877}
3878
3879impl CommandLineStep for CrateRustdoc {
3880    type Output = ();
3881    const IS_HOST: bool = true;
3882
3883    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
3884        run.multi_path(&["src/librustdoc", "src/tools/rustdoc"])
3885    }
3886
3887    fn is_default_step(_builder: &Builder<'_>) -> bool {
3888        true
3889    }
3890
3891    fn make_run(run: RunConfig<'_>) {
3892        let builder = run.builder;
3893
3894        builder.ensure(CrateRustdoc { host: run.target });
3895    }
3896
3897    fn run(self, builder: &Builder<'_>) {
3898        let target = self.host;
3899        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
3900
3901        let compiler = if builder.download_rustc() {
3902            builder.compiler(builder.top_stage, target)
3903        } else {
3904            // Use the previous stage compiler to reuse the artifacts that are
3905            // created when running compiletest for tests/rustdoc-html. If this used
3906            // `compiler`, then it would cause rustdoc to be built *again*, which
3907            // isn't really necessary.
3908            builder.compiler_for(builder.top_stage, target, target)
3909        };
3910        // NOTE: normally `ensure(Rustc)` automatically runs `ensure(Std)` for us. However, when
3911        // using `download-rustc`, the rustc_private artifacts may be in a *different sysroot* from
3912        // the target rustdoc (`ci-rustc-sysroot` vs `stage2`). In that case, we need to ensure this
3913        // explicitly to make sure it ends up in the stage2 sysroot.
3914        builder.std(compiler, target);
3915        builder.ensure(compile::Rustc::new(compiler, target));
3916
3917        let mut cargo = tool::prepare_tool_cargo(
3918            builder,
3919            compiler,
3920            Mode::ToolRustcPrivate,
3921            target,
3922            builder.kind,
3923            "src/tools/rustdoc",
3924            SourceType::InTree,
3925            &[],
3926        );
3927        if self.host.contains("musl") {
3928            cargo.arg("'-Ctarget-feature=-crt-static'");
3929        }
3930
3931        // This is needed for running doctests on librustdoc. This is a bit of
3932        // an unfortunate interaction with how bootstrap works and how cargo
3933        // sets up the dylib path, and the fact that the doctest (in
3934        // html/markdown.rs) links to rustc-private libs. For stage1, the
3935        // compiler host dylibs (in stage1/lib) are not the same as the target
3936        // dylibs (in stage1/lib/rustlib/...). This is different from a normal
3937        // rust distribution where they are the same.
3938        //
3939        // On the cargo side, normal tests use `target_process` which handles
3940        // setting up the dylib for a *target* (stage1/lib/rustlib/... in this
3941        // case). However, for doctests it uses `rustdoc_process` which only
3942        // sets up the dylib path for the *host* (stage1/lib), which is the
3943        // wrong directory.
3944        //
3945        // Recall that we special-cased `compiler_for(top_stage)` above, so we always use stage1.
3946        //
3947        // It should be considered to just stop running doctests on
3948        // librustdoc. There is only one test, and it doesn't look too
3949        // important. There might be other ways to avoid this, but it seems
3950        // pretty convoluted.
3951        //
3952        // See also https://github.com/rust-lang/rust/issues/13983 where the
3953        // host vs target dylibs for rustdoc are consistently tricky to deal
3954        // with.
3955        //
3956        // Note that this set the host libdir for `download_rustc`, which uses a normal rust distribution.
3957        let libdir = if builder.download_rustc() {
3958            builder.rustc_libdir(compiler)
3959        } else {
3960            builder.sysroot_target_libdir(compiler, target).to_path_buf()
3961        };
3962        let mut dylib_path = dylib_path();
3963        dylib_path.insert(0, PathBuf::from(&*libdir));
3964        cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
3965
3966        run_cargo_test(
3967            cargo,
3968            &[],
3969            &["rustdoc:0.0.0".to_string()],
3970            "rustdoc",
3971            target,
3972            builder,
3973            record_failed_tests,
3974        );
3975    }
3976}
3977
3978#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3979pub struct CrateRustdocJsonTypes {
3980    build_compiler: Compiler,
3981    target: TargetSelection,
3982}
3983
3984impl CommandLineStep for CrateRustdocJsonTypes {
3985    type Output = ();
3986    const IS_HOST: bool = true;
3987
3988    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
3989        run.path("src/rustdoc-json-types")
3990    }
3991
3992    fn is_default_step(_builder: &Builder<'_>) -> bool {
3993        true
3994    }
3995
3996    fn make_run(run: RunConfig<'_>) {
3997        let builder = run.builder;
3998
3999        builder.ensure(CrateRustdocJsonTypes {
4000            build_compiler: get_tool_target_compiler(
4001                builder,
4002                ToolTargetBuildMode::Build(run.target),
4003            ),
4004            target: run.target,
4005        });
4006    }
4007
4008    fn run(self, builder: &Builder<'_>) {
4009        let target = self.target;
4010        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
4011
4012        let cargo = tool::prepare_tool_cargo(
4013            builder,
4014            self.build_compiler,
4015            Mode::ToolTarget,
4016            target,
4017            builder.kind,
4018            "src/rustdoc-json-types",
4019            SourceType::InTree,
4020            &["rkyv_0_8".to_owned()],
4021        );
4022
4023        // FIXME: this looks very wrong, libtest doesn't accept `-C` arguments and the quotes are fishy.
4024        let libtest_args = if target.contains("musl") {
4025            ["'-Ctarget-feature=-crt-static'"].as_slice()
4026        } else {
4027            &[]
4028        };
4029
4030        run_cargo_test(
4031            cargo,
4032            libtest_args,
4033            &["rustdoc-json-types".to_string()],
4034            "rustdoc-json-types",
4035            target,
4036            builder,
4037            record_failed_tests,
4038        );
4039    }
4040}
4041
4042/// Some test suites are run inside emulators or on remote devices, and most
4043/// of our test binaries are linked dynamically which means we need to ship
4044/// the standard library and such to the emulator ahead of time. This step
4045/// represents this and is a dependency of all test suites.
4046///
4047/// Most of the time this is a no-op. For some steps such as shipping data to
4048/// QEMU we have to build our own tools so we've got conditional dependencies
4049/// on those programs as well. Note that the remote test client is built for
4050/// the build target (us) and the server is built for the target.
4051#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4052pub struct RemoteCopyLibs {
4053    build_compiler: Compiler,
4054    target: TargetSelection,
4055}
4056
4057impl Step for RemoteCopyLibs {
4058    type Output = ();
4059
4060    fn run(self, builder: &Builder<'_>) {
4061        let build_compiler = self.build_compiler;
4062        let target = self.target;
4063        if !builder.remote_tested(target) {
4064            return;
4065        }
4066
4067        builder.std(build_compiler, target);
4068
4069        builder.info(&format!("REMOTE copy libs to emulator ({target})"));
4070
4071        let remote_test_server = builder.ensure(tool::RemoteTestServer { build_compiler, target });
4072
4073        // Spawn the emulator and wait for it to come online
4074        let tool = builder.tool_exe(Tool::RemoteTestClient);
4075        let mut cmd = command(&tool);
4076        cmd.arg("spawn-emulator")
4077            .arg(target.triple)
4078            .arg(&remote_test_server.tool_path)
4079            .arg(builder.tempdir());
4080        if let Some(rootfs) = builder.qemu_rootfs(target) {
4081            cmd.arg(rootfs);
4082        }
4083        cmd.run(builder);
4084
4085        // Push all our dylibs to the emulator
4086        for f in t!(builder.sysroot_target_libdir(build_compiler, target).read_dir()) {
4087            let f = t!(f);
4088            if helpers::is_dylib(&f.path()) {
4089                command(&tool).arg("push").arg(f.path()).run(builder);
4090            }
4091        }
4092    }
4093}
4094
4095#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4096pub struct Distcheck;
4097
4098impl CommandLineStep for Distcheck {
4099    type Output = ();
4100
4101    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4102        run.alias("distcheck")
4103    }
4104
4105    fn make_run(run: RunConfig<'_>) {
4106        run.builder.ensure(Distcheck);
4107    }
4108
4109    /// Runs `distcheck`, which is a collection of smoke tests:
4110    ///
4111    /// - Run `make check` from an unpacked dist tarball to make sure we can at the minimum run
4112    ///   check steps from those sources.
4113    /// - Check that selected dist components (`rust-src` only at the moment) at least have expected
4114    ///   directory shape and crate manifests that cargo can generate a lockfile from.
4115    /// - Check that we can run `cargo metadata` on the workspace in the `rustc-dev` component
4116    ///
4117    /// FIXME(#136822): dist components are under-tested.
4118    fn run(self, builder: &Builder<'_>) {
4119        // Use a temporary directory completely outside the current checkout, to avoid reusing any
4120        // local source code, built artifacts or configuration by accident
4121        let root_dir = std::env::temp_dir().join("distcheck");
4122
4123        distcheck_plain_source_tarball(builder, &root_dir.join("distcheck-rustc-src"));
4124        distcheck_rust_src(builder, &root_dir.join("distcheck-rust-src"));
4125        distcheck_rustc_dev(builder, &root_dir.join("distcheck-rustc-dev"));
4126    }
4127}
4128
4129/// Check that we can build some basic things from the plain source tarball
4130fn distcheck_plain_source_tarball(builder: &Builder<'_>, plain_src_dir: &Path) {
4131    builder.info("Distcheck plain source tarball");
4132    let plain_src_tarball = builder.ensure(dist::PlainSourceTarball);
4133    builder.clear_dir(plain_src_dir);
4134
4135    let configure_args: Vec<String> = std::env::var("DISTCHECK_CONFIGURE_ARGS")
4136        .map(|args| args.split(" ").map(|s| s.to_string()).collect::<Vec<String>>())
4137        .unwrap_or_default();
4138
4139    command("tar")
4140        .arg("-xf")
4141        .arg(plain_src_tarball.tarball())
4142        .arg("--strip-components=1")
4143        .current_dir(plain_src_dir)
4144        .run(builder);
4145    command("./configure")
4146        .arg("--set")
4147        .arg("rust.omit-git-hash=false")
4148        .arg("--set")
4149        .arg("rust.remap-debuginfo=false")
4150        .args(&configure_args)
4151        .arg("--enable-vendor")
4152        .current_dir(plain_src_dir)
4153        .run(builder);
4154    command(helpers::make(&builder.config.host_target.triple))
4155        .arg("check")
4156        // Do not run the build as if we were in CI, otherwise git would be assumed to be
4157        // present, but we build from a tarball here
4158        .env("GITHUB_ACTIONS", "0")
4159        .current_dir(plain_src_dir)
4160        .run(builder);
4161    // Mitigate pressure on small-capacity disks.
4162    builder.remove_dir(plain_src_dir);
4163}
4164
4165/// Check that rust-src has all of libstd's dependencies
4166fn distcheck_rust_src(builder: &Builder<'_>, src_dir: &Path) {
4167    builder.info("Distcheck rust-src");
4168    let src_tarball = builder.ensure(dist::Src);
4169    builder.clear_dir(src_dir);
4170
4171    command("tar")
4172        .arg("-xf")
4173        .arg(src_tarball.tarball())
4174        .arg("--strip-components=1")
4175        .current_dir(src_dir)
4176        .run(builder);
4177
4178    let toml = src_dir.join("rust-src/lib/rustlib/src/rust/library/std/Cargo.toml");
4179    command(&builder.initial_cargo)
4180        // Will read the libstd Cargo.toml
4181        // which uses the unstable `public-dependency` feature.
4182        .env("RUSTC_BOOTSTRAP", "1")
4183        .arg("generate-lockfile")
4184        .arg("--manifest-path")
4185        .arg(&toml)
4186        .current_dir(src_dir)
4187        .run(builder);
4188    // Mitigate pressure on small-capacity disks.
4189    builder.remove_dir(src_dir);
4190}
4191
4192/// Check that rustc-dev's compiler crate source code can be loaded with `cargo metadata`
4193fn distcheck_rustc_dev(builder: &Builder<'_>, dir: &Path) {
4194    builder.info("Distcheck rustc-dev");
4195    let tarball = builder.ensure(dist::RustcDev::new(builder, builder.host_target)).unwrap();
4196    builder.clear_dir(dir);
4197
4198    command("tar")
4199        .arg("-xf")
4200        .arg(tarball.tarball())
4201        .arg("--strip-components=1")
4202        .current_dir(dir)
4203        .run(builder);
4204
4205    command(&builder.initial_cargo)
4206        .arg("metadata")
4207        .arg("--manifest-path")
4208        .arg("rustc-dev/lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml")
4209        .env("RUSTC_BOOTSTRAP", "1")
4210        // We might not have a globally available `rustc` binary on CI
4211        .env("RUSTC", &builder.initial_rustc)
4212        .current_dir(dir)
4213        .run(builder);
4214    // Mitigate pressure on small-capacity disks.
4215    builder.remove_dir(dir);
4216}
4217
4218/// Runs unit tests in `bootstrap_test.py`, which test the Python parts of bootstrap.
4219#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4220pub(crate) struct BootstrapPy;
4221
4222impl CommandLineStep for BootstrapPy {
4223    type Output = ();
4224    const IS_HOST: bool = true;
4225
4226    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4227        run.alias("bootstrap-py")
4228    }
4229
4230    fn is_default_step(builder: &Builder<'_>) -> bool {
4231        // Bootstrap tests might not be perfectly self-contained and can depend
4232        // on the environment, so only run them by default in CI, not locally.
4233        // See `test::Bootstrap::should_run`.
4234        builder.config.is_running_on_ci()
4235    }
4236
4237    fn make_run(run: RunConfig<'_>) {
4238        run.builder.ensure(BootstrapPy)
4239    }
4240
4241    fn run(self, builder: &Builder<'_>) -> Self::Output {
4242        let mut check_bootstrap = command(
4243            builder.config.python.as_ref().expect("python is required for running bootstrap tests"),
4244        );
4245        check_bootstrap
4246            .args(["-m", "unittest", "bootstrap_test.py"])
4247            // Forward command-line args after `--` to unittest, for filtering etc.
4248            .args(builder.config.test_args())
4249            .env("BUILD_DIR", &builder.out)
4250            .env("BUILD_PLATFORM", builder.sess.host_target.triple)
4251            .env("BOOTSTRAP_TEST_RUSTC_BIN", &builder.initial_rustc)
4252            .env("BOOTSTRAP_TEST_CARGO_BIN", &builder.initial_cargo)
4253            .current_dir(builder.src.join("src/bootstrap/"));
4254        check_bootstrap.delay_failure().run(builder);
4255    }
4256}
4257
4258#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4259pub struct Bootstrap;
4260
4261impl CommandLineStep for Bootstrap {
4262    type Output = ();
4263    const IS_HOST: bool = true;
4264
4265    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4266        run.path("src/bootstrap")
4267    }
4268
4269    fn is_default_step(builder: &Builder<'_>) -> bool {
4270        // Bootstrap tests might not be perfectly self-contained and can depend on the external
4271        // environment, submodules that are checked out, etc.
4272        // Therefore we only run them by default on CI.
4273        builder.config.is_running_on_ci()
4274    }
4275
4276    /// Tests the build system itself.
4277    fn run(self, builder: &Builder<'_>) {
4278        let host = builder.config.host_target;
4279        let build_compiler = builder.compiler(0, host);
4280        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
4281
4282        let mut cargo = tool::prepare_tool_cargo(
4283            builder,
4284            build_compiler,
4285            Mode::ToolBootstrap,
4286            host,
4287            Kind::Test,
4288            "src/bootstrap",
4289            SourceType::InTree,
4290            &[],
4291        );
4292
4293        cargo.release_build(false);
4294
4295        cargo
4296            .rustflag("-Cdebuginfo=2")
4297            .env("CARGO_TARGET_DIR", builder.out.join("bootstrap"))
4298            // Needed for insta to correctly write pending snapshots to the right directories.
4299            .env("INSTA_WORKSPACE_ROOT", &builder.src)
4300            .env("RUSTC_BOOTSTRAP", "1");
4301
4302        if builder.config.cmd.bless() {
4303            // Tell `expect-test` to automatically bless any failing `.snap` files
4304            // and inline snapshots.
4305            cargo.env("UPDATE_EXPECT", "1");
4306        }
4307
4308        run_cargo_test(cargo, &[], &[], None, host, builder, record_failed_tests);
4309    }
4310
4311    fn make_run(run: RunConfig<'_>) {
4312        run.builder.ensure(Bootstrap);
4313    }
4314}
4315
4316fn get_compiler_to_test(builder: &Builder<'_>, target: TargetSelection) -> Compiler {
4317    builder.compiler(builder.top_stage, target)
4318}
4319
4320/// Tests the Platform Support page in the rustc book.
4321/// `test_compiler` is used to query the actual targets that are checked.
4322#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4323pub struct TierCheck {
4324    test_compiler: Compiler,
4325}
4326
4327impl CommandLineStep for TierCheck {
4328    type Output = ();
4329    const IS_HOST: bool = true;
4330
4331    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4332        run.path("src/tools/tier-check")
4333    }
4334
4335    fn is_default_step(_builder: &Builder<'_>) -> bool {
4336        true
4337    }
4338
4339    fn make_run(run: RunConfig<'_>) {
4340        run.builder
4341            .ensure(TierCheck { test_compiler: get_compiler_to_test(run.builder, run.target) });
4342    }
4343
4344    fn run(self, builder: &Builder<'_>) {
4345        let tool_build_compiler = builder.compiler(0, builder.host_target);
4346
4347        let mut cargo = tool::prepare_tool_cargo(
4348            builder,
4349            tool_build_compiler,
4350            Mode::ToolBootstrap,
4351            tool_build_compiler.host,
4352            Kind::Run,
4353            "src/tools/tier-check",
4354            SourceType::InTree,
4355            &[],
4356        );
4357        cargo.arg(builder.src.join("src/doc/rustc/src/platform-support.md"));
4358        cargo.arg(builder.rustc(self.test_compiler));
4359
4360        let _guard = builder.msg_test(
4361            "platform support check",
4362            self.test_compiler.host,
4363            self.test_compiler.stage,
4364        );
4365        BootstrapCommand::from(cargo).delay_failure().run(builder);
4366    }
4367
4368    fn metadata(&self) -> Option<StepMetadata> {
4369        Some(StepMetadata::test("tier-check", self.test_compiler.host))
4370    }
4371}
4372
4373#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4374pub struct LintDocs {
4375    build_compiler: Compiler,
4376    target: TargetSelection,
4377}
4378
4379impl CommandLineStep for LintDocs {
4380    type Output = ();
4381    const IS_HOST: bool = true;
4382
4383    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4384        run.path("src/tools/lint-docs")
4385    }
4386
4387    fn is_default_step(builder: &Builder<'_>) -> bool {
4388        // Lint docs tests might not work with stage 1, so do not run this test by default in
4389        // `x test` below stage 2.
4390        builder.top_stage >= 2
4391    }
4392
4393    fn make_run(run: RunConfig<'_>) {
4394        if run.builder.top_stage < 2 {
4395            eprintln!("WARNING: lint-docs tests might not work below stage 2");
4396        }
4397
4398        run.builder.ensure(LintDocs {
4399            build_compiler: prepare_doc_compiler(
4400                run.builder,
4401                run.builder.config.host_target,
4402                run.builder.top_stage,
4403            ),
4404            target: run.target,
4405        });
4406    }
4407
4408    /// Tests that the lint examples in the rustc book generate the correct
4409    /// lints and have the expected format.
4410    fn run(self, builder: &Builder<'_>) {
4411        builder.ensure(crate::core::build_steps::doc::RustcBook::validate(
4412            self.build_compiler,
4413            self.target,
4414        ));
4415    }
4416
4417    fn metadata(&self) -> Option<StepMetadata> {
4418        Some(StepMetadata::test("lint-docs", self.target).built_by(self.build_compiler))
4419    }
4420}
4421
4422#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4423pub struct RustInstaller;
4424
4425impl CommandLineStep for RustInstaller {
4426    type Output = ();
4427    const IS_HOST: bool = true;
4428
4429    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4430        run.path("src/tools/rust-installer")
4431    }
4432
4433    fn is_default_step(_builder: &Builder<'_>) -> bool {
4434        true
4435    }
4436
4437    fn make_run(run: RunConfig<'_>) {
4438        run.builder.ensure(Self);
4439    }
4440
4441    /// Ensure the version placeholder replacement tool builds
4442    fn run(self, builder: &Builder<'_>) {
4443        let bootstrap_host = builder.config.host_target;
4444        let build_compiler = builder.compiler(0, bootstrap_host);
4445        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
4446        let cargo = tool::prepare_tool_cargo(
4447            builder,
4448            build_compiler,
4449            Mode::ToolBootstrap,
4450            bootstrap_host,
4451            Kind::Test,
4452            "src/tools/rust-installer",
4453            SourceType::InTree,
4454            &[],
4455        );
4456
4457        let _guard = builder.msg_test("rust-installer", bootstrap_host, 1);
4458        run_cargo_test(cargo, &[], &[], None, bootstrap_host, builder, record_failed_tests);
4459
4460        // We currently don't support running the test.sh script outside linux(?) environments.
4461        // Eventually this should likely migrate to #[test]s in rust-installer proper rather than a
4462        // set of scripts, which will likely allow dropping this if.
4463        if bootstrap_host != "x86_64-unknown-linux-gnu" {
4464            return;
4465        }
4466
4467        let mut cmd = command(builder.src.join("src/tools/rust-installer/test.sh"));
4468        let tmpdir = testdir(builder, build_compiler.host).join("rust-installer");
4469        let _ = std::fs::remove_dir_all(&tmpdir);
4470        let _ = std::fs::create_dir_all(&tmpdir);
4471        cmd.current_dir(&tmpdir);
4472        cmd.env("CARGO_TARGET_DIR", tmpdir.join("cargo-target"));
4473        cmd.env("CARGO", &builder.initial_cargo);
4474        cmd.env("RUSTC", &builder.initial_rustc);
4475        cmd.env("TMP_DIR", &tmpdir);
4476        cmd.delay_failure().run(builder);
4477    }
4478}
4479
4480/// Compiles native (C/C++) code that is used as helper code for tests.
4481///
4482/// Returns a path to the directory where the native test helpers have been built into.
4483#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4484pub struct TestHelpers {
4485    pub target: TargetSelection,
4486}
4487
4488impl CommandLineStep for TestHelpers {
4489    type Output = PathBuf;
4490
4491    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4492        run.path("tests/auxiliary/rust_test_helpers.c")
4493    }
4494
4495    fn make_run(run: RunConfig<'_>) {
4496        run.builder.ensure(TestHelpers { target: run.target });
4497    }
4498
4499    /// Compiles the `rust_test_helpers.c` library which we used in various
4500    /// `run-pass` tests for ABI testing.
4501    fn run(self, builder: &Builder<'_>) -> Self::Output {
4502        // The x86_64-fortanix-unknown-sgx target doesn't have a working C
4503        // toolchain. However, some x86_64 ELF objects can be linked
4504        // without issues. Use this hack to compile the test helpers.
4505        let target = if self.target == "x86_64-fortanix-unknown-sgx" {
4506            TargetSelection::from_user("x86_64-unknown-linux-gnu")
4507        } else {
4508            self.target
4509        };
4510        let dst = builder.native_dir(target).join("rust-test-helpers");
4511        if builder.config.dry_run() {
4512            return dst;
4513        }
4514
4515        let src = builder.src.join("tests/auxiliary/rust_test_helpers.c");
4516        let _guard = builder.msg_unstaged(Kind::Build, "test helpers", target);
4517        t!(fs::create_dir_all(&dst));
4518
4519        if !up_to_date(&src, &dst.join("librust_test_helpers.a")) {
4520            let mut cfg = cc::Build::new();
4521
4522            // We may have found various cross-compilers a little differently due to our
4523            // extra configuration, so inform cc of these compilers. Note, though, that
4524            // on MSVC we still need cc's detection of env vars (ugh).
4525            if !target.is_msvc() {
4526                if let Some(ar) = builder.ar(target) {
4527                    cfg.archiver(ar);
4528                }
4529                cfg.compiler(builder.cc(target));
4530            }
4531            cfg.cargo_metadata(false)
4532                .out_dir(&dst)
4533                .target(&target.triple)
4534                .host(&builder.config.host_target.triple)
4535                .opt_level(0)
4536                .warnings(false)
4537                .debug(false)
4538                .file(builder.src.join("tests/auxiliary/rust_test_helpers.c"))
4539                .compile("rust_test_helpers");
4540        }
4541        if target.is_pauthtest() {
4542            let so = dst.join("librust_test_helpers.so");
4543            if up_to_date(&src, &so) {
4544                return dst;
4545            }
4546
4547            let status = Command::new(builder.cc(target))
4548                .arg("-target")
4549                .arg(target.triple)
4550                .arg("-march=armv8.3-a+pauth")
4551                .arg("-fPIC")
4552                .arg("-shared")
4553                .arg("-O0") // Use O0 to match what static library is compiled at.
4554                .arg("-o")
4555                .arg(&so)
4556                .arg(&src)
4557                .status()
4558                .unwrap_or_else(|_| panic!("Failed to run clang for {} toolchain", target.triple));
4559
4560            if !status.success() {
4561                panic!("Linking of librust_test_helpers.so failed (target: {})", target.triple);
4562            }
4563        }
4564        dst
4565    }
4566}
4567
4568#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4569pub struct CodegenCranelift {
4570    compilers: RustcPrivateCompilers,
4571    target: TargetSelection,
4572}
4573
4574impl CommandLineStep for CodegenCranelift {
4575    type Output = ();
4576    const IS_HOST: bool = true;
4577
4578    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4579        run.path("compiler/rustc_codegen_cranelift")
4580    }
4581
4582    fn is_default_step(_builder: &Builder<'_>) -> bool {
4583        true
4584    }
4585
4586    fn make_run(run: RunConfig<'_>) {
4587        let builder = run.builder;
4588        let host = run.build_triple();
4589        let compilers = RustcPrivateCompilers::new(run.builder, run.builder.top_stage, host);
4590
4591        if builder.test_target == TestTarget::DocOnly {
4592            return;
4593        }
4594
4595        if builder.download_rustc() {
4596            builder.info("CI rustc uses the default codegen backend. skipping");
4597            return;
4598        }
4599
4600        if !target_supports_cranelift_backend(run.target) {
4601            builder.info("target not supported by rustc_codegen_cranelift. skipping");
4602            return;
4603        }
4604
4605        if builder.remote_tested(run.target) {
4606            builder.info("remote testing is not supported by rustc_codegen_cranelift. skipping");
4607            return;
4608        }
4609
4610        if !builder
4611            .config
4612            .enabled_codegen_backends(run.target)
4613            .contains(&CodegenBackendKind::Cranelift)
4614        {
4615            builder.info("cranelift not in rust.codegen-backends. skipping");
4616            return;
4617        }
4618
4619        builder.ensure(CodegenCranelift { compilers, target: run.target });
4620    }
4621
4622    fn run(self, builder: &Builder<'_>) {
4623        let compilers = self.compilers;
4624        let build_compiler = compilers.build_compiler();
4625
4626        // We need to run the cranelift tests with the compiler against cranelift links to, not with
4627        // the build compiler.
4628        let target_compiler = compilers.target_compiler();
4629        let target = self.target;
4630
4631        builder.std(target_compiler, target);
4632
4633        let mut cargo = builder::Cargo::new(
4634            builder,
4635            target_compiler,
4636            Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works
4637            SourceType::InTree,
4638            target,
4639            Kind::Run,
4640        );
4641
4642        cargo.current_dir(&builder.src.join("compiler/rustc_codegen_cranelift"));
4643        cargo
4644            .arg("--manifest-path")
4645            .arg(builder.src.join("compiler/rustc_codegen_cranelift/build_system/Cargo.toml"));
4646
4647        // Avoid incremental cache issues when changing rustc
4648        cargo.env("CARGO_BUILD_INCREMENTAL", "false");
4649
4650        let _guard = builder.msg_test(
4651            "rustc_codegen_cranelift",
4652            target_compiler.host,
4653            target_compiler.stage,
4654        );
4655
4656        // FIXME handle vendoring for source tarballs before removing the --skip-test below
4657        let download_dir = builder.out.join("cg_clif_download");
4658
4659        cargo
4660            .arg("--")
4661            .arg("test")
4662            .arg("--download-dir")
4663            .arg(&download_dir)
4664            .arg("--out-dir")
4665            .arg(builder.stage_out(build_compiler, Mode::Codegen).join("cg_clif"))
4666            .arg("--no-unstable-features")
4667            .arg("--use-backend")
4668            .arg("cranelift")
4669            // Avoid having to vendor the standard library dependencies
4670            .arg("--sysroot")
4671            .arg("llvm")
4672            // These tests depend on crates that are not yet vendored
4673            // FIXME remove once vendoring is handled
4674            .arg("--skip-test")
4675            .arg("testsuite.extended_sysroot");
4676
4677        cargo.into_cmd().run(builder);
4678    }
4679
4680    fn metadata(&self) -> Option<StepMetadata> {
4681        Some(
4682            StepMetadata::test("rustc_codegen_cranelift", self.target)
4683                .built_by(self.compilers.build_compiler()),
4684        )
4685    }
4686}
4687
4688#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4689pub struct CodegenGCC {
4690    compilers: RustcPrivateCompilers,
4691    target: TargetSelection,
4692}
4693
4694impl CommandLineStep for CodegenGCC {
4695    type Output = ();
4696    const IS_HOST: bool = true;
4697
4698    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4699        run.path("compiler/rustc_codegen_gcc")
4700    }
4701
4702    fn is_default_step(_builder: &Builder<'_>) -> bool {
4703        true
4704    }
4705
4706    fn make_run(run: RunConfig<'_>) {
4707        let builder = run.builder;
4708        let host = run.build_triple();
4709        let compilers = RustcPrivateCompilers::new(run.builder, run.builder.top_stage, host);
4710
4711        if builder.test_target == TestTarget::DocOnly {
4712            return;
4713        }
4714
4715        if builder.download_rustc() {
4716            builder.info("CI rustc uses the default codegen backend. skipping");
4717            return;
4718        }
4719
4720        if builder.remote_tested(run.target) {
4721            builder.info("remote testing is not supported by rustc_codegen_gcc. skipping");
4722            return;
4723        }
4724
4725        if !builder.config.enabled_codegen_backends(run.target).contains(&CodegenBackendKind::Gcc) {
4726            builder.info("gcc not in rust.codegen-backends. skipping");
4727            return;
4728        }
4729
4730        builder.ensure(CodegenGCC { compilers, target: run.target });
4731    }
4732
4733    fn run(self, builder: &Builder<'_>) {
4734        let compilers = self.compilers;
4735        let target = self.target;
4736
4737        let gcc = builder.ensure(Gcc { target_pair: GccTargetPair::for_native_build(target) });
4738
4739        if builder.config.rustc_debug_assertions {
4740            eprintln!(
4741                "WARNING: cg_gcc tests will likely fail when debug assertions are enabled for rustc"
4742            );
4743        }
4744
4745        // We need to run the cg_gcc tests with the compiler which it links against, not with
4746        // the build compiler.
4747        let target_compiler = compilers.target_compiler();
4748
4749        builder.ensure(
4750            compile::Std::new(target_compiler, target)
4751                .extra_rust_args(&["-Csymbol-mangling-version=v0", "-Cpanic=abort"]),
4752        );
4753
4754        let _guard = builder.msg_test("rustc_codegen_gcc", target, target_compiler.stage);
4755        let mut cargo = builder::Cargo::new(
4756            builder,
4757            target_compiler,
4758            Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works
4759            SourceType::InTree,
4760            target,
4761            Kind::Run,
4762        );
4763
4764        cargo.current_dir(&builder.src.join("compiler/rustc_codegen_gcc"));
4765        cargo
4766            .arg("--manifest-path")
4767            .arg(builder.src.join("compiler/rustc_codegen_gcc/build_system/Cargo.toml"));
4768        add_cg_gcc_cargo_flags(&mut cargo, &gcc);
4769
4770        // Avoid incremental cache issues when changing rustc
4771        cargo.env("CARGO_BUILD_INCREMENTAL", "false");
4772        cargo.rustflag("-Cpanic=abort");
4773
4774        cargo
4775            // cg_gcc's build system ignores RUSTFLAGS. pass some flags through CG_RUSTFLAGS instead.
4776            .env("CG_RUSTFLAGS", "-Alinker-messages")
4777            .arg("--")
4778            .arg("test")
4779            .arg("--use-backend")
4780            .arg("gcc")
4781            .arg("--gcc-path")
4782            .arg(gcc.libgccjit().parent().unwrap())
4783            .arg("--out-dir")
4784            .arg(builder.stage_out(compilers.build_compiler(), Mode::Codegen).join("cg_gcc"))
4785            .arg("--release")
4786            .arg("--mini-tests")
4787            .arg("--std-tests");
4788
4789        cargo.args(builder.config.test_args());
4790
4791        cargo.into_cmd().run(builder);
4792    }
4793
4794    fn metadata(&self) -> Option<StepMetadata> {
4795        Some(
4796            StepMetadata::test("rustc_codegen_gcc", self.target)
4797                .built_by(self.compilers.build_compiler()),
4798        )
4799    }
4800}
4801
4802/// Test step that does two things:
4803/// - Runs `cargo test` for the `src/tools/test-float-parse` tool.
4804/// - Invokes the `test-float-parse` tool to test the standard library's
4805///   float parsing routines.
4806#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4807pub struct TestFloatParse {
4808    /// The build compiler which will build and run unit tests of `test-float-parse`, and which will
4809    /// build the `test-float-parse` tool itself.
4810    ///
4811    /// Note that the staging is a bit funny here, because this step essentially tests std, but it
4812    /// also needs to build the tool. So if we test stage1 std, we build:
4813    /// 1) stage1 rustc
4814    /// 2) Use that to build stage1 libstd
4815    /// 3) Use that to build and run *stage2* test-float-parse
4816    build_compiler: Compiler,
4817    /// Target for which we build std and test that std.
4818    target: TargetSelection,
4819}
4820
4821impl CommandLineStep for TestFloatParse {
4822    type Output = ();
4823    const IS_HOST: bool = false;
4824
4825    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4826        run.path("src/tools/test-float-parse")
4827    }
4828
4829    fn is_default_step(_builder: &Builder<'_>) -> bool {
4830        true
4831    }
4832
4833    fn make_run(run: RunConfig<'_>) {
4834        let builder = run.builder;
4835
4836        // Other targets usually run under an emulator, which is too slow to do by default.
4837        if run.target != run.build_triple() && !builder.was_invoked_explicitly::<Self>(Kind::Test) {
4838            return;
4839        }
4840
4841        builder.ensure(Self {
4842            build_compiler: builder.compiler(builder.top_stage, run.build_triple()),
4843            target: run.target,
4844        });
4845    }
4846
4847    fn run(self, builder: &Builder<'_>) {
4848        let build_compiler = self.build_compiler;
4849        let target = self.target;
4850
4851        // Build the standard library that will be tested, and a stdlib for host code
4852        builder.std(build_compiler, target);
4853        builder.std(build_compiler, builder.host_target);
4854        // Start the emulator or remote test server, if the tests need one.
4855        builder.ensure(RemoteCopyLibs { build_compiler, target });
4856        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
4857
4858        // Run any unit tests in the crate
4859        let mut cargo_test = tool::prepare_tool_cargo(
4860            builder,
4861            build_compiler,
4862            Mode::ToolStd,
4863            target,
4864            Kind::Test,
4865            "src/tools/test-float-parse",
4866            SourceType::InTree,
4867            &[],
4868        );
4869        cargo_test.allow_features(TEST_FLOAT_PARSE_ALLOW_FEATURES);
4870
4871        run_cargo_test(
4872            cargo_test,
4873            &[],
4874            &[],
4875            "test-float-parse",
4876            target,
4877            builder,
4878            record_failed_tests,
4879        );
4880
4881        // Run the actual parse tests.
4882        let mut cargo_run = tool::prepare_tool_cargo(
4883            builder,
4884            build_compiler,
4885            Mode::ToolStd,
4886            target,
4887            Kind::Run,
4888            "src/tools/test-float-parse",
4889            SourceType::InTree,
4890            &[],
4891        );
4892        cargo_run.allow_features(TEST_FLOAT_PARSE_ALLOW_FEATURES);
4893
4894        if !matches!(env::var("FLOAT_PARSE_TESTS_NO_SKIP_HUGE").as_deref(), Ok("1") | Ok("true")) {
4895            cargo_run.args(["--", "--skip-huge"]);
4896        }
4897
4898        let mut cargo_run = cargo_run.into_cmd();
4899        add_cargo_target_runner(&mut cargo_run, target, builder);
4900        cargo_run.run(builder);
4901    }
4902
4903    fn metadata(&self) -> Option<StepMetadata> {
4904        Some(StepMetadata::test("test-float-parse", self.target))
4905    }
4906}
4907
4908/// Runs the tool `src/tools/collect-license-metadata` in `ONLY_CHECK=1` mode,
4909/// which verifies that `license-metadata.json` is up-to-date and therefore
4910/// running the tool normally would not update anything.
4911#[derive(Debug, Clone, Hash, PartialEq, Eq)]
4912pub struct CollectLicenseMetadata;
4913
4914impl CommandLineStep for CollectLicenseMetadata {
4915    type Output = PathBuf;
4916    const IS_HOST: bool = true;
4917
4918    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4919        run.path("src/tools/collect-license-metadata")
4920    }
4921
4922    fn make_run(run: RunConfig<'_>) {
4923        run.builder.ensure(CollectLicenseMetadata);
4924    }
4925
4926    fn run(self, builder: &Builder<'_>) -> Self::Output {
4927        let Some(reuse) = &builder.config.reuse else {
4928            panic!("REUSE is required to collect the license metadata");
4929        };
4930
4931        let dest = builder.src.join("license-metadata.json");
4932
4933        let mut cmd = builder.tool_cmd(Tool::CollectLicenseMetadata);
4934        cmd.env("REUSE_EXE", reuse);
4935        cmd.env("DEST", &dest);
4936        cmd.env("ONLY_CHECK", "1");
4937        cmd.run(builder);
4938
4939        dest
4940    }
4941}
4942
4943#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4944pub struct RemoteTestClientTests {
4945    host: TargetSelection,
4946}
4947
4948impl CommandLineStep for RemoteTestClientTests {
4949    type Output = ();
4950    const IS_HOST: bool = true;
4951
4952    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
4953        run.path("src/tools/remote-test-client")
4954    }
4955
4956    fn is_default_step(_builder: &Builder<'_>) -> bool {
4957        true
4958    }
4959
4960    fn make_run(run: RunConfig<'_>) {
4961        run.builder.ensure(Self { host: run.target });
4962    }
4963
4964    fn run(self, builder: &Builder<'_>) {
4965        let bootstrap_host = builder.config.host_target;
4966        let compiler = builder.compiler(0, bootstrap_host);
4967        let record_failed_tests = builder.ensure(SetupFailedTestsFile);
4968
4969        let cargo = tool::prepare_tool_cargo(
4970            builder,
4971            compiler,
4972            Mode::ToolBootstrap,
4973            bootstrap_host,
4974            Kind::Test,
4975            "src/tools/remote-test-client",
4976            SourceType::InTree,
4977            &[],
4978        );
4979
4980        run_cargo_test(
4981            cargo,
4982            &[],
4983            &[],
4984            "remote-test-client",
4985            bootstrap_host,
4986            builder,
4987            record_failed_tests,
4988        );
4989    }
4990}
4991
4992fn check_if_cargo_semver_checks_is_installed(builder: &Builder<'_>) -> bool {
4993    command(&builder.initial_cargo)
4994        .allow_failure()
4995        .arg("semver-checks")
4996        .arg("--version")
4997        // Cache the output to avoid running this command more than once (per builder).
4998        .cached()
4999        .run_capture_stdout(builder)
5000        .is_success()
5001}
5002
5003/// Run cargo-semver-checks on the standard library and compare its API
5004/// versus a previous baseline, using rustdoc JSON data.
5005///
5006/// The baseline commit can be configured using `rust.stdlib-semver-baseline`.
5007/// If unset, the first upstream parent commit will be used.
5008///
5009/// Fails if a semver-breaking change is detected.
5010///
5011/// If you want to allow a breaking change in a given PR, or if cargo-semver-checks has a false
5012/// positive, modify the `src/bootstrap/stdlib-semver-check-stamp` file.
5013#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5014pub struct StdSemverCheck {
5015    build_compiler: Compiler,
5016    target: TargetSelection,
5017    /// The baseline commit that we are comparing the local stdlib API against.
5018    commit: String,
5019}
5020
5021impl CommandLineStep for StdSemverCheck {
5022    type Output = ();
5023
5024    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
5025        run.alias("std-semver-check")
5026    }
5027
5028    fn make_run(run: RunConfig<'_>) {
5029        if !check_if_cargo_semver_checks_is_installed(run.builder) {
5030            panic!("cargo-semver-checks was not found, please install it");
5031        }
5032
5033        let baseline_commit =
5034            run.builder.config.stdlib_semver_baseline.clone().unwrap_or_else(|| {
5035                match get_closest_upstream_commit(
5036                    Some(&run.builder.config.src),
5037                    &run.builder.config.git_config(),
5038                    run.builder.config.ci_env,
5039                ) {
5040                    Ok(Some(commit)) => commit,
5041                    Ok(None) => {
5042                        panic!("No baseline parent commit found for std-semver-check");
5043                    }
5044                    Err(error) => {
5045                        panic!("Cannot get baseline parent commit for std-semver-check: {error:?}");
5046                    }
5047                }
5048            });
5049
5050        run.builder.ensure(Self {
5051            build_compiler: run.builder.compiler_for_std(run.builder.top_stage),
5052            target: run.target,
5053            commit: baseline_commit,
5054        });
5055    }
5056
5057    fn run(self, builder: &Builder<'_>) {
5058        const STDLIB_SEMVER_CHECK_STAMP_PATH: &str = "src/bootstrap/stdlib-semver-check-stamp";
5059
5060        if builder.config.ci_env.is_running_in_ci()
5061            && builder.config.has_changes_from_upstream(&[STDLIB_SEMVER_CHECK_STAMP_PATH])
5062        {
5063            builder.info(&format!("Skipping stdlib semver check, because {STDLIB_SEMVER_CHECK_STAMP_PATH} was modified."));
5064            return;
5065        }
5066
5067        let Some(docs_dir) = builder.config.download_std_json_docs(self.target, &self.commit)
5068        else {
5069            return;
5070        };
5071
5072        let directory = builder.ensure(crate::core::build_steps::doc::Std::from_build_compiler(
5073            self.build_compiler,
5074            self.target,
5075            DocumentationFormat::Json,
5076        ));
5077        let baseline_dir = docs_dir.join("share").join("doc").join("rust").join("json");
5078
5079        for library in ["core", "alloc", "std"] {
5080            println!("Checking semver compatibility of {library}");
5081            let mut cmd = command(&builder.initial_cargo);
5082            cmd.arg("semver-checks")
5083                .arg("-Z")
5084                .arg("unstable-options")
5085                .arg("--stability-aware")
5086                .arg("--release-type")
5087                .arg("minor")
5088                .arg("--current-rustdoc")
5089                .arg(directory.join(format!("{library}.json")))
5090                .arg("--baseline-rustdoc")
5091                .arg(baseline_dir.join(format!("{library}.json")));
5092
5093            // We use run_capture to get the exit status
5094            let res = cmd.allow_failure().run_capture(builder);
5095            match res.status() {
5096                Some(status) if status.success() => {
5097                    println!("{}\n{}", res.stdout(), res.stderr());
5098                }
5099                // 101 marks that csc was unable to parse the JSON data, but it did not fail with a
5100                // semver breakage.
5101                Some(status) if status.code() == Some(101) => {
5102                    eprintln!(
5103                        "cargo-semver-checks was unable to process {library} (this is not a fatal error)\n{}\n{}",
5104                        res.stderr(),
5105                        res.stdout()
5106                    );
5107                }
5108                // 100 marks semver breakage
5109                Some(status) if status.code() == Some(100) => {
5110                    let error = format!(
5111                        "cargo-semver-checks found semver breakage in {library}\n{}\n{}",
5112                        res.stderr(),
5113                        res.stdout()
5114                    );
5115                    if builder.fail_fast {
5116                        eprintln!("{error}");
5117                        helpers::exit_process(1);
5118                    } else {
5119                        builder.config.exec_ctx().add_to_delay_failure(error);
5120                    }
5121                }
5122                _ => {
5123                    eprintln!("cargo-semver-checks failed.\n{}\n{}", res.stderr(), res.stdout());
5124                    helpers::exit_process(1);
5125                }
5126            }
5127        }
5128    }
5129}