Skip to content

Commit c7f90c1

Browse files
committed
Do not unnecessarily build stage2 host rustc in some dist builders
1 parent 598387b commit c7f90c1

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,22 @@ pub struct Std {
764764

765765
impl Std {
766766
pub fn new(builder: &Builder<'_>, target: TargetSelection) -> Self {
767-
Std {
768-
build_compiler: builder.compiler(builder.top_stage, builder.config.host_target),
769-
target,
770-
}
767+
// This is an important optimization mainly for CI.
768+
// Normally, to build stage N libstd, we need stage N rustc.
769+
// However, if we know that we will uplift libstd from stage 1 anyway, building the stage N
770+
// rustc can be wasteful.
771+
// In particular, if we do a cross-compiling dist stage 2 build from T1 to T2, we need:
772+
// - stage 2 libstd for T2 (uplifted from stage 1, where it was built by T1 rustc)
773+
// - stage 2 rustc for T2
774+
// However, without this optimization, we would also build stage 2 rustc for **T1**, which
775+
// is completely wasteful.
776+
let build_compiler =
777+
if compile::Std::should_be_uplifted_from_stage_1(builder, builder.top_stage, target) {
778+
builder.compiler(1, builder.host_target)
779+
} else {
780+
builder.compiler(builder.top_stage, builder.host_target)
781+
};
782+
Std { build_compiler, target }
771783
}
772784
}
773785

src/bootstrap/src/core/builder/tests.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,7 @@ mod snapshot {
11331133
[dist] mingw <host>
11341134
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
11351135
[dist] rustc <host>
1136-
[build] rustc 2 <host> -> std 2 <host>
1137-
[dist] rustc 2 <host> -> std 2 <host>
1136+
[dist] rustc 1 <host> -> std 1 <host>
11381137
[dist] rustc 1 <host> -> rustc-dev 2 <host>
11391138
[dist] src <>
11401139
[dist] reproducible-artifacts <host>
@@ -1198,8 +1197,7 @@ mod snapshot {
11981197
[build] rustc 1 <host> -> rust-analyzer-proc-macro-srv 2 <host>
11991198
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
12001199
[dist] rustc <host>
1201-
[build] rustc 2 <host> -> std 2 <host>
1202-
[dist] rustc 2 <host> -> std 2 <host>
1200+
[dist] rustc 1 <host> -> std 1 <host>
12031201
[dist] rustc 1 <host> -> rustc-dev 2 <host>
12041202
[dist] rustc 1 <host> -> analysis 2 <host>
12051203
[dist] src <>
@@ -1216,7 +1214,6 @@ mod snapshot {
12161214
[build] rustc 1 <host> -> miri 2 <host>
12171215
[build] rustc 1 <host> -> cargo-miri 2 <host>
12181216
[dist] rustc 1 <host> -> miri 2 <host>
1219-
[dist] rustc 1 <host> -> std 1 <host>
12201217
[dist] rustc 1 <host> -> extended 2 <host>
12211218
[dist] reproducible-artifacts <host>
12221219
");
@@ -1287,8 +1284,7 @@ mod snapshot {
12871284
[dist] mingw <target1>
12881285
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
12891286
[dist] rustc <host>
1290-
[build] rustc 2 <host> -> std 2 <host>
1291-
[dist] rustc 2 <host> -> std 2 <host>
1287+
[dist] rustc 1 <host> -> std 1 <host>
12921288
[build] rustc 2 <host> -> std 2 <target1>
12931289
[dist] rustc 2 <host> -> std 2 <target1>
12941290
[dist] rustc 1 <host> -> rustc-dev 2 <host>
@@ -1350,8 +1346,7 @@ mod snapshot {
13501346
[dist] rustc <host>
13511347
[build] rustdoc 2 <target1>
13521348
[dist] rustc <target1>
1353-
[build] rustc 2 <host> -> std 2 <host>
1354-
[dist] rustc 2 <host> -> std 2 <host>
1349+
[dist] rustc 1 <host> -> std 1 <host>
13551350
[dist] rustc 1 <host> -> rustc-dev 2 <host>
13561351
[dist] rustc 1 <host> -> rustc-dev 2 <target1>
13571352
[dist] src <>
@@ -1433,10 +1428,8 @@ mod snapshot {
14331428
[dist] rustc <host>
14341429
[build] rustdoc 2 <target1>
14351430
[dist] rustc <target1>
1436-
[build] rustc 2 <host> -> std 2 <host>
1437-
[dist] rustc 2 <host> -> std 2 <host>
1438-
[build] rustc 2 <host> -> std 2 <target1>
1439-
[dist] rustc 2 <host> -> std 2 <target1>
1431+
[dist] rustc 1 <host> -> std 1 <host>
1432+
[dist] rustc 1 <host> -> std 1 <target1>
14401433
[dist] rustc 1 <host> -> rustc-dev 2 <host>
14411434
[dist] rustc 1 <host> -> rustc-dev 2 <target1>
14421435
[dist] src <>
@@ -1490,8 +1483,6 @@ mod snapshot {
14901483
");
14911484
}
14921485

1493-
/// This also serves as an important regression test for <https://github.com/rust-lang/rust/issues/138123>
1494-
/// and <https://github.com/rust-lang/rust/issues/138004>.
14951486
#[test]
14961487
fn dist_all_cross_extended() {
14971488
let ctx = TestCtx::new();
@@ -1545,8 +1536,7 @@ mod snapshot {
15451536
[build] rustc 1 <host> -> rust-analyzer-proc-macro-srv 2 <target1>
15461537
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
15471538
[dist] rustc <target1>
1548-
[build] rustc 2 <host> -> std 2 <target1>
1549-
[dist] rustc 2 <host> -> std 2 <target1>
1539+
[dist] rustc 1 <host> -> std 1 <target1>
15501540
[dist] rustc 1 <host> -> rustc-dev 2 <target1>
15511541
[dist] rustc 1 <host> -> analysis 2 <target1>
15521542
[dist] src <>
@@ -1564,7 +1554,6 @@ mod snapshot {
15641554
[build] rustc 1 <host> -> cargo-miri 2 <target1>
15651555
[dist] rustc 1 <host> -> miri 2 <target1>
15661556
[build] rustc 1 <host> -> LlvmBitcodeLinker 2 <target1>
1567-
[dist] rustc 1 <host> -> std 1 <target1>
15681557
[doc] rustc 2 <target1> -> std 2 <target1> crates=[]
15691558
[dist] rustc 1 <host> -> extended 2 <target1>
15701559
[dist] reproducible-artifacts <target1>
@@ -1573,6 +1562,9 @@ mod snapshot {
15731562

15741563
/// Simulates e.g. the powerpc64 builder, which is fully cross-compiled from x64, but it does
15751564
/// not build docs. Crutically, it shouldn't build host stage 2 rustc.
1565+
///
1566+
/// This is a regression test for <https://github.com/rust-lang/rust/issues/138123>
1567+
/// and <https://github.com/rust-lang/rust/issues/138004>.
15761568
#[test]
15771569
fn dist_all_cross_extended_no_docs() {
15781570
let ctx = TestCtx::new();
@@ -1591,11 +1583,11 @@ mod snapshot {
15911583
.get_steps();
15921584

15931585
// Make sure that we don't build stage2 host rustc
1594-
// steps.assert_no_match(|m| {
1595-
// m.name == "rustc"
1596-
// && m.built_by.map(|b| b.stage) == Some(1)
1597-
// && *m.target.triple == host_target()
1598-
// });
1586+
steps.assert_no_match(|m| {
1587+
m.name == "rustc"
1588+
&& m.built_by.map(|b| b.stage) == Some(1)
1589+
&& *m.target.triple == host_target()
1590+
});
15991591

16001592
insta::assert_snapshot!(
16011593
steps.render(), @r"
@@ -1613,10 +1605,7 @@ mod snapshot {
16131605
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
16141606
[build] rustc 0 <host> -> RustInstaller 1 <host>
16151607
[dist] rustc <target1>
1616-
[build] rustc 1 <host> -> rustc 2 <host>
1617-
[build] rustc 1 <host> -> WasmComponentLd 2 <host>
1618-
[build] rustc 2 <host> -> std 2 <target1>
1619-
[dist] rustc 2 <host> -> std 2 <target1>
1608+
[dist] rustc 1 <host> -> std 1 <target1>
16201609
[dist] rustc 1 <host> -> rustc-dev 2 <target1>
16211610
[dist] rustc 1 <host> -> analysis 2 <target1>
16221611
[dist] src <>
@@ -1634,7 +1623,6 @@ mod snapshot {
16341623
[build] rustc 1 <host> -> cargo-miri 2 <target1>
16351624
[dist] rustc 1 <host> -> miri 2 <target1>
16361625
[build] rustc 1 <host> -> LlvmBitcodeLinker 2 <target1>
1637-
[dist] rustc 1 <host> -> std 1 <target1>
16381626
[dist] rustc 1 <host> -> extended 2 <target1>
16391627
[dist] reproducible-artifacts <target1>
16401628
");
@@ -1688,8 +1676,7 @@ mod snapshot {
16881676
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
16891677
[dist] rustc <host>
16901678
[dist] rustc 1 <host> -> rustc_codegen_cranelift 2 <host>
1691-
[build] rustc 2 <host> -> std 2 <host>
1692-
[dist] rustc 2 <host> -> std 2 <host>
1679+
[dist] rustc 1 <host> -> std 1 <host>
16931680
[dist] rustc 1 <host> -> rustc-dev 2 <host>
16941681
[dist] src <>
16951682
[dist] reproducible-artifacts <host>
@@ -2420,8 +2407,7 @@ mod snapshot {
24202407
[doc] rustc 1 <host> -> releases 2 <host>
24212408
[build] rustc 0 <host> -> RustInstaller 1 <host>
24222409
[dist] docs <host>
2423-
[build] rustc 2 <host> -> std 2 <host>
2424-
[dist] rustc 2 <host> -> std 2 <host>
2410+
[dist] rustc 1 <host> -> std 1 <host>
24252411
[build] rustc 1 <host> -> rust-analyzer-proc-macro-srv 2 <host>
24262412
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
24272413
[dist] rustc <host>

0 commit comments

Comments
 (0)