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.
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.
11 //! Definition of steps of the build system.
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.
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.
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.
29 use std::collections::{BTreeMap, HashSet, HashMap};
33 use check::{self, TestKind};
37 use flags::Subcommand;
40 use {Compiler, Build, Mode};
42 pub fn run(build: &Build) {
43 let rules = build_rules(build);
44 let steps = rules.plan();
48 pub fn build_rules<'a>(build: &'a Build) -> Rules {
49 let mut rules = Rules::new(build);
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
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.
61 // Next up we'll see two arguments to this method:
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.
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).
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.
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.
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
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")
107 if s.target == build.config.build {
110 s.target(&build.config.build)
113 .run(move |s| native::llvm(build, s.target));
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.
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))
127 .host(&build.config.build)
131 .run(move |s| compile::assemble_rustc(build, s.stage, s.target));
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" {
154 // ========================================================================
155 // Crate compilations
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()));
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.
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"))
177 rules.build("librustc", "src/librustc")
178 .dep(|s| s.name("libtest"))
179 .dep(|s| s.name("librustc-link"))
183 // Helper method to define the rules to link a crate into its place in the
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:
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.
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.
201 // * Finally, there are some cases, however, which do indeed comiple crates
202 // and link them into place afterwards.
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>,
213 link: fn(&Build, &Compiler, &Compiler, &str))
214 -> RuleBuilder<'a, 'b> {
215 let mut rule = rules.build(&krate, "path/to/nowhere");
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 {
222 s.host(&build.config.build)
226 if build.force_use_stage1(&s.compiler(), s.target) {
228 &s.stage(1).host(&build.config.build).compiler(),
231 } else if s.host == build.config.build {
232 link(build, &s.compiler(), &s.compiler(), s.target)
235 &s.host(&build.config.build).compiler(),
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.
248 // All of these rules use the helper definition above.
254 .dep(|s| s.name("startup-objects"))
255 .dep(|s| s.name("create-sysroot").target(s.host));
261 .dep(|s| s.name("libstd-link"));
265 "build-crate-rustc-main",
267 .dep(|s| s.name("libtest-link"));
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()));
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()));
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()));
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
291 rules.build("may-run-build-script", "path/to/nowhere")
293 s.name("libstd-link")
294 .host(&build.config.build)
295 .target(&build.config.build)
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));
301 // ========================================================================
304 // Various unit tests and tests suites we can run
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
314 check::compiletest(build, &s.compiler(), s.target, mode, dir)
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");
329 suite("check-codegen-units", "src/test/codegen-units", "codegen-units",
331 suite("check-incremental", "src/test/incremental", "incremental",
335 if build.config.build.contains("msvc") {
336 // nothing to do for debuginfo tests
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");
355 if build.config.build.contains("apple") {
356 rule.dep(|s| s.name("check-debuginfo-lldb"));
358 rule.dep(|s| s.name("check-debuginfo-gdb"));
362 rules.test("debugger-scripts", "src/etc/lldb_batchmode.py")
363 .run(move |s| dist::debugger_scripts(build, &build.sysroot(&s.compiler()),
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")
375 check::compiletest(build, &s.compiler(), s.target, mode, dir)
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",
391 suite("check-pretty-rfail", "src/test/run-fail/pretty", "pretty",
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");
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,
409 rules.test("check-std-all", "path/to/nowhere")
410 .dep(|s| s.name("libtest"))
411 .dep(|s| s.name("remote-copy-libs"))
413 .run(move |s| check::krate(build, &s.compiler(), s.target,
414 Mode::Libstd, TestKind::Test, None));
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,
425 rules.bench("bench-std-all", "path/to/nowhere")
426 .dep(|s| s.name("libtest"))
427 .dep(|s| s.name("remote-copy-libs"))
429 .run(move |s| check::krate(build, &s.compiler(), s.target,
430 Mode::Libstd, TestKind::Bench, None));
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,
440 rules.test("check-test-all", "path/to/nowhere")
441 .dep(|s| s.name("libtest"))
442 .dep(|s| s.name("remote-copy-libs"))
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"))
451 .run(move |s| check::krate(build, &s.compiler(), s.target,
452 Mode::Librustc, TestKind::Test,
455 rules.test("check-rustc-all", "path/to/nowhere")
456 .dep(|s| s.name("librustc"))
457 .dep(|s| s.name("remote-copy-libs"))
460 .run(move |s| check::krate(build, &s.compiler(), s.target,
461 Mode::Librustc, TestKind::Test, None));
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(build.config.docs)
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"))
473 .run(move |s| check::cargotest(build, s.stage, s.target));
474 rules.test("check-cargo", "cargo")
475 .dep(|s| s.name("tool-cargo"))
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))
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))
489 .run(move |s| check::error_index(build, &s.compiler()));
490 rules.test("check-docs", "src/doc")
491 .dep(|s| s.name("libtest"))
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));
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));
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.
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"))
518 if build.remote_tested(s.target) {
519 s.name("tool-remote-test-client").target(s.host).stage(0)
525 if build.remote_tested(s.target) {
526 s.name("tool-remote-test-server")
531 .run(move |s| check::remote_copy_libs(build, &s.compiler(), s.target));
533 rules.test("check-bootstrap", "src/bootstrap")
537 .run(move |_| check::bootstrap(build));
539 // ========================================================================
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")
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"))
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)
596 .run(move |s| compile::tool(build, s.stage, s.target, "cargo"));
597 rules.build("tool-rls", "src/tools/rls")
599 .default(build.config.extended)
600 .dep(|s| s.name("librustc-tool"))
601 .dep(|s| s.stage(0).host(s.target).name("openssl"))
603 // rls, like cargo, uses procedural macros
604 s.name("librustc-link")
605 .target(&build.config.build)
606 .host(&build.config.build)
608 .run(move |s| compile::tool(build, s.stage, s.target, "rls"));
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");
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));
629 // ========================================================================
630 // Documentation targets
631 rules.doc("doc-book", "src/doc/book")
633 s.name("tool-rustbook")
634 .host(&build.config.build)
635 .target(&build.config.build)
638 .default(build.config.docs)
639 .run(move |s| doc::book(build, s.target, "book"));
640 rules.doc("doc-nomicon", "src/doc/nomicon")
642 s.name("tool-rustbook")
643 .host(&build.config.build)
644 .target(&build.config.build)
647 .default(build.config.docs)
648 .run(move |s| doc::rustbook(build, s.target, "nomicon"));
649 rules.doc("doc-reference", "src/doc/reference")
651 s.name("tool-rustbook")
652 .host(&build.config.build)
653 .target(&build.config.build)
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")
660 s.name("tool-rustbook")
661 .host(&build.config.build)
662 .target(&build.config.build)
665 .default(build.config.docs)
666 .run(move |s| doc::rustbook(build, s.target, "unstable-book"));
667 rules.doc("doc-standalone", "src/doc")
670 .host(&build.config.build)
671 .target(&build.config.build)
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)
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));
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));
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"))
702 .default(default && build.config.docs)
703 .run(move |s| doc::rustc(build, s.stage, s.target));
706 // ========================================================================
707 // Distribution targets
708 rules.dist("dist-rustc", "src/librustc")
709 .dep(move |s| s.name("rustc").host(&build.config.build))
711 .only_host_build(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")
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")
723 s.name("libtest-link")
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")
732 .only_host_build(true)
733 .dep(move |s| tool_rust_installer(build, s))
735 if s.target.contains("pc-windows-gnu") {
736 dist::mingw(build, s.target)
739 rules.dist("dist-plain-source-tarball", "src")
740 .default(build.config.rust_dist_src)
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")
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")
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")
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")
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)
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));
791 rules.dist("dist-sign", "hash-and-sign")
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));
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")
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)
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)
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)
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")
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));
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)
851 #[derive(PartialEq, Eq, Hash, Clone, Debug)]
853 /// Human readable name of the rule this step is executing. Possible names
854 /// are all defined above in `build_rules`.
857 /// The stage this step is executing in. This is typically 0, 1, or 2.
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.
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.
872 fn noop() -> Step<'a> {
873 Step { name: "", stage: 0, host: "", target: "" }
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 }
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 }
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 }
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 }
896 /// Returns the `Compiler` structure that this step corresponds to.
897 fn compiler(&self) -> Compiler<'a> {
898 Compiler::new(self.stage, self.host)
903 /// The human readable name of this target, defined in `build_rules`.
906 /// The path associated with this target, used in the `./x.py` driver for
907 /// easy and ergonomic specification of what to do.
910 /// The "kind" of top-level command that this rule is associated with, only
911 /// relevant if this is a default rule.
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>>,
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>,
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.
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
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,
935 /// Whether this rule is only for the build triple, not anything in hosts or
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.
956 fn new(name: &'a str, path: &'a str, kind: Kind) -> Rule<'a> {
960 run: Box::new(|_| ()),
965 only_host_build: false,
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>,
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,
983 self.rule.deps.push(Box::new(f));
987 fn after(&mut self, step: &'a str) -> &mut Self {
988 self.rule.after.push(step);
992 fn run<F>(&mut self, f: F) -> &mut Self
993 where F: Fn(&Step<'a>) + 'a,
995 self.rule.run = Box::new(f);
999 fn default(&mut self, default: bool) -> &mut Self {
1000 self.rule.default = default;
1004 fn host(&mut self, host: bool) -> &mut Self {
1005 self.rule.host = host;
1009 fn only_build(&mut self, only_build: bool) -> &mut Self {
1010 self.rule.only_build = only_build;
1014 fn only_host_build(&mut self, only_host_build: bool) -> &mut Self {
1015 self.rule.only_host_build = only_host_build;
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);
1030 pub struct Rules<'a> {
1033 rules: BTreeMap<&'a str, Rule<'a>>,
1036 impl<'a> Rules<'a> {
1037 fn new(build: &'a Build) -> Rules<'a> {
1041 stage: build.flags.stage.unwrap_or(2),
1042 target: &build.config.build,
1043 host: &build.config.build,
1046 rules: BTreeMap::new(),
1050 /// Creates a new rule of `Kind::Build` with the specified human readable
1051 /// name and path associated with it.
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)
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)
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)
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)
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)
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)
1090 fn rule<'b>(&'b mut self,
1093 kind: Kind) -> RuleBuilder<'a, 'b> {
1096 rule: Rule::new(name, path, kind),
1100 /// Verify the dependency graph defined by all our rules are correct, e.g.
1101 /// everything points to a valid something else.
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:") {
1109 if dep == Step::noop() {
1114 invalid rule dependency graph detected, was a rule added and maybe typo'd?
1116 `{}` depends on `{}` which does not exist
1118 ", rule.name, dep.name);
1123 pub fn get_help(&self, command: &str) -> Option<String> {
1124 let kind = match command {
1125 "build" => Kind::Build,
1127 "test" => Kind::Test,
1128 "bench" => Kind::Bench,
1129 "dist" => Kind::Dist,
1130 "install" => Kind::Install,
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);
1138 let mut help_string = String::from("Available paths:\n");
1140 help_string.push_str(format!(" ./x.py {} {}\n", command, rule.path).as_str());
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:
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.
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.
1161 // 3. Finally, we generate some steps with host and target information.
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.
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!(),
1185 let mut rules: Vec<_> = self.rules.values().filter_map(|rule| {
1186 if rule.kind != kind {
1190 if paths.len() == 0 && rule.default {
1193 paths.iter().position(|path| path.ends_with(rule.path))
1194 .map(|priority| (rule, priority))
1198 rules.sort_by_key(|&(_, priority)| priority);
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
1206 &self.build.config.host
1208 let targets = if self.build.flags.target.len() > 0 {
1209 &self.build.flags.target
1211 &self.build.config.target
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 {
1226 } else if rule.only_build {
1227 &self.build.config.host[..1]
1229 &self.build.config.host[..]
1235 hosts.iter().flat_map(move |host| {
1236 arr.iter().map(move |target| {
1237 self.sbuild.name(rule.name).target(target).host(host)
1243 /// Execute all top-level targets indicated by `steps`.
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));
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);
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));
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));
1269 self.build.verbose(&format!("executing step {:?}", step));
1270 (self.rules[step.name].run)(step);
1273 // Check for postponed failures from `test --no-fail-fast`.
1274 let failures = self.build.delayed_failures.get();
1276 println!("\n{} command(s) did not execute successfully.\n", failures);
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());
1293 self.build_graph(step.clone(), &mut nodes, &mut edges);
1296 // Now that we've built up the actual dependency graph, draw more
1297 // dependency edges to satisfy the `after` dependencies field for each
1299 self.satisfy_after_deps(&nodes, &mut edges);
1301 // And finally, perform a topological sort to return a list of steps to
1303 let mut order = Vec::new();
1304 let mut visited = HashSet::new();
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);
1313 /// Builds the dependency graph rooted at `step`.
1315 /// The `nodes` and `edges` maps are filled out according to the rule
1316 /// described by `step.name`.
1317 fn build_graph(&self,
1319 nodes: &mut HashMap<Step<'a>, usize>,
1320 edges: &mut HashMap<usize, HashSet<usize>>) -> usize {
1321 use std::collections::hash_map::Entry;
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(),
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..] {
1335 "dist" => Kind::Dist,
1336 kind => panic!("unknown kind: `{}`", kind),
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));
1344 deps.push(self.build_graph(dep, nodes, edges));
1348 edges.entry(idx).or_insert(HashSet::new()).extend(deps);
1352 /// Given a dependency graph with a finished list of `nodes`, fill out more
1353 /// dependency `edges`.
1355 /// This is the step which satisfies all `after` listed dependencies in
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
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);
1367 for (step, idx) in nodes {
1368 if *step == Step::noop() {
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());
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) {
1392 for dep in edges[&cur].iter() {
1393 self.topo_sort(*dep, nodes, edges, visited, order);
1395 order.push(nodes[&cur].clone());
1407 fn build(args: &[&str],
1408 extra_host: &[&str],
1409 extra_target: &[&str]) -> Build {
1410 build_(args, extra_host, extra_target, true)
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);
1422 let mut config = Config::default();
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()));
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(),
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(),
1442 build.crates.insert("test".to_string(), ::Crate {
1443 name: "test".to_string(),
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(),
1452 build.crates.insert("rustc-main".to_string(), ::Crate {
1453 name: "rustc-main".to_string(),
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(),
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" ));
1475 let step = super::Step {
1478 host: &build.config.build,
1479 target: &build.config.build,
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")));
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" ));
1498 let step = super::Step {
1501 host: &build.config.build,
1502 target: &build.config.build,
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")));
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")));
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));
1526 let step = super::Step {
1529 host: &build.config.build,
1530 target: &build.config.build,
1533 assert!(!plan.iter().any(|s| s.host == "B"));
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")));
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")));
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));
1556 let step = super::Step {
1559 host: &build.config.build,
1560 target: &build.config.build,
1563 assert!(!plan.iter().any(|s| s.host == "B"));
1564 assert!(!plan.iter().any(|s| s.host == "C"));
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")));
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")));
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")));
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));
1593 let step = super::Step {
1596 host: &build.config.build,
1597 target: &build.config.build,
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"));
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")));
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));
1620 let step = super::Step {
1623 host: &build.config.build,
1624 target: &build.config.build,
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"));
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")));
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)));
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") {
1648 println!("step: {:?}", step);
1649 assert!(step.host != "B");
1650 assert!(step.target != "B");
1651 assert!(step.host != "C");
1652 assert!(step.target != "C");
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));
1664 let step = super::Step {
1667 host: &build.config.build,
1668 target: &build.config.build,
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")));
1677 // rustc never built for C
1678 assert!(!plan.iter().any(|s| {
1679 s.name.contains("rustc") && (s.host == "C" || s.target == "C")
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")));
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")));
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));
1704 assert!(!plan.iter().any(|s| s.name.contains("rustc")));
1705 assert!(plan.iter().all(|s| {
1706 !s.name.contains("test") || s.target == "C"
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"));
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")));
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"));
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")));
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")
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")