Skip to content

Commit aaf6016

Browse files
committed
Unify selection of build compiler for checking in a single function
1 parent de6a380 commit aaf6016

File tree

1 file changed

+47
-48
lines changed
  • src/bootstrap/src/core/build_steps

1 file changed

+47
-48
lines changed

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

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Step for Std {
5050
fn make_run(run: RunConfig<'_>) {
5151
let crates = std_crates_for_run_make(&run);
5252
run.builder.ensure(Std {
53-
build_compiler: run.builder.compiler(run.builder.top_stage, run.target),
53+
build_compiler: prepare_compiler_for_check(run.builder, run.target, Mode::Std),
5454
target: run.target,
5555
crates,
5656
});
@@ -138,11 +138,6 @@ impl Step for Std {
138138
}
139139
}
140140

141-
fn default_compiler_for_checking_rustc(builder: &Builder<'_>) -> Compiler {
142-
// When checking the stage N compiler, we want to do it with the stage N-1 compiler,
143-
builder.compiler(builder.top_stage - 1, builder.config.host_target)
144-
}
145-
146141
/// Checks rustc using `build_compiler` and copies the built
147142
/// .rmeta files into the sysroot of `build_compiler`.
148143
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -182,7 +177,7 @@ impl Step for Rustc {
182177
let crates = run.make_run_crates(Alias::Compiler);
183178
run.builder.ensure(Rustc {
184179
target: run.target,
185-
build_compiler: default_compiler_for_checking_rustc(run.builder),
180+
build_compiler: prepare_compiler_for_check(run.builder, run.target, Mode::Rustc),
186181
crates,
187182
});
188183
}
@@ -193,11 +188,6 @@ impl Step for Rustc {
193188
/// the `compiler` targeting the `target` architecture. The artifacts
194189
/// created will also be linked into the sysroot directory.
195190
fn run(self, builder: &Builder<'_>) {
196-
if builder.top_stage < 2 && builder.config.host_target != self.target {
197-
eprintln!("Cannot do a cross-compilation check on stage 1, use stage 2");
198-
exit!(1);
199-
}
200-
201191
let build_compiler = self.build_compiler;
202192
let target = self.target;
203193

@@ -249,13 +239,45 @@ impl Step for Rustc {
249239
}
250240
}
251241

252-
/// Prepares a build compiler sysroot that will check a `Mode::ToolRustc` tool.
253-
/// Also checks rustc using this compiler, to prepare .rmetas that the tool will link to.
254-
fn prepare_compiler_for_tool_rustc(builder: &Builder<'_>, target: TargetSelection) -> Compiler {
255-
// When we check tool stage N, we check it with compiler stage N-1
256-
let build_compiler = builder.compiler(builder.top_stage - 1, builder.config.host_target);
257-
builder.ensure(Rustc::new(builder, build_compiler, target));
258-
build_compiler
242+
/// Prepares a compiler that will check something with the given `mode`.
243+
fn prepare_compiler_for_check(
244+
builder: &Builder<'_>,
245+
target: TargetSelection,
246+
mode: Mode,
247+
) -> Compiler {
248+
let host = builder.host_target;
249+
match mode {
250+
Mode::ToolBootstrap => builder.compiler(0, host),
251+
Mode::ToolStd => {
252+
// A small number of tools rely on in-tree standard
253+
// library crates (e.g. compiletest needs libtest).
254+
let build_compiler = builder.compiler(builder.top_stage, host);
255+
builder.std(build_compiler, host);
256+
builder.std(build_compiler, target);
257+
build_compiler
258+
}
259+
Mode::ToolRustc | Mode::Codegen => {
260+
// When checking tool stage N, we check it with compiler stage N-1
261+
let build_compiler = builder.compiler(builder.top_stage - 1, host);
262+
builder.ensure(Rustc::new(builder, build_compiler, target));
263+
build_compiler
264+
}
265+
Mode::Rustc => {
266+
if builder.top_stage < 2 && host != target {
267+
eprintln!("Cannot do a cross-compilation check of rustc on stage 1, use stage 2");
268+
exit!(1);
269+
}
270+
271+
// When checking the stage N compiler, we want to do it with the stage N-1 compiler
272+
builder.compiler(builder.top_stage - 1, host)
273+
}
274+
Mode::Std => {
275+
// When checking std stage N, we want to do it with the stage N compiler
276+
// Note: we don't need to build the host stdlib here, because when compiling std, the
277+
// stage 0 stdlib is used to compile build scripts and proc macros.
278+
builder.compiler(builder.top_stage, host)
279+
}
280+
}
259281
}
260282

261283
/// Checks a single codegen backend.
@@ -277,7 +299,7 @@ impl Step for CodegenBackend {
277299

278300
fn make_run(run: RunConfig<'_>) {
279301
// FIXME: only check the backend(s) that were actually selected in run.paths
280-
let build_compiler = prepare_compiler_for_tool_rustc(run.builder, run.target);
302+
let build_compiler = prepare_compiler_for_check(run.builder, run.target, Mode::Codegen);
281303
for &backend in &["cranelift", "gcc"] {
282304
run.builder.ensure(CodegenBackend { build_compiler, target: run.target, backend });
283305
}
@@ -345,7 +367,7 @@ impl Step for RustAnalyzer {
345367
}
346368

347369
fn make_run(run: RunConfig<'_>) {
348-
let build_compiler = prepare_compiler_for_tool_rustc(run.builder, run.target);
370+
let build_compiler = prepare_compiler_for_check(run.builder, run.target, Mode::ToolRustc);
349371
run.builder.ensure(RustAnalyzer { build_compiler, target: run.target });
350372
}
351373

@@ -410,19 +432,11 @@ impl Step for Compiletest {
410432
} else {
411433
Mode::ToolStd
412434
};
413-
414-
let compiler = builder.compiler(
415-
if mode == Mode::ToolBootstrap { 0 } else { builder.top_stage },
416-
builder.config.host_target,
417-
);
418-
419-
if mode != Mode::ToolBootstrap {
420-
builder.std(compiler, self.target);
421-
}
435+
let build_compiler = prepare_compiler_for_check(builder, self.target, mode);
422436

423437
let mut cargo = prepare_tool_cargo(
424438
builder,
425-
compiler,
439+
build_compiler,
426440
mode,
427441
self.target,
428442
builder.kind,
@@ -435,7 +449,7 @@ impl Step for Compiletest {
435449

436450
cargo.arg("--all-targets");
437451

438-
let stamp = BuildStamp::new(&builder.cargo_out(compiler, mode, self.target))
452+
let stamp = BuildStamp::new(&builder.cargo_out(build_compiler, mode, self.target))
439453
.with_prefix("compiletest-check");
440454

441455
let _guard = builder.msg_check("compiletest artifacts", self.target, None);
@@ -475,23 +489,8 @@ macro_rules! tool_check_step {
475489
}
476490

477491
fn make_run(run: RunConfig<'_>) {
478-
let host = run.builder.config.host_target;
479492
let target = run.target;
480-
let build_compiler = match $mode {
481-
Mode::ToolBootstrap => run.builder.compiler(0, host),
482-
Mode::ToolStd => {
483-
// A small number of tools rely on in-tree standard
484-
// library crates (e.g. compiletest needs libtest).
485-
let build_compiler = run.builder.compiler(run.builder.top_stage, host);
486-
run.builder.std(build_compiler, host);
487-
run.builder.std(build_compiler, target);
488-
build_compiler
489-
}
490-
Mode::ToolRustc => {
491-
prepare_compiler_for_tool_rustc(run.builder, target)
492-
}
493-
_ => panic!("unexpected mode for tool check step: {:?}", $mode),
494-
};
493+
let build_compiler = prepare_compiler_for_check(run.builder, target, $mode);
495494
run.builder.ensure($name { target, build_compiler });
496495
}
497496

0 commit comments

Comments
 (0)