1 //! Implementation of the test-related targets of the build system.
3 //! This file implements the various regression test suites that we execute on
7 use std::ffi::OsString;
11 use std::path::{Path, PathBuf};
12 use std::process::Command;
14 use build_helper::{self, output, t};
16 use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
17 use crate::cache::Interned;
19 use crate::config::TargetSelection;
21 use crate::flags::Subcommand;
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};
29 const ADB_TEST_DIR: &str = "/data/tmp/work";
31 /// The two modes of the test runner; tests or benchmarks.
32 #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, PartialOrd, Ord)]
36 /// Run `cargo bench`.
40 impl From<Kind> for TestKind {
41 fn from(kind: Kind) -> Self {
43 Kind::Test => TestKind::Test,
44 Kind::Bench => TestKind::Bench,
45 _ => panic!("unexpected kind in crate: {:?}", kind),
51 // Return the cargo subcommand for this test kind
52 fn subcommand(self) -> &'static str {
54 TestKind::Test => "test",
55 TestKind::Bench => "bench",
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",
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));
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));
90 builder.run_quiet(cmd);
95 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
96 pub struct Linkcheck {
97 host: TargetSelection,
100 impl Step for Linkcheck {
102 const ONLY_HOSTS: bool = true;
103 const DEFAULT: bool = true;
105 /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
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 let hosts = &builder.hosts;
112 let targets = &builder.targets;
114 // if we have different hosts and targets, some things may be built for
115 // the host (e.g. rustc) and others for the target (e.g. std). The
116 // documentation built for each will contain broken links to
117 // docs built for the other platform (e.g. rustc linking to cargo)
118 if (hosts != targets) && !hosts.is_empty() && !targets.is_empty() {
120 "Linkcheck currently does not support builds with different hosts and targets.
121 You can skip linkcheck with --exclude src/tools/linkchecker"
125 builder.info(&format!("Linkcheck ({})", host));
127 // Test the linkchecker itself.
128 let bootstrap_host = builder.config.build;
129 let compiler = builder.compiler(0, bootstrap_host);
130 let cargo = tool::prepare_tool_cargo(
136 "src/tools/linkchecker",
140 try_run(builder, &mut cargo.into());
142 // Build all the default documentation.
143 builder.default_doc(&[]);
145 // Run the linkchecker.
146 let _time = util::timeit(&builder);
149 builder.tool_cmd(Tool::Linkchecker).arg(builder.out.join(host.triple).join("doc")),
153 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
154 let builder = run.builder;
155 let run = run.path("src/tools/linkchecker");
156 run.default_condition(builder.config.docs)
159 fn make_run(run: RunConfig<'_>) {
160 run.builder.ensure(Linkcheck { host: run.target });
164 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
165 pub struct Cargotest {
167 host: TargetSelection,
170 impl Step for Cargotest {
172 const ONLY_HOSTS: bool = true;
174 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
175 run.path("src/tools/cargotest")
178 fn make_run(run: RunConfig<'_>) {
179 run.builder.ensure(Cargotest { stage: run.builder.top_stage, host: run.target });
182 /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler.
184 /// This tool in `src/tools` will check out a few Rust projects and run `cargo
185 /// test` to ensure that we don't regress the test suites there.
186 fn run(self, builder: &Builder<'_>) {
187 let compiler = builder.compiler(self.stage, self.host);
188 builder.ensure(compile::Rustc { compiler, target: compiler.host });
189 let cargo = builder.ensure(tool::Cargo { compiler, target: compiler.host });
191 // Note that this is a short, cryptic, and not scoped directory name. This
192 // is currently to minimize the length of path on Windows where we otherwise
193 // quickly run into path name limit constraints.
194 let out_dir = builder.out.join("ct");
195 t!(fs::create_dir_all(&out_dir));
197 let _time = util::timeit(&builder);
198 let mut cmd = builder.tool_cmd(Tool::CargoTest);
203 .args(builder.config.cmd.test_args())
204 .env("RUSTC", builder.rustc(compiler))
205 .env("RUSTDOC", builder.rustdoc(compiler)),
210 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
213 host: TargetSelection,
216 impl Step for Cargo {
218 const ONLY_HOSTS: bool = true;
220 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
221 run.path("src/tools/cargo")
224 fn make_run(run: RunConfig<'_>) {
225 run.builder.ensure(Cargo { stage: run.builder.top_stage, host: run.target });
228 /// Runs `cargo test` for `cargo` packaged with Rust.
229 fn run(self, builder: &Builder<'_>) {
230 let compiler = builder.compiler(self.stage, self.host);
232 builder.ensure(tool::Cargo { compiler, target: self.host });
233 let mut cargo = tool::prepare_tool_cargo(
240 SourceType::Submodule,
244 if !builder.fail_fast {
245 cargo.arg("--no-fail-fast");
247 cargo.arg("--").args(builder.config.cmd.test_args());
249 // Don't run cross-compile tests, we may not have cross-compiled libstd libs
251 cargo.env("CFG_DISABLE_CROSS_TESTS", "1");
252 // Disable a test that has issues with mingw.
253 cargo.env("CARGO_TEST_DISABLE_GIT_CLI", "1");
254 // Forcibly disable tests using nightly features since any changes to
255 // those features won't be able to land.
256 cargo.env("CARGO_TEST_DISABLE_NIGHTLY", "1");
258 cargo.env("PATH", &path_for_cargo(builder, compiler));
260 try_run(builder, &mut cargo.into());
264 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
267 host: TargetSelection,
272 const ONLY_HOSTS: bool = true;
274 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
275 run.path("src/tools/rls")
278 fn make_run(run: RunConfig<'_>) {
279 run.builder.ensure(Rls { stage: run.builder.top_stage, host: run.target });
282 /// Runs `cargo test` for the rls.
283 fn run(self, builder: &Builder<'_>) {
284 let stage = self.stage;
285 let host = self.host;
286 let compiler = builder.compiler(stage, host);
289 builder.ensure(tool::Rls { compiler, target: self.host, extra_features: Vec::new() });
290 if build_result.is_none() {
291 eprintln!("failed to test rls: could not build");
295 let mut cargo = tool::prepare_tool_cargo(
302 SourceType::Submodule,
306 cargo.add_rustc_lib_path(builder, compiler);
307 cargo.arg("--").args(builder.config.cmd.test_args());
309 if try_run(builder, &mut cargo.into()) {
310 builder.save_toolstate("rls", ToolState::TestPass);
315 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
318 host: TargetSelection,
321 impl Step for Rustfmt {
323 const ONLY_HOSTS: bool = true;
325 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
326 run.path("src/tools/rustfmt")
329 fn make_run(run: RunConfig<'_>) {
330 run.builder.ensure(Rustfmt { stage: run.builder.top_stage, host: run.target });
333 /// Runs `cargo test` for rustfmt.
334 fn run(self, builder: &Builder<'_>) {
335 let stage = self.stage;
336 let host = self.host;
337 let compiler = builder.compiler(stage, host);
340 .ensure(tool::Rustfmt { compiler, target: self.host, extra_features: Vec::new() })
341 .expect("in-tree tool");
343 let mut cargo = tool::prepare_tool_cargo(
350 SourceType::Submodule,
354 let dir = testdir(builder, compiler.host);
355 t!(fs::create_dir_all(&dir));
356 cargo.env("RUSTFMT_TEST_DIR", dir);
358 cargo.add_rustc_lib_path(builder, compiler);
360 builder.run(&mut cargo.into());
364 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
365 pub struct RustDemangler {
367 host: TargetSelection,
370 impl Step for RustDemangler {
372 const ONLY_HOSTS: bool = true;
374 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
375 run.path("src/tools/rust-demangler")
378 fn make_run(run: RunConfig<'_>) {
379 run.builder.ensure(RustDemangler { stage: run.builder.top_stage, host: run.target });
382 /// Runs `cargo test` for rust-demangler.
383 fn run(self, builder: &Builder<'_>) {
384 let stage = self.stage;
385 let host = self.host;
386 let compiler = builder.compiler(stage, host);
388 let rust_demangler = builder
389 .ensure(tool::RustDemangler { compiler, target: self.host, extra_features: Vec::new() })
390 .expect("in-tree tool");
391 let mut cargo = tool::prepare_tool_cargo(
397 "src/tools/rust-demangler",
402 let dir = testdir(builder, compiler.host);
403 t!(fs::create_dir_all(&dir));
405 cargo.env("RUST_DEMANGLER_DRIVER_PATH", rust_demangler);
407 cargo.arg("--").args(builder.config.cmd.test_args());
409 cargo.add_rustc_lib_path(builder, compiler);
411 builder.run(&mut cargo.into());
415 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
418 host: TargetSelection,
423 const ONLY_HOSTS: bool = true;
425 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
426 run.path("src/tools/miri")
429 fn make_run(run: RunConfig<'_>) {
430 run.builder.ensure(Miri { stage: run.builder.top_stage, host: run.target });
433 /// Runs `cargo test` for miri.
434 fn run(self, builder: &Builder<'_>) {
435 let stage = self.stage;
436 let host = self.host;
437 let compiler = builder.compiler(stage, host);
440 builder.ensure(tool::Miri { compiler, target: self.host, extra_features: Vec::new() });
441 let cargo_miri = builder.ensure(tool::CargoMiri {
444 extra_features: Vec::new(),
446 if let (Some(miri), Some(_cargo_miri)) = (miri, cargo_miri) {
448 builder.cargo(compiler, Mode::ToolRustc, SourceType::Submodule, host, "install");
450 // Configure `cargo install` path. cargo adds a `bin/`.
451 cargo.env("CARGO_INSTALL_ROOT", &builder.out);
453 let mut cargo = Command::from(cargo);
454 if !try_run(builder, &mut cargo) {
458 // # Run `cargo miri setup`.
459 let mut cargo = tool::prepare_tool_cargo(
465 "src/tools/miri/cargo-miri",
466 SourceType::Submodule,
469 cargo.add_rustc_lib_path(builder, compiler);
470 cargo.arg("--").arg("miri").arg("setup");
472 // Tell `cargo miri setup` where to find the sources.
473 cargo.env("XARGO_RUST_SRC", builder.src.join("library"));
474 // Tell it where to find Miri.
475 cargo.env("MIRI", &miri);
477 cargo.env("RUST_BACKTRACE", "1");
478 // Let cargo-miri know where xargo ended up.
479 cargo.env("XARGO_CHECK", builder.out.join("bin").join("xargo-check"));
481 let mut cargo = Command::from(cargo);
482 if !try_run(builder, &mut cargo) {
486 // # Determine where Miri put its sysroot.
487 // To this end, we run `cargo miri setup --print-sysroot` and capture the output.
488 // (We do this separately from the above so that when the setup actually
489 // happens we get some output.)
490 // We re-use the `cargo` from above.
491 cargo.arg("--print-sysroot");
493 // FIXME: Is there a way in which we can re-use the usual `run` helpers?
494 let miri_sysroot = if builder.config.dry_run {
497 builder.verbose(&format!("running: {:?}", cargo));
500 .expect("We already ran `cargo miri setup` before and that worked");
501 assert!(out.status.success(), "`cargo miri setup` returned with non-0 exit code");
502 // Output is "<sysroot>\n".
503 let stdout = String::from_utf8(out.stdout)
504 .expect("`cargo miri setup` stdout is not valid UTF-8");
505 let sysroot = stdout.trim_end();
506 builder.verbose(&format!("`cargo miri setup --print-sysroot` said: {:?}", sysroot));
510 // # Run `cargo test`.
511 let mut cargo = tool::prepare_tool_cargo(
518 SourceType::Submodule,
521 cargo.add_rustc_lib_path(builder, compiler);
523 // miri tests need to know about the stage sysroot
524 cargo.env("MIRI_SYSROOT", miri_sysroot);
525 cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
526 cargo.env("MIRI", miri);
528 cargo.arg("--").args(builder.config.cmd.test_args());
530 let mut cargo = Command::from(cargo);
531 if !try_run(builder, &mut cargo) {
535 // # Run `cargo test` with `-Zmir-opt-level=4`.
536 cargo.env("MIRIFLAGS", "-O -Zmir-opt-level=4");
537 if !try_run(builder, &mut cargo) {
542 builder.save_toolstate("miri", ToolState::TestPass);
544 eprintln!("failed to test miri: could not build");
549 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
550 pub struct CompiletestTest {
551 host: TargetSelection,
554 impl Step for CompiletestTest {
557 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
558 run.path("src/tools/compiletest")
561 fn make_run(run: RunConfig<'_>) {
562 run.builder.ensure(CompiletestTest { host: run.target });
565 /// Runs `cargo test` for compiletest.
566 fn run(self, builder: &Builder<'_>) {
567 let host = self.host;
568 let compiler = builder.compiler(0, host);
570 // We need `ToolStd` for the locally-built sysroot because
571 // compiletest uses unstable features of the `test` crate.
572 builder.ensure(compile::Std { compiler, target: host });
573 let cargo = tool::prepare_tool_cargo(
579 "src/tools/compiletest",
584 try_run(builder, &mut cargo.into());
588 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
591 host: TargetSelection,
594 impl Step for Clippy {
596 const ONLY_HOSTS: bool = true;
597 const DEFAULT: bool = false;
599 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
600 run.path("src/tools/clippy")
603 fn make_run(run: RunConfig<'_>) {
604 run.builder.ensure(Clippy { stage: run.builder.top_stage, host: run.target });
607 /// Runs `cargo test` for clippy.
608 fn run(self, builder: &Builder<'_>) {
609 let stage = self.stage;
610 let host = self.host;
611 let compiler = builder.compiler(stage, host);
614 .ensure(tool::Clippy { compiler, target: self.host, extra_features: Vec::new() })
615 .expect("in-tree tool");
616 let mut cargo = tool::prepare_tool_cargo(
627 // clippy tests need to know about the stage sysroot
628 cargo.env("SYSROOT", builder.sysroot(compiler));
629 cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
630 cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
631 let host_libs = builder.stage_out(compiler, Mode::ToolRustc).join(builder.cargo_dir());
632 let target_libs = builder
633 .stage_out(compiler, Mode::ToolRustc)
634 .join(&self.host.triple)
635 .join(builder.cargo_dir());
636 cargo.env("HOST_LIBS", host_libs);
637 cargo.env("TARGET_LIBS", target_libs);
638 // clippy tests need to find the driver
639 cargo.env("CLIPPY_DRIVER_PATH", clippy);
641 cargo.arg("--").args(builder.config.cmd.test_args());
643 cargo.add_rustc_lib_path(builder, compiler);
645 if builder.try_run(&mut cargo.into()) {
646 // The tests succeeded; nothing to do.
650 if !builder.config.cmd.bless() {
651 std::process::exit(1);
654 let mut cargo = builder.cargo(compiler, Mode::ToolRustc, SourceType::InTree, host, "run");
655 cargo.arg("-p").arg("clippy_dev");
656 // clippy_dev gets confused if it can't find `clippy/Cargo.toml`
657 cargo.current_dir(&builder.src.join("src").join("tools").join("clippy"));
658 if builder.config.rust_optimize {
659 cargo.env("PROFILE", "release");
661 cargo.env("PROFILE", "debug");
665 builder.run(&mut cargo.into());
669 fn path_for_cargo(builder: &Builder<'_>, compiler: Compiler) -> OsString {
670 // Configure PATH to find the right rustc. NB. we have to use PATH
671 // and not RUSTC because the Cargo test suite has tests that will
672 // fail if rustc is not spelled `rustc`.
673 let path = builder.sysroot(compiler).join("bin");
674 let old_path = env::var_os("PATH").unwrap_or_default();
675 env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("")
678 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
679 pub struct RustdocTheme {
680 pub compiler: Compiler,
683 impl Step for RustdocTheme {
685 const DEFAULT: bool = true;
686 const ONLY_HOSTS: bool = true;
688 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
689 run.path("src/tools/rustdoc-themes")
692 fn make_run(run: RunConfig<'_>) {
693 let compiler = run.builder.compiler(run.builder.top_stage, run.target);
695 run.builder.ensure(RustdocTheme { compiler });
698 fn run(self, builder: &Builder<'_>) {
699 let rustdoc = builder.out.join("bootstrap/debug/rustdoc");
700 let mut cmd = builder.tool_cmd(Tool::RustdocTheme);
701 cmd.arg(rustdoc.to_str().unwrap())
702 .arg(builder.src.join("src/librustdoc/html/static/themes").to_str().unwrap())
703 .env("RUSTC_STAGE", self.compiler.stage.to_string())
704 .env("RUSTC_SYSROOT", builder.sysroot(self.compiler))
705 .env("RUSTDOC_LIBDIR", builder.sysroot_libdir(self.compiler, self.compiler.host))
706 .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
707 .env("RUSTDOC_REAL", builder.rustdoc(self.compiler))
708 .env("RUSTC_BOOTSTRAP", "1");
709 if let Some(linker) = builder.linker(self.compiler.host) {
710 cmd.env("RUSTDOC_LINKER", linker);
712 if builder.is_fuse_ld_lld(self.compiler.host) {
713 cmd.env("RUSTDOC_FUSE_LD_LLD", "1");
715 try_run(builder, &mut cmd);
719 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
720 pub struct RustdocJSStd {
721 pub target: TargetSelection,
724 impl Step for RustdocJSStd {
726 const DEFAULT: bool = true;
727 const ONLY_HOSTS: bool = true;
729 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
730 run.path("src/test/rustdoc-js-std")
733 fn make_run(run: RunConfig<'_>) {
734 run.builder.ensure(RustdocJSStd { target: run.target });
737 fn run(self, builder: &Builder<'_>) {
738 if let Some(ref nodejs) = builder.config.nodejs {
739 let mut command = Command::new(nodejs);
741 .arg(builder.src.join("src/tools/rustdoc-js/tester.js"))
744 .arg("--resource-suffix")
745 .arg(&builder.version)
747 .arg(builder.doc_out(self.target))
748 .arg("--test-folder")
749 .arg(builder.src.join("src/test/rustdoc-js-std"));
750 builder.ensure(crate::doc::Std { target: self.target, stage: builder.top_stage });
751 builder.run(&mut command);
753 builder.info("No nodejs found, skipping \"src/test/rustdoc-js-std\" tests");
758 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
759 pub struct RustdocJSNotStd {
760 pub target: TargetSelection,
761 pub compiler: Compiler,
764 impl Step for RustdocJSNotStd {
766 const DEFAULT: bool = true;
767 const ONLY_HOSTS: bool = true;
769 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
770 run.path("src/test/rustdoc-js")
773 fn make_run(run: RunConfig<'_>) {
774 let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
775 run.builder.ensure(RustdocJSNotStd { target: run.target, compiler });
778 fn run(self, builder: &Builder<'_>) {
779 if builder.config.nodejs.is_some() {
780 builder.ensure(Compiletest {
781 compiler: self.compiler,
785 path: "src/test/rustdoc-js",
789 builder.info("No nodejs found, skipping \"src/test/rustdoc-js\" tests");
794 fn check_if_browser_ui_test_is_installed_global(npm: &Path, global: bool) -> bool {
795 let mut command = Command::new(&npm);
796 command.arg("list").arg("--depth=0");
798 command.arg("--global");
802 .map(|output| String::from_utf8_lossy(&output.stdout).into_owned())
803 .unwrap_or(String::new());
804 lines.contains(&" browser-ui-test@")
807 fn check_if_browser_ui_test_is_installed(npm: &Path) -> bool {
808 check_if_browser_ui_test_is_installed_global(npm, false)
809 || check_if_browser_ui_test_is_installed_global(npm, true)
812 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
813 pub struct RustdocGUI {
814 pub target: TargetSelection,
815 pub compiler: Compiler,
818 impl Step for RustdocGUI {
820 const DEFAULT: bool = true;
821 const ONLY_HOSTS: bool = true;
823 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
824 let builder = run.builder;
825 let run = run.suite_path("src/test/rustdoc-gui");
826 run.lazy_default_condition(Box::new(move || {
827 builder.config.nodejs.is_some()
832 .map(|p| check_if_browser_ui_test_is_installed(p))
837 fn make_run(run: RunConfig<'_>) {
838 let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
839 run.builder.ensure(RustdocGUI { target: run.target, compiler });
842 fn run(self, builder: &Builder<'_>) {
843 let nodejs = builder.config.nodejs.as_ref().expect("nodejs isn't available");
844 let npm = builder.config.npm.as_ref().expect("npm isn't available");
846 builder.ensure(compile::Std { compiler: self.compiler, target: self.target });
848 // The goal here is to check if the necessary packages are installed, and if not, we
850 if !check_if_browser_ui_test_is_installed(&npm) {
852 "error: rustdoc-gui test suite cannot be run because npm `browser-ui-test` \
853 dependency is missing",
856 "If you want to install the `{0}` dependency, run `npm install {0}`",
859 panic!("Cannot run rustdoc-gui tests");
862 let out_dir = builder.test_out(self.target).join("rustdoc-gui");
864 // We remove existing folder to be sure there won't be artifacts remaining.
865 let _ = fs::remove_dir_all(&out_dir);
867 let mut nb_generated = 0;
868 // We generate docs for the libraries present in the rustdoc-gui's src folder.
869 let libs_dir = builder.build.src.join("src/test/rustdoc-gui/src");
870 for entry in libs_dir.read_dir().expect("read_dir call failed") {
871 let entry = entry.expect("invalid entry");
872 let path = entry.path();
873 if path.extension().map(|e| e == "rs").unwrap_or(false) {
874 let mut command = builder.rustdoc_cmd(self.compiler);
875 command.arg(path).arg("-o").arg(&out_dir);
876 builder.run(&mut command);
880 assert!(nb_generated > 0, "no documentation was generated...");
882 // We now run GUI tests.
883 let mut command = Command::new(&nodejs);
885 .arg(builder.build.src.join("src/tools/rustdoc-gui/tester.js"))
888 .arg("--tests-folder")
889 .arg(builder.build.src.join("src/test/rustdoc-gui"));
890 for path in &builder.paths {
891 if let Some(name) = path.file_name().and_then(|f| f.to_str()) {
892 if name.ends_with(".goml") {
893 command.arg("--file").arg(name);
897 for test_arg in builder.config.cmd.test_args() {
898 command.arg(test_arg);
900 builder.run(&mut command);
904 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
909 const DEFAULT: bool = true;
910 const ONLY_HOSTS: bool = true;
912 /// Runs the `tidy` tool.
914 /// This tool in `src/tools` checks up on various bits and pieces of style and
915 /// otherwise just implements a few lint-like checks that are specific to the
918 /// Once tidy passes, this step also runs `fmt --check` if tests are being run
919 /// for the `dev` or `nightly` channels.
920 fn run(self, builder: &Builder<'_>) {
921 let mut cmd = builder.tool_cmd(Tool::Tidy);
922 cmd.arg(&builder.src);
923 cmd.arg(&builder.initial_cargo);
924 cmd.arg(&builder.out);
925 cmd.arg(builder.jobs().to_string());
926 if builder.is_verbose() {
927 cmd.arg("--verbose");
930 builder.info("tidy check");
931 try_run(builder, &mut cmd);
933 if builder.config.channel == "dev" || builder.config.channel == "nightly" {
934 builder.info("fmt check");
935 if builder.config.initial_rustfmt.is_none() {
936 let inferred_rustfmt_dir = builder.config.initial_rustc.parent().unwrap();
939 error: no `rustfmt` binary found in {PATH}
940 info: `rust.channel` is currently set to \"{CHAN}\"
941 help: if you are testing a beta branch, set `rust.channel` to \"beta\" in the `config.toml` file
942 help: to skip test's attempt to check tidiness, pass `--exclude src/tools/tidy` to `x.py test`",
943 PATH = inferred_rustfmt_dir.display(),
944 CHAN = builder.config.channel,
946 std::process::exit(1);
948 crate::format::format(&builder.build, !builder.config.cmd.bless(), &[]);
952 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
953 run.path("src/tools/tidy")
956 fn make_run(run: RunConfig<'_>) {
957 run.builder.ensure(Tidy);
961 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
962 pub struct ExpandYamlAnchors;
964 impl Step for ExpandYamlAnchors {
966 const ONLY_HOSTS: bool = true;
968 /// Ensure the `generate-ci-config` tool was run locally.
970 /// The tool in `src/tools` reads the CI definition in `src/ci/builders.yml` and generates the
971 /// appropriate configuration for all our CI providers. This step ensures the tool was called
972 /// by the user before committing CI changes.
973 fn run(self, builder: &Builder<'_>) {
974 builder.info("Ensuring the YAML anchors in the GitHub Actions config were expanded");
977 &mut builder.tool_cmd(Tool::ExpandYamlAnchors).arg("check").arg(&builder.src),
981 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
982 run.path("src/tools/expand-yaml-anchors")
985 fn make_run(run: RunConfig<'_>) {
986 run.builder.ensure(ExpandYamlAnchors);
990 fn testdir(builder: &Builder<'_>, host: TargetSelection) -> PathBuf {
991 builder.out.join(host.triple).join("test")
994 macro_rules! default_test {
995 ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
996 test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: false });
1000 macro_rules! default_test_with_compare_mode {
1001 ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr,
1002 compare_mode: $compare_mode:expr }) => {
1003 test_with_compare_mode!($name {
1009 compare_mode: $compare_mode
1014 macro_rules! host_test {
1015 ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
1016 test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: true });
1021 ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr, default: $default:expr,
1022 host: $host:expr }) => {
1023 test_definitions!($name {
1034 macro_rules! test_with_compare_mode {
1035 ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr, default: $default:expr,
1036 host: $host:expr, compare_mode: $compare_mode:expr }) => {
1037 test_definitions!($name {
1043 compare_mode: Some($compare_mode)
1048 macro_rules! test_definitions {
1053 default: $default:expr,
1055 compare_mode: $compare_mode:expr
1057 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1059 pub compiler: Compiler,
1060 pub target: TargetSelection,
1063 impl Step for $name {
1065 const DEFAULT: bool = $default;
1066 const ONLY_HOSTS: bool = $host;
1068 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1069 run.suite_path($path)
1072 fn make_run(run: RunConfig<'_>) {
1073 let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple());
1075 run.builder.ensure($name { compiler, target: run.target });
1078 fn run(self, builder: &Builder<'_>) {
1079 builder.ensure(Compiletest {
1080 compiler: self.compiler,
1081 target: self.target,
1085 compare_mode: $compare_mode,
1092 default_test_with_compare_mode!(Ui {
1093 path: "src/test/ui",
1099 default_test!(RunPassValgrind {
1100 path: "src/test/run-pass-valgrind",
1101 mode: "run-pass-valgrind",
1102 suite: "run-pass-valgrind"
1105 default_test!(MirOpt { path: "src/test/mir-opt", mode: "mir-opt", suite: "mir-opt" });
1107 default_test!(Codegen { path: "src/test/codegen", mode: "codegen", suite: "codegen" });
1109 default_test!(CodegenUnits {
1110 path: "src/test/codegen-units",
1111 mode: "codegen-units",
1112 suite: "codegen-units"
1115 default_test!(Incremental {
1116 path: "src/test/incremental",
1117 mode: "incremental",
1118 suite: "incremental"
1121 default_test_with_compare_mode!(Debuginfo {
1122 path: "src/test/debuginfo",
1125 compare_mode: "split-dwarf"
1128 host_test!(UiFullDeps { path: "src/test/ui-fulldeps", mode: "ui", suite: "ui-fulldeps" });
1130 host_test!(Rustdoc { path: "src/test/rustdoc", mode: "rustdoc", suite: "rustdoc" });
1131 host_test!(RustdocUi { path: "src/test/rustdoc-ui", mode: "ui", suite: "rustdoc-ui" });
1133 host_test!(RustdocJson {
1134 path: "src/test/rustdoc-json",
1135 mode: "rustdoc-json",
1136 suite: "rustdoc-json"
1139 host_test!(Pretty { path: "src/test/pretty", mode: "pretty", suite: "pretty" });
1141 default_test!(RunMake { path: "src/test/run-make", mode: "run-make", suite: "run-make" });
1143 host_test!(RunMakeFullDeps {
1144 path: "src/test/run-make-fulldeps",
1146 suite: "run-make-fulldeps"
1149 default_test!(Assembly { path: "src/test/assembly", mode: "assembly", suite: "assembly" });
1151 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1152 struct Compiletest {
1154 target: TargetSelection,
1156 suite: &'static str,
1158 compare_mode: Option<&'static str>,
1161 impl Step for Compiletest {
1164 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1168 /// Executes the `compiletest` tool to run a suite of tests.
1170 /// Compiles all tests with `compiler` for `target` with the specified
1171 /// compiletest `mode` and `suite` arguments. For example `mode` can be
1172 /// "run-pass" or `suite` can be something like `debuginfo`.
1173 fn run(self, builder: &Builder<'_>) {
1174 if builder.top_stage == 0 && env::var("COMPILETEST_FORCE_STAGE0").is_err() {
1176 error: `--stage 0` runs compiletest on the beta compiler, not your local changes, and will almost always cause tests to fail
1177 help: to test the compiler, use `--stage 1` instead
1178 help: to test the standard library, use `--stage 0 library/std` instead
1179 note: if you're sure you want to do this, please open an issue as to why. In the meantime, you can override this with `COMPILETEST_FORCE_STAGE0=1`."
1181 std::process::exit(1);
1184 let compiler = self.compiler;
1185 let target = self.target;
1186 let mode = self.mode;
1187 let suite = self.suite;
1189 // Path for test suite
1190 let suite_path = self.path;
1192 // Skip codegen tests if they aren't enabled in configuration.
1193 if !builder.config.codegen_tests && suite == "codegen" {
1197 if suite == "debuginfo" {
1199 .ensure(dist::DebuggerScripts { sysroot: builder.sysroot(compiler), host: target });
1202 if suite.ends_with("fulldeps") {
1203 builder.ensure(compile::Rustc { compiler, target });
1206 builder.ensure(compile::Std { compiler, target });
1207 // ensure that `libproc_macro` is available on the host.
1208 builder.ensure(compile::Std { compiler, target: compiler.host });
1210 // Also provide `rust_test_helpers` for the host.
1211 builder.ensure(native::TestHelpers { target: compiler.host });
1213 // As well as the target, except for plain wasm32, which can't build it
1214 if !target.contains("wasm32") || target.contains("emscripten") {
1215 builder.ensure(native::TestHelpers { target });
1218 builder.ensure(RemoteCopyLibs { compiler, target });
1220 let mut cmd = builder.tool_cmd(Tool::Compiletest);
1222 // compiletest currently has... a lot of arguments, so let's just pass all
1225 cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(compiler));
1226 cmd.arg("--run-lib-path").arg(builder.sysroot_libdir(compiler, target));
1227 cmd.arg("--rustc-path").arg(builder.rustc(compiler));
1229 let is_rustdoc = suite.ends_with("rustdoc-ui") || suite.ends_with("rustdoc-js");
1231 // Avoid depending on rustdoc when we don't need it.
1232 if mode == "rustdoc"
1233 || mode == "run-make"
1234 || (mode == "ui" && is_rustdoc)
1235 || mode == "js-doc-test"
1236 || mode == "rustdoc-json"
1238 cmd.arg("--rustdoc-path").arg(builder.rustdoc(compiler));
1241 if mode == "rustdoc-json" {
1242 // Use the beta compiler for jsondocck
1243 let json_compiler = compiler.with_stage(0);
1244 cmd.arg("--jsondocck-path")
1245 .arg(builder.ensure(tool::JsonDocCk { compiler: json_compiler, target }));
1248 if mode == "run-make" && suite.ends_with("fulldeps") {
1249 let rust_demangler = builder
1250 .ensure(tool::RustDemangler { compiler, target, extra_features: Vec::new() })
1251 .expect("in-tree tool");
1252 cmd.arg("--rust-demangler-path").arg(rust_demangler);
1255 cmd.arg("--src-base").arg(builder.src.join("src/test").join(suite));
1256 cmd.arg("--build-base").arg(testdir(builder, compiler.host).join(suite));
1257 cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target));
1258 cmd.arg("--suite").arg(suite);
1259 cmd.arg("--mode").arg(mode);
1260 cmd.arg("--target").arg(target.rustc_target_arg());
1261 cmd.arg("--host").arg(&*compiler.host.triple);
1262 cmd.arg("--llvm-filecheck").arg(builder.llvm_filecheck(builder.config.build));
1264 if builder.config.cmd.bless() {
1269 builder.config.cmd.compare_mode().or_else(|| {
1270 if builder.config.test_compare_mode { self.compare_mode } else { None }
1273 if let Some(ref pass) = builder.config.cmd.pass() {
1278 if let Some(ref run) = builder.config.cmd.run() {
1283 if let Some(ref nodejs) = builder.config.nodejs {
1284 cmd.arg("--nodejs").arg(nodejs);
1286 if let Some(ref npm) = builder.config.npm {
1287 cmd.arg("--npm").arg(npm);
1290 let mut flags = if is_rustdoc { Vec::new() } else { vec!["-Crpath".to_string()] };
1292 if builder.config.rust_optimize_tests {
1293 flags.push("-O".to_string());
1296 flags.push(format!("-Cdebuginfo={}", builder.config.rust_debuginfo_level_tests));
1297 flags.push(builder.config.cmd.rustc_args().join(" "));
1299 if let Some(linker) = builder.linker(target) {
1300 cmd.arg("--linker").arg(linker);
1303 let mut hostflags = flags.clone();
1304 hostflags.push(format!("-Lnative={}", builder.test_helpers_out(compiler.host).display()));
1305 hostflags.extend(builder.lld_flags(compiler.host));
1306 cmd.arg("--host-rustcflags").arg(hostflags.join(" "));
1308 let mut targetflags = flags;
1309 targetflags.push(format!("-Lnative={}", builder.test_helpers_out(target).display()));
1310 targetflags.extend(builder.lld_flags(target));
1311 cmd.arg("--target-rustcflags").arg(targetflags.join(" "));
1313 cmd.arg("--docck-python").arg(builder.python());
1315 if builder.config.build.ends_with("apple-darwin") {
1316 // Force /usr/bin/python3 on macOS for LLDB tests because we're loading the
1317 // LLDB plugin's compiled module which only works with the system python
1318 // (namely not Homebrew-installed python)
1319 cmd.arg("--lldb-python").arg("/usr/bin/python3");
1321 cmd.arg("--lldb-python").arg(builder.python());
1324 if let Some(ref gdb) = builder.config.gdb {
1325 cmd.arg("--gdb").arg(gdb);
1328 let run = |cmd: &mut Command| {
1329 cmd.output().map(|output| {
1330 String::from_utf8_lossy(&output.stdout)
1333 .unwrap_or_else(|| panic!("{:?} failed {:?}", cmd, output))
1337 let lldb_exe = "lldb";
1338 let lldb_version = Command::new(lldb_exe)
1341 .map(|output| String::from_utf8_lossy(&output.stdout).to_string())
1343 if let Some(ref vers) = lldb_version {
1344 cmd.arg("--lldb-version").arg(vers);
1345 let lldb_python_dir = run(Command::new(lldb_exe).arg("-P")).ok();
1346 if let Some(ref dir) = lldb_python_dir {
1347 cmd.arg("--lldb-python-dir").arg(dir);
1351 if util::forcing_clang_based_tests() {
1352 let clang_exe = builder.llvm_out(target).join("bin").join("clang");
1353 cmd.arg("--run-clang-based-tests-with").arg(clang_exe);
1356 // Get paths from cmd args
1357 let paths = match &builder.config.cmd {
1358 Subcommand::Test { ref paths, .. } => &paths[..],
1362 // Get test-args by striping suite path
1363 let mut test_args: Vec<&str> = paths
1365 .map(|p| match p.strip_prefix(".") {
1369 .filter(|p| p.starts_with(suite_path))
1371 let exists = p.is_dir() || p.is_file();
1373 if let Some(p) = p.to_str() {
1374 builder.info(&format!(
1375 "Warning: Skipping \"{}\": not a regular file or directory",
1383 // Since test suite paths are themselves directories, if we don't
1384 // specify a directory or file, we'll get an empty string here
1385 // (the result of the test suite directory without its suite prefix).
1386 // Therefore, we need to filter these out, as only the first --test-args
1387 // flag is respected, so providing an empty --test-args conflicts with
1388 // any following it.
1389 match p.strip_prefix(suite_path).ok().and_then(|p| p.to_str()) {
1390 Some(s) if !s.is_empty() => Some(s),
1396 test_args.append(&mut builder.config.cmd.test_args());
1398 cmd.args(&test_args);
1400 if builder.is_verbose() {
1401 cmd.arg("--verbose");
1404 if !builder.config.verbose_tests {
1408 let mut llvm_components_passed = false;
1409 let mut copts_passed = false;
1410 if builder.config.llvm_enabled() {
1411 let llvm_config = builder.ensure(native::Llvm { target: builder.config.build });
1412 if !builder.config.dry_run {
1413 let llvm_version = output(Command::new(&llvm_config).arg("--version"));
1414 let llvm_components = output(Command::new(&llvm_config).arg("--components"));
1415 // Remove trailing newline from llvm-config output.
1416 cmd.arg("--llvm-version")
1417 .arg(llvm_version.trim())
1418 .arg("--llvm-components")
1419 .arg(llvm_components.trim());
1420 llvm_components_passed = true;
1422 if !builder.is_rust_llvm(target) {
1423 cmd.arg("--system-llvm");
1426 // Tests that use compiler libraries may inherit the `-lLLVM` link
1427 // requirement, but the `-L` library path is not propagated across
1428 // separate compilations. We can add LLVM's library path to the
1429 // platform-specific environment variable as a workaround.
1430 if !builder.config.dry_run && suite.ends_with("fulldeps") {
1431 let llvm_libdir = output(Command::new(&llvm_config).arg("--libdir"));
1432 add_link_lib_path(vec![llvm_libdir.trim().into()], &mut cmd);
1435 // Only pass correct values for these flags for the `run-make` suite as it
1436 // requires that a C++ compiler was configured which isn't always the case.
1437 if !builder.config.dry_run && matches!(suite, "run-make" | "run-make-fulldeps") {
1438 // The llvm/bin directory contains many useful cross-platform
1439 // tools. Pass the path to run-make tests so they can use them.
1440 let llvm_bin_path = llvm_config
1442 .expect("Expected llvm-config to be contained in directory");
1443 assert!(llvm_bin_path.is_dir());
1444 cmd.arg("--llvm-bin-dir").arg(llvm_bin_path);
1446 // If LLD is available, add it to the PATH
1447 if builder.config.lld_enabled {
1448 let lld_install_root =
1449 builder.ensure(native::Lld { target: builder.config.build });
1451 let lld_bin_path = lld_install_root.join("bin");
1453 let old_path = env::var_os("PATH").unwrap_or_default();
1454 let new_path = env::join_paths(
1455 std::iter::once(lld_bin_path).chain(env::split_paths(&old_path)),
1457 .expect("Could not add LLD bin path to PATH");
1458 cmd.env("PATH", new_path);
1463 // Only pass correct values for these flags for the `run-make` suite as it
1464 // requires that a C++ compiler was configured which isn't always the case.
1465 if !builder.config.dry_run && matches!(suite, "run-make" | "run-make-fulldeps") {
1467 .arg(builder.cc(target))
1469 .arg(builder.cxx(target).unwrap())
1471 .arg(builder.cflags(target, GitRepo::Rustc).join(" "));
1472 copts_passed = true;
1473 if let Some(ar) = builder.ar(target) {
1474 cmd.arg("--ar").arg(ar);
1478 if !llvm_components_passed {
1479 cmd.arg("--llvm-components").arg("");
1482 cmd.arg("--cc").arg("").arg("--cxx").arg("").arg("--cflags").arg("");
1485 if builder.remote_tested(target) {
1486 cmd.arg("--remote-test-client").arg(builder.tool_exe(Tool::RemoteTestClient));
1489 // Running a C compiler on MSVC requires a few env vars to be set, to be
1490 // sure to set them here.
1492 // Note that if we encounter `PATH` we make sure to append to our own `PATH`
1493 // rather than stomp over it.
1494 if target.contains("msvc") {
1495 for &(ref k, ref v) in builder.cc[&target].env() {
1501 cmd.env("RUSTC_BOOTSTRAP", "1");
1502 cmd.env("DOC_RUST_LANG_ORG_CHANNEL", builder.doc_rust_lang_org_channel());
1503 builder.add_rust_test_threads(&mut cmd);
1505 if builder.config.sanitizers_enabled(target) {
1506 cmd.env("RUSTC_SANITIZER_SUPPORT", "1");
1509 if builder.config.profiler_enabled(target) {
1510 cmd.env("RUSTC_PROFILER_SUPPORT", "1");
1513 let tmp = builder.out.join("tmp");
1514 std::fs::create_dir_all(&tmp).unwrap();
1515 cmd.env("RUST_TEST_TMPDIR", tmp);
1517 cmd.arg("--adb-path").arg("adb");
1518 cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
1519 if target.contains("android") {
1520 // Assume that cc for this target comes from the android sysroot
1521 cmd.arg("--android-cross-path")
1522 .arg(builder.cc(target).parent().unwrap().parent().unwrap());
1524 cmd.arg("--android-cross-path").arg("");
1527 if builder.config.cmd.rustfix_coverage() {
1528 cmd.arg("--rustfix-coverage");
1531 cmd.env("BOOTSTRAP_CARGO", &builder.initial_cargo);
1533 cmd.arg("--channel").arg(&builder.config.channel);
1535 builder.ci_env.force_coloring_in_ci(&mut cmd);
1537 builder.info(&format!(
1538 "Check compiletest suite={} mode={} ({} -> {})",
1539 suite, mode, &compiler.host, target
1541 let _time = util::timeit(&builder);
1542 try_run(builder, &mut cmd);
1544 if let Some(compare_mode) = compare_mode {
1545 cmd.arg("--compare-mode").arg(compare_mode);
1546 builder.info(&format!(
1547 "Check compiletest suite={} mode={} compare_mode={} ({} -> {})",
1548 suite, mode, compare_mode, &compiler.host, target
1550 let _time = util::timeit(&builder);
1551 try_run(builder, &mut cmd);
1556 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
1564 impl Step for BookTest {
1566 const ONLY_HOSTS: bool = true;
1568 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1572 /// Runs the documentation tests for a book in `src/doc`.
1574 /// This uses the `rustdoc` that sits next to `compiler`.
1575 fn run(self, builder: &Builder<'_>) {
1576 // External docs are different from local because:
1577 // - Some books need pre-processing by mdbook before being tested.
1578 // - They need to save their state to toolstate.
1579 // - They are only tested on the "checktools" builders.
1581 // The local docs are tested by default, and we don't want to pay the
1582 // cost of building mdbook, so they use `rustdoc --test` directly.
1583 // Also, the unstable book is special because SUMMARY.md is generated,
1584 // so it is easier to just run `rustdoc` on its files.
1585 if self.is_ext_doc {
1586 self.run_ext_doc(builder);
1588 self.run_local_doc(builder);
1594 /// This runs the equivalent of `mdbook test` (via the rustbook wrapper)
1595 /// which in turn runs `rustdoc --test` on each file in the book.
1596 fn run_ext_doc(self, builder: &Builder<'_>) {
1597 let compiler = self.compiler;
1599 builder.ensure(compile::Std { compiler, target: compiler.host });
1601 // mdbook just executes a binary named "rustdoc", so we need to update
1602 // PATH so that it points to our rustdoc.
1603 let mut rustdoc_path = builder.rustdoc(compiler);
1605 let old_path = env::var_os("PATH").unwrap_or_default();
1606 let new_path = env::join_paths(iter::once(rustdoc_path).chain(env::split_paths(&old_path)))
1607 .expect("could not add rustdoc to PATH");
1609 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
1610 let path = builder.src.join(&self.path);
1611 rustbook_cmd.env("PATH", new_path).arg("test").arg(path);
1612 builder.add_rust_test_threads(&mut rustbook_cmd);
1613 builder.info(&format!("Testing rustbook {}", self.path.display()));
1614 let _time = util::timeit(&builder);
1615 let toolstate = if try_run(builder, &mut rustbook_cmd) {
1620 builder.save_toolstate(self.name, toolstate);
1623 /// This runs `rustdoc --test` on all `.md` files in the path.
1624 fn run_local_doc(self, builder: &Builder<'_>) {
1625 let compiler = self.compiler;
1627 builder.ensure(compile::Std { compiler, target: compiler.host });
1629 // Do a breadth-first traversal of the `src/doc` directory and just run
1630 // tests for all files that end in `*.md`
1631 let mut stack = vec![builder.src.join(self.path)];
1632 let _time = util::timeit(&builder);
1633 let mut files = Vec::new();
1634 while let Some(p) = stack.pop() {
1636 stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
1640 if p.extension().and_then(|s| s.to_str()) != Some("md") {
1650 markdown_test(builder, compiler, &file);
1655 macro_rules! test_book {
1656 ($($name:ident, $path:expr, $book_name:expr, default=$default:expr;)+) => {
1658 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1663 impl Step for $name {
1665 const DEFAULT: bool = $default;
1666 const ONLY_HOSTS: bool = true;
1668 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1672 fn make_run(run: RunConfig<'_>) {
1673 run.builder.ensure($name {
1674 compiler: run.builder.compiler(run.builder.top_stage, run.target),
1678 fn run(self, builder: &Builder<'_>) {
1679 builder.ensure(BookTest {
1680 compiler: self.compiler,
1681 path: PathBuf::from($path),
1683 is_ext_doc: !$default,
1692 Nomicon, "src/doc/nomicon", "nomicon", default=false;
1693 Reference, "src/doc/reference", "reference", default=false;
1694 RustdocBook, "src/doc/rustdoc", "rustdoc", default=true;
1695 RustcBook, "src/doc/rustc", "rustc", default=true;
1696 RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false;
1697 EmbeddedBook, "src/doc/embedded-book", "embedded-book", default=false;
1698 TheBook, "src/doc/book", "book", default=false;
1699 UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
1700 EditionGuide, "src/doc/edition-guide", "edition-guide", default=false;
1703 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1704 pub struct ErrorIndex {
1708 impl Step for ErrorIndex {
1710 const DEFAULT: bool = true;
1711 const ONLY_HOSTS: bool = true;
1713 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1714 run.path("src/tools/error_index_generator")
1717 fn make_run(run: RunConfig<'_>) {
1718 // error_index_generator depends on librustdoc. Use the compiler that
1719 // is normally used to build rustdoc for other tests (like compiletest
1720 // tests in src/test/rustdoc) so that it shares the same artifacts.
1721 let compiler = run.builder.compiler(run.builder.top_stage, run.builder.config.build);
1722 run.builder.ensure(ErrorIndex { compiler });
1725 /// Runs the error index generator tool to execute the tests located in the error
1728 /// The `error_index_generator` tool lives in `src/tools` and is used to
1729 /// generate a markdown file from the error indexes of the code base which is
1730 /// then passed to `rustdoc --test`.
1731 fn run(self, builder: &Builder<'_>) {
1732 let compiler = self.compiler;
1734 let dir = testdir(builder, compiler.host);
1735 t!(fs::create_dir_all(&dir));
1736 let output = dir.join("error-index.md");
1738 let mut tool = tool::ErrorIndex::command(builder);
1739 tool.arg("markdown").arg(&output);
1741 builder.info(&format!("Testing error-index stage{}", compiler.stage));
1742 let _time = util::timeit(&builder);
1743 builder.run_quiet(&mut tool);
1744 // The tests themselves need to link to std, so make sure it is
1746 builder.ensure(compile::Std { compiler, target: compiler.host });
1747 markdown_test(builder, compiler, &output);
1751 fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) -> bool {
1752 if let Ok(contents) = fs::read_to_string(markdown) {
1753 if !contents.contains("```") {
1758 builder.info(&format!("doc tests for: {}", markdown.display()));
1759 let mut cmd = builder.rustdoc_cmd(compiler);
1760 builder.add_rust_test_threads(&mut cmd);
1761 // allow for unstable options such as new editions
1763 cmd.arg("unstable-options");
1766 cmd.env("RUSTC_BOOTSTRAP", "1");
1768 let test_args = builder.config.cmd.test_args().join(" ");
1769 cmd.arg("--test-args").arg(test_args);
1771 if builder.config.verbose_tests {
1772 try_run(builder, &mut cmd)
1774 try_run_quiet(builder, &mut cmd)
1778 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1779 pub struct RustcGuide;
1781 impl Step for RustcGuide {
1783 const DEFAULT: bool = false;
1784 const ONLY_HOSTS: bool = true;
1786 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1787 run.path("src/doc/rustc-dev-guide")
1790 fn make_run(run: RunConfig<'_>) {
1791 run.builder.ensure(RustcGuide);
1794 fn run(self, builder: &Builder<'_>) {
1795 let src = builder.src.join("src/doc/rustc-dev-guide");
1796 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
1797 let toolstate = if try_run(builder, rustbook_cmd.arg("linkcheck").arg(&src)) {
1802 builder.save_toolstate("rustc-dev-guide", toolstate);
1806 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1807 pub struct CrateLibrustc {
1809 target: TargetSelection,
1810 test_kind: TestKind,
1811 krate: Interned<String>,
1814 impl Step for CrateLibrustc {
1816 const DEFAULT: bool = true;
1817 const ONLY_HOSTS: bool = true;
1819 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1820 run.krate("rustc-main")
1823 fn make_run(run: RunConfig<'_>) {
1824 let builder = run.builder;
1825 let compiler = builder.compiler(builder.top_stage, run.build_triple());
1827 for krate in builder.in_tree_crates("rustc-main", Some(run.target)) {
1828 if krate.path.ends_with(&run.path) {
1829 let test_kind = builder.kind.into();
1831 builder.ensure(CrateLibrustc {
1841 fn run(self, builder: &Builder<'_>) {
1842 builder.ensure(Crate {
1843 compiler: self.compiler,
1844 target: self.target,
1846 test_kind: self.test_kind,
1852 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1854 pub compiler: Compiler,
1855 pub target: TargetSelection,
1857 pub test_kind: TestKind,
1858 pub krate: Interned<String>,
1861 impl Step for Crate {
1863 const DEFAULT: bool = true;
1865 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1869 fn make_run(run: RunConfig<'_>) {
1870 let builder = run.builder;
1871 let compiler = builder.compiler(builder.top_stage, run.build_triple());
1873 let make = |mode: Mode, krate: &CargoCrate| {
1874 let test_kind = builder.kind.into();
1876 builder.ensure(Crate {
1885 for krate in builder.in_tree_crates("test", Some(run.target)) {
1886 if krate.path.ends_with(&run.path) {
1887 make(Mode::Std, krate);
1892 /// Runs all unit tests plus documentation tests for a given crate defined
1893 /// by a `Cargo.toml` (single manifest)
1895 /// This is what runs tests for crates like the standard library, compiler, etc.
1896 /// It essentially is the driver for running `cargo test`.
1898 /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
1899 /// arguments, and those arguments are discovered from `cargo metadata`.
1900 fn run(self, builder: &Builder<'_>) {
1901 let compiler = self.compiler;
1902 let target = self.target;
1903 let mode = self.mode;
1904 let test_kind = self.test_kind;
1905 let krate = self.krate;
1907 builder.ensure(compile::Std { compiler, target });
1908 builder.ensure(RemoteCopyLibs { compiler, target });
1910 // If we're not doing a full bootstrap but we're testing a stage2
1911 // version of libstd, then what we're actually testing is the libstd
1912 // produced in stage1. Reflect that here by updating the compiler that
1913 // we're working with automatically.
1914 let compiler = builder.compiler_for(compiler.stage, compiler.host, target);
1917 builder.cargo(compiler, mode, SourceType::InTree, target, test_kind.subcommand());
1920 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
1923 builder.ensure(compile::Rustc { compiler, target });
1924 compile::rustc_cargo(builder, &mut cargo, target);
1926 _ => panic!("can only test libraries"),
1929 // Build up the base `cargo test` command.
1931 // Pass in some standard flags then iterate over the graph we've discovered
1932 // in `cargo metadata` with the maps above and figure out what `-p`
1933 // arguments need to get passed.
1934 if test_kind.subcommand() == "test" && !builder.fail_fast {
1935 cargo.arg("--no-fail-fast");
1937 match builder.doc_tests {
1942 cargo.args(&["--lib", "--bins", "--examples", "--tests", "--benches"]);
1947 cargo.arg("-p").arg(krate);
1949 // The tests are going to run with the *target* libraries, so we need to
1950 // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
1952 // Note that to run the compiler we need to run with the *host* libraries,
1953 // but our wrapper scripts arrange for that to be the case anyway.
1954 let mut dylib_path = dylib_path();
1955 dylib_path.insert(0, PathBuf::from(&*builder.sysroot_libdir(compiler, target)));
1956 cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1959 cargo.args(&builder.config.cmd.test_args());
1961 if !builder.config.verbose_tests {
1962 cargo.arg("--quiet");
1965 if target.contains("emscripten") {
1967 format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
1968 builder.config.nodejs.as_ref().expect("nodejs not configured"),
1970 } else if target.starts_with("wasm32") {
1971 let node = builder.config.nodejs.as_ref().expect("nodejs not configured");
1973 format!("{} {}/src/etc/wasm32-shim.js", node.display(), builder.src.display());
1974 cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)), &runner);
1975 } else if builder.remote_tested(target) {
1977 format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
1978 format!("{} run 0", builder.tool_exe(Tool::RemoteTestClient).display()),
1982 builder.info(&format!(
1983 "{} {} stage{} ({} -> {})",
1984 test_kind, krate, compiler.stage, &compiler.host, target
1986 let _time = util::timeit(&builder);
1987 try_run(builder, &mut cargo.into());
1991 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1992 pub struct CrateRustdoc {
1993 host: TargetSelection,
1994 test_kind: TestKind,
1997 impl Step for CrateRustdoc {
1999 const DEFAULT: bool = true;
2000 const ONLY_HOSTS: bool = true;
2002 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2003 run.paths(&["src/librustdoc", "src/tools/rustdoc"])
2006 fn make_run(run: RunConfig<'_>) {
2007 let builder = run.builder;
2009 let test_kind = builder.kind.into();
2011 builder.ensure(CrateRustdoc { host: run.target, test_kind });
2014 fn run(self, builder: &Builder<'_>) {
2015 let test_kind = self.test_kind;
2016 let target = self.host;
2018 // Use the previous stage compiler to reuse the artifacts that are
2019 // created when running compiletest for src/test/rustdoc. If this used
2020 // `compiler`, then it would cause rustdoc to be built *again*, which
2021 // isn't really necessary.
2022 let compiler = builder.compiler_for(builder.top_stage, target, target);
2023 builder.ensure(compile::Rustc { compiler, target });
2025 let mut cargo = tool::prepare_tool_cargo(
2030 test_kind.subcommand(),
2031 "src/tools/rustdoc",
2035 if test_kind.subcommand() == "test" && !builder.fail_fast {
2036 cargo.arg("--no-fail-fast");
2039 cargo.arg("-p").arg("rustdoc:0.0.0");
2042 cargo.args(&builder.config.cmd.test_args());
2044 if self.host.contains("musl") {
2045 cargo.arg("'-Ctarget-feature=-crt-static'");
2048 // This is needed for running doctests on librustdoc. This is a bit of
2049 // an unfortunate interaction with how bootstrap works and how cargo
2050 // sets up the dylib path, and the fact that the doctest (in
2051 // html/markdown.rs) links to rustc-private libs. For stage1, the
2052 // compiler host dylibs (in stage1/lib) are not the same as the target
2053 // dylibs (in stage1/lib/rustlib/...). This is different from a normal
2054 // rust distribution where they are the same.
2056 // On the cargo side, normal tests use `target_process` which handles
2057 // setting up the dylib for a *target* (stage1/lib/rustlib/... in this
2058 // case). However, for doctests it uses `rustdoc_process` which only
2059 // sets up the dylib path for the *host* (stage1/lib), which is the
2062 // It should be considered to just stop running doctests on
2063 // librustdoc. There is only one test, and it doesn't look too
2064 // important. There might be other ways to avoid this, but it seems
2065 // pretty convoluted.
2067 // See also https://github.com/rust-lang/rust/issues/13983 where the
2068 // host vs target dylibs for rustdoc are consistently tricky to deal
2070 let mut dylib_path = dylib_path();
2071 dylib_path.insert(0, PathBuf::from(&*builder.sysroot_libdir(compiler, target)));
2072 cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
2074 if !builder.config.verbose_tests {
2075 cargo.arg("--quiet");
2078 builder.info(&format!(
2079 "{} rustdoc stage{} ({} -> {})",
2080 test_kind, compiler.stage, &compiler.host, target
2082 let _time = util::timeit(&builder);
2084 try_run(builder, &mut cargo.into());
2088 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2089 pub struct CrateRustdocJsonTypes {
2090 host: TargetSelection,
2091 test_kind: TestKind,
2094 impl Step for CrateRustdocJsonTypes {
2096 const DEFAULT: bool = true;
2097 const ONLY_HOSTS: bool = true;
2099 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2100 run.path("src/rustdoc-json-types")
2103 fn make_run(run: RunConfig<'_>) {
2104 let builder = run.builder;
2106 let test_kind = builder.kind.into();
2108 builder.ensure(CrateRustdocJsonTypes { host: run.target, test_kind });
2111 fn run(self, builder: &Builder<'_>) {
2112 let test_kind = self.test_kind;
2113 let target = self.host;
2115 // Use the previous stage compiler to reuse the artifacts that are
2116 // created when running compiletest for src/test/rustdoc. If this used
2117 // `compiler`, then it would cause rustdoc to be built *again*, which
2118 // isn't really necessary.
2119 let compiler = builder.compiler_for(builder.top_stage, target, target);
2120 builder.ensure(compile::Rustc { compiler, target });
2122 let mut cargo = tool::prepare_tool_cargo(
2127 test_kind.subcommand(),
2128 "src/rustdoc-json-types",
2132 if test_kind.subcommand() == "test" && !builder.fail_fast {
2133 cargo.arg("--no-fail-fast");
2136 cargo.arg("-p").arg("rustdoc-json-types");
2139 cargo.args(&builder.config.cmd.test_args());
2141 if self.host.contains("musl") {
2142 cargo.arg("'-Ctarget-feature=-crt-static'");
2145 if !builder.config.verbose_tests {
2146 cargo.arg("--quiet");
2149 builder.info(&format!(
2150 "{} rustdoc-json-types stage{} ({} -> {})",
2151 test_kind, compiler.stage, &compiler.host, target
2153 let _time = util::timeit(&builder);
2155 try_run(builder, &mut cargo.into());
2159 /// Some test suites are run inside emulators or on remote devices, and most
2160 /// of our test binaries are linked dynamically which means we need to ship
2161 /// the standard library and such to the emulator ahead of time. This step
2162 /// represents this and is a dependency of all test suites.
2164 /// Most of the time this is a no-op. For some steps such as shipping data to
2165 /// QEMU we have to build our own tools so we've got conditional dependencies
2166 /// on those programs as well. Note that the remote test client is built for
2167 /// the build target (us) and the server is built for the target.
2168 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2169 pub struct RemoteCopyLibs {
2171 target: TargetSelection,
2174 impl Step for RemoteCopyLibs {
2177 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2181 fn run(self, builder: &Builder<'_>) {
2182 let compiler = self.compiler;
2183 let target = self.target;
2184 if !builder.remote_tested(target) {
2188 builder.ensure(compile::Std { compiler, target });
2190 builder.info(&format!("REMOTE copy libs to emulator ({})", target));
2191 t!(fs::create_dir_all(builder.out.join("tmp")));
2193 let server = builder.ensure(tool::RemoteTestServer { compiler, target });
2195 // Spawn the emulator and wait for it to come online
2196 let tool = builder.tool_exe(Tool::RemoteTestClient);
2197 let mut cmd = Command::new(&tool);
2198 cmd.arg("spawn-emulator").arg(target.triple).arg(&server).arg(builder.out.join("tmp"));
2199 if let Some(rootfs) = builder.qemu_rootfs(target) {
2202 builder.run(&mut cmd);
2204 // Push all our dylibs to the emulator
2205 for f in t!(builder.sysroot_libdir(compiler, target).read_dir()) {
2207 let name = f.file_name().into_string().unwrap();
2208 if util::is_dylib(&name) {
2209 builder.run(Command::new(&tool).arg("push").arg(f.path()));
2215 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2216 pub struct Distcheck;
2218 impl Step for Distcheck {
2221 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2222 run.path("distcheck")
2225 fn make_run(run: RunConfig<'_>) {
2226 run.builder.ensure(Distcheck);
2229 /// Runs "distcheck", a 'make check' from a tarball
2230 fn run(self, builder: &Builder<'_>) {
2231 builder.info("Distcheck");
2232 let dir = builder.out.join("tmp").join("distcheck");
2233 let _ = fs::remove_dir_all(&dir);
2234 t!(fs::create_dir_all(&dir));
2236 // Guarantee that these are built before we begin running.
2237 builder.ensure(dist::PlainSourceTarball);
2238 builder.ensure(dist::Src);
2240 let mut cmd = Command::new("tar");
2242 .arg(builder.ensure(dist::PlainSourceTarball).tarball())
2243 .arg("--strip-components=1")
2245 builder.run(&mut cmd);
2247 Command::new("./configure")
2248 .args(&builder.config.configure_args)
2249 .arg("--enable-vendor")
2253 Command::new(build_helper::make(&builder.config.build.triple))
2258 // Now make sure that rust-src has all of libstd's dependencies
2259 builder.info("Distcheck rust-src");
2260 let dir = builder.out.join("tmp").join("distcheck-src");
2261 let _ = fs::remove_dir_all(&dir);
2262 t!(fs::create_dir_all(&dir));
2264 let mut cmd = Command::new("tar");
2266 .arg(builder.ensure(dist::Src).tarball())
2267 .arg("--strip-components=1")
2269 builder.run(&mut cmd);
2271 let toml = dir.join("rust-src/lib/rustlib/src/rust/library/std/Cargo.toml");
2273 Command::new(&builder.initial_cargo)
2274 .arg("generate-lockfile")
2275 .arg("--manifest-path")
2282 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2283 pub struct Bootstrap;
2285 impl Step for Bootstrap {
2287 const DEFAULT: bool = true;
2288 const ONLY_HOSTS: bool = true;
2290 /// Tests the build system itself.
2291 fn run(self, builder: &Builder<'_>) {
2292 let mut cmd = Command::new(&builder.initial_cargo);
2294 .current_dir(builder.src.join("src/bootstrap"))
2295 .env("RUSTFLAGS", "-Cdebuginfo=2")
2296 .env("CARGO_TARGET_DIR", builder.out.join("bootstrap"))
2297 .env("BOOTSTRAP_OUTPUT_DIRECTORY", &builder.config.out)
2298 .env("BOOTSTRAP_INITIAL_CARGO", &builder.config.initial_cargo)
2299 .env("RUSTC_BOOTSTRAP", "1")
2300 .env("RUSTC", &builder.initial_rustc);
2301 if let Some(flags) = option_env!("RUSTFLAGS") {
2302 // Use the same rustc flags for testing as for "normal" compilation,
2303 // so that Cargo doesn’t recompile the entire dependency graph every time:
2304 // https://github.com/rust-lang/rust/issues/49215
2305 cmd.env("RUSTFLAGS", flags);
2307 if !builder.fail_fast {
2308 cmd.arg("--no-fail-fast");
2310 cmd.arg("--").args(&builder.config.cmd.test_args());
2311 // rustbuild tests are racy on directory creation so just run them one at a time.
2312 // Since there's not many this shouldn't be a problem.
2313 cmd.arg("--test-threads=1");
2314 try_run(builder, &mut cmd);
2317 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2318 run.path("src/bootstrap")
2321 fn make_run(run: RunConfig<'_>) {
2322 run.builder.ensure(Bootstrap);
2326 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2327 pub struct TierCheck {
2328 pub compiler: Compiler,
2331 impl Step for TierCheck {
2333 const DEFAULT: bool = true;
2334 const ONLY_HOSTS: bool = true;
2336 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2337 run.path("src/tools/tier-check")
2340 fn make_run(run: RunConfig<'_>) {
2342 run.builder.compiler_for(run.builder.top_stage, run.builder.build.build, run.target);
2343 run.builder.ensure(TierCheck { compiler });
2346 /// Tests the Platform Support page in the rustc book.
2347 fn run(self, builder: &Builder<'_>) {
2348 builder.ensure(compile::Std { compiler: self.compiler, target: self.compiler.host });
2349 let mut cargo = tool::prepare_tool_cargo(
2355 "src/tools/tier-check",
2359 cargo.arg(builder.src.join("src/doc/rustc/src/platform-support.md"));
2360 cargo.arg(&builder.rustc(self.compiler));
2361 if builder.is_verbose() {
2362 cargo.arg("--verbose");
2365 builder.info("platform support check");
2366 try_run(builder, &mut cargo.into());
2370 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2371 pub struct LintDocs {
2372 pub compiler: Compiler,
2373 pub target: TargetSelection,
2376 impl Step for LintDocs {
2378 const DEFAULT: bool = true;
2379 const ONLY_HOSTS: bool = true;
2381 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2382 run.path("src/tools/lint-docs")
2385 fn make_run(run: RunConfig<'_>) {
2386 run.builder.ensure(LintDocs {
2387 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
2392 /// Tests that the lint examples in the rustc book generate the correct
2393 /// lints and have the expected format.
2394 fn run(self, builder: &Builder<'_>) {
2395 builder.ensure(crate::doc::RustcBook {
2396 compiler: self.compiler,
2397 target: self.target,