]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/check.rs
Fix more incorrectly transitioned code
[rust.git] / src / bootstrap / check.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::collections::HashSet;
17 use std::env;
18 use std::ffi::OsString;
19 use std::iter;
20 use std::fmt;
21 use std::fs::{self, File};
22 use std::path::{PathBuf, Path};
23 use std::process::Command;
24 use std::io::Read;
25
26 use build_helper::{self, output};
27
28 use {Build, Mode};
29 use dist;
30 use util::{self, dylib_path, dylib_path_var};
31
32 use compile;
33 use native;
34 use builder::{Kind, Builder, Compiler, Step};
35 use tool::{self, Tool};
36
37 const ADB_TEST_DIR: &str = "/data/tmp/work";
38
39 /// The two modes of the test runner; tests or benchmarks.
40 #[derive(Serialize, Copy, Clone)]
41 pub enum TestKind {
42     /// Run `cargo test`
43     Test,
44     /// Run `cargo bench`
45     Bench,
46 }
47
48 impl TestKind {
49     // Return the cargo subcommand for this test kind
50     fn subcommand(self) -> &'static str {
51         match self {
52             TestKind::Test => "test",
53             TestKind::Bench => "bench",
54         }
55     }
56 }
57
58 impl fmt::Display for TestKind {
59     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60         f.write_str(match *self {
61             TestKind::Test => "Testing",
62             TestKind::Bench => "Benchmarking",
63         })
64     }
65 }
66
67 fn try_run(build: &Build, cmd: &mut Command) {
68     if !build.fail_fast {
69         if !build.try_run(cmd) {
70             let failures = build.delayed_failures.get();
71             build.delayed_failures.set(failures + 1);
72         }
73     } else {
74         build.run(cmd);
75     }
76 }
77
78 fn try_run_quiet(build: &Build, cmd: &mut Command) {
79     if !build.fail_fast {
80         if !build.try_run_quiet(cmd) {
81             let failures = build.delayed_failures.get();
82             build.delayed_failures.set(failures + 1);
83         }
84     } else {
85         build.run_quiet(cmd);
86     }
87 }
88
89 // rules.test("check-linkchecker", "src/tools/linkchecker")
90 //      .dep(|s| s.name("tool-linkchecker").stage(0))
91 //      .dep(|s| s.name("default:doc"))
92 //      .default(build.config.docs)
93 //      .host(true)
94 //      .run(move |s| check::linkcheck(build, s.target));
95
96 #[derive(Serialize)]
97 pub struct Linkcheck<'a> {
98     host: &'a str,
99 }
100
101 impl<'a> Step<'a> for Linkcheck<'a> {
102     type Output = ();
103     const ONLY_HOSTS: bool = true;
104     const DEFAULT: bool = true;
105
106     /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler.
107     ///
108     /// This tool in `src/tools` will verify the validity of all our links in the
109     /// documentation to ensure we don't have a bunch of dead ones.
110     fn run(self, builder: &Builder) {
111         let build = builder.build;
112         let host = self.host;
113
114         println!("Linkcheck ({})", host);
115
116         builder.default_doc(None);
117
118         let _time = util::timeit();
119         try_run(build, builder.tool_cmd(Tool::Linkchecker)
120                             .arg(build.out.join(host).join("doc")));
121     }
122
123     fn should_run(_builder: &Builder, path: &Path) -> bool {
124         path.ends_with("src/tools/linkchecker")
125     }
126
127     fn make_run(builder: &Builder, path: Option<&Path>, host: &str, _target: &str) {
128         if path.is_some() {
129             builder.ensure(Linkcheck { host });
130         } else {
131             if builder.build.config.docs {
132                 builder.ensure(Linkcheck { host });
133             }
134         }
135     }
136 }
137
138 // rules.test("check-cargotest", "src/tools/cargotest")
139 //      .dep(|s| s.name("tool-cargotest").stage(0))
140 //      .dep(|s| s.name("librustc"))
141 //      .host(true)
142 //      .run(move |s| check::cargotest(build, s.stage, s.target));
143
144 #[derive(Serialize)]
145 pub struct Cargotest<'a> {
146     stage: u32,
147     host: &'a str,
148 }
149
150 impl<'a> Step<'a> for Cargotest<'a> {
151     type Output = ();
152     const ONLY_HOSTS: bool = true;
153
154     fn should_run(_builder: &Builder, path: &Path) -> bool {
155         path.ends_with("src/tools/cargotest")
156     }
157
158     fn make_run(builder: &Builder, _path: Option<&Path>, host: &str, _target: &str) {
159         builder.ensure(Cargotest {
160             stage: builder.top_stage,
161             host: host,
162         });
163     }
164
165     /// Runs the `cargotest` tool as compiled in `stage` by the `host` compiler.
166     ///
167     /// This tool in `src/tools` will check out a few Rust projects and run `cargo
168     /// test` to ensure that we don't regress the test suites there.
169     fn run(self, builder: &Builder) {
170         let build = builder.build;
171         let compiler = builder.compiler(self.stage, self.host);
172         builder.ensure(compile::Rustc { compiler, target: compiler.host });
173
174         // Note that this is a short, cryptic, and not scoped directory name. This
175         // is currently to minimize the length of path on Windows where we otherwise
176         // quickly run into path name limit constraints.
177         let out_dir = build.out.join("ct");
178         t!(fs::create_dir_all(&out_dir));
179
180         let _time = util::timeit();
181         let mut cmd = builder.tool_cmd(Tool::CargoTest);
182         try_run(build, cmd.arg(&build.initial_cargo)
183                           .arg(&out_dir)
184                           .env("RUSTC", builder.rustc(compiler))
185                           .env("RUSTDOC", builder.rustdoc(compiler)));
186     }
187 }
188
189 //rules.test("check-cargo", "cargo")
190 //     .dep(|s| s.name("tool-cargo"))
191 //     .host(true)
192 //     .run(move |s| check::cargo(build, s.stage, s.target));
193
194 #[derive(Serialize)]
195 pub struct Cargo<'a> {
196     stage: u32,
197     host: &'a str,
198 }
199
200 impl<'a> Step<'a> for Cargo<'a> {
201     type Output = ();
202     const ONLY_HOSTS: bool = true;
203
204     fn should_run(_builder: &Builder, path: &Path) -> bool {
205         path.ends_with("src/tools/cargo")
206     }
207
208     fn make_run(builder: &Builder, _path: Option<&Path>, _host: &str, target: &str) {
209         builder.ensure(Cargotest {
210             stage: builder.top_stage,
211             host: target,
212         });
213     }
214
215     /// Runs `cargo test` for `cargo` packaged with Rust.
216     fn run(self, builder: &Builder) {
217         let build = builder.build;
218         let compiler = builder.compiler(self.stage, self.host);
219
220         // Configure PATH to find the right rustc. NB. we have to use PATH
221         // and not RUSTC because the Cargo test suite has tests that will
222         // fail if rustc is not spelled `rustc`.
223         let path = builder.sysroot(compiler).join("bin");
224         let old_path = env::var_os("PATH").unwrap_or_default();
225         let newpath = env::join_paths(
226             iter::once(path).chain(env::split_paths(&old_path))
227         ).expect("");
228
229         let mut cargo = builder.cargo(compiler, Mode::Tool, self.host, "test");
230         cargo.arg("--manifest-path").arg(build.src.join("src/tools/cargo/Cargo.toml"));
231         if !build.fail_fast {
232             cargo.arg("--no-fail-fast");
233         }
234
235         let compiler = &Compiler::new(stage, host);
236
237         let mut cargo = build.cargo(compiler, Mode::Tool, host, "test");
238         cargo.arg("--manifest-path").arg(build.src.join("src/tools/cargo/Cargo.toml"));
239         if !build.fail_fast {
240             cargo.arg("--no-fail-fast");
241         }
242
243         // Don't build tests dynamically, just a pain to work with
244         cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
245
246         // Don't run cross-compile tests, we may not have cross-compiled libstd libs
247         // available.
248         cargo.env("CFG_DISABLE_CROSS_TESTS", "1");
249
250         try_run(build, cargo.env("PATH", &path_for_cargo(build, compiler)));
251     }
252 }
253
254 #[derive(Serialize)]
255 pub struct Rls<'a> {
256     stage: u32,
257     host: &'a str,
258 }
259
260 impl<'a> Step<'a> for Rls<'a> {
261     type Output = ();
262
263     /// Runs `cargo test` for the rls.
264     fn run(self, builder: &Builder) {
265         let build = builder.build;
266         let stage = self.stage;
267         let host = self.host;
268         let compiler = &Compiler::new(stage, host);
269
270         let mut cargo = build.cargo(compiler, Mode::Tool, host, "test");
271         cargo.arg("--manifest-path").arg(build.src.join("src/tools/rls/Cargo.toml"));
272
273         // Don't build tests dynamically, just a pain to work with
274         cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1");
275
276         build.add_rustc_lib_path(compiler, &mut cargo);
277
278         try_run(build, &mut cargo);
279     }
280 }
281
282 fn path_for_cargo(build: &Build, compiler: &Compiler) -> OsString {
283     // Configure PATH to find the right rustc. NB. we have to use PATH
284     // and not RUSTC because the Cargo test suite has tests that will
285     // fail if rustc is not spelled `rustc`.
286     let path = build.sysroot(compiler).join("bin");
287     let old_path = env::var_os("PATH").unwrap_or_default();
288     env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("")
289 ||||||| parent of adabe3889e... Move code into Step trait implementations.
290     try_run(build, cargo.env("PATH", newpath));
291 =======
292         try_run(build, cargo.env("PATH", newpath));
293     }
294 >>>>>>> adabe3889e... Move code into Step trait implementations.
295 }
296
297 //rules.test("check-tidy", "src/tools/tidy")
298 //     .dep(|s| s.name("tool-tidy").stage(0))
299 //     .default(true)
300 //     .host(true)
301 //     .only_build(true)
302 //     .run(move |s| check::tidy(build, s.target));
303
304 #[derive(Serialize)]
305 pub struct Tidy<'a> {
306     host: &'a str,
307 }
308
309 impl<'a> Step<'a> for Tidy<'a> {
310     type Output = ();
311     const DEFAULT: bool = true;
312     const ONLY_HOSTS: bool = true;
313     const ONLY_BUILD: bool = true;
314
315     /// Runs the `tidy` tool as compiled in `stage` by the `host` compiler.
316     ///
317     /// This tool in `src/tools` checks up on various bits and pieces of style and
318     /// otherwise just implements a few lint-like checks that are specific to the
319     /// compiler itself.
320     fn run(self, builder: &Builder) {
321         let build = builder.build;
322         let host = self.host;
323
324         let _folder = build.fold_output(|| "tidy");
325         println!("tidy check ({})", host);
326         let mut cmd = builder.tool_cmd(Tool::Tidy);
327         cmd.arg(build.src.join("src"));
328         if !build.config.vendor {
329             cmd.arg("--no-vendor");
330         }
331         if build.config.quiet_tests {
332             cmd.arg("--quiet");
333         }
334         try_run(build, &mut cmd);
335     }
336
337     fn should_run(_builder: &Builder, path: &Path) -> bool {
338         path.ends_with("src/tools/tidy")
339     }
340
341     fn make_run(builder: &Builder, _path: Option<&Path>, _host: &str, _target: &str) {
342         builder.ensure(Tidy {
343             host: &builder.build.build,
344         });
345     }
346 }
347
348 fn testdir(build: &Build, host: &str) -> PathBuf {
349     build.out.join(host).join("test")
350 }
351
352 //    // ========================================================================
353 //    // Test targets
354 //    //
355 //    // Various unit tests and tests suites we can run
356 //    {
357 //        let mut suite = |name, path, mode, dir| {
358 //            rules.test(name, path)
359 //                 .dep(|s| s.name("libtest"))
360 //                 .dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
361 //                 .dep(|s| s.name("test-helpers"))
362 //                 .dep(|s| s.name("remote-copy-libs"))
363 //                 .default(mode != "pretty") // pretty tests don't run everywhere
364 //                 .run(move |s| {
365 //                     check::compiletest(build, &s.compiler(), s.target, mode, dir)
366 //                 });
367 //        };
368 //
369 //        suite("check-ui", "src/test/ui", "ui", "ui");
370 //        suite("check-rpass", "src/test/run-pass", "run-pass", "run-pass");
371 //        suite("check-cfail", "src/test/compile-fail", "compile-fail", "compile-fail");
372 //        suite("check-pfail", "src/test/parse-fail", "parse-fail", "parse-fail");
373 //        suite("check-rfail", "src/test/run-fail", "run-fail", "run-fail");
374 //        suite("check-rpass-valgrind", "src/test/run-pass-valgrind",
375 //              "run-pass-valgrind", "run-pass-valgrind");
376 //        suite("check-mir-opt", "src/test/mir-opt", "mir-opt", "mir-opt");
377 //        if build.config.codegen_tests {
378 //            suite("check-codegen", "src/test/codegen", "codegen", "codegen");
379 //        }
380 //        suite("check-codegen-units", "src/test/codegen-units", "codegen-units",
381 //              "codegen-units");
382 //        suite("check-incremental", "src/test/incremental", "incremental",
383 //              "incremental");
384 //    }
385 //
386 //    if build.build.contains("msvc") {
387 //        // nothing to do for debuginfo tests
388 //    } else {
389 //        rules.test("check-debuginfo-lldb", "src/test/debuginfo-lldb")
390 //             .dep(|s| s.name("libtest"))
391 //             .dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
392 //             .dep(|s| s.name("test-helpers"))
393 //             .dep(|s| s.name("debugger-scripts"))
394 //             .run(move |s| check::compiletest(build, &s.compiler(), s.target,
395 //                                         "debuginfo-lldb", "debuginfo"));
396 //        rules.test("check-debuginfo-gdb", "src/test/debuginfo-gdb")
397 //             .dep(|s| s.name("libtest"))
398 //             .dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
399 //             .dep(|s| s.name("test-helpers"))
400 //             .dep(|s| s.name("debugger-scripts"))
401 //             .dep(|s| s.name("remote-copy-libs"))
402 //             .run(move |s| check::compiletest(build, &s.compiler(), s.target,
403 //                                         "debuginfo-gdb", "debuginfo"));
404 //        let mut rule = rules.test("check-debuginfo", "src/test/debuginfo");
405 //        rule.default(true);
406 //        if build.build.contains("apple") {
407 //            rule.dep(|s| s.name("check-debuginfo-lldb"));
408 //        } else {
409 //            rule.dep(|s| s.name("check-debuginfo-gdb"));
410 //        }
411 //    }
412 //
413 //
414 //
415 //    {
416 //        let mut suite = |name, path, mode, dir| {
417 //            rules.test(name, path)
418 //                 .dep(|s| s.name("librustc"))
419 //                 .dep(|s| s.name("test-helpers"))
420 //                 .dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
421 //                 .default(mode != "pretty")
422 //                 .host(true)
423 //                 .run(move |s| {
424 //                     check::compiletest(build, &s.compiler(), s.target, mode, dir)
425 //                 });
426 //        };
427 //
428 //        suite("check-ui-full", "src/test/ui-fulldeps", "ui", "ui-fulldeps");
429 //        suite("check-rpass-full", "src/test/run-pass-fulldeps",
430 //              "run-pass", "run-pass-fulldeps");
431 //        suite("check-rfail-full", "src/test/run-fail-fulldeps",
432 //              "run-fail", "run-fail-fulldeps");
433 //        suite("check-cfail-full", "src/test/compile-fail-fulldeps",
434 //              "compile-fail", "compile-fail-fulldeps");
435 //        suite("check-rmake", "src/test/run-make", "run-make", "run-make");
436 //        suite("check-rustdoc", "src/test/rustdoc", "rustdoc", "rustdoc");
437 //        suite("check-pretty", "src/test/pretty", "pretty", "pretty");
438 //        suite("check-pretty-rpass", "src/test/run-pass/pretty", "pretty",
439 //              "run-pass");
440 //        suite("check-pretty-rfail", "src/test/run-fail/pretty", "pretty",
441 //              "run-fail");
442 //        suite("check-pretty-valgrind", "src/test/run-pass-valgrind/pretty", "pretty",
443 //              "run-pass-valgrind");
444 //        suite("check-pretty-rpass-full", "src/test/run-pass-fulldeps/pretty",
445 //              "pretty", "run-pass-fulldeps");
446 //        suite("check-pretty-rfail-full", "src/test/run-fail-fulldeps/pretty",
447 //              "pretty", "run-fail-fulldeps");
448 //    }
449
450 #[derive(Serialize)]
451 pub struct Compiletest<'a> {
452     compiler: Compiler<'a>,
453     target: &'a str,
454     mode: &'a str,
455     suite: &'a str,
456 }
457
458 #[derive(Copy, Clone, Debug, PartialEq)]
459 struct Test {
460     path: &'static str,
461     mode: &'static str,
462     suite: &'static str,
463 }
464
465 static DEFAULT_COMPILETESTS: &[Test] = &[
466     Test { path: "src/test/ui", mode: "ui", suite: "ui" },
467     Test { path: "src/test/run-pass", mode: "run-pass", suite: "run-pass" },
468     Test { path: "src/test/compile-fail", mode: "compile-fail", suite: "compile-fail" },
469     Test { path: "src/test/parse-fail", mode: "parse-fail", suite: "parse-fail" },
470     Test { path: "src/test/run-fail", mode: "run-fail", suite: "run-fail" },
471     Test {
472         path: "src/test/run-pass-valgrind",
473         mode: "run-pass-valgrind",
474         suite: "run-pass-valgrind"
475     },
476     Test { path: "src/test/mir-opt", mode: "mir-opt", suite: "mir-opt" },
477     Test { path: "src/test/codegen", mode: "codegen", suite: "codegen" },
478     Test { path: "src/test/codegen-units", mode: "codegen-units", suite: "codegen-units" },
479     Test { path: "src/test/incremental", mode: "incremental", suite: "incremental" },
480
481     // What this runs varies depending on the native platform being apple
482     Test { path: "src/test/debuginfo", mode: "debuginfo-XXX", suite: "debuginfo" },
483 ];
484
485 // Also default, but host-only.
486 static HOST_COMPILETESTS: &[Test] = &[
487     Test { path: "src/test/ui-fulldeps", mode: "ui", suite: "ui-fulldeps" },
488     Test { path: "src/test/run-pass-fulldeps", mode: "run-pass", suite: "run-pass-fulldeps" },
489     Test { path: "src/test/run-fail-fulldeps", mode: "run-fail", suite: "run-fail-fulldeps" },
490     Test {
491         path: "src/test/compile-fail-fulldeps",
492         mode: "compile-fail",
493         suite: "compile-fail-fulldeps",
494     },
495     Test { path: "src/test/run-make", mode: "run-make", suite: "run-make" },
496     Test { path: "src/test/rustdoc", mode: "rustdoc", suite: "rustdoc" },
497
498     Test { path: "src/test/pretty", mode: "pretty", suite: "pretty" },
499     Test { path: "src/test/run-pass/pretty", mode: "pretty", suite: "run-pass" },
500     Test { path: "src/test/run-fail/pretty", mode: "pretty", suite: "run-fail" },
501     Test { path: "src/test/run-pass-valgrind/pretty", mode: "pretty", suite: "run-pass-valgrind" },
502     Test { path: "src/test/run-pass-fulldeps/pretty", mode: "pretty", suite: "run-pass-fulldeps" },
503     Test { path: "src/test/run-fail-fulldeps/pretty", mode: "pretty", suite: "run-fail-fulldeps" },
504 ];
505
506 static COMPILETESTS: &[Test] = &[
507     Test { path: "src/test/debuginfo-lldb", mode: "debuginfo-lldb", suite: "debuginfo" },
508     Test { path: "src/test/debuginfo-gdb", mode: "debuginfo-gdb", suite: "debuginfo" },
509 ];
510
511 impl<'a> Step<'a> for Compiletest<'a> {
512     type Output = ();
513     const DEFAULT: bool = true;
514
515     fn should_run(_builder: &Builder, path: &Path) -> bool {
516         // Note that this is general, while a few more cases are skipped inside
517         // run() itself. This is to avoid duplication across should_run and
518         // make_run.
519         COMPILETESTS.iter().chain(DEFAULT_COMPILETESTS).chain(HOST_COMPILETESTS).any(|&test| {
520             path.ends_with(test.path)
521         })
522     }
523
524     fn make_run(builder: &Builder, path: Option<&Path>, host: &str, target: &str) {
525         let compiler = builder.compiler(builder.top_stage, host);
526
527         let test = path.map(|path| {
528             COMPILETESTS.iter().chain(DEFAULT_COMPILETESTS).chain(HOST_COMPILETESTS).find(|&&test| {
529                 path.ends_with(test.path)
530             }).unwrap_or_else(|| {
531                 panic!("make_run in compile test to receive test path, received {:?}", path);
532             })
533         });
534
535         if let Some(test) = test { // specific test
536             let target = if HOST_COMPILETESTS.contains(test) {
537                 host
538             } else {
539                 target
540             };
541             builder.ensure(Compiletest {
542                 compiler, target, mode: test.mode, suite: test.suite
543             });
544         } else { // default tests
545             for test in DEFAULT_COMPILETESTS {
546                 builder.ensure(Compiletest {
547                     compiler,
548                     target,
549                     mode: test.mode,
550                     suite: test.suite
551                 });
552             }
553             for test in HOST_COMPILETESTS {
554                 if test.mode != "pretty" {
555                     builder.ensure(Compiletest {
556                         compiler,
557                         target: host,
558                         mode: test.mode,
559                         suite: test.suite
560                     });
561                 }
562             }
563         }
564     }
565
566     /// Executes the `compiletest` tool to run a suite of tests.
567     ///
568     /// Compiles all tests with `compiler` for `target` with the specified
569     /// compiletest `mode` and `suite` arguments. For example `mode` can be
570     /// "run-pass" or `suite` can be something like `debuginfo`.
571     fn run(self, builder: &Builder) {
572         let build = builder.build;
573         let compiler = self.compiler;
574         let target = self.target;
575         let mode = self.mode;
576         let suite = self.suite;
577
578         // Skip codegen tests if they aren't enabled in configuration.
579         if !build.config.codegen_tests && suite == "codegen" {
580             return;
581         }
582
583         if suite == "debuginfo" {
584             if mode == "debuginfo-XXX" {
585                 return if build.build.contains("apple") {
586                     builder.ensure(Compiletest {
587                         mode: "debuginfo-lldb",
588                         ..self
589                     })
590                 } else {
591                     builder.ensure(Compiletest {
592                         mode: "debuginfo-gdb",
593                         ..self
594                     })
595                 };
596             }
597
598             // Skip debuginfo tests on MSVC
599             if build.build.contains("msvc") {
600                 return;
601             }
602
603             builder.ensure(dist::DebuggerScripts {
604                 sysroot: &builder.sysroot(compiler),
605                 host: compiler.host
606             });
607
608             if mode == "debuginfo-gdb" {
609                 builder.ensure(RemoteCopyLibs { compiler, target });
610             }
611         }
612
613         if suite.ends_with("fulldeps") ||
614             // FIXME: Does pretty need librustc compiled? Note that there are
615             // fulldeps test suites with mode = pretty as well.
616             mode == "pretty" ||
617             mode == "rustdoc" ||
618             mode == "run-make" {
619             builder.ensure(compile::Rustc { compiler, target });
620         }
621
622         builder.ensure(compile::Test { compiler, target });
623         builder.ensure(native::TestHelpers { target });
624         builder.ensure(RemoteCopyLibs { compiler, target });
625
626         let _folder = build.fold_output(|| format!("test_{}", suite));
627         println!("Check compiletest suite={} mode={} ({} -> {})",
628                  suite, mode, compiler.host, target);
629         let mut cmd = builder.tool_cmd(Tool::Compiletest);
630
631         // compiletest currently has... a lot of arguments, so let's just pass all
632         // of them!
633
634         cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(compiler));
635         cmd.arg("--run-lib-path").arg(builder.sysroot_libdir(compiler, target));
636         cmd.arg("--rustc-path").arg(builder.rustc(compiler));
637         cmd.arg("--rustdoc-path").arg(builder.rustdoc(compiler));
638         cmd.arg("--src-base").arg(build.src.join("src/test").join(suite));
639         cmd.arg("--build-base").arg(testdir(build, compiler.host).join(suite));
640         cmd.arg("--stage-id").arg(format!("stage{}-{}", compiler.stage, target));
641         cmd.arg("--mode").arg(mode);
642         cmd.arg("--target").arg(target);
643         cmd.arg("--host").arg(compiler.host);
644         cmd.arg("--llvm-filecheck").arg(build.llvm_filecheck(&build.build));
645
646         if let Some(ref nodejs) = build.config.nodejs {
647             cmd.arg("--nodejs").arg(nodejs);
648         }
649
650         let mut flags = vec!["-Crpath".to_string()];
651         if build.config.rust_optimize_tests {
652             flags.push("-O".to_string());
653         }
654         if build.config.rust_debuginfo_tests {
655             flags.push("-g".to_string());
656         }
657
658         let mut hostflags = build.rustc_flags(compiler.host);
659         hostflags.extend(flags.clone());
660         cmd.arg("--host-rustcflags").arg(hostflags.join(" "));
661
662         let mut targetflags = build.rustc_flags(&target);
663         targetflags.extend(flags);
664         targetflags.push(format!("-Lnative={}",
665                                  build.test_helpers_out(target).display()));
666         cmd.arg("--target-rustcflags").arg(targetflags.join(" "));
667
668         cmd.arg("--docck-python").arg(build.python());
669
670         if build.build.ends_with("apple-darwin") {
671             // Force /usr/bin/python on macOS for LLDB tests because we're loading the
672             // LLDB plugin's compiled module which only works with the system python
673             // (namely not Homebrew-installed python)
674             cmd.arg("--lldb-python").arg("/usr/bin/python");
675         } else {
676             cmd.arg("--lldb-python").arg(build.python());
677         }
678
679         if let Some(ref gdb) = build.config.gdb {
680             cmd.arg("--gdb").arg(gdb);
681         }
682         if let Some(ref vers) = build.lldb_version {
683             cmd.arg("--lldb-version").arg(vers);
684         }
685         if let Some(ref dir) = build.lldb_python_dir {
686             cmd.arg("--lldb-python-dir").arg(dir);
687         }
688         let llvm_config = build.llvm_config(target);
689         let llvm_version = output(Command::new(&llvm_config).arg("--version"));
690         cmd.arg("--llvm-version").arg(llvm_version);
691         if !build.is_rust_llvm(target) {
692             cmd.arg("--system-llvm");
693         }
694
695         cmd.args(&build.flags.cmd.test_args());
696
697         if build.is_verbose() {
698             cmd.arg("--verbose");
699         }
700
701         if build.config.quiet_tests {
702             cmd.arg("--quiet");
703         }
704
705         // Only pass correct values for these flags for the `run-make` suite as it
706         // requires that a C++ compiler was configured which isn't always the case.
707         if suite == "run-make" {
708             let llvm_components = output(Command::new(&llvm_config).arg("--components"));
709             let llvm_cxxflags = output(Command::new(&llvm_config).arg("--cxxflags"));
710             cmd.arg("--cc").arg(build.cc(target))
711                .arg("--cxx").arg(build.cxx(target).unwrap())
712                .arg("--cflags").arg(build.cflags(target).join(" "))
713                .arg("--llvm-components").arg(llvm_components.trim())
714                .arg("--llvm-cxxflags").arg(llvm_cxxflags.trim());
715         } else {
716             cmd.arg("--cc").arg("")
717                .arg("--cxx").arg("")
718                .arg("--cflags").arg("")
719                .arg("--llvm-components").arg("")
720                .arg("--llvm-cxxflags").arg("");
721         }
722
723         if build.remote_tested(target) {
724             cmd.arg("--remote-test-client").arg(builder.tool_exe(Tool::RemoteTestClient));
725         }
726
727         // Running a C compiler on MSVC requires a few env vars to be set, to be
728         // sure to set them here.
729         //
730         // Note that if we encounter `PATH` we make sure to append to our own `PATH`
731         // rather than stomp over it.
732         if target.contains("msvc") {
733             for &(ref k, ref v) in build.cc[target].0.env() {
734                 if k != "PATH" {
735                     cmd.env(k, v);
736                 }
737             }
738         }
739         cmd.env("RUSTC_BOOTSTRAP", "1");
740         build.add_rust_test_threads(&mut cmd);
741
742         if build.config.sanitizers {
743             cmd.env("SANITIZER_SUPPORT", "1");
744         }
745
746         if build.config.profiler {
747             cmd.env("PROFILER_SUPPORT", "1");
748         }
749
750         cmd.arg("--adb-path").arg("adb");
751         cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
752         if target.contains("android") {
753             // Assume that cc for this target comes from the android sysroot
754             cmd.arg("--android-cross-path")
755                .arg(build.cc(target).parent().unwrap().parent().unwrap());
756         } else {
757             cmd.arg("--android-cross-path").arg("");
758         }
759
760         build.ci_env.force_coloring_in_ci(&mut cmd);
761
762         let _time = util::timeit();
763         try_run(build, &mut cmd);
764     }
765 }
766
767 #[derive(Serialize)]
768 pub struct Docs<'a> {
769     compiler: Compiler<'a>,
770 }
771
772 // rules.test("check-docs", "src/doc")
773 //     .dep(|s| s.name("libtest"))
774 //     .default(true)
775 //     .host(true)
776 //     .run(move |s| check::docs(build, &s.compiler()));
777 impl<'a> Step<'a> for Docs<'a> {
778     type Output = ();
779     const DEFAULT: bool = true;
780     const ONLY_HOSTS: bool = true;
781
782     fn should_run(_builder: &Builder, path: &Path) -> bool {
783         path.ends_with("src/doc")
784     }
785
786     fn make_run(builder: &Builder, _path: Option<&Path>, host: &str, _target: &str) {
787         builder.ensure(Docs {
788             compiler: builder.compiler(builder.top_stage, host),
789         });
790     }
791
792     /// Run `rustdoc --test` for all documentation in `src/doc`.
793     ///
794     /// This will run all tests in our markdown documentation (e.g. the book)
795     /// located in `src/doc`. The `rustdoc` that's run is the one that sits next to
796     /// `compiler`.
797     fn run(self, builder: &Builder) {
798         let build = builder.build;
799         let compiler = self.compiler;
800
801         builder.ensure(compile::Test { compiler, target: compiler.host });
802
803         // Do a breadth-first traversal of the `src/doc` directory and just run
804         // tests for all files that end in `*.md`
805         let mut stack = vec![build.src.join("src/doc")];
806         let _time = util::timeit();
807         let _folder = build.fold_output(|| "test_docs");
808
809         while let Some(p) = stack.pop() {
810             if p.is_dir() {
811                 stack.extend(t!(p.read_dir()).map(|p| t!(p).path()));
812                 continue
813             }
814
815             if p.extension().and_then(|s| s.to_str()) != Some("md") {
816                 continue;
817             }
818
819             // The nostarch directory in the book is for no starch, and so isn't
820             // guaranteed to build. We don't care if it doesn't build, so skip it.
821             if p.to_str().map_or(false, |p| p.contains("nostarch")) {
822                 continue;
823             }
824
825             markdown_test(builder, compiler, &p);
826         }
827     }
828 }
829
830 //rules.test("check-error-index", "src/tools/error_index_generator")
831 //     .dep(|s| s.name("libstd"))
832 //     .dep(|s| s.name("tool-error-index").host(s.host).stage(0))
833 //     .default(true)
834 //     .host(true)
835 //     .run(move |s| check::error_index(build, &s.compiler()));
836
837 #[derive(Serialize)]
838 pub struct ErrorIndex<'a> {
839     compiler: Compiler<'a>,
840 }
841
842 impl<'a> Step<'a> for ErrorIndex<'a> {
843     type Output = ();
844     const DEFAULT: bool = true;
845     const ONLY_HOSTS: bool = true;
846
847     fn should_run(_builder: &Builder, path: &Path) -> bool {
848         path.ends_with("src/tools/error_index_generator")
849     }
850
851     fn make_run(builder: &Builder, _path: Option<&Path>, host: &str, _target: &str) {
852         builder.ensure(ErrorIndex {
853             compiler: builder.compiler(builder.top_stage, host),
854         });
855     }
856
857     /// Run the error index generator tool to execute the tests located in the error
858     /// index.
859     ///
860     /// The `error_index_generator` tool lives in `src/tools` and is used to
861     /// generate a markdown file from the error indexes of the code base which is
862     /// then passed to `rustdoc --test`.
863     fn run(self, builder: &Builder) {
864         let build = builder.build;
865         let compiler = self.compiler;
866
867         builder.ensure(compile::Std { compiler, target: compiler.host });
868
869         let _folder = build.fold_output(|| "test_error_index");
870         println!("Testing error-index stage{}", compiler.stage);
871
872         let dir = testdir(build, compiler.host);
873         t!(fs::create_dir_all(&dir));
874         let output = dir.join("error-index.md");
875
876         let _time = util::timeit();
877         build.run(builder.tool_cmd(Tool::ErrorIndex)
878                     .arg("markdown")
879                     .arg(&output)
880                     .env("CFG_BUILD", &build.build));
881
882         markdown_test(builder, compiler, &output);
883     }
884 }
885
886 fn markdown_test(builder: &Builder, compiler: Compiler, markdown: &Path) {
887     let build = builder.build;
888     let mut file = t!(File::open(markdown));
889     let mut contents = String::new();
890     t!(file.read_to_string(&mut contents));
891     if !contents.contains("```") {
892         return;
893     }
894
895     println!("doc tests for: {}", markdown.display());
896     let mut cmd = Command::new(builder.rustdoc(compiler));
897     builder.add_rustc_lib_path(compiler, &mut cmd);
898     build.add_rust_test_threads(&mut cmd);
899     cmd.arg("--test");
900     cmd.arg(markdown);
901     cmd.env("RUSTC_BOOTSTRAP", "1");
902
903     let test_args = build.flags.cmd.test_args().join(" ");
904     cmd.arg("--test-args").arg(test_args);
905
906     if build.config.quiet_tests {
907         try_run_quiet(build, &mut cmd);
908     } else {
909         try_run(build, &mut cmd);
910     }
911 }
912
913 //    for (krate, path, _default) in krates("rustc-main") {
914 //        rules.test(&krate.test_step, path)
915 //             .dep(|s| s.name("librustc"))
916 //             .dep(|s| s.name("remote-copy-libs"))
917 //             .host(true)
918 //             .run(move |s| check::krate(build, &s.compiler(), s.target,
919 //                                        Mode::Librustc, TestKind::Test,
920 //                                        Some(&krate.name)));
921 //    }
922 //    rules.test("check-rustc-all", "path/to/nowhere")
923 //         .dep(|s| s.name("librustc"))
924 //         .dep(|s| s.name("remote-copy-libs"))
925 //         .default(true)
926 //         .host(true)
927 //         .run(move |s| check::krate(build, &s.compiler(), s.target,
928 //                                    Mode::Librustc, TestKind::Test, None));
929 #[derive(Serialize)]
930 pub struct KrateLibrustc<'a> {
931     compiler: Compiler<'a>,
932     target: &'a str,
933     test_kind: TestKind,
934     krate: Option<&'a str>,
935 }
936
937 impl<'a> Step<'a> for KrateLibrustc<'a> {
938     type Output = ();
939     const DEFAULT: bool = true;
940     const ONLY_HOSTS: bool = true;
941
942     fn should_run(builder: &Builder, path: &Path) -> bool {
943         builder.crates("rustc-main").into_iter().any(|(_, krate_path)| {
944             path.ends_with(krate_path)
945         })
946     }
947
948     fn make_run(builder: &Builder, path: Option<&Path>, host: &str, target: &str) {
949         let compiler = builder.compiler(builder.top_stage, host);
950
951         let run = |name: Option<&str>| {
952             let test_kind = if builder.kind == Kind::Test {
953                 TestKind::Test
954             } else if builder.kind == Kind::Bench {
955                 TestKind::Bench
956             } else {
957                 panic!("unexpected builder.kind in Krate: {:?}", builder.kind);
958             };
959
960             builder.ensure(KrateLibrustc {
961                 compiler,
962                 target,
963                 test_kind: test_kind,
964                 krate: name,
965             });
966         };
967
968         if let Some(path) = path {
969             for (name, krate_path) in builder.crates("rustc-main") {
970                 if path.ends_with(krate_path) {
971                     run(Some(name));
972                 }
973             }
974         } else {
975             run(None);
976         }
977     }
978
979
980     fn run(self, builder: &Builder) {
981         builder.ensure(Krate {
982             compiler: self.compiler,
983             target: self.target,
984             mode: Mode::Librustc,
985             test_kind: self.test_kind,
986             krate: self.krate,
987         });
988     }
989 }
990
991
992 //    for (krate, path, _default) in krates("std") {
993 //        rules.test(&krate.test_step, path)
994 //             .dep(|s| s.name("libtest"))
995 //             .dep(|s| s.name("remote-copy-libs"))
996 //             .run(move |s| check::krate(build, &s.compiler(), s.target,
997 //                                        Mode::Libstd, TestKind::Test,
998 //                                        Some(&krate.name)));
999 //    }
1000 //    rules.test("check-std-all", "path/to/nowhere")
1001 //         .dep(|s| s.name("libtest"))
1002 //         .dep(|s| s.name("remote-copy-libs"))
1003 //         .default(true)
1004 //         .run(move |s| check::krate(build, &s.compiler(), s.target,
1005 //                                    Mode::Libstd, TestKind::Test, None));
1006 //
1007 //    // std benchmarks
1008 //    for (krate, path, _default) in krates("std") {
1009 //        rules.bench(&krate.bench_step, path)
1010 //             .dep(|s| s.name("libtest"))
1011 //             .dep(|s| s.name("remote-copy-libs"))
1012 //             .run(move |s| check::krate(build, &s.compiler(), s.target,
1013 //                                        Mode::Libstd, TestKind::Bench,
1014 //                                        Some(&krate.name)));
1015 //    }
1016 //    rules.bench("bench-std-all", "path/to/nowhere")
1017 //         .dep(|s| s.name("libtest"))
1018 //         .dep(|s| s.name("remote-copy-libs"))
1019 //         .default(true)
1020 //         .run(move |s| check::krate(build, &s.compiler(), s.target,
1021 //                                    Mode::Libstd, TestKind::Bench, None));
1022 //
1023 //    for (krate, path, _default) in krates("test") {
1024 //        rules.test(&krate.test_step, path)
1025 //             .dep(|s| s.name("libtest"))
1026 //             .dep(|s| s.name("remote-copy-libs"))
1027 //             .run(move |s| check::krate(build, &s.compiler(), s.target,
1028 //                                        Mode::Libtest, TestKind::Test,
1029 //                                        Some(&krate.name)));
1030 //    }
1031 //    rules.test("check-test-all", "path/to/nowhere")
1032 //         .dep(|s| s.name("libtest"))
1033 //         .dep(|s| s.name("remote-copy-libs"))
1034 //         .default(true)
1035 //         .run(move |s| check::krate(build, &s.compiler(), s.target,
1036 //                                    Mode::Libtest, TestKind::Test, None));
1037
1038 #[derive(Serialize)]
1039 pub struct Krate<'a> {
1040     compiler: Compiler<'a>,
1041     target: &'a str,
1042     mode: Mode,
1043     test_kind: TestKind,
1044     krate: Option<&'a str>,
1045 }
1046
1047 impl<'a> Step<'a> for Krate<'a> {
1048     type Output = ();
1049     const DEFAULT: bool = true;
1050
1051     fn should_run(builder: &Builder, path: &Path) -> bool {
1052         builder.crates("std").into_iter().any(|(_, krate_path)| {
1053             path.ends_with(krate_path)
1054         }) ||
1055         builder.crates("test").into_iter().any(|(_, krate_path)| {
1056             path.ends_with(krate_path)
1057         })
1058     }
1059
1060     fn make_run(builder: &Builder, path: Option<&Path>, host: &str, target: &str) {
1061         let compiler = builder.compiler(builder.top_stage, host);
1062
1063         let run = |mode: Mode, name: Option<&str>| {
1064             let test_kind = if builder.kind == Kind::Test {
1065                 TestKind::Test
1066             } else if builder.kind == Kind::Bench {
1067                 TestKind::Bench
1068             } else {
1069                 panic!("unexpected builder.kind in Krate: {:?}", builder.kind);
1070             };
1071
1072             builder.ensure(Krate {
1073                 compiler, target,
1074                 mode: mode,
1075                 test_kind: test_kind,
1076                 krate: name,
1077             });
1078         };
1079
1080         if let Some(path) = path {
1081             for (name, krate_path) in builder.crates("std") {
1082                 if path.ends_with(krate_path) {
1083                     run(Mode::Libstd, Some(name));
1084                 }
1085             }
1086             for (name, krate_path) in builder.crates("test") {
1087                 if path.ends_with(krate_path) {
1088                     run(Mode::Libtest, Some(name));
1089                 }
1090             }
1091         } else {
1092             run(Mode::Libstd, None);
1093             run(Mode::Libtest, None);
1094         }
1095     }
1096
1097     /// Run all unit tests plus documentation tests for an entire crate DAG defined
1098     /// by a `Cargo.toml`
1099     ///
1100     /// This is what runs tests for crates like the standard library, compiler, etc.
1101     /// It essentially is the driver for running `cargo test`.
1102     ///
1103     /// Currently this runs all tests for a DAG by passing a bunch of `-p foo`
1104     /// arguments, and those arguments are discovered from `cargo metadata`.
1105     fn run(self, builder: &Builder) {
1106         let build = builder.build;
1107         let compiler = self.compiler;
1108         let target = self.target;
1109         let mode = self.mode;
1110         let test_kind = self.test_kind;
1111         let krate = self.krate;
1112
1113         builder.ensure(compile::Test { compiler, target });
1114         builder.ensure(RemoteCopyLibs { compiler, target });
1115         let (name, path, features, root) = match mode {
1116             Mode::Libstd => {
1117                 ("libstd", "src/libstd", build.std_features(), "std")
1118             }
1119             Mode::Libtest => {
1120                 ("libtest", "src/libtest", String::new(), "test")
1121             }
1122             Mode::Librustc => {
1123                 builder.ensure(compile::Rustc { compiler, target });
1124                 ("librustc", "src/rustc", build.rustc_features(), "rustc-main")
1125             }
1126             _ => panic!("can only test libraries"),
1127         };
1128         let _folder = build.fold_output(|| {
1129             format!("{}_stage{}-{}", test_kind.subcommand(), compiler.stage, name)
1130         });
1131         println!("{} {} stage{} ({} -> {})", test_kind, name, compiler.stage,
1132                 compiler.host, target);
1133
1134         // If we're not doing a full bootstrap but we're testing a stage2 version of
1135         // libstd, then what we're actually testing is the libstd produced in
1136         // stage1. Reflect that here by updating the compiler that we're working
1137         // with automatically.
1138         let compiler = if build.force_use_stage1(compiler, target) {
1139             builder.compiler(1, compiler.host)
1140         } else {
1141             compiler.clone()
1142         };
1143
1144         // Build up the base `cargo test` command.
1145         //
1146         // Pass in some standard flags then iterate over the graph we've discovered
1147         // in `cargo metadata` with the maps above and figure out what `-p`
1148         // arguments need to get passed.
1149         let mut cargo = builder.cargo(compiler, mode, target, test_kind.subcommand());
1150         cargo.arg("--manifest-path")
1151             .arg(build.src.join(path).join("Cargo.toml"))
1152             .arg("--features").arg(features);
1153         if test_kind.subcommand() == "test" && !build.fail_fast {
1154             cargo.arg("--no-fail-fast");
1155         }
1156
1157         match krate {
1158             Some(krate) => {
1159                 cargo.arg("-p").arg(krate);
1160             }
1161             None => {
1162                 let mut visited = HashSet::new();
1163                 let mut next = vec![root];
1164                 while let Some(name) = next.pop() {
1165                     // Right now jemalloc is our only target-specific crate in the
1166                     // sense that it's not present on all platforms. Custom skip it
1167                     // here for now, but if we add more this probably wants to get
1168                     // more generalized.
1169                     //
1170                     // Also skip `build_helper` as it's not compiled normally for
1171                     // target during the bootstrap and it's just meant to be a
1172                     // helper crate, not tested. If it leaks through then it ends up
1173                     // messing with various mtime calculations and such.
1174                     if !name.contains("jemalloc") && name != "build_helper" {
1175                         cargo.arg("-p").arg(&format!("{}:0.0.0", name));
1176                     }
1177                     for dep in build.crates[name].deps.iter() {
1178                         if visited.insert(dep) {
1179                             next.push(dep);
1180                         }
1181                     }
1182                 }
1183             }
1184         }
1185
1186         // The tests are going to run with the *target* libraries, so we need to
1187         // ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
1188         //
1189         // Note that to run the compiler we need to run with the *host* libraries,
1190         // but our wrapper scripts arrange for that to be the case anyway.
1191         let mut dylib_path = dylib_path();
1192         dylib_path.insert(0, builder.sysroot_libdir(compiler, target));
1193         cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1194
1195         if target.contains("emscripten") || build.remote_tested(target) {
1196             cargo.arg("--no-run");
1197         }
1198
1199         cargo.arg("--");
1200
1201         if build.config.quiet_tests {
1202             cargo.arg("--quiet");
1203         }
1204
1205         let _time = util::timeit();
1206
1207         if target.contains("emscripten") {
1208             build.run(&mut cargo);
1209             krate_emscripten(build, compiler, target, mode);
1210         } else if build.remote_tested(target) {
1211             build.run(&mut cargo);
1212             krate_remote(builder, compiler, target, mode);
1213         } else {
1214             cargo.args(&build.flags.cmd.test_args());
1215             try_run(build, &mut cargo);
1216         }
1217     }
1218 }
1219
1220 fn krate_emscripten(build: &Build,
1221                     compiler: Compiler,
1222                     target: &str,
1223                     mode: Mode) {
1224     let out_dir = build.cargo_out(compiler, mode, target);
1225     let tests = find_tests(&out_dir.join("deps"), target);
1226
1227     let nodejs = build.config.nodejs.as_ref().expect("nodejs not configured");
1228     for test in tests {
1229         println!("running {}", test.display());
1230         let mut cmd = Command::new(nodejs);
1231         cmd.arg(&test);
1232         if build.config.quiet_tests {
1233             cmd.arg("--quiet");
1234         }
1235         try_run(build, &mut cmd);
1236     }
1237 }
1238
1239 fn krate_remote(builder: &Builder,
1240                 compiler: Compiler,
1241                 target: &str,
1242                 mode: Mode) {
1243     let build = builder.build;
1244     let out_dir = build.cargo_out(compiler, mode, target);
1245     let tests = find_tests(&out_dir.join("deps"), target);
1246
1247     let tool = builder.tool_exe(Tool::RemoteTestClient);
1248     for test in tests {
1249         let mut cmd = Command::new(&tool);
1250         cmd.arg("run")
1251            .arg(&test);
1252         if build.config.quiet_tests {
1253             cmd.arg("--quiet");
1254         }
1255         cmd.args(&build.flags.cmd.test_args());
1256         try_run(build, &mut cmd);
1257     }
1258 }
1259
1260 fn find_tests(dir: &Path, target: &str) -> Vec<PathBuf> {
1261     let mut dst = Vec::new();
1262     for e in t!(dir.read_dir()).map(|e| t!(e)) {
1263         let file_type = t!(e.file_type());
1264         if !file_type.is_file() {
1265             continue
1266         }
1267         let filename = e.file_name().into_string().unwrap();
1268         if (target.contains("windows") && filename.ends_with(".exe")) ||
1269            (!target.contains("windows") && !filename.contains(".")) ||
1270            (target.contains("emscripten") &&
1271             filename.ends_with(".js") &&
1272             !filename.ends_with(".asm.js")) {
1273             dst.push(e.path());
1274         }
1275     }
1276     dst
1277 }
1278
1279 //    rules.test("remote-copy-libs", "path/to/nowhere")
1280 //         .dep(|s| s.name("libtest"))
1281 //         .dep(move |s| {
1282 //             if build.remote_tested(s.target) {
1283 //                s.name("tool-remote-test-client").target(s.host).stage(0)
1284 //             } else {
1285 //                 Step::noop()
1286 //             }
1287 //         })
1288 //         .dep(move |s| {
1289 //             if build.remote_tested(s.target) {
1290 //                s.name("tool-remote-test-server")
1291 //             } else {
1292 //                 Step::noop()
1293 //             }
1294 //         })
1295 //         .run(move |s| check::remote_copy_libs(build, &s.compiler(), s.target));
1296 //
1297
1298 /// Some test suites are run inside emulators or on remote devices, and most
1299 /// of our test binaries are linked dynamically which means we need to ship
1300 /// the standard library and such to the emulator ahead of time. This step
1301 /// represents this and is a dependency of all test suites.
1302 ///
1303 /// Most of the time this is a noop. For some steps such as shipping data to
1304 /// QEMU we have to build our own tools so we've got conditional dependencies
1305 /// on those programs as well. Note that the remote test client is built for
1306 /// the build target (us) and the server is built for the target.
1307 #[derive(Serialize)]
1308 pub struct RemoteCopyLibs<'a> {
1309     compiler: Compiler<'a>,
1310     target: &'a str,
1311 }
1312
1313 impl<'a> Step<'a> for RemoteCopyLibs<'a> {
1314     type Output = ();
1315
1316     fn run(self, builder: &Builder) {
1317         let build = builder.build;
1318         let compiler = self.compiler;
1319         let target = self.target;
1320         if !build.remote_tested(target) {
1321             return
1322         }
1323
1324         builder.ensure(compile::Test { compiler, target });
1325
1326         println!("REMOTE copy libs to emulator ({})", target);
1327         t!(fs::create_dir_all(build.out.join("tmp")));
1328
1329         let server = builder.ensure(tool::RemoteTestServer { compiler, target }));
1330
1331         // Spawn the emulator and wait for it to come online
1332         let tool = builder.tool_exe(Tool::RemoteTestClient);
1333         let mut cmd = Command::new(&tool);
1334         cmd.arg("spawn-emulator")
1335            .arg(target)
1336            .arg(&server)
1337            .arg(build.out.join("tmp"));
1338         if let Some(rootfs) = build.qemu_rootfs(target) {
1339             cmd.arg(rootfs);
1340         }
1341         build.run(&mut cmd);
1342
1343         // Push all our dylibs to the emulator
1344         for f in t!(builder.sysroot_libdir(compiler, target).read_dir()) {
1345             let f = t!(f);
1346             let name = f.file_name().into_string().unwrap();
1347             if util::is_dylib(&name) {
1348                 build.run(Command::new(&tool)
1349                                   .arg("push")
1350                                   .arg(f.path()));
1351             }
1352         }
1353     }
1354 }
1355
1356 //rules.test("check-distcheck", "distcheck")
1357 //     .dep(|s| s.name("dist-plain-source-tarball"))
1358 //     .dep(|s| s.name("dist-src"))
1359 //     .run(move |_| check::distcheck(build));
1360
1361 #[derive(Serialize)]
1362 pub struct Distcheck;
1363
1364 impl<'a> Step<'a> for Distcheck {
1365     type Output = ();
1366
1367     /// Run "distcheck", a 'make check' from a tarball
1368     fn run(self, builder: &Builder) {
1369         let build = builder.build;
1370
1371         builder.ensure(dist::PlainSourceTarball);
1372         builder.ensure(dist::Src);
1373
1374         if build.build != "x86_64-unknown-linux-gnu" {
1375             return
1376         }
1377         if !build.config.host.iter().any(|s| s == "x86_64-unknown-linux-gnu") {
1378             return
1379         }
1380         if !build.config.target.iter().any(|s| s == "x86_64-unknown-linux-gnu") {
1381             return
1382         }
1383
1384         println!("Distcheck");
1385         let dir = build.out.join("tmp").join("distcheck");
1386         let _ = fs::remove_dir_all(&dir);
1387         t!(fs::create_dir_all(&dir));
1388
1389         let mut cmd = Command::new("tar");
1390         cmd.arg("-xzf")
1391            .arg(dist::rust_src_location(build))
1392            .arg("--strip-components=1")
1393            .current_dir(&dir);
1394         build.run(&mut cmd);
1395         build.run(Command::new("./configure")
1396                          .args(&build.config.configure_args)
1397                          .arg("--enable-vendor")
1398                          .current_dir(&dir));
1399         build.run(Command::new(build_helper::make(&build.build))
1400                          .arg("check")
1401                          .current_dir(&dir));
1402
1403         // Now make sure that rust-src has all of libstd's dependencies
1404         println!("Distcheck rust-src");
1405         let dir = build.out.join("tmp").join("distcheck-src");
1406         let _ = fs::remove_dir_all(&dir);
1407         t!(fs::create_dir_all(&dir));
1408
1409         let mut cmd = Command::new("tar");
1410         cmd.arg("-xzf")
1411            .arg(dist::rust_src_installer(build))
1412            .arg("--strip-components=1")
1413            .current_dir(&dir);
1414         build.run(&mut cmd);
1415
1416         let toml = dir.join("rust-src/lib/rustlib/src/rust/src/libstd/Cargo.toml");
1417         build.run(Command::new(&build.initial_cargo)
1418                          .arg("generate-lockfile")
1419                          .arg("--manifest-path")
1420                          .arg(&toml)
1421                          .current_dir(&dir));
1422     }
1423 }
1424
1425 //rules.test("check-bootstrap", "src/bootstrap")
1426 //     .default(true)
1427 //     .host(true)
1428 //     .only_build(true)
1429 //     .run(move |_| check::bootstrap(build));
1430
1431 #[derive(Serialize)]
1432 pub struct Bootstrap;
1433
1434 impl<'a> Step<'a> for Bootstrap {
1435     type Output = ();
1436     const DEFAULT: bool = true;
1437     const ONLY_HOSTS: bool = true;
1438     const ONLY_BUILD: bool = true;
1439
1440     /// Test the build system itself
1441     fn run(self, builder: &Builder) {
1442         let build = builder.build;
1443         let mut cmd = Command::new(&build.initial_cargo);
1444         cmd.arg("test")
1445            .current_dir(build.src.join("src/bootstrap"))
1446            .env("CARGO_TARGET_DIR", build.out.join("bootstrap"))
1447            .env("RUSTC_BOOTSTRAP", "1")
1448            .env("RUSTC", &build.initial_rustc);
1449         if !build.fail_fast {
1450             cmd.arg("--no-fail-fast");
1451         }
1452         cmd.arg("--").args(&build.flags.cmd.test_args());
1453         try_run(build, &mut cmd);
1454     }
1455
1456     fn should_run(_builder: &Builder, path: &Path) -> bool {
1457         path.ends_with("src/bootstrap")
1458     }
1459
1460     fn make_run(builder: &Builder, _path: Option<&Path>, _host: &str, _target: &str) {
1461         builder.ensure(Bootstrap);
1462     }
1463 }