@@ -363,19 +363,35 @@ impl Build {
363363 let in_tree_llvm_info = config. in_tree_llvm_info . clone ( ) ;
364364 let in_tree_gcc_info = config. in_tree_gcc_info . clone ( ) ;
365365
366- let initial_target_libdir_str =
367- config. initial_sysroot . join ( "lib/rustlib" ) . join ( config. build ) . join ( "lib" ) ;
366+ let initial_target_libdir =
367+ output ( Command :: new ( & config. initial_rustc ) . args ( [ "--print" , "target-libdir" ] ) )
368+ . trim ( )
369+ . to_owned ( ) ;
370+
371+ let initial_target_dir = Path :: new ( & initial_target_libdir)
372+ . parent ( )
373+ . unwrap_or_else ( || panic ! ( "{initial_target_libdir} has no parent" ) ) ;
368374
369- let initial_target_dir = Path :: new ( & initial_target_libdir_str) . parent ( ) . unwrap ( ) ;
370375 let initial_lld = initial_target_dir. join ( "bin" ) . join ( "rust-lld" ) ;
371376
372- let initial_relative_libdir = initial_target_dir
373- . ancestors ( )
374- . nth ( 2 )
375- . unwrap ( )
376- . strip_prefix ( & config. initial_sysroot )
377- . expect ( "Couldn’t determine initial relative libdir." )
378- . to_path_buf ( ) ;
377+ let initial_relative_libdir = if cfg ! ( test) {
378+ // On tests, bootstrap uses the shim rustc, not the one from the stage0 toolchain.
379+ PathBuf :: default ( )
380+ } else {
381+ let ancestor = initial_target_dir. ancestors ( ) . nth ( 2 ) . unwrap_or_else ( || {
382+ panic ! ( "Not enough ancestors for {}" , initial_target_dir. display( ) )
383+ } ) ;
384+
385+ ancestor
386+ . strip_prefix ( & config. initial_sysroot )
387+ . unwrap_or_else ( |_| {
388+ panic ! (
389+ "Couldn’t resolve the initial relative libdir from {}" ,
390+ initial_target_dir. display( )
391+ )
392+ } )
393+ . to_path_buf ( )
394+ } ;
379395
380396 let version = std:: fs:: read_to_string ( src. join ( "src" ) . join ( "version" ) )
381397 . expect ( "failed to read src/version" ) ;
0 commit comments