@@ -25,8 +25,10 @@ use crate::core::builder::{
2525} ;
2626use crate :: core:: config:: { DebuginfoLevel , LlvmLibunwind , RustcLto , TargetSelection } ;
2727use crate :: utils:: exec:: command;
28+ use crate :: utils:: helpers;
2829use crate :: utils:: helpers:: {
29- exe, get_clang_cl_resource_dir, is_debug_info, is_dylib, symlink_dir, t, up_to_date,
30+ exe, get_clang_cl_resource_dir, get_closest_merge_base_commit, is_debug_info, is_dylib,
31+ symlink_dir, t, up_to_date,
3032} ;
3133use crate :: { CLang , Compiler , DependencyType , GitRepo , Mode , LLVM_TOOLS } ;
3234
@@ -114,21 +116,43 @@ impl Step for Std {
114116 const DEFAULT : bool = true ;
115117
116118 fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
117- // When downloading stage1, the standard library has already been copied to the sysroot, so
118- // there's no need to rebuild it.
119- let builder = run. builder ;
120- run. crate_or_deps ( "sysroot" )
121- . path ( "library" )
122- . lazy_default_condition ( Box :: new ( || !builder. download_rustc ( ) ) )
119+ run. crate_or_deps ( "sysroot" ) . path ( "library" )
123120 }
124121
125122 fn make_run ( run : RunConfig < ' _ > ) {
126123 let crates = std_crates_for_run_make ( & run) ;
124+ let builder = run. builder ;
125+
126+ // Force compilation of the standard library from source if the `library` is modified. This allows
127+ // library team to compile the standard library without needing to compile the compiler with
128+ // the `rust.download-rustc=true` option.
129+ let force_recompile =
130+ if builder. rust_info ( ) . is_managed_git_subrepository ( ) && builder. download_rustc ( ) {
131+ let closest_merge_commit = get_closest_merge_base_commit (
132+ Some ( & builder. src ) ,
133+ & builder. config . git_config ( ) ,
134+ & builder. config . stage0_metadata . config . git_merge_commit_email ,
135+ & [ ] ,
136+ )
137+ . unwrap ( ) ;
138+
139+ // Check if `library` has changes (returns false otherwise)
140+ !t ! ( helpers:: git( Some ( & builder. src) )
141+ . args( [ "diff-index" , "--quiet" , & closest_merge_commit] )
142+ . arg( "--" )
143+ . arg( builder. src. join( "library" ) )
144+ . as_command_mut( )
145+ . status( ) )
146+ . success ( )
147+ } else {
148+ false
149+ } ;
150+
127151 run. builder . ensure ( Std {
128152 compiler : run. builder . compiler ( run. builder . top_stage , run. build_triple ( ) ) ,
129153 target : run. target ,
130154 crates,
131- force_recompile : false ,
155+ force_recompile,
132156 extra_rust_args : & [ ] ,
133157 is_for_mir_opt_tests : false ,
134158 } ) ;
0 commit comments