11use std:: fs;
2- use std:: path:: Path ;
2+ use std:: path:: { Path , PathBuf } ;
33use std:: process:: { self , Command } ;
44
55use super :: path:: { Dirs , RelPath } ;
@@ -56,35 +56,40 @@ pub(crate) fn build_sysroot(
5656 spawn_and_wait ( build_cargo_wrapper_cmd) ;
5757 }
5858
59- let default_sysroot = super :: rustc_info:: get_default_sysroot ( & bootstrap_host_compiler. rustc ) ;
60-
61- let host_rustlib_lib =
62- RUSTLIB_DIR . to_path ( dirs) . join ( & bootstrap_host_compiler. triple ) . join ( "lib" ) ;
63- let target_rustlib_lib = RUSTLIB_DIR . to_path ( dirs) . join ( & target_triple) . join ( "lib" ) ;
64- fs:: create_dir_all ( & host_rustlib_lib) . unwrap ( ) ;
65- fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
66-
6759 if target_triple. ends_with ( "windows-gnu" ) {
6860 eprintln ! ( "[BUILD] rtstartup for {target_triple}" ) ;
6961
7062 let rtstartup_src = SYSROOT_SRC . to_path ( dirs) . join ( "library" ) . join ( "rtstartup" ) ;
63+ let mut target_libs = SysrootTarget { triple : target_triple. clone ( ) , libs : vec ! [ ] } ;
7164
7265 for file in [ "rsbegin" , "rsend" ] {
66+ let obj = RelPath :: BUILD . to_path ( dirs) . join ( format ! ( "{file}.o" ) ) ;
7367 let mut build_rtstartup_cmd = Command :: new ( & bootstrap_host_compiler. rustc ) ;
7468 build_rtstartup_cmd
7569 . arg ( "--target" )
7670 . arg ( & target_triple)
7771 . arg ( "--emit=obj" )
7872 . arg ( "-o" )
79- . arg ( target_rustlib_lib . join ( format ! ( "{file}.o" ) ) )
73+ . arg ( & obj )
8074 . arg ( rtstartup_src. join ( format ! ( "{file}.rs" ) ) ) ;
8175 spawn_and_wait ( build_rtstartup_cmd) ;
76+ target_libs. libs . push ( obj) ;
8277 }
83- }
8478
79+ target_libs. install_into_sysroot ( & DIST_DIR . to_path ( dirs) )
80+ }
8581 match sysroot_kind {
8682 SysrootKind :: None => { } // Nothing to do
8783 SysrootKind :: Llvm => {
84+ let default_sysroot =
85+ super :: rustc_info:: get_default_sysroot ( & bootstrap_host_compiler. rustc ) ;
86+
87+ let host_rustlib_lib =
88+ RUSTLIB_DIR . to_path ( dirs) . join ( & bootstrap_host_compiler. triple ) . join ( "lib" ) ;
89+ let target_rustlib_lib = RUSTLIB_DIR . to_path ( dirs) . join ( & target_triple) . join ( "lib" ) ;
90+ fs:: create_dir_all ( & host_rustlib_lib) . unwrap ( ) ;
91+ fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
92+
8893 for file in fs:: read_dir (
8994 default_sysroot
9095 . join ( "lib" )
@@ -122,12 +127,13 @@ pub(crate) fn build_sysroot(
122127 }
123128 }
124129 SysrootKind :: Clif => {
125- build_clif_sysroot_for_triple (
130+ let host = build_clif_sysroot_for_triple (
126131 dirs,
127132 channel,
128133 bootstrap_host_compiler. clone ( ) ,
129134 & cg_clif_dylib_path,
130135 ) ;
136+ host. install_into_sysroot ( & DIST_DIR . to_path ( dirs) ) ;
131137
132138 if !is_native {
133139 build_clif_sysroot_for_triple (
@@ -140,16 +146,16 @@ pub(crate) fn build_sysroot(
140146 bootstrap_target_compiler
141147 } ,
142148 & cg_clif_dylib_path,
143- ) ;
149+ )
150+ . install_into_sysroot ( & DIST_DIR . to_path ( dirs) ) ;
144151 }
145152
146153 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
147154 // libstd.
148- for file in fs:: read_dir ( host_rustlib_lib) . unwrap ( ) {
149- let file = file. unwrap ( ) . path ( ) ;
150- let filename = file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
155+ for lib in host. libs {
156+ let filename = lib. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
151157 if filename. contains ( "std-" ) && !filename. contains ( ".rlib" ) {
152- try_hard_link ( & file , LIB_DIR . to_path ( dirs) . join ( file . file_name ( ) . unwrap ( ) ) ) ;
158+ try_hard_link ( & lib , LIB_DIR . to_path ( dirs) . join ( lib . file_name ( ) . unwrap ( ) ) ) ;
153159 }
154160 }
155161 }
@@ -162,19 +168,36 @@ pub(crate) fn build_sysroot(
162168 target_compiler
163169}
164170
171+ struct SysrootTarget {
172+ triple : String ,
173+ libs : Vec < PathBuf > ,
174+ }
175+
176+ impl SysrootTarget {
177+ fn install_into_sysroot ( & self , sysroot : & Path ) {
178+ let target_rustlib_lib = sysroot. join ( "lib" ) . join ( "rustlib" ) . join ( & self . triple ) . join ( "lib" ) ;
179+ fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
180+
181+ for lib in & self . libs {
182+ try_hard_link ( lib, target_rustlib_lib. join ( lib. file_name ( ) . unwrap ( ) ) ) ;
183+ }
184+ }
185+ }
186+
165187pub ( crate ) static ORIG_BUILD_SYSROOT : RelPath = RelPath :: SOURCE . join ( "build_sysroot" ) ;
166188pub ( crate ) static BUILD_SYSROOT : RelPath = RelPath :: DOWNLOAD . join ( "sysroot" ) ;
167189pub ( crate ) static SYSROOT_RUSTC_VERSION : RelPath = BUILD_SYSROOT . join ( "rustc_version" ) ;
168190pub ( crate ) static SYSROOT_SRC : RelPath = BUILD_SYSROOT . join ( "sysroot_src" ) ;
169191pub ( crate ) static STANDARD_LIBRARY : CargoProject =
170192 CargoProject :: new ( & BUILD_SYSROOT , "build_sysroot" ) ;
171193
194+ #[ must_use]
172195fn build_clif_sysroot_for_triple (
173196 dirs : & Dirs ,
174197 channel : & str ,
175198 mut compiler : Compiler ,
176199 cg_clif_dylib_path : & Path ,
177- ) {
200+ ) -> SysrootTarget {
178201 match fs:: read_to_string ( SYSROOT_RUSTC_VERSION . to_path ( dirs) ) {
179202 Err ( e) => {
180203 eprintln ! ( "Failed to get rustc version for patched sysroot source: {}" , e) ;
@@ -219,7 +242,8 @@ fn build_clif_sysroot_for_triple(
219242 build_cmd. env ( "__CARGO_DEFAULT_LIB_METADATA" , "cg_clif" ) ;
220243 spawn_and_wait ( build_cmd) ;
221244
222- // Copy all relevant files to the sysroot
245+ let mut target_libs = SysrootTarget { triple : compiler. triple , libs : vec ! [ ] } ;
246+
223247 for entry in fs:: read_dir ( build_dir. join ( "deps" ) ) . unwrap ( ) {
224248 let entry = entry. unwrap ( ) ;
225249 if let Some ( ext) = entry. path ( ) . extension ( ) {
@@ -229,9 +253,8 @@ fn build_clif_sysroot_for_triple(
229253 } else {
230254 continue ;
231255 } ;
232- try_hard_link (
233- entry. path ( ) ,
234- RUSTLIB_DIR . to_path ( dirs) . join ( & compiler. triple ) . join ( "lib" ) . join ( entry. file_name ( ) ) ,
235- ) ;
256+ target_libs. libs . push ( entry. path ( ) ) ;
236257 }
258+
259+ target_libs
237260}
0 commit comments