Skip to content

Commit 33b3b13

Browse files
committed
Auto merge of #58897 - Mark-Simulacrum:tool-rework, r=alexcrichton
Rework how bootstrap tools are built This makes bootstrap tools buildable and testable in stage 0 with the downloaded bootstrap compiler, futhermore, it makes it such that they cannot be built in any other stage. Notably, this will also mean that compiletest may need to wait a cycle before it can use changes to `libtest`, as it no longer depends on the in-tree libtest.
2 parents 82e2f3e + 03718ed commit 33b3b13

File tree

5 files changed

+99
-62
lines changed

5 files changed

+99
-62
lines changed

src/bootstrap/builder.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -670,28 +670,27 @@ impl<'a> Builder<'a> {
670670
.map(|entry| entry.path())
671671
}
672672

673-
pub fn rustdoc(&self, host: Interned<String>) -> PathBuf {
674-
self.ensure(tool::Rustdoc { host })
673+
pub fn rustdoc(&self, compiler: Compiler) -> PathBuf {
674+
self.ensure(tool::Rustdoc { compiler })
675675
}
676676

677-
pub fn rustdoc_cmd(&self, host: Interned<String>) -> Command {
677+
pub fn rustdoc_cmd(&self, compiler: Compiler) -> Command {
678678
let mut cmd = Command::new(&self.out.join("bootstrap/debug/rustdoc"));
679-
let compiler = self.compiler(self.top_stage, host);
680679
cmd.env("RUSTC_STAGE", compiler.stage.to_string())
681680
.env("RUSTC_SYSROOT", self.sysroot(compiler))
682681
// Note that this is *not* the sysroot_libdir because rustdoc must be linked
683682
// equivalently to rustc.
684683
.env("RUSTDOC_LIBDIR", self.rustc_libdir(compiler))
685684
.env("CFG_RELEASE_CHANNEL", &self.config.channel)
686-
.env("RUSTDOC_REAL", self.rustdoc(host))
685+
.env("RUSTDOC_REAL", self.rustdoc(compiler))
687686
.env("RUSTDOC_CRATE_VERSION", self.rust_version())
688687
.env("RUSTC_BOOTSTRAP", "1");
689688

690689
// Remove make-related flags that can cause jobserver problems.
691690
cmd.env_remove("MAKEFLAGS");
692691
cmd.env_remove("MFLAGS");
693692

694-
if let Some(linker) = self.linker(host) {
693+
if let Some(linker) = self.linker(compiler.host) {
695694
cmd.env("RUSTC_TARGET_LINKER", linker);
696695
}
697696
cmd
@@ -753,7 +752,7 @@ impl<'a> Builder<'a> {
753752
// This is the intended out directory for compiler documentation.
754753
my_out = self.compiler_doc_out(target);
755754
}
756-
let rustdoc = self.rustdoc(compiler.host);
755+
let rustdoc = self.rustdoc(compiler);
757756
self.clear_if_dirty(&my_out, &rustdoc);
758757
} else if cmd != "test" {
759758
match mode {
@@ -910,7 +909,7 @@ impl<'a> Builder<'a> {
910909
.env(
911910
"RUSTDOC_REAL",
912911
if cmd == "doc" || cmd == "rustdoc" || (cmd == "test" && want_rustdoc) {
913-
self.rustdoc(compiler.host)
912+
self.rustdoc(compiler)
914913
} else {
915914
PathBuf::from("/path/to/nowhere/rustdoc/not/required")
916915
},

src/bootstrap/dist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ impl Step for Rustc {
479479
t!(fs::create_dir_all(image.join("bin")));
480480
builder.cp_r(&src.join("bin"), &image.join("bin"));
481481

482-
builder.install(&builder.rustdoc(compiler.host), &image.join("bin"), 0o755);
482+
builder.install(&builder.rustdoc(compiler), &image.join("bin"), 0o755);
483483

484484
// Copy runtime DLLs needed by the compiler
485485
if libdir != "bin" {

src/bootstrap/doc.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ fn invoke_rustdoc(
335335
let footer = builder.src.join("src/doc/footer.inc");
336336
let version_info = out.join("version_info.html");
337337

338-
let mut cmd = builder.rustdoc_cmd(compiler.host);
338+
let mut cmd = builder.rustdoc_cmd(compiler);
339339

340340
let out = out.join("book");
341341

@@ -415,7 +415,7 @@ impl Step for Standalone {
415415
}
416416

417417
let html = out.join(filename).with_extension("html");
418-
let rustdoc = builder.rustdoc(compiler.host);
418+
let rustdoc = builder.rustdoc(compiler);
419419
if up_to_date(&path, &html) &&
420420
up_to_date(&footer, &html) &&
421421
up_to_date(&favicon, &html) &&
@@ -425,7 +425,7 @@ impl Step for Standalone {
425425
continue
426426
}
427427

428-
let mut cmd = builder.rustdoc_cmd(compiler.host);
428+
let mut cmd = builder.rustdoc_cmd(compiler);
429429
cmd.arg("--html-after-content").arg(&footer)
430430
.arg("--html-before-content").arg(&version_info)
431431
.arg("--html-in-header").arg(&favicon)
@@ -824,7 +824,7 @@ impl Step for Rustdoc {
824824
builder.ensure(Rustc { stage, target });
825825

826826
// Build rustdoc.
827-
builder.ensure(tool::Rustdoc { host: compiler.host });
827+
builder.ensure(tool::Rustdoc { compiler: compiler });
828828

829829
// Symlink compiler docs to the output directory of rustdoc documentation.
830830
let out_dir = builder.stage_out(compiler, Mode::ToolRustc)
@@ -883,7 +883,11 @@ impl Step for ErrorIndex {
883883
builder.info(&format!("Documenting error index ({})", target));
884884
let out = builder.doc_out(target);
885885
t!(fs::create_dir_all(&out));
886-
let mut index = builder.tool_cmd(Tool::ErrorIndex);
886+
let compiler = builder.compiler(2, builder.config.build);
887+
let mut index = tool::ErrorIndex::command(
888+
builder,
889+
compiler,
890+
);
887891
index.arg("html");
888892
index.arg(out.join("error-index.html"));
889893

src/bootstrap/test.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Step for Cargotest {
177177
cmd.arg(&builder.initial_cargo)
178178
.arg(&out_dir)
179179
.env("RUSTC", builder.rustc(compiler))
180-
.env("RUSTDOC", builder.rustdoc(compiler.host)),
180+
.env("RUSTDOC", builder.rustdoc(compiler)),
181181
);
182182
}
183183
}
@@ -414,7 +414,6 @@ impl Step for Miri {
414414

415415
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
416416
pub struct CompiletestTest {
417-
stage: u32,
418417
host: Interned<String>,
419418
}
420419

@@ -427,16 +426,14 @@ impl Step for CompiletestTest {
427426

428427
fn make_run(run: RunConfig<'_>) {
429428
run.builder.ensure(CompiletestTest {
430-
stage: run.builder.top_stage,
431429
host: run.target,
432430
});
433431
}
434432

435433
/// Runs `cargo test` for compiletest.
436434
fn run(self, builder: &Builder<'_>) {
437-
let stage = self.stage;
438435
let host = self.host;
439-
let compiler = builder.compiler(stage, host);
436+
let compiler = builder.compiler(0, host);
440437

441438
let mut cargo = tool::prepare_tool_cargo(builder,
442439
compiler,
@@ -563,7 +560,7 @@ impl Step for RustdocTheme {
563560
builder.sysroot_libdir(self.compiler, self.compiler.host),
564561
)
565562
.env("CFG_RELEASE_CHANNEL", &builder.config.channel)
566-
.env("RUSTDOC_REAL", builder.rustdoc(self.compiler.host))
563+
.env("RUSTDOC_REAL", builder.rustdoc(self.compiler))
567564
.env("RUSTDOC_CRATE_VERSION", builder.rust_version())
568565
.env("RUSTC_BOOTSTRAP", "1");
569566
if let Some(linker) = builder.linker(self.compiler.host) {
@@ -1042,7 +1039,7 @@ impl Step for Compiletest {
10421039
|| mode == "js-doc-test"
10431040
{
10441041
cmd.arg("--rustdoc-path")
1045-
.arg(builder.rustdoc(compiler.host));
1042+
.arg(builder.rustdoc(compiler));
10461043
}
10471044

10481045
cmd.arg("--src-base")
@@ -1464,7 +1461,10 @@ impl Step for ErrorIndex {
14641461
t!(fs::create_dir_all(&dir));
14651462
let output = dir.join("error-index.md");
14661463

1467-
let mut tool = builder.tool_cmd(Tool::ErrorIndex);
1464+
let mut tool = tool::ErrorIndex::command(
1465+
builder,
1466+
builder.compiler(compiler.stage, builder.config.build),
1467+
);
14681468
tool.arg("markdown")
14691469
.arg(&output)
14701470
.env("CFG_BUILD", &builder.config.build)
@@ -1489,7 +1489,7 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) ->
14891489
}
14901490

14911491
builder.info(&format!("doc tests for: {}", markdown.display()));
1492-
let mut cmd = builder.rustdoc_cmd(compiler.host);
1492+
let mut cmd = builder.rustdoc_cmd(compiler);
14931493
builder.add_rust_test_threads(&mut cmd);
14941494
cmd.arg("--test");
14951495
cmd.arg(markdown);

src/bootstrap/tool.rs

+73-39
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ pub fn prepare_tool_cargo(
251251
cargo
252252
}
253253

254-
macro_rules! tool {
254+
macro_rules! bootstrap_tool {
255255
($(
256-
$name:ident, $path:expr, $tool_name:expr, $mode:expr
256+
$name:ident, $path:expr, $tool_name:expr
257257
$(,llvm_tools = $llvm:expr)*
258258
$(,is_external_tool = $external:expr)*
259259
;
@@ -267,10 +267,7 @@ macro_rules! tool {
267267

268268
impl Tool {
269269
pub fn get_mode(&self) -> Mode {
270-
let mode = match self {
271-
$(Tool::$name => $mode,)+
272-
};
273-
mode
270+
Mode::ToolBootstrap
274271
}
275272

276273
/// Whether this tool requires LLVM to run
@@ -283,27 +280,15 @@ macro_rules! tool {
283280

284281
impl<'a> Builder<'a> {
285282
pub fn tool_exe(&self, tool: Tool) -> PathBuf {
286-
let stage = self.tool_default_stage(tool);
287283
match tool {
288284
$(Tool::$name =>
289285
self.ensure($name {
290-
compiler: self.compiler(stage, self.config.build),
286+
compiler: self.compiler(0, self.config.build),
291287
target: self.config.build,
292288
}),
293289
)+
294290
}
295291
}
296-
297-
pub fn tool_default_stage(&self, tool: Tool) -> u32 {
298-
// Compile the error-index in the same stage as rustdoc to avoid
299-
// recompiling rustdoc twice if we can. Otherwise compile
300-
// everything else in stage0 as there's no need to rebootstrap
301-
// everything.
302-
match tool {
303-
Tool::ErrorIndex if self.top_stage >= 2 => self.top_stage,
304-
_ => 0,
305-
}
306-
}
307292
}
308293

309294
$(
@@ -322,7 +307,8 @@ macro_rules! tool {
322307

323308
fn make_run(run: RunConfig<'_>) {
324309
run.builder.ensure($name {
325-
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
310+
// snapshot compiler
311+
compiler: run.builder.compiler(0, run.builder.config.build),
326312
target: run.target,
327313
});
328314
}
@@ -332,7 +318,7 @@ macro_rules! tool {
332318
compiler: self.compiler,
333319
target: self.target,
334320
tool: $tool_name,
335-
mode: $mode,
321+
mode: Mode::ToolBootstrap,
336322
path: $path,
337323
is_optional_tool: false,
338324
source_type: if false $(|| $external)* {
@@ -348,21 +334,67 @@ macro_rules! tool {
348334
}
349335
}
350336

351-
tool!(
352-
Rustbook, "src/tools/rustbook", "rustbook", Mode::ToolBootstrap;
353-
ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::ToolRustc;
354-
UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::ToolBootstrap;
355-
Tidy, "src/tools/tidy", "tidy", Mode::ToolBootstrap;
356-
Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::ToolBootstrap;
357-
CargoTest, "src/tools/cargotest", "cargotest", Mode::ToolBootstrap;
358-
Compiletest, "src/tools/compiletest", "compiletest", Mode::ToolBootstrap, llvm_tools = true;
359-
BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::ToolBootstrap;
360-
RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::ToolBootstrap;
361-
RustInstaller, "src/tools/rust-installer", "fabricate", Mode::ToolBootstrap,
362-
is_external_tool = true;
363-
RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes", Mode::ToolBootstrap;
337+
bootstrap_tool!(
338+
Rustbook, "src/tools/rustbook", "rustbook";
339+
UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen";
340+
Tidy, "src/tools/tidy", "tidy";
341+
Linkchecker, "src/tools/linkchecker", "linkchecker";
342+
CargoTest, "src/tools/cargotest", "cargotest";
343+
Compiletest, "src/tools/compiletest", "compiletest", llvm_tools = true;
344+
BuildManifest, "src/tools/build-manifest", "build-manifest";
345+
RemoteTestClient, "src/tools/remote-test-client", "remote-test-client";
346+
RustInstaller, "src/tools/rust-installer", "fabricate", is_external_tool = true;
347+
RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
364348
);
365349

350+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
351+
pub struct ErrorIndex {
352+
pub compiler: Compiler,
353+
}
354+
355+
impl ErrorIndex {
356+
pub fn command(builder: &Builder<'_>, compiler: Compiler) -> Command {
357+
let mut cmd = Command::new(builder.ensure(ErrorIndex {
358+
compiler
359+
}));
360+
add_lib_path(
361+
vec![PathBuf::from(&builder.sysroot_libdir(compiler, compiler.host))],
362+
&mut cmd,
363+
);
364+
cmd
365+
}
366+
}
367+
368+
impl Step for ErrorIndex {
369+
type Output = PathBuf;
370+
371+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
372+
run.path("src/tools/error_index_generator")
373+
}
374+
375+
fn make_run(run: RunConfig<'_>) {
376+
// Compile the error-index in the same stage as rustdoc to avoid
377+
// recompiling rustdoc twice if we can.
378+
let stage = if run.builder.top_stage >= 2 { run.builder.top_stage } else { 0 };
379+
run.builder.ensure(ErrorIndex {
380+
compiler: run.builder.compiler(stage, run.builder.config.build),
381+
});
382+
}
383+
384+
fn run(self, builder: &Builder<'_>) -> PathBuf {
385+
builder.ensure(ToolBuild {
386+
compiler: self.compiler,
387+
target: self.compiler.host,
388+
tool: "error_index_generator",
389+
mode: Mode::ToolRustc,
390+
path: "src/tools/error_index_generator",
391+
is_optional_tool: false,
392+
source_type: SourceType::InTree,
393+
extra_features: Vec::new(),
394+
}).expect("expected to build -- essential tool")
395+
}
396+
}
397+
366398
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
367399
pub struct RemoteTestServer {
368400
pub compiler: Compiler,
@@ -399,7 +431,9 @@ impl Step for RemoteTestServer {
399431

400432
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
401433
pub struct Rustdoc {
402-
pub host: Interned<String>,
434+
/// This should only ever be 0 or 2.
435+
/// We sometimes want to reference the "bootstrap" rustdoc, which is why this option is here.
436+
pub compiler: Compiler,
403437
}
404438

405439
impl Step for Rustdoc {
@@ -413,12 +447,12 @@ impl Step for Rustdoc {
413447

414448
fn make_run(run: RunConfig<'_>) {
415449
run.builder.ensure(Rustdoc {
416-
host: run.host,
450+
compiler: run.builder.compiler(run.builder.top_stage, run.host),
417451
});
418452
}
419453

420454
fn run(self, builder: &Builder<'_>) -> PathBuf {
421-
let target_compiler = builder.compiler(builder.top_stage, self.host);
455+
let target_compiler = self.compiler;
422456
if target_compiler.stage == 0 {
423457
if !target_compiler.is_snapshot(builder) {
424458
panic!("rustdoc in stage 0 must be snapshot rustdoc");
@@ -626,7 +660,7 @@ impl<'a> Builder<'a> {
626660
/// `host`.
627661
pub fn tool_cmd(&self, tool: Tool) -> Command {
628662
let mut cmd = Command::new(self.tool_exe(tool));
629-
let compiler = self.compiler(self.tool_default_stage(tool), self.config.build);
663+
let compiler = self.compiler(0, self.config.build);
630664
self.prepare_tool_cmd(compiler, tool, &mut cmd);
631665
cmd
632666
}
@@ -638,7 +672,7 @@ impl<'a> Builder<'a> {
638672
fn prepare_tool_cmd(&self, compiler: Compiler, tool: Tool, cmd: &mut Command) {
639673
let host = &compiler.host;
640674
let mut lib_paths: Vec<PathBuf> = vec![
641-
if compiler.stage == 0 && tool != Tool::ErrorIndex {
675+
if compiler.stage == 0 {
642676
self.build.rustc_snapshot_libdir()
643677
} else {
644678
PathBuf::from(&self.sysroot_libdir(compiler, compiler.host))

0 commit comments

Comments
 (0)