Skip to content

Commit cf1d3e5

Browse files
committed
refactor ToolBuild to handle compiler tool stages
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 2162e9d commit cf1d3e5

File tree

1 file changed

+26
-25
lines changed
  • src/bootstrap/src/core/build_steps

1 file changed

+26
-25
lines changed

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,33 @@ impl Step for ToolBuild {
7575
///
7676
/// This will build the specified tool with the specified `host` compiler in
7777
/// `stage` into the normal cargo output directory.
78-
fn run(self, builder: &Builder<'_>) -> PathBuf {
79-
let compiler = self.compiler;
80-
let target = self.target;
81-
let mut tool = self.tool;
82-
let path = self.path;
83-
78+
fn run(mut self, builder: &Builder<'_>) -> PathBuf {
8479
match self.mode {
8580
Mode::ToolRustc => {
86-
builder.ensure(compile::Std::new(compiler, compiler.host));
87-
builder.ensure(compile::Rustc::new(compiler, target));
81+
assert!(
82+
self.compiler.stage > 0,
83+
"stage0 isn't supported for `Mode::ToolRustc` programs"
84+
);
85+
// Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
86+
// we'd have stageN/bin/rustc and stageN/bin/$tool_name be effectively different stage
87+
// compilers, which isn't what we want.
88+
//
89+
// Compiler tools should be linked in the same way as the compiler it's paired with,
90+
// so it must be built with the previous stage compiler.
91+
self.compiler.stage -= 1
8892
}
89-
Mode::ToolStd => builder.ensure(compile::Std::new(compiler, target)),
90-
Mode::ToolBootstrap => {} // uses downloaded stage0 compiler libs
93+
Mode::ToolStd => builder.ensure(compile::Std::new(self.compiler, self.target)),
94+
Mode::ToolBootstrap => {}
9195
_ => panic!("unexpected Mode for tool build"),
9296
}
9397

9498
let mut cargo = prepare_tool_cargo(
9599
builder,
96-
compiler,
100+
self.compiler,
97101
self.mode,
98-
target,
102+
self.target,
99103
Kind::Build,
100-
path,
104+
self.path,
101105
self.source_type,
102106
&self.extra_features,
103107
);
@@ -118,7 +122,7 @@ impl Step for ToolBuild {
118122
let build_success = compile::stream_cargo(builder, cargo, vec![], &mut |_| {});
119123

120124
builder.save_toolstate(
121-
tool,
125+
self.tool,
122126
if build_success { ToolState::TestFail } else { ToolState::BuildFail },
123127
);
124128

@@ -128,10 +132,10 @@ impl Step for ToolBuild {
128132
// HACK(#82501): on Windows, the tools directory gets added to PATH when running tests, and
129133
// compiletest confuses HTML tidy with the in-tree tidy. Name the in-tree tidy something
130134
// different so the problem doesn't come up.
131-
if tool == "tidy" {
132-
tool = "rust-tidy";
135+
if self.tool == "tidy" {
136+
self.tool = "rust-tidy";
133137
}
134-
copy_link_tool_bin(builder, self.compiler, self.target, self.mode, tool)
138+
copy_link_tool_bin(builder, self.compiler, self.target, self.mode, self.tool)
135139
}
136140
}
137141
}
@@ -693,9 +697,9 @@ impl Step for Rustdoc {
693697
);
694698
cargo.into_cmd().run(builder);
695699

696-
// Cargo adds a number of paths to the dylib search path on windows, which results in
697-
// the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
698-
// rustdoc a different name.
700+
// Cargo adds a number of paths to the dylib search path on windows, which results in
701+
// the wrong rustdoc being executed. To avoid the conflicting rustdocs, we name the "tool"
702+
// rustdoc a different name.
699703
let tool_rustdoc = builder
700704
.cargo_out(build_compiler, Mode::ToolRustc, target)
701705
.join(exe("rustdoc_tool_binary", target_compiler.host));
@@ -1134,7 +1138,7 @@ fn run_tool_build_step(
11341138
path: &'static str,
11351139
add_bins_to_sysroot: Option<&[&str]>,
11361140
) -> PathBuf {
1137-
let tool = builder.ensure(ToolBuild {
1141+
let bin_source = builder.ensure(ToolBuild {
11381142
compiler,
11391143
target,
11401144
tool: tool_name,
@@ -1153,18 +1157,15 @@ fn run_tool_build_step(
11531157
let bindir = builder.sysroot(compiler).join("bin");
11541158
t!(fs::create_dir_all(&bindir));
11551159

1156-
let tools_out = builder.cargo_out(compiler, Mode::ToolRustc, target);
1157-
11581160
for add_bin in add_bins_to_sysroot {
1159-
let bin_source = tools_out.join(exe(add_bin, target));
11601161
let bin_destination = bindir.join(exe(add_bin, compiler.host));
11611162
builder.copy_link(&bin_source, &bin_destination);
11621163
}
11631164

11641165
// Return a path into the bin dir.
11651166
bindir.join(exe(tool_name, compiler.host))
11661167
} else {
1167-
tool
1168+
bin_source
11681169
}
11691170
}
11701171

0 commit comments

Comments
 (0)