]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/step.rs
4b3be04b57c57948627bbbb601fcb7665c5315b5
[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 //! Major workhorse of rustbuild, definition and dependencies between stages of
12 //! the copmile.
13 //!
14 //! The primary purpose of this module is to define the various `Step`s of
15 //! execution of the build. Each `Step` has a corresponding `Source` indicating
16 //! what it's actually doing along with a number of dependencies which must be
17 //! executed first.
18 //!
19 //! This module will take the CLI as input and calculate the steps required for
20 //! the build requested, ensuring that all intermediate pieces are in place.
21 //! Essentially this module is a `make`-replacement, but not as good.
22
23 use std::collections::HashSet;
24
25 use {Build, Compiler};
26
27 #[derive(Hash, Eq, PartialEq, Clone, Debug)]
28 pub struct Step<'a> {
29     pub src: Source<'a>,
30     pub target: &'a str,
31 }
32
33 /// Macro used to iterate over all targets that are recognized by the build
34 /// system.
35 ///
36 /// Whenever a new step is added it will involve adding an entry here, updating
37 /// the dependencies section below, and then adding an implementation of the
38 /// step in `build/mod.rs`.
39 ///
40 /// This macro takes another macro as an argument and then calls that macro with
41 /// all steps that the build system knows about.
42 macro_rules! targets {
43     ($m:ident) => {
44         $m! {
45             // Step representing building the stageN compiler. This is just the
46             // compiler executable itself, not any of the support libraries
47             (rustc, Rustc { stage: u32 }),
48
49             // Steps for the two main cargo builds. These are parameterized over
50             // the compiler which is producing the artifact.
51             (libstd, Libstd { compiler: Compiler<'a> }),
52             (libtest, Libtest { compiler: Compiler<'a> }),
53             (librustc, Librustc { compiler: Compiler<'a> }),
54
55             // Links the target produced by the compiler provided into the
56             // host's directory also provided.
57             (libstd_link, LibstdLink {
58                 compiler: Compiler<'a>,
59                 host: &'a str
60             }),
61             (libtest_link, LibtestLink {
62                 compiler: Compiler<'a>,
63                 host: &'a str
64             }),
65             (librustc_link, LibrustcLink {
66                 compiler: Compiler<'a>,
67                 host: &'a str
68             }),
69
70             // Various tools that we can build as part of the build.
71             (tool_linkchecker, ToolLinkchecker { stage: u32 }),
72             (tool_rustbook, ToolRustbook { stage: u32 }),
73             (tool_error_index, ToolErrorIndex { stage: u32 }),
74             (tool_cargotest, ToolCargoTest { stage: u32 }),
75             (tool_tidy, ToolTidy { stage: u32 }),
76             (tool_compiletest, ToolCompiletest { stage: u32 }),
77
78             // Steps for long-running native builds. Ideally these wouldn't
79             // actually exist and would be part of build scripts, but for now
80             // these are here.
81             //
82             // There aren't really any parameters to this, but empty structs
83             // with braces are unstable so we just pick something that works.
84             (llvm, Llvm { _dummy: () }),
85             (compiler_rt, CompilerRt { _dummy: () }),
86             (test_helpers, TestHelpers { _dummy: () }),
87             (debugger_scripts, DebuggerScripts { stage: u32 }),
88
89             // Steps for various pieces of documentation that we can generate,
90             // the 'doc' step is just a pseudo target to depend on a bunch of
91             // others.
92             (doc, Doc { stage: u32 }),
93             (doc_book, DocBook { stage: u32 }),
94             (doc_nomicon, DocNomicon { stage: u32 }),
95             (doc_style, DocStyle { stage: u32 }),
96             (doc_standalone, DocStandalone { stage: u32 }),
97             (doc_std, DocStd { stage: u32 }),
98             (doc_test, DocTest { stage: u32 }),
99             (doc_rustc, DocRustc { stage: u32 }),
100             (doc_error_index, DocErrorIndex { stage: u32 }),
101
102             // Steps for running tests. The 'check' target is just a pseudo
103             // target to depend on a bunch of others.
104             (check, Check { stage: u32, compiler: Compiler<'a> }),
105             (check_target, CheckTarget { stage: u32, compiler: Compiler<'a> }),
106             (check_linkcheck, CheckLinkcheck { stage: u32 }),
107             (check_cargotest, CheckCargoTest { stage: u32 }),
108             (check_tidy, CheckTidy { stage: u32 }),
109             (check_rpass, CheckRPass { compiler: Compiler<'a> }),
110             (check_rpass_full, CheckRPassFull { compiler: Compiler<'a> }),
111             (check_rpass_valgrind, CheckRPassValgrind { compiler: Compiler<'a> }),
112             (check_rfail, CheckRFail { compiler: Compiler<'a> }),
113             (check_rfail_full, CheckRFailFull { compiler: Compiler<'a> }),
114             (check_cfail, CheckCFail { compiler: Compiler<'a> }),
115             (check_cfail_full, CheckCFailFull { compiler: Compiler<'a> }),
116             (check_pfail, CheckPFail { compiler: Compiler<'a> }),
117             (check_pretty, CheckPretty { compiler: Compiler<'a> }),
118             (check_pretty_rpass, CheckPrettyRPass { compiler: Compiler<'a> }),
119             (check_pretty_rpass_full, CheckPrettyRPassFull { compiler: Compiler<'a> }),
120             (check_pretty_rfail, CheckPrettyRFail { compiler: Compiler<'a> }),
121             (check_pretty_rfail_full, CheckPrettyRFailFull { compiler: Compiler<'a> }),
122             (check_pretty_rpass_valgrind, CheckPrettyRPassValgrind { compiler: Compiler<'a> }),
123             (check_codegen, CheckCodegen { compiler: Compiler<'a> }),
124             (check_codegen_units, CheckCodegenUnits { compiler: Compiler<'a> }),
125             (check_incremental, CheckIncremental { compiler: Compiler<'a> }),
126             (check_ui, CheckUi { compiler: Compiler<'a> }),
127             (check_debuginfo, CheckDebuginfo { compiler: Compiler<'a> }),
128             (check_rustdoc, CheckRustdoc { compiler: Compiler<'a> }),
129             (check_docs, CheckDocs { compiler: Compiler<'a> }),
130             (check_error_index, CheckErrorIndex { compiler: Compiler<'a> }),
131             (check_rmake, CheckRMake { compiler: Compiler<'a> }),
132             (check_crate_std, CheckCrateStd { compiler: Compiler<'a> }),
133             (check_crate_test, CheckCrateTest { compiler: Compiler<'a> }),
134             (check_crate_rustc, CheckCrateRustc { compiler: Compiler<'a> }),
135
136             // Distribution targets, creating tarballs
137             (dist, Dist { stage: u32 }),
138             (dist_docs, DistDocs { stage: u32 }),
139             (dist_mingw, DistMingw { _dummy: () }),
140             (dist_rustc, DistRustc { stage: u32 }),
141             (dist_std, DistStd { compiler: Compiler<'a> }),
142
143             // Misc targets
144             (android_copy_libs, AndroidCopyLibs { compiler: Compiler<'a> }),
145         }
146     }
147 }
148
149 // Define the `Source` enum by iterating over all the steps and peeling out just
150 // the types that we want to define.
151
152 macro_rules! item { ($a:item) => ($a) }
153
154 macro_rules! define_source {
155     ($(($short:ident, $name:ident { $($args:tt)* }),)*) => {
156         item! {
157             #[derive(Hash, Eq, PartialEq, Clone, Debug)]
158             pub enum Source<'a> {
159                 $($name { $($args)* }),*
160             }
161         }
162     }
163 }
164
165 targets!(define_source);
166
167 /// Calculate a list of all steps described by `build`.
168 ///
169 /// This will inspect the flags passed in on the command line and use that to
170 /// build up a list of steps to execute. These steps will then be transformed
171 /// into a topologically sorted list which when executed left-to-right will
172 /// correctly sequence the entire build.
173 pub fn all(build: &Build) -> Vec<Step> {
174     let mut ret = Vec::new();
175     let mut all = HashSet::new();
176     for target in top_level(build) {
177         fill(build, &target, &mut ret, &mut all);
178     }
179     return ret;
180
181     fn fill<'a>(build: &'a Build,
182                 target: &Step<'a>,
183                 ret: &mut Vec<Step<'a>>,
184                 set: &mut HashSet<Step<'a>>) {
185         if set.insert(target.clone()) {
186             for dep in target.deps(build) {
187                 fill(build, &dep, ret, set);
188             }
189             ret.push(target.clone());
190         }
191     }
192 }
193
194 /// Determines what top-level targets are requested as part of this build,
195 /// returning them as a list.
196 fn top_level(build: &Build) -> Vec<Step> {
197     let mut targets = Vec::new();
198     let stage = build.flags.stage.unwrap_or(2);
199
200     let host = Step {
201         src: Source::Llvm { _dummy: () },
202         target: build.flags.host.iter().next()
203                      .unwrap_or(&build.config.build),
204     };
205     let target = Step {
206         src: Source::Llvm { _dummy: () },
207         target: build.flags.target.iter().next().map(|x| &x[..])
208                      .unwrap_or(host.target)
209     };
210
211     // First, try to find steps on the command line.
212     add_steps(build, stage, &host, &target, &mut targets);
213
214     // If none are specified, then build everything.
215     if targets.len() == 0 {
216         let t = Step {
217             src: Source::Llvm { _dummy: () },
218             target: &build.config.build,
219         };
220         if build.config.docs {
221           targets.push(t.doc(stage));
222         }
223         for host in build.config.host.iter() {
224             if !build.flags.host.contains(host) {
225                 continue
226             }
227             let host = t.target(host);
228             if host.target == build.config.build {
229                 targets.push(host.librustc(host.compiler(stage)));
230             } else {
231                 targets.push(host.librustc_link(t.compiler(stage), host.target));
232             }
233             for target in build.config.target.iter() {
234                 if !build.flags.target.contains(target) {
235                     continue
236                 }
237
238                 if host.target == build.config.build {
239                     targets.push(host.target(target)
240                                      .libtest(host.compiler(stage)));
241                 } else {
242                     targets.push(host.target(target)
243                                      .libtest_link(t.compiler(stage), host.target));
244                 }
245             }
246         }
247     }
248
249     return targets
250
251 }
252
253 fn add_steps<'a>(build: &'a Build,
254                  stage: u32,
255                  host: &Step<'a>,
256                  target: &Step<'a>,
257                  targets: &mut Vec<Step<'a>>) {
258     struct Context<'a> {
259         stage: u32,
260         compiler: Compiler<'a>,
261         _dummy: (),
262         host: &'a str,
263     }
264     for step in build.flags.step.iter() {
265
266         // The macro below insists on hygienic access to all local variables, so
267         // we shove them all in a struct and subvert hygiene by accessing struct
268         // fields instead,
269         let cx = Context {
270             stage: stage,
271             compiler: host.target(&build.config.build).compiler(stage),
272             _dummy: (),
273             host: host.target,
274         };
275         macro_rules! add_step {
276             ($(($short:ident, $name:ident { $($arg:ident: $t:ty),* }),)*) => ({$(
277                 let name = stringify!($short).replace("_", "-");
278                 if &step[..] == &name[..] {
279                     targets.push(target.$short($(cx.$arg),*));
280                     continue
281                 }
282                 drop(name);
283             )*})
284         }
285
286         targets!(add_step);
287
288         panic!("unknown step: {}", step);
289     }
290 }
291
292 macro_rules! constructors {
293     ($(($short:ident, $name:ident { $($arg:ident: $t:ty),* }),)*) => {$(
294         fn $short(&self, $($arg: $t),*) -> Step<'a> {
295             Step {
296                 src: Source::$name { $($arg: $arg),* },
297                 target: self.target,
298             }
299         }
300     )*}
301 }
302
303 impl<'a> Step<'a> {
304     fn compiler(&self, stage: u32) -> Compiler<'a> {
305         Compiler::new(stage, self.target)
306     }
307
308     fn target(&self, target: &'a str) -> Step<'a> {
309         Step { target: target, src: self.src.clone() }
310     }
311
312     // Define ergonomic constructors for each step defined above so they can be
313     // easily constructed.
314     targets!(constructors);
315
316     /// Mapping of all dependencies for rustbuild.
317     ///
318     /// This function receives a step, the build that we're building for, and
319     /// then returns a list of all the dependencies of that step.
320     pub fn deps(&self, build: &'a Build) -> Vec<Step<'a>> {
321         match self.src {
322             Source::Rustc { stage: 0 } => {
323                 Vec::new()
324             }
325             Source::Rustc { stage } => {
326                 let compiler = Compiler::new(stage - 1, &build.config.build);
327                 vec![self.librustc(compiler)]
328             }
329             Source::Librustc { compiler } => {
330                 vec![self.libtest(compiler), self.llvm(())]
331             }
332             Source::Libtest { compiler } => {
333                 vec![self.libstd(compiler)]
334             }
335             Source::Libstd { compiler } => {
336                 vec![self.compiler_rt(()),
337                      self.rustc(compiler.stage).target(compiler.host)]
338             }
339             Source::LibrustcLink { compiler, host } => {
340                 vec![self.librustc(compiler),
341                      self.libtest_link(compiler, host)]
342             }
343             Source::LibtestLink { compiler, host } => {
344                 vec![self.libtest(compiler), self.libstd_link(compiler, host)]
345             }
346             Source::LibstdLink { compiler, host } => {
347                 vec![self.libstd(compiler),
348                      self.target(host).rustc(compiler.stage)]
349             }
350             Source::CompilerRt { _dummy } => {
351                 vec![self.llvm(()).target(&build.config.build)]
352             }
353             Source::Llvm { _dummy } => Vec::new(),
354             Source::TestHelpers { _dummy } => Vec::new(),
355             Source::DebuggerScripts { stage: _ } => Vec::new(),
356
357             // Note that all doc targets depend on artifacts from the build
358             // architecture, not the target (which is where we're generating
359             // docs into).
360             Source::DocStd { stage } => {
361                 let compiler = self.target(&build.config.build).compiler(stage);
362                 vec![self.libstd(compiler)]
363             }
364             Source::DocTest { stage } => {
365                 let compiler = self.target(&build.config.build).compiler(stage);
366                 vec![self.libtest(compiler)]
367             }
368             Source::DocBook { stage } |
369             Source::DocNomicon { stage } |
370             Source::DocStyle { stage } => {
371                 vec![self.target(&build.config.build).tool_rustbook(stage)]
372             }
373             Source::DocErrorIndex { stage } => {
374                 vec![self.target(&build.config.build).tool_error_index(stage)]
375             }
376             Source::DocStandalone { stage } => {
377                 vec![self.target(&build.config.build).rustc(stage)]
378             }
379             Source::DocRustc { stage } => {
380                 vec![self.doc_test(stage)]
381             }
382             Source::Doc { stage } => {
383                 vec![self.doc_book(stage), self.doc_nomicon(stage),
384                      self.doc_style(stage), self.doc_standalone(stage),
385                      self.doc_std(stage),
386                      self.doc_error_index(stage)]
387             }
388             Source::Check { stage, compiler } => {
389                 // Check is just a pseudo step which means check all targets,
390                 // so just depend on checking all targets.
391                 build.config.target.iter().map(|t| {
392                     self.target(t).check_target(stage, compiler)
393                 }).collect()
394             }
395             Source::CheckTarget { stage, compiler } => {
396                 // CheckTarget here means run all possible test suites for this
397                 // target. Most of the time, however, we can't actually run
398                 // anything if we're not the build triple as we could be cross
399                 // compiling.
400                 //
401                 // As a result, the base set of targets here is quite stripped
402                 // down from the standard set of targets. These suites have
403                 // their own internal logic to run in cross-compiled situations
404                 // if they'll run at all. For example compiletest knows that
405                 // when testing Android targets we ship artifacts to the
406                 // emulator.
407                 //
408                 // When in doubt the rule of thumb for adding to this list is
409                 // "should this test suite run on the android bot?"
410                 let mut base = vec![
411                     self.check_rpass(compiler),
412                     self.check_rfail(compiler),
413                     self.check_crate_std(compiler),
414                     self.check_crate_test(compiler),
415                     self.check_debuginfo(compiler),
416                     self.dist(stage),
417                 ];
418
419                 // If we're testing the build triple, then we know we can
420                 // actually run binaries and such, so we run all possible tests
421                 // that we know about.
422                 if self.target == build.config.build {
423                     base.extend(vec![
424                         // docs-related
425                         self.check_docs(compiler),
426                         self.check_error_index(compiler),
427                         self.check_rustdoc(compiler),
428
429                         // UI-related
430                         self.check_cfail(compiler),
431                         self.check_pfail(compiler),
432                         self.check_ui(compiler),
433
434                         // codegen-related
435                         self.check_incremental(compiler),
436                         self.check_codegen(compiler),
437                         self.check_codegen_units(compiler),
438
439                         // misc compiletest-test suites
440                         self.check_rpass_full(compiler),
441                         self.check_rfail_full(compiler),
442                         self.check_cfail_full(compiler),
443                         self.check_pretty_rpass_full(compiler),
444                         self.check_pretty_rfail_full(compiler),
445                         self.check_rpass_valgrind(compiler),
446                         self.check_rmake(compiler),
447
448                         // crates
449                         self.check_crate_rustc(compiler),
450
451                         // pretty
452                         self.check_pretty(compiler),
453                         self.check_pretty_rpass(compiler),
454                         self.check_pretty_rfail(compiler),
455                         self.check_pretty_rpass_valgrind(compiler),
456
457                         // misc
458                         self.check_linkcheck(stage),
459                         self.check_tidy(stage),
460                     ]);
461                 }
462                 return base
463             }
464             Source::CheckLinkcheck { stage } => {
465                 vec![self.tool_linkchecker(stage), self.doc(stage)]
466             }
467             Source::CheckCargoTest { stage } => {
468                 vec![self.tool_cargotest(stage),
469                      self.librustc(self.compiler(stage))]
470             }
471             Source::CheckTidy { stage } => {
472                 vec![self.tool_tidy(stage)]
473             }
474             Source::CheckPrettyRPass { compiler } |
475             Source::CheckPrettyRFail { compiler } |
476             Source::CheckRFail { compiler } |
477             Source::CheckPFail { compiler } |
478             Source::CheckCodegen { compiler } |
479             Source::CheckCodegenUnits { compiler } |
480             Source::CheckIncremental { compiler } |
481             Source::CheckUi { compiler } |
482             Source::CheckRustdoc { compiler } |
483             Source::CheckPretty { compiler } |
484             Source::CheckCFail { compiler } |
485             Source::CheckRPassValgrind { compiler } |
486             Source::CheckRPass { compiler } => {
487                 let mut base = vec![
488                     self.libtest(compiler),
489                     self.target(compiler.host).tool_compiletest(compiler.stage),
490                     self.test_helpers(()),
491                 ];
492                 if self.target.contains("android") {
493                     base.push(self.android_copy_libs(compiler));
494                 }
495                 base
496             }
497             Source::CheckDebuginfo { compiler } => {
498                 vec![
499                     self.libtest(compiler),
500                     self.target(compiler.host).tool_compiletest(compiler.stage),
501                     self.test_helpers(()),
502                     self.debugger_scripts(compiler.stage),
503                 ]
504             }
505             Source::CheckRPassFull { compiler } |
506             Source::CheckRFailFull { compiler } |
507             Source::CheckCFailFull { compiler } |
508             Source::CheckPrettyRPassFull { compiler } |
509             Source::CheckPrettyRFailFull { compiler } |
510             Source::CheckPrettyRPassValgrind { compiler } |
511             Source::CheckRMake { compiler } => {
512                 vec![self.librustc(compiler),
513                      self.target(compiler.host).tool_compiletest(compiler.stage)]
514             }
515             Source::CheckDocs { compiler } => {
516                 vec![self.libstd(compiler)]
517             }
518             Source::CheckErrorIndex { compiler } => {
519                 vec![self.libstd(compiler),
520                      self.target(compiler.host).tool_error_index(compiler.stage)]
521             }
522             Source::CheckCrateStd { compiler } => {
523                 vec![self.libtest(compiler)]
524             }
525             Source::CheckCrateTest { compiler } => {
526                 vec![self.libtest(compiler)]
527             }
528             Source::CheckCrateRustc { compiler } => {
529                 vec![self.libtest(compiler)]
530             }
531
532             Source::ToolLinkchecker { stage } |
533             Source::ToolTidy { stage } => {
534                 vec![self.libstd(self.compiler(stage))]
535             }
536             Source::ToolErrorIndex { stage } |
537             Source::ToolRustbook { stage } => {
538                 vec![self.librustc(self.compiler(stage))]
539             }
540             Source::ToolCargoTest { stage } => {
541                 vec![self.libstd(self.compiler(stage))]
542             }
543             Source::ToolCompiletest { stage } => {
544                 vec![self.libtest(self.compiler(stage))]
545             }
546
547             Source::DistDocs { stage } => vec![self.doc(stage)],
548             Source::DistMingw { _dummy: _ } => Vec::new(),
549             Source::DistRustc { stage } => {
550                 vec![self.rustc(stage)]
551             }
552             Source::DistStd { compiler } => {
553                 // We want to package up as many target libraries as possible
554                 // for the `rust-std` package, so if this is a host target we
555                 // depend on librustc and otherwise we just depend on libtest.
556                 if build.config.host.iter().any(|t| t == self.target) {
557                     vec![self.librustc(compiler)]
558                 } else {
559                     vec![self.libtest(compiler)]
560                 }
561             }
562
563             Source::Dist { stage } => {
564                 let mut base = Vec::new();
565
566                 for host in build.config.host.iter() {
567                     let host = self.target(host);
568                     base.push(host.dist_rustc(stage));
569                     if host.target.contains("windows-gnu") {
570                         base.push(host.dist_mingw(()));
571                     }
572
573                     let compiler = self.compiler(stage);
574                     for target in build.config.target.iter() {
575                         let target = self.target(target);
576                         if build.config.docs {
577                             base.push(target.dist_docs(stage));
578                         }
579                         base.push(target.dist_std(compiler));
580                     }
581                 }
582                 return base
583             }
584
585             Source::AndroidCopyLibs { compiler } => {
586                 vec![self.libtest(compiler)]
587             }
588         }
589     }
590 }