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