Skip to content

Commit 4ddb1ac

Browse files
committed
let ToolBuild to handle compiler tool stages automatically
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 234a2b5 commit 4ddb1ac

File tree

3 files changed

+19
-33
lines changed

3 files changed

+19
-33
lines changed

src/bootstrap/src/core/build_steps/run.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl Step for Miri {
126126
// This compiler runs on the host, we'll just use it for the target.
127127
let target_compiler = builder.compiler(stage, host);
128128
// Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
129-
// we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
129+
// we'd have stageN/bin/rustc and stageN/bin/miri be effectively different stage
130130
// compilers, which isn't what we want. Rustdoc should be linked in the same way as the
131131
// rustc compiler it's paired with, so it must be built with the previous stage compiler.
132132
let host_compiler = builder.compiler(stage - 1, host);

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

+8-18
Original file line numberDiff line numberDiff line change
@@ -521,23 +521,24 @@ impl Step for Miri {
521521

522522
// This compiler runs on the host, we'll just use it for the target.
523523
let target_compiler = builder.compiler(stage, host);
524-
// Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
525-
// we'd have stageN/bin/rustc and stageN/bin/rustdoc be effectively different stage
526-
// compilers, which isn't what we want. Rustdoc should be linked in the same way as the
527-
// rustc compiler it's paired with, so it must be built with the previous stage compiler.
528-
let host_compiler = builder.compiler(stage - 1, host);
529524

530525
// Build our tools.
531-
let miri = builder.ensure(tool::Miri { compiler: host_compiler, target: host });
526+
let miri = builder.ensure(tool::Miri { compiler: target_compiler, target: host });
532527
// the ui tests also assume cargo-miri has been built
533-
builder.ensure(tool::CargoMiri { compiler: host_compiler, target: host });
528+
builder.ensure(tool::CargoMiri { compiler: target_compiler, target: host });
534529

535530
// We also need sysroots, for Miri and for the host (the latter for build scripts).
536531
// This is for the tests so everything is done with the target compiler.
537532
let miri_sysroot = Miri::build_miri_sysroot(builder, target_compiler, target);
538533
builder.ensure(compile::Std::new(target_compiler, host));
539534
let host_sysroot = builder.sysroot(target_compiler);
540535

536+
// Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
537+
// we'd have stageN/bin/rustc and stageN/bin/miri be effectively different stage
538+
// compilers, which isn't what we want. Rustdoc should be linked in the same way as the
539+
// rustc compiler it's paired with, so it must be built with the previous stage compiler.
540+
let host_compiler = builder.compiler(stage - 1, host);
541+
541542
// Miri has its own "target dir" for ui test dependencies. Make sure it gets cleared when
542543
// the sysroot gets rebuilt, to avoid "found possibly newer version of crate `std`" errors.
543544
if !builder.config.dry_run() {
@@ -1715,17 +1716,6 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
17151716
// If we're using `--stage 0`, we should provide the bootstrap cargo.
17161717
builder.initial_cargo.clone()
17171718
} else {
1718-
// We need to properly build cargo using the suitable stage compiler.
1719-
1720-
let compiler = builder.download_rustc().then_some(compiler).unwrap_or_else(||
1721-
// HACK: currently tool stages are off-by-one compared to compiler stages, i.e. if
1722-
// you give `tool::Cargo` a stage 1 rustc, it will cause stage 2 rustc to be built
1723-
// and produce a cargo built with stage 2 rustc. To fix this, we need to chop off
1724-
// the compiler stage by 1 to align with expected `./x test run-make --stage N`
1725-
// behavior, i.e. we need to pass `N - 1` compiler stage to cargo. See also Miri
1726-
// which does a similar hack.
1727-
builder.compiler(builder.top_stage - 1, compiler.host));
1728-
17291719
builder.ensure(tool::Cargo { compiler, target: compiler.host })
17301720
};
17311721

src/bootstrap/src/core/builder/mod.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -1351,8 +1351,8 @@ impl<'a> Builder<'a> {
13511351
self.ensure(tool::Rustdoc { compiler })
13521352
}
13531353

1354-
pub fn cargo_clippy_cmd(&self, run_compiler: Compiler) -> BootstrapCommand {
1355-
if run_compiler.stage == 0 {
1354+
pub fn cargo_clippy_cmd(&self, compiler: Compiler) -> BootstrapCommand {
1355+
if compiler.stage == 0 {
13561356
let cargo_clippy = self
13571357
.config
13581358
.initial_cargo_clippy
@@ -1364,27 +1364,23 @@ impl<'a> Builder<'a> {
13641364
return cmd;
13651365
}
13661366

1367-
let build_compiler = self.compiler(run_compiler.stage - 1, self.build.build);
1368-
self.ensure(tool::Clippy { compiler: build_compiler, target: self.build.build });
1369-
let cargo_clippy =
1370-
self.ensure(tool::CargoClippy { compiler: build_compiler, target: self.build.build });
1367+
self.ensure(tool::Clippy { compiler, target: self.build.build });
1368+
let cargo_clippy = self.ensure(tool::CargoClippy { compiler, target: self.build.build });
13711369
let mut dylib_path = helpers::dylib_path();
1372-
dylib_path.insert(0, self.sysroot(run_compiler).join("lib"));
1370+
dylib_path.insert(0, self.sysroot(compiler).join("lib"));
13731371

13741372
let mut cmd = command(cargo_clippy);
13751373
cmd.env(helpers::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
13761374
cmd.env("CARGO", &self.initial_cargo);
13771375
cmd
13781376
}
13791377

1380-
pub fn cargo_miri_cmd(&self, run_compiler: Compiler) -> BootstrapCommand {
1381-
assert!(run_compiler.stage > 0, "miri can not be invoked at stage 0");
1382-
let build_compiler = self.compiler(run_compiler.stage - 1, self.build.build);
1378+
pub fn cargo_miri_cmd(&self, compiler: Compiler) -> BootstrapCommand {
1379+
assert!(compiler.stage > 0, "miri can not be invoked at stage 0");
13831380

13841381
// Prepare the tools
1385-
let miri = self.ensure(tool::Miri { compiler: build_compiler, target: self.build.build });
1386-
let cargo_miri =
1387-
self.ensure(tool::CargoMiri { compiler: build_compiler, target: self.build.build });
1382+
let miri = self.ensure(tool::Miri { compiler, target: self.build.build });
1383+
let cargo_miri = self.ensure(tool::CargoMiri { compiler, target: self.build.build });
13881384
// Invoke cargo-miri, make sure it can find miri and cargo.
13891385
let mut cmd = command(cargo_miri);
13901386
cmd.env("MIRI", &miri);
@@ -1397,7 +1393,7 @@ impl<'a> Builder<'a> {
13971393
// `add_rustc_lib_path` as that's a NOP on Windows but we do need these libraries added to
13981394
// the PATH due to the stage mismatch.
13991395
// Also see https://github.com/rust-lang/rust/pull/123192#issuecomment-2028901503.
1400-
add_dylib_path(self.rustc_lib_paths(run_compiler), &mut cmd);
1396+
add_dylib_path(self.rustc_lib_paths(compiler), &mut cmd);
14011397
cmd
14021398
}
14031399

0 commit comments

Comments
 (0)