]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/step.rs
Rollup merge of #34916 - tbu-:pr_comment_on_seek_cast, r=GuillaumeGomez
[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_mir_opt, CheckMirOpt { compiler: Compiler<'a> }),
128             (check_debuginfo, CheckDebuginfo { compiler: Compiler<'a> }),
129             (check_rustdoc, CheckRustdoc { compiler: Compiler<'a> }),
130             (check_docs, CheckDocs { compiler: Compiler<'a> }),
131             (check_error_index, CheckErrorIndex { compiler: Compiler<'a> }),
132             (check_rmake, CheckRMake { compiler: Compiler<'a> }),
133             (check_crate_std, CheckCrateStd { compiler: Compiler<'a> }),
134             (check_crate_test, CheckCrateTest { compiler: Compiler<'a> }),
135             (check_crate_rustc, CheckCrateRustc { compiler: Compiler<'a> }),
136
137             // Distribution targets, creating tarballs
138             (dist, Dist { stage: u32 }),
139             (dist_docs, DistDocs { stage: u32 }),
140             (dist_mingw, DistMingw { _dummy: () }),
141             (dist_rustc, DistRustc { stage: u32 }),
142             (dist_std, DistStd { compiler: Compiler<'a> }),
143
144             // Misc targets
145             (android_copy_libs, AndroidCopyLibs { compiler: Compiler<'a> }),
146         }
147     }
148 }
149
150 // Define the `Source` enum by iterating over all the steps and peeling out just
151 // the types that we want to define.
152
153 macro_rules! item { ($a:item) => ($a) }
154
155 macro_rules! define_source {
156     ($(($short:ident, $name:ident { $($args:tt)* }),)*) => {
157         item! {
158             #[derive(Hash, Eq, PartialEq, Clone, Debug)]
159             pub enum Source<'a> {
160                 $($name { $($args)* }),*
161             }
162         }
163     }
164 }
165
166 targets!(define_source);
167
168 /// Calculate a list of all steps described by `build`.
169 ///
170 /// This will inspect the flags passed in on the command line and use that to
171 /// build up a list of steps to execute. These steps will then be transformed
172 /// into a topologically sorted list which when executed left-to-right will
173 /// correctly sequence the entire build.
174 pub fn all(build: &Build) -> Vec<Step> {
175     let mut ret = Vec::new();
176     let mut all = HashSet::new();
177     for target in top_level(build) {
178         fill(build, &target, &mut ret, &mut all);
179     }
180     return ret;
181
182     fn fill<'a>(build: &'a Build,
183                 target: &Step<'a>,
184                 ret: &mut Vec<Step<'a>>,
185                 set: &mut HashSet<Step<'a>>) {
186         if set.insert(target.clone()) {
187             for dep in target.deps(build) {
188                 fill(build, &dep, ret, set);
189             }
190             ret.push(target.clone());
191         }
192     }
193 }
194
195 /// Determines what top-level targets are requested as part of this build,
196 /// returning them as a list.
197 fn top_level(build: &Build) -> Vec<Step> {
198     let mut targets = Vec::new();
199     let stage = build.flags.stage.unwrap_or(2);
200
201     let host = Step {
202         src: Source::Llvm { _dummy: () },
203         target: build.flags.host.iter().next()
204                      .unwrap_or(&build.config.build),
205     };
206     let target = Step {
207         src: Source::Llvm { _dummy: () },
208         target: build.flags.target.iter().next().map(|x| &x[..])
209                      .unwrap_or(host.target)
210     };
211
212     // First, try to find steps on the command line.
213     add_steps(build, stage, &host, &target, &mut targets);
214
215     // If none are specified, then build everything.
216     if targets.len() == 0 {
217         let t = Step {
218             src: Source::Llvm { _dummy: () },
219             target: &build.config.build,
220         };
221         if build.config.docs {
222           targets.push(t.doc(stage));
223         }
224         for host in build.config.host.iter() {
225             if !build.flags.host.contains(host) {
226                 continue
227             }
228             let host = t.target(host);
229             if host.target == build.config.build {
230                 targets.push(host.librustc(host.compiler(stage)));
231             } else {
232                 targets.push(host.librustc_link(t.compiler(stage), host.target));
233             }
234             for target in build.config.target.iter() {
235                 if !build.flags.target.contains(target) {
236                     continue
237                 }
238
239                 if host.target == build.config.build {
240                     targets.push(host.target(target)
241                                      .libtest(host.compiler(stage)));
242                 } else {
243                     targets.push(host.target(target)
244                                      .libtest_link(t.compiler(stage), host.target));
245                 }
246             }
247         }
248     }
249
250     return targets
251
252 }
253
254 fn add_steps<'a>(build: &'a Build,
255                  stage: u32,
256                  host: &Step<'a>,
257                  target: &Step<'a>,
258                  targets: &mut Vec<Step<'a>>) {
259     struct Context<'a> {
260         stage: u32,
261         compiler: Compiler<'a>,
262         _dummy: (),
263         host: &'a str,
264     }
265     for step in build.flags.step.iter() {
266
267         // The macro below insists on hygienic access to all local variables, so
268         // we shove them all in a struct and subvert hygiene by accessing struct
269         // fields instead,
270         let cx = Context {
271             stage: stage,
272             compiler: host.target(&build.config.build).compiler(stage),
273             _dummy: (),
274             host: host.target,
275         };
276         macro_rules! add_step {
277             ($(($short:ident, $name:ident { $($arg:ident: $t:ty),* }),)*) => ({$(
278                 let name = stringify!($short).replace("_", "-");
279                 if &step[..] == &name[..] {
280                     targets.push(target.$short($(cx.$arg),*));
281                     continue
282                 }
283                 drop(name);
284             )*})
285         }
286
287         targets!(add_step);
288
289         panic!("unknown step: {}", step);
290     }
291 }
292
293 macro_rules! constructors {
294     ($(($short:ident, $name:ident { $($arg:ident: $t:ty),* }),)*) => {$(
295         fn $short(&self, $($arg: $t),*) -> Step<'a> {
296             Step {
297                 src: Source::$name { $($arg: $arg),* },
298                 target: self.target,
299             }
300         }
301     )*}
302 }
303
304 impl<'a> Step<'a> {
305     fn compiler(&self, stage: u32) -> Compiler<'a> {
306         Compiler::new(stage, self.target)
307     }
308
309     fn target(&self, target: &'a str) -> Step<'a> {
310         Step { target: target, src: self.src.clone() }
311     }
312
313     // Define ergonomic constructors for each step defined above so they can be
314     // easily constructed.
315     targets!(constructors);
316
317     /// Mapping of all dependencies for rustbuild.
318     ///
319     /// This function receives a step, the build that we're building for, and
320     /// then returns a list of all the dependencies of that step.
321     pub fn deps(&self, build: &'a Build) -> Vec<Step<'a>> {
322         match self.src {
323             Source::Rustc { stage: 0 } => {
324                 Vec::new()
325             }
326             Source::Rustc { stage } => {
327                 let compiler = Compiler::new(stage - 1, &build.config.build);
328                 vec![self.librustc(compiler)]
329             }
330             Source::Librustc { compiler } => {
331                 vec![self.libtest(compiler), self.llvm(())]
332             }
333             Source::Libtest { compiler } => {
334                 vec![self.libstd(compiler)]
335             }
336             Source::Libstd { compiler } => {
337                 vec![self.compiler_rt(()),
338                      self.rustc(compiler.stage).target(compiler.host)]
339             }
340             Source::LibrustcLink { compiler, host } => {
341                 vec![self.librustc(compiler),
342                      self.libtest_link(compiler, host)]
343             }
344             Source::LibtestLink { compiler, host } => {
345                 vec![self.libtest(compiler), self.libstd_link(compiler, host)]
346             }
347             Source::LibstdLink { compiler, host } => {
348                 vec![self.libstd(compiler),
349                      self.target(host).rustc(compiler.stage)]
350             }
351             Source::CompilerRt { _dummy } => Vec::new(),
352             Source::Llvm { _dummy } => Vec::new(),
353             Source::TestHelpers { _dummy } => Vec::new(),
354             Source::DebuggerScripts { stage: _ } => Vec::new(),
355
356             // Note that all doc targets depend on artifacts from the build
357             // architecture, not the target (which is where we're generating
358             // docs into).
359             Source::DocStd { stage } => {
360                 let compiler = self.target(&build.config.build).compiler(stage);
361                 vec![self.libstd(compiler)]
362             }
363             Source::DocTest { stage } => {
364                 let compiler = self.target(&build.config.build).compiler(stage);
365                 vec![self.libtest(compiler)]
366             }
367             Source::DocBook { stage } |
368             Source::DocNomicon { stage } |
369             Source::DocStyle { stage } => {
370                 vec![self.target(&build.config.build).tool_rustbook(stage)]
371             }
372             Source::DocErrorIndex { stage } => {
373                 vec![self.target(&build.config.build).tool_error_index(stage)]
374             }
375             Source::DocStandalone { stage } => {
376                 vec![self.target(&build.config.build).rustc(stage)]
377             }
378             Source::DocRustc { stage } => {
379                 vec![self.doc_test(stage)]
380             }
381             Source::Doc { stage } => {
382                 let mut deps = vec![
383                     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
389                 if build.config.compiler_docs {
390                     deps.push(self.doc_rustc(stage));
391                 }
392
393                 deps
394             }
395             Source::Check { stage, compiler } => {
396                 // Check is just a pseudo step which means check all targets,
397                 // so just depend on checking all targets.
398                 build.config.target.iter().map(|t| {
399                     self.target(t).check_target(stage, compiler)
400                 }).collect()
401             }
402             Source::CheckTarget { stage, compiler } => {
403                 // CheckTarget here means run all possible test suites for this
404                 // target. Most of the time, however, we can't actually run
405                 // anything if we're not the build triple as we could be cross
406                 // compiling.
407                 //
408                 // As a result, the base set of targets here is quite stripped
409                 // down from the standard set of targets. These suites have
410                 // their own internal logic to run in cross-compiled situations
411                 // if they'll run at all. For example compiletest knows that
412                 // when testing Android targets we ship artifacts to the
413                 // emulator.
414                 //
415                 // When in doubt the rule of thumb for adding to this list is
416                 // "should this test suite run on the android bot?"
417                 let mut base = vec![
418                     self.check_rpass(compiler),
419                     self.check_rfail(compiler),
420                     self.check_crate_std(compiler),
421                     self.check_crate_test(compiler),
422                     self.check_debuginfo(compiler),
423                     self.dist(stage),
424                 ];
425
426                 // If we're testing the build triple, then we know we can
427                 // actually run binaries and such, so we run all possible tests
428                 // that we know about.
429                 if self.target == build.config.build {
430                     base.extend(vec![
431                         // docs-related
432                         self.check_docs(compiler),
433                         self.check_error_index(compiler),
434                         self.check_rustdoc(compiler),
435
436                         // UI-related
437                         self.check_cfail(compiler),
438                         self.check_pfail(compiler),
439                         self.check_ui(compiler),
440
441                         // codegen-related
442                         self.check_incremental(compiler),
443                         self.check_codegen(compiler),
444                         self.check_codegen_units(compiler),
445
446                         // misc compiletest-test suites
447                         self.check_rpass_full(compiler),
448                         self.check_rfail_full(compiler),
449                         self.check_cfail_full(compiler),
450                         self.check_pretty_rpass_full(compiler),
451                         self.check_pretty_rfail_full(compiler),
452                         self.check_rpass_valgrind(compiler),
453                         self.check_rmake(compiler),
454                         self.check_mir_opt(compiler),
455
456                         // crates
457                         self.check_crate_rustc(compiler),
458
459                         // pretty
460                         self.check_pretty(compiler),
461                         self.check_pretty_rpass(compiler),
462                         self.check_pretty_rfail(compiler),
463                         self.check_pretty_rpass_valgrind(compiler),
464
465                         // misc
466                         self.check_linkcheck(stage),
467                         self.check_tidy(stage),
468                     ]);
469                 }
470                 return base
471             }
472             Source::CheckLinkcheck { stage } => {
473                 vec![self.tool_linkchecker(stage), self.doc(stage)]
474             }
475             Source::CheckCargoTest { stage } => {
476                 vec![self.tool_cargotest(stage),
477                      self.librustc(self.compiler(stage))]
478             }
479             Source::CheckTidy { stage } => {
480                 vec![self.tool_tidy(stage)]
481             }
482             Source::CheckMirOpt { compiler} |
483             Source::CheckPrettyRPass { compiler } |
484             Source::CheckPrettyRFail { compiler } |
485             Source::CheckRFail { compiler } |
486             Source::CheckPFail { compiler } |
487             Source::CheckCodegen { compiler } |
488             Source::CheckCodegenUnits { compiler } |
489             Source::CheckIncremental { compiler } |
490             Source::CheckUi { compiler } |
491             Source::CheckRustdoc { compiler } |
492             Source::CheckPretty { compiler } |
493             Source::CheckCFail { compiler } |
494             Source::CheckRPassValgrind { compiler } |
495             Source::CheckRPass { compiler } => {
496                 let mut base = vec![
497                     self.libtest(compiler),
498                     self.target(compiler.host).tool_compiletest(compiler.stage),
499                     self.test_helpers(()),
500                 ];
501                 if self.target.contains("android") {
502                     base.push(self.android_copy_libs(compiler));
503                 }
504                 base
505             }
506             Source::CheckDebuginfo { compiler } => {
507                 vec![
508                     self.libtest(compiler),
509                     self.target(compiler.host).tool_compiletest(compiler.stage),
510                     self.test_helpers(()),
511                     self.debugger_scripts(compiler.stage),
512                 ]
513             }
514             Source::CheckRPassFull { compiler } |
515             Source::CheckRFailFull { compiler } |
516             Source::CheckCFailFull { compiler } |
517             Source::CheckPrettyRPassFull { compiler } |
518             Source::CheckPrettyRFailFull { compiler } |
519             Source::CheckPrettyRPassValgrind { compiler } |
520             Source::CheckRMake { compiler } => {
521                 vec![self.librustc(compiler),
522                      self.target(compiler.host).tool_compiletest(compiler.stage)]
523             }
524             Source::CheckDocs { compiler } => {
525                 vec![self.libstd(compiler)]
526             }
527             Source::CheckErrorIndex { compiler } => {
528                 vec![self.libstd(compiler),
529                      self.target(compiler.host).tool_error_index(compiler.stage)]
530             }
531             Source::CheckCrateStd { compiler } => {
532                 vec![self.libtest(compiler)]
533             }
534             Source::CheckCrateTest { compiler } => {
535                 vec![self.libtest(compiler)]
536             }
537             Source::CheckCrateRustc { compiler } => {
538                 vec![self.libtest(compiler)]
539             }
540
541             Source::ToolLinkchecker { stage } |
542             Source::ToolTidy { stage } => {
543                 vec![self.libstd(self.compiler(stage))]
544             }
545             Source::ToolErrorIndex { stage } |
546             Source::ToolRustbook { stage } => {
547                 vec![self.librustc(self.compiler(stage))]
548             }
549             Source::ToolCargoTest { stage } => {
550                 vec![self.libstd(self.compiler(stage))]
551             }
552             Source::ToolCompiletest { stage } => {
553                 vec![self.libtest(self.compiler(stage))]
554             }
555
556             Source::DistDocs { stage } => vec![self.doc(stage)],
557             Source::DistMingw { _dummy: _ } => Vec::new(),
558             Source::DistRustc { stage } => {
559                 vec![self.rustc(stage)]
560             }
561             Source::DistStd { compiler } => {
562                 // We want to package up as many target libraries as possible
563                 // for the `rust-std` package, so if this is a host target we
564                 // depend on librustc and otherwise we just depend on libtest.
565                 if build.config.host.iter().any(|t| t == self.target) {
566                     vec![self.librustc(compiler)]
567                 } else {
568                     vec![self.libtest(compiler)]
569                 }
570             }
571
572             Source::Dist { stage } => {
573                 let mut base = Vec::new();
574
575                 for host in build.config.host.iter() {
576                     let host = self.target(host);
577                     base.push(host.dist_rustc(stage));
578                     if host.target.contains("windows-gnu") {
579                         base.push(host.dist_mingw(()));
580                     }
581
582                     let compiler = self.compiler(stage);
583                     for target in build.config.target.iter() {
584                         let target = self.target(target);
585                         if build.config.docs {
586                             base.push(target.dist_docs(stage));
587                         }
588                         base.push(target.dist_std(compiler));
589                     }
590                 }
591                 return base
592             }
593
594             Source::AndroidCopyLibs { compiler } => {
595                 vec![self.libtest(compiler)]
596             }
597         }
598     }
599 }