@@ -151,16 +151,26 @@ impl Step for Std {
151
151
) ]
152
152
fn run ( self , builder : & Builder < ' _ > ) {
153
153
let target = self . target ;
154
- let compiler = self . compiler ;
154
+
155
+ // We already have std ready to be used for stage 0.
156
+ if self . compiler . stage == 0 {
157
+ let compiler = self . compiler ;
158
+ builder. ensure ( StdLink :: from_std ( self , compiler) ) ;
159
+
160
+ return ;
161
+ }
162
+
163
+ let compiler = if builder. download_rustc ( ) && self . force_recompile {
164
+ // When there are changes in the library tree with CI-rustc, we want to build
165
+ // the stageN library and that requires using stageN-1 compiler.
166
+ builder. compiler ( self . compiler . stage . saturating_sub ( 1 ) , builder. config . build )
167
+ } else {
168
+ self . compiler
169
+ } ;
155
170
156
171
// When using `download-rustc`, we already have artifacts for the host available. Don't
157
172
// recompile them.
158
- if builder. download_rustc ( ) && builder. is_builder_target ( target)
159
- // NOTE: the beta compiler may generate different artifacts than the downloaded compiler, so
160
- // its artifacts can't be reused.
161
- && compiler. stage != 0
162
- && !self . force_recompile
163
- {
173
+ if builder. download_rustc ( ) && builder. is_builder_target ( target) && !self . force_recompile {
164
174
let sysroot = builder. ensure ( Sysroot { compiler, force_recompile : false } ) ;
165
175
cp_rustc_component_to_ci_sysroot (
166
176
builder,
@@ -193,7 +203,13 @@ impl Step for Std {
193
203
let compiler_to_use = builder. compiler_for ( compiler. stage , compiler. host , target) ;
194
204
trace ! ( ?compiler_to_use) ;
195
205
196
- if compiler_to_use != compiler {
206
+ if compiler_to_use != compiler
207
+ // Never uplift std unless we have compiled stage 1; if stage 1 is compiled,
208
+ // uplift it from there.
209
+ //
210
+ // FIXME: improve `fn compiler_for` to avoid adding stage condition here.
211
+ && compiler. stage > 1
212
+ {
197
213
trace ! ( ?compiler_to_use, ?compiler, "compiler != compiler_to_use, uplifting library" ) ;
198
214
199
215
builder. ensure ( Std :: new ( compiler_to_use, target) ) ;
@@ -226,27 +242,6 @@ impl Step for Std {
226
242
227
243
target_deps. extend ( self . copy_extra_objects ( builder, & compiler, target) ) ;
228
244
229
- // The LLD wrappers and `rust-lld` are self-contained linking components that can be
230
- // necessary to link the stdlib on some targets. We'll also need to copy these binaries to
231
- // the `stage0-sysroot` to ensure the linker is found when bootstrapping on such a target.
232
- if compiler. stage == 0 && builder. is_builder_target ( compiler. host ) {
233
- trace ! (
234
- "(build == host) copying linking components to `stage0-sysroot` for bootstrapping"
235
- ) ;
236
- // We want to copy the host `bin` folder within the `rustlib` folder in the sysroot.
237
- let src_sysroot_bin = builder
238
- . rustc_snapshot_sysroot ( )
239
- . join ( "lib" )
240
- . join ( "rustlib" )
241
- . join ( compiler. host )
242
- . join ( "bin" ) ;
243
- if src_sysroot_bin. exists ( ) {
244
- let target_sysroot_bin = builder. sysroot_target_bindir ( compiler, target) ;
245
- t ! ( fs:: create_dir_all( & target_sysroot_bin) ) ;
246
- builder. cp_link_r ( & src_sysroot_bin, & target_sysroot_bin) ;
247
- }
248
- }
249
-
250
245
// We build a sysroot for mir-opt tests using the same trick that Miri does: A check build
251
246
// with -Zalways-encode-mir. This frees us from the need to have a target linker, and the
252
247
// fact that this is a check build integrates nicely with run_cargo.
@@ -741,7 +736,7 @@ impl Step for StdLink {
741
736
let target = self . target ;
742
737
743
738
// NOTE: intentionally does *not* check `target == builder.build` to avoid having to add the same check in `test::Crate`.
744
- let ( libdir, hostdir) = if self . force_recompile && builder. download_rustc ( ) {
739
+ let ( libdir, hostdir) = if ! self . force_recompile && builder. download_rustc ( ) {
745
740
// NOTE: copies part of `sysroot_libdir` to avoid having to add a new `force_recompile` argument there too
746
741
let lib = builder. sysroot_libdir_relative ( self . compiler ) ;
747
742
let sysroot = builder. ensure ( crate :: core:: build_steps:: compile:: Sysroot {
@@ -757,23 +752,16 @@ impl Step for StdLink {
757
752
( libdir, hostdir)
758
753
} ;
759
754
760
- add_to_sysroot (
761
- builder,
762
- & libdir,
763
- & hostdir,
764
- & build_stamp:: libstd_stamp ( builder, compiler, target) ,
765
- ) ;
755
+ let is_downloaded_beta_stage0 = builder
756
+ . build
757
+ . config
758
+ . initial_rustc
759
+ . starts_with ( builder. out . join ( compiler. host ) . join ( "stage0/bin" ) ) ;
766
760
767
761
// Special case for stage0, to make `rustup toolchain link` and `x dist --stage 0`
768
762
// work for stage0-sysroot. We only do this if the stage0 compiler comes from beta,
769
763
// and is not set to a custom path.
770
- if compiler. stage == 0
771
- && builder
772
- . build
773
- . config
774
- . initial_rustc
775
- . starts_with ( builder. out . join ( compiler. host ) . join ( "stage0/bin" ) )
776
- {
764
+ if compiler. stage == 0 && is_downloaded_beta_stage0 {
777
765
// Copy bin files from stage0/bin to stage0-sysroot/bin
778
766
let sysroot = builder. out . join ( compiler. host ) . join ( "stage0-sysroot" ) ;
779
767
@@ -783,21 +771,9 @@ impl Step for StdLink {
783
771
t ! ( fs:: create_dir_all( & sysroot_bin_dir) ) ;
784
772
builder. cp_link_r ( & stage0_bin_dir, & sysroot_bin_dir) ;
785
773
786
- // Copy all files from stage0/lib to stage0-sysroot/lib
787
774
let stage0_lib_dir = builder. out . join ( host) . join ( "stage0/lib" ) ;
788
- if let Ok ( files) = fs:: read_dir ( stage0_lib_dir) {
789
- for file in files {
790
- let file = t ! ( file) ;
791
- let path = file. path ( ) ;
792
- if path. is_file ( ) {
793
- builder. copy_link (
794
- & path,
795
- & sysroot. join ( "lib" ) . join ( path. file_name ( ) . unwrap ( ) ) ,
796
- FileType :: Regular ,
797
- ) ;
798
- }
799
- }
800
- }
775
+ t ! ( fs:: create_dir_all( sysroot. join( "lib" ) ) ) ;
776
+ builder. cp_link_r ( & stage0_lib_dir, & sysroot. join ( "lib" ) ) ;
801
777
802
778
// Copy codegen-backends from stage0
803
779
let sysroot_codegen_backends = builder. sysroot_codegen_backends ( compiler) ;
@@ -811,6 +787,30 @@ impl Step for StdLink {
811
787
if stage0_codegen_backends. exists ( ) {
812
788
builder. cp_link_r ( & stage0_codegen_backends, & sysroot_codegen_backends) ;
813
789
}
790
+ } else if compiler. stage == 0 {
791
+ let sysroot = builder. out . join ( compiler. host . triple ) . join ( "stage0-sysroot" ) ;
792
+
793
+ if builder. local_rebuild {
794
+ // On local rebuilds this path might be a symlink to the project root,
795
+ // which can be read-only (e.g., on CI). So remove it before copying
796
+ // the stage0 lib.
797
+ let _ = fs:: remove_dir_all ( sysroot. join ( "lib/rustlib/src/rust" ) ) ;
798
+ }
799
+
800
+ builder. cp_link_r ( & builder. initial_sysroot . join ( "lib" ) , & sysroot. join ( "lib" ) ) ;
801
+ } else {
802
+ if builder. download_rustc ( ) {
803
+ // Ensure there are no CI-rustc std artifacts.
804
+ let _ = fs:: remove_dir_all ( & libdir) ;
805
+ let _ = fs:: remove_dir_all ( & hostdir) ;
806
+ }
807
+
808
+ add_to_sysroot (
809
+ builder,
810
+ & libdir,
811
+ & hostdir,
812
+ & build_stamp:: libstd_stamp ( builder, compiler, target) ,
813
+ ) ;
814
814
}
815
815
}
816
816
}
@@ -1033,7 +1033,7 @@ impl Step for Rustc {
1033
1033
let compiler = self . compiler ;
1034
1034
let target = self . target ;
1035
1035
1036
- // NOTE: the ABI of the beta compiler is different from the ABI of the downloaded compiler,
1036
+ // NOTE: the ABI of the stage0 compiler is different from the ABI of the downloaded compiler,
1037
1037
// so its artifacts can't be reused.
1038
1038
if builder. download_rustc ( ) && compiler. stage != 0 {
1039
1039
trace ! ( stage = compiler. stage, "`download_rustc` requested" ) ;
@@ -1788,9 +1788,9 @@ impl Step for Sysroot {
1788
1788
t ! ( fs:: create_dir_all( & sysroot) ) ;
1789
1789
1790
1790
// In some cases(see https://github.com./rust-lang/rust/issues/109314), when the stage0
1791
- // compiler relies on more recent version of LLVM than the beta compiler, it may not
1791
+ // compiler relies on more recent version of LLVM than the stage0 compiler, it may not
1792
1792
// be able to locate the correct LLVM in the sysroot. This situation typically occurs
1793
- // when we upgrade LLVM version while the beta compiler continues to use an older version.
1793
+ // when we upgrade LLVM version while the stage0 compiler continues to use an older version.
1794
1794
//
1795
1795
// Make sure to add the correct version of LLVM into the stage0 sysroot.
1796
1796
if compiler. stage == 0 {
0 commit comments