@@ -25,6 +25,7 @@ use crate::flags::{Color, Flags, Warnings};
2525use crate :: util:: { exe, output, t} ;
2626use build_helper:: detail_exit_macro;
2727use once_cell:: sync:: OnceCell ;
28+ use semver:: Version ;
2829use serde:: { Deserialize , Deserializer } ;
2930use serde_derive:: Deserialize ;
3031
@@ -1114,10 +1115,14 @@ impl Config {
11141115 config. out = crate :: util:: absolute ( & config. out ) ;
11151116 }
11161117
1117- config. initial_rustc = build. rustc . map ( PathBuf :: from) . unwrap_or_else ( || {
1118+ config. initial_rustc = if let Some ( rustc) = build. rustc {
1119+ config. check_build_rustc_version ( & rustc) ;
1120+ PathBuf :: from ( rustc)
1121+ } else {
11181122 config. download_beta_toolchain ( ) ;
11191123 config. out . join ( config. build . triple ) . join ( "stage0/bin/rustc" )
1120- } ) ;
1124+ } ;
1125+
11211126 config. initial_cargo = build
11221127 . cargo
11231128 . map ( |cargo| {
@@ -1779,6 +1784,42 @@ impl Config {
17791784 self . rust_codegen_backends . get ( 0 ) . cloned ( )
17801785 }
17811786
1787+ pub fn check_build_rustc_version ( & self , rustc_path : & str ) {
1788+ if self . dry_run ( ) {
1789+ return ;
1790+ }
1791+
1792+ // check rustc version is same or lower with 1 apart from the building one
1793+ let mut cmd = Command :: new ( rustc_path) ;
1794+ cmd. arg ( "--version" ) ;
1795+ let rustc_output = output ( & mut cmd)
1796+ . lines ( )
1797+ . next ( )
1798+ . unwrap ( )
1799+ . split ( ' ' )
1800+ . nth ( 1 )
1801+ . unwrap ( )
1802+ . split ( '-' )
1803+ . next ( )
1804+ . unwrap ( )
1805+ . to_owned ( ) ;
1806+ let rustc_version = Version :: parse ( & rustc_output. trim ( ) ) . unwrap ( ) ;
1807+ let source_version =
1808+ Version :: parse ( & fs:: read_to_string ( self . src . join ( "src/version" ) ) . unwrap ( ) . trim ( ) )
1809+ . unwrap ( ) ;
1810+ if !( source_version == rustc_version
1811+ || ( source_version. major == rustc_version. major
1812+ && source_version. minor == rustc_version. minor + 1 ) )
1813+ {
1814+ let prev_version = format ! ( "{}.{}.x" , source_version. major, source_version. minor - 1 ) ;
1815+ eprintln ! (
1816+ "Unexpected rustc version: {}, we should use {}/{} to build source with {}" ,
1817+ rustc_version, prev_version, source_version, source_version
1818+ ) ;
1819+ detail_exit_macro ! ( 1 ) ;
1820+ }
1821+ }
1822+
17821823 /// Returns the commit to download, or `None` if we shouldn't download CI artifacts.
17831824 fn download_ci_rustc_commit ( & self , download_rustc : Option < StringOrBool > ) -> Option < String > {
17841825 // If `download-rustc` is not set, default to rebuilding.
0 commit comments