Skip to content

Commit bdaf186

Browse files
committed
bootstrap: stage0 version check expanded
+ Check not just rustc, but also cargo. + Check that the program's self-reported name matches the expected name (such as "rustc" or "cargo").
1 parent 997ab61 commit bdaf186

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

src/bootstrap/src/core/config/config.rs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::utils::cache::{Interned, INTERNER};
2222
use crate::utils::channel::{self, GitInfo};
2323
use crate::utils::helpers::{exe, output, t};
2424
use build_helper::exit;
25+
use build_helper::util::fail;
2526
use semver::Version;
2627
use serde::{Deserialize, Deserializer};
2728
use serde_derive::Deserialize;
@@ -1416,17 +1417,23 @@ impl Config {
14161417

14171418
config.initial_rustc = if let Some(rustc) = rustc {
14181419
if !flags.skip_stage0_validation {
1419-
config.check_build_rustc_version(&rustc);
1420+
config.check_stage0_version(&rustc, "rustc");
14201421
}
14211422
PathBuf::from(rustc)
14221423
} else {
14231424
config.download_beta_toolchain();
14241425
config.out.join(config.build.triple).join("stage0/bin/rustc")
14251426
};
14261427

1427-
config.initial_cargo = cargo
1428-
.map(PathBuf::from)
1429-
.unwrap_or_else(|| config.out.join(config.build.triple).join("stage0/bin/cargo"));
1428+
config.initial_cargo = if let Some(cargo) = cargo {
1429+
if !flags.skip_stage0_validation {
1430+
config.check_stage0_version(&cargo, "cargo");
1431+
}
1432+
PathBuf::from(cargo)
1433+
} else {
1434+
config.download_beta_toolchain();
1435+
config.out.join(config.build.triple).join("stage0/bin/cargo")
1436+
};
14301437

14311438
// NOTE: it's important this comes *after* we set `initial_rustc` just above.
14321439
if config.dry_run() {
@@ -2282,39 +2289,37 @@ impl Config {
22822289
}
22832290
}
22842291

2285-
pub fn check_build_rustc_version(&self, rustc_path: &str) {
2292+
// check rustc/cargo version is same or lower with 1 apart from the building one
2293+
pub fn check_stage0_version(&self, program_path: &str, component_name: &'static str) {
22862294
if self.dry_run() {
22872295
return;
22882296
}
22892297

2290-
// check rustc version is same or lower with 1 apart from the building one
2291-
let mut cmd = Command::new(rustc_path);
2292-
cmd.arg("--version");
2293-
let rustc_output = output(&mut cmd)
2294-
.lines()
2295-
.next()
2296-
.unwrap()
2297-
.split(' ')
2298-
.nth(1)
2299-
.unwrap()
2300-
.split('-')
2301-
.next()
2302-
.unwrap()
2303-
.to_owned();
2304-
let rustc_version = Version::parse(&rustc_output.trim()).unwrap();
2298+
let stage0_output = output(Command::new(program_path).arg("--version"));
2299+
let mut stage0_output = stage0_output.lines().next().unwrap().split(' ');
2300+
2301+
let stage0_name = stage0_output.next().unwrap();
2302+
if stage0_name != component_name {
2303+
fail(&format!(
2304+
"Expected to find {component_name} at {program_path} but it claims to be {stage0_name}"
2305+
));
2306+
}
2307+
2308+
let stage0_version =
2309+
Version::parse(stage0_output.next().unwrap().split('-').next().unwrap().trim())
2310+
.unwrap();
23052311
let source_version =
23062312
Version::parse(&fs::read_to_string(self.src.join("src/version")).unwrap().trim())
23072313
.unwrap();
2308-
if !(source_version == rustc_version
2309-
|| (source_version.major == rustc_version.major
2310-
&& (source_version.minor == rustc_version.minor
2311-
|| source_version.minor == rustc_version.minor + 1)))
2314+
if !(source_version == stage0_version
2315+
|| (source_version.major == stage0_version.major
2316+
&& (source_version.minor == stage0_version.minor
2317+
|| source_version.minor == stage0_version.minor + 1)))
23122318
{
23132319
let prev_version = format!("{}.{}.x", source_version.major, source_version.minor - 1);
2314-
eprintln!(
2315-
"Unexpected rustc version: {rustc_version}, we should use {prev_version}/{source_version} to build source with {source_version}"
2316-
);
2317-
exit!(1);
2320+
fail(&format!(
2321+
"Unexpected {component_name} version: {stage0_version}, we should use {prev_version}/{source_version} to build source with {source_version}"
2322+
));
23182323
}
23192324
}
23202325

0 commit comments

Comments
 (0)