Skip to content

Commit 9c6372c

Browse files
committed
Auto merge of #119899 - onur-ozkan:redesign-stage0-std, r=<try>
redesign stage 0 std ### Summary This PR changes how bootstrap builds the stage 1 compiler by switching to precompiled stage 0 standard library instead of building the in-tree one. The goal was to update bootstrap to use the beta standard library at stage 0 rather than compiling it from source (see the motivation at rust-lang/compiler-team#619). Previously, to build a stage 1 compiler bootstrap followed this path: ``` download stage0 compiler -> build in-tree std -> compile stage1 compiler with in-tree std ``` With this PR, the new path is: ``` download stage0 compiler -> compile stage1 compiler with precompiled stage0 std ``` This also means that `cfg(bootstrap)`/`cfg(not(bootstrap))` is no longer needed for library development. ### Building "library" Since stage0 `std` is no longer in-tree `x build/test/check library --stage 0` is now no-op. The minimum supported stage to build `std` is now 1. For the same reason, default stage values in the library profile is no longer 0. Because building the in-tree library now requires a stage1 compiler, I highly recommend library developers to enable `download-rustc` to speed up compilation time. <hr> If you encounter a bug or unexpected results please open a topic in the [#t-infra/bootstrap](https://rust-lang.zulipchat.com/#narrow/channel/326414-t-infra.2Fbootstrap) Zulip channel or create a [bootstrap issue](https://github.com/rust-lang/rust/issues/new?template=bootstrap.md). (Review thread: https://rust-lang.zulipchat.com/#narrow/channel/326414-t-infra.2Fbootstrap/topic/Review.20thread.3A.20stage.200.20redesign.20PR/with/508271433) ~~Blocked on #122709 try-job: dist-x86_64-linux try-job: `*msvc*` try-job: `*apple*` try-job: x86_64-gnu try-job: `x86_64-gnu-llvm*`
2 parents 642e49b + 2a4e930 commit 9c6372c

File tree

44 files changed

+526
-499
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+526
-499
lines changed

compiler/rustc_data_structures/src/flock.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
//! green/native threading. This is just a bare-bones enough solution for
55
//! librustdoc, it is not production quality at all.
66
7-
cfg_select! {
7+
// cfg(bootstrap)
8+
macro_rules! cfg_select_dispatch {
9+
($($tokens:tt)*) => {
10+
#[cfg(bootstrap)]
11+
cfg_match! { $($tokens)* }
12+
13+
#[cfg(not(bootstrap))]
14+
cfg_select! { $($tokens)* }
15+
};
16+
}
17+
18+
cfg_select_dispatch! {
819
target_os = "linux" => {
920
mod linux;
1021
use linux as imp;

compiler/rustc_data_structures/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#![allow(internal_features)]
1111
#![allow(rustc::default_hash_types)]
1212
#![allow(rustc::potential_query_instability)]
13+
#![cfg_attr(bootstrap, feature(cfg_match))]
14+
#![cfg_attr(not(bootstrap), feature(cfg_select))]
1315
#![deny(unsafe_op_in_unsafe_fn)]
1416
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1517
#![doc(rust_logo)]
@@ -19,7 +21,6 @@
1921
#![feature(ascii_char_variants)]
2022
#![feature(assert_matches)]
2123
#![feature(auto_traits)]
22-
#![feature(cfg_select)]
2324
#![feature(core_intrinsics)]
2425
#![feature(dropck_eyepatch)]
2526
#![feature(extend_one)]

compiler/rustc_data_structures/src/profiling.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,19 @@ fn get_thread_id() -> u32 {
859859
std::thread::current().id().as_u64().get() as u32
860860
}
861861

862+
// cfg(bootstrap)
863+
macro_rules! cfg_select_dispatch {
864+
($($tokens:tt)*) => {
865+
#[cfg(bootstrap)]
866+
cfg_match! { $($tokens)* }
867+
868+
#[cfg(not(bootstrap))]
869+
cfg_select! { $($tokens)* }
870+
};
871+
}
872+
862873
// Memory reporting
863-
cfg_select! {
874+
cfg_select_dispatch! {
864875
windows => {
865876
pub fn get_resident_set_size() -> Option<usize> {
866877
use windows::{

compiler/rustc_span/src/analyze_source_file.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ pub(crate) fn analyze_source_file(src: &str) -> (Vec<RelativeBytePos>, Vec<Multi
2929
(lines, multi_byte_chars)
3030
}
3131

32-
cfg_select! {
32+
// cfg(bootstrap)
33+
macro_rules! cfg_select_dispatch {
34+
($($tokens:tt)*) => {
35+
#[cfg(bootstrap)]
36+
cfg_match! { $($tokens)* }
37+
38+
#[cfg(not(bootstrap))]
39+
cfg_select! { $($tokens)* }
40+
};
41+
}
42+
43+
cfg_select_dispatch! {
3344
any(target_arch = "x86", target_arch = "x86_64") => {
3445
fn analyze_source_file_dispatch(
3546
src: &str,

compiler/rustc_span/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
1818
// tidy-alphabetical-start
1919
#![allow(internal_features)]
20+
#![cfg_attr(bootstrap, feature(cfg_match))]
21+
#![cfg_attr(not(bootstrap), feature(cfg_select))]
2022
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
2123
#![doc(rust_logo)]
2224
#![feature(array_windows)]
23-
#![feature(cfg_select)]
2425
#![feature(core_io_borrowed_buf)]
2526
#![feature(hash_set_entry)]
2627
#![feature(if_let_guard)]

src/bootstrap/defaults/bootstrap.library.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# These defaults are meant for contributors to the standard library and documentation.
22
[build]
3-
# When building the standard library, you almost never want to build the compiler itself.
4-
build-stage = 0
5-
test-stage = 0
6-
bench-stage = 0
3+
build-stage = 1
4+
test-stage = 1
5+
bench-stage = 1
76

87
[rust]
98
# This greatly increases the speed of rebuilds, especially when there are only minor changes. However, it makes the initial build slightly slower.
109
incremental = true
1110
# Make the compiler and standard library faster to build, at the expense of a ~20% runtime slowdown.
1211
lto = "off"
13-
# Download rustc by default for library profile if compiler-affecting
14-
# directories are not modified. For CI this is disabled.
12+
# When building the standard library, you almost never want to build the compiler itself.
13+
#
14+
# If compiler-affecting directories are not modified, use precompiled rustc to speed up
15+
# library development by skipping compiler builds.
1516
download-rustc = "if-unchanged"
1617

1718
[llvm]

src/bootstrap/src/bin/rustc.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,12 @@ fn main() {
120120
};
121121
cmd.args(&args).env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
122122

123-
if let Some(crate_name) = crate_name {
124-
if let Some(target) = env::var_os("RUSTC_TIME") {
125-
if target == "all"
126-
|| target.into_string().unwrap().split(',').any(|c| c.trim() == crate_name)
127-
{
128-
cmd.arg("-Ztime-passes");
129-
}
130-
}
123+
if let Some(crate_name) = crate_name
124+
&& let Some(target) = env::var_os("RUSTC_TIME")
125+
&& (target == "all"
126+
|| target.into_string().unwrap().split(',').any(|c| c.trim() == crate_name))
127+
{
128+
cmd.arg("-Ztime-passes");
131129
}
132130

133131
// Print backtrace in case of ICE
@@ -242,10 +240,10 @@ fn main() {
242240
}
243241
}
244242

245-
if env::var_os("RUSTC_BOLT_LINK_FLAGS").is_some() {
246-
if let Some("rustc_driver") = crate_name {
247-
cmd.arg("-Clink-args=-Wl,-q");
248-
}
243+
if env::var_os("RUSTC_BOLT_LINK_FLAGS").is_some()
244+
&& let Some("rustc_driver") = crate_name
245+
{
246+
cmd.arg("-Clink-args=-Wl,-q");
249247
}
250248

251249
let is_test = args.iter().any(|a| a == "--test");
@@ -282,25 +280,24 @@ fn main() {
282280
(child, status)
283281
};
284282

285-
if env::var_os("RUSTC_PRINT_STEP_TIMINGS").is_some()
286-
|| env::var_os("RUSTC_PRINT_STEP_RUSAGE").is_some()
283+
if (env::var_os("RUSTC_PRINT_STEP_TIMINGS").is_some()
284+
|| env::var_os("RUSTC_PRINT_STEP_RUSAGE").is_some())
285+
&& let Some(crate_name) = crate_name
287286
{
288-
if let Some(crate_name) = crate_name {
289-
let dur = start.elapsed();
290-
// If the user requested resource usage data, then
291-
// include that in addition to the timing output.
292-
let rusage_data =
293-
env::var_os("RUSTC_PRINT_STEP_RUSAGE").and_then(|_| format_rusage_data(child));
294-
eprintln!(
295-
"[RUSTC-TIMING] {} test:{} {}.{:03}{}{}",
296-
crate_name,
297-
is_test,
298-
dur.as_secs(),
299-
dur.subsec_millis(),
300-
if rusage_data.is_some() { " " } else { "" },
301-
rusage_data.unwrap_or_default(),
302-
);
303-
}
287+
let dur = start.elapsed();
288+
// If the user requested resource usage data, then
289+
// include that in addition to the timing output.
290+
let rusage_data =
291+
env::var_os("RUSTC_PRINT_STEP_RUSAGE").and_then(|_| format_rusage_data(child));
292+
eprintln!(
293+
"[RUSTC-TIMING] {} test:{} {}.{:03}{}{}",
294+
crate_name,
295+
is_test,
296+
dur.as_secs(),
297+
dur.subsec_millis(),
298+
if rusage_data.is_some() { " " } else { "" },
299+
rusage_data.unwrap_or_default(),
300+
);
304301
}
305302

306303
if status.success() {

src/bootstrap/src/core/build_steps/check.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Implementation of compiling the compiler and standard library, in "check"-based modes.
22
3+
use crate::core::build_steps::compile;
34
use crate::core::build_steps::compile::{
45
add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo, std_crates_for_run_make,
56
};
@@ -45,10 +46,12 @@ impl Step for Std {
4546
const DEFAULT: bool = true;
4647

4748
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
49+
let stage = run.builder.top_stage;
4850
run.crate_or_deps("sysroot")
4951
.crate_or_deps("coretests")
5052
.crate_or_deps("alloctests")
5153
.path("library")
54+
.default_condition(stage != 0)
5255
}
5356

5457
fn make_run(run: RunConfig<'_>) {
@@ -62,6 +65,12 @@ impl Step for Std {
6265
let target = self.target;
6366
let compiler = builder.compiler(builder.top_stage, builder.config.build);
6467

68+
if builder.top_stage == 0 {
69+
// Reuse the stage0 libstd
70+
builder.ensure(compile::Std::new(compiler, target));
71+
return;
72+
}
73+
6574
let mut cargo = builder::Cargo::new(
6675
builder,
6776
compiler,

src/bootstrap/src/core/build_steps/clippy.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,18 @@ impl Step for Rustc {
207207
let compiler = builder.compiler(builder.top_stage, builder.config.build);
208208
let target = self.target;
209209

210-
if compiler.stage != 0 {
211-
// If we're not in stage 0, then we won't have a std from the beta
212-
// compiler around. That means we need to make sure there's one in
213-
// the sysroot for the compiler to find. Otherwise, we're going to
214-
// fail when building crates that need to generate code (e.g., build
215-
// scripts and their dependencies).
216-
builder.ensure(compile::Std::new(compiler, compiler.host));
217-
builder.ensure(compile::Std::new(compiler, target));
218-
} else {
219-
builder.ensure(check::Std::new(target).build_kind(Some(Kind::Check)));
210+
if !builder.download_rustc() {
211+
if compiler.stage != 0 {
212+
// If we're not in stage 0, then we won't have a std from the beta
213+
// compiler around. That means we need to make sure there's one in
214+
// the sysroot for the compiler to find. Otherwise, we're going to
215+
// fail when building crates that need to generate code (e.g., build
216+
// scripts and their dependencies).
217+
builder.ensure(compile::Std::new(compiler, compiler.host));
218+
builder.ensure(compile::Std::new(compiler, target));
219+
} else {
220+
builder.ensure(check::Std::new(target).build_kind(Some(Kind::Check)));
221+
}
220222
}
221223

222224
let mut cargo = builder::Cargo::new(
@@ -286,7 +288,9 @@ macro_rules! lint_any {
286288
let compiler = builder.compiler(builder.top_stage, builder.config.build);
287289
let target = self.target;
288290

289-
builder.ensure(check::Rustc::new(target, builder).build_kind(Some(Kind::Check)));
291+
if !builder.download_rustc() {
292+
builder.ensure(check::Rustc::new(target, builder).build_kind(Some(Kind::Check)));
293+
};
290294

291295
let cargo = prepare_tool_cargo(
292296
builder,

0 commit comments

Comments
 (0)