@@ -87,7 +87,7 @@ impl Step for Std {
8787 target_deps. extend ( copy_third_party_objects ( builder, & compiler, target) . into_iter ( ) ) ;
8888
8989 let mut cargo = builder. cargo ( compiler, Mode :: Std , target, "build" ) ;
90- std_cargo ( builder, & compiler , target, & mut cargo) ;
90+ std_cargo ( builder, target, & mut cargo) ;
9191
9292 builder. info ( & format ! (
9393 "Building stage{} std artifacts ({} -> {})" ,
@@ -153,17 +153,18 @@ fn copy_third_party_objects(
153153 copy_and_stamp ( Path :: new ( & src) , "libunwind.a" ) ;
154154 }
155155
156+ if builder. config . sanitizers && compiler. stage != 0 {
157+ // The sanitizers are only copied in stage1 or above,
158+ // to avoid creating dependency on LLVM.
159+ target_deps. extend ( copy_sanitizers ( builder, & compiler, target) ) ;
160+ }
161+
156162 target_deps
157163}
158164
159165/// Configure cargo to compile the standard library, adding appropriate env vars
160166/// and such.
161- pub fn std_cargo (
162- builder : & Builder < ' _ > ,
163- compiler : & Compiler ,
164- target : Interned < String > ,
165- cargo : & mut Cargo ,
166- ) {
167+ pub fn std_cargo ( builder : & Builder < ' _ > , target : Interned < String > , cargo : & mut Cargo ) {
167168 if let Some ( target) = env:: var_os ( "MACOSX_STD_DEPLOYMENT_TARGET" ) {
168169 cargo. env ( "MACOSX_DEPLOYMENT_TARGET" , target) ;
169170 }
@@ -206,19 +207,6 @@ pub fn std_cargo(
206207 let mut features = builder. std_features ( ) ;
207208 features. push_str ( & compiler_builtins_c_feature) ;
208209
209- if compiler. stage != 0 && builder. config . sanitizers {
210- // This variable is used by the sanitizer runtime crates, e.g.
211- // rustc_lsan, to build the sanitizer runtime from C code
212- // When this variable is missing, those crates won't compile the C code,
213- // so we don't set this variable during stage0 where llvm-config is
214- // missing
215- // We also only build the runtimes when --enable-sanitizers (or its
216- // config.toml equivalent) is used
217- let llvm_config = builder. ensure ( native:: Llvm { target : builder. config . build } ) ;
218- cargo. env ( "LLVM_CONFIG" , llvm_config) ;
219- cargo. env ( "RUSTC_BUILD_SANITIZERS" , "1" ) ;
220- }
221-
222210 cargo
223211 . arg ( "--features" )
224212 . arg ( features)
@@ -276,31 +264,43 @@ impl Step for StdLink {
276264 let libdir = builder. sysroot_libdir ( target_compiler, target) ;
277265 let hostdir = builder. sysroot_libdir ( target_compiler, compiler. host ) ;
278266 add_to_sysroot ( builder, & libdir, & hostdir, & libstd_stamp ( builder, compiler, target) ) ;
279-
280- if builder. config . sanitizers && compiler. stage != 0 && target == "x86_64-apple-darwin" {
281- // The sanitizers are only built in stage1 or above, so the dylibs will
282- // be missing in stage0 and causes panic. See the `std()` function above
283- // for reason why the sanitizers are not built in stage0.
284- copy_apple_sanitizer_dylibs ( builder, & builder. native_dir ( target) , "osx" , & libdir) ;
285- }
286267 }
287268}
288269
289- fn copy_apple_sanitizer_dylibs (
270+ /// Copies sanitizer runtime libraries into target libdir.
271+ fn copy_sanitizers (
290272 builder : & Builder < ' _ > ,
291- native_dir : & Path ,
292- platform : & str ,
293- into : & Path ,
294- ) {
295- for & sanitizer in & [ "asan" , "tsan" ] {
296- let filename = format ! ( "lib__rustc__clang_rt.{}_{}_dynamic.dylib" , sanitizer, platform) ;
297- let mut src_path = native_dir. join ( sanitizer) ;
298- src_path. push ( "build" ) ;
299- src_path. push ( "lib" ) ;
300- src_path. push ( "darwin" ) ;
301- src_path. push ( & filename) ;
302- builder. copy ( & src_path, & into. join ( filename) ) ;
273+ compiler : & Compiler ,
274+ target : Interned < String > ,
275+ ) -> Vec < PathBuf > {
276+ let runtimes: Vec < native:: SanitizerRuntime > = builder. ensure ( native:: Sanitizers { target } ) ;
277+
278+ if builder. config . dry_run {
279+ return Vec :: new ( ) ;
280+ }
281+
282+ let mut target_deps = Vec :: new ( ) ;
283+ let libdir = builder. sysroot_libdir ( * compiler, target) ;
284+
285+ for runtime in & runtimes {
286+ let dst = libdir. join ( & runtime. name ) ;
287+ builder. copy ( & runtime. path , & dst) ;
288+
289+ if target == "x86_64-apple-darwin" {
290+ // Update the library install name reflect the fact it has been renamed.
291+ let status = Command :: new ( "install_name_tool" )
292+ . arg ( "-id" )
293+ . arg ( format ! ( "@rpath/{}" , runtime. name) )
294+ . arg ( & dst)
295+ . status ( )
296+ . expect ( "failed to execute `install_name_tool`" ) ;
297+ assert ! ( status. success( ) ) ;
298+ }
299+
300+ target_deps. push ( dst) ;
303301 }
302+
303+ target_deps
304304}
305305
306306#[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
0 commit comments