@@ -26,7 +26,8 @@ use crate::core::builder::{
2626use crate :: core:: config:: { DebuginfoLevel , LlvmLibunwind , RustcLto , TargetSelection } ;
2727use crate :: utils:: exec:: command;
2828use crate :: utils:: helpers:: {
29- exe, get_clang_cl_resource_dir, is_debug_info, is_dylib, symlink_dir, t, up_to_date,
29+ self , exe, get_clang_cl_resource_dir, get_closest_merge_base_commit, is_debug_info, is_dylib,
30+ symlink_dir, t, up_to_date,
3031} ;
3132use crate :: { CLang , Compiler , DependencyType , GitRepo , Mode , LLVM_TOOLS } ;
3233
@@ -114,21 +115,43 @@ impl Step for Std {
114115 const DEFAULT : bool = true ;
115116
116117 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 ( ) ) )
118+ run. crate_or_deps ( "sysroot" ) . path ( "library" )
123119 }
124120
125121 fn make_run ( run : RunConfig < ' _ > ) {
126122 let crates = std_crates_for_run_make ( & run) ;
123+ let builder = run. builder ;
124+
125+ // Force compilation of the standard library from source if the `library` is modified. This allows
126+ // library team to compile the standard library without needing to compile the compiler with
127+ // the `rust.download-rustc=true` option.
128+ let force_recompile =
129+ if builder. rust_info ( ) . is_managed_git_subrepository ( ) && builder. download_rustc ( ) {
130+ let closest_merge_commit = get_closest_merge_base_commit (
131+ Some ( & builder. src ) ,
132+ & builder. config . git_config ( ) ,
133+ & builder. config . stage0_metadata . config . git_merge_commit_email ,
134+ & [ ] ,
135+ )
136+ . unwrap ( ) ;
137+
138+ // Check if `library` has changes (returns false otherwise)
139+ !t ! ( helpers:: git( Some ( & builder. src) )
140+ . args( [ "diff-index" , "--quiet" , & closest_merge_commit] )
141+ . arg( "--" )
142+ . arg( builder. src. join( "library" ) )
143+ . as_command_mut( )
144+ . status( ) )
145+ . success ( )
146+ } else {
147+ false
148+ } ;
149+
127150 run. builder . ensure ( Std {
128151 compiler : run. builder . compiler ( run. builder . top_stage , run. build_triple ( ) ) ,
129152 target : run. target ,
130153 crates,
131- force_recompile : false ,
154+ force_recompile,
132155 extra_rust_args : & [ ] ,
133156 is_for_mir_opt_tests : false ,
134157 } ) ;
0 commit comments