Skip to content

Commit 7d68e7b

Browse files
authored
Rollup merge of rust-lang#144303 - Kobzol:bootstrap-tool-cleanup, r=jieyouxu
Consolidate staging for `rustc_private` tools This PR continues bootstrap refactoring, this time by consolidating staging for `Mode::ToolRustc` tools. This refactoring was in the critical path of refactoring `test`/`dist`/`clippy`/`doc` steps, and getting rid of the rmeta/rlib sysroot copy, because tools are pervasive and they are being used for a lot of things in bootstrap. The main idea is to explicitly model the fact that a stage N `Mode::ToolRustc` tool always works with two different compilers: - Stage N-1 rustc (`build_compiler`) builds stage N rustc (`target_compiler`) - Rlib artifacts from stage N rustc are copied to the sysroot of stage N-1 rustc - Stage N-1 rustc builds the (stage N) tool itself, the tool links to the rlib artifacts of the stage N rustc Before, the code often used `compiler`, which meant sometimes the build compiler, sometimes the target compiler, and sometimes neither (looking at you, `download-rustc`). This is especially annoying when you get to a situation where you have an install step that invokes a dist step that invokes a tool build step, where *some* compiler is being propagated through, without it being clear what does that compiler represent. This refactoring hopefully makes that clearer and more explicit. It also gets rid of a few `builder.ensure(Rustc(...))` calls within bootstrap, which is always nice. `Rustdoc` needs to be handled a bit specially, because it acts as a compiler itself, I documented that in the changes. It wasn't practical to do these refactorings in multiple PRs, so I did it all in one PR. The meat of the change is 9ee6d1c. I tested manually that `x build rustdoc` and `x build miri` still works even with `download-rustc`, although I cannot promise any extra support for `download-rustc`, IMO we will just have to reimplement it from scratch in a different way. As usually, I did some drive-by refactorings to bootstrap, trying to document and clarify things, add more step metadata and tests. Best reviewed commit-by-commit (note that I renamed `link_compiler` to `target_compiler`, in accordance to the rest of bootstrap, in the last commit). r? ````@jieyouxu````
2 parents b0af8ce + 7bf4a1a commit 7d68e7b

File tree

12 files changed

+585
-406
lines changed

12 files changed

+585
-406
lines changed

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

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use serde_derive::Deserialize;
1919
use tracing::{instrument, span};
2020

2121
use crate::core::build_steps::gcc::{Gcc, add_cg_gcc_cargo_flags};
22-
use crate::core::build_steps::tool::{SourceType, copy_lld_artifacts};
22+
use crate::core::build_steps::tool::{RustcPrivateCompilers, SourceType, copy_lld_artifacts};
2323
use crate::core::build_steps::{dist, llvm};
2424
use crate::core::builder;
2525
use crate::core::builder::{
@@ -1128,7 +1128,7 @@ impl Step for Rustc {
11281128
cargo.env("RUSTC_BOLT_LINK_FLAGS", "1");
11291129
}
11301130

1131-
let _guard = builder.msg_sysroot_tool(
1131+
let _guard = builder.msg_rustc_tool(
11321132
Kind::Build,
11331133
build_compiler.stage,
11341134
format_args!("compiler artifacts{}", crate_description(&self.crates)),
@@ -1541,9 +1541,8 @@ impl Step for RustcLink {
15411541

15421542
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15431543
pub struct CodegenBackend {
1544-
pub target: TargetSelection,
1545-
pub compiler: Compiler,
1546-
pub backend: String,
1544+
compilers: RustcPrivateCompilers,
1545+
backend: String,
15471546
}
15481547

15491548
fn needs_codegen_config(run: &RunConfig<'_>) -> bool {
@@ -1607,8 +1606,11 @@ impl Step for CodegenBackend {
16071606
}
16081607

16091608
run.builder.ensure(CodegenBackend {
1610-
target: run.target,
1611-
compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
1609+
compilers: RustcPrivateCompilers::new(
1610+
run.builder,
1611+
run.builder.top_stage,
1612+
run.target,
1613+
),
16121614
backend: backend.clone(),
16131615
});
16141616
}
@@ -1621,20 +1623,17 @@ impl Step for CodegenBackend {
16211623
name = "CodegenBackend::run",
16221624
skip_all,
16231625
fields(
1624-
compiler = ?self.compiler,
1625-
target = ?self.target,
1626-
backend = ?self.target,
1626+
compilers = ?self.compilers,
1627+
backend = ?self.backend,
16271628
),
16281629
),
16291630
)]
16301631
fn run(self, builder: &Builder<'_>) {
1631-
let compiler = self.compiler;
1632-
let target = self.target;
16331632
let backend = self.backend;
1633+
let target = self.compilers.target();
1634+
let build_compiler = self.compilers.build_compiler();
16341635

1635-
builder.ensure(Rustc::new(compiler, target));
1636-
1637-
if builder.config.keep_stage.contains(&compiler.stage) {
1636+
if builder.config.keep_stage.contains(&build_compiler.stage) {
16381637
trace!("`keep-stage` requested");
16391638
builder.info(
16401639
"WARNING: Using a potentially old codegen backend. \
@@ -1645,17 +1644,11 @@ impl Step for CodegenBackend {
16451644
return;
16461645
}
16471646

1648-
let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
1649-
if compiler_to_use != compiler {
1650-
builder.ensure(CodegenBackend { compiler: compiler_to_use, target, backend });
1651-
return;
1652-
}
1653-
1654-
let out_dir = builder.cargo_out(compiler, Mode::Codegen, target);
1647+
let out_dir = builder.cargo_out(build_compiler, Mode::Codegen, target);
16551648

16561649
let mut cargo = builder::Cargo::new(
16571650
builder,
1658-
compiler,
1651+
build_compiler,
16591652
Mode::Codegen,
16601653
SourceType::InTree,
16611654
target,
@@ -1676,7 +1669,13 @@ impl Step for CodegenBackend {
16761669

16771670
let tmp_stamp = BuildStamp::new(&out_dir).with_prefix("tmp");
16781671

1679-
let _guard = builder.msg_build(compiler, format_args!("codegen backend {backend}"), target);
1672+
let _guard = builder.msg_rustc_tool(
1673+
Kind::Build,
1674+
build_compiler.stage,
1675+
format_args!("codegen backend {backend}"),
1676+
build_compiler.host,
1677+
target,
1678+
);
16801679
let files = run_cargo(builder, cargo, vec![], &tmp_stamp, vec![], false, false);
16811680
if builder.config.dry_run() {
16821681
return;
@@ -1696,10 +1695,20 @@ impl Step for CodegenBackend {
16961695
f.display()
16971696
);
16981697
}
1699-
let stamp = build_stamp::codegen_backend_stamp(builder, compiler, target, &backend);
1698+
let stamp = build_stamp::codegen_backend_stamp(builder, build_compiler, target, &backend);
17001699
let codegen_backend = codegen_backend.to_str().unwrap();
17011700
t!(stamp.add_stamp(codegen_backend).write());
17021701
}
1702+
1703+
fn metadata(&self) -> Option<StepMetadata> {
1704+
Some(
1705+
StepMetadata::build(
1706+
&format!("rustc_codegen_{}", self.backend),
1707+
self.compilers.target(),
1708+
)
1709+
.built_by(self.compilers.build_compiler()),
1710+
)
1711+
}
17031712
}
17041713

17051714
/// Creates the `codegen-backends` folder for a compiler that's about to be
@@ -2186,8 +2195,10 @@ impl Step for Assemble {
21862195
continue;
21872196
}
21882197
builder.ensure(CodegenBackend {
2189-
compiler: build_compiler,
2190-
target: target_compiler.host,
2198+
compilers: RustcPrivateCompilers::from_build_and_target_compiler(
2199+
build_compiler,
2200+
target_compiler,
2201+
),
21912202
backend: backend.clone(),
21922203
});
21932204
}

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use object::read::archive::ArchiveFile;
2020
use tracing::instrument;
2121

2222
use crate::core::build_steps::doc::DocumentationFormat;
23-
use crate::core::build_steps::tool::{self, Tool};
23+
use crate::core::build_steps::tool::{self, RustcPrivateCompilers, Tool};
2424
use crate::core::build_steps::vendor::{VENDOR_DIR, Vendor};
2525
use crate::core::build_steps::{compile, llvm};
2626
use crate::core::builder::{Builder, Kind, RunConfig, ShouldRun, Step, StepMetadata};
@@ -425,19 +425,20 @@ impl Step for Rustc {
425425
.as_ref()
426426
.is_none_or(|tools| tools.iter().any(|tool| tool == "rustdoc"))
427427
{
428-
let rustdoc = builder.rustdoc(compiler);
428+
let rustdoc = builder.rustdoc_for_compiler(compiler);
429429
builder.install(&rustdoc, &image.join("bin"), FileType::Executable);
430430
}
431431

432432
let ra_proc_macro_srv_compiler =
433433
builder.compiler_for(compiler.stage, builder.config.host_target, compiler.host);
434-
builder.ensure(compile::Rustc::new(ra_proc_macro_srv_compiler, compiler.host));
434+
let compilers = RustcPrivateCompilers::from_build_compiler(
435+
builder,
436+
ra_proc_macro_srv_compiler,
437+
compiler.host,
438+
);
435439

436440
if let Some(ra_proc_macro_srv) = builder.ensure_if_default(
437-
tool::RustAnalyzerProcMacroSrv {
438-
compiler: ra_proc_macro_srv_compiler,
439-
target: compiler.host,
440-
},
441+
tool::RustAnalyzerProcMacroSrv::from_compilers(compilers),
441442
builder.kind,
442443
) {
443444
let dst = image.join("libexec");
@@ -1228,7 +1229,7 @@ impl Step for Cargo {
12281229

12291230
#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
12301231
pub struct RustAnalyzer {
1231-
pub compiler: Compiler,
1232+
pub build_compiler: Compiler,
12321233
pub target: TargetSelection,
12331234
}
12341235

@@ -1244,7 +1245,7 @@ impl Step for RustAnalyzer {
12441245

12451246
fn make_run(run: RunConfig<'_>) {
12461247
run.builder.ensure(RustAnalyzer {
1247-
compiler: run.builder.compiler_for(
1248+
build_compiler: run.builder.compiler_for(
12481249
run.builder.top_stage,
12491250
run.builder.config.host_target,
12501251
run.target,
@@ -1254,12 +1255,11 @@ impl Step for RustAnalyzer {
12541255
}
12551256

12561257
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
1257-
let compiler = self.compiler;
12581258
let target = self.target;
1259+
let compilers =
1260+
RustcPrivateCompilers::from_build_compiler(builder, self.build_compiler, self.target);
12591261

1260-
builder.ensure(compile::Rustc::new(compiler, target));
1261-
1262-
let rust_analyzer = builder.ensure(tool::RustAnalyzer { compiler, target });
1262+
let rust_analyzer = builder.ensure(tool::RustAnalyzer::from_compilers(compilers));
12631263

12641264
let mut tarball = Tarball::new(builder, "rust-analyzer", &target.triple);
12651265
tarball.set_overlay(OverlayKind::RustAnalyzer);
@@ -1270,9 +1270,9 @@ impl Step for RustAnalyzer {
12701270
}
12711271
}
12721272

1273-
#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
1273+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
12741274
pub struct Clippy {
1275-
pub compiler: Compiler,
1275+
pub build_compiler: Compiler,
12761276
pub target: TargetSelection,
12771277
}
12781278

@@ -1288,7 +1288,7 @@ impl Step for Clippy {
12881288

12891289
fn make_run(run: RunConfig<'_>) {
12901290
run.builder.ensure(Clippy {
1291-
compiler: run.builder.compiler_for(
1291+
build_compiler: run.builder.compiler_for(
12921292
run.builder.top_stage,
12931293
run.builder.config.host_target,
12941294
run.target,
@@ -1298,16 +1298,15 @@ impl Step for Clippy {
12981298
}
12991299

13001300
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
1301-
let compiler = self.compiler;
13021301
let target = self.target;
1303-
1304-
builder.ensure(compile::Rustc::new(compiler, target));
1302+
let compilers =
1303+
RustcPrivateCompilers::from_build_compiler(builder, self.build_compiler, target);
13051304

13061305
// Prepare the image directory
13071306
// We expect clippy to build, because we've exited this step above if tool
13081307
// state for clippy isn't testing.
1309-
let clippy = builder.ensure(tool::Clippy { compiler, target });
1310-
let cargoclippy = builder.ensure(tool::CargoClippy { compiler, target });
1308+
let clippy = builder.ensure(tool::Clippy::from_compilers(compilers));
1309+
let cargoclippy = builder.ensure(tool::CargoClippy::from_compilers(compilers));
13111310

13121311
let mut tarball = Tarball::new(builder, "clippy", &target.triple);
13131312
tarball.set_overlay(OverlayKind::Clippy);
@@ -1319,9 +1318,9 @@ impl Step for Clippy {
13191318
}
13201319
}
13211320

1322-
#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
1321+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
13231322
pub struct Miri {
1324-
pub compiler: Compiler,
1323+
pub build_compiler: Compiler,
13251324
pub target: TargetSelection,
13261325
}
13271326

@@ -1337,7 +1336,7 @@ impl Step for Miri {
13371336

13381337
fn make_run(run: RunConfig<'_>) {
13391338
run.builder.ensure(Miri {
1340-
compiler: run.builder.compiler_for(
1339+
build_compiler: run.builder.compiler_for(
13411340
run.builder.top_stage,
13421341
run.builder.config.host_target,
13431342
run.target,
@@ -1354,15 +1353,12 @@ impl Step for Miri {
13541353
return None;
13551354
}
13561355

1357-
let compiler = self.compiler;
1358-
let target = self.target;
1359-
1360-
builder.ensure(compile::Rustc::new(compiler, target));
1361-
1362-
let miri = builder.ensure(tool::Miri { compiler, target });
1363-
let cargomiri = builder.ensure(tool::CargoMiri { compiler, target });
1356+
let compilers =
1357+
RustcPrivateCompilers::from_build_compiler(builder, self.build_compiler, self.target);
1358+
let miri = builder.ensure(tool::Miri::from_compilers(compilers));
1359+
let cargomiri = builder.ensure(tool::CargoMiri::from_compilers(compilers));
13641360

1365-
let mut tarball = Tarball::new(builder, "miri", &target.triple);
1361+
let mut tarball = Tarball::new(builder, "miri", &self.target.triple);
13661362
tarball.set_overlay(OverlayKind::Miri);
13671363
tarball.is_preview(true);
13681364
tarball.add_file(&miri.tool_path, "bin", FileType::Executable);
@@ -1464,9 +1460,9 @@ impl Step for CodegenBackend {
14641460
}
14651461
}
14661462

1467-
#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
1463+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
14681464
pub struct Rustfmt {
1469-
pub compiler: Compiler,
1465+
pub build_compiler: Compiler,
14701466
pub target: TargetSelection,
14711467
}
14721468

@@ -1482,7 +1478,7 @@ impl Step for Rustfmt {
14821478

14831479
fn make_run(run: RunConfig<'_>) {
14841480
run.builder.ensure(Rustfmt {
1485-
compiler: run.builder.compiler_for(
1481+
build_compiler: run.builder.compiler_for(
14861482
run.builder.top_stage,
14871483
run.builder.config.host_target,
14881484
run.target,
@@ -1492,14 +1488,13 @@ impl Step for Rustfmt {
14921488
}
14931489

14941490
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
1495-
let compiler = self.compiler;
1496-
let target = self.target;
1491+
let compilers =
1492+
RustcPrivateCompilers::from_build_compiler(builder, self.build_compiler, self.target);
14971493

1498-
builder.ensure(compile::Rustc::new(compiler, target));
1494+
let rustfmt = builder.ensure(tool::Rustfmt::from_compilers(compilers));
1495+
let cargofmt = builder.ensure(tool::Cargofmt::from_compilers(compilers));
14991496

1500-
let rustfmt = builder.ensure(tool::Rustfmt { compiler, target });
1501-
let cargofmt = builder.ensure(tool::Cargofmt { compiler, target });
1502-
let mut tarball = Tarball::new(builder, "rustfmt", &target.triple);
1497+
let mut tarball = Tarball::new(builder, "rustfmt", &self.target.triple);
15031498
tarball.set_overlay(OverlayKind::Rustfmt);
15041499
tarball.is_preview(true);
15051500
tarball.add_file(&rustfmt.tool_path, "bin", FileType::Executable);
@@ -1546,7 +1541,7 @@ impl Step for Extended {
15461541
let mut built_tools = HashSet::new();
15471542
macro_rules! add_component {
15481543
($name:expr => $step:expr) => {
1549-
if let Some(tarball) = builder.ensure_if_default($step, Kind::Dist) {
1544+
if let Some(Some(tarball)) = builder.ensure_if_default($step, Kind::Dist) {
15501545
tarballs.push(tarball);
15511546
built_tools.insert($name);
15521547
}
@@ -1567,11 +1562,11 @@ impl Step for Extended {
15671562
add_component!("rust-docs" => Docs { host: target });
15681563
add_component!("rust-json-docs" => JsonDocs { host: target });
15691564
add_component!("cargo" => Cargo { compiler, target });
1570-
add_component!("rustfmt" => Rustfmt { compiler, target });
1571-
add_component!("rust-analyzer" => RustAnalyzer { compiler, target });
1565+
add_component!("rustfmt" => Rustfmt { build_compiler: compiler, target });
1566+
add_component!("rust-analyzer" => RustAnalyzer { build_compiler: compiler, target });
15721567
add_component!("llvm-components" => LlvmTools { target });
1573-
add_component!("clippy" => Clippy { compiler, target });
1574-
add_component!("miri" => Miri { compiler, target });
1568+
add_component!("clippy" => Clippy { build_compiler: compiler, target });
1569+
add_component!("miri" => Miri { build_compiler: compiler, target });
15751570
add_component!("analysis" => Analysis { compiler, target });
15761571
add_component!("rustc-codegen-cranelift" => CodegenBackend {
15771572
compiler: builder.compiler(stage, target),

0 commit comments

Comments
 (0)