@@ -77,6 +77,14 @@ impl Step for Std {
7777 target,
7878 } ) ;
7979 println ! ( "Uplifting stage1 std ({} -> {})" , from. host, target) ;
80+
81+ // Even if we're not building std this stage, the new sysroot must
82+ // still contain the musl startup objects.
83+ if target. contains ( "musl" ) && !target. contains ( "mips" ) {
84+ let libdir = builder. sysroot_libdir ( compiler, target) ;
85+ copy_musl_third_party_objects ( build, target, & libdir) ;
86+ }
87+
8088 builder. ensure ( StdLink {
8189 compiler : from,
8290 target_compiler : compiler,
@@ -89,6 +97,11 @@ impl Step for Std {
8997 println ! ( "Building stage{} std artifacts ({} -> {})" , compiler. stage,
9098 & compiler. host, target) ;
9199
100+ if target. contains ( "musl" ) && !target. contains ( "mips" ) {
101+ let libdir = builder. sysroot_libdir ( compiler, target) ;
102+ copy_musl_third_party_objects ( build, target, & libdir) ;
103+ }
104+
92105 let out_dir = build. cargo_out ( compiler, Mode :: Libstd , target) ;
93106 build. clear_if_dirty ( & out_dir, & builder. rustc ( compiler) ) ;
94107 let mut cargo = builder. cargo ( compiler, Mode :: Libstd , target, "build" ) ;
@@ -105,6 +118,20 @@ impl Step for Std {
105118 }
106119}
107120
121+ /// Copies the crt(1,i,n).o startup objects
122+ ///
123+ /// Since musl supports fully static linking, we can cross link for it even
124+ /// with a glibc-targeting toolchain, given we have the appropriate startup
125+ /// files. As those shipped with glibc won't work, copy the ones provided by
126+ /// musl so we have them on linux-gnu hosts.
127+ fn copy_musl_third_party_objects ( build : & Build ,
128+ target : Interned < String > ,
129+ into : & Path ) {
130+ for & obj in & [ "crt1.o" , "crti.o" , "crtn.o" ] {
131+ copy ( & build. musl_root ( target) . unwrap ( ) . join ( "lib" ) . join ( obj) , & into. join ( obj) ) ;
132+ }
133+ }
134+
108135/// Configure cargo to compile the standard library, adding appropriate env vars
109136/// and such.
110137pub fn std_cargo ( build : & Build ,
@@ -189,10 +216,6 @@ impl Step for StdLink {
189216 let libdir = builder. sysroot_libdir ( target_compiler, target) ;
190217 add_to_sysroot ( & libdir, & libstd_stamp ( build, compiler, target) ) ;
191218
192- if target. contains ( "musl" ) && !target. contains ( "mips" ) {
193- copy_musl_third_party_objects ( build, target, & libdir) ;
194- }
195-
196219 if build. config . sanitizers && compiler. stage != 0 && target == "x86_64-apple-darwin" {
197220 // The sanitizers are only built in stage1 or above, so the dylibs will
198221 // be missing in stage0 and causes panic. See the `std()` function above
@@ -208,15 +231,6 @@ impl Step for StdLink {
208231 }
209232}
210233
211- /// Copies the crt(1,i,n).o startup objects
212- ///
213- /// Only required for musl targets that statically link to libc
214- fn copy_musl_third_party_objects ( build : & Build , target : Interned < String > , into : & Path ) {
215- for & obj in & [ "crt1.o" , "crti.o" , "crtn.o" ] {
216- copy ( & build. musl_root ( target) . unwrap ( ) . join ( "lib" ) . join ( obj) , & into. join ( obj) ) ;
217- }
218- }
219-
220234fn copy_apple_sanitizer_dylibs ( native_dir : & Path , platform : & str , into : & Path ) {
221235 for & sanitizer in & [ "asan" , "tsan" ] {
222236 let filename = format ! ( "libclang_rt.{}_{}_dynamic.dylib" , sanitizer, platform) ;
0 commit comments