]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/test.rs
Modify executable checking to be more universal
[rust.git] / src / bootstrap / test.rs
1 //! Implementation of the test-related targets of the build system.
2 //!
3 //! This file implements the various regression test suites that we execute on
4 //! our CI.
5
6 use std::env;
7 use std::ffi::OsString;
8 use std::fmt;
9 use std::fs;
10 use std::iter;
11 use std::path::{Path, PathBuf};
12 use std::process::Command;
13
14 use build_helper::{self, output, t};
15
16 use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
17 use crate::cache::{Interned, INTERNER};
18 use crate::compile;
19 use crate::config::TargetSelection;
20 use crate::dist;
21 use crate::flags::Subcommand;
22 use crate::native;
23 use crate::tool::{self, SourceType, Tool};
24 use crate::toolstate::ToolState;
25 use crate::util::{self, add_link_lib_path, dylib_path, dylib_path_var};
26 use crate::Crate as CargoCrate;
27 use crate::{envify, DocTests, GitRepo, Mode};
28
29 const ADB_TEST_DIR: &str = "/data/tmp/work";
30
31 /// The two modes of the test runner; tests or benchmarks.
32 #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord)]
33 pub enum TestKind {
34     /// Run `cargo test`.
35     Test,
36     /// Run `cargo bench`.
37     Bench,
38 }
39
40 impl From<Kind> for TestKind {
41     fn from(kind: Kind) -> Self {
42         match kind {
43             Kind::Test => TestKind::Test,
44             Kind::Bench => TestKind::Bench,
45             _ => panic!("unexpected kind in crate: {:?}", kind),
46         }
47     }
48 }
49
50 impl TestKind {
51     // Return the cargo subcommand for this test kind
52     fn subcommand(self) -> &'static str {
53         match self {
54             TestKind::Test => "test",
55             TestKind::Bench => "bench",
56         }
57     }
58 }
59
60 impl fmt::Display for TestKind {
61     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62         f.write_str(match *self {
63             TestKind::Test => "Testing",
64             TestKind::Bench => "Benchmarking",
65         })
66     }
67 }
68
69 fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> bool {
70     if !builder.fail_fast {
71         if !builder.try_run(cmd) {
72             let mut failures = builder.delayed_failures.borrow_mut();
73             failures.push(format!("{:?}", cmd));
74             return false;
75         }
76     } else {
77         builder.run(cmd);
78     }
79     true
80 }
81
82 fn try_run_quiet(builder: &Builder<'_>, cmd: &mut Command) -> bool {
83     if !builder.fail_fast {
84         if !builder.try_run_quiet(cmd) {
85             let mut failures = builder.delayed_failures.borrow_mut();
86             failures.push(format!("{:?}", cmd));
87             return false;
88         }
89     } else {
90         builder.run_quiet(cmd);
91     }
92     true
93 }
94
95 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
96 pub struct Linkcheck {
97     host: TargetSelection,
98 }
99
100 impl Step for Linkcheck {
101     type Output = ();
102     const ONLY_HOSTS: bool = true;
103     const DEFAULT: bool = true;
104
105     /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
106     ///
107     /// This tool in `src/tools` will verify the validity of all our links in the
108     /// documentation to ensure we don't have a bunch of dead ones.
109     fn run(self, builder: &Builder<'_>) {
110         let host = self.host;
111
112         builder.info(&format!("Linkcheck ({})", host));
113
114         builder.default_doc(None);
115
116         let _time = util::timeit(&builder);
117         try_run(
118             builder,
119             builder.tool_cmd(Tool::Linkchecker).arg(builder.out.join(host.triple).join("doc")),
120         );
121     }
122
123     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
124         let builder = run.builder;
125         run.path("src/tools/linkchecker").default_condition(builder.config.docs)
126     }
127
128     fn make_run(run: RunConfig<'_>) {
129         run.builder.ensure(Linkcheck { host: run.target });
130     }
131 }
132
133 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
134 pub struct Cargotest {
135     stage: u32,
136     host: TargetSelection,
137 }
138
139 impl Step for Cargotest {
140     type Output = ();
141     const ONLY_HOSTS: bool = true;
142
143     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
144         run.path("src/tools/cargotest")
145     }
146
147     fn make_run(run: RunConfig<'_>) {
148         run.builder.ensure(Cargotest { stage: run.builder.top_stage, host: run.target });
149     }
150
151     /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler.
152     ///
153     /// This tool in `src/tools` will check out a few Rust projects and run `cargo
154     /// test` to ensure that we don't regress the test suites there.
155     fn run(self, builder: &Builder<'_>) {
156         let compiler = builder.compiler(self.stage, self.host);
157         builder.ensure(compile::Rustc { compiler, target: compiler.host });
158         let cargo = builder.ensure(tool::Cargo { compiler, target: compiler.host });
159
160         // Note that this is a short, cryptic, and not scoped directory name. This
161         // is currently to minimize the length of path on Windows where we otherwise
162         // quickly run into path name limit constraints.
163         let out_dir = builder.out.join("ct");
164         t!(fs::create_dir_all(&out_dir));
165
166         let _time = util::timeit(&builder);
167         let mut cmd = builder.tool_cmd(Tool::CargoTest);
168         try_run(
169             builder,
170             cmd.arg(&cargo)
171                 .arg(&out_dir)
172                 .env("RUSTC", builder.rustc(compiler))
173                 .env("RUSTDOC", builder.rustdoc(compiler)),
174         );
175     }
176 }
177
178 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
179 pub struct Cargo {
180     stage: u32,
181     host: TargetSelection,
182 }
183
184 impl Step for Cargo {
185     type Output = ();
186     const ONLY_HOSTS: bool = true;
187
188     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
189         run.path("src/tools/cargo")
190     }
191
192     fn make_run(run: RunConfig<'_>) {
193         run.builder.ensure(Cargo { stage: run.builder.top_stage, host: run.target });
194     }
195
196     /// Runs `cargo test` for `cargo` packaged with Rust.
197     fn run(self, builder: &Builder<'_>) {
198         let compiler = builder.compiler(self.stage, self.host);
199
200         builder.ensure(tool::Cargo { compiler, target: self.host });
201         let mut cargo = tool::prepare_tool_cargo(
202             builder,
203             compiler,
204             Mode::ToolRustc,
205             self.host,
206             "test",
207             "src/tools/cargo",
208             SourceType::Submodule,
209             &[],
210         );
211
212         if !builder.fail_fast {
213             cargo.arg("--no-fail-fast");
214         }
215
216         // Don't run cross-compile tests, we may not have cross-compiled libstd libs
217         // available.
218         cargo.env("CFG_DISABLE_CROSS_TESTS", "1");
219         // Disable a test that has issues with mingw.
220         cargo.env("CARGO_TEST_DISABLE_GIT_CLI", "1");
221         // Forcibly disable tests using nightly features since any changes to
222         // those features won't be able to land.
223         cargo.env("CARGO_TEST_DISABLE_NIGHTLY", "1");
224
225         cargo.env("PATH", &path_for_cargo(builder, compiler));
226
227         try_run(builder, &mut cargo.into());
228     }
229 }
230
231 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
232 pub struct Rls {
233     stage: u32,
234     host: TargetSelection,
235 }
236
237 impl Step for Rls {
238     type Output = ();
239     const ONLY_HOSTS: bool = true;
240
241     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
242         run.path("src/tools/rls")
243     }
244
245     fn make_run(run: RunConfig<'_>) {
246         run.builder.ensure(Rls { stage: run.builder.top_stage, host: run.target });
247     }
248
249     /// Runs `cargo test` for the rls.
250     fn run(self, builder: &Builder<'_>) {
251         let stage = self.stage;
252         let host = self.host;
253         let compiler = builder.compiler(stage, host);
254
255         let build_result =
256             builder.ensure(tool::Rls { compiler, target: self.host, extra_features: Vec::new() });
257         if build_result.is_none() {
258             eprintln!("failed to test rls: could not build");
259             return;
260         }
261
262         let mut cargo = tool::prepare_tool_cargo(
263             builder,
264             compiler,
265             Mode::ToolRustc,
266             host,
267             "test",
268             "src/tools/rls",
269             SourceType::Submodule,
270             &[],
271         );
272
273         builder.add_rustc_lib_path(compiler, &mut cargo);
274         cargo.arg("--").args(builder.config.cmd.test_args());
275
276         if try_run(builder, &mut cargo.into()) {
277             builder.save_toolstate("rls", ToolState::TestPass);
278         }
279     }
280 }
281
282 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
283 pub struct Rustfmt {
284     stage: u32,
285     host: TargetSelection,
286 }
287
288 impl Step for Rustfmt {
289     type Output = ();
290     const ONLY_HOSTS: bool = true;
291
292     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
293         run.path("src/tools/rustfmt")
294     }
295
296     fn make_run(run: RunConfig<'_>) {
297         run.builder.ensure(Rustfmt { stage: run.builder.top_stage, host: run.target });
298     }
299
300     /// Runs `cargo test` for rustfmt.
301     fn run(self, builder: &Builder<'_>) {
302         let stage = self.stage;
303         let host = self.host;
304         let compiler = builder.compiler(stage, host);
305
306         let build_result = builder.ensure(tool::Rustfmt {
307             compiler,
308             target: self.host,
309             extra_features: Vec::new(),
310         });
311         if build_result.is_none() {
312             eprintln!("failed to test rustfmt: could not build");
313             return;
314         }
315
316         let mut cargo = tool::prepare_tool_cargo(
317             builder,
318             compiler,
319             Mode::ToolRustc,
320             host,
321             "test",
322             "src/tools/rustfmt",
323             SourceType::Submodule,
324             &[],
325         );
326
327         let dir = testdir(builder, compiler.host);
328         t!(fs::create_dir_all(&dir));
329         cargo.env("RUSTFMT_TEST_DIR", dir);
330
331         builder.add_rustc_lib_path(compiler, &mut cargo);
332
333         if try_run(builder, &mut cargo.into()) {
334             builder.save_toolstate("rustfmt", ToolState::TestPass);
335         }
336     }
337 }
338
339 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
340 pub struct Miri {
341     stage: u32,
342     host: TargetSelection,
343 }
344
345 impl Step for Miri {
346     type Output = ();
347     const ONLY_HOSTS: bool = true;
348
349     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
350         run.path("src/tools/miri")
351     }
352
353     fn make_run(run: RunConfig<'_>) {
354         run.builder.ensure(Miri { stage: run.builder.top_stage, host: run.target });
355     }
356
357     /// Runs `cargo test` for miri.
358     fn run(self, builder: &Builder<'_>) {
359         let stage = self.stage;
360         let host = self.host;
361         let compiler = builder.compiler(stage, host);
362
363         let miri =
364             builder.ensure(tool::Miri { compiler, target: self.host, extra_features: Vec::new() });
365         let cargo_miri = builder.ensure(tool::CargoMiri {
366             compiler,
367             target: self.host,
368             extra_features: Vec::new(),
369         });
370         if let (Some(miri), Some(_cargo_miri)) = (miri, cargo_miri) {
371             let mut cargo =
372                 builder.cargo(compiler, Mode::ToolRustc, SourceType::Submodule, host, "install");
373             cargo.arg("xargo");
374             // Configure `cargo install` path. cargo adds a `bin/`.
375             cargo.env("CARGO_INSTALL_ROOT", &builder.out);
376
377             let mut cargo = Command::from(cargo);
378             if !try_run(builder, &mut cargo) {
379                 return;
380             }
381
382             // # Run `cargo miri setup`.
383             let mut cargo = tool::prepare_tool_cargo(
384                 builder,
385                 compiler,
386                 Mode::ToolRustc,
387                 host,
388                 "run",
389                 "src/tools/miri/cargo-miri",
390                 SourceType::Submodule,
391                 &[],
392             );
393             cargo.arg("--").arg("miri").arg("setup");
394
395             // Tell `cargo miri setup` where to find the sources.
396             cargo.env("XARGO_RUST_SRC", builder.src.join("library"));
397             // Tell it where to find Miri.
398             cargo.env("MIRI", &miri);
399             // Debug things.
400             cargo.env("RUST_BACKTRACE", "1");
401             // Let cargo-miri know where xargo ended up.
402             cargo.env("XARGO_CHECK", builder.out.join("bin").join("xargo-check"));
403
404             let mut cargo = Command::from(cargo);
405             if !try_run(builder, &mut cargo) {
406                 return;
407             }
408
409             // # Determine where Miri put its sysroot.
410             // To this end, we run `cargo miri setup --print-sysroot` and capture the output.
411             // (We do this separately from the above so that when the setup actually
412             // happens we get some output.)
413             // We re-use the `cargo` from above.
414             cargo.arg("--print-sysroot");
415
416             // FIXME: Is there a way in which we can re-use the usual `run` helpers?
417             let miri_sysroot = if builder.config.dry_run {
418                 String::new()
419             } else {
420                 builder.verbose(&format!("running: {:?}", cargo));
421                 let out = cargo
422                     .output()
423                     .expect("We already ran `cargo miri setup` before and that worked");
424                 assert!(out.status.success(), "`cargo miri setup` returned with non-0 exit code");
425                 // Output is "<sysroot>\n".
426                 let stdout = String::from_utf8(out.stdout)
427                     .expect("`cargo miri setup` stdout is not valid UTF-8");
428                 let sysroot = stdout.trim_end();
429                 builder.verbose(&format!("`cargo miri setup --print-sysroot` said: {:?}", sysroot));
430                 sysroot.to_owned()
431             };
432
433             // # Run `cargo test`.
434             let mut cargo = tool::prepare_tool_cargo(
435                 builder,
436                 compiler,
437                 Mode::ToolRustc,
438                 host,
439                 "test",
440                 "src/tools/miri",
441                 SourceType::Submodule,
442                 &[],
443             );
444
445             // miri tests need to know about the stage sysroot
446             cargo.env("MIRI_SYSROOT", miri_sysroot);
447             cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
448             cargo.env("MIRI", miri);
449
450             cargo.arg("--").args(builder.config.cmd.test_args());
451
452             builder.add_rustc_lib_path(compiler, &mut cargo);
453
454             if !try_run(builder, &mut cargo.into()) {
455                 return;
456             }
457
458             // # Done!
459             builder.save_toolstate("miri", ToolState::TestPass);
460         } else {
461             eprintln!("failed to test miri: could not build");
462         }
463     }
464 }
465
466 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
467 pub struct CompiletestTest {
468     host: TargetSelection,
469 }
470
471 impl Step for CompiletestTest {
472     type Output = ();
473
474     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
475         run.path("src/tools/compiletest")
476     }
477
478     fn make_run(run: RunConfig<'_>) {
479         run.builder.ensure(CompiletestTest { host: run.target });
480     }
481
482     /// Runs `cargo test` for compiletest.
483     fn run(self, builder: &Builder<'_>) {
484         let host = self.host;
485         let compiler = builder.compiler(0, host);
486
487         let cargo = tool::prepare_tool_cargo(
488             builder,
489             compiler,
490             Mode::ToolBootstrap,
491             host,
492             "test",
493             "src/tools/compiletest",
494             SourceType::InTree,
495             &[],
496         );
497
498         try_run(builder, &mut cargo.into());
499     }
500 }
501
502 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
503 pub struct Clippy {
504     stage: u32,
505     host: TargetSelection,
506 }
507
508 impl Step for Clippy {
509     type Output = ();
510     const ONLY_HOSTS: bool = true;
511     const DEFAULT: bool = false;
512
513     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
514         run.path("src/tools/clippy")
515     }
516
517     fn make_run(run: RunConfig<'_>) {
518         run.builder.ensure(Clippy { stage: run.builder.top_stage, host: run.target });
519     }
520
521     /// Runs `cargo test` for clippy.
522     fn run(self, builder: &Builder<'_>) {
523         let stage = self.stage;
524         let host = self.host;
525         let compiler = builder.compiler(stage, host);
526
527         let clippy = builder
528             .ensure(tool::Clippy { compiler, target: self.host, extra_features: Vec::new() })
529             .expect("in-tree tool");
530         let mut cargo = tool::prepare_tool_cargo(
531             builder,
532             compiler,
533             Mode::ToolRustc,
534             host,
535             "test",
536             "src/tools/clippy",
537             SourceType::InTree,
538             &[],
539         );
540
541         // clippy tests need to know about the stage sysroot
542         cargo.env("SYSROOT", builder.sysroot(compiler));
543         cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
544         cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
545         let host_libs = builder.stage_out(compiler, Mode::ToolRustc).join(builder.cargo_dir());
546         let target_libs = builder
547             .stage_out(compiler, Mode::ToolRustc)
548             .join(&self.host.triple)
549             .join(builder.cargo_dir());
550         cargo.env("HOST_LIBS", host_libs);
551         cargo.env("TARGET_LIBS", target_libs);
552         // clippy tests need to find the driver
553         cargo.env("CLIPPY_DRIVER_PATH", clippy);
554
555         cargo.arg("--").args(builder.config.cmd.test_args());
556
557         builder.add_rustc_lib_path(compiler, &mut cargo);
558
559         builder.run(&mut cargo.into());
560     }
561 }
562
563 fn path_for_cargo(builder: &Builder<'_>, compiler: Compiler) -> OsString {
564     // Configure PATH to find the right rustc. NB. we have to use PATH
565     // and not RUSTC because the Cargo test suite has tests that will
566     // fail if rustc is not spelled `rustc`.
567     let path = builder.sysroot(compiler).join("bin");
568     let old_path = env::var_os("PATH").unwrap_or_default();
569     env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("")
570 }
571
572 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
573 pub struct RustdocTheme {
574     pub compiler: Compiler,
575 }
576
577 impl Step for RustdocTheme {
578     type Output = ();
579     const DEFAULT: bool = true;
580     const ONLY_HOSTS: bool = true;
581
582     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
583         run.path("src/tools/rustdoc-themes")
584     }
585
586     fn make_run(run: RunConfig<'_>) {
587         let compiler = run.builder.compiler(run.builder.top_stage, run.target);
588
589         run.builder.ensure(RustdocTheme { compiler });
590     }
591
592     fn run(self, builder: &Builder<'_>) {
593         let rustdoc = builder.out.join("bootstrap/debug/rustdoc");
594         let mut cmd = builder.tool_cmd(Tool::RustdocTheme);
595         cmd.arg(rustdoc.to_str().unwrap())
596             .arg(builder.src.join("src/librustdoc/html/static/themes").to_str().unwrap())
597             .env("RUSTC_STAGE", self.compiler.stage.to_string())
598             .env("RUSTC_SYSROOT", builder.sysroot(self.compiler))
599             .env("RUSTDOC_LIBDIR", builder.sysroot_libdir(self.compiler, self.compiler.host))
600             .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
601             .env("RUSTDOC_REAL", builder.rustdoc(self.compiler))
602             .env("RUSTC_BOOTSTRAP", "1");
603         if let Some(linker) = builder.linker(self.compiler.host) {
604             cmd.env("RUSTDOC_LINKER", linker);
605         }
606         if builder.is_fuse_ld_lld(self.compiler.host) {
607             cmd.env("RUSTDOC_FUSE_LD_LLD", "1");
608         }
609         try_run(builder, &mut cmd);
610     }
611 }
612
613 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
614 pub struct RustdocJSStd {
615     pub target: TargetSelection,
616 }
617
618 impl Step for RustdocJSStd {
619     type Output = ();
620     const DEFAULT: bool = true;
621     const ONLY_HOSTS: bool = true;
622
623     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
624         run.path("src/test/rustdoc-js-std")
625     }
626
627     fn make_run(run: RunConfig<'_>) {
628         run.builder.ensure(RustdocJSStd { target: run.target });
629     }
630
631     fn run(self, builder: &Builder<'_>) {
632         if let Some(ref nodejs) = builder.config.nodejs {
633             let mut command = Command::new(nodejs);
634             command
635                 .arg(builder.src.join("src/tools/rustdoc-js/tester.js"))
636                 .arg("--crate-name")
637                 .arg("std")
638                 .arg("--resource-suffix")
639                 .arg(crate::channel::CFG_RELEASE_NUM)
640                 .arg("--doc-folder")
641                 .arg(builder.doc_out(self.target))
642                 .arg("--test-folder")
643                 .arg(builder.src.join("src/test/rustdoc-js-std"));
644             builder.ensure(crate::doc::Std { target: self.target, stage: builder.top_stage });
645             builder.run(&mut command);
646         } else {
647             builder.info("No nodejs found, skipping \"src/test/rustdoc-js-std\" tests");
648         }
649     }
650 }
651
652 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
653 pub struct RustdocJSNotStd {
654     pub target: TargetSelection,
655     pub compiler: Compiler,
656 }
657
658 impl Step for RustdocJSNotStd {
659     type Output = ();
660     const DEFAULT: bool = true;
661     const ONLY_HOSTS: bool = true;
662
663     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
664         run.path("src/test/rustdoc-js")
665     }
666
667     fn make_run(run: RunConfig<'_>) {
668         let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
669         run.builder.ensure(RustdocJSNotStd { target: run.target, compiler });
670     }
671
672     fn run(self, builder: &Builder<'_>) {
673         if builder.config.nodejs.is_some() {
674             builder.ensure(Compiletest {
675                 compiler: self.compiler,
676                 target: self.target,
677                 mode: "js-doc-test",
678                 suite: "rustdoc-js",
679                 path: "src/test/rustdoc-js",
680                 compare_mode: None,
681             });
682         } else {
683             builder.info("No nodejs found, skipping \"src/test/rustdoc-js\" tests");
684         }
685     }
686 }
687
688 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
689 pub struct RustdocUi {
690     pub target: TargetSelection,
691     pub compiler: Compiler,
692 }
693
694 impl Step for RustdocUi {
695     type Output = ();
696     const DEFAULT: bool = true;
697     const ONLY_HOSTS: bool = true;
698
699     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
700         run.path("src/test/rustdoc-ui")
701     }
702
703     fn make_run(run: RunConfig<'_>) {
704         let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
705         run.builder.ensure(RustdocUi { target: run.target, compiler });
706     }
707
708     fn run(self, builder: &Builder<'_>) {
709         builder.ensure(Compiletest {
710             compiler: self.compiler,
711             target: self.target,
712             mode: "ui",
713             suite: "rustdoc-ui",
714             path: "src/test/rustdoc-ui",
715             compare_mode: None,
716         })
717     }
718 }
719
720 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
721 pub struct Tidy;
722
723 impl Step for Tidy {
724     type Output = ();
725     const DEFAULT: bool = true;
726     const ONLY_HOSTS: bool = true;
727
728     /// Runs the `tidy` tool.
729     ///
730     /// This tool in `src/tools` checks up on various bits and pieces of style and
731     /// otherwise just implements a few lint-like checks that are specific to the
732     /// compiler itself.
733     ///
734     /// Once tidy passes, this step also runs `fmt --check` if tests are being run
735     /// for the `dev` or `nightly` channels.
736     fn run(self, builder: &Builder<'_>) {
737         let mut cmd = builder.tool_cmd(Tool::Tidy);
738         cmd.arg(&builder.src);
739         cmd.arg(&builder.initial_cargo);
740         cmd.arg(&builder.out);
741         if builder.is_verbose() {
742             cmd.arg("--verbose");
743         }
744
745         builder.info("tidy check");
746         try_run(builder, &mut cmd);
747
748         if builder.config.channel == "dev" || builder.config.channel == "nightly" {
749             builder.info("fmt check");
750             crate::format::format(&builder.build, !builder.config.cmd.bless());
751         }
752     }
753
754     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
755         run.path("src/tools/tidy")
756     }
757
758     fn make_run(run: RunConfig<'_>) {
759         run.builder.ensure(Tidy);
760     }
761 }
762
763 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
764 pub struct ExpandYamlAnchors;
765
766 impl Step for ExpandYamlAnchors {
767     type Output = ();
768     const ONLY_HOSTS: bool = true;
769
770     /// Ensure the `generate-ci-config` tool was run locally.
771     ///
772     /// The tool in `src/tools` reads the CI definition in `src/ci/builders.yml` and generates the
773     /// appropriate configuration for all our CI providers. This step ensures the tool was called
774     /// by the user before committing CI changes.
775     fn run(self, builder: &Builder<'_>) {
776         builder.info("Ensuring the YAML anchors in the GitHub Actions config were expanded");
777         try_run(
778             builder,
779             &mut builder.tool_cmd(Tool::ExpandYamlAnchors).arg("check").arg(&builder.src),
780         );
781     }
782
783     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
784         run.path("src/tools/expand-yaml-anchors")
785     }
786
787     fn make_run(run: RunConfig<'_>) {
788         run.builder.ensure(ExpandYamlAnchors);
789     }
790 }
791
792 fn testdir(builder: &Builder<'_>, host: TargetSelection) -> PathBuf {
793     builder.out.join(host.triple).join("test")
794 }
795
796 macro_rules! default_test {
797     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
798         test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: false });
799     };
800 }
801
802 macro_rules! default_test_with_compare_mode {
803     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr,
804                    compare_mode: $compare_mode:expr }) => {
805         test_with_compare_mode!($name {
806             path: $path,
807             mode: $mode,
808             suite: $suite,
809             default: true,
810             host: false,
811             compare_mode: $compare_mode
812         });
813     };
814 }
815
816 macro_rules! host_test {
817     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
818         test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: true });
819     };
820 }
821
822 macro_rules! test {
823     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr, default: $default:expr,
824                    host: $host:expr }) => {
825         test_definitions!($name {
826             path: $path,
827             mode: $mode,
828             suite: $suite,
829             default: $default,
830             host: $host,
831             compare_mode: None
832         });
833     };
834 }
835
836 macro_rules! test_with_compare_mode {
837     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr, default: $default:expr,
838                    host: $host:expr, compare_mode: $compare_mode:expr }) => {
839         test_definitions!($name {
840             path: $path,
841             mode: $mode,
842             suite: $suite,
843             default: $default,
844             host: $host,
845             compare_mode: Some($compare_mode)
846         });
847     };
848 }
849
850 macro_rules! test_definitions {
851     ($name:ident {
852         path: $path:expr,
853         mode: $mode:expr,
854         suite: $suite:expr,
855         default: $default:expr,
856         host: $host:expr,
857         compare_mode: $compare_mode:expr
858     }) => {
859         #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
860         pub struct $name {
861             pub compiler: Compiler,
862             pub target: TargetSelection,
863         }
864
865         impl Step for $name {
866             type Output = ();
867             const DEFAULT: bool = $default;
868             const ONLY_HOSTS: bool = $host;
869
870             fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
871                 run.suite_path($path)
872             }
873
874             fn make_run(run: RunConfig<'_>) {
875                 let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
876
877                 run.builder.ensure($name { compiler, target: run.target });
878             }
879
880             fn run(self, builder: &Builder<'_>) {
881                 builder.ensure(Compiletest {
882                     compiler: self.compiler,
883                     target: self.target,
884                     mode: $mode,
885                     suite: $suite,
886                     path: $path,
887                     compare_mode: $compare_mode,
888                 })
889             }
890         }
891     };
892 }
893
894 default_test_with_compare_mode!(Ui {
895     path: "src/test/ui",
896     mode: "ui",
897     suite: "ui",
898     compare_mode: "nll"
899 });
900
901 default_test!(CompileFail {
902     path: "src/test/compile-fail",
903     mode: "compile-fail",
904     suite: "compile-fail"
905 });
906
907 default_test!(RunPassValgrind {
908     path: "src/test/run-pass-valgrind",
909     mode: "run-pass-valgrind",
910     suite: "run-pass-valgrind"
911 });
912
913 default_test!(MirOpt { path: "src/test/mir-opt", mode: "mir-opt", suite: "mir-opt" });
914
915 default_test!(Codegen { path: "src/test/codegen", mode: "codegen", suite: "codegen" });
916
917 default_test!(CodegenUnits {
918     path: "src/test/codegen-units",
919     mode: "codegen-units",
920     suite: "codegen-units"
921 });
922
923 default_test!(Incremental {
924     path: "src/test/incremental",
925     mode: "incremental",
926     suite: "incremental"
927 });
928
929 default_test!(Debuginfo { path: "src/test/debuginfo", mode: "debuginfo", suite: "debuginfo" });
930
931 host_test!(UiFullDeps { path: "src/test/ui-fulldeps", mode: "ui", suite: "ui-fulldeps" });
932
933 host_test!(Rustdoc { path: "src/test/rustdoc", mode: "rustdoc", suite: "rustdoc" });
934
935 host_test!(Pretty { path: "src/test/pretty", mode: "pretty", suite: "pretty" });
936
937 default_test!(RunMake { path: "src/test/run-make", mode: "run-make", suite: "run-make" });
938
939 host_test!(RunMakeFullDeps {
940     path: "src/test/run-make-fulldeps",
941     mode: "run-make",
942     suite: "run-make-fulldeps"
943 });
944
945 default_test!(Assembly { path: "src/test/assembly", mode: "assembly", suite: "assembly" });
946
947 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
948 struct Compiletest {
949     compiler: Compiler,
950     target: TargetSelection,
951     mode: &'static str,
952     suite: &'static str,
953     path: &'static str,
954     compare_mode: Option<&'static str>,
955 }
956
957 impl Step for Compiletest {
958     type Output = ();
959
960     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
961         run.never()
962     }
963
964     /// Executes the `compiletest` tool to run a suite of tests.
965     ///
966     /// Compiles all tests with `compiler` for `target` with the specified
967     /// compiletest `mode` and `suite` arguments. For example `mode` can be
968     /// "run-pass" or `suite` can be something like `debuginfo`.
969     fn run(self, builder: &Builder<'_>) {
970         let compiler = self.compiler;
971         let target = self.target;
972         let mode = self.mode;
973         let suite = self.suite;
974
975         // Path for test suite
976         let suite_path = self.path;
977
978         // Skip codegen tests if they aren't enabled in configuration.
979         if !builder.config.codegen_tests && suite == "codegen" {
980             return;
981         }
982
983         if suite == "debuginfo" {
984             builder
985                 .ensure(dist::DebuggerScripts { sysroot: builder.sysroot(compiler), host: target });
986         }
987
988         if suite.ends_with("fulldeps") {
989             builder.ensure(compile::Rustc { compiler, target });
990         }
991
992         builder.ensure(compile::Std { compiler, target });
993         // ensure that `libproc_macro` is available on the host.
994         builder.ensure(compile::Std { compiler, target: compiler.host });
995
996         // Also provide `rust_test_helpers` for the host.
997         builder.ensure(native::TestHelpers { target: compiler.host });
998
999         // As well as the target, except for plain wasm32, which can't build it
1000         if !target.contains("wasm32") || target.contains("emscripten") {
1001             builder.ensure(native::TestHelpers { target });
1002         }
1003
1004         builder.ensure(RemoteCopyLibs { compiler, target });
1005
1006         let mut cmd = builder.tool_cmd(Tool::Compiletest);
1007
1008         // compiletest currently has... a lot of arguments, so let's just pass all
1009         // of them!
1010
1011         cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(compiler));
1012         cmd.arg("--run-lib-path").arg(builder.sysroot_libdir(compiler, target));
1013         cmd.arg("--rustc-path").arg(builder.rustc(compiler));
1014
1015         let is_rustdoc = suite.ends_with("rustdoc-ui") || suite.ends_with("rustdoc-js");
1016
1017         // Avoid depending on rustdoc when we don't need it.
1018         if mode == "rustdoc"
1019             || (mode == "run-make" && suite.ends_with("fulldeps"))
1020             || (mode == "ui" && is_rustdoc)
1021             || mode == "js-doc-test"
1022         {
1023             cmd.arg("--rustdoc-path").arg(builder.rustdoc(compiler));
1024         }
1025
1026         if mode == "run-make" && suite.ends_with("fulldeps") {
1027             cmd.arg("--rust-demangler-path").arg(builder.tool_exe(Tool::RustDemangler));
1028         }
1029
1030         cmd.arg("--src-base").arg(builder.src.join("src/test").join(suite));
1031         cmd.arg("--build-base").arg(testdir(builder, compiler.host).join(suite));
1032         cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target));
1033         cmd.arg("--mode").arg(mode);
1034         cmd.arg("--target").arg(target.rustc_target_arg());
1035         cmd.arg("--host").arg(&*compiler.host.triple);
1036         cmd.arg("--llvm-filecheck").arg(builder.llvm_filecheck(builder.config.build));
1037
1038         if builder.config.cmd.bless() {
1039             cmd.arg("--bless");
1040         }
1041
1042         let compare_mode =
1043             builder.config.cmd.compare_mode().or_else(|| {
1044                 if builder.config.test_compare_mode { self.compare_mode } else { None }
1045             });
1046
1047         if let Some(ref pass) = builder.config.cmd.pass() {
1048             cmd.arg("--pass");
1049             cmd.arg(pass);
1050         }
1051
1052         if let Some(ref nodejs) = builder.config.nodejs {
1053             cmd.arg("--nodejs").arg(nodejs);
1054         }
1055
1056         let mut flags = if is_rustdoc { Vec::new() } else { vec!["-Crpath".to_string()] };
1057         if !is_rustdoc {
1058             if builder.config.rust_optimize_tests {
1059                 flags.push("-O".to_string());
1060             }
1061         }
1062         flags.push(format!("-Cdebuginfo={}", builder.config.rust_debuginfo_level_tests));
1063         flags.push("-Zunstable-options".to_string());
1064         flags.push(builder.config.cmd.rustc_args().join(" "));
1065
1066         if let Some(linker) = builder.linker(target) {
1067             cmd.arg("--linker").arg(linker);
1068         }
1069
1070         let mut hostflags = flags.clone();
1071         hostflags.push(format!("-Lnative={}", builder.test_helpers_out(compiler.host).display()));
1072         if builder.is_fuse_ld_lld(compiler.host) {
1073             hostflags.push("-Clink-args=-fuse-ld=lld".to_string());
1074         }
1075         cmd.arg("--host-rustcflags").arg(hostflags.join(" "));
1076
1077         let mut targetflags = flags;
1078         targetflags.push(format!("-Lnative={}", builder.test_helpers_out(target).display()));
1079         if builder.is_fuse_ld_lld(target) {
1080             targetflags.push("-Clink-args=-fuse-ld=lld".to_string());
1081         }
1082         cmd.arg("--target-rustcflags").arg(targetflags.join(" "));
1083
1084         cmd.arg("--docck-python").arg(builder.python());
1085
1086         if builder.config.build.ends_with("apple-darwin") {
1087             // Force /usr/bin/python3 on macOS for LLDB tests because we're loading the
1088             // LLDB plugin's compiled module which only works with the system python
1089             // (namely not Homebrew-installed python)
1090             cmd.arg("--lldb-python").arg("/usr/bin/python3");
1091         } else {
1092             cmd.arg("--lldb-python").arg(builder.python());
1093         }
1094
1095         if let Some(ref gdb) = builder.config.gdb {
1096             cmd.arg("--gdb").arg(gdb);
1097         }
1098
1099         let run = |cmd: &mut Command| {
1100             cmd.output().map(|output| {
1101                 String::from_utf8_lossy(&output.stdout)
1102                     .lines()
1103                     .next()
1104                     .unwrap_or_else(|| panic!("{:?} failed {:?}", cmd, output))
1105                     .to_string()
1106             })
1107         };
1108         let lldb_exe = "lldb";
1109         let lldb_version = Command::new(lldb_exe)
1110             .arg("--version")
1111             .output()
1112             .map(|output| String::from_utf8_lossy(&output.stdout).to_string())
1113             .ok();
1114         if let Some(ref vers) = lldb_version {
1115             cmd.arg("--lldb-version").arg(vers);
1116             let lldb_python_dir = run(Command::new(lldb_exe).arg("-P")).ok();
1117             if let Some(ref dir) = lldb_python_dir {
1118                 cmd.arg("--lldb-python-dir").arg(dir);
1119             }
1120         }
1121
1122         if util::forcing_clang_based_tests() {
1123             let clang_exe = builder.llvm_out(target).join("bin").join("clang");
1124             cmd.arg("--run-clang-based-tests-with").arg(clang_exe);
1125         }
1126
1127         // Get paths from cmd args
1128         let paths = match &builder.config.cmd {
1129             Subcommand::Test { ref paths, .. } => &paths[..],
1130             _ => &[],
1131         };
1132
1133         // Get test-args by striping suite path
1134         let mut test_args: Vec<&str> = paths
1135             .iter()
1136             .map(|p| match p.strip_prefix(".") {
1137                 Ok(path) => path,
1138                 Err(_) => p,
1139             })
1140             .filter(|p| p.starts_with(suite_path) && (p.is_dir() || p.is_file()))
1141             .filter_map(|p| {
1142                 // Since test suite paths are themselves directories, if we don't
1143                 // specify a directory or file, we'll get an empty string here
1144                 // (the result of the test suite directory without its suite prefix).
1145                 // Therefore, we need to filter these out, as only the first --test-args
1146                 // flag is respected, so providing an empty --test-args conflicts with
1147                 // any following it.
1148                 match p.strip_prefix(suite_path).ok().and_then(|p| p.to_str()) {
1149                     Some(s) if s != "" => Some(s),
1150                     _ => None,
1151                 }
1152             })
1153             .collect();
1154
1155         test_args.append(&mut builder.config.cmd.test_args());
1156
1157         cmd.args(&test_args);
1158
1159         if builder.is_verbose() {
1160             cmd.arg("--verbose");
1161         }
1162
1163         if !builder.config.verbose_tests {
1164             cmd.arg("--quiet");
1165         }
1166
1167         let mut llvm_components_passed = false;
1168         let mut copts_passed = false;
1169         if builder.config.llvm_enabled() {
1170             let llvm_config = builder.ensure(native::Llvm { target: builder.config.build });
1171             if !builder.config.dry_run {
1172                 let llvm_version = output(Command::new(&llvm_config).arg("--version"));
1173                 let llvm_components = output(Command::new(&llvm_config).arg("--components"));
1174                 // Remove trailing newline from llvm-config output.
1175                 cmd.arg("--llvm-version")
1176                     .arg(llvm_version.trim())
1177                     .arg("--llvm-components")
1178                     .arg(llvm_components.trim());
1179                 llvm_components_passed = true;
1180             }
1181             if !builder.is_rust_llvm(target) {
1182                 cmd.arg("--system-llvm");
1183             }
1184
1185             // Tests that use compiler libraries may inherit the `-lLLVM` link
1186             // requirement, but the `-L` library path is not propagated across
1187             // separate compilations. We can add LLVM's library path to the
1188             // platform-specific environment variable as a workaround.
1189             if !builder.config.dry_run && suite.ends_with("fulldeps") {
1190                 let llvm_libdir = output(Command::new(&llvm_config).arg("--libdir"));
1191                 add_link_lib_path(vec![llvm_libdir.trim().into()], &mut cmd);
1192             }
1193
1194             // Only pass correct values for these flags for the `run-make` suite as it
1195             // requires that a C++ compiler was configured which isn't always the case.
1196             if !builder.config.dry_run && suite == "run-make-fulldeps" {
1197                 cmd.arg("--cc")
1198                     .arg(builder.cc(target))
1199                     .arg("--cxx")
1200                     .arg(builder.cxx(target).unwrap())
1201                     .arg("--cflags")
1202                     .arg(builder.cflags(target, GitRepo::Rustc).join(" "));
1203                 copts_passed = true;
1204                 if let Some(ar) = builder.ar(target) {
1205                     cmd.arg("--ar").arg(ar);
1206                 }
1207
1208                 // The llvm/bin directory contains many useful cross-platform
1209                 // tools. Pass the path to run-make tests so they can use them.
1210                 let llvm_bin_path = llvm_config
1211                     .parent()
1212                     .expect("Expected llvm-config to be contained in directory");
1213                 assert!(llvm_bin_path.is_dir());
1214                 cmd.arg("--llvm-bin-dir").arg(llvm_bin_path);
1215
1216                 // If LLD is available, add it to the PATH
1217                 if builder.config.lld_enabled {
1218                     let lld_install_root =
1219                         builder.ensure(native::Lld { target: builder.config.build });
1220
1221                     let lld_bin_path = lld_install_root.join("bin");
1222
1223                     let old_path = env::var_os("PATH").unwrap_or_default();
1224                     let new_path = env::join_paths(
1225                         std::iter::once(lld_bin_path).chain(env::split_paths(&old_path)),
1226                     )
1227                     .expect("Could not add LLD bin path to PATH");
1228                     cmd.env("PATH", new_path);
1229                 }
1230             }
1231         }
1232
1233         if !llvm_components_passed {
1234             cmd.arg("--llvm-components").arg("");
1235         }
1236         if !copts_passed {
1237             cmd.arg("--cc").arg("").arg("--cxx").arg("").arg("--cflags").arg("");
1238         }
1239
1240         if builder.remote_tested(target) {
1241             cmd.arg("--remote-test-client").arg(builder.tool_exe(Tool::RemoteTestClient));
1242         }
1243
1244         // Running a C compiler on MSVC requires a few env vars to be set, to be
1245         // sure to set them here.
1246         //
1247         // Note that if we encounter `PATH` we make sure to append to our own `PATH`
1248         // rather than stomp over it.
1249         if target.contains("msvc") {
1250             for &(ref k, ref v) in builder.cc[&target].env() {
1251                 if k != "PATH" {
1252                     cmd.env(k, v);
1253                 }
1254             }
1255         }
1256         cmd.env("RUSTC_BOOTSTRAP", "1");
1257         builder.add_rust_test_threads(&mut cmd);
1258
1259         if builder.config.sanitizers {
1260             cmd.env("RUSTC_SANITIZER_SUPPORT", "1");
1261         }
1262
1263         if builder.config.profiler {
1264             cmd.env("RUSTC_PROFILER_SUPPORT", "1");
1265         }
1266
1267         let tmp = builder.out.join("tmp");
1268         std::fs::create_dir_all(&tmp).unwrap();
1269         cmd.env("RUST_TEST_TMPDIR", tmp);
1270
1271         cmd.arg("--adb-path").arg("adb");
1272         cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
1273         if target.contains("android") {
1274             // Assume that cc for this target comes from the android sysroot
1275             cmd.arg("--android-cross-path")
1276                 .arg(builder.cc(target).parent().unwrap().parent().unwrap());
1277         } else {
1278             cmd.arg("--android-cross-path").arg("");
1279         }
1280
1281         if builder.config.cmd.rustfix_coverage() {
1282             cmd.arg("--rustfix-coverage");
1283         }
1284
1285         builder.ci_env.force_coloring_in_ci(&mut cmd);
1286
1287         builder.info(&format!(
1288             "Check compiletest suite={} mode={} ({} -> {})",
1289             suite, mode, &compiler.host, target
1290         ));
1291         let _time = util::timeit(&builder);
1292         try_run(builder, &mut cmd);
1293
1294         if let Some(compare_mode) = compare_mode {
1295             cmd.arg("--compare-mode").arg(compare_mode);
1296             builder.info(&format!(
1297                 "Check compiletest suite={} mode={} compare_mode={} ({} -> {})",
1298                 suite, mode, compare_mode, &compiler.host, target
1299             ));
1300             let _time = util::timeit(&builder);
1301             try_run(builder, &mut cmd);
1302         }
1303     }
1304 }
1305
1306 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
1307 struct BookTest {
1308     compiler: Compiler,
1309     path: PathBuf,
1310     name: &'static str,
1311     is_ext_doc: bool,
1312 }
1313
1314 impl Step for BookTest {
1315     type Output = ();
1316     const ONLY_HOSTS: bool = true;
1317
1318     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1319         run.never()
1320     }
1321
1322     /// Runs the documentation tests for a book in `src/doc`.
1323     ///
1324     /// This uses the `rustdoc` that sits next to `compiler`.
1325     fn run(self, builder: &Builder<'_>) {
1326         // External docs are different from local because:
1327         // - Some books need pre-processing by mdbook before being tested.
1328         // - They need to save their state to toolstate.
1329         // - They are only tested on the "checktools" builders.
1330         //
1331         // The local docs are tested by default, and we don't want to pay the
1332         // cost of building mdbook, so they use `rustdoc --test` directly.
1333         // Also, the unstable book is special because SUMMARY.md is generated,
1334         // so it is easier to just run `rustdoc` on its files.
1335         if self.is_ext_doc {
1336             self.run_ext_doc(builder);
1337         } else {
1338             self.run_local_doc(builder);
1339         }
1340     }
1341 }
1342
1343 impl BookTest {
1344     /// This runs the equivalent of `mdbook test` (via the rustbook wrapper)
1345     /// which in turn runs `rustdoc --test` on each file in the book.
1346     fn run_ext_doc(self, builder: &Builder<'_>) {
1347         let compiler = self.compiler;
1348
1349         builder.ensure(compile::Std { compiler, target: compiler.host });
1350
1351         // mdbook just executes a binary named "rustdoc", so we need to update
1352         // PATH so that it points to our rustdoc.
1353         let mut rustdoc_path = builder.rustdoc(compiler);
1354         rustdoc_path.pop();
1355         let old_path = env::var_os("PATH").unwrap_or_default();
1356         let new_path = env::join_paths(iter::once(rustdoc_path).chain(env::split_paths(&old_path)))
1357             .expect("could not add rustdoc to PATH");
1358
1359         let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
1360         let path = builder.src.join(&self.path);
1361         rustbook_cmd.env("PATH", new_path).arg("test").arg(path);
1362         builder.add_rust_test_threads(&mut rustbook_cmd);
1363         builder.info(&format!("Testing rustbook {}", self.path.display()));
1364         let _time = util::timeit(&builder);
1365         let toolstate = if try_run(builder, &mut rustbook_cmd) {
1366             ToolState::TestPass
1367         } else {
1368             ToolState::TestFail
1369         };
1370         builder.save_toolstate(self.name, toolstate);
1371     }
1372
1373     /// This runs `rustdoc --test` on all `.md` files in the path.
1374     fn run_local_doc(self, builder: &Builder<'_>) {
1375         let compiler = self.compiler;
1376
1377         builder.ensure(compile::Std { compiler, target: compiler.host });
1378
1379         // Do a breadth-first traversal of the `src/doc` directory and just run
1380         // tests for all files that end in `*.md`
1381         let mut stack = vec![builder.src.join(self.path)];
1382         let _time = util::timeit(&builder);
1383         let mut files = Vec::new();
1384         while let Some(p) = stack.pop() {
1385             if p.is_dir() {
1386                 stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
1387                 continue;
1388             }
1389
1390             if p.extension().and_then(|s| s.to_str()) != Some("md") {
1391                 continue;
1392             }
1393
1394             files.push(p);
1395         }
1396
1397         files.sort();
1398
1399         for file in files {
1400             markdown_test(builder, compiler, &file);
1401         }
1402     }
1403 }
1404
1405 macro_rules! test_book {
1406     ($($name:ident, $path:expr, $book_name:expr, default=$default:expr;)+) => {
1407         $(
1408             #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1409             pub struct $name {
1410                 compiler: Compiler,
1411             }
1412
1413             impl Step for $name {
1414                 type Output = ();
1415                 const DEFAULT: bool = $default;
1416                 const ONLY_HOSTS: bool = true;
1417
1418                 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1419                     run.path($path)
1420                 }
1421
1422                 fn make_run(run: RunConfig<'_>) {
1423                     run.builder.ensure($name {
1424                         compiler: run.builder.compiler(run.builder.top_stage, run.target),
1425                     });
1426                 }
1427
1428                 fn run(self, builder: &Builder<'_>) {
1429                     builder.ensure(BookTest {
1430                         compiler: self.compiler,
1431                         path: PathBuf::from($path),
1432                         name: $book_name,
1433                         is_ext_doc: !$default,
1434                     });
1435                 }
1436             }
1437         )+
1438     }
1439 }
1440
1441 test_book!(
1442     Nomicon, "src/doc/nomicon", "nomicon", default=false;
1443     Reference, "src/doc/reference", "reference", default=false;
1444     RustdocBook, "src/doc/rustdoc", "rustdoc", default=true;
1445     RustcBook, "src/doc/rustc", "rustc", default=true;
1446     RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false;
1447     EmbeddedBook, "src/doc/embedded-book", "embedded-book", default=false;
1448     TheBook, "src/doc/book", "book", default=false;
1449     UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
1450     EditionGuide, "src/doc/edition-guide", "edition-guide", default=false;
1451 );
1452
1453 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1454 pub struct ErrorIndex {
1455     compiler: Compiler,
1456 }
1457
1458 impl Step for ErrorIndex {
1459     type Output = ();
1460     const DEFAULT: bool = true;
1461     const ONLY_HOSTS: bool = true;
1462
1463     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1464         run.path("src/tools/error_index_generator")
1465     }
1466
1467     fn make_run(run: RunConfig<'_>) {
1468         // error_index_generator depends on librustdoc. Use the compiler that
1469         // is normally used to build rustdoc for other tests (like compiletest
1470         // tests in src/test/rustdoc) so that it shares the same artifacts.
1471         let compiler = run.builder.compiler_for(run.builder.top_stage, run.target, run.target);
1472         run.builder.ensure(ErrorIndex { compiler });
1473     }
1474
1475     /// Runs the error index generator tool to execute the tests located in the error
1476     /// index.
1477     ///
1478     /// The `error_index_generator` tool lives in `src/tools` and is used to
1479     /// generate a markdown file from the error indexes of the code base which is
1480     /// then passed to `rustdoc --test`.
1481     fn run(self, builder: &Builder<'_>) {
1482         let compiler = self.compiler;
1483
1484         let dir = testdir(builder, compiler.host);
1485         t!(fs::create_dir_all(&dir));
1486         let output = dir.join("error-index.md");
1487
1488         let mut tool = tool::ErrorIndex::command(builder, compiler);
1489         tool.arg("markdown").arg(&output);
1490
1491         // Use the rustdoc that was built by self.compiler. This copy of
1492         // rustdoc is shared with other tests (like compiletest tests in
1493         // src/test/rustdoc). This helps avoid building rustdoc multiple
1494         // times.
1495         let rustdoc_compiler = builder.compiler(builder.top_stage, builder.config.build);
1496         builder.info(&format!("Testing error-index stage{}", rustdoc_compiler.stage));
1497         let _time = util::timeit(&builder);
1498         builder.run_quiet(&mut tool);
1499         builder.ensure(compile::Std { compiler: rustdoc_compiler, target: rustdoc_compiler.host });
1500         markdown_test(builder, rustdoc_compiler, &output);
1501     }
1502 }
1503
1504 fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) -> bool {
1505     if let Ok(contents) = fs::read_to_string(markdown) {
1506         if !contents.contains("```") {
1507             return true;
1508         }
1509     }
1510
1511     builder.info(&format!("doc tests for: {}", markdown.display()));
1512     let mut cmd = builder.rustdoc_cmd(compiler);
1513     builder.add_rust_test_threads(&mut cmd);
1514     cmd.arg("--test");
1515     cmd.arg(markdown);
1516     cmd.env("RUSTC_BOOTSTRAP", "1");
1517
1518     let test_args = builder.config.cmd.test_args().join(" ");
1519     cmd.arg("--test-args").arg(test_args);
1520
1521     if builder.config.verbose_tests {
1522         try_run(builder, &mut cmd)
1523     } else {
1524         try_run_quiet(builder, &mut cmd)
1525     }
1526 }
1527
1528 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1529 pub struct RustcGuide;
1530
1531 impl Step for RustcGuide {
1532     type Output = ();
1533     const DEFAULT: bool = false;
1534     const ONLY_HOSTS: bool = true;
1535
1536     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1537         run.path("src/doc/rustc-dev-guide")
1538     }
1539
1540     fn make_run(run: RunConfig<'_>) {
1541         run.builder.ensure(RustcGuide);
1542     }
1543
1544     fn run(self, builder: &Builder<'_>) {
1545         let src = builder.src.join("src/doc/rustc-dev-guide");
1546         let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
1547         let toolstate = if try_run(builder, rustbook_cmd.arg("linkcheck").arg(&src)) {
1548             ToolState::TestPass
1549         } else {
1550             ToolState::TestFail
1551         };
1552         builder.save_toolstate("rustc-dev-guide", toolstate);
1553     }
1554 }
1555
1556 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1557 pub struct CrateLibrustc {
1558     compiler: Compiler,
1559     target: TargetSelection,
1560     test_kind: TestKind,
1561     krate: Interned<String>,
1562 }
1563
1564 impl Step for CrateLibrustc {
1565     type Output = ();
1566     const DEFAULT: bool = true;
1567     const ONLY_HOSTS: bool = true;
1568
1569     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1570         run.krate("rustc-main")
1571     }
1572
1573     fn make_run(run: RunConfig<'_>) {
1574         let builder = run.builder;
1575         let compiler = builder.compiler(builder.top_stage, run.build_triple());
1576
1577         for krate in builder.in_tree_crates("rustc-main") {
1578             if krate.path.ends_with(&run.path) {
1579                 let test_kind = builder.kind.into();
1580
1581                 builder.ensure(CrateLibrustc {
1582                     compiler,
1583                     target: run.target,
1584                     test_kind,
1585                     krate: krate.name,
1586                 });
1587             }
1588         }
1589     }
1590
1591     fn run(self, builder: &Builder<'_>) {
1592         builder.ensure(Crate {
1593             compiler: self.compiler,
1594             target: self.target,
1595             mode: Mode::Rustc,
1596             test_kind: self.test_kind,
1597             krate: self.krate,
1598         });
1599     }
1600 }
1601
1602 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1603 pub struct CrateNotDefault {
1604     compiler: Compiler,
1605     target: TargetSelection,
1606     test_kind: TestKind,
1607     krate: &'static str,
1608 }
1609
1610 impl Step for CrateNotDefault {
1611     type Output = ();
1612
1613     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1614         run.path("src/librustc_asan")
1615             .path("src/librustc_lsan")
1616             .path("src/librustc_msan")
1617             .path("src/librustc_tsan")
1618     }
1619
1620     fn make_run(run: RunConfig<'_>) {
1621         let builder = run.builder;
1622         let compiler = builder.compiler(builder.top_stage, run.build_triple());
1623
1624         let test_kind = builder.kind.into();
1625
1626         builder.ensure(CrateNotDefault {
1627             compiler,
1628             target: run.target,
1629             test_kind,
1630             krate: match run.path {
1631                 _ if run.path.ends_with("src/librustc_asan") => "rustc_asan",
1632                 _ if run.path.ends_with("src/librustc_lsan") => "rustc_lsan",
1633                 _ if run.path.ends_with("src/librustc_msan") => "rustc_msan",
1634                 _ if run.path.ends_with("src/librustc_tsan") => "rustc_tsan",
1635                 _ => panic!("unexpected path {:?}", run.path),
1636             },
1637         });
1638     }
1639
1640     fn run(self, builder: &Builder<'_>) {
1641         builder.ensure(Crate {
1642             compiler: self.compiler,
1643             target: self.target,
1644             mode: Mode::Std,
1645             test_kind: self.test_kind,
1646             krate: INTERNER.intern_str(self.krate),
1647         });
1648     }
1649 }
1650
1651 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1652 pub struct Crate {
1653     pub compiler: Compiler,
1654     pub target: TargetSelection,
1655     pub mode: Mode,
1656     pub test_kind: TestKind,
1657     pub krate: Interned<String>,
1658 }
1659
1660 impl Step for Crate {
1661     type Output = ();
1662     const DEFAULT: bool = true;
1663
1664     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1665         run.krate("test")
1666     }
1667
1668     fn make_run(run: RunConfig<'_>) {
1669         let builder = run.builder;
1670         let compiler = builder.compiler(builder.top_stage, run.build_triple());
1671
1672         let make = |mode: Mode, krate: &CargoCrate| {
1673             let test_kind = builder.kind.into();
1674
1675             builder.ensure(Crate {
1676                 compiler,
1677                 target: run.target,
1678                 mode,
1679                 test_kind,
1680                 krate: krate.name,
1681             });
1682         };
1683
1684         for krate in builder.in_tree_crates("test") {
1685             if krate.path.ends_with(&run.path) {
1686                 make(Mode::Std, krate);
1687             }
1688         }
1689     }
1690
1691     /// Runs all unit tests plus documentation tests for a given crate defined
1692     /// by a `Cargo.toml` (single manifest)
1693     ///
1694     /// This is what runs tests for crates like the standard library, compiler, etc.
1695     /// It essentially is the driver for running `cargo test`.
1696     ///
1697     /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
1698     /// arguments, and those arguments are discovered from `cargo metadata`.
1699     fn run(self, builder: &Builder<'_>) {
1700         let compiler = self.compiler;
1701         let target = self.target;
1702         let mode = self.mode;
1703         let test_kind = self.test_kind;
1704         let krate = self.krate;
1705
1706         builder.ensure(compile::Std { compiler, target });
1707         builder.ensure(RemoteCopyLibs { compiler, target });
1708
1709         // If we're not doing a full bootstrap but we're testing a stage2
1710         // version of libstd, then what we're actually testing is the libstd
1711         // produced in stage1. Reflect that here by updating the compiler that
1712         // we're working with automatically.
1713         let compiler = builder.compiler_for(compiler.stage, compiler.host, target);
1714
1715         let mut cargo =
1716             builder.cargo(compiler, mode, SourceType::InTree, target, test_kind.subcommand());
1717         match mode {
1718             Mode::Std => {
1719                 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
1720             }
1721             Mode::Rustc => {
1722                 builder.ensure(compile::Rustc { compiler, target });
1723                 compile::rustc_cargo(builder, &mut cargo, target);
1724             }
1725             _ => panic!("can only test libraries"),
1726         };
1727
1728         // Build up the base `cargo test` command.
1729         //
1730         // Pass in some standard flags then iterate over the graph we've discovered
1731         // in `cargo metadata` with the maps above and figure out what `-p`
1732         // arguments need to get passed.
1733         if test_kind.subcommand() == "test" && !builder.fail_fast {
1734             cargo.arg("--no-fail-fast");
1735         }
1736         match builder.doc_tests {
1737             DocTests::Only => {
1738                 cargo.arg("--doc");
1739             }
1740             DocTests::No => {
1741                 cargo.args(&["--lib", "--bins", "--examples", "--tests", "--benches"]);
1742             }
1743             DocTests::Yes => {}
1744         }
1745
1746         cargo.arg("-p").arg(krate);
1747
1748         // The tests are going to run with the *target* libraries, so we need to
1749         // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
1750         //
1751         // Note that to run the compiler we need to run with the *host* libraries,
1752         // but our wrapper scripts arrange for that to be the case anyway.
1753         let mut dylib_path = dylib_path();
1754         dylib_path.insert(0, PathBuf::from(&*builder.sysroot_libdir(compiler, target)));
1755         cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1756
1757         cargo.arg("--");
1758         cargo.args(&builder.config.cmd.test_args());
1759
1760         if !builder.config.verbose_tests {
1761             cargo.arg("--quiet");
1762         }
1763
1764         if target.contains("emscripten") {
1765             cargo.env(
1766                 format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
1767                 builder.config.nodejs.as_ref().expect("nodejs not configured"),
1768             );
1769         } else if target.starts_with("wasm32") {
1770             let node = builder.config.nodejs.as_ref().expect("nodejs not configured");
1771             let runner =
1772                 format!("{} {}/src/etc/wasm32-shim.js", node.display(), builder.src.display());
1773             cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)), &runner);
1774         } else if builder.remote_tested(target) {
1775             cargo.env(
1776                 format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
1777                 format!("{} run 0", builder.tool_exe(Tool::RemoteTestClient).display()),
1778             );
1779         }
1780
1781         builder.info(&format!(
1782             "{} {} stage{} ({} -> {})",
1783             test_kind, krate, compiler.stage, &compiler.host, target
1784         ));
1785         let _time = util::timeit(&builder);
1786         try_run(builder, &mut cargo.into());
1787     }
1788 }
1789
1790 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1791 pub struct CrateRustdoc {
1792     host: TargetSelection,
1793     test_kind: TestKind,
1794 }
1795
1796 impl Step for CrateRustdoc {
1797     type Output = ();
1798     const DEFAULT: bool = true;
1799     const ONLY_HOSTS: bool = true;
1800
1801     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1802         run.paths(&["src/librustdoc", "src/tools/rustdoc"])
1803     }
1804
1805     fn make_run(run: RunConfig<'_>) {
1806         let builder = run.builder;
1807
1808         let test_kind = builder.kind.into();
1809
1810         builder.ensure(CrateRustdoc { host: run.target, test_kind });
1811     }
1812
1813     fn run(self, builder: &Builder<'_>) {
1814         let test_kind = self.test_kind;
1815         let target = self.host;
1816
1817         // Use the previous stage compiler to reuse the artifacts that are
1818         // created when running compiletest for src/test/rustdoc. If this used
1819         // `compiler`, then it would cause rustdoc to be built *again*, which
1820         // isn't really necessary.
1821         let compiler = builder.compiler_for(builder.top_stage, target, target);
1822         builder.ensure(compile::Rustc { compiler, target });
1823
1824         let mut cargo = tool::prepare_tool_cargo(
1825             builder,
1826             compiler,
1827             Mode::ToolRustc,
1828             target,
1829             test_kind.subcommand(),
1830             "src/tools/rustdoc",
1831             SourceType::InTree,
1832             &[],
1833         );
1834         if test_kind.subcommand() == "test" && !builder.fail_fast {
1835             cargo.arg("--no-fail-fast");
1836         }
1837
1838         cargo.arg("-p").arg("rustdoc:0.0.0");
1839
1840         cargo.arg("--");
1841         cargo.args(&builder.config.cmd.test_args());
1842
1843         if self.host.contains("musl") {
1844             cargo.arg("'-Ctarget-feature=-crt-static'");
1845         }
1846
1847         // This is needed for running doctests on librustdoc. This is a bit of
1848         // an unfortunate interaction with how bootstrap works and how cargo
1849         // sets up the dylib path, and the fact that the doctest (in
1850         // html/markdown.rs) links to rustc-private libs. For stage1, the
1851         // compiler host dylibs (in stage1/lib) are not the same as the target
1852         // dylibs (in stage1/lib/rustlib/...). This is different from a normal
1853         // rust distribution where they are the same.
1854         //
1855         // On the cargo side, normal tests use `target_process` which handles
1856         // setting up the dylib for a *target* (stage1/lib/rustlib/... in this
1857         // case). However, for doctests it uses `rustdoc_process` which only
1858         // sets up the dylib path for the *host* (stage1/lib), which is the
1859         // wrong directory.
1860         //
1861         // It should be considered to just stop running doctests on
1862         // librustdoc. There is only one test, and it doesn't look too
1863         // important. There might be other ways to avoid this, but it seems
1864         // pretty convoluted.
1865         //
1866         // See also https://github.com/rust-lang/rust/issues/13983 where the
1867         // host vs target dylibs for rustdoc are consistently tricky to deal
1868         // with.
1869         let mut dylib_path = dylib_path();
1870         dylib_path.insert(0, PathBuf::from(&*builder.sysroot_libdir(compiler, target)));
1871         cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1872
1873         if !builder.config.verbose_tests {
1874             cargo.arg("--quiet");
1875         }
1876
1877         builder.info(&format!(
1878             "{} rustdoc stage{} ({} -> {})",
1879             test_kind, compiler.stage, &compiler.host, target
1880         ));
1881         let _time = util::timeit(&builder);
1882
1883         try_run(builder, &mut cargo.into());
1884     }
1885 }
1886
1887 /// Some test suites are run inside emulators or on remote devices, and most
1888 /// of our test binaries are linked dynamically which means we need to ship
1889 /// the standard library and such to the emulator ahead of time. This step
1890 /// represents this and is a dependency of all test suites.
1891 ///
1892 /// Most of the time this is a no-op. For some steps such as shipping data to
1893 /// QEMU we have to build our own tools so we've got conditional dependencies
1894 /// on those programs as well. Note that the remote test client is built for
1895 /// the build target (us) and the server is built for the target.
1896 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1897 pub struct RemoteCopyLibs {
1898     compiler: Compiler,
1899     target: TargetSelection,
1900 }
1901
1902 impl Step for RemoteCopyLibs {
1903     type Output = ();
1904
1905     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1906         run.never()
1907     }
1908
1909     fn run(self, builder: &Builder<'_>) {
1910         let compiler = self.compiler;
1911         let target = self.target;
1912         if !builder.remote_tested(target) {
1913             return;
1914         }
1915
1916         builder.ensure(compile::Std { compiler, target });
1917
1918         builder.info(&format!("REMOTE copy libs to emulator ({})", target));
1919         t!(fs::create_dir_all(builder.out.join("tmp")));
1920
1921         let server =
1922             builder.ensure(tool::RemoteTestServer { compiler: compiler.with_stage(0), target });
1923
1924         // Spawn the emulator and wait for it to come online
1925         let tool = builder.tool_exe(Tool::RemoteTestClient);
1926         let mut cmd = Command::new(&tool);
1927         cmd.arg("spawn-emulator").arg(target.triple).arg(&server).arg(builder.out.join("tmp"));
1928         if let Some(rootfs) = builder.qemu_rootfs(target) {
1929             cmd.arg(rootfs);
1930         }
1931         builder.run(&mut cmd);
1932
1933         // Push all our dylibs to the emulator
1934         for f in t!(builder.sysroot_libdir(compiler, target).read_dir()) {
1935             let f = t!(f);
1936             let name = f.file_name().into_string().unwrap();
1937             if util::is_dylib(&name) {
1938                 builder.run(Command::new(&tool).arg("push").arg(f.path()));
1939             }
1940         }
1941     }
1942 }
1943
1944 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1945 pub struct Distcheck;
1946
1947 impl Step for Distcheck {
1948     type Output = ();
1949
1950     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1951         run.path("distcheck")
1952     }
1953
1954     fn make_run(run: RunConfig<'_>) {
1955         run.builder.ensure(Distcheck);
1956     }
1957
1958     /// Runs "distcheck", a 'make check' from a tarball
1959     fn run(self, builder: &Builder<'_>) {
1960         builder.info("Distcheck");
1961         let dir = builder.out.join("tmp").join("distcheck");
1962         let _ = fs::remove_dir_all(&dir);
1963         t!(fs::create_dir_all(&dir));
1964
1965         // Guarantee that these are built before we begin running.
1966         builder.ensure(dist::PlainSourceTarball);
1967         builder.ensure(dist::Src);
1968
1969         let mut cmd = Command::new("tar");
1970         cmd.arg("-xzf")
1971             .arg(builder.ensure(dist::PlainSourceTarball))
1972             .arg("--strip-components=1")
1973             .current_dir(&dir);
1974         builder.run(&mut cmd);
1975         builder.run(
1976             Command::new("./configure")
1977                 .args(&builder.config.configure_args)
1978                 .arg("--enable-vendor")
1979                 .current_dir(&dir),
1980         );
1981         builder.run(
1982             Command::new(build_helper::make(&builder.config.build.triple))
1983                 .arg("check")
1984                 .current_dir(&dir),
1985         );
1986
1987         // Now make sure that rust-src has all of libstd's dependencies
1988         builder.info("Distcheck rust-src");
1989         let dir = builder.out.join("tmp").join("distcheck-src");
1990         let _ = fs::remove_dir_all(&dir);
1991         t!(fs::create_dir_all(&dir));
1992
1993         let mut cmd = Command::new("tar");
1994         cmd.arg("-xzf")
1995             .arg(builder.ensure(dist::Src))
1996             .arg("--strip-components=1")
1997             .current_dir(&dir);
1998         builder.run(&mut cmd);
1999
2000         let toml = dir.join("rust-src/lib/rustlib/src/rust/library/std/Cargo.toml");
2001         builder.run(
2002             Command::new(&builder.initial_cargo)
2003                 .arg("generate-lockfile")
2004                 .arg("--manifest-path")
2005                 .arg(&toml)
2006                 .current_dir(&dir),
2007         );
2008     }
2009 }
2010
2011 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2012 pub struct Bootstrap;
2013
2014 impl Step for Bootstrap {
2015     type Output = ();
2016     const DEFAULT: bool = true;
2017     const ONLY_HOSTS: bool = true;
2018
2019     /// Tests the build system itself.
2020     fn run(self, builder: &Builder<'_>) {
2021         let mut cmd = Command::new(&builder.initial_cargo);
2022         cmd.arg("test")
2023             .current_dir(builder.src.join("src/bootstrap"))
2024             .env("RUSTFLAGS", "-Cdebuginfo=2")
2025             .env("CARGO_TARGET_DIR", builder.out.join("bootstrap"))
2026             .env("RUSTC_BOOTSTRAP", "1")
2027             .env("RUSTC", &builder.initial_rustc);
2028         if let Some(flags) = option_env!("RUSTFLAGS") {
2029             // Use the same rustc flags for testing as for "normal" compilation,
2030             // so that Cargo doesn’t recompile the entire dependency graph every time:
2031             // https://github.com/rust-lang/rust/issues/49215
2032             cmd.env("RUSTFLAGS", flags);
2033         }
2034         if !builder.fail_fast {
2035             cmd.arg("--no-fail-fast");
2036         }
2037         cmd.arg("--").args(&builder.config.cmd.test_args());
2038         // rustbuild tests are racy on directory creation so just run them one at a time.
2039         // Since there's not many this shouldn't be a problem.
2040         cmd.arg("--test-threads=1");
2041         try_run(builder, &mut cmd);
2042     }
2043
2044     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2045         run.path("src/bootstrap")
2046     }
2047
2048     fn make_run(run: RunConfig<'_>) {
2049         run.builder.ensure(Bootstrap);
2050     }
2051 }
2052
2053 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2054 pub struct TierCheck {
2055     pub compiler: Compiler,
2056 }
2057
2058 impl Step for TierCheck {
2059     type Output = ();
2060     const DEFAULT: bool = true;
2061     const ONLY_HOSTS: bool = true;
2062
2063     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2064         run.path("src/tools/tier-check")
2065     }
2066
2067     fn make_run(run: RunConfig<'_>) {
2068         let compiler =
2069             run.builder.compiler_for(run.builder.top_stage, run.builder.build.build, run.target);
2070         run.builder.ensure(TierCheck { compiler });
2071     }
2072
2073     /// Tests the Platform Support page in the rustc book.
2074     fn run(self, builder: &Builder<'_>) {
2075         builder.ensure(compile::Std { compiler: self.compiler, target: self.compiler.host });
2076         let mut cargo = tool::prepare_tool_cargo(
2077             builder,
2078             self.compiler,
2079             Mode::ToolStd,
2080             self.compiler.host,
2081             "run",
2082             "src/tools/tier-check",
2083             SourceType::InTree,
2084             &[],
2085         );
2086         cargo.arg(builder.src.join("src/doc/rustc/src/platform-support.md"));
2087         cargo.arg(&builder.rustc(self.compiler));
2088         if builder.is_verbose() {
2089             cargo.arg("--verbose");
2090         }
2091
2092         builder.info("platform support check");
2093         try_run(builder, &mut cargo.into());
2094     }
2095 }