@@ -25,6 +25,7 @@ use crate::flags::{Color, Flags, Warnings};
25
25
use crate :: util:: { exe, output, t} ;
26
26
use build_helper:: detail_exit_macro;
27
27
use once_cell:: sync:: OnceCell ;
28
+ use semver:: Version ;
28
29
use serde:: { Deserialize , Deserializer } ;
29
30
use serde_derive:: Deserialize ;
30
31
@@ -1114,10 +1115,14 @@ impl Config {
1114
1115
config. out = crate :: util:: absolute ( & config. out ) ;
1115
1116
}
1116
1117
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 {
1118
1122
config. download_beta_toolchain ( ) ;
1119
1123
config. out . join ( config. build . triple ) . join ( "stage0/bin/rustc" )
1120
- } ) ;
1124
+ } ;
1125
+
1121
1126
config. initial_cargo = build
1122
1127
. cargo
1123
1128
. map ( |cargo| {
@@ -1779,6 +1784,42 @@ impl Config {
1779
1784
self . rust_codegen_backends . get ( 0 ) . cloned ( )
1780
1785
}
1781
1786
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
+
1782
1823
/// Returns the commit to download, or `None` if we shouldn't download CI artifacts.
1783
1824
fn download_ci_rustc_commit ( & self , download_rustc : Option < StringOrBool > ) -> Option < String > {
1784
1825
// If `download-rustc` is not set, default to rebuilding.
0 commit comments