@@ -445,6 +445,72 @@ impl Step for StdLink {
445445 let libdir = builder. sysroot_libdir ( target_compiler, target) ;
446446 let hostdir = builder. sysroot_libdir ( target_compiler, compiler. host ) ;
447447 add_to_sysroot ( builder, & libdir, & hostdir, & libstd_stamp ( builder, compiler, target) ) ;
448+
449+ if compiler. stage == 0 {
450+ // special handling for stage0, to make `rustup toolchain link` and `x dist --stage 0`
451+ // work for stage0-sysroot
452+ let sysroot = builder. out . join ( & compiler. host . triple ) . join ( "stage0-sysroot" ) ;
453+
454+ let host_lib_dir = builder. initial_rustc . ancestors ( ) . nth ( 2 ) . unwrap ( ) . join ( "lib" ) ;
455+ let host_bin_dir = builder. out . join ( & builder. initial_rustc . parent ( ) . unwrap ( ) ) ;
456+ let host_codegen_backends =
457+ host_lib_dir. join ( "rustlib" ) . join ( & compiler. host . triple ) . join ( "codegen-backends" ) ;
458+ let sysroot_bin_dir = sysroot. join ( "bin" ) ;
459+ let sysroot_lib_dir = sysroot. join ( "lib" ) ;
460+ let sysroot_codegen_backends = builder. sysroot_codegen_backends ( compiler) ;
461+
462+ // Create the `bin` directory in stage0-sysroot
463+ t ! ( fs:: create_dir_all( & sysroot_bin_dir) ) ;
464+
465+ // copy bin files from `builder.initial_rustc/./` to `stage0-sysroot/bin`
466+ if let Ok ( files) = fs:: read_dir ( & host_bin_dir) {
467+ for file in files {
468+ let file = t ! ( file) ;
469+ if file. file_name ( ) == "rustfmt" {
470+ // This is when `rustc` and `cargo` are set in `config.toml`
471+ if !file. path ( ) . starts_with ( & builder. out ) {
472+ builder. copy (
473+ & file. path ( ) . into_boxed_path ( ) ,
474+ & sysroot_bin_dir. join ( file. file_name ( ) ) ,
475+ ) ;
476+ } else {
477+ builder. copy (
478+ & builder
479+ . out
480+ . join ( & compiler. host . triple )
481+ . join ( "rustfmt/bin/rustfmt" ) ,
482+ & sysroot_bin_dir. join ( file. file_name ( ) ) ,
483+ ) ;
484+ }
485+ } else {
486+ builder. copy (
487+ & file. path ( ) . into_boxed_path ( ) ,
488+ & sysroot_bin_dir. join ( file. file_name ( ) ) ,
489+ ) ;
490+ }
491+ }
492+ }
493+
494+ // copy dylib files from `builder.initial_rustc/../lib/*` while excluding the `rustlib` directory to `stage0-sysroot/lib`
495+ if let Ok ( files) = fs:: read_dir ( & host_lib_dir) {
496+ for file in files {
497+ let file = t ! ( file) ;
498+ let path = file. path ( ) ;
499+ if path. is_file ( )
500+ && is_dylib ( & file. file_name ( ) . into_string ( ) . unwrap ( ) )
501+ && !path. starts_with ( sysroot_lib_dir. join ( "rustlib" ) . into_boxed_path ( ) )
502+ {
503+ builder. copy ( & path, & sysroot_lib_dir. join ( path. file_name ( ) . unwrap ( ) ) ) ;
504+ }
505+ }
506+ }
507+
508+ t ! ( fs:: create_dir_all( & sysroot_codegen_backends) ) ;
509+ // copy `codegen-backends` from `host_lib_dir/rustlib/codegen_backends` to `stage0-sysroot/lib/rustlib/host-triple/codegen-backends` if it exists.
510+ if host_codegen_backends. exists ( ) {
511+ builder. cp_r ( & host_codegen_backends, & sysroot_codegen_backends) ;
512+ }
513+ }
448514 }
449515}
450516
0 commit comments