forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep.rs
1558 lines (1423 loc) · 62.4 KB
/
step.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Definition of steps of the build system.
//!
//! This is where some of the real meat of rustbuild is located, in how we
//! define targets and the dependencies amongst them. This file can sort of be
//! viewed as just defining targets in a makefile which shell out to predefined
//! functions elsewhere about how to execute the target.
//!
//! The primary function here you're likely interested in is the `build_rules`
//! function. This will create a `Rules` structure which basically just lists
//! everything that rustbuild can do. Each rule has a human-readable name, a
//! path associated with it, some dependencies, and then a closure of how to
//! actually perform the rule.
//!
//! All steps below are defined in self-contained units, so adding a new target
//! to the build system should just involve adding the meta information here
//! along with the actual implementation elsewhere. You can find more comments
//! about how to define rules themselves below.
use std::collections::{BTreeMap, HashSet};
use std::mem;
use check::{self, TestKind};
use compile;
use dist;
use doc;
use flags::Subcommand;
use install;
use native;
use {Compiler, Build, Mode};
pub fn run(build: &Build) {
let rules = build_rules(build);
let steps = rules.plan();
rules.run(&steps);
}
pub fn build_rules<'a>(build: &'a Build) -> Rules {
let mut rules = Rules::new(build);
// This is the first rule that we're going to define for rustbuild, which is
// used to compile LLVM itself. All rules are added through the `rules`
// structure created above and are configured through a builder-style
// interface.
//
// First up we see the `build` method. This represents a rule that's part of
// the top-level `build` subcommand. For example `./x.py build` is what this
// is associating with. Note that this is normally only relevant if you flag
// a rule as `default`, which we'll talk about later.
//
// Next up we'll see two arguments to this method:
//
// * `llvm` - this is the "human readable" name of this target. This name is
// not accessed anywhere outside this file itself (e.g. not in
// the CLI nor elsewhere in rustbuild). The purpose of this is to
// easily define dependencies between rules. That is, other rules
// will depend on this with the name "llvm".
// * `src/llvm` - this is the relevant path to the rule that we're working
// with. This path is the engine behind how commands like
// `./x.py build src/llvm` work. This should typically point
// to the relevant component, but if there's not really a
// path to be assigned here you can pass something like
// `path/to/nowhere` to ignore it.
//
// After we create the rule with the `build` method we can then configure
// various aspects of it. For example this LLVM rule uses `.host(true)` to
// flag that it's a rule only for host targets. In other words, LLVM isn't
// compiled for targets configured through `--target` (e.g. those we're just
// building a standard library for).
//
// Next up the `dep` method will add a dependency to this rule. The closure
// is yielded the step that represents executing the `llvm` rule itself
// (containing information like stage, host, target, ...) and then it must
// return a target that the step depends on. Here LLVM is actually
// interesting where a cross-compiled LLVM depends on the host LLVM, but
// otherwise it has no dependencies.
//
// To handle this we do a bit of dynamic dispatch to see what the dependency
// is. If we're building a LLVM for the build triple, then we don't actually
// have any dependencies! To do that we return a dependency on the `Step::noop()`
// target which does nothing.
//
// If we're build a cross-compiled LLVM, however, we need to assemble the
// libraries from the previous compiler. This step has the same name as
// ours (llvm) but we want it for a different target, so we use the
// builder-style methods on `Step` to configure this target to the build
// triple.
//
// Finally, to finish off this rule, we define how to actually execute it.
// That logic is all defined in the `native` module so we just delegate to
// the relevant function there. The argument to the closure passed to `run`
// is a `Step` (defined below) which encapsulates information like the
// stage, target, host, etc.
rules.build("llvm", "src/llvm")
.host(true)
.dep(move |s| {
if s.target == build.config.build {
Step::noop()
} else {
s.target(&build.config.build)
}
})
.run(move |s| native::llvm(build, s.target));
// Ok! After that example rule that's hopefully enough to explain what's
// going on here. You can check out the API docs below and also see a bunch
// more examples of rules directly below as well.
// the compiler with no target libraries ready to go
rules.build("rustc", "src/rustc")
.dep(|s| s.name("create-sysroot").target(s.host))
.dep(move |s| {
if s.stage == 0 {
Step::noop()
} else {
s.name("librustc")
.host(&build.config.build)
.stage(s.stage - 1)
}
})
.run(move |s| compile::assemble_rustc(build, s.stage, s.target));
// Helper for loading an entire DAG of crates, rooted at `name`
let krates = |name: &str| {
let mut ret = Vec::new();
let mut list = vec![name];
let mut visited = HashSet::new();
while let Some(krate) = list.pop() {
let default = krate == name;
let krate = &build.crates[krate];
let path = krate.path.strip_prefix(&build.src).unwrap();
ret.push((krate, path.to_str().unwrap(), default));
for dep in krate.deps.iter() {
if visited.insert(dep) && dep != "build_helper" {
list.push(dep);
}
}
}
return ret
};
// ========================================================================
// Crate compilations
//
// Tools used during the build system but not shipped
rules.build("create-sysroot", "path/to/nowhere")
.run(move |s| compile::create_sysroot(build, &s.compiler()));
// These rules are "pseudo rules" that don't actually do any work
// themselves, but represent a complete sysroot with the relevant compiler
// linked into place.
//
// That is, depending on "libstd" means that when the rule is completed then
// the `stage` sysroot for the compiler `host` will be available with a
// standard library built for `target` linked in place. Not all rules need
// the compiler itself to be available, just the standard library, so
// there's a distinction between the two.
rules.build("libstd", "src/libstd")
.dep(|s| s.name("rustc").target(s.host))
.dep(|s| s.name("libstd-link"));
rules.build("libtest", "src/libtest")
.dep(|s| s.name("libstd"))
.dep(|s| s.name("libtest-link"))
.default(true);
rules.build("librustc", "src/librustc")
.dep(|s| s.name("libtest"))
.dep(|s| s.name("librustc-link"))
.host(true)
.default(true);
// Helper method to define the rules to link a crate into its place in the
// sysroot.
//
// The logic here is a little subtle as there's a few cases to consider.
// Not all combinations of (stage, host, target) actually require something
// to be compiled, but rather libraries could get propagated from a
// different location. For example:
//
// * Any crate with a `host` that's not the build triple will not actually
// compile something. A different `host` means that the build triple will
// actually compile the libraries, and then we'll copy them over from the
// build triple to the `host` directory.
//
// * Some crates aren't even compiled by the build triple, but may be copied
// from previous stages. For example if we're not doing a full bootstrap
// then we may just depend on the stage1 versions of libraries to be
// available to get linked forward.
//
// * Finally, there are some cases, however, which do indeed comiple crates
// and link them into place afterwards.
//
// The rule definition below mirrors these three cases. The `dep` method
// calculates the correct dependency which either comes from stage1, a
// different compiler, or from actually building the crate itself (the `dep`
// rule). The `run` rule then mirrors these three cases and links the cases
// forward into the compiler sysroot specified from the correct location.
fn crate_rule<'a, 'b>(build: &'a Build,
rules: &'b mut Rules<'a>,
krate: &'a str,
dep: &'a str,
link: fn(&Build, &Compiler, &Compiler, &str))
-> RuleBuilder<'a, 'b> {
let mut rule = rules.build(&krate, "path/to/nowhere");
rule.dep(move |s| {
if build.force_use_stage1(&s.compiler(), s.target) {
s.host(&build.config.build).stage(1)
} else if s.host == build.config.build {
s.name(dep)
} else {
s.host(&build.config.build)
}
})
.run(move |s| {
if build.force_use_stage1(&s.compiler(), s.target) {
link(build,
&s.stage(1).host(&build.config.build).compiler(),
&s.compiler(),
s.target)
} else if s.host == build.config.build {
link(build, &s.compiler(), &s.compiler(), s.target)
} else {
link(build,
&s.host(&build.config.build).compiler(),
&s.compiler(),
s.target)
}
});
return rule
}
// Similar to the `libstd`, `libtest`, and `librustc` rules above, except
// these rules only represent the libraries being available in the sysroot,
// not the compiler itself. This is done as not all rules need a compiler in
// the sysroot, but may just need the libraries.
//
// All of these rules use the helper definition above.
crate_rule(build,
&mut rules,
"libstd-link",
"build-crate-std",
compile::std_link)
.dep(|s| s.name("startup-objects"))
.dep(|s| s.name("create-sysroot").target(s.host));
crate_rule(build,
&mut rules,
"libtest-link",
"build-crate-test",
compile::test_link)
.dep(|s| s.name("libstd-link"));
crate_rule(build,
&mut rules,
"librustc-link",
"build-crate-rustc-main",
compile::rustc_link)
.dep(|s| s.name("libtest-link"));
for (krate, path, _default) in krates("std") {
rules.build(&krate.build_step, path)
.dep(|s| s.name("startup-objects"))
.dep(move |s| s.name("rustc").host(&build.config.build).target(s.host))
.run(move |s| compile::std(build, s.target, &s.compiler()));
}
for (krate, path, _default) in krates("test") {
rules.build(&krate.build_step, path)
.dep(|s| s.name("libstd-link"))
.run(move |s| compile::test(build, s.target, &s.compiler()));
}
for (krate, path, _default) in krates("rustc-main") {
rules.build(&krate.build_step, path)
.dep(|s| s.name("libtest-link"))
.dep(move |s| s.name("llvm").host(&build.config.build).stage(0))
.dep(|s| s.name("may-run-build-script"))
.run(move |s| compile::rustc(build, s.target, &s.compiler()));
}
// Crates which have build scripts need to rely on this rule to ensure that
// the necessary prerequisites for a build script are linked and located in
// place.
rules.build("may-run-build-script", "path/to/nowhere")
.dep(move |s| {
s.name("libstd-link")
.host(&build.config.build)
.target(&build.config.build)
});
rules.build("startup-objects", "src/rtstartup")
.dep(|s| s.name("create-sysroot").target(s.host))
.run(move |s| compile::build_startup_objects(build, &s.compiler(), s.target));
// ========================================================================
// Test targets
//
// Various unit tests and tests suites we can run
{
let mut suite = |name, path, mode, dir| {
rules.test(name, path)
.dep(|s| s.name("libtest"))
.dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
.dep(|s| s.name("test-helpers"))
.dep(|s| s.name("emulator-copy-libs"))
.default(mode != "pretty") // pretty tests don't run everywhere
.run(move |s| {
check::compiletest(build, &s.compiler(), s.target, mode, dir)
});
};
suite("check-ui", "src/test/ui", "ui", "ui");
suite("check-rpass", "src/test/run-pass", "run-pass", "run-pass");
suite("check-cfail", "src/test/compile-fail", "compile-fail", "compile-fail");
suite("check-pfail", "src/test/parse-fail", "parse-fail", "parse-fail");
suite("check-rfail", "src/test/run-fail", "run-fail", "run-fail");
suite("check-rpass-valgrind", "src/test/run-pass-valgrind",
"run-pass-valgrind", "run-pass-valgrind");
suite("check-mir-opt", "src/test/mir-opt", "mir-opt", "mir-opt");
if build.config.codegen_tests {
suite("check-codegen", "src/test/codegen", "codegen", "codegen");
}
suite("check-codegen-units", "src/test/codegen-units", "codegen-units",
"codegen-units");
suite("check-incremental", "src/test/incremental", "incremental",
"incremental");
}
if build.config.build.contains("msvc") {
// nothing to do for debuginfo tests
} else {
rules.test("check-debuginfo-lldb", "src/test/debuginfo-lldb")
.dep(|s| s.name("libtest"))
.dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
.dep(|s| s.name("test-helpers"))
.dep(|s| s.name("debugger-scripts"))
.run(move |s| check::compiletest(build, &s.compiler(), s.target,
"debuginfo-lldb", "debuginfo"));
rules.test("check-debuginfo-gdb", "src/test/debuginfo-gdb")
.dep(|s| s.name("libtest"))
.dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
.dep(|s| s.name("test-helpers"))
.dep(|s| s.name("debugger-scripts"))
.dep(|s| s.name("emulator-copy-libs"))
.run(move |s| check::compiletest(build, &s.compiler(), s.target,
"debuginfo-gdb", "debuginfo"));
let mut rule = rules.test("check-debuginfo", "src/test/debuginfo");
rule.default(true);
if build.config.build.contains("apple") {
rule.dep(|s| s.name("check-debuginfo-lldb"));
} else {
rule.dep(|s| s.name("check-debuginfo-gdb"));
}
}
rules.test("debugger-scripts", "src/etc/lldb_batchmode.py")
.run(move |s| dist::debugger_scripts(build, &build.sysroot(&s.compiler()),
s.target));
{
let mut suite = |name, path, mode, dir| {
rules.test(name, path)
.dep(|s| s.name("librustc"))
.dep(|s| s.name("test-helpers"))
.dep(|s| s.name("tool-compiletest").target(s.host).stage(0))
.default(mode != "pretty")
.host(true)
.run(move |s| {
check::compiletest(build, &s.compiler(), s.target, mode, dir)
});
};
suite("check-ui-full", "src/test/ui-fulldeps", "ui", "ui-fulldeps");
suite("check-rpass-full", "src/test/run-pass-fulldeps",
"run-pass", "run-pass-fulldeps");
suite("check-rfail-full", "src/test/run-fail-fulldeps",
"run-fail", "run-fail-fulldeps");
suite("check-cfail-full", "src/test/compile-fail-fulldeps",
"compile-fail", "compile-fail-fulldeps");
suite("check-rmake", "src/test/run-make", "run-make", "run-make");
suite("check-rustdoc", "src/test/rustdoc", "rustdoc", "rustdoc");
suite("check-pretty", "src/test/pretty", "pretty", "pretty");
suite("check-pretty-rpass", "src/test/run-pass/pretty", "pretty",
"run-pass");
suite("check-pretty-rfail", "src/test/run-fail/pretty", "pretty",
"run-fail");
suite("check-pretty-valgrind", "src/test/run-pass-valgrind/pretty", "pretty",
"run-pass-valgrind");
suite("check-pretty-rpass-full", "src/test/run-pass-fulldeps/pretty",
"pretty", "run-pass-fulldeps");
suite("check-pretty-rfail-full", "src/test/run-fail-fulldeps/pretty",
"pretty", "run-fail-fulldeps");
}
for (krate, path, _default) in krates("std") {
rules.test(&krate.test_step, path)
.dep(|s| s.name("libtest"))
.dep(|s| s.name("emulator-copy-libs"))
.run(move |s| check::krate(build, &s.compiler(), s.target,
Mode::Libstd, TestKind::Test,
Some(&krate.name)));
}
rules.test("check-std-all", "path/to/nowhere")
.dep(|s| s.name("libtest"))
.dep(|s| s.name("emulator-copy-libs"))
.default(true)
.run(move |s| check::krate(build, &s.compiler(), s.target,
Mode::Libstd, TestKind::Test, None));
// std benchmarks
for (krate, path, _default) in krates("std") {
rules.bench(&krate.bench_step, path)
.dep(|s| s.name("libtest"))
.dep(|s| s.name("emulator-copy-libs"))
.run(move |s| check::krate(build, &s.compiler(), s.target,
Mode::Libstd, TestKind::Bench,
Some(&krate.name)));
}
rules.bench("bench-std-all", "path/to/nowhere")
.dep(|s| s.name("libtest"))
.dep(|s| s.name("emulator-copy-libs"))
.default(true)
.run(move |s| check::krate(build, &s.compiler(), s.target,
Mode::Libstd, TestKind::Bench, None));
for (krate, path, _default) in krates("test") {
rules.test(&krate.test_step, path)
.dep(|s| s.name("libtest"))
.dep(|s| s.name("emulator-copy-libs"))
.run(move |s| check::krate(build, &s.compiler(), s.target,
Mode::Libtest, TestKind::Test,
Some(&krate.name)));
}
rules.test("check-test-all", "path/to/nowhere")
.dep(|s| s.name("libtest"))
.dep(|s| s.name("emulator-copy-libs"))
.default(true)
.run(move |s| check::krate(build, &s.compiler(), s.target,
Mode::Libtest, TestKind::Test, None));
for (krate, path, _default) in krates("rustc-main") {
rules.test(&krate.test_step, path)
.dep(|s| s.name("librustc"))
.dep(|s| s.name("emulator-copy-libs"))
.host(true)
.run(move |s| check::krate(build, &s.compiler(), s.target,
Mode::Librustc, TestKind::Test,
Some(&krate.name)));
}
rules.test("check-rustc-all", "path/to/nowhere")
.dep(|s| s.name("librustc"))
.dep(|s| s.name("emulator-copy-libs"))
.default(true)
.host(true)
.run(move |s| check::krate(build, &s.compiler(), s.target,
Mode::Librustc, TestKind::Test, None));
rules.test("check-linkchecker", "src/tools/linkchecker")
.dep(|s| s.name("tool-linkchecker").stage(0))
.dep(|s| s.name("default:doc"))
.default(true)
.host(true)
.run(move |s| check::linkcheck(build, s.target));
rules.test("check-cargotest", "src/tools/cargotest")
.dep(|s| s.name("tool-cargotest").stage(0))
.dep(|s| s.name("librustc"))
.host(true)
.run(move |s| check::cargotest(build, s.stage, s.target));
rules.test("check-tidy", "src/tools/tidy")
.dep(|s| s.name("tool-tidy").stage(0))
.default(true)
.host(true)
.only_build(true)
.run(move |s| check::tidy(build, s.target));
rules.test("check-error-index", "src/tools/error_index_generator")
.dep(|s| s.name("libstd"))
.dep(|s| s.name("tool-error-index").host(s.host).stage(0))
.default(true)
.host(true)
.run(move |s| check::error_index(build, &s.compiler()));
rules.test("check-docs", "src/doc")
.dep(|s| s.name("libtest"))
.default(true)
.host(true)
.run(move |s| check::docs(build, &s.compiler()));
rules.test("check-distcheck", "distcheck")
.dep(|s| s.name("dist-src"))
.run(move |_| check::distcheck(build));
rules.build("test-helpers", "src/rt/rust_test_helpers.c")
.run(move |s| native::test_helpers(build, s.target));
rules.build("openssl", "path/to/nowhere")
.run(move |s| native::openssl(build, s.target));
// Some test suites are run inside emulators, and most of our test binaries
// are linked dynamically which means we need to ship the standard library
// and such to the emulator ahead of time. This step represents this and is
// a dependency of all test suites.
//
// Most of the time this step is a noop (the `check::emulator_copy_libs`
// only does work if necessary). For some steps such as shipping data to
// QEMU we have to build our own tools so we've got conditional dependencies
// on those programs as well. Note that the QEMU client is built for the
// build target (us) and the server is built for the target.
rules.test("emulator-copy-libs", "path/to/nowhere")
.dep(|s| s.name("libtest"))
.dep(move |s| {
if build.qemu_rootfs(s.target).is_some() {
s.name("tool-qemu-test-client").target(s.host).stage(0)
} else {
Step::noop()
}
})
.dep(move |s| {
if build.qemu_rootfs(s.target).is_some() {
s.name("tool-qemu-test-server")
} else {
Step::noop()
}
})
.run(move |s| check::emulator_copy_libs(build, &s.compiler(), s.target));
rules.test("check-bootstrap", "src/bootstrap")
.default(true)
.host(true)
.only_build(true)
.run(move |_| check::bootstrap(build));
// ========================================================================
// Build tools
//
// Tools used during the build system but not shipped
rules.build("tool-rustbook", "src/tools/rustbook")
.dep(|s| s.name("librustc"))
.run(move |s| compile::tool(build, s.stage, s.target, "rustbook"));
rules.build("tool-error-index", "src/tools/error_index_generator")
.dep(|s| s.name("librustc"))
.run(move |s| compile::tool(build, s.stage, s.target, "error_index_generator"));
rules.build("tool-tidy", "src/tools/tidy")
.dep(|s| s.name("libstd"))
.run(move |s| compile::tool(build, s.stage, s.target, "tidy"));
rules.build("tool-linkchecker", "src/tools/linkchecker")
.dep(|s| s.name("libstd"))
.run(move |s| compile::tool(build, s.stage, s.target, "linkchecker"));
rules.build("tool-cargotest", "src/tools/cargotest")
.dep(|s| s.name("libstd"))
.run(move |s| compile::tool(build, s.stage, s.target, "cargotest"));
rules.build("tool-compiletest", "src/tools/compiletest")
.dep(|s| s.name("libtest"))
.run(move |s| compile::tool(build, s.stage, s.target, "compiletest"));
rules.build("tool-build-manifest", "src/tools/build-manifest")
.dep(|s| s.name("libstd"))
.run(move |s| compile::tool(build, s.stage, s.target, "build-manifest"));
rules.build("tool-qemu-test-server", "src/tools/qemu-test-server")
.dep(|s| s.name("libstd"))
.run(move |s| compile::tool(build, s.stage, s.target, "qemu-test-server"));
rules.build("tool-qemu-test-client", "src/tools/qemu-test-client")
.dep(|s| s.name("libstd"))
.run(move |s| compile::tool(build, s.stage, s.target, "qemu-test-client"));
rules.build("tool-cargo", "src/tools/cargo")
.dep(|s| s.name("libstd"))
.dep(|s| s.stage(0).host(s.target).name("openssl"))
.dep(move |s| {
// Cargo depends on procedural macros, which requires a full host
// compiler to be available, so we need to depend on that.
s.name("librustc-link")
.target(&build.config.build)
.host(&build.config.build)
})
.run(move |s| compile::tool(build, s.stage, s.target, "cargo"));
// ========================================================================
// Documentation targets
rules.doc("doc-book", "src/doc/book")
.dep(move |s| {
s.name("tool-rustbook")
.host(&build.config.build)
.target(&build.config.build)
.stage(0)
})
.default(build.config.docs)
.run(move |s| doc::rustbook(build, s.target, "book"));
rules.doc("doc-nomicon", "src/doc/nomicon")
.dep(move |s| {
s.name("tool-rustbook")
.host(&build.config.build)
.target(&build.config.build)
.stage(0)
})
.default(build.config.docs)
.run(move |s| doc::rustbook(build, s.target, "nomicon"));
rules.doc("doc-reference", "src/doc/reference")
.dep(move |s| {
s.name("tool-rustbook")
.host(&build.config.build)
.target(&build.config.build)
.stage(0)
})
.default(build.config.docs)
.run(move |s| doc::rustbook(build, s.target, "reference"));
rules.doc("doc-unstable-book", "src/doc/unstable-book")
.dep(move |s| {
s.name("tool-rustbook")
.host(&build.config.build)
.target(&build.config.build)
.stage(0)
})
.default(build.config.docs)
.run(move |s| doc::rustbook(build, s.target, "unstable-book"));
rules.doc("doc-standalone", "src/doc")
.dep(move |s| {
s.name("rustc")
.host(&build.config.build)
.target(&build.config.build)
.stage(0)
})
.default(build.config.docs)
.run(move |s| doc::standalone(build, s.target));
rules.doc("doc-error-index", "src/tools/error_index_generator")
.dep(move |s| s.name("tool-error-index").target(&build.config.build).stage(0))
.dep(move |s| s.name("librustc-link"))
.default(build.config.docs)
.host(true)
.run(move |s| doc::error_index(build, s.target));
for (krate, path, default) in krates("std") {
rules.doc(&krate.doc_step, path)
.dep(|s| s.name("libstd-link"))
.default(default && build.config.docs)
.run(move |s| doc::std(build, s.stage, s.target));
}
for (krate, path, default) in krates("test") {
rules.doc(&krate.doc_step, path)
.dep(|s| s.name("libtest-link"))
.default(default && build.config.compiler_docs)
.run(move |s| doc::test(build, s.stage, s.target));
}
for (krate, path, default) in krates("rustc-main") {
rules.doc(&krate.doc_step, path)
.dep(|s| s.name("librustc-link"))
.host(true)
.default(default && build.config.docs)
.run(move |s| doc::rustc(build, s.stage, s.target));
}
// ========================================================================
// Distribution targets
rules.dist("dist-rustc", "src/librustc")
.dep(move |s| s.name("rustc").host(&build.config.build))
.host(true)
.only_host_build(true)
.default(true)
.run(move |s| dist::rustc(build, s.stage, s.target));
rules.dist("dist-std", "src/libstd")
.dep(move |s| {
// We want to package up as many target libraries as possible
// for the `rust-std` package, so if this is a host target we
// depend on librustc and otherwise we just depend on libtest.
if build.config.host.iter().any(|t| t == s.target) {
s.name("librustc-link")
} else {
s.name("libtest-link")
}
})
.default(true)
.only_host_build(true)
.run(move |s| dist::std(build, &s.compiler(), s.target));
rules.dist("dist-mingw", "path/to/nowhere")
.default(true)
.only_host_build(true)
.run(move |s| {
if s.target.contains("pc-windows-gnu") {
dist::mingw(build, s.target)
}
});
rules.dist("dist-src", "src")
.default(true)
.host(true)
.only_build(true)
.only_host_build(true)
.run(move |_| dist::rust_src(build));
rules.dist("dist-docs", "src/doc")
.default(true)
.only_host_build(true)
.dep(|s| s.name("default:doc"))
.run(move |s| dist::docs(build, s.stage, s.target));
rules.dist("dist-analysis", "analysis")
.dep(|s| s.name("dist-std"))
.default(true)
.only_host_build(true)
.run(move |s| dist::analysis(build, &s.compiler(), s.target));
rules.dist("install", "path/to/nowhere")
.dep(|s| s.name("default:dist"))
.run(move |s| install::install(build, s.stage, s.target));
rules.dist("dist-cargo", "cargo")
.host(true)
.only_host_build(true)
.dep(|s| s.name("tool-cargo"))
.run(move |s| dist::cargo(build, s.stage, s.target));
rules.dist("dist-extended", "extended")
.default(build.config.extended)
.host(true)
.only_host_build(true)
.dep(|d| d.name("dist-std"))
.dep(|d| d.name("dist-rustc"))
.dep(|d| d.name("dist-mingw"))
.dep(|d| d.name("dist-docs"))
.dep(|d| d.name("dist-cargo"))
.run(move |s| dist::extended(build, s.stage, s.target));
rules.dist("dist-sign", "hash-and-sign")
.host(true)
.only_build(true)
.only_host_build(true)
.dep(move |s| s.name("tool-build-manifest").target(&build.config.build).stage(0))
.run(move |_| dist::hash_and_sign(build));
rules.verify();
return rules;
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
struct Step<'a> {
/// Human readable name of the rule this step is executing. Possible names
/// are all defined above in `build_rules`.
name: &'a str,
/// The stage this step is executing in. This is typically 0, 1, or 2.
stage: u32,
/// This step will likely involve a compiler, and the target that compiler
/// itself is built for is called the host, this variable. Typically this is
/// the target of the build machine itself.
host: &'a str,
/// The target that this step represents generating. If you're building a
/// standard library for a new suite of targets, for example, this'll be set
/// to those targets.
target: &'a str,
}
impl<'a> Step<'a> {
fn noop() -> Step<'a> {
Step { name: "", stage: 0, host: "", target: "" }
}
/// Creates a new step which is the same as this, except has a new name.
fn name(&self, name: &'a str) -> Step<'a> {
Step { name: name, ..*self }
}
/// Creates a new step which is the same as this, except has a new stage.
fn stage(&self, stage: u32) -> Step<'a> {
Step { stage: stage, ..*self }
}
/// Creates a new step which is the same as this, except has a new host.
fn host(&self, host: &'a str) -> Step<'a> {
Step { host: host, ..*self }
}
/// Creates a new step which is the same as this, except has a new target.
fn target(&self, target: &'a str) -> Step<'a> {
Step { target: target, ..*self }
}
/// Returns the `Compiler` structure that this step corresponds to.
fn compiler(&self) -> Compiler<'a> {
Compiler::new(self.stage, self.host)
}
}
struct Rule<'a> {
/// The human readable name of this target, defined in `build_rules`.
name: &'a str,
/// The path associated with this target, used in the `./x.py` driver for
/// easy and ergonomic specification of what to do.
path: &'a str,
/// The "kind" of top-level command that this rule is associated with, only
/// relevant if this is a default rule.
kind: Kind,
/// List of dependencies this rule has. Each dependency is a function from a
/// step that's being executed to another step that should be executed.
deps: Vec<Box<Fn(&Step<'a>) -> Step<'a> + 'a>>,
/// How to actually execute this rule. Takes a step with contextual
/// information and then executes it.
run: Box<Fn(&Step<'a>) + 'a>,
/// Whether or not this is a "default" rule. That basically means that if
/// you run, for example, `./x.py test` whether it's included or not.
default: bool,
/// Whether or not this is a "host" rule, or in other words whether this is
/// only intended for compiler hosts and not for targets that are being
/// generated.
host: bool,
/// Whether this rule is only for steps where the host is the build triple,
/// not anything in hosts or targets.
only_host_build: bool,
/// Whether this rule is only for the build triple, not anything in hosts or
/// targets.
only_build: bool,
}
#[derive(PartialEq)]
enum Kind {
Build,
Test,
Bench,
Dist,
Doc,
}
impl<'a> Rule<'a> {
fn new(name: &'a str, path: &'a str, kind: Kind) -> Rule<'a> {
Rule {
name: name,
deps: Vec::new(),
run: Box::new(|_| ()),
path: path,
kind: kind,
default: false,
host: false,
only_host_build: false,
only_build: false,
}
}
}
/// Builder pattern returned from the various methods on `Rules` which will add
/// the rule to the internal list on `Drop`.
struct RuleBuilder<'a: 'b, 'b> {
rules: &'b mut Rules<'a>,
rule: Rule<'a>,
}
impl<'a, 'b> RuleBuilder<'a, 'b> {
fn dep<F>(&mut self, f: F) -> &mut Self
where F: Fn(&Step<'a>) -> Step<'a> + 'a,
{
self.rule.deps.push(Box::new(f));
self
}
fn run<F>(&mut self, f: F) -> &mut Self
where F: Fn(&Step<'a>) + 'a,
{
self.rule.run = Box::new(f);
self
}
fn default(&mut self, default: bool) -> &mut Self {
self.rule.default = default;
self
}
fn host(&mut self, host: bool) -> &mut Self {
self.rule.host = host;
self
}
fn only_build(&mut self, only_build: bool) -> &mut Self {
self.rule.only_build = only_build;
self
}
fn only_host_build(&mut self, only_host_build: bool) -> &mut Self {
self.rule.only_host_build = only_host_build;
self
}
}
impl<'a, 'b> Drop for RuleBuilder<'a, 'b> {
fn drop(&mut self) {
let rule = mem::replace(&mut self.rule, Rule::new("", "", Kind::Build));
let prev = self.rules.rules.insert(rule.name, rule);
if let Some(prev) = prev {
panic!("duplicate rule named: {}", prev.name);
}
}
}
pub struct Rules<'a> {
build: &'a Build,
sbuild: Step<'a>,
rules: BTreeMap<&'a str, Rule<'a>>,
}
impl<'a> Rules<'a> {
fn new(build: &'a Build) -> Rules<'a> {
Rules {
build: build,
sbuild: Step {
stage: build.flags.stage.unwrap_or(2),
target: &build.config.build,
host: &build.config.build,
name: "",
},
rules: BTreeMap::new(),
}
}
/// Creates a new rule of `Kind::Build` with the specified human readable
/// name and path associated with it.
///
/// The builder returned should be configured further with information such
/// as how to actually run this rule.
fn build<'b>(&'b mut self, name: &'a str, path: &'a str)
-> RuleBuilder<'a, 'b> {
self.rule(name, path, Kind::Build)
}
/// Same as `build`, but for `Kind::Test`.
fn test<'b>(&'b mut self, name: &'a str, path: &'a str)
-> RuleBuilder<'a, 'b> {
self.rule(name, path, Kind::Test)
}
/// Same as `build`, but for `Kind::Bench`.
fn bench<'b>(&'b mut self, name: &'a str, path: &'a str)
-> RuleBuilder<'a, 'b> {
self.rule(name, path, Kind::Bench)
}
/// Same as `build`, but for `Kind::Doc`.
fn doc<'b>(&'b mut self, name: &'a str, path: &'a str)
-> RuleBuilder<'a, 'b> {
self.rule(name, path, Kind::Doc)
}
/// Same as `build`, but for `Kind::Dist`.
fn dist<'b>(&'b mut self, name: &'a str, path: &'a str)
-> RuleBuilder<'a, 'b> {
self.rule(name, path, Kind::Dist)
}
fn rule<'b>(&'b mut self,
name: &'a str,
path: &'a str,
kind: Kind) -> RuleBuilder<'a, 'b> {
RuleBuilder {
rules: self,
rule: Rule::new(name, path, kind),
}
}
/// Verify the dependency graph defined by all our rules are correct, e.g.
/// everything points to a valid something else.
fn verify(&self) {
for rule in self.rules.values() {
for dep in rule.deps.iter() {
let dep = dep(&self.sbuild.name(rule.name));
if self.rules.contains_key(&dep.name) || dep.name.starts_with("default:") {
continue
}
if dep == Step::noop() {
continue
}
panic!("\
invalid rule dependency graph detected, was a rule added and maybe typo'd?
`{}` depends on `{}` which does not exist
", rule.name, dep.name);
}
}
}
pub fn print_help(&self, command: &str) {
let kind = match command {
"build" => Kind::Build,
"doc" => Kind::Doc,
"test" => Kind::Test,
"bench" => Kind::Bench,
"dist" => Kind::Dist,
_ => return,
};
let rules = self.rules.values().filter(|r| r.kind == kind);
let rules = rules.filter(|r| !r.path.contains("nowhere"));
let mut rules = rules.collect::<Vec<_>>();
rules.sort_by_key(|r| r.path);
println!("Available paths:\n");
for rule in rules {
print!(" ./x.py {} {}", command, rule.path);
println!("");
}
}
/// Construct the top-level build steps that we're going to be executing,
/// given the subcommand that our build is performing.