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