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