]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/test.rs
Rollup merge of #48522 - etaoins:fix-find-width-of-character-at-span-bounds-check...
[rust.git] / src / bootstrap / test.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Implementation of the test-related targets of the build system.
12 //!
13 //! This file implements the various regression test suites that we execute on
14 //! our CI.
15
16 use std::env;
17 use std::ffi::OsString;
18 use std::iter;
19 use std::fmt;
20 use std::fs::{self, File};
21 use std::path::{PathBuf, Path};
22 use std::process::Command;
23 use std::io::Read;
24
25 use build_helper::{self, output};
26
27 use builder::{Kind, RunConfig, ShouldRun, Builder, Compiler, Step};
28 use Crate as CargoCrate;
29 use cache::{INTERNER, Interned};
30 use compile;
31 use dist;
32 use native;
33 use tool::{self, Tool};
34 use util::{self, dylib_path, dylib_path_var};
35 use {Build, Mode};
36 use toolstate::ToolState;
37
38 const ADB_TEST_DIR: &str = "/data/tmp/work";
39
40 /// The two modes of the test runner; tests or benchmarks.
41 #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
42 pub enum TestKind {
43     /// Run `cargo test`
44     Test,
45     /// Run `cargo bench`
46     Bench,
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(build: &Build, cmd: &mut Command) -> bool {
69     if !build.fail_fast {
70         if !build.try_run(cmd) {
71             let mut failures = build.delayed_failures.borrow_mut();
72             failures.push(format!("{:?}", cmd));
73             return false;
74         }
75     } else {
76         build.run(cmd);
77     }
78     true
79 }
80
81 fn try_run_quiet(build: &Build, cmd: &mut Command) -> bool {
82     if !build.fail_fast {
83         if !build.try_run_quiet(cmd) {
84             let mut failures = build.delayed_failures.borrow_mut();
85             failures.push(format!("{:?}", cmd));
86             return false;
87         }
88     } else {
89         build.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 build = builder.build;
110         let host = self.host;
111
112         println!("Linkcheck ({})", host);
113
114         builder.default_doc(None);
115
116         let _time = util::timeit();
117         try_run(build, builder.tool_cmd(Tool::Linkchecker)
118                               .arg(build.out.join(host).join("doc")));
119     }
120
121     fn should_run(run: ShouldRun) -> ShouldRun {
122         let builder = run.builder;
123         run.path("src/tools/linkchecker").default_condition(builder.build.config.docs)
124     }
125
126     fn make_run(run: RunConfig) {
127         run.builder.ensure(Linkcheck { host: run.target });
128     }
129 }
130
131 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
132 pub struct Cargotest {
133     stage: u32,
134     host: Interned<String>,
135 }
136
137 impl Step for Cargotest {
138     type Output = ();
139     const ONLY_HOSTS: bool = true;
140
141     fn should_run(run: ShouldRun) -> ShouldRun {
142         run.path("src/tools/cargotest")
143     }
144
145     fn make_run(run: RunConfig) {
146         run.builder.ensure(Cargotest {
147             stage: run.builder.top_stage,
148             host: run.target,
149         });
150     }
151
152     /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler.
153     ///
154     /// This tool in `src/tools` will check out a few Rust projects and run `cargo
155     /// test` to ensure that we don't regress the test suites there.
156     fn run(self, builder: &Builder) {
157         let build = builder.build;
158         let compiler = builder.compiler(self.stage, self.host);
159         builder.ensure(compile::Rustc { compiler, target: compiler.host });
160
161         // Note that this is a short, cryptic, and not scoped directory name. This
162         // is currently to minimize the length of path on Windows where we otherwise
163         // quickly run into path name limit constraints.
164         let out_dir = build.out.join("ct");
165         t!(fs::create_dir_all(&out_dir));
166
167         let _time = util::timeit();
168         let mut cmd = builder.tool_cmd(Tool::CargoTest);
169         try_run(build, cmd.arg(&build.initial_cargo)
170                           .arg(&out_dir)
171                           .env("RUSTC", builder.rustc(compiler))
172                           .env("RUSTDOC", builder.rustdoc(compiler.host)));
173     }
174 }
175
176 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
177 pub struct Cargo {
178     stage: u32,
179     host: Interned<String>,
180 }
181
182 impl Step for Cargo {
183     type Output = ();
184     const ONLY_HOSTS: bool = true;
185
186     fn should_run(run: ShouldRun) -> ShouldRun {
187         run.path("src/tools/cargo")
188     }
189
190     fn make_run(run: RunConfig) {
191         run.builder.ensure(Cargo {
192             stage: run.builder.top_stage,
193             host: run.target,
194         });
195     }
196
197     /// Runs `cargo test` for `cargo` packaged with Rust.
198     fn run(self, builder: &Builder) {
199         let build = builder.build;
200         let compiler = builder.compiler(self.stage, self.host);
201
202         builder.ensure(tool::Cargo { compiler, target: self.host });
203         let mut cargo = builder.cargo(compiler, Mode::Tool, self.host, "test");
204         cargo.arg("--manifest-path").arg(build.src.join("src/tools/cargo/Cargo.toml"));
205         if !build.fail_fast {
206             cargo.arg("--no-fail-fast");
207         }
208
209         // Don't build tests dynamically, just a pain to work with
210         cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
211
212         // Don't run cross-compile tests, we may not have cross-compiled libstd libs
213         // available.
214         cargo.env("CFG_DISABLE_CROSS_TESTS", "1");
215
216         try_run(build, cargo.env("PATH", &path_for_cargo(builder, compiler)));
217     }
218 }
219
220 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
221 pub struct Rls {
222     stage: u32,
223     host: Interned<String>,
224 }
225
226 impl Step for Rls {
227     type Output = ();
228     const ONLY_HOSTS: bool = true;
229
230     fn should_run(run: ShouldRun) -> ShouldRun {
231         run.path("src/tools/rls")
232     }
233
234     fn make_run(run: RunConfig) {
235         run.builder.ensure(Rls {
236             stage: run.builder.top_stage,
237             host: run.target,
238         });
239     }
240
241     /// Runs `cargo test` for the rls.
242     fn run(self, builder: &Builder) {
243         let build = builder.build;
244         let stage = self.stage;
245         let host = self.host;
246         let compiler = builder.compiler(stage, host);
247
248         builder.ensure(tool::Rls { compiler, target: self.host });
249         let mut cargo = tool::prepare_tool_cargo(builder,
250                                                  compiler,
251                                                  host,
252                                                  "test",
253                                                  "src/tools/rls");
254
255         // Don't build tests dynamically, just a pain to work with
256         cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
257
258         builder.add_rustc_lib_path(compiler, &mut cargo);
259
260         if try_run(build, &mut cargo) {
261             build.save_toolstate("rls", ToolState::TestPass);
262         }
263     }
264 }
265
266 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
267 pub struct Rustfmt {
268     stage: u32,
269     host: Interned<String>,
270 }
271
272 impl Step for Rustfmt {
273     type Output = ();
274     const ONLY_HOSTS: bool = true;
275
276     fn should_run(run: ShouldRun) -> ShouldRun {
277         run.path("src/tools/rustfmt")
278     }
279
280     fn make_run(run: RunConfig) {
281         run.builder.ensure(Rustfmt {
282             stage: run.builder.top_stage,
283             host: run.target,
284         });
285     }
286
287     /// Runs `cargo test` for rustfmt.
288     fn run(self, builder: &Builder) {
289         let build = builder.build;
290         let stage = self.stage;
291         let host = self.host;
292         let compiler = builder.compiler(stage, host);
293
294         builder.ensure(tool::Rustfmt { compiler, target: self.host });
295         let mut cargo = tool::prepare_tool_cargo(builder,
296                                                  compiler,
297                                                  host,
298                                                  "test",
299                                                  "src/tools/rustfmt");
300
301         // Don't build tests dynamically, just a pain to work with
302         cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
303
304         builder.add_rustc_lib_path(compiler, &mut cargo);
305
306         if try_run(build, &mut cargo) {
307             build.save_toolstate("rustfmt", ToolState::TestPass);
308         }
309     }
310 }
311
312 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
313 pub struct Miri {
314     stage: u32,
315     host: Interned<String>,
316 }
317
318 impl Step for Miri {
319     type Output = ();
320     const ONLY_HOSTS: bool = true;
321     const DEFAULT: bool = true;
322
323     fn should_run(run: ShouldRun) -> ShouldRun {
324         let test_miri = run.builder.build.config.test_miri;
325         run.path("src/tools/miri").default_condition(test_miri)
326     }
327
328     fn make_run(run: RunConfig) {
329         run.builder.ensure(Miri {
330             stage: run.builder.top_stage,
331             host: run.target,
332         });
333     }
334
335     /// Runs `cargo test` for miri.
336     fn run(self, builder: &Builder) {
337         let build = builder.build;
338         let stage = self.stage;
339         let host = self.host;
340         let compiler = builder.compiler(stage, host);
341
342         if let Some(miri) = builder.ensure(tool::Miri { compiler, target: self.host }) {
343             let mut cargo = builder.cargo(compiler, Mode::Tool, host, "test");
344             cargo.arg("--manifest-path").arg(build.src.join("src/tools/miri/Cargo.toml"));
345
346             // Don't build tests dynamically, just a pain to work with
347             cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
348             // miri tests need to know about the stage sysroot
349             cargo.env("MIRI_SYSROOT", builder.sysroot(compiler));
350             cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
351             cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
352             cargo.env("MIRI_PATH", miri);
353
354             builder.add_rustc_lib_path(compiler, &mut cargo);
355
356             if try_run(build, &mut cargo) {
357                 build.save_toolstate("miri", ToolState::TestPass);
358             }
359         } else {
360             eprintln!("failed to test miri: could not build");
361         }
362     }
363 }
364
365 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
366 pub struct Clippy {
367     stage: u32,
368     host: Interned<String>,
369 }
370
371 impl Step for Clippy {
372     type Output = ();
373     const ONLY_HOSTS: bool = true;
374     const DEFAULT: bool = false;
375
376     fn should_run(run: ShouldRun) -> ShouldRun {
377         run.path("src/tools/clippy")
378     }
379
380     fn make_run(run: RunConfig) {
381         run.builder.ensure(Clippy {
382             stage: run.builder.top_stage,
383             host: run.target,
384         });
385     }
386
387     /// Runs `cargo test` for clippy.
388     fn run(self, builder: &Builder) {
389         let build = builder.build;
390         let stage = self.stage;
391         let host = self.host;
392         let compiler = builder.compiler(stage, host);
393
394         if let Some(clippy) = builder.ensure(tool::Clippy { compiler, target: self.host }) {
395             let mut cargo = builder.cargo(compiler, Mode::Tool, host, "test");
396             cargo.arg("--manifest-path").arg(build.src.join("src/tools/clippy/Cargo.toml"));
397
398             // Don't build tests dynamically, just a pain to work with
399             cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
400             // clippy tests need to know about the stage sysroot
401             cargo.env("SYSROOT", builder.sysroot(compiler));
402             cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
403             cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
404             let host_libs = builder.stage_out(compiler, Mode::Tool).join(builder.cargo_dir());
405             cargo.env("HOST_LIBS", host_libs);
406             // clippy tests need to find the driver
407             cargo.env("CLIPPY_DRIVER_PATH", clippy);
408
409             builder.add_rustc_lib_path(compiler, &mut cargo);
410
411             if try_run(build, &mut cargo) {
412                 build.save_toolstate("clippy-driver", ToolState::TestPass);
413             }
414         } else {
415             eprintln!("failed to test clippy: could not build");
416         }
417     }
418 }
419
420 fn path_for_cargo(builder: &Builder, compiler: Compiler) -> OsString {
421     // Configure PATH to find the right rustc. NB. we have to use PATH
422     // and not RUSTC because the Cargo test suite has tests that will
423     // fail if rustc is not spelled `rustc`.
424     let path = builder.sysroot(compiler).join("bin");
425     let old_path = env::var_os("PATH").unwrap_or_default();
426     env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("")
427 }
428
429 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
430 pub struct RustdocTheme {
431     pub compiler: Compiler,
432 }
433
434 impl Step for RustdocTheme {
435     type Output = ();
436     const DEFAULT: bool = true;
437     const ONLY_HOSTS: bool = true;
438
439     fn should_run(run: ShouldRun) -> ShouldRun {
440         run.path("src/tools/rustdoc-themes")
441     }
442
443     fn make_run(run: RunConfig) {
444         let compiler = run.builder.compiler(run.builder.top_stage, run.host);
445
446         run.builder.ensure(RustdocTheme {
447             compiler: compiler,
448         });
449     }
450
451     fn run(self, builder: &Builder) {
452         let rustdoc = builder.rustdoc(self.compiler.host);
453         let mut cmd = builder.tool_cmd(Tool::RustdocTheme);
454         cmd.arg(rustdoc.to_str().unwrap())
455            .arg(builder.src.join("src/librustdoc/html/static/themes").to_str().unwrap())
456            .env("RUSTC_STAGE", self.compiler.stage.to_string())
457            .env("RUSTC_SYSROOT", builder.sysroot(self.compiler))
458            .env("RUSTDOC_LIBDIR", builder.sysroot_libdir(self.compiler, self.compiler.host))
459            .env("CFG_RELEASE_CHANNEL", &builder.build.config.channel)
460            .env("RUSTDOC_REAL", builder.rustdoc(self.compiler.host))
461            .env("RUSTDOC_CRATE_VERSION", builder.build.rust_version())
462            .env("RUSTC_BOOTSTRAP", "1");
463         if let Some(linker) = builder.build.linker(self.compiler.host) {
464             cmd.env("RUSTC_TARGET_LINKER", linker);
465         }
466         try_run(builder.build, &mut cmd);
467     }
468 }
469
470 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
471 pub struct RustdocJS {
472     pub host: Interned<String>,
473     pub target: Interned<String>,
474 }
475
476 impl Step for RustdocJS {
477     type Output = ();
478     const DEFAULT: bool = true;
479     const ONLY_HOSTS: bool = true;
480
481     fn should_run(run: ShouldRun) -> ShouldRun {
482         run.path("src/test/rustdoc-js")
483     }
484
485     fn make_run(run: RunConfig) {
486         run.builder.ensure(RustdocJS {
487             host: run.host,
488             target: run.target,
489         });
490     }
491
492     fn run(self, builder: &Builder) {
493         if let Some(ref nodejs) = builder.config.nodejs {
494             let mut command = Command::new(nodejs);
495             command.args(&["src/tools/rustdoc-js/tester.js", &*self.host]);
496             builder.ensure(::doc::Std {
497                 target: self.target,
498                 stage: builder.top_stage,
499             });
500             builder.run(&mut command);
501         } else {
502             println!("No nodejs found, skipping \"src/test/rustdoc-js\" tests");
503         }
504     }
505 }
506
507 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
508 pub struct Tidy {
509     host: Interned<String>,
510 }
511
512 impl Step for Tidy {
513     type Output = ();
514     const DEFAULT: bool = true;
515     const ONLY_HOSTS: bool = true;
516     const ONLY_BUILD: bool = true;
517
518     /// Runs the `tidy` tool as compiled in `stage` by the `host` compiler.
519     ///
520     /// This tool in `src/tools` checks up on various bits and pieces of style and
521     /// otherwise just implements a few lint-like checks that are specific to the
522     /// compiler itself.
523     fn run(self, builder: &Builder) {
524         let build = builder.build;
525         let host = self.host;
526
527         let _folder = build.fold_output(|| "tidy");
528         println!("tidy check ({})", host);
529         let mut cmd = builder.tool_cmd(Tool::Tidy);
530         cmd.arg(build.src.join("src"));
531         if !build.config.vendor {
532             cmd.arg("--no-vendor");
533         }
534         if build.config.quiet_tests {
535             cmd.arg("--quiet");
536         }
537         try_run(build, &mut cmd);
538     }
539
540     fn should_run(run: ShouldRun) -> ShouldRun {
541         run.path("src/tools/tidy")
542     }
543
544     fn make_run(run: RunConfig) {
545         run.builder.ensure(Tidy {
546             host: run.builder.build.build,
547         });
548     }
549 }
550
551 fn testdir(build: &Build, host: Interned<String>) -> PathBuf {
552     build.out.join(host).join("test")
553 }
554
555 macro_rules! default_test {
556     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
557         test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: false });
558     }
559 }
560
561 macro_rules! host_test {
562     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
563         test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: true });
564     }
565 }
566
567 macro_rules! test {
568     ($name:ident {
569         path: $path:expr,
570         mode: $mode:expr,
571         suite: $suite:expr,
572         default: $default:expr,
573         host: $host:expr
574     }) => {
575         #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
576         pub struct $name {
577             pub compiler: Compiler,
578             pub target: Interned<String>,
579         }
580
581         impl Step for $name {
582             type Output = ();
583             const DEFAULT: bool = $default;
584             const ONLY_HOSTS: bool = $host;
585
586             fn should_run(run: ShouldRun) -> ShouldRun {
587                 run.path($path)
588             }
589
590             fn make_run(run: RunConfig) {
591                 let compiler = run.builder.compiler(run.builder.top_stage, run.host);
592
593                 run.builder.ensure($name {
594                     compiler,
595                     target: run.target,
596                 });
597             }
598
599             fn run(self, builder: &Builder) {
600                 builder.ensure(Compiletest {
601                     compiler: self.compiler,
602                     target: self.target,
603                     mode: $mode,
604                     suite: $suite,
605                 })
606             }
607         }
608     }
609 }
610
611 default_test!(Ui {
612     path: "src/test/ui",
613     mode: "ui",
614     suite: "ui"
615 });
616
617 default_test!(RunPass {
618     path: "src/test/run-pass",
619     mode: "run-pass",
620     suite: "run-pass"
621 });
622
623 default_test!(CompileFail {
624     path: "src/test/compile-fail",
625     mode: "compile-fail",
626     suite: "compile-fail"
627 });
628
629 default_test!(ParseFail {
630     path: "src/test/parse-fail",
631     mode: "parse-fail",
632     suite: "parse-fail"
633 });
634
635 default_test!(RunFail {
636     path: "src/test/run-fail",
637     mode: "run-fail",
638     suite: "run-fail"
639 });
640
641 default_test!(RunPassValgrind {
642     path: "src/test/run-pass-valgrind",
643     mode: "run-pass-valgrind",
644     suite: "run-pass-valgrind"
645 });
646
647 default_test!(MirOpt {
648     path: "src/test/mir-opt",
649     mode: "mir-opt",
650     suite: "mir-opt"
651 });
652
653 default_test!(Codegen {
654     path: "src/test/codegen",
655     mode: "codegen",
656     suite: "codegen"
657 });
658
659 default_test!(CodegenUnits {
660     path: "src/test/codegen-units",
661     mode: "codegen-units",
662     suite: "codegen-units"
663 });
664
665 default_test!(Incremental {
666     path: "src/test/incremental",
667     mode: "incremental",
668     suite: "incremental"
669 });
670
671 default_test!(Debuginfo {
672     path: "src/test/debuginfo",
673     // What this runs varies depending on the native platform being apple
674     mode: "debuginfo-XXX",
675     suite: "debuginfo"
676 });
677
678 host_test!(UiFullDeps {
679     path: "src/test/ui-fulldeps",
680     mode: "ui",
681     suite: "ui-fulldeps"
682 });
683
684 host_test!(RunPassFullDeps {
685     path: "src/test/run-pass-fulldeps",
686     mode: "run-pass",
687     suite: "run-pass-fulldeps"
688 });
689
690 host_test!(RunFailFullDeps {
691     path: "src/test/run-fail-fulldeps",
692     mode: "run-fail",
693     suite: "run-fail-fulldeps"
694 });
695
696 host_test!(CompileFailFullDeps {
697     path: "src/test/compile-fail-fulldeps",
698     mode: "compile-fail",
699     suite: "compile-fail-fulldeps"
700 });
701
702 host_test!(IncrementalFullDeps {
703     path: "src/test/incremental-fulldeps",
704     mode: "incremental",
705     suite: "incremental-fulldeps"
706 });
707
708 host_test!(Rustdoc {
709     path: "src/test/rustdoc",
710     mode: "rustdoc",
711     suite: "rustdoc"
712 });
713
714 test!(Pretty {
715     path: "src/test/pretty",
716     mode: "pretty",
717     suite: "pretty",
718     default: false,
719     host: true
720 });
721 test!(RunPassPretty {
722     path: "src/test/run-pass/pretty",
723     mode: "pretty",
724     suite: "run-pass",
725     default: false,
726     host: true
727 });
728 test!(RunFailPretty {
729     path: "src/test/run-fail/pretty",
730     mode: "pretty",
731     suite: "run-fail",
732     default: false,
733     host: true
734 });
735 test!(RunPassValgrindPretty {
736     path: "src/test/run-pass-valgrind/pretty",
737     mode: "pretty",
738     suite: "run-pass-valgrind",
739     default: false,
740     host: true
741 });
742 test!(RunPassFullDepsPretty {
743     path: "src/test/run-pass-fulldeps/pretty",
744     mode: "pretty",
745     suite: "run-pass-fulldeps",
746     default: false,
747     host: true
748 });
749 test!(RunFailFullDepsPretty {
750     path: "src/test/run-fail-fulldeps/pretty",
751     mode: "pretty",
752     suite: "run-fail-fulldeps",
753     default: false,
754     host: true
755 });
756
757 host_test!(RunMake {
758     path: "src/test/run-make",
759     mode: "run-make",
760     suite: "run-make"
761 });
762
763 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
764 struct Compiletest {
765     compiler: Compiler,
766     target: Interned<String>,
767     mode: &'static str,
768     suite: &'static str,
769 }
770
771 impl Step for Compiletest {
772     type Output = ();
773
774     fn should_run(run: ShouldRun) -> ShouldRun {
775         run.never()
776     }
777
778     /// Executes the `compiletest` tool to run a suite of tests.
779     ///
780     /// Compiles all tests with `compiler` for `target` with the specified
781     /// compiletest `mode` and `suite` arguments. For example `mode` can be
782     /// "run-pass" or `suite` can be something like `debuginfo`.
783     fn run(self, builder: &Builder) {
784         let build = builder.build;
785         let compiler = self.compiler;
786         let target = self.target;
787         let mode = self.mode;
788         let suite = self.suite;
789
790         // Skip codegen tests if they aren't enabled in configuration.
791         if !build.config.codegen_tests && suite == "codegen" {
792             return;
793         }
794
795         if suite == "debuginfo" {
796             // Skip debuginfo tests on MSVC
797             if build.build.contains("msvc") {
798                 return;
799             }
800
801             if mode == "debuginfo-XXX" {
802                 return if build.build.contains("apple") {
803                     builder.ensure(Compiletest {
804                         mode: "debuginfo-lldb",
805                         ..self
806                     });
807                 } else {
808                     builder.ensure(Compiletest {
809                         mode: "debuginfo-gdb",
810                         ..self
811                     });
812                 };
813             }
814
815             builder.ensure(dist::DebuggerScripts {
816                 sysroot: builder.sysroot(compiler),
817                 host: target
818             });
819         }
820
821         if suite.ends_with("fulldeps") ||
822             // FIXME: Does pretty need librustc compiled? Note that there are
823             // fulldeps test suites with mode = pretty as well.
824             mode == "pretty" ||
825             mode == "rustdoc" ||
826             mode == "run-make" {
827             builder.ensure(compile::Rustc { compiler, target });
828         }
829
830         builder.ensure(compile::Test { compiler, target });
831         builder.ensure(native::TestHelpers { target });
832         builder.ensure(RemoteCopyLibs { compiler, target });
833
834         let _folder = build.fold_output(|| format!("test_{}", suite));
835         println!("Check compiletest suite={} mode={} ({} -> {})",
836                  suite, mode, &compiler.host, target);
837         let mut cmd = builder.tool_cmd(Tool::Compiletest);
838
839         // compiletest currently has... a lot of arguments, so let's just pass all
840         // of them!
841
842         cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(compiler));
843         cmd.arg("--run-lib-path").arg(builder.sysroot_libdir(compiler, target));
844         cmd.arg("--rustc-path").arg(builder.rustc(compiler));
845
846         // Avoid depending on rustdoc when we don't need it.
847         if mode == "rustdoc" || mode == "run-make" {
848             cmd.arg("--rustdoc-path").arg(builder.rustdoc(compiler.host));
849         }
850
851         cmd.arg("--src-base").arg(build.src.join("src/test").join(suite));
852         cmd.arg("--build-base").arg(testdir(build, compiler.host).join(suite));
853         cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target));
854         cmd.arg("--mode").arg(mode);
855         cmd.arg("--target").arg(target);
856         cmd.arg("--host").arg(&*compiler.host);
857         cmd.arg("--llvm-filecheck").arg(build.llvm_filecheck(build.build));
858
859         if let Some(ref nodejs) = build.config.nodejs {
860             cmd.arg("--nodejs").arg(nodejs);
861         }
862
863         let mut flags = vec!["-Crpath".to_string()];
864         if build.config.rust_optimize_tests {
865             flags.push("-O".to_string());
866         }
867         if build.config.rust_debuginfo_tests {
868             flags.push("-g".to_string());
869         }
870         flags.push("-Zmiri -Zunstable-options".to_string());
871         flags.push(build.config.cmd.rustc_args().join(" "));
872
873         if let Some(linker) = build.linker(target) {
874             cmd.arg("--linker").arg(linker);
875         }
876
877         let hostflags = flags.clone();
878         cmd.arg("--host-rustcflags").arg(hostflags.join(" "));
879
880         let mut targetflags = flags.clone();
881         targetflags.push(format!("-Lnative={}",
882                                  build.test_helpers_out(target).display()));
883         cmd.arg("--target-rustcflags").arg(targetflags.join(" "));
884
885         cmd.arg("--docck-python").arg(build.python());
886
887         if build.build.ends_with("apple-darwin") {
888             // Force /usr/bin/python on macOS for LLDB tests because we're loading the
889             // LLDB plugin's compiled module which only works with the system python
890             // (namely not Homebrew-installed python)
891             cmd.arg("--lldb-python").arg("/usr/bin/python");
892         } else {
893             cmd.arg("--lldb-python").arg(build.python());
894         }
895
896         if let Some(ref gdb) = build.config.gdb {
897             cmd.arg("--gdb").arg(gdb);
898         }
899         if let Some(ref vers) = build.lldb_version {
900             cmd.arg("--lldb-version").arg(vers);
901         }
902         if let Some(ref dir) = build.lldb_python_dir {
903             cmd.arg("--lldb-python-dir").arg(dir);
904         }
905
906         cmd.args(&build.config.cmd.test_args());
907
908         if build.is_verbose() {
909             cmd.arg("--verbose");
910         }
911
912         if build.config.quiet_tests {
913             cmd.arg("--quiet");
914         }
915
916         if build.config.llvm_enabled {
917             let llvm_config = build.llvm_config(target);
918             let llvm_version = output(Command::new(&llvm_config).arg("--version"));
919             cmd.arg("--llvm-version").arg(llvm_version);
920             if !build.is_rust_llvm(target) {
921                 cmd.arg("--system-llvm");
922             }
923
924             // Only pass correct values for these flags for the `run-make` suite as it
925             // requires that a C++ compiler was configured which isn't always the case.
926             if suite == "run-make" {
927                 let llvm_components = output(Command::new(&llvm_config).arg("--components"));
928                 let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags"));
929                 cmd.arg("--cc").arg(build.cc(target))
930                 .arg("--cxx").arg(build.cxx(target).unwrap())
931                 .arg("--cflags").arg(build.cflags(target).join(" "))
932                 .arg("--llvm-components").arg(llvm_components.trim())
933                 .arg("--llvm-cxxflags").arg(llvm_cxxflags.trim());
934                 if let Some(ar) = build.ar(target) {
935                     cmd.arg("--ar").arg(ar);
936                 }
937             }
938         }
939         if suite == "run-make" && !build.config.llvm_enabled {
940             println!("Ignoring run-make test suite as they generally don't work without LLVM");
941             return;
942         }
943
944         if suite != "run-make" {
945             cmd.arg("--cc").arg("")
946                .arg("--cxx").arg("")
947                .arg("--cflags").arg("")
948                .arg("--llvm-components").arg("")
949                .arg("--llvm-cxxflags").arg("");
950         }
951
952         if build.remote_tested(target) {
953             cmd.arg("--remote-test-client").arg(builder.tool_exe(Tool::RemoteTestClient));
954         }
955
956         // Running a C compiler on MSVC requires a few env vars to be set, to be
957         // sure to set them here.
958         //
959         // Note that if we encounter `PATH` we make sure to append to our own `PATH`
960         // rather than stomp over it.
961         if target.contains("msvc") {
962             for &(ref k, ref v) in build.cc[&target].env() {
963                 if k != "PATH" {
964                     cmd.env(k, v);
965                 }
966             }
967         }
968         cmd.env("RUSTC_BOOTSTRAP", "1");
969         build.add_rust_test_threads(&mut cmd);
970
971         if build.config.sanitizers {
972             cmd.env("SANITIZER_SUPPORT", "1");
973         }
974
975         if build.config.profiler {
976             cmd.env("PROFILER_SUPPORT", "1");
977         }
978
979         cmd.env("RUST_TEST_TMPDIR", build.out.join("tmp"));
980
981         cmd.arg("--adb-path").arg("adb");
982         cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
983         if target.contains("android") {
984             // Assume that cc for this target comes from the android sysroot
985             cmd.arg("--android-cross-path")
986                .arg(build.cc(target).parent().unwrap().parent().unwrap());
987         } else {
988             cmd.arg("--android-cross-path").arg("");
989         }
990
991         build.ci_env.force_coloring_in_ci(&mut cmd);
992
993         let _time = util::timeit();
994         try_run(build, &mut cmd);
995     }
996 }
997
998 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
999 struct DocTest {
1000     compiler: Compiler,
1001     path: &'static str,
1002     name: &'static str,
1003     is_ext_doc: bool,
1004 }
1005
1006 impl Step for DocTest {
1007     type Output = ();
1008     const ONLY_HOSTS: bool = true;
1009
1010     fn should_run(run: ShouldRun) -> ShouldRun {
1011         run.never()
1012     }
1013
1014     /// Run `rustdoc --test` for all documentation in `src/doc`.
1015     ///
1016     /// This will run all tests in our markdown documentation (e.g. the book)
1017     /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to
1018     /// `compiler`.
1019     fn run(self, builder: &Builder) {
1020         let build = builder.build;
1021         let compiler = self.compiler;
1022
1023         builder.ensure(compile::Test { compiler, target: compiler.host });
1024
1025         // Do a breadth-first traversal of the `src/doc` directory and just run
1026         // tests for all files that end in `*.md`
1027         let mut stack = vec![build.src.join(self.path)];
1028         let _time = util::timeit();
1029         let _folder = build.fold_output(|| format!("test_{}", self.name));
1030
1031         while let Some(p) = stack.pop() {
1032             if p.is_dir() {
1033                 stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
1034                 continue
1035             }
1036
1037             if p.extension().and_then(|s| s.to_str()) != Some("md") {
1038                 continue;
1039             }
1040
1041             // The nostarch directory in the book is for no starch, and so isn't
1042             // guaranteed to build. We don't care if it doesn't build, so skip it.
1043             if p.to_str().map_or(false, |p| p.contains("nostarch")) {
1044                 continue;
1045             }
1046
1047             let test_result = markdown_test(builder, compiler, &p);
1048             if self.is_ext_doc {
1049                 let toolstate = if test_result {
1050                     ToolState::TestPass
1051                 } else {
1052                     ToolState::TestFail
1053                 };
1054                 build.save_toolstate(self.name, toolstate);
1055             }
1056         }
1057     }
1058 }
1059
1060 macro_rules! test_book {
1061     ($($name:ident, $path:expr, $book_name:expr, default=$default:expr;)+) => {
1062         $(
1063             #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1064             pub struct $name {
1065                 compiler: Compiler,
1066             }
1067
1068             impl Step for $name {
1069                 type Output = ();
1070                 const DEFAULT: bool = $default;
1071                 const ONLY_HOSTS: bool = true;
1072
1073                 fn should_run(run: ShouldRun) -> ShouldRun {
1074                     run.path($path)
1075                 }
1076
1077                 fn make_run(run: RunConfig) {
1078                     run.builder.ensure($name {
1079                         compiler: run.builder.compiler(run.builder.top_stage, run.host),
1080                     });
1081                 }
1082
1083                 fn run(self, builder: &Builder) {
1084                     builder.ensure(DocTest {
1085                         compiler: self.compiler,
1086                         path: $path,
1087                         name: $book_name,
1088                         is_ext_doc: !$default,
1089                     });
1090                 }
1091             }
1092         )+
1093     }
1094 }
1095
1096 test_book!(
1097     Nomicon, "src/doc/nomicon", "nomicon", default=false;
1098     Reference, "src/doc/reference", "reference", default=false;
1099     RustdocBook, "src/doc/rustdoc", "rustdoc", default=true;
1100     RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false;
1101     TheBook, "src/doc/book", "book", default=false;
1102     UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
1103 );
1104
1105 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1106 pub struct ErrorIndex {
1107     compiler: Compiler,
1108 }
1109
1110 impl Step for ErrorIndex {
1111     type Output = ();
1112     const DEFAULT: bool = true;
1113     const ONLY_HOSTS: bool = true;
1114
1115     fn should_run(run: ShouldRun) -> ShouldRun {
1116         run.path("src/tools/error_index_generator")
1117     }
1118
1119     fn make_run(run: RunConfig) {
1120         run.builder.ensure(ErrorIndex {
1121             compiler: run.builder.compiler(run.builder.top_stage, run.host),
1122         });
1123     }
1124
1125     /// Run the error index generator tool to execute the tests located in the error
1126     /// index.
1127     ///
1128     /// The `error_index_generator` tool lives in `src/tools` and is used to
1129     /// generate a markdown file from the error indexes of the code base which is
1130     /// then passed to `rustdoc --test`.
1131     fn run(self, builder: &Builder) {
1132         let build = builder.build;
1133         let compiler = self.compiler;
1134
1135         builder.ensure(compile::Std { compiler, target: compiler.host });
1136
1137         let _folder = build.fold_output(|| "test_error_index");
1138         println!("Testing error-index stage{}", compiler.stage);
1139
1140         let dir = testdir(build, compiler.host);
1141         t!(fs::create_dir_all(&dir));
1142         let output = dir.join("error-index.md");
1143
1144         let _time = util::timeit();
1145         build.run(builder.tool_cmd(Tool::ErrorIndex)
1146                     .arg("markdown")
1147                     .arg(&output)
1148                     .env("CFG_BUILD", &build.build)
1149                     .env("RUSTC_ERROR_METADATA_DST", build.extended_error_dir()));
1150
1151         markdown_test(builder, compiler, &output);
1152     }
1153 }
1154
1155 fn markdown_test(builder: &Builder, compiler: Compiler, markdown: &Path) -> bool {
1156     let build = builder.build;
1157     let mut file = t!(File::open(markdown));
1158     let mut contents = String::new();
1159     t!(file.read_to_string(&mut contents));
1160     if !contents.contains("```") {
1161         return true;
1162     }
1163
1164     println!("doc tests for: {}", markdown.display());
1165     let mut cmd = builder.rustdoc_cmd(compiler.host);
1166     build.add_rust_test_threads(&mut cmd);
1167     cmd.arg("--test");
1168     cmd.arg(markdown);
1169     cmd.env("RUSTC_BOOTSTRAP", "1");
1170
1171     let test_args = build.config.cmd.test_args().join(" ");
1172     cmd.arg("--test-args").arg(test_args);
1173
1174     if build.config.quiet_tests {
1175         try_run_quiet(build, &mut cmd)
1176     } else {
1177         try_run(build, &mut cmd)
1178     }
1179 }
1180
1181 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1182 pub struct CrateLibrustc {
1183     compiler: Compiler,
1184     target: Interned<String>,
1185     test_kind: TestKind,
1186     krate: Interned<String>,
1187 }
1188
1189 impl Step for CrateLibrustc {
1190     type Output = ();
1191     const DEFAULT: bool = true;
1192     const ONLY_HOSTS: bool = true;
1193
1194     fn should_run(run: ShouldRun) -> ShouldRun {
1195         run.krate("rustc-main")
1196     }
1197
1198     fn make_run(run: RunConfig) {
1199         let builder = run.builder;
1200         let compiler = builder.compiler(builder.top_stage, run.host);
1201
1202         for krate in builder.in_tree_crates("rustc-main") {
1203             if run.path.ends_with(&krate.path) {
1204                 let test_kind = if builder.kind == Kind::Test {
1205                     TestKind::Test
1206                 } else if builder.kind == Kind::Bench {
1207                     TestKind::Bench
1208                 } else {
1209                     panic!("unexpected builder.kind in crate: {:?}", builder.kind);
1210                 };
1211
1212                 builder.ensure(CrateLibrustc {
1213                     compiler,
1214                     target: run.target,
1215                     test_kind,
1216                     krate: krate.name,
1217                 });
1218             }
1219         }
1220     }
1221
1222     fn run(self, builder: &Builder) {
1223         builder.ensure(Crate {
1224             compiler: self.compiler,
1225             target: self.target,
1226             mode: Mode::Librustc,
1227             test_kind: self.test_kind,
1228             krate: self.krate,
1229         });
1230     }
1231 }
1232
1233 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1234 pub struct CrateNotDefault {
1235     compiler: Compiler,
1236     target: Interned<String>,
1237     test_kind: TestKind,
1238     krate: &'static str,
1239 }
1240
1241 impl Step for CrateNotDefault {
1242     type Output = ();
1243
1244     fn should_run(run: ShouldRun) -> ShouldRun {
1245         run.path("src/liballoc_jemalloc")
1246             .path("src/librustc_asan")
1247             .path("src/librustc_lsan")
1248             .path("src/librustc_msan")
1249             .path("src/librustc_tsan")
1250     }
1251
1252     fn make_run(run: RunConfig) {
1253         let builder = run.builder;
1254         let compiler = builder.compiler(builder.top_stage, run.host);
1255
1256         let test_kind = if builder.kind == Kind::Test {
1257             TestKind::Test
1258         } else if builder.kind == Kind::Bench {
1259             TestKind::Bench
1260         } else {
1261             panic!("unexpected builder.kind in crate: {:?}", builder.kind);
1262         };
1263
1264         builder.ensure(CrateNotDefault {
1265             compiler,
1266             target: run.target,
1267             test_kind,
1268             krate: match run.path {
1269                 _ if run.path.ends_with("src/liballoc_jemalloc") => "alloc_jemalloc",
1270                 _ if run.path.ends_with("src/librustc_asan") => "rustc_asan",
1271                 _ if run.path.ends_with("src/librustc_lsan") => "rustc_lsan",
1272                 _ if run.path.ends_with("src/librustc_msan") => "rustc_msan",
1273                 _ if run.path.ends_with("src/librustc_tsan") => "rustc_tsan",
1274                 _ => panic!("unexpected path {:?}", run.path),
1275             },
1276         });
1277     }
1278
1279     fn run(self, builder: &Builder) {
1280         builder.ensure(Crate {
1281             compiler: self.compiler,
1282             target: self.target,
1283             mode: Mode::Libstd,
1284             test_kind: self.test_kind,
1285             krate: INTERNER.intern_str(self.krate),
1286         });
1287     }
1288 }
1289
1290
1291 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1292 pub struct Crate {
1293     compiler: Compiler,
1294     target: Interned<String>,
1295     mode: Mode,
1296     test_kind: TestKind,
1297     krate: Interned<String>,
1298 }
1299
1300 impl Step for Crate {
1301     type Output = ();
1302     const DEFAULT: bool = true;
1303
1304     fn should_run(mut run: ShouldRun) -> ShouldRun {
1305         let builder = run.builder;
1306         run = run.krate("test");
1307         for krate in run.builder.in_tree_crates("std") {
1308             if krate.is_local(&run.builder) &&
1309                 !krate.name.contains("jemalloc") &&
1310                 !(krate.name.starts_with("rustc_") && krate.name.ends_with("san")) &&
1311                 krate.name != "dlmalloc" {
1312                 run = run.path(krate.local_path(&builder).to_str().unwrap());
1313             }
1314         }
1315         run
1316     }
1317
1318     fn make_run(run: RunConfig) {
1319         let builder = run.builder;
1320         let compiler = builder.compiler(builder.top_stage, run.host);
1321
1322         let make = |mode: Mode, krate: &CargoCrate| {
1323             let test_kind = if builder.kind == Kind::Test {
1324                 TestKind::Test
1325             } else if builder.kind == Kind::Bench {
1326                 TestKind::Bench
1327             } else {
1328                 panic!("unexpected builder.kind in crate: {:?}", builder.kind);
1329             };
1330
1331             builder.ensure(Crate {
1332                 compiler,
1333                 target: run.target,
1334                 mode,
1335                 test_kind,
1336                 krate: krate.name,
1337             });
1338         };
1339
1340         for krate in builder.in_tree_crates("std") {
1341             if run.path.ends_with(&krate.local_path(&builder)) {
1342                 make(Mode::Libstd, krate);
1343             }
1344         }
1345         for krate in builder.in_tree_crates("test") {
1346             if run.path.ends_with(&krate.local_path(&builder)) {
1347                 make(Mode::Libtest, krate);
1348             }
1349         }
1350     }
1351
1352     /// Run all unit tests plus documentation tests for a given crate defined
1353     /// by a `Cargo.toml` (single manifest)
1354     ///
1355     /// This is what runs tests for crates like the standard library, compiler, etc.
1356     /// It essentially is the driver for running `cargo test`.
1357     ///
1358     /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
1359     /// arguments, and those arguments are discovered from `cargo metadata`.
1360     fn run(self, builder: &Builder) {
1361         let build = builder.build;
1362         let compiler = self.compiler;
1363         let target = self.target;
1364         let mode = self.mode;
1365         let test_kind = self.test_kind;
1366         let krate = self.krate;
1367
1368         builder.ensure(compile::Test { compiler, target });
1369         builder.ensure(RemoteCopyLibs { compiler, target });
1370
1371         // If we're not doing a full bootstrap but we're testing a stage2 version of
1372         // libstd, then what we're actually testing is the libstd produced in
1373         // stage1. Reflect that here by updating the compiler that we're working
1374         // with automatically.
1375         let compiler = if build.force_use_stage1(compiler, target) {
1376             builder.compiler(1, compiler.host)
1377         } else {
1378             compiler.clone()
1379         };
1380
1381         let mut cargo = builder.cargo(compiler, mode, target, test_kind.subcommand());
1382         match mode {
1383             Mode::Libstd => {
1384                 compile::std_cargo(build, &compiler, target, &mut cargo);
1385             }
1386             Mode::Libtest => {
1387                 compile::test_cargo(build, &compiler, target, &mut cargo);
1388             }
1389             Mode::Librustc => {
1390                 builder.ensure(compile::Rustc { compiler, target });
1391                 compile::rustc_cargo(build, &mut cargo);
1392             }
1393             _ => panic!("can only test libraries"),
1394         };
1395         let _folder = build.fold_output(|| {
1396             format!("{}_stage{}-{}", test_kind.subcommand(), compiler.stage, krate)
1397         });
1398         println!("{} {} stage{} ({} -> {})", test_kind, krate, compiler.stage,
1399                 &compiler.host, target);
1400
1401         // Build up the base `cargo test` command.
1402         //
1403         // Pass in some standard flags then iterate over the graph we've discovered
1404         // in `cargo metadata` with the maps above and figure out what `-p`
1405         // arguments need to get passed.
1406         if test_kind.subcommand() == "test" && !build.fail_fast {
1407             cargo.arg("--no-fail-fast");
1408         }
1409         if build.doc_tests {
1410             cargo.arg("--doc");
1411         }
1412
1413         cargo.arg("-p").arg(krate);
1414
1415         // The tests are going to run with the *target* libraries, so we need to
1416         // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
1417         //
1418         // Note that to run the compiler we need to run with the *host* libraries,
1419         // but our wrapper scripts arrange for that to be the case anyway.
1420         let mut dylib_path = dylib_path();
1421         dylib_path.insert(0, PathBuf::from(&*builder.sysroot_libdir(compiler, target)));
1422         cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1423
1424         cargo.arg("--");
1425         cargo.args(&build.config.cmd.test_args());
1426
1427         if build.config.quiet_tests {
1428             cargo.arg("--quiet");
1429         }
1430
1431         let _time = util::timeit();
1432
1433         if target.contains("emscripten") {
1434             cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target)),
1435                       build.config.nodejs.as_ref().expect("nodejs not configured"));
1436         } else if target.starts_with("wasm32") {
1437             // Warn about running tests without the `wasm_syscall` feature enabled.
1438             // The javascript shim implements the syscall interface so that test
1439             // output can be correctly reported.
1440             if !build.config.wasm_syscall {
1441                 println!("Libstd was built without `wasm_syscall` feature enabled: \
1442                           test output may not be visible.");
1443             }
1444
1445             // On the wasm32-unknown-unknown target we're using LTO which is
1446             // incompatible with `-C prefer-dynamic`, so disable that here
1447             cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
1448
1449             let node = build.config.nodejs.as_ref()
1450                 .expect("nodejs not configured");
1451             let runner = format!("{} {}/src/etc/wasm32-shim.js",
1452                                  node.display(),
1453                                  build.src.display());
1454             cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target)), &runner);
1455         } else if build.remote_tested(target) {
1456             cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target)),
1457                       format!("{} run",
1458                               builder.tool_exe(Tool::RemoteTestClient).display()));
1459         }
1460         try_run(build, &mut cargo);
1461     }
1462 }
1463
1464 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1465 pub struct CrateRustdoc {
1466     host: Interned<String>,
1467     test_kind: TestKind,
1468 }
1469
1470 impl Step for CrateRustdoc {
1471     type Output = ();
1472     const DEFAULT: bool = true;
1473     const ONLY_HOSTS: bool = true;
1474
1475     fn should_run(run: ShouldRun) -> ShouldRun {
1476         run.paths(&["src/librustdoc", "src/tools/rustdoc"])
1477     }
1478
1479     fn make_run(run: RunConfig) {
1480         let builder = run.builder;
1481
1482         let test_kind = if builder.kind == Kind::Test {
1483             TestKind::Test
1484         } else if builder.kind == Kind::Bench {
1485             TestKind::Bench
1486         } else {
1487             panic!("unexpected builder.kind in crate: {:?}", builder.kind);
1488         };
1489
1490         builder.ensure(CrateRustdoc {
1491             host: run.host,
1492             test_kind,
1493         });
1494     }
1495
1496     fn run(self, builder: &Builder) {
1497         let build = builder.build;
1498         let test_kind = self.test_kind;
1499
1500         let compiler = builder.compiler(builder.top_stage, self.host);
1501         let target = compiler.host;
1502
1503         let mut cargo = tool::prepare_tool_cargo(builder,
1504                                                  compiler,
1505                                                  target,
1506                                                  test_kind.subcommand(),
1507                                                  "src/tools/rustdoc");
1508         let _folder = build.fold_output(|| {
1509             format!("{}_stage{}-rustdoc", test_kind.subcommand(), compiler.stage)
1510         });
1511         println!("{} rustdoc stage{} ({} -> {})", test_kind, compiler.stage,
1512                 &compiler.host, target);
1513
1514         if test_kind.subcommand() == "test" && !build.fail_fast {
1515             cargo.arg("--no-fail-fast");
1516         }
1517
1518         cargo.arg("-p").arg("rustdoc:0.0.0");
1519
1520         cargo.arg("--");
1521         cargo.args(&build.config.cmd.test_args());
1522
1523         if build.config.quiet_tests {
1524             cargo.arg("--quiet");
1525         }
1526
1527         let _time = util::timeit();
1528
1529         try_run(build, &mut cargo);
1530     }
1531 }
1532
1533 fn envify(s: &str) -> String {
1534     s.chars().map(|c| {
1535         match c {
1536             '-' => '_',
1537             c => c,
1538         }
1539     }).flat_map(|c| c.to_uppercase()).collect()
1540 }
1541
1542 /// Some test suites are run inside emulators or on remote devices, and most
1543 /// of our test binaries are linked dynamically which means we need to ship
1544 /// the standard library and such to the emulator ahead of time. This step
1545 /// represents this and is a dependency of all test suites.
1546 ///
1547 /// Most of the time this is a noop. For some steps such as shipping data to
1548 /// QEMU we have to build our own tools so we've got conditional dependencies
1549 /// on those programs as well. Note that the remote test client is built for
1550 /// the build target (us) and the server is built for the target.
1551 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1552 pub struct RemoteCopyLibs {
1553     compiler: Compiler,
1554     target: Interned<String>,
1555 }
1556
1557 impl Step for RemoteCopyLibs {
1558     type Output = ();
1559
1560     fn should_run(run: ShouldRun) -> ShouldRun {
1561         run.never()
1562     }
1563
1564     fn run(self, builder: &Builder) {
1565         let build = builder.build;
1566         let compiler = self.compiler;
1567         let target = self.target;
1568         if !build.remote_tested(target) {
1569             return
1570         }
1571
1572         builder.ensure(compile::Test { compiler, target });
1573
1574         println!("REMOTE copy libs to emulator ({})", target);
1575         t!(fs::create_dir_all(build.out.join("tmp")));
1576
1577         let server = builder.ensure(tool::RemoteTestServer { compiler, target });
1578
1579         // Spawn the emulator and wait for it to come online
1580         let tool = builder.tool_exe(Tool::RemoteTestClient);
1581         let mut cmd = Command::new(&tool);
1582         cmd.arg("spawn-emulator")
1583            .arg(target)
1584            .arg(&server)
1585            .arg(build.out.join("tmp"));
1586         if let Some(rootfs) = build.qemu_rootfs(target) {
1587             cmd.arg(rootfs);
1588         }
1589         build.run(&mut cmd);
1590
1591         // Push all our dylibs to the emulator
1592         for f in t!(builder.sysroot_libdir(compiler, target).read_dir()) {
1593             let f = t!(f);
1594             let name = f.file_name().into_string().unwrap();
1595             if util::is_dylib(&name) {
1596                 build.run(Command::new(&tool)
1597                                   .arg("push")
1598                                   .arg(f.path()));
1599             }
1600         }
1601     }
1602 }
1603
1604 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1605 pub struct Distcheck;
1606
1607 impl Step for Distcheck {
1608     type Output = ();
1609     const ONLY_BUILD: bool = true;
1610
1611     fn should_run(run: ShouldRun) -> ShouldRun {
1612         run.path("distcheck")
1613     }
1614
1615     fn make_run(run: RunConfig) {
1616         run.builder.ensure(Distcheck);
1617     }
1618
1619     /// Run "distcheck", a 'make check' from a tarball
1620     fn run(self, builder: &Builder) {
1621         let build = builder.build;
1622
1623         println!("Distcheck");
1624         let dir = build.out.join("tmp").join("distcheck");
1625         let _ = fs::remove_dir_all(&dir);
1626         t!(fs::create_dir_all(&dir));
1627
1628         // Guarantee that these are built before we begin running.
1629         builder.ensure(dist::PlainSourceTarball);
1630         builder.ensure(dist::Src);
1631
1632         let mut cmd = Command::new("tar");
1633         cmd.arg("-xzf")
1634            .arg(builder.ensure(dist::PlainSourceTarball))
1635            .arg("--strip-components=1")
1636            .current_dir(&dir);
1637         build.run(&mut cmd);
1638         build.run(Command::new("./configure")
1639                          .args(&build.config.configure_args)
1640                          .arg("--enable-vendor")
1641                          .current_dir(&dir));
1642         build.run(Command::new(build_helper::make(&build.build))
1643                          .arg("check")
1644                          .current_dir(&dir));
1645
1646         // Now make sure that rust-src has all of libstd's dependencies
1647         println!("Distcheck rust-src");
1648         let dir = build.out.join("tmp").join("distcheck-src");
1649         let _ = fs::remove_dir_all(&dir);
1650         t!(fs::create_dir_all(&dir));
1651
1652         let mut cmd = Command::new("tar");
1653         cmd.arg("-xzf")
1654            .arg(builder.ensure(dist::Src))
1655            .arg("--strip-components=1")
1656            .current_dir(&dir);
1657         build.run(&mut cmd);
1658
1659         let toml = dir.join("rust-src/lib/rustlib/src/rust/src/libstd/Cargo.toml");
1660         build.run(Command::new(&build.initial_cargo)
1661                          .arg("generate-lockfile")
1662                          .arg("--manifest-path")
1663                          .arg(&toml)
1664                          .current_dir(&dir));
1665     }
1666 }
1667
1668 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1669 pub struct Bootstrap;
1670
1671 impl Step for Bootstrap {
1672     type Output = ();
1673     const DEFAULT: bool = true;
1674     const ONLY_HOSTS: bool = true;
1675     const ONLY_BUILD: bool = true;
1676
1677     /// Test the build system itself
1678     fn run(self, builder: &Builder) {
1679         let build = builder.build;
1680         let mut cmd = Command::new(&build.initial_cargo);
1681         cmd.arg("test")
1682            .current_dir(build.src.join("src/bootstrap"))
1683            .env("CARGO_TARGET_DIR", build.out.join("bootstrap"))
1684            .env("RUSTC_BOOTSTRAP", "1")
1685            .env("RUSTC", &build.initial_rustc);
1686         if !build.fail_fast {
1687             cmd.arg("--no-fail-fast");
1688         }
1689         cmd.arg("--").args(&build.config.cmd.test_args());
1690         try_run(build, &mut cmd);
1691     }
1692
1693     fn should_run(run: ShouldRun) -> ShouldRun {
1694         run.path("src/bootstrap")
1695     }
1696
1697     fn make_run(run: RunConfig) {
1698         run.builder.ensure(Bootstrap);
1699     }
1700 }