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