Skip to content

Commit f06e41e

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

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
});
@@ -140,11 +140,6 @@ impl Step for Std {
140140
}
141141
}
142142

143-
fn default_compiler_for_checking_rustc(builder: &Builder<'_>) -> Compiler {
144-
// When checking the stage N compiler, we want to do it with the stage N-1 compiler,
145-
builder.compiler(builder.top_stage - 1, builder.config.host_target)
146-
}
147-
148143
/// Checks rustc using `build_compiler` and copies the built
149144
/// .rmeta files into the sysroot of `build_compiler`.
150145
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -184,7 +179,7 @@ impl Step for Rustc {
184179
let crates = run.make_run_crates(Alias::Compiler);
185180
run.builder.ensure(Rustc {
186181
target: run.target,
187-
build_compiler: default_compiler_for_checking_rustc(run.builder),
182+
build_compiler: prepare_compiler_for_check(run.builder, run.target, Mode::Rustc),
188183
crates,
189184
});
190185
}
@@ -195,11 +190,6 @@ impl Step for Rustc {
195190
/// the `compiler` targeting the `target` architecture. The artifacts
196191
/// created will also be linked into the sysroot directory.
197192
fn run(self, builder: &Builder<'_>) {
198-
if builder.top_stage < 2 && builder.config.host_target != self.target {
199-
eprintln!("Cannot do a cross-compilation check on stage 1, use stage 2");
200-
exit!(1);
201-
}
202-
203193
let build_compiler = self.build_compiler;
204194
let target = self.target;
205195

@@ -251,13 +241,45 @@ impl Step for Rustc {
251241
}
252242
}
253243

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

263285
/// Checks a single codegen backend.
@@ -279,7 +301,7 @@ impl Step for CodegenBackend {
279301

280302
fn make_run(run: RunConfig<'_>) {
281303
// FIXME: only check the backend(s) that were actually selected in run.paths
282-
let build_compiler = prepare_compiler_for_tool_rustc(run.builder, run.target);
304+
let build_compiler = prepare_compiler_for_check(run.builder, run.target, Mode::Codegen);
283305
for &backend in &["cranelift", "gcc"] {
284306
run.builder.ensure(CodegenBackend { build_compiler, target: run.target, backend });
285307
}
@@ -347,7 +369,7 @@ impl Step for RustAnalyzer {
347369
}
348370

349371
fn make_run(run: RunConfig<'_>) {
350-
let build_compiler = prepare_compiler_for_tool_rustc(run.builder, run.target);
372+
let build_compiler = prepare_compiler_for_check(run.builder, run.target, Mode::ToolRustc);
351373
run.builder.ensure(RustAnalyzer { build_compiler, target: run.target });
352374
}
353375

@@ -412,19 +434,11 @@ impl Step for Compiletest {
412434
} else {
413435
Mode::ToolStd
414436
};
415-
416-
let compiler = builder.compiler(
417-
if mode == Mode::ToolBootstrap { 0 } else { builder.top_stage },
418-
builder.config.host_target,
419-
);
420-
421-
if mode != Mode::ToolBootstrap {
422-
builder.std(compiler, self.target);
423-
}
437+
let build_compiler = prepare_compiler_for_check(builder, self.target, mode);
424438

425439
let mut cargo = prepare_tool_cargo(
426440
builder,
427-
compiler,
441+
build_compiler,
428442
mode,
429443
self.target,
430444
builder.kind,
@@ -437,7 +451,7 @@ impl Step for Compiletest {
437451

438452
cargo.arg("--all-targets");
439453

440-
let stamp = BuildStamp::new(&builder.cargo_out(compiler, mode, self.target))
454+
let stamp = BuildStamp::new(&builder.cargo_out(build_compiler, mode, self.target))
441455
.with_prefix("compiletest-check");
442456

443457
let _guard = builder.msg_check("compiletest artifacts", self.target, None);
@@ -477,23 +491,8 @@ macro_rules! tool_check_step {
477491
}
478492

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

0 commit comments

Comments
 (0)