Skip to content

Commit 2831203

Browse files
committed
Auto merge of rust-lang#119899 - onur-ozkan:redesign-stage0-std, r=albertlarsan68
redesign stage 0 std This is intended to update bootstrap to use the beta standard library on stage 0, rather than compiling it from source (see the motivation at rust-lang/compiler-team#619). The only drawback encountered was the requirement of using the stage 1 compiler to build the standard library from source. This issue has been resolved by adding the `--build-std-on-stage0` flag. Therefore if the goal is to only build/test the compiled standard library without spending time on compiling rustc, it can be achieved by running `x build --stage 0 std --build-std-on-stage0`.
2 parents e35a56d + f053669 commit 2831203

File tree

14 files changed

+120
-31
lines changed

14 files changed

+120
-31
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+39-11
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ impl Step for Std {
137137
let target = self.target;
138138
let compiler = self.compiler;
139139

140+
// We already have std ready to be used for stage 0.
141+
if compiler.stage == 0 && !builder.config.build_std_on_stage0 {
142+
builder.ensure(StdLink::from_std(
143+
self,
144+
builder.compiler(compiler.stage, builder.config.build),
145+
));
146+
147+
return;
148+
}
149+
140150
// When using `download-rustc`, we already have artifacts for the host available. Don't
141151
// recompile them.
142152
if builder.download_rustc() && target == builder.build.build
@@ -175,8 +185,20 @@ impl Step for Std {
175185

176186
let mut target_deps = builder.ensure(StartupObjects { compiler, target });
177187

178-
let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
179-
if compiler_to_use != compiler {
188+
let mut compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
189+
190+
// We only need to build std twice; therefore,
191+
// - If stage 2 std is built, the artifacts are fully up to date. For the next stages, uplift the artifacts instead of compiling std again.
192+
// - If `--build-std-on-stage0` was used it means std was already built on stage 0. So, if stage 1 is built, that means std is fully up to date.
193+
// For the next stages, uplift the artifacts instead of compiling std again.
194+
if (compiler.stage > 2 || builder.config.build_std_on_stage0) && compiler_to_use != compiler
195+
{
196+
// If stage 2 std was built (not uplifted), uplift the artifacts from stage 2.
197+
// (This is necessary; without this, they will be uplifted from stage 1).
198+
if !builder.config.build_std_on_stage0 && compiler.stage > 2 {
199+
compiler_to_use.stage = 2;
200+
}
201+
180202
builder.ensure(Std::new(compiler_to_use, target));
181203
let msg = if compiler_to_use.host == target {
182204
format!(
@@ -585,18 +607,16 @@ impl Step for StdLink {
585607
(libdir, hostdir)
586608
};
587609

588-
add_to_sysroot(builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target));
610+
let is_downloaded_beta_stage0 = builder
611+
.build
612+
.config
613+
.initial_rustc
614+
.starts_with(builder.out.join(&compiler.host.triple).join("stage0/bin"));
589615

590616
// Special case for stage0, to make `rustup toolchain link` and `x dist --stage 0`
591617
// work for stage0-sysroot. We only do this if the stage0 compiler comes from beta,
592618
// and is not set to a custom path.
593-
if compiler.stage == 0
594-
&& builder
595-
.build
596-
.config
597-
.initial_rustc
598-
.starts_with(builder.out.join(&compiler.host.triple).join("stage0/bin"))
599-
{
619+
if compiler.stage == 0 && is_downloaded_beta_stage0 && !builder.config.build_std_on_stage0 {
600620
// Copy bin files from stage0/bin to stage0-sysroot/bin
601621
let sysroot = builder.out.join(&compiler.host.triple).join("stage0-sysroot");
602622

@@ -608,7 +628,10 @@ impl Step for StdLink {
608628

609629
// Copy all *.so files from stage0/lib to stage0-sysroot/lib
610630
let stage0_lib_dir = builder.out.join(&host).join("stage0/lib");
611-
if let Ok(files) = fs::read_dir(&stage0_lib_dir) {
631+
632+
if !builder.config.build_std_on_stage0 {
633+
builder.cp_r(&stage0_lib_dir, &sysroot.join("lib"));
634+
} else if let Ok(files) = fs::read_dir(&stage0_lib_dir) {
612635
for file in files {
613636
let file = t!(file);
614637
let path = file.path();
@@ -630,6 +653,11 @@ impl Step for StdLink {
630653
if stage0_codegen_backends.exists() {
631654
builder.cp_r(&stage0_codegen_backends, &sysroot_codegen_backends);
632655
}
656+
} else if compiler.stage == 0 && !builder.config.build_std_on_stage0 {
657+
let sysroot = builder.out.join(&compiler.host.triple).join("stage0-sysroot");
658+
builder.cp_r(&builder.initial_sysroot.join("lib"), &sysroot.join("lib"));
659+
} else {
660+
add_to_sysroot(builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target));
633661
}
634662
}
635663
}

src/bootstrap/src/core/build_steps/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ impl Step for Tidy {
11211121
if builder.config.channel == "dev" || builder.config.channel == "nightly" {
11221122
builder.info("fmt check");
11231123
if builder.initial_rustfmt().is_none() {
1124-
let inferred_rustfmt_dir = builder.initial_rustc.parent().unwrap();
1124+
let inferred_rustfmt_dir = builder.initial_sysroot.join("bin");
11251125
eprintln!(
11261126
"\
11271127
ERROR: no `rustfmt` binary found in {PATH}

src/bootstrap/src/core/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ impl<'a> Builder<'a> {
11671167
}
11681168

11691169
pub fn cargo_clippy_cmd(&self, run_compiler: Compiler) -> Command {
1170-
let initial_sysroot_bin = self.initial_rustc.parent().unwrap();
1170+
let initial_sysroot_bin = self.initial_sysroot.join("bin");
11711171
// Set PATH to include the sysroot bin dir so clippy can find cargo.
11721172
// FIXME: once rust-clippy#11944 lands on beta, set `CARGO` directly instead.
11731173
let path = t!(env::join_paths(

src/bootstrap/src/core/config/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ pub struct Config {
186186

187187
pub on_fail: Option<String>,
188188
pub stage: u32,
189+
pub build_std_on_stage0: bool,
189190
pub keep_stage: Vec<u32>,
190191
pub keep_stage_std: Vec<u32>,
191192
pub src: PathBuf,
@@ -1219,6 +1220,7 @@ impl Config {
12191220
config.incremental = flags.incremental;
12201221
config.dry_run = if flags.dry_run { DryRun::UserSelected } else { DryRun::Disabled };
12211222
config.dump_bootstrap_shims = flags.dump_bootstrap_shims;
1223+
config.build_std_on_stage0 = flags.build_std_on_stage0;
12221224
config.keep_stage = flags.keep_stage;
12231225
config.keep_stage_std = flags.keep_stage_std;
12241226
config.color = flags.color;

src/bootstrap/src/core/config/flags.rs

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ pub struct Flags {
9292
/// stage to build (indicates compiler to use/test, e.g., stage 0 uses the
9393
/// bootstrap compiler, stage 1 the stage 0 rustc artifacts, etc.)
9494
pub stage: Option<u32>,
95+
/// Useful for library testing with the beta compiler (without building the stage 1 compiler)
96+
#[arg(global(true), long)]
97+
pub build_std_on_stage0: bool,
9598

9699
#[arg(global(true), value_hint = clap::ValueHint::Other, long, value_name = "N")]
97100
/// stage(s) to keep without recompiling

src/bootstrap/src/utils/change_tracker.rs

+5
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
111111
severity: ChangeSeverity::Warning,
112112
summary: "A new `optimized-compiler-builtins` option has been introduced. Whether to build llvm's `compiler-rt` from source is no longer implicitly controlled by git state. See the PR for more details.",
113113
},
114+
ChangeInfo {
115+
change_id: 119899,
116+
severity: ChangeSeverity::Info,
117+
summary: "Stage 0 std is now same as beta std rather than the compiled version from source (which is now stage 1). If you want to test/build std on stage 0, use `--build-std-on-stage0` flag (e.g., `x test --stage0 --build-std-on-stage0 alloc`).",
118+
},
114119
];

src/ci/docker/host-x86_64/i686-gnu-nopt/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ RUN echo "[rust]" > /config/nopt-std-config.toml
2727
RUN echo "optimize = false" >> /config/nopt-std-config.toml
2828

2929
ENV RUST_CONFIGURE_ARGS --build=i686-unknown-linux-gnu --disable-optimize-tests
30-
ENV SCRIPT python3 ../x.py test --stage 0 --config /config/nopt-std-config.toml library/std \
30+
ENV SCRIPT python3 ../x.py test --build-std-on-stage0 --stage 0 --config /config/nopt-std-config.toml library/std \
3131
&& python3 ../x.py --stage 2 test

src/ci/docker/host-x86_64/mingw-check/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ENV SCRIPT python3 ../x.py --stage 2 test src/tools/expand-yaml-anchors && \
4949
python3 ../x.py clippy --stage 0 -Awarnings && \
5050
python3 ../x.py build --stage 0 src/tools/build-manifest && \
5151
python3 ../x.py test --stage 0 src/tools/compiletest && \
52-
python3 ../x.py test --stage 0 core alloc std test proc_macro && \
52+
python3 ../x.py test --build-std-on-stage0 --stage 0 core alloc std test proc_macro && \
5353
# Build both public and internal documentation.
5454
RUSTDOCFLAGS=\"--document-private-items --document-hidden-items\" python3 ../x.py doc --stage 0 library && \
5555
mkdir -p /checkout/obj/staging/doc && \

src/ci/docker/host-x86_64/x86_64-gnu-nopt/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ RUN echo "optimize = false" >> /config/nopt-std-config.toml
2929
ENV RUST_CONFIGURE_ARGS --build=x86_64-unknown-linux-gnu \
3030
--disable-optimize-tests \
3131
--set rust.test-compare-mode
32-
ENV SCRIPT python3 ../x.py test --stage 0 --config /config/nopt-std-config.toml library/std \
32+
ENV SCRIPT python3 ../x.py test --build-std-on-stage0 --stage 0 --config /config/nopt-std-config.toml library/std \
3333
&& python3 ../x.py --stage 2 test

0 commit comments

Comments
 (0)