@@ -76,11 +76,16 @@ impl Step for ToolBuild {
76
76
/// This will build the specified tool with the specified `host` compiler in
77
77
/// `stage` into the normal cargo output directory.
78
78
fn run ( self , builder : & Builder < ' _ > ) -> PathBuf {
79
- let compiler = self . compiler ;
80
79
let target = self . target ;
81
80
let mut tool = self . tool ;
82
81
let path = self . path ;
83
82
83
+ let compiler = if self . mode == Mode :: ToolRustc {
84
+ get_tool_rustc_compiler ( builder, self . compiler )
85
+ } else {
86
+ self . compiler
87
+ } ;
88
+
84
89
match self . mode {
85
90
Mode :: ToolRustc => {
86
91
builder. ensure ( compile:: Std :: new ( compiler, compiler. host ) ) ;
@@ -147,6 +152,9 @@ pub fn prepare_tool_cargo(
147
152
source_type : SourceType ,
148
153
extra_features : & [ String ] ,
149
154
) -> CargoCommand {
155
+ let compiler =
156
+ if mode == Mode :: ToolRustc { get_tool_rustc_compiler ( builder, compiler) } else { compiler } ;
157
+
150
158
let mut cargo = builder:: Cargo :: new ( builder, compiler, mode, source_type, target, cmd_kind) ;
151
159
152
160
let dir = builder. src . join ( path) ;
@@ -240,6 +248,19 @@ pub fn prepare_tool_cargo(
240
248
cargo
241
249
}
242
250
251
+ fn get_tool_rustc_compiler ( builder : & Builder < ' _ > , target_compiler : Compiler ) -> Compiler {
252
+ if builder. download_rustc ( ) && target_compiler. stage == 1 {
253
+ // We already have the stage 1 compiler, we don't need to cut the stage.
254
+ builder. compiler ( target_compiler. stage , builder. config . build )
255
+ } else {
256
+ // Similar to `compile::Assemble`, build with the previous stage's compiler. Otherwise
257
+ // we'd have stageN/bin/rustc and stageN/bin/$rustc_tool be effectively different stage
258
+ // compilers, which isn't what we want. Rustc tools should be linked in the same way as the
259
+ // compiler it's paired with, so it must be built with the previous stage compiler.
260
+ builder. compiler ( target_compiler. stage . saturating_sub ( 1 ) , builder. config . build )
261
+ }
262
+ }
263
+
243
264
/// Links a built tool binary with the given `name` from the build directory to the
244
265
/// tools directory.
245
266
fn copy_link_tool_bin (
0 commit comments