]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/step.rs
Auto merge of #41577 - Keruspe:master, r=alexcrichton
[rust.git] / src / bootstrap / step.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 //! Definition of steps of the build system.
12 //!
13 //! This is where some of the real meat of rustbuild is located, in how we
14 //! define targets and the dependencies amongst them. This file can sort of be
15 //! viewed as just defining targets in a makefile which shell out to predefined
16 //! functions elsewhere about how to execute the target.
17 //!
18 //! The primary function here you're likely interested in is the `build_rules`
19 //! function. This will create a `Rules` structure which basically just lists
20 //! everything that rustbuild can do. Each rule has a human-readable name, a
21 //! path associated with it, some dependencies, and then a closure of how to
22 //! actually perform the rule.
23 //!
24 //! All steps below are defined in self-contained units, so adding a new target
25 //! to the build system should just involve adding the meta information here
26 //! along with the actual implementation elsewhere. You can find more comments
27 //! about how to define rules themselves below.
28
29 use std::collections::{BTreeMap, HashSet, HashMap};
30 use std::mem;
31
32 use check::{self, TestKind};
33 use compile;
34 use dist;
35 use doc;
36 use flags::Subcommand;
37 use install;
38 use native;
39 use {Compiler, Build, Mode};
40
41 pub fn run(build: &Build) {
42     let rules = build_rules(build);
43     let steps = rules.plan();
44     rules.run(&steps);
45 }
46
47 pub fn build_rules<'a>(build: &'a Build) -> Rules {
48     let mut rules = Rules::new(build);
49
50     // This is the first rule that we're going to define for rustbuild, which is
51     // used to compile LLVM itself. All rules are added through the `rules`
52     // structure created above and are configured through a builder-style
53     // interface.
54     //
55     // First up we see the `build` method. This represents a rule that's part of
56     // the top-level `build` subcommand. For example `./x.py build` is what this
57     // is associating with. Note that this is normally only relevant if you flag
58     // a rule as `default`, which we'll talk about later.
59     //
60     // Next up we'll see two arguments to this method:
61     //
62     // * `llvm` - this is the "human readable" name of this target. This name is
63     //            not accessed anywhere outside this file itself (e.g. not in
64     //            the CLI nor elsewhere in rustbuild). The purpose of this is to
65     //            easily define dependencies between rules. That is, other rules
66     //            will depend on this with the name "llvm".
67     // * `src/llvm` - this is the relevant path to the rule that we're working
68     //                with. This path is the engine behind how commands like
69     //                `./x.py build src/llvm` work. This should typically point
70     //                to the relevant component, but if there's not really a
71     //                path to be assigned here you can pass something like
72     //                `path/to/nowhere` to ignore it.
73     //
74     // After we create the rule with the `build` method we can then configure
75     // various aspects of it. For example this LLVM rule uses `.host(true)` to
76     // flag that it's a rule only for host targets. In other words, LLVM isn't
77     // compiled for targets configured through `--target` (e.g. those we're just
78     // building a standard library for).
79     //
80     // Next up the `dep` method will add a dependency to this rule. The closure
81     // is yielded the step that represents executing the `llvm` rule itself
82     // (containing information like stage, host, target, ...) and then it must
83     // return a target that the step depends on. Here LLVM is actually
84     // interesting where a cross-compiled LLVM depends on the host LLVM, but
85     // otherwise it has no dependencies.
86     //
87     // To handle this we do a bit of dynamic dispatch to see what the dependency
88     // is. If we're building a LLVM for the build triple, then we don't actually
89     // have any dependencies! To do that we return a dependency on the `Step::noop()`
90     // target which does nothing.
91     //
92     // If we're build a cross-compiled LLVM, however, we need to assemble the
93     // libraries from the previous compiler. This step has the same name as
94     // ours (llvm) but we want it for a different target, so we use the
95     // builder-style methods on `Step` to configure this target to the build
96     // triple.
97     //
98     // Finally, to finish off this rule, we define how to actually execute it.
99     // That logic is all defined in the `native` module so we just delegate to
100     // the relevant function there. The argument to the closure passed to `run`
101     // is a `Step` (defined below) which encapsulates information like the
102     // stage, target, host, etc.
103     rules.build("llvm", "src/llvm")
104          .host(true)
105          .dep(move |s| {
106              if s.target == build.config.build {
107                  Step::noop()
108              } else {
109                  s.target(&build.config.build)
110              }
111          })
112          .run(move |s| native::llvm(build, s.target));
113
114     // Ok! After that example rule  that's hopefully enough to explain what's
115     // going on here. You can check out the API docs below and also see a bunch
116     // more examples of rules directly below as well.
117
118     // the compiler with no target libraries ready to go
119     rules.build("rustc", "src/rustc")
120          .dep(|s| s.name("create-sysroot").target(s.host))
121          .dep(move |s| {
122              if s.stage == 0 {
123                  Step::noop()
124              } else {
125                  s.name("librustc")
126                   .host(&build.config.build)
127                   .stage(s.stage - 1)
128              }
129          })
130          .run(move |s| compile::assemble_rustc(build, s.stage, s.target));
131
132     // Helper for loading an entire DAG of crates, rooted at `name`
133     let krates = |name: &str| {
134         let mut ret = Vec::new();
135         let mut list = vec![name];
136         let mut visited = HashSet::new();
137         while let Some(krate) = list.pop() {
138             let default = krate == name;
139             let krate = &build.crates[krate];
140             let path = krate.path.strip_prefix(&build.src)
141                 // This handles out of tree paths
142                 .unwrap_or(&krate.path);
143             ret.push((krate, path.to_str().unwrap(), default));
144             for dep in krate.deps.iter() {
145                 if visited.insert(dep) && dep != "build_helper" {
146                     list.push(dep);
147                 }
148             }
149         }
150         return ret
151     };
152
153     // ========================================================================
154     // Crate compilations
155     //
156     // Tools used during the build system but not shipped
157     rules.build("create-sysroot", "path/to/nowhere")
158          .run(move |s| compile::create_sysroot(build, &s.compiler()));
159
160     // These rules are "pseudo rules" that don't actually do any work
161     // themselves, but represent a complete sysroot with the relevant compiler
162     // linked into place.
163     //
164     // That is, depending on "libstd" means that when the rule is completed then
165     // the `stage` sysroot for the compiler `host` will be available with a
166     // standard library built for `target` linked in place. Not all rules need
167     // the compiler itself to be available, just the standard library, so
168     // there's a distinction between the two.
169     rules.build("libstd", "src/libstd")
170          .dep(|s| s.name("rustc").target(s.host))
171          .dep(|s| s.name("libstd-link"));
172     rules.build("libtest", "src/libtest")
173          .dep(|s| s.name("libstd"))
174          .dep(|s| s.name("libtest-link"))
175          .default(true);
176     rules.build("librustc", "src/librustc")
177          .dep(|s| s.name("libtest"))
178          .dep(|s| s.name("librustc-link"))
179          .host(true)
180          .default(true);
181
182     // Helper method to define the rules to link a crate into its place in the
183     // sysroot.
184     //
185     // The logic here is a little subtle as there's a few cases to consider.
186     // Not all combinations of (stage, host, target) actually require something
187     // to be compiled, but rather libraries could get propagated from a
188     // different location. For example:
189     //
190     // * Any crate with a `host` that's not the build triple will not actually
191     //   compile something. A different `host` means that the build triple will
192     //   actually compile the libraries, and then we'll copy them over from the
193     //   build triple to the `host` directory.
194     //
195     // * Some crates aren't even compiled by the build triple, but may be copied
196     //   from previous stages. For example if we're not doing a full bootstrap
197     //   then we may just depend on the stage1 versions of libraries to be
198     //   available to get linked forward.
199     //
200     // * Finally, there are some cases, however, which do indeed comiple crates
201     //   and link them into place afterwards.
202     //
203     // The rule definition below mirrors these three cases. The `dep` method
204     // calculates the correct dependency which either comes from stage1, a
205     // different compiler, or from actually building the crate itself (the `dep`
206     // rule). The `run` rule then mirrors these three cases and links the cases
207     // forward into the compiler sysroot specified from the correct location.
208     fn crate_rule<'a, 'b>(build: &'a Build,
209                           rules: &'b mut Rules<'a>,
210                           krate: &'a str,
211                           dep: &'a str,
212                           link: fn(&Build, &Compiler, &Compiler, &str))
213                           -> RuleBuilder<'a, 'b> {
214         let mut rule = rules.build(&krate, "path/to/nowhere");
215         rule.dep(move |s| {
216                 if build.force_use_stage1(&s.compiler(), s.target) {
217                     s.host(&build.config.build).stage(1)
218                 } else if s.host == build.config.build {
219                     s.name(dep)
220                 } else {
221                     s.host(&build.config.build)
222                 }
223             })
224             .run(move |s| {
225                 if build.force_use_stage1(&s.compiler(), s.target) {
226                     link(build,
227                          &s.stage(1).host(&build.config.build).compiler(),
228                          &s.compiler(),
229                          s.target)
230                 } else if s.host == build.config.build {
231                     link(build, &s.compiler(), &s.compiler(), s.target)
232                 } else {
233                     link(build,
234                          &s.host(&build.config.build).compiler(),
235                          &s.compiler(),
236                          s.target)
237                 }
238             });
239             return rule
240     }
241
242     // Similar to the `libstd`, `libtest`, and `librustc` rules above, except
243     // these rules only represent the libraries being available in the sysroot,
244     // not the compiler itself. This is done as not all rules need a compiler in
245     // the sysroot, but may just need the libraries.
246     //
247     // All of these rules use the helper definition above.
248     crate_rule(build,
249                &mut rules,
250                "libstd-link",
251                "build-crate-std",
252                compile::std_link)
253         .dep(|s| s.name("startup-objects"))
254         .dep(|s| s.name("create-sysroot").target(s.host));
255     crate_rule(build,
256                &mut rules,
257                "libtest-link",
258                "build-crate-test",
259                compile::test_link)
260         .dep(|s| s.name("libstd-link"));
261     crate_rule(build,
262                &mut rules,
263                "librustc-link",
264                "build-crate-rustc-main",
265                compile::rustc_link)
266         .dep(|s| s.name("libtest-link"));
267
268     for (krate, path, _default) in krates("std") {
269         rules.build(&krate.build_step, path)
270              .dep(|s| s.name("startup-objects"))
271              .dep(move |s| s.name("rustc").host(&build.config.build).target(s.host))
272              .run(move |s| compile::std(build, s.target, &s.compiler()));
273     }
274     for (krate, path, _default) in krates("test") {
275         rules.build(&krate.build_step, path)
276              .dep(|s| s.name("libstd-link"))
277              .run(move |s| compile::test(build, s.target, &s.compiler()));
278     }
279     for (krate, path, _default) in krates("rustc-main") {
280         rules.build(&krate.build_step, path)
281              .dep(|s| s.name("libtest-link"))
282              .dep(move |s| s.name("llvm").host(&build.config.build).stage(0))
283              .dep(|s| s.name("may-run-build-script"))
284              .run(move |s| compile::rustc(build, s.target, &s.compiler()));
285     }
286
287     // Crates which have build scripts need to rely on this rule to ensure that
288     // the necessary prerequisites for a build script are linked and located in
289     // place.
290     rules.build("may-run-build-script", "path/to/nowhere")
291          .dep(move |s| {
292              s.name("libstd-link")
293               .host(&build.config.build)
294               .target(&build.config.build)
295          });
296     rules.build("startup-objects", "src/rtstartup")
297          .dep(|s| s.name("create-sysroot").target(s.host))
298          .run(move |s| compile::build_startup_objects(build, &s.compiler(), s.target));
299
300     // ========================================================================
301     // Test targets
302     //
303     // Various unit tests and tests suites we can run
304     {
305         let mut suite = |name, path, mode, dir| {
306             rules.test(name, path)
307                  .dep(|s| s.name("libtest"))
308                  .dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
309                  .dep(|s| s.name("test-helpers"))
310                  .dep(|s| s.name("emulator-copy-libs"))
311                  .default(mode != "pretty") // pretty tests don't run everywhere
312                  .run(move |s| {
313                      check::compiletest(build, &s.compiler(), s.target, mode, dir)
314                  });
315         };
316
317         suite("check-ui", "src/test/ui", "ui", "ui");
318         suite("check-rpass", "src/test/run-pass", "run-pass", "run-pass");
319         suite("check-cfail", "src/test/compile-fail", "compile-fail", "compile-fail");
320         suite("check-pfail", "src/test/parse-fail", "parse-fail", "parse-fail");
321         suite("check-rfail", "src/test/run-fail", "run-fail", "run-fail");
322         suite("check-rpass-valgrind", "src/test/run-pass-valgrind",
323               "run-pass-valgrind", "run-pass-valgrind");
324         suite("check-mir-opt", "src/test/mir-opt", "mir-opt", "mir-opt");
325         if build.config.codegen_tests {
326             suite("check-codegen", "src/test/codegen", "codegen", "codegen");
327         }
328         suite("check-codegen-units", "src/test/codegen-units", "codegen-units",
329               "codegen-units");
330         suite("check-incremental", "src/test/incremental", "incremental",
331               "incremental");
332     }
333
334     if build.config.build.contains("msvc") {
335         // nothing to do for debuginfo tests
336     } else {
337         rules.test("check-debuginfo-lldb", "src/test/debuginfo-lldb")
338              .dep(|s| s.name("libtest"))
339              .dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
340              .dep(|s| s.name("test-helpers"))
341              .dep(|s| s.name("debugger-scripts"))
342              .run(move |s| check::compiletest(build, &s.compiler(), s.target,
343                                          "debuginfo-lldb", "debuginfo"));
344         rules.test("check-debuginfo-gdb", "src/test/debuginfo-gdb")
345              .dep(|s| s.name("libtest"))
346              .dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
347              .dep(|s| s.name("test-helpers"))
348              .dep(|s| s.name("debugger-scripts"))
349              .dep(|s| s.name("emulator-copy-libs"))
350              .run(move |s| check::compiletest(build, &s.compiler(), s.target,
351                                          "debuginfo-gdb", "debuginfo"));
352         let mut rule = rules.test("check-debuginfo", "src/test/debuginfo");
353         rule.default(true);
354         if build.config.build.contains("apple") {
355             rule.dep(|s| s.name("check-debuginfo-lldb"));
356         } else {
357             rule.dep(|s| s.name("check-debuginfo-gdb"));
358         }
359     }
360
361     rules.test("debugger-scripts", "src/etc/lldb_batchmode.py")
362          .run(move |s| dist::debugger_scripts(build, &build.sysroot(&s.compiler()),
363                                          s.target));
364
365     {
366         let mut suite = |name, path, mode, dir| {
367             rules.test(name, path)
368                  .dep(|s| s.name("librustc"))
369                  .dep(|s| s.name("test-helpers"))
370                  .dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
371                  .default(mode != "pretty")
372                  .host(true)
373                  .run(move |s| {
374                      check::compiletest(build, &s.compiler(), s.target, mode, dir)
375                  });
376         };
377
378         suite("check-ui-full", "src/test/ui-fulldeps", "ui", "ui-fulldeps");
379         suite("check-rpass-full", "src/test/run-pass-fulldeps",
380               "run-pass", "run-pass-fulldeps");
381         suite("check-rfail-full", "src/test/run-fail-fulldeps",
382               "run-fail", "run-fail-fulldeps");
383         suite("check-cfail-full", "src/test/compile-fail-fulldeps",
384               "compile-fail", "compile-fail-fulldeps");
385         suite("check-rmake", "src/test/run-make", "run-make", "run-make");
386         suite("check-rustdoc", "src/test/rustdoc", "rustdoc", "rustdoc");
387         suite("check-pretty", "src/test/pretty", "pretty", "pretty");
388         suite("check-pretty-rpass", "src/test/run-pass/pretty", "pretty",
389               "run-pass");
390         suite("check-pretty-rfail", "src/test/run-fail/pretty", "pretty",
391               "run-fail");
392         suite("check-pretty-valgrind", "src/test/run-pass-valgrind/pretty", "pretty",
393               "run-pass-valgrind");
394         suite("check-pretty-rpass-full", "src/test/run-pass-fulldeps/pretty",
395               "pretty", "run-pass-fulldeps");
396         suite("check-pretty-rfail-full", "src/test/run-fail-fulldeps/pretty",
397               "pretty", "run-fail-fulldeps");
398     }
399
400     for (krate, path, _default) in krates("std") {
401         rules.test(&krate.test_step, path)
402              .dep(|s| s.name("libtest"))
403              .dep(|s| s.name("emulator-copy-libs"))
404              .run(move |s| check::krate(build, &s.compiler(), s.target,
405                                         Mode::Libstd, TestKind::Test,
406                                         Some(&krate.name)));
407     }
408     rules.test("check-std-all", "path/to/nowhere")
409          .dep(|s| s.name("libtest"))
410          .dep(|s| s.name("emulator-copy-libs"))
411          .default(true)
412          .run(move |s| check::krate(build, &s.compiler(), s.target,
413                                     Mode::Libstd, TestKind::Test, None));
414
415     // std benchmarks
416     for (krate, path, _default) in krates("std") {
417         rules.bench(&krate.bench_step, path)
418              .dep(|s| s.name("libtest"))
419              .dep(|s| s.name("emulator-copy-libs"))
420              .run(move |s| check::krate(build, &s.compiler(), s.target,
421                                         Mode::Libstd, TestKind::Bench,
422                                         Some(&krate.name)));
423     }
424     rules.bench("bench-std-all", "path/to/nowhere")
425          .dep(|s| s.name("libtest"))
426          .dep(|s| s.name("emulator-copy-libs"))
427          .default(true)
428          .run(move |s| check::krate(build, &s.compiler(), s.target,
429                                     Mode::Libstd, TestKind::Bench, None));
430
431     for (krate, path, _default) in krates("test") {
432         rules.test(&krate.test_step, path)
433              .dep(|s| s.name("libtest"))
434              .dep(|s| s.name("emulator-copy-libs"))
435              .run(move |s| check::krate(build, &s.compiler(), s.target,
436                                         Mode::Libtest, TestKind::Test,
437                                         Some(&krate.name)));
438     }
439     rules.test("check-test-all", "path/to/nowhere")
440          .dep(|s| s.name("libtest"))
441          .dep(|s| s.name("emulator-copy-libs"))
442          .default(true)
443          .run(move |s| check::krate(build, &s.compiler(), s.target,
444                                     Mode::Libtest, TestKind::Test, None));
445     for (krate, path, _default) in krates("rustc-main") {
446         rules.test(&krate.test_step, path)
447              .dep(|s| s.name("librustc"))
448              .dep(|s| s.name("emulator-copy-libs"))
449              .host(true)
450              .run(move |s| check::krate(build, &s.compiler(), s.target,
451                                         Mode::Librustc, TestKind::Test,
452                                         Some(&krate.name)));
453     }
454     rules.test("check-rustc-all", "path/to/nowhere")
455          .dep(|s| s.name("librustc"))
456          .dep(|s| s.name("emulator-copy-libs"))
457          .default(true)
458          .host(true)
459          .run(move |s| check::krate(build, &s.compiler(), s.target,
460                                     Mode::Librustc, TestKind::Test, None));
461
462     rules.test("check-linkchecker", "src/tools/linkchecker")
463          .dep(|s| s.name("tool-linkchecker").stage(0))
464          .dep(|s| s.name("default:doc"))
465          .default(true)
466          .host(true)
467          .run(move |s| check::linkcheck(build, s.target));
468     rules.test("check-cargotest", "src/tools/cargotest")
469          .dep(|s| s.name("tool-cargotest").stage(0))
470          .dep(|s| s.name("librustc"))
471          .host(true)
472          .run(move |s| check::cargotest(build, s.stage, s.target));
473     rules.test("check-cargo", "cargo")
474          .dep(|s| s.name("tool-cargo"))
475          .host(true)
476          .run(move |s| check::cargo(build, s.stage, s.target));
477     rules.test("check-tidy", "src/tools/tidy")
478          .dep(|s| s.name("tool-tidy").stage(0))
479          .default(true)
480          .host(true)
481          .only_build(true)
482          .run(move |s| check::tidy(build, s.target));
483     rules.test("check-error-index", "src/tools/error_index_generator")
484          .dep(|s| s.name("libstd"))
485          .dep(|s| s.name("tool-error-index").host(s.host).stage(0))
486          .default(true)
487          .host(true)
488          .run(move |s| check::error_index(build, &s.compiler()));
489     rules.test("check-docs", "src/doc")
490          .dep(|s| s.name("libtest"))
491          .default(true)
492          .host(true)
493          .run(move |s| check::docs(build, &s.compiler()));
494     rules.test("check-distcheck", "distcheck")
495          .dep(|s| s.name("dist-src"))
496          .run(move |_| check::distcheck(build));
497
498     rules.build("test-helpers", "src/rt/rust_test_helpers.c")
499          .run(move |s| native::test_helpers(build, s.target));
500     rules.build("openssl", "path/to/nowhere")
501          .run(move |s| native::openssl(build, s.target));
502
503     // Some test suites are run inside emulators, and most of our test binaries
504     // are linked dynamically which means we need to ship the standard library
505     // and such to the emulator ahead of time. This step represents this and is
506     // a dependency of all test suites.
507     //
508     // Most of the time this step is a noop (the `check::emulator_copy_libs`
509     // only does work if necessary). For some steps such as shipping data to
510     // QEMU we have to build our own tools so we've got conditional dependencies
511     // on those programs as well. Note that the QEMU client is built for the
512     // build target (us) and the server is built for the target.
513     rules.test("emulator-copy-libs", "path/to/nowhere")
514          .dep(|s| s.name("libtest"))
515          .dep(move |s| {
516              if build.remote_tested(s.target) {
517                 s.name("tool-remote-test-client").target(s.host).stage(0)
518              } else {
519                  Step::noop()
520              }
521          })
522          .dep(move |s| {
523              if build.remote_tested(s.target) {
524                 s.name("tool-remote-test-server")
525              } else {
526                  Step::noop()
527              }
528          })
529          .run(move |s| check::emulator_copy_libs(build, &s.compiler(), s.target));
530
531     rules.test("check-bootstrap", "src/bootstrap")
532          .default(true)
533          .host(true)
534          .only_build(true)
535          .run(move |_| check::bootstrap(build));
536
537     // ========================================================================
538     // Build tools
539     //
540     // Tools used during the build system but not shipped
541     rules.build("tool-rustbook", "src/tools/rustbook")
542          .dep(|s| s.name("maybe-clean-tools"))
543          .dep(|s| s.name("librustc-tool"))
544          .run(move |s| compile::tool(build, s.stage, s.target, "rustbook"));
545     rules.build("tool-error-index", "src/tools/error_index_generator")
546          .dep(|s| s.name("maybe-clean-tools"))
547          .dep(|s| s.name("librustc-tool"))
548          .run(move |s| compile::tool(build, s.stage, s.target, "error_index_generator"));
549     rules.build("tool-tidy", "src/tools/tidy")
550          .dep(|s| s.name("maybe-clean-tools"))
551          .dep(|s| s.name("libstd-tool"))
552          .run(move |s| compile::tool(build, s.stage, s.target, "tidy"));
553     rules.build("tool-linkchecker", "src/tools/linkchecker")
554          .dep(|s| s.name("maybe-clean-tools"))
555          .dep(|s| s.name("libstd-tool"))
556          .run(move |s| compile::tool(build, s.stage, s.target, "linkchecker"));
557     rules.build("tool-cargotest", "src/tools/cargotest")
558          .dep(|s| s.name("maybe-clean-tools"))
559          .dep(|s| s.name("libstd-tool"))
560          .run(move |s| compile::tool(build, s.stage, s.target, "cargotest"));
561     rules.build("tool-compiletest", "src/tools/compiletest")
562          .dep(|s| s.name("maybe-clean-tools"))
563          .dep(|s| s.name("libtest-tool"))
564          .run(move |s| compile::tool(build, s.stage, s.target, "compiletest"));
565     rules.build("tool-build-manifest", "src/tools/build-manifest")
566          .dep(|s| s.name("maybe-clean-tools"))
567          .dep(|s| s.name("libstd-tool"))
568          .run(move |s| compile::tool(build, s.stage, s.target, "build-manifest"));
569     rules.build("tool-remote-test-server", "src/tools/remote-test-server")
570          .dep(|s| s.name("maybe-clean-tools"))
571          .dep(|s| s.name("libstd-tool"))
572          .run(move |s| compile::tool(build, s.stage, s.target, "remote-test-server"));
573     rules.build("tool-remote-test-client", "src/tools/remote-test-client")
574          .dep(|s| s.name("maybe-clean-tools"))
575          .dep(|s| s.name("libstd-tool"))
576          .run(move |s| compile::tool(build, s.stage, s.target, "remote-test-client"));
577     rules.build("tool-cargo", "cargo")
578          .host(true)
579          .default(build.config.extended)
580          .dep(|s| s.name("maybe-clean-tools"))
581          .dep(|s| s.name("libstd-tool"))
582          .dep(|s| s.stage(0).host(s.target).name("openssl"))
583          .dep(move |s| {
584              // Cargo depends on procedural macros, which requires a full host
585              // compiler to be available, so we need to depend on that.
586              s.name("librustc-link")
587               .target(&build.config.build)
588               .host(&build.config.build)
589          })
590          .run(move |s| compile::tool(build, s.stage, s.target, "cargo"));
591     rules.build("tool-rls", "rls")
592          .host(true)
593          .default(build.config.extended)
594          .dep(|s| s.name("librustc-tool"))
595          .dep(|s| s.stage(0).host(s.target).name("openssl"))
596          .dep(move |s| {
597              // rls, like cargo, uses procedural macros
598              s.name("librustc-link")
599               .target(&build.config.build)
600               .host(&build.config.build)
601          })
602          .run(move |s| compile::tool(build, s.stage, s.target, "rls"));
603
604     // "pseudo rule" which represents completely cleaning out the tools dir in
605     // one stage. This needs to happen whenever a dependency changes (e.g.
606     // libstd, libtest, librustc) and all of the tool compilations above will
607     // be sequenced after this rule.
608     rules.build("maybe-clean-tools", "path/to/nowhere")
609          .after("librustc-tool")
610          .after("libtest-tool")
611          .after("libstd-tool");
612
613     rules.build("librustc-tool", "path/to/nowhere")
614          .dep(|s| s.name("librustc"))
615          .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Librustc));
616     rules.build("libtest-tool", "path/to/nowhere")
617          .dep(|s| s.name("libtest"))
618          .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Libtest));
619     rules.build("libstd-tool", "path/to/nowhere")
620          .dep(|s| s.name("libstd"))
621          .run(move |s| compile::maybe_clean_tools(build, s.stage, s.target, Mode::Libstd));
622
623     // ========================================================================
624     // Documentation targets
625     rules.doc("doc-book", "src/doc/book")
626          .dep(move |s| {
627              s.name("tool-rustbook")
628               .host(&build.config.build)
629               .target(&build.config.build)
630               .stage(0)
631          })
632          .default(build.config.docs)
633          .run(move |s| doc::book(build, s.target, "book"));
634     rules.doc("doc-nomicon", "src/doc/nomicon")
635          .dep(move |s| {
636              s.name("tool-rustbook")
637               .host(&build.config.build)
638               .target(&build.config.build)
639               .stage(0)
640          })
641          .default(build.config.docs)
642          .run(move |s| doc::rustbook(build, s.target, "nomicon"));
643     rules.doc("doc-reference", "src/doc/reference")
644          .dep(move |s| {
645              s.name("tool-rustbook")
646               .host(&build.config.build)
647               .target(&build.config.build)
648               .stage(0)
649          })
650          .default(build.config.docs)
651          .run(move |s| doc::rustbook(build, s.target, "reference"));
652     rules.doc("doc-unstable-book", "src/doc/unstable-book")
653          .dep(move |s| {
654              s.name("tool-rustbook")
655               .host(&build.config.build)
656               .target(&build.config.build)
657               .stage(0)
658          })
659          .default(build.config.docs)
660          .run(move |s| doc::rustbook(build, s.target, "unstable-book"));
661     rules.doc("doc-standalone", "src/doc")
662          .dep(move |s| {
663              s.name("rustc")
664               .host(&build.config.build)
665               .target(&build.config.build)
666               .stage(0)
667          })
668          .default(build.config.docs)
669          .run(move |s| doc::standalone(build, s.target));
670     rules.doc("doc-error-index", "src/tools/error_index_generator")
671          .dep(move |s| s.name("tool-error-index").target(&build.config.build).stage(0))
672          .dep(move |s| s.name("librustc-link"))
673          .default(build.config.docs)
674          .host(true)
675          .run(move |s| doc::error_index(build, s.target));
676     for (krate, path, default) in krates("std") {
677         rules.doc(&krate.doc_step, path)
678              .dep(|s| s.name("libstd-link"))
679              .default(default && build.config.docs)
680              .run(move |s| doc::std(build, s.stage, s.target));
681     }
682     for (krate, path, default) in krates("test") {
683         rules.doc(&krate.doc_step, path)
684              .dep(|s| s.name("libtest-link"))
685              // Needed so rustdoc generates relative links to std.
686              .dep(|s| s.name("doc-crate-std"))
687              .default(default && build.config.compiler_docs)
688              .run(move |s| doc::test(build, s.stage, s.target));
689     }
690     for (krate, path, default) in krates("rustc-main") {
691         rules.doc(&krate.doc_step, path)
692              .dep(|s| s.name("librustc-link"))
693              // Needed so rustdoc generates relative links to std.
694              .dep(|s| s.name("doc-crate-std"))
695              .host(true)
696              .default(default && build.config.docs)
697              .run(move |s| doc::rustc(build, s.stage, s.target));
698     }
699
700     // ========================================================================
701     // Distribution targets
702     rules.dist("dist-rustc", "src/librustc")
703          .dep(move |s| s.name("rustc").host(&build.config.build))
704          .host(true)
705          .only_host_build(true)
706          .default(true)
707          .run(move |s| dist::rustc(build, s.stage, s.target));
708     rules.dist("dist-std", "src/libstd")
709          .dep(move |s| {
710              // We want to package up as many target libraries as possible
711              // for the `rust-std` package, so if this is a host target we
712              // depend on librustc and otherwise we just depend on libtest.
713              if build.config.host.iter().any(|t| t == s.target) {
714                  s.name("librustc-link")
715              } else {
716                  s.name("libtest-link")
717              }
718          })
719          .default(true)
720          .only_host_build(true)
721          .run(move |s| dist::std(build, &s.compiler(), s.target));
722     rules.dist("dist-mingw", "path/to/nowhere")
723          .default(true)
724          .only_host_build(true)
725          .run(move |s| {
726              if s.target.contains("pc-windows-gnu") {
727                  dist::mingw(build, s.target)
728              }
729          });
730     rules.dist("dist-src", "src")
731          .default(true)
732          .host(true)
733          .only_build(true)
734          .only_host_build(true)
735          .run(move |_| dist::rust_src(build));
736     rules.dist("dist-docs", "src/doc")
737          .default(true)
738          .only_host_build(true)
739          .dep(|s| s.name("default:doc"))
740          .run(move |s| dist::docs(build, s.stage, s.target));
741     rules.dist("dist-analysis", "analysis")
742          .default(build.config.extended)
743          .dep(|s| s.name("dist-std"))
744          .only_host_build(true)
745          .run(move |s| dist::analysis(build, &s.compiler(), s.target));
746     rules.dist("dist-rls", "rls")
747          .host(true)
748          .only_host_build(true)
749          .dep(|s| s.name("tool-rls"))
750          .run(move |s| dist::rls(build, s.stage, s.target));
751     rules.dist("install", "path/to/nowhere")
752          .dep(|s| s.name("default:dist"))
753          .run(move |s| install::install(build, s.stage, s.target));
754     rules.dist("dist-cargo", "cargo")
755          .host(true)
756          .only_host_build(true)
757          .dep(|s| s.name("tool-cargo"))
758          .run(move |s| dist::cargo(build, s.stage, s.target));
759     rules.dist("dist-extended", "extended")
760          .default(build.config.extended)
761          .host(true)
762          .only_host_build(true)
763          .dep(|d| d.name("dist-std"))
764          .dep(|d| d.name("dist-rustc"))
765          .dep(|d| d.name("dist-mingw"))
766          .dep(|d| d.name("dist-docs"))
767          .dep(|d| d.name("dist-cargo"))
768          .dep(|d| d.name("dist-rls"))
769          .dep(|d| d.name("dist-analysis"))
770          .run(move |s| dist::extended(build, s.stage, s.target));
771
772     rules.dist("dist-sign", "hash-and-sign")
773          .host(true)
774          .only_build(true)
775          .only_host_build(true)
776          .dep(move |s| s.name("tool-build-manifest").target(&build.config.build).stage(0))
777          .run(move |_| dist::hash_and_sign(build));
778
779     rules.verify();
780     return rules;
781 }
782
783 #[derive(PartialEq, Eq, Hash, Clone, Debug)]
784 struct Step<'a> {
785     /// Human readable name of the rule this step is executing. Possible names
786     /// are all defined above in `build_rules`.
787     name: &'a str,
788
789     /// The stage this step is executing in. This is typically 0, 1, or 2.
790     stage: u32,
791
792     /// This step will likely involve a compiler, and the target that compiler
793     /// itself is built for is called the host, this variable. Typically this is
794     /// the target of the build machine itself.
795     host: &'a str,
796
797     /// The target that this step represents generating. If you're building a
798     /// standard library for a new suite of targets, for example, this'll be set
799     /// to those targets.
800     target: &'a str,
801 }
802
803 impl<'a> Step<'a> {
804     fn noop() -> Step<'a> {
805         Step { name: "", stage: 0, host: "", target: "" }
806     }
807
808     /// Creates a new step which is the same as this, except has a new name.
809     fn name(&self, name: &'a str) -> Step<'a> {
810         Step { name: name, ..*self }
811     }
812
813     /// Creates a new step which is the same as this, except has a new stage.
814     fn stage(&self, stage: u32) -> Step<'a> {
815         Step { stage: stage, ..*self }
816     }
817
818     /// Creates a new step which is the same as this, except has a new host.
819     fn host(&self, host: &'a str) -> Step<'a> {
820         Step { host: host, ..*self }
821     }
822
823     /// Creates a new step which is the same as this, except has a new target.
824     fn target(&self, target: &'a str) -> Step<'a> {
825         Step { target: target, ..*self }
826     }
827
828     /// Returns the `Compiler` structure that this step corresponds to.
829     fn compiler(&self) -> Compiler<'a> {
830         Compiler::new(self.stage, self.host)
831     }
832 }
833
834 struct Rule<'a> {
835     /// The human readable name of this target, defined in `build_rules`.
836     name: &'a str,
837
838     /// The path associated with this target, used in the `./x.py` driver for
839     /// easy and ergonomic specification of what to do.
840     path: &'a str,
841
842     /// The "kind" of top-level command that this rule is associated with, only
843     /// relevant if this is a default rule.
844     kind: Kind,
845
846     /// List of dependencies this rule has. Each dependency is a function from a
847     /// step that's being executed to another step that should be executed.
848     deps: Vec<Box<Fn(&Step<'a>) -> Step<'a> + 'a>>,
849
850     /// How to actually execute this rule. Takes a step with contextual
851     /// information and then executes it.
852     run: Box<Fn(&Step<'a>) + 'a>,
853
854     /// Whether or not this is a "default" rule. That basically means that if
855     /// you run, for example, `./x.py test` whether it's included or not.
856     default: bool,
857
858     /// Whether or not this is a "host" rule, or in other words whether this is
859     /// only intended for compiler hosts and not for targets that are being
860     /// generated.
861     host: bool,
862
863     /// Whether this rule is only for steps where the host is the build triple,
864     /// not anything in hosts or targets.
865     only_host_build: bool,
866
867     /// Whether this rule is only for the build triple, not anything in hosts or
868     /// targets.
869     only_build: bool,
870
871     /// A list of "order only" dependencies. This rules does not actually
872     /// depend on these rules, but if they show up in the dependency graph then
873     /// this rule must be executed after all these rules.
874     after: Vec<&'a str>,
875 }
876
877 #[derive(PartialEq)]
878 enum Kind {
879     Build,
880     Test,
881     Bench,
882     Dist,
883     Doc,
884 }
885
886 impl<'a> Rule<'a> {
887     fn new(name: &'a str, path: &'a str, kind: Kind) -> Rule<'a> {
888         Rule {
889             name: name,
890             deps: Vec::new(),
891             run: Box::new(|_| ()),
892             path: path,
893             kind: kind,
894             default: false,
895             host: false,
896             only_host_build: false,
897             only_build: false,
898             after: Vec::new(),
899         }
900     }
901 }
902
903 /// Builder pattern returned from the various methods on `Rules` which will add
904 /// the rule to the internal list on `Drop`.
905 struct RuleBuilder<'a: 'b, 'b> {
906     rules: &'b mut Rules<'a>,
907     rule: Rule<'a>,
908 }
909
910 impl<'a, 'b> RuleBuilder<'a, 'b> {
911     fn dep<F>(&mut self, f: F) -> &mut Self
912         where F: Fn(&Step<'a>) -> Step<'a> + 'a,
913     {
914         self.rule.deps.push(Box::new(f));
915         self
916     }
917
918     fn after(&mut self, step: &'a str) -> &mut Self {
919         self.rule.after.push(step);
920         self
921     }
922
923     fn run<F>(&mut self, f: F) -> &mut Self
924         where F: Fn(&Step<'a>) + 'a,
925     {
926         self.rule.run = Box::new(f);
927         self
928     }
929
930     fn default(&mut self, default: bool) -> &mut Self {
931         self.rule.default = default;
932         self
933     }
934
935     fn host(&mut self, host: bool) -> &mut Self {
936         self.rule.host = host;
937         self
938     }
939
940     fn only_build(&mut self, only_build: bool) -> &mut Self {
941         self.rule.only_build = only_build;
942         self
943     }
944
945     fn only_host_build(&mut self, only_host_build: bool) -> &mut Self {
946         self.rule.only_host_build = only_host_build;
947         self
948     }
949 }
950
951 impl<'a, 'b> Drop for RuleBuilder<'a, 'b> {
952     fn drop(&mut self) {
953         let rule = mem::replace(&mut self.rule, Rule::new("", "", Kind::Build));
954         let prev = self.rules.rules.insert(rule.name, rule);
955         if let Some(prev) = prev {
956             panic!("duplicate rule named: {}", prev.name);
957         }
958     }
959 }
960
961 pub struct Rules<'a> {
962     build: &'a Build,
963     sbuild: Step<'a>,
964     rules: BTreeMap<&'a str, Rule<'a>>,
965 }
966
967 impl<'a> Rules<'a> {
968     fn new(build: &'a Build) -> Rules<'a> {
969         Rules {
970             build: build,
971             sbuild: Step {
972                 stage: build.flags.stage.unwrap_or(2),
973                 target: &build.config.build,
974                 host: &build.config.build,
975                 name: "",
976             },
977             rules: BTreeMap::new(),
978         }
979     }
980
981     /// Creates a new rule of `Kind::Build` with the specified human readable
982     /// name and path associated with it.
983     ///
984     /// The builder returned should be configured further with information such
985     /// as how to actually run this rule.
986     fn build<'b>(&'b mut self, name: &'a str, path: &'a str)
987                  -> RuleBuilder<'a, 'b> {
988         self.rule(name, path, Kind::Build)
989     }
990
991     /// Same as `build`, but for `Kind::Test`.
992     fn test<'b>(&'b mut self, name: &'a str, path: &'a str)
993                 -> RuleBuilder<'a, 'b> {
994         self.rule(name, path, Kind::Test)
995     }
996
997     /// Same as `build`, but for `Kind::Bench`.
998     fn bench<'b>(&'b mut self, name: &'a str, path: &'a str)
999                 -> RuleBuilder<'a, 'b> {
1000         self.rule(name, path, Kind::Bench)
1001     }
1002
1003     /// Same as `build`, but for `Kind::Doc`.
1004     fn doc<'b>(&'b mut self, name: &'a str, path: &'a str)
1005                -> RuleBuilder<'a, 'b> {
1006         self.rule(name, path, Kind::Doc)
1007     }
1008
1009     /// Same as `build`, but for `Kind::Dist`.
1010     fn dist<'b>(&'b mut self, name: &'a str, path: &'a str)
1011                 -> RuleBuilder<'a, 'b> {
1012         self.rule(name, path, Kind::Dist)
1013     }
1014
1015     fn rule<'b>(&'b mut self,
1016                 name: &'a str,
1017                 path: &'a str,
1018                 kind: Kind) -> RuleBuilder<'a, 'b> {
1019         RuleBuilder {
1020             rules: self,
1021             rule: Rule::new(name, path, kind),
1022         }
1023     }
1024
1025     /// Verify the dependency graph defined by all our rules are correct, e.g.
1026     /// everything points to a valid something else.
1027     fn verify(&self) {
1028         for rule in self.rules.values() {
1029             for dep in rule.deps.iter() {
1030                 let dep = dep(&self.sbuild.name(rule.name));
1031                 if self.rules.contains_key(&dep.name) || dep.name.starts_with("default:") {
1032                     continue
1033                 }
1034                 if dep == Step::noop() {
1035                     continue
1036                 }
1037                 panic!("\
1038
1039 invalid rule dependency graph detected, was a rule added and maybe typo'd?
1040
1041     `{}` depends on `{}` which does not exist
1042
1043 ", rule.name, dep.name);
1044             }
1045         }
1046     }
1047
1048     pub fn get_help(&self, command: &str) -> Option<String> {
1049         let kind = match command {
1050             "build" => Kind::Build,
1051             "doc" => Kind::Doc,
1052             "test" => Kind::Test,
1053             "bench" => Kind::Bench,
1054             "dist" => Kind::Dist,
1055             _ => return None,
1056         };
1057         let rules = self.rules.values().filter(|r| r.kind == kind);
1058         let rules = rules.filter(|r| !r.path.contains("nowhere"));
1059         let mut rules = rules.collect::<Vec<_>>();
1060         rules.sort_by_key(|r| r.path);
1061
1062         let mut help_string = String::from("Available paths:\n");
1063         for rule in rules {
1064             help_string.push_str(format!("    ./x.py {} {}\n", command, rule.path).as_str());
1065         }
1066         Some(help_string)
1067     }
1068
1069     /// Construct the top-level build steps that we're going to be executing,
1070     /// given the subcommand that our build is performing.
1071     fn plan(&self) -> Vec<Step<'a>> {
1072         // Ok, the logic here is pretty subtle, and involves quite a few
1073         // conditionals. The basic idea here is to:
1074         //
1075         // 1. First, filter all our rules to the relevant ones. This means that
1076         //    the command specified corresponds to one of our `Kind` variants,
1077         //    and we filter all rules based on that.
1078         //
1079         // 2. Next, we determine which rules we're actually executing. If a
1080         //    number of path filters were specified on the command line we look
1081         //    for those, otherwise we look for anything tagged `default`.
1082         //    Here we also compute the priority of each rule based on how early
1083         //    in the command line the matching path filter showed up.
1084         //
1085         // 3. Finally, we generate some steps with host and target information.
1086         //
1087         // The last step is by far the most complicated and subtle. The basic
1088         // thinking here is that we want to take the cartesian product of
1089         // specified hosts and targets and build rules with that. The list of
1090         // hosts and targets, if not specified, come from the how this build was
1091         // configured. If the rule we're looking at is a host-only rule the we
1092         // ignore the list of targets and instead consider the list of hosts
1093         // also the list of targets.
1094         //
1095         // Once the host and target lists are generated we take the cartesian
1096         // product of the two and then create a step based off them. Note that
1097         // the stage each step is associated was specified with the `--step`
1098         // flag on the command line.
1099         let (kind, paths) = match self.build.flags.cmd {
1100             Subcommand::Build { ref paths } => (Kind::Build, &paths[..]),
1101             Subcommand::Doc { ref paths } => (Kind::Doc, &paths[..]),
1102             Subcommand::Test { ref paths, test_args: _ } => (Kind::Test, &paths[..]),
1103             Subcommand::Bench { ref paths, test_args: _ } => (Kind::Bench, &paths[..]),
1104             Subcommand::Dist { ref paths, install } => {
1105                 if install {
1106                     return vec![self.sbuild.name("install")]
1107                 } else {
1108                     (Kind::Dist, &paths[..])
1109                 }
1110             }
1111             Subcommand::Clean => panic!(),
1112         };
1113
1114         let mut rules: Vec<_> = self.rules.values().filter_map(|rule| {
1115             if rule.kind != kind {
1116                 return None;
1117             }
1118
1119             if paths.len() == 0 && rule.default {
1120                 Some((rule, 0))
1121             } else {
1122                 paths.iter().position(|path| path.ends_with(rule.path))
1123                      .map(|priority| (rule, priority))
1124             }
1125         }).collect();
1126
1127         rules.sort_by_key(|&(_, priority)| priority);
1128
1129         rules.into_iter().flat_map(|(rule, _)| {
1130             let hosts = if rule.only_host_build || rule.only_build {
1131                 &self.build.config.host[..1]
1132             } else if self.build.flags.host.len() > 0 {
1133                 &self.build.flags.host
1134             } else {
1135                 &self.build.config.host
1136             };
1137             let targets = if self.build.flags.target.len() > 0 {
1138                 &self.build.flags.target
1139             } else {
1140                 &self.build.config.target
1141             };
1142             // Determine the actual targets participating in this rule.
1143             // NOTE: We should keep the full projection from build triple to
1144             // the hosts for the dist steps, now that the hosts array above is
1145             // truncated to avoid duplication of work in that case. Therefore
1146             // the original non-shadowed hosts array is used below.
1147             let arr = if rule.host {
1148                 // If --target was specified but --host wasn't specified,
1149                 // don't run any host-only tests. Also, respect any `--host`
1150                 // overrides as done for `hosts`.
1151                 if self.build.flags.host.len() > 0 {
1152                     &self.build.flags.host[..]
1153                 } else if self.build.flags.target.len() > 0 {
1154                     &[]
1155                 } else if rule.only_build {
1156                     &self.build.config.host[..1]
1157                 } else {
1158                     &self.build.config.host[..]
1159                 }
1160             } else {
1161                 targets
1162             };
1163
1164             hosts.iter().flat_map(move |host| {
1165                 arr.iter().map(move |target| {
1166                     self.sbuild.name(rule.name).target(target).host(host)
1167                 })
1168             })
1169         }).collect()
1170     }
1171
1172     /// Execute all top-level targets indicated by `steps`.
1173     ///
1174     /// This will take the list returned by `plan` and then execute each step
1175     /// along with all required dependencies as it goes up the chain.
1176     fn run(&self, steps: &[Step<'a>]) {
1177         self.build.verbose("bootstrap top targets:");
1178         for step in steps.iter() {
1179             self.build.verbose(&format!("\t{:?}", step));
1180         }
1181
1182         // Using `steps` as the top-level targets, make a topological ordering
1183         // of what we need to do.
1184         let order = self.expand(steps);
1185
1186         // Print out what we're doing for debugging
1187         self.build.verbose("bootstrap build plan:");
1188         for step in order.iter() {
1189             self.build.verbose(&format!("\t{:?}", step));
1190         }
1191
1192         // And finally, iterate over everything and execute it.
1193         for step in order.iter() {
1194             if self.build.flags.keep_stage.map_or(false, |s| step.stage <= s) {
1195                 self.build.verbose(&format!("keeping step {:?}", step));
1196                 continue;
1197             }
1198             self.build.verbose(&format!("executing step {:?}", step));
1199             (self.rules[step.name].run)(step);
1200         }
1201     }
1202
1203     /// From the top level targets `steps` generate a topological ordering of
1204     /// all steps needed to run those steps.
1205     fn expand(&self, steps: &[Step<'a>]) -> Vec<Step<'a>> {
1206         // First up build a graph of steps and their dependencies. The `nodes`
1207         // map is a map from step to a unique number. The `edges` map is a
1208         // map from these unique numbers to a list of other numbers,
1209         // representing dependencies.
1210         let mut nodes = HashMap::new();
1211         nodes.insert(Step::noop(), 0);
1212         let mut edges = HashMap::new();
1213         edges.insert(0, HashSet::new());
1214         for step in steps {
1215             self.build_graph(step.clone(), &mut nodes, &mut edges);
1216         }
1217
1218         // Now that we've built up the actual dependency graph, draw more
1219         // dependency edges to satisfy the `after` dependencies field for each
1220         // rule.
1221         self.satisfy_after_deps(&nodes, &mut edges);
1222
1223         // And finally, perform a topological sort to return a list of steps to
1224         // execute.
1225         let mut order = Vec::new();
1226         let mut visited = HashSet::new();
1227         visited.insert(0);
1228         let idx_to_node = nodes.iter().map(|p| (*p.1, p.0)).collect::<HashMap<_, _>>();
1229         for idx in 0..nodes.len() {
1230             self.topo_sort(idx, &idx_to_node, &edges, &mut visited, &mut order);
1231         }
1232         return order
1233     }
1234
1235     /// Builds the dependency graph rooted at `step`.
1236     ///
1237     /// The `nodes` and `edges` maps are filled out according to the rule
1238     /// described by `step.name`.
1239     fn build_graph(&self,
1240                    step: Step<'a>,
1241                    nodes: &mut HashMap<Step<'a>, usize>,
1242                    edges: &mut HashMap<usize, HashSet<usize>>) -> usize {
1243         use std::collections::hash_map::Entry;
1244
1245         let idx = nodes.len();
1246         match nodes.entry(step.clone()) {
1247             Entry::Vacant(e) => { e.insert(idx); }
1248             Entry::Occupied(e) => return *e.get(),
1249         }
1250
1251         let mut deps = Vec::new();
1252         for dep in self.rules[step.name].deps.iter() {
1253             let dep = dep(&step);
1254             if dep.name.starts_with("default:") {
1255                 let kind = match &dep.name[8..] {
1256                     "doc" => Kind::Doc,
1257                     "dist" => Kind::Dist,
1258                     kind => panic!("unknown kind: `{}`", kind),
1259                 };
1260                 let host = self.build.config.host.iter().any(|h| h == dep.target);
1261                 let rules = self.rules.values().filter(|r| r.default);
1262                 for rule in rules.filter(|r| r.kind == kind && (!r.host || host)) {
1263                     deps.push(self.build_graph(dep.name(rule.name), nodes, edges));
1264                 }
1265             } else {
1266                 deps.push(self.build_graph(dep, nodes, edges));
1267             }
1268         }
1269
1270         edges.entry(idx).or_insert(HashSet::new()).extend(deps);
1271         return idx
1272     }
1273
1274     /// Given a dependency graph with a finished list of `nodes`, fill out more
1275     /// dependency `edges`.
1276     ///
1277     /// This is the step which satisfies all `after` listed dependencies in
1278     /// `Rule` above.
1279     fn satisfy_after_deps(&self,
1280                           nodes: &HashMap<Step<'a>, usize>,
1281                           edges: &mut HashMap<usize, HashSet<usize>>) {
1282         // Reverse map from the name of a step to the node indices that it
1283         // appears at.
1284         let mut name_to_idx = HashMap::new();
1285         for (step, &idx) in nodes {
1286             name_to_idx.entry(step.name).or_insert(Vec::new()).push(idx);
1287         }
1288
1289         for (step, idx) in nodes {
1290             if *step == Step::noop() {
1291                 continue
1292             }
1293             for after in self.rules[step.name].after.iter() {
1294                 // This is the critical piece of an `after` dependency. If the
1295                 // dependency isn't actually in our graph then no edge is drawn,
1296                 // only if it's already present do we draw the edges.
1297                 if let Some(idxs) = name_to_idx.get(after) {
1298                     edges.get_mut(idx).unwrap()
1299                          .extend(idxs.iter().cloned());
1300                 }
1301             }
1302         }
1303     }
1304
1305     fn topo_sort(&self,
1306                  cur: usize,
1307                  nodes: &HashMap<usize, &Step<'a>>,
1308                  edges: &HashMap<usize, HashSet<usize>>,
1309                  visited: &mut HashSet<usize>,
1310                  order: &mut Vec<Step<'a>>) {
1311         if !visited.insert(cur) {
1312             return
1313         }
1314         for dep in edges[&cur].iter() {
1315             self.topo_sort(*dep, nodes, edges, visited, order);
1316         }
1317         order.push(nodes[&cur].clone());
1318     }
1319 }
1320
1321 #[cfg(test)]
1322 mod tests {
1323     use std::env;
1324
1325     use Build;
1326     use config::Config;
1327     use flags::Flags;
1328
1329     macro_rules! a {
1330         ($($a:expr),*) => (vec![$($a.to_string()),*])
1331     }
1332
1333     fn build(args: &[&str],
1334              extra_host: &[&str],
1335              extra_target: &[&str]) -> Build {
1336         let mut args = args.iter().map(|s| s.to_string()).collect::<Vec<_>>();
1337         args.push("--build".to_string());
1338         args.push("A".to_string());
1339         let flags = Flags::parse(&args);
1340
1341         let mut config = Config::default();
1342         config.docs = true;
1343         config.build = "A".to_string();
1344         config.host = vec![config.build.clone()];
1345         config.host.extend(extra_host.iter().map(|s| s.to_string()));
1346         config.target = config.host.clone();
1347         config.target.extend(extra_target.iter().map(|s| s.to_string()));
1348
1349         let mut build = Build::new(flags, config);
1350         let cwd = env::current_dir().unwrap();
1351         build.crates.insert("std".to_string(), ::Crate {
1352             name: "std".to_string(),
1353             deps: Vec::new(),
1354             path: cwd.join("src/std"),
1355             doc_step: "doc-crate-std".to_string(),
1356             build_step: "build-crate-std".to_string(),
1357             test_step: "test-crate-std".to_string(),
1358             bench_step: "bench-crate-std".to_string(),
1359             version: String::new(),
1360         });
1361         build.crates.insert("test".to_string(), ::Crate {
1362             name: "test".to_string(),
1363             deps: Vec::new(),
1364             path: cwd.join("src/test"),
1365             doc_step: "doc-crate-test".to_string(),
1366             build_step: "build-crate-test".to_string(),
1367             test_step: "test-crate-test".to_string(),
1368             bench_step: "bench-crate-test".to_string(),
1369             version: String::new(),
1370         });
1371         build.crates.insert("rustc-main".to_string(), ::Crate {
1372             name: "rustc-main".to_string(),
1373             deps: Vec::new(),
1374             version: String::new(),
1375             path: cwd.join("src/rustc-main"),
1376             doc_step: "doc-crate-rustc-main".to_string(),
1377             build_step: "build-crate-rustc-main".to_string(),
1378             test_step: "test-crate-rustc-main".to_string(),
1379             bench_step: "bench-crate-rustc-main".to_string(),
1380         });
1381         return build
1382     }
1383
1384     #[test]
1385     fn dist_baseline() {
1386         let build = build(&["dist"], &[], &[]);
1387         let rules = super::build_rules(&build);
1388         let plan = rules.plan();
1389         println!("rules: {:#?}", plan);
1390         assert!(plan.iter().all(|s| s.stage == 2));
1391         assert!(plan.iter().all(|s| s.host == "A" ));
1392         assert!(plan.iter().all(|s| s.target == "A" ));
1393
1394         let step = super::Step {
1395             name: "",
1396             stage: 2,
1397             host: &build.config.build,
1398             target: &build.config.build,
1399         };
1400
1401         assert!(plan.contains(&step.name("dist-docs")));
1402         assert!(plan.contains(&step.name("dist-mingw")));
1403         assert!(plan.contains(&step.name("dist-rustc")));
1404         assert!(plan.contains(&step.name("dist-std")));
1405         assert!(plan.contains(&step.name("dist-src")));
1406     }
1407
1408     #[test]
1409     fn dist_with_targets() {
1410         let build = build(&["dist"], &[], &["B"]);
1411         let rules = super::build_rules(&build);
1412         let plan = rules.plan();
1413         println!("rules: {:#?}", plan);
1414         assert!(plan.iter().all(|s| s.stage == 2));
1415         assert!(plan.iter().all(|s| s.host == "A" ));
1416
1417         let step = super::Step {
1418             name: "",
1419             stage: 2,
1420             host: &build.config.build,
1421             target: &build.config.build,
1422         };
1423
1424         assert!(plan.contains(&step.name("dist-docs")));
1425         assert!(plan.contains(&step.name("dist-mingw")));
1426         assert!(plan.contains(&step.name("dist-rustc")));
1427         assert!(plan.contains(&step.name("dist-std")));
1428         assert!(plan.contains(&step.name("dist-src")));
1429
1430         assert!(plan.contains(&step.target("B").name("dist-docs")));
1431         assert!(plan.contains(&step.target("B").name("dist-mingw")));
1432         assert!(!plan.contains(&step.target("B").name("dist-rustc")));
1433         assert!(plan.contains(&step.target("B").name("dist-std")));
1434         assert!(!plan.contains(&step.target("B").name("dist-src")));
1435     }
1436
1437     #[test]
1438     fn dist_with_hosts() {
1439         let build = build(&["dist"], &["B"], &[]);
1440         let rules = super::build_rules(&build);
1441         let plan = rules.plan();
1442         println!("rules: {:#?}", plan);
1443         assert!(plan.iter().all(|s| s.stage == 2));
1444
1445         let step = super::Step {
1446             name: "",
1447             stage: 2,
1448             host: &build.config.build,
1449             target: &build.config.build,
1450         };
1451
1452         assert!(!plan.iter().any(|s| s.host == "B"));
1453
1454         assert!(plan.contains(&step.name("dist-docs")));
1455         assert!(plan.contains(&step.name("dist-mingw")));
1456         assert!(plan.contains(&step.name("dist-rustc")));
1457         assert!(plan.contains(&step.name("dist-std")));
1458         assert!(plan.contains(&step.name("dist-src")));
1459
1460         assert!(plan.contains(&step.target("B").name("dist-docs")));
1461         assert!(plan.contains(&step.target("B").name("dist-mingw")));
1462         assert!(plan.contains(&step.target("B").name("dist-rustc")));
1463         assert!(plan.contains(&step.target("B").name("dist-std")));
1464         assert!(!plan.contains(&step.target("B").name("dist-src")));
1465     }
1466
1467     #[test]
1468     fn dist_with_targets_and_hosts() {
1469         let build = build(&["dist"], &["B"], &["C"]);
1470         let rules = super::build_rules(&build);
1471         let plan = rules.plan();
1472         println!("rules: {:#?}", plan);
1473         assert!(plan.iter().all(|s| s.stage == 2));
1474
1475         let step = super::Step {
1476             name: "",
1477             stage: 2,
1478             host: &build.config.build,
1479             target: &build.config.build,
1480         };
1481
1482         assert!(!plan.iter().any(|s| s.host == "B"));
1483         assert!(!plan.iter().any(|s| s.host == "C"));
1484
1485         assert!(plan.contains(&step.name("dist-docs")));
1486         assert!(plan.contains(&step.name("dist-mingw")));
1487         assert!(plan.contains(&step.name("dist-rustc")));
1488         assert!(plan.contains(&step.name("dist-std")));
1489         assert!(plan.contains(&step.name("dist-src")));
1490
1491         assert!(plan.contains(&step.target("B").name("dist-docs")));
1492         assert!(plan.contains(&step.target("B").name("dist-mingw")));
1493         assert!(plan.contains(&step.target("B").name("dist-rustc")));
1494         assert!(plan.contains(&step.target("B").name("dist-std")));
1495         assert!(!plan.contains(&step.target("B").name("dist-src")));
1496
1497         assert!(plan.contains(&step.target("C").name("dist-docs")));
1498         assert!(plan.contains(&step.target("C").name("dist-mingw")));
1499         assert!(!plan.contains(&step.target("C").name("dist-rustc")));
1500         assert!(plan.contains(&step.target("C").name("dist-std")));
1501         assert!(!plan.contains(&step.target("C").name("dist-src")));
1502     }
1503
1504     #[test]
1505     fn dist_target_with_target_flag() {
1506         let build = build(&["dist", "--target=C"], &["B"], &["C"]);
1507         let rules = super::build_rules(&build);
1508         let plan = rules.plan();
1509         println!("rules: {:#?}", plan);
1510         assert!(plan.iter().all(|s| s.stage == 2));
1511
1512         let step = super::Step {
1513             name: "",
1514             stage: 2,
1515             host: &build.config.build,
1516             target: &build.config.build,
1517         };
1518
1519         assert!(!plan.iter().any(|s| s.target == "A"));
1520         assert!(!plan.iter().any(|s| s.target == "B"));
1521         assert!(!plan.iter().any(|s| s.host == "B"));
1522         assert!(!plan.iter().any(|s| s.host == "C"));
1523
1524         assert!(plan.contains(&step.target("C").name("dist-docs")));
1525         assert!(plan.contains(&step.target("C").name("dist-mingw")));
1526         assert!(!plan.contains(&step.target("C").name("dist-rustc")));
1527         assert!(plan.contains(&step.target("C").name("dist-std")));
1528         assert!(!plan.contains(&step.target("C").name("dist-src")));
1529     }
1530
1531     #[test]
1532     fn dist_host_with_target_flag() {
1533         let build = build(&["dist", "--host=B", "--target=B"], &["B"], &["C"]);
1534         let rules = super::build_rules(&build);
1535         let plan = rules.plan();
1536         println!("rules: {:#?}", plan);
1537         assert!(plan.iter().all(|s| s.stage == 2));
1538
1539         let step = super::Step {
1540             name: "",
1541             stage: 2,
1542             host: &build.config.build,
1543             target: &build.config.build,
1544         };
1545
1546         assert!(!plan.iter().any(|s| s.target == "A"));
1547         assert!(!plan.iter().any(|s| s.target == "C"));
1548         assert!(!plan.iter().any(|s| s.host == "B"));
1549         assert!(!plan.iter().any(|s| s.host == "C"));
1550
1551         assert!(plan.contains(&step.target("B").name("dist-docs")));
1552         assert!(plan.contains(&step.target("B").name("dist-mingw")));
1553         assert!(plan.contains(&step.target("B").name("dist-rustc")));
1554         assert!(plan.contains(&step.target("B").name("dist-std")));
1555         assert!(plan.contains(&step.target("B").name("dist-src")));
1556
1557         let all = rules.expand(&plan);
1558         println!("all rules: {:#?}", all);
1559         assert!(!all.contains(&step.name("rustc")));
1560         assert!(!all.contains(&step.name("build-crate-test").stage(1)));
1561
1562         // all stage0 compiles should be for the build target, A
1563         for step in all.iter().filter(|s| s.stage == 0) {
1564             if !step.name.contains("build-crate") {
1565                 continue
1566             }
1567             println!("step: {:?}", step);
1568             assert!(step.host != "B");
1569             assert!(step.target != "B");
1570             assert!(step.host != "C");
1571             assert!(step.target != "C");
1572         }
1573     }
1574
1575     #[test]
1576     fn build_default() {
1577         let build = build(&["build"], &["B"], &["C"]);
1578         let rules = super::build_rules(&build);
1579         let plan = rules.plan();
1580         println!("rules: {:#?}", plan);
1581         assert!(plan.iter().all(|s| s.stage == 2));
1582
1583         let step = super::Step {
1584             name: "",
1585             stage: 2,
1586             host: &build.config.build,
1587             target: &build.config.build,
1588         };
1589
1590         // rustc built for all for of (A, B) x (A, B)
1591         assert!(plan.contains(&step.name("librustc")));
1592         assert!(plan.contains(&step.target("B").name("librustc")));
1593         assert!(plan.contains(&step.host("B").target("A").name("librustc")));
1594         assert!(plan.contains(&step.host("B").target("B").name("librustc")));
1595
1596         // rustc never built for C
1597         assert!(!plan.iter().any(|s| {
1598             s.name.contains("rustc") && (s.host == "C" || s.target == "C")
1599         }));
1600
1601         // test built for everything
1602         assert!(plan.contains(&step.name("libtest")));
1603         assert!(plan.contains(&step.target("B").name("libtest")));
1604         assert!(plan.contains(&step.host("B").target("A").name("libtest")));
1605         assert!(plan.contains(&step.host("B").target("B").name("libtest")));
1606         assert!(plan.contains(&step.host("A").target("C").name("libtest")));
1607         assert!(plan.contains(&step.host("B").target("C").name("libtest")));
1608
1609         let all = rules.expand(&plan);
1610         println!("all rules: {:#?}", all);
1611         assert!(all.contains(&step.name("rustc")));
1612         assert!(all.contains(&step.name("libstd")));
1613     }
1614
1615     #[test]
1616     fn build_filtered() {
1617         let build = build(&["build", "--target=C"], &["B"], &["C"]);
1618         let rules = super::build_rules(&build);
1619         let plan = rules.plan();
1620         println!("rules: {:#?}", plan);
1621         assert!(plan.iter().all(|s| s.stage == 2));
1622
1623         assert!(!plan.iter().any(|s| s.name.contains("rustc")));
1624         assert!(plan.iter().all(|s| {
1625             !s.name.contains("test") || s.target == "C"
1626         }));
1627     }
1628
1629     #[test]
1630     fn test_default() {
1631         let build = build(&["test"], &[], &[]);
1632         let rules = super::build_rules(&build);
1633         let plan = rules.plan();
1634         println!("rules: {:#?}", plan);
1635         assert!(plan.iter().all(|s| s.stage == 2));
1636         assert!(plan.iter().all(|s| s.host == "A"));
1637         assert!(plan.iter().all(|s| s.target == "A"));
1638
1639         assert!(plan.iter().any(|s| s.name.contains("-ui")));
1640         assert!(plan.iter().any(|s| s.name.contains("cfail")));
1641         assert!(plan.iter().any(|s| s.name.contains("cfail-full")));
1642         assert!(plan.iter().any(|s| s.name.contains("codegen-units")));
1643         assert!(plan.iter().any(|s| s.name.contains("debuginfo")));
1644         assert!(plan.iter().any(|s| s.name.contains("docs")));
1645         assert!(plan.iter().any(|s| s.name.contains("error-index")));
1646         assert!(plan.iter().any(|s| s.name.contains("incremental")));
1647         assert!(plan.iter().any(|s| s.name.contains("linkchecker")));
1648         assert!(plan.iter().any(|s| s.name.contains("mir-opt")));
1649         assert!(plan.iter().any(|s| s.name.contains("pfail")));
1650         assert!(plan.iter().any(|s| s.name.contains("rfail")));
1651         assert!(plan.iter().any(|s| s.name.contains("rfail-full")));
1652         assert!(plan.iter().any(|s| s.name.contains("rmake")));
1653         assert!(plan.iter().any(|s| s.name.contains("rpass")));
1654         assert!(plan.iter().any(|s| s.name.contains("rpass-full")));
1655         assert!(plan.iter().any(|s| s.name.contains("rustc-all")));
1656         assert!(plan.iter().any(|s| s.name.contains("rustdoc")));
1657         assert!(plan.iter().any(|s| s.name.contains("std-all")));
1658         assert!(plan.iter().any(|s| s.name.contains("test-all")));
1659         assert!(plan.iter().any(|s| s.name.contains("tidy")));
1660         assert!(plan.iter().any(|s| s.name.contains("valgrind")));
1661     }
1662
1663     #[test]
1664     fn test_with_a_target() {
1665         let build = build(&["test", "--target=C"], &[], &["C"]);
1666         let rules = super::build_rules(&build);
1667         let plan = rules.plan();
1668         println!("rules: {:#?}", plan);
1669         assert!(plan.iter().all(|s| s.stage == 2));
1670         assert!(plan.iter().all(|s| s.host == "A"));
1671         assert!(plan.iter().all(|s| s.target == "C"));
1672
1673         assert!(plan.iter().any(|s| s.name.contains("-ui")));
1674         assert!(!plan.iter().any(|s| s.name.contains("ui-full")));
1675         assert!(plan.iter().any(|s| s.name.contains("cfail")));
1676         assert!(!plan.iter().any(|s| s.name.contains("cfail-full")));
1677         assert!(plan.iter().any(|s| s.name.contains("codegen-units")));
1678         assert!(plan.iter().any(|s| s.name.contains("debuginfo")));
1679         assert!(!plan.iter().any(|s| s.name.contains("docs")));
1680         assert!(!plan.iter().any(|s| s.name.contains("error-index")));
1681         assert!(plan.iter().any(|s| s.name.contains("incremental")));
1682         assert!(!plan.iter().any(|s| s.name.contains("linkchecker")));
1683         assert!(plan.iter().any(|s| s.name.contains("mir-opt")));
1684         assert!(plan.iter().any(|s| s.name.contains("pfail")));
1685         assert!(plan.iter().any(|s| s.name.contains("rfail")));
1686         assert!(!plan.iter().any(|s| s.name.contains("rfail-full")));
1687         assert!(!plan.iter().any(|s| s.name.contains("rmake")));
1688         assert!(plan.iter().any(|s| s.name.contains("rpass")));
1689         assert!(!plan.iter().any(|s| s.name.contains("rpass-full")));
1690         assert!(!plan.iter().any(|s| s.name.contains("rustc-all")));
1691         assert!(!plan.iter().any(|s| s.name.contains("rustdoc")));
1692         assert!(plan.iter().any(|s| s.name.contains("std-all")));
1693         assert!(plan.iter().any(|s| s.name.contains("test-all")));
1694         assert!(!plan.iter().any(|s| s.name.contains("tidy")));
1695         assert!(plan.iter().any(|s| s.name.contains("valgrind")));
1696     }
1697 }