]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/test.rs
Remove ONLY_BUILD.
[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
510 impl Step for Tidy {
511     type Output = ();
512     const DEFAULT: bool = true;
513     const ONLY_HOSTS: bool = true;
514
515     /// Runs the `tidy` tool.
516     ///
517     /// This tool in `src/tools` checks up on various bits and pieces of style and
518     /// otherwise just implements a few lint-like checks that are specific to the
519     /// compiler itself.
520     fn run(self, builder: &Builder) {
521         let build = builder.build;
522
523         let _folder = build.fold_output(|| "tidy");
524         println!("tidy check");
525         let mut cmd = builder.tool_cmd(Tool::Tidy);
526         cmd.arg(build.src.join("src"));
527         cmd.arg(&build.initial_cargo);
528         if !build.config.vendor {
529             cmd.arg("--no-vendor");
530         }
531         if build.config.quiet_tests {
532             cmd.arg("--quiet");
533         }
534         try_run(build, &mut cmd);
535     }
536
537     fn should_run(run: ShouldRun) -> ShouldRun {
538         run.path("src/tools/tidy")
539     }
540
541     fn make_run(run: RunConfig) {
542         run.builder.ensure(Tidy);
543     }
544 }
545
546 fn testdir(build: &Build, host: Interned<String>) -> PathBuf {
547     build.out.join(host).join("test")
548 }
549
550 macro_rules! default_test {
551     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
552         test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: false });
553     }
554 }
555
556 macro_rules! host_test {
557     ($name:ident { path: $path:expr, mode: $mode:expr, suite: $suite:expr }) => {
558         test!($name { path: $path, mode: $mode, suite: $suite, default: true, host: true });
559     }
560 }
561
562 macro_rules! test {
563     ($name:ident {
564         path: $path:expr,
565         mode: $mode:expr,
566         suite: $suite:expr,
567         default: $default:expr,
568         host: $host:expr
569     }) => {
570         #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
571         pub struct $name {
572             pub compiler: Compiler,
573             pub target: Interned<String>,
574         }
575
576         impl Step for $name {
577             type Output = ();
578             const DEFAULT: bool = $default;
579             const ONLY_HOSTS: bool = $host;
580
581             fn should_run(run: ShouldRun) -> ShouldRun {
582                 run.path($path)
583             }
584
585             fn make_run(run: RunConfig) {
586                 let compiler = run.builder.compiler(run.builder.top_stage, run.host);
587
588                 run.builder.ensure($name {
589                     compiler,
590                     target: run.target,
591                 });
592             }
593
594             fn run(self, builder: &Builder) {
595                 builder.ensure(Compiletest {
596                     compiler: self.compiler,
597                     target: self.target,
598                     mode: $mode,
599                     suite: $suite,
600                 })
601             }
602         }
603     }
604 }
605
606 default_test!(Ui {
607     path: "src/test/ui",
608     mode: "ui",
609     suite: "ui"
610 });
611
612 default_test!(RunPass {
613     path: "src/test/run-pass",
614     mode: "run-pass",
615     suite: "run-pass"
616 });
617
618 default_test!(CompileFail {
619     path: "src/test/compile-fail",
620     mode: "compile-fail",
621     suite: "compile-fail"
622 });
623
624 default_test!(ParseFail {
625     path: "src/test/parse-fail",
626     mode: "parse-fail",
627     suite: "parse-fail"
628 });
629
630 default_test!(RunFail {
631     path: "src/test/run-fail",
632     mode: "run-fail",
633     suite: "run-fail"
634 });
635
636 default_test!(RunPassValgrind {
637     path: "src/test/run-pass-valgrind",
638     mode: "run-pass-valgrind",
639     suite: "run-pass-valgrind"
640 });
641
642 default_test!(MirOpt {
643     path: "src/test/mir-opt",
644     mode: "mir-opt",
645     suite: "mir-opt"
646 });
647
648 default_test!(Codegen {
649     path: "src/test/codegen",
650     mode: "codegen",
651     suite: "codegen"
652 });
653
654 default_test!(CodegenUnits {
655     path: "src/test/codegen-units",
656     mode: "codegen-units",
657     suite: "codegen-units"
658 });
659
660 default_test!(Incremental {
661     path: "src/test/incremental",
662     mode: "incremental",
663     suite: "incremental"
664 });
665
666 default_test!(Debuginfo {
667     path: "src/test/debuginfo",
668     // What this runs varies depending on the native platform being apple
669     mode: "debuginfo-XXX",
670     suite: "debuginfo"
671 });
672
673 host_test!(UiFullDeps {
674     path: "src/test/ui-fulldeps",
675     mode: "ui",
676     suite: "ui-fulldeps"
677 });
678
679 host_test!(RunPassFullDeps {
680     path: "src/test/run-pass-fulldeps",
681     mode: "run-pass",
682     suite: "run-pass-fulldeps"
683 });
684
685 host_test!(RunFailFullDeps {
686     path: "src/test/run-fail-fulldeps",
687     mode: "run-fail",
688     suite: "run-fail-fulldeps"
689 });
690
691 host_test!(CompileFailFullDeps {
692     path: "src/test/compile-fail-fulldeps",
693     mode: "compile-fail",
694     suite: "compile-fail-fulldeps"
695 });
696
697 host_test!(IncrementalFullDeps {
698     path: "src/test/incremental-fulldeps",
699     mode: "incremental",
700     suite: "incremental-fulldeps"
701 });
702
703 host_test!(Rustdoc {
704     path: "src/test/rustdoc",
705     mode: "rustdoc",
706     suite: "rustdoc"
707 });
708
709 test!(Pretty {
710     path: "src/test/pretty",
711     mode: "pretty",
712     suite: "pretty",
713     default: false,
714     host: true
715 });
716 test!(RunPassPretty {
717     path: "src/test/run-pass/pretty",
718     mode: "pretty",
719     suite: "run-pass",
720     default: false,
721     host: true
722 });
723 test!(RunFailPretty {
724     path: "src/test/run-fail/pretty",
725     mode: "pretty",
726     suite: "run-fail",
727     default: false,
728     host: true
729 });
730 test!(RunPassValgrindPretty {
731     path: "src/test/run-pass-valgrind/pretty",
732     mode: "pretty",
733     suite: "run-pass-valgrind",
734     default: false,
735     host: true
736 });
737 test!(RunPassFullDepsPretty {
738     path: "src/test/run-pass-fulldeps/pretty",
739     mode: "pretty",
740     suite: "run-pass-fulldeps",
741     default: false,
742     host: true
743 });
744 test!(RunFailFullDepsPretty {
745     path: "src/test/run-fail-fulldeps/pretty",
746     mode: "pretty",
747     suite: "run-fail-fulldeps",
748     default: false,
749     host: true
750 });
751
752 host_test!(RunMake {
753     path: "src/test/run-make",
754     mode: "run-make",
755     suite: "run-make"
756 });
757
758 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
759 struct Compiletest {
760     compiler: Compiler,
761     target: Interned<String>,
762     mode: &'static str,
763     suite: &'static str,
764 }
765
766 impl Step for Compiletest {
767     type Output = ();
768
769     fn should_run(run: ShouldRun) -> ShouldRun {
770         run.never()
771     }
772
773     /// Executes the `compiletest` tool to run a suite of tests.
774     ///
775     /// Compiles all tests with `compiler` for `target` with the specified
776     /// compiletest `mode` and `suite` arguments. For example `mode` can be
777     /// "run-pass" or `suite` can be something like `debuginfo`.
778     fn run(self, builder: &Builder) {
779         let build = builder.build;
780         let compiler = self.compiler;
781         let target = self.target;
782         let mode = self.mode;
783         let suite = self.suite;
784
785         // Skip codegen tests if they aren't enabled in configuration.
786         if !build.config.codegen_tests && suite == "codegen" {
787             return;
788         }
789
790         if suite == "debuginfo" {
791             // Skip debuginfo tests on MSVC
792             if build.build.contains("msvc") {
793                 return;
794             }
795
796             if mode == "debuginfo-XXX" {
797                 return if build.build.contains("apple") {
798                     builder.ensure(Compiletest {
799                         mode: "debuginfo-lldb",
800                         ..self
801                     });
802                 } else {
803                     builder.ensure(Compiletest {
804                         mode: "debuginfo-gdb",
805                         ..self
806                     });
807                 };
808             }
809
810             builder.ensure(dist::DebuggerScripts {
811                 sysroot: builder.sysroot(compiler),
812                 host: target
813             });
814         }
815
816         if suite.ends_with("fulldeps") ||
817             // FIXME: Does pretty need librustc compiled? Note that there are
818             // fulldeps test suites with mode = pretty as well.
819             mode == "pretty" ||
820             mode == "rustdoc" ||
821             mode == "run-make" {
822             builder.ensure(compile::Rustc { compiler, target });
823         }
824
825         builder.ensure(compile::Test { compiler, target });
826         builder.ensure(native::TestHelpers { target });
827         builder.ensure(RemoteCopyLibs { compiler, target });
828
829         let _folder = build.fold_output(|| format!("test_{}", suite));
830         println!("Check compiletest suite={} mode={} ({} -> {})",
831                  suite, mode, &compiler.host, target);
832         let mut cmd = builder.tool_cmd(Tool::Compiletest);
833
834         // compiletest currently has... a lot of arguments, so let's just pass all
835         // of them!
836
837         cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(compiler));
838         cmd.arg("--run-lib-path").arg(builder.sysroot_libdir(compiler, target));
839         cmd.arg("--rustc-path").arg(builder.rustc(compiler));
840
841         // Avoid depending on rustdoc when we don't need it.
842         if mode == "rustdoc" || mode == "run-make" {
843             cmd.arg("--rustdoc-path").arg(builder.rustdoc(compiler.host));
844         }
845
846         cmd.arg("--src-base").arg(build.src.join("src/test").join(suite));
847         cmd.arg("--build-base").arg(testdir(build, compiler.host).join(suite));
848         cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target));
849         cmd.arg("--mode").arg(mode);
850         cmd.arg("--target").arg(target);
851         cmd.arg("--host").arg(&*compiler.host);
852         cmd.arg("--llvm-filecheck").arg(build.llvm_filecheck(build.build));
853
854         if let Some(ref nodejs) = build.config.nodejs {
855             cmd.arg("--nodejs").arg(nodejs);
856         }
857
858         let mut flags = vec!["-Crpath".to_string()];
859         if build.config.rust_optimize_tests {
860             flags.push("-O".to_string());
861         }
862         if build.config.rust_debuginfo_tests {
863             flags.push("-g".to_string());
864         }
865         flags.push("-Zmiri -Zunstable-options".to_string());
866         flags.push(build.config.cmd.rustc_args().join(" "));
867
868         if let Some(linker) = build.linker(target) {
869             cmd.arg("--linker").arg(linker);
870         }
871
872         let hostflags = flags.clone();
873         cmd.arg("--host-rustcflags").arg(hostflags.join(" "));
874
875         let mut targetflags = flags.clone();
876         targetflags.push(format!("-Lnative={}",
877                                  build.test_helpers_out(target).display()));
878         cmd.arg("--target-rustcflags").arg(targetflags.join(" "));
879
880         cmd.arg("--docck-python").arg(build.python());
881
882         if build.build.ends_with("apple-darwin") {
883             // Force /usr/bin/python on macOS for LLDB tests because we're loading the
884             // LLDB plugin's compiled module which only works with the system python
885             // (namely not Homebrew-installed python)
886             cmd.arg("--lldb-python").arg("/usr/bin/python");
887         } else {
888             cmd.arg("--lldb-python").arg(build.python());
889         }
890
891         if let Some(ref gdb) = build.config.gdb {
892             cmd.arg("--gdb").arg(gdb);
893         }
894         if let Some(ref vers) = build.lldb_version {
895             cmd.arg("--lldb-version").arg(vers);
896         }
897         if let Some(ref dir) = build.lldb_python_dir {
898             cmd.arg("--lldb-python-dir").arg(dir);
899         }
900
901         cmd.args(&build.config.cmd.test_args());
902
903         if build.is_verbose() {
904             cmd.arg("--verbose");
905         }
906
907         if build.config.quiet_tests {
908             cmd.arg("--quiet");
909         }
910
911         if build.config.llvm_enabled {
912             let llvm_config = build.llvm_config(build.config.build);
913             let llvm_version = output(Command::new(&llvm_config).arg("--version"));
914             cmd.arg("--llvm-version").arg(llvm_version);
915             if !build.is_rust_llvm(target) {
916                 cmd.arg("--system-llvm");
917             }
918
919             // Only pass correct values for these flags for the `run-make` suite as it
920             // requires that a C++ compiler was configured which isn't always the case.
921             if suite == "run-make" {
922                 let llvm_components = output(Command::new(&llvm_config).arg("--components"));
923                 let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags"));
924                 cmd.arg("--cc").arg(build.cc(target))
925                 .arg("--cxx").arg(build.cxx(target).unwrap())
926                 .arg("--cflags").arg(build.cflags(target).join(" "))
927                 .arg("--llvm-components").arg(llvm_components.trim())
928                 .arg("--llvm-cxxflags").arg(llvm_cxxflags.trim());
929                 if let Some(ar) = build.ar(target) {
930                     cmd.arg("--ar").arg(ar);
931                 }
932             }
933         }
934         if suite == "run-make" && !build.config.llvm_enabled {
935             println!("Ignoring run-make test suite as they generally don't work without LLVM");
936             return;
937         }
938
939         if suite != "run-make" {
940             cmd.arg("--cc").arg("")
941                .arg("--cxx").arg("")
942                .arg("--cflags").arg("")
943                .arg("--llvm-components").arg("")
944                .arg("--llvm-cxxflags").arg("");
945         }
946
947         if build.remote_tested(target) {
948             cmd.arg("--remote-test-client").arg(builder.tool_exe(Tool::RemoteTestClient));
949         }
950
951         // Running a C compiler on MSVC requires a few env vars to be set, to be
952         // sure to set them here.
953         //
954         // Note that if we encounter `PATH` we make sure to append to our own `PATH`
955         // rather than stomp over it.
956         if target.contains("msvc") {
957             for &(ref k, ref v) in build.cc[&target].env() {
958                 if k != "PATH" {
959                     cmd.env(k, v);
960                 }
961             }
962         }
963         cmd.env("RUSTC_BOOTSTRAP", "1");
964         build.add_rust_test_threads(&mut cmd);
965
966         if build.config.sanitizers {
967             cmd.env("SANITIZER_SUPPORT", "1");
968         }
969
970         if build.config.profiler {
971             cmd.env("PROFILER_SUPPORT", "1");
972         }
973
974         cmd.env("RUST_TEST_TMPDIR", build.out.join("tmp"));
975
976         cmd.arg("--adb-path").arg("adb");
977         cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
978         if target.contains("android") {
979             // Assume that cc for this target comes from the android sysroot
980             cmd.arg("--android-cross-path")
981                .arg(build.cc(target).parent().unwrap().parent().unwrap());
982         } else {
983             cmd.arg("--android-cross-path").arg("");
984         }
985
986         build.ci_env.force_coloring_in_ci(&mut cmd);
987
988         let _time = util::timeit();
989         try_run(build, &mut cmd);
990     }
991 }
992
993 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
994 struct DocTest {
995     compiler: Compiler,
996     path: &'static str,
997     name: &'static str,
998     is_ext_doc: bool,
999 }
1000
1001 impl Step for DocTest {
1002     type Output = ();
1003     const ONLY_HOSTS: bool = true;
1004
1005     fn should_run(run: ShouldRun) -> ShouldRun {
1006         run.never()
1007     }
1008
1009     /// Run `rustdoc --test` for all documentation in `src/doc`.
1010     ///
1011     /// This will run all tests in our markdown documentation (e.g. the book)
1012     /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to
1013     /// `compiler`.
1014     fn run(self, builder: &Builder) {
1015         let build = builder.build;
1016         let compiler = self.compiler;
1017
1018         builder.ensure(compile::Test { compiler, target: compiler.host });
1019
1020         // Do a breadth-first traversal of the `src/doc` directory and just run
1021         // tests for all files that end in `*.md`
1022         let mut stack = vec![build.src.join(self.path)];
1023         let _time = util::timeit();
1024         let _folder = build.fold_output(|| format!("test_{}", self.name));
1025
1026         while let Some(p) = stack.pop() {
1027             if p.is_dir() {
1028                 stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
1029                 continue
1030             }
1031
1032             if p.extension().and_then(|s| s.to_str()) != Some("md") {
1033                 continue;
1034             }
1035
1036             // The nostarch directory in the book is for no starch, and so isn't
1037             // guaranteed to build. We don't care if it doesn't build, so skip it.
1038             if p.to_str().map_or(false, |p| p.contains("nostarch")) {
1039                 continue;
1040             }
1041
1042             let test_result = markdown_test(builder, compiler, &p);
1043             if self.is_ext_doc {
1044                 let toolstate = if test_result {
1045                     ToolState::TestPass
1046                 } else {
1047                     ToolState::TestFail
1048                 };
1049                 build.save_toolstate(self.name, toolstate);
1050             }
1051         }
1052     }
1053 }
1054
1055 macro_rules! test_book {
1056     ($($name:ident, $path:expr, $book_name:expr, default=$default:expr;)+) => {
1057         $(
1058             #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1059             pub struct $name {
1060                 compiler: Compiler,
1061             }
1062
1063             impl Step for $name {
1064                 type Output = ();
1065                 const DEFAULT: bool = $default;
1066                 const ONLY_HOSTS: bool = true;
1067
1068                 fn should_run(run: ShouldRun) -> ShouldRun {
1069                     run.path($path)
1070                 }
1071
1072                 fn make_run(run: RunConfig) {
1073                     run.builder.ensure($name {
1074                         compiler: run.builder.compiler(run.builder.top_stage, run.host),
1075                     });
1076                 }
1077
1078                 fn run(self, builder: &Builder) {
1079                     builder.ensure(DocTest {
1080                         compiler: self.compiler,
1081                         path: $path,
1082                         name: $book_name,
1083                         is_ext_doc: !$default,
1084                     });
1085                 }
1086             }
1087         )+
1088     }
1089 }
1090
1091 test_book!(
1092     Nomicon, "src/doc/nomicon", "nomicon", default=false;
1093     Reference, "src/doc/reference", "reference", default=false;
1094     RustdocBook, "src/doc/rustdoc", "rustdoc", default=true;
1095     RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false;
1096     TheBook, "src/doc/book", "book", default=false;
1097     UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
1098 );
1099
1100 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1101 pub struct ErrorIndex {
1102     compiler: Compiler,
1103 }
1104
1105 impl Step for ErrorIndex {
1106     type Output = ();
1107     const DEFAULT: bool = true;
1108     const ONLY_HOSTS: bool = true;
1109
1110     fn should_run(run: ShouldRun) -> ShouldRun {
1111         run.path("src/tools/error_index_generator")
1112     }
1113
1114     fn make_run(run: RunConfig) {
1115         run.builder.ensure(ErrorIndex {
1116             compiler: run.builder.compiler(run.builder.top_stage, run.host),
1117         });
1118     }
1119
1120     /// Run the error index generator tool to execute the tests located in the error
1121     /// index.
1122     ///
1123     /// The `error_index_generator` tool lives in `src/tools` and is used to
1124     /// generate a markdown file from the error indexes of the code base which is
1125     /// then passed to `rustdoc --test`.
1126     fn run(self, builder: &Builder) {
1127         let build = builder.build;
1128         let compiler = self.compiler;
1129
1130         builder.ensure(compile::Std { compiler, target: compiler.host });
1131
1132         let _folder = build.fold_output(|| "test_error_index");
1133         println!("Testing error-index stage{}", compiler.stage);
1134
1135         let dir = testdir(build, compiler.host);
1136         t!(fs::create_dir_all(&dir));
1137         let output = dir.join("error-index.md");
1138
1139         let _time = util::timeit();
1140         build.run(builder.tool_cmd(Tool::ErrorIndex)
1141                     .arg("markdown")
1142                     .arg(&output)
1143                     .env("CFG_BUILD", &build.build)
1144                     .env("RUSTC_ERROR_METADATA_DST", build.extended_error_dir()));
1145
1146         markdown_test(builder, compiler, &output);
1147     }
1148 }
1149
1150 fn markdown_test(builder: &Builder, compiler: Compiler, markdown: &Path) -> bool {
1151     let build = builder.build;
1152     let mut file = t!(File::open(markdown));
1153     let mut contents = String::new();
1154     t!(file.read_to_string(&mut contents));
1155     if !contents.contains("```") {
1156         return true;
1157     }
1158
1159     println!("doc tests for: {}", markdown.display());
1160     let mut cmd = builder.rustdoc_cmd(compiler.host);
1161     build.add_rust_test_threads(&mut cmd);
1162     cmd.arg("--test");
1163     cmd.arg(markdown);
1164     cmd.env("RUSTC_BOOTSTRAP", "1");
1165
1166     let test_args = build.config.cmd.test_args().join(" ");
1167     cmd.arg("--test-args").arg(test_args);
1168
1169     if build.config.quiet_tests {
1170         try_run_quiet(build, &mut cmd)
1171     } else {
1172         try_run(build, &mut cmd)
1173     }
1174 }
1175
1176 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1177 pub struct CrateLibrustc {
1178     compiler: Compiler,
1179     target: Interned<String>,
1180     test_kind: TestKind,
1181     krate: Interned<String>,
1182 }
1183
1184 impl Step for CrateLibrustc {
1185     type Output = ();
1186     const DEFAULT: bool = true;
1187     const ONLY_HOSTS: bool = true;
1188
1189     fn should_run(run: ShouldRun) -> ShouldRun {
1190         run.krate("rustc-main")
1191     }
1192
1193     fn make_run(run: RunConfig) {
1194         let builder = run.builder;
1195         let compiler = builder.compiler(builder.top_stage, run.host);
1196
1197         for krate in builder.in_tree_crates("rustc-main") {
1198             if run.path.ends_with(&krate.path) {
1199                 let test_kind = if builder.kind == Kind::Test {
1200                     TestKind::Test
1201                 } else if builder.kind == Kind::Bench {
1202                     TestKind::Bench
1203                 } else {
1204                     panic!("unexpected builder.kind in crate: {:?}", builder.kind);
1205                 };
1206
1207                 builder.ensure(CrateLibrustc {
1208                     compiler,
1209                     target: run.target,
1210                     test_kind,
1211                     krate: krate.name,
1212                 });
1213             }
1214         }
1215     }
1216
1217     fn run(self, builder: &Builder) {
1218         builder.ensure(Crate {
1219             compiler: self.compiler,
1220             target: self.target,
1221             mode: Mode::Librustc,
1222             test_kind: self.test_kind,
1223             krate: self.krate,
1224         });
1225     }
1226 }
1227
1228 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1229 pub struct CrateNotDefault {
1230     compiler: Compiler,
1231     target: Interned<String>,
1232     test_kind: TestKind,
1233     krate: &'static str,
1234 }
1235
1236 impl Step for CrateNotDefault {
1237     type Output = ();
1238
1239     fn should_run(run: ShouldRun) -> ShouldRun {
1240         run.path("src/liballoc_jemalloc")
1241             .path("src/librustc_asan")
1242             .path("src/librustc_lsan")
1243             .path("src/librustc_msan")
1244             .path("src/librustc_tsan")
1245     }
1246
1247     fn make_run(run: RunConfig) {
1248         let builder = run.builder;
1249         let compiler = builder.compiler(builder.top_stage, run.host);
1250
1251         let test_kind = if builder.kind == Kind::Test {
1252             TestKind::Test
1253         } else if builder.kind == Kind::Bench {
1254             TestKind::Bench
1255         } else {
1256             panic!("unexpected builder.kind in crate: {:?}", builder.kind);
1257         };
1258
1259         builder.ensure(CrateNotDefault {
1260             compiler,
1261             target: run.target,
1262             test_kind,
1263             krate: match run.path {
1264                 _ if run.path.ends_with("src/liballoc_jemalloc") => "alloc_jemalloc",
1265                 _ if run.path.ends_with("src/librustc_asan") => "rustc_asan",
1266                 _ if run.path.ends_with("src/librustc_lsan") => "rustc_lsan",
1267                 _ if run.path.ends_with("src/librustc_msan") => "rustc_msan",
1268                 _ if run.path.ends_with("src/librustc_tsan") => "rustc_tsan",
1269                 _ => panic!("unexpected path {:?}", run.path),
1270             },
1271         });
1272     }
1273
1274     fn run(self, builder: &Builder) {
1275         builder.ensure(Crate {
1276             compiler: self.compiler,
1277             target: self.target,
1278             mode: Mode::Libstd,
1279             test_kind: self.test_kind,
1280             krate: INTERNER.intern_str(self.krate),
1281         });
1282     }
1283 }
1284
1285
1286 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1287 pub struct Crate {
1288     compiler: Compiler,
1289     target: Interned<String>,
1290     mode: Mode,
1291     test_kind: TestKind,
1292     krate: Interned<String>,
1293 }
1294
1295 impl Step for Crate {
1296     type Output = ();
1297     const DEFAULT: bool = true;
1298
1299     fn should_run(mut run: ShouldRun) -> ShouldRun {
1300         let builder = run.builder;
1301         run = run.krate("test");
1302         for krate in run.builder.in_tree_crates("std") {
1303             if krate.is_local(&run.builder) &&
1304                 !krate.name.contains("jemalloc") &&
1305                 !(krate.name.starts_with("rustc_") && krate.name.ends_with("san")) &&
1306                 krate.name != "dlmalloc" {
1307                 run = run.path(krate.local_path(&builder).to_str().unwrap());
1308             }
1309         }
1310         run
1311     }
1312
1313     fn make_run(run: RunConfig) {
1314         let builder = run.builder;
1315         let compiler = builder.compiler(builder.top_stage, run.host);
1316
1317         let make = |mode: Mode, krate: &CargoCrate| {
1318             let test_kind = if builder.kind == Kind::Test {
1319                 TestKind::Test
1320             } else if builder.kind == Kind::Bench {
1321                 TestKind::Bench
1322             } else {
1323                 panic!("unexpected builder.kind in crate: {:?}", builder.kind);
1324             };
1325
1326             builder.ensure(Crate {
1327                 compiler,
1328                 target: run.target,
1329                 mode,
1330                 test_kind,
1331                 krate: krate.name,
1332             });
1333         };
1334
1335         for krate in builder.in_tree_crates("std") {
1336             if run.path.ends_with(&krate.local_path(&builder)) {
1337                 make(Mode::Libstd, krate);
1338             }
1339         }
1340         for krate in builder.in_tree_crates("test") {
1341             if run.path.ends_with(&krate.local_path(&builder)) {
1342                 make(Mode::Libtest, krate);
1343             }
1344         }
1345     }
1346
1347     /// Run all unit tests plus documentation tests for a given crate defined
1348     /// by a `Cargo.toml` (single manifest)
1349     ///
1350     /// This is what runs tests for crates like the standard library, compiler, etc.
1351     /// It essentially is the driver for running `cargo test`.
1352     ///
1353     /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
1354     /// arguments, and those arguments are discovered from `cargo metadata`.
1355     fn run(self, builder: &Builder) {
1356         let build = builder.build;
1357         let compiler = self.compiler;
1358         let target = self.target;
1359         let mode = self.mode;
1360         let test_kind = self.test_kind;
1361         let krate = self.krate;
1362
1363         builder.ensure(compile::Test { compiler, target });
1364         builder.ensure(RemoteCopyLibs { compiler, target });
1365
1366         // If we're not doing a full bootstrap but we're testing a stage2 version of
1367         // libstd, then what we're actually testing is the libstd produced in
1368         // stage1. Reflect that here by updating the compiler that we're working
1369         // with automatically.
1370         let compiler = if build.force_use_stage1(compiler, target) {
1371             builder.compiler(1, compiler.host)
1372         } else {
1373             compiler.clone()
1374         };
1375
1376         let mut cargo = builder.cargo(compiler, mode, target, test_kind.subcommand());
1377         match mode {
1378             Mode::Libstd => {
1379                 compile::std_cargo(build, &compiler, target, &mut cargo);
1380             }
1381             Mode::Libtest => {
1382                 compile::test_cargo(build, &compiler, target, &mut cargo);
1383             }
1384             Mode::Librustc => {
1385                 builder.ensure(compile::Rustc { compiler, target });
1386                 compile::rustc_cargo(build, &mut cargo);
1387             }
1388             _ => panic!("can only test libraries"),
1389         };
1390         let _folder = build.fold_output(|| {
1391             format!("{}_stage{}-{}", test_kind.subcommand(), compiler.stage, krate)
1392         });
1393         println!("{} {} stage{} ({} -> {})", test_kind, krate, compiler.stage,
1394                 &compiler.host, target);
1395
1396         // Build up the base `cargo test` command.
1397         //
1398         // Pass in some standard flags then iterate over the graph we've discovered
1399         // in `cargo metadata` with the maps above and figure out what `-p`
1400         // arguments need to get passed.
1401         if test_kind.subcommand() == "test" && !build.fail_fast {
1402             cargo.arg("--no-fail-fast");
1403         }
1404         if build.doc_tests {
1405             cargo.arg("--doc");
1406         }
1407
1408         cargo.arg("-p").arg(krate);
1409
1410         // The tests are going to run with the *target* libraries, so we need to
1411         // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
1412         //
1413         // Note that to run the compiler we need to run with the *host* libraries,
1414         // but our wrapper scripts arrange for that to be the case anyway.
1415         let mut dylib_path = dylib_path();
1416         dylib_path.insert(0, PathBuf::from(&*builder.sysroot_libdir(compiler, target)));
1417         cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1418
1419         cargo.arg("--");
1420         cargo.args(&build.config.cmd.test_args());
1421
1422         if build.config.quiet_tests {
1423             cargo.arg("--quiet");
1424         }
1425
1426         let _time = util::timeit();
1427
1428         if target.contains("emscripten") {
1429             cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target)),
1430                       build.config.nodejs.as_ref().expect("nodejs not configured"));
1431         } else if target.starts_with("wasm32") {
1432             // Warn about running tests without the `wasm_syscall` feature enabled.
1433             // The javascript shim implements the syscall interface so that test
1434             // output can be correctly reported.
1435             if !build.config.wasm_syscall {
1436                 println!("Libstd was built without `wasm_syscall` feature enabled: \
1437                           test output may not be visible.");
1438             }
1439
1440             // On the wasm32-unknown-unknown target we're using LTO which is
1441             // incompatible with `-C prefer-dynamic`, so disable that here
1442             cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
1443
1444             let node = build.config.nodejs.as_ref()
1445                 .expect("nodejs not configured");
1446             let runner = format!("{} {}/src/etc/wasm32-shim.js",
1447                                  node.display(),
1448                                  build.src.display());
1449             cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target)), &runner);
1450         } else if build.remote_tested(target) {
1451             cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target)),
1452                       format!("{} run",
1453                               builder.tool_exe(Tool::RemoteTestClient).display()));
1454         }
1455         try_run(build, &mut cargo);
1456     }
1457 }
1458
1459 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1460 pub struct CrateRustdoc {
1461     host: Interned<String>,
1462     test_kind: TestKind,
1463 }
1464
1465 impl Step for CrateRustdoc {
1466     type Output = ();
1467     const DEFAULT: bool = true;
1468     const ONLY_HOSTS: bool = true;
1469
1470     fn should_run(run: ShouldRun) -> ShouldRun {
1471         run.paths(&["src/librustdoc", "src/tools/rustdoc"])
1472     }
1473
1474     fn make_run(run: RunConfig) {
1475         let builder = run.builder;
1476
1477         let test_kind = if builder.kind == Kind::Test {
1478             TestKind::Test
1479         } else if builder.kind == Kind::Bench {
1480             TestKind::Bench
1481         } else {
1482             panic!("unexpected builder.kind in crate: {:?}", builder.kind);
1483         };
1484
1485         builder.ensure(CrateRustdoc {
1486             host: run.host,
1487             test_kind,
1488         });
1489     }
1490
1491     fn run(self, builder: &Builder) {
1492         let build = builder.build;
1493         let test_kind = self.test_kind;
1494
1495         let compiler = builder.compiler(builder.top_stage, self.host);
1496         let target = compiler.host;
1497
1498         let mut cargo = tool::prepare_tool_cargo(builder,
1499                                                  compiler,
1500                                                  target,
1501                                                  test_kind.subcommand(),
1502                                                  "src/tools/rustdoc");
1503         let _folder = build.fold_output(|| {
1504             format!("{}_stage{}-rustdoc", test_kind.subcommand(), compiler.stage)
1505         });
1506         println!("{} rustdoc stage{} ({} -> {})", test_kind, compiler.stage,
1507                 &compiler.host, target);
1508
1509         if test_kind.subcommand() == "test" && !build.fail_fast {
1510             cargo.arg("--no-fail-fast");
1511         }
1512
1513         cargo.arg("-p").arg("rustdoc:0.0.0");
1514
1515         cargo.arg("--");
1516         cargo.args(&build.config.cmd.test_args());
1517
1518         if build.config.quiet_tests {
1519             cargo.arg("--quiet");
1520         }
1521
1522         let _time = util::timeit();
1523
1524         try_run(build, &mut cargo);
1525     }
1526 }
1527
1528 fn envify(s: &str) -> String {
1529     s.chars().map(|c| {
1530         match c {
1531             '-' => '_',
1532             c => c,
1533         }
1534     }).flat_map(|c| c.to_uppercase()).collect()
1535 }
1536
1537 /// Some test suites are run inside emulators or on remote devices, and most
1538 /// of our test binaries are linked dynamically which means we need to ship
1539 /// the standard library and such to the emulator ahead of time. This step
1540 /// represents this and is a dependency of all test suites.
1541 ///
1542 /// Most of the time this is a noop. For some steps such as shipping data to
1543 /// QEMU we have to build our own tools so we've got conditional dependencies
1544 /// on those programs as well. Note that the remote test client is built for
1545 /// the build target (us) and the server is built for the target.
1546 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1547 pub struct RemoteCopyLibs {
1548     compiler: Compiler,
1549     target: Interned<String>,
1550 }
1551
1552 impl Step for RemoteCopyLibs {
1553     type Output = ();
1554
1555     fn should_run(run: ShouldRun) -> ShouldRun {
1556         run.never()
1557     }
1558
1559     fn run(self, builder: &Builder) {
1560         let build = builder.build;
1561         let compiler = self.compiler;
1562         let target = self.target;
1563         if !build.remote_tested(target) {
1564             return
1565         }
1566
1567         builder.ensure(compile::Test { compiler, target });
1568
1569         println!("REMOTE copy libs to emulator ({})", target);
1570         t!(fs::create_dir_all(build.out.join("tmp")));
1571
1572         let server = builder.ensure(tool::RemoteTestServer { compiler, target });
1573
1574         // Spawn the emulator and wait for it to come online
1575         let tool = builder.tool_exe(Tool::RemoteTestClient);
1576         let mut cmd = Command::new(&tool);
1577         cmd.arg("spawn-emulator")
1578            .arg(target)
1579            .arg(&server)
1580            .arg(build.out.join("tmp"));
1581         if let Some(rootfs) = build.qemu_rootfs(target) {
1582             cmd.arg(rootfs);
1583         }
1584         build.run(&mut cmd);
1585
1586         // Push all our dylibs to the emulator
1587         for f in t!(builder.sysroot_libdir(compiler, target).read_dir()) {
1588             let f = t!(f);
1589             let name = f.file_name().into_string().unwrap();
1590             if util::is_dylib(&name) {
1591                 build.run(Command::new(&tool)
1592                                   .arg("push")
1593                                   .arg(f.path()));
1594             }
1595         }
1596     }
1597 }
1598
1599 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1600 pub struct Distcheck;
1601
1602 impl Step for Distcheck {
1603     type Output = ();
1604
1605     fn should_run(run: ShouldRun) -> ShouldRun {
1606         run.path("distcheck")
1607     }
1608
1609     fn make_run(run: RunConfig) {
1610         run.builder.ensure(Distcheck);
1611     }
1612
1613     /// Run "distcheck", a 'make check' from a tarball
1614     fn run(self, builder: &Builder) {
1615         let build = builder.build;
1616
1617         println!("Distcheck");
1618         let dir = build.out.join("tmp").join("distcheck");
1619         let _ = fs::remove_dir_all(&dir);
1620         t!(fs::create_dir_all(&dir));
1621
1622         // Guarantee that these are built before we begin running.
1623         builder.ensure(dist::PlainSourceTarball);
1624         builder.ensure(dist::Src);
1625
1626         let mut cmd = Command::new("tar");
1627         cmd.arg("-xzf")
1628            .arg(builder.ensure(dist::PlainSourceTarball))
1629            .arg("--strip-components=1")
1630            .current_dir(&dir);
1631         build.run(&mut cmd);
1632         build.run(Command::new("./configure")
1633                          .args(&build.config.configure_args)
1634                          .arg("--enable-vendor")
1635                          .current_dir(&dir));
1636         build.run(Command::new(build_helper::make(&build.build))
1637                          .arg("check")
1638                          .current_dir(&dir));
1639
1640         // Now make sure that rust-src has all of libstd's dependencies
1641         println!("Distcheck rust-src");
1642         let dir = build.out.join("tmp").join("distcheck-src");
1643         let _ = fs::remove_dir_all(&dir);
1644         t!(fs::create_dir_all(&dir));
1645
1646         let mut cmd = Command::new("tar");
1647         cmd.arg("-xzf")
1648            .arg(builder.ensure(dist::Src))
1649            .arg("--strip-components=1")
1650            .current_dir(&dir);
1651         build.run(&mut cmd);
1652
1653         let toml = dir.join("rust-src/lib/rustlib/src/rust/src/libstd/Cargo.toml");
1654         build.run(Command::new(&build.initial_cargo)
1655                          .arg("generate-lockfile")
1656                          .arg("--manifest-path")
1657                          .arg(&toml)
1658                          .current_dir(&dir));
1659     }
1660 }
1661
1662 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1663 pub struct Bootstrap;
1664
1665 impl Step for Bootstrap {
1666     type Output = ();
1667     const DEFAULT: bool = true;
1668     const ONLY_HOSTS: bool = true;
1669
1670     /// Test the build system itself
1671     fn run(self, builder: &Builder) {
1672         let build = builder.build;
1673         let mut cmd = Command::new(&build.initial_cargo);
1674         cmd.arg("test")
1675            .current_dir(build.src.join("src/bootstrap"))
1676            .env("CARGO_TARGET_DIR", build.out.join("bootstrap"))
1677            .env("RUSTC_BOOTSTRAP", "1")
1678            .env("RUSTC", &build.initial_rustc);
1679         if !build.fail_fast {
1680             cmd.arg("--no-fail-fast");
1681         }
1682         cmd.arg("--").args(&build.config.cmd.test_args());
1683         try_run(build, &mut cmd);
1684     }
1685
1686     fn should_run(run: ShouldRun) -> ShouldRun {
1687         run.path("src/bootstrap")
1688     }
1689
1690     fn make_run(run: RunConfig) {
1691         run.builder.ensure(Bootstrap);
1692     }
1693 }