@@ -22,6 +22,7 @@ use crate::utils::cache::{Interned, INTERNER};
2222use crate :: utils:: channel:: { self , GitInfo } ;
2323use crate :: utils:: helpers:: { exe, output, t} ;
2424use build_helper:: exit;
25+ use build_helper:: util:: fail;
2526use semver:: Version ;
2627use serde:: { Deserialize , Deserializer } ;
2728use serde_derive:: Deserialize ;
@@ -1418,19 +1419,23 @@ impl Config {
14181419
14191420 config. initial_rustc = if let Some ( rustc) = rustc {
14201421 if !flags. skip_stage0_validation {
1421- config. check_build_rustc_version ( & rustc) ;
1422+ config. check_stage0_version ( & rustc, "rustc" ) ;
14221423 }
14231424 PathBuf :: from ( rustc)
14241425 } else {
14251426 config. download_beta_toolchain ( ) ;
14261427 config. out . join ( config. build . triple ) . join ( "stage0/bin/rustc" )
14271428 } ;
14281429
1429- config. initial_cargo = cargo
1430- . map ( |cargo| {
1431- t ! ( PathBuf :: from( cargo) . canonicalize( ) , "`initial_cargo` not found on disk" )
1432- } )
1433- . unwrap_or_else ( || config. out . join ( config. build . triple ) . join ( "stage0/bin/cargo" ) ) ;
1430+ config. initial_cargo = if let Some ( cargo) = cargo {
1431+ if !flags. skip_stage0_validation {
1432+ config. check_stage0_version ( & cargo, "cargo" ) ;
1433+ }
1434+ PathBuf :: from ( cargo)
1435+ } else {
1436+ config. download_beta_toolchain ( ) ;
1437+ config. out . join ( config. build . triple ) . join ( "stage0/bin/cargo" )
1438+ } ;
14341439
14351440 // NOTE: it's important this comes *after* we set `initial_rustc` just above.
14361441 if config. dry_run ( ) {
@@ -2286,39 +2291,37 @@ impl Config {
22862291 }
22872292 }
22882293
2289- pub fn check_build_rustc_version ( & self , rustc_path : & str ) {
2294+ // check rustc/cargo version is same or lower with 1 apart from the building one
2295+ pub fn check_stage0_version ( & self , program_path : & str , component_name : & ' static str ) {
22902296 if self . dry_run ( ) {
22912297 return ;
22922298 }
22932299
2294- // check rustc version is same or lower with 1 apart from the building one
2295- let mut cmd = Command :: new ( rustc_path) ;
2296- cmd. arg ( "--version" ) ;
2297- let rustc_output = output ( & mut cmd)
2298- . lines ( )
2299- . next ( )
2300- . unwrap ( )
2301- . split ( ' ' )
2302- . nth ( 1 )
2303- . unwrap ( )
2304- . split ( '-' )
2305- . next ( )
2306- . unwrap ( )
2307- . to_owned ( ) ;
2308- let rustc_version = Version :: parse ( rustc_output. trim ( ) ) . unwrap ( ) ;
2300+ let stage0_output = output ( Command :: new ( program_path) . arg ( "--version" ) ) ;
2301+ let mut stage0_output = stage0_output. lines ( ) . next ( ) . unwrap ( ) . split ( ' ' ) ;
2302+
2303+ let stage0_name = stage0_output. next ( ) . unwrap ( ) ;
2304+ if stage0_name != component_name {
2305+ fail ( & format ! (
2306+ "Expected to find {component_name} at {program_path} but it claims to be {stage0_name}"
2307+ ) ) ;
2308+ }
2309+
2310+ let stage0_version =
2311+ Version :: parse ( stage0_output. next ( ) . unwrap ( ) . split ( '-' ) . next ( ) . unwrap ( ) . trim ( ) )
2312+ . unwrap ( ) ;
23092313 let source_version =
23102314 Version :: parse ( fs:: read_to_string ( self . src . join ( "src/version" ) ) . unwrap ( ) . trim ( ) )
23112315 . unwrap ( ) ;
2312- if !( source_version == rustc_version
2313- || ( source_version. major == rustc_version . major
2314- && ( source_version. minor == rustc_version . minor
2315- || source_version. minor == rustc_version . minor + 1 ) ) )
2316+ if !( source_version == stage0_version
2317+ || ( source_version. major == stage0_version . major
2318+ && ( source_version. minor == stage0_version . minor
2319+ || source_version. minor == stage0_version . minor + 1 ) ) )
23162320 {
23172321 let prev_version = format ! ( "{}.{}.x" , source_version. major, source_version. minor - 1 ) ;
2318- eprintln ! (
2319- "Unexpected rustc version: {rustc_version}, we should use {prev_version}/{source_version} to build source with {source_version}"
2320- ) ;
2321- exit ! ( 1 ) ;
2322+ fail ( & format ! (
2323+ "Unexpected {component_name} version: {stage0_version}, we should use {prev_version}/{source_version} to build source with {source_version}"
2324+ ) ) ;
23222325 }
23232326 }
23242327
0 commit comments