@@ -566,6 +566,16 @@ impl CommandLineStep for Llvm {
566566 // Keep the pre-LLVM23 behavior for now.
567567 cfg. define ( "LLVM_VERSIONED_DYLIB_NAME_ON_DARWIN" , "OFF" ) ;
568568
569+ if link_shared {
570+ // We inform LLVM tools where they can find the LLVM library
571+ // by adding an rpath entry to the expected parent `lib` directory,
572+ // e.g. `rust-objcopy` is shipped in `lib/rustlib/<host>/bin`.
573+ if let Some ( rpath) = llvm_lib_rpath ( builder, target) {
574+ ldflags. exe . push ( " " ) ;
575+ ldflags. exe . push ( rpath) ;
576+ }
577+ }
578+
569579 if ( target. starts_with ( "csky" )
570580 || target. starts_with ( "riscv" )
571581 || target. starts_with ( "sparc-" ) )
@@ -1162,6 +1172,23 @@ fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
11621172 . or_else ( || env:: var_os ( var_base) )
11631173}
11641174
1175+ /// Generate a RPATH entry for a binary that will link to LLVM (libLLVM.so) dynamically.
1176+ /// This will make it possible to execute the binary outside Rustup, so that it can still find the libLLVM.so file.
1177+ fn llvm_lib_rpath ( builder : & Builder < ' _ > , target : TargetSelection ) -> Option < & ' static str > {
1178+ if builder. config . rpath_enabled ( target) && helpers:: use_host_linker ( target) {
1179+ // Be careful when changing this path, we need to ensure it's quoted or escaped:
1180+ // `$ORIGIN` would otherwise be expanded when the `LdFlags` are passed verbatim to
1181+ // cmake.
1182+ if target. contains ( "linux" ) {
1183+ return Some ( "-Wl,-rpath,'$ORIGIN/../../../'" ) ;
1184+ }
1185+ if target. contains ( "apple-darwin" ) {
1186+ return Some ( "-Wl,-rpath,'@loader_path/../../../'" ) ;
1187+ }
1188+ }
1189+ None
1190+ }
1191+
11651192#[ derive( Clone ) ]
11661193pub struct BuiltRustOffload {
11671194 /// Path to the rust offload dylib
@@ -1436,19 +1463,12 @@ impl CommandLineStep for OmpOffload {
14361463 ldflags. push_all ( format ! ( "-L{}" , dir. display( ) ) ) ;
14371464 }
14381465
1439- if builder. config . rpath_enabled ( target)
1440- && helpers:: use_host_linker ( target)
1441- && llvm_output. link_shared ( )
1442- && target. contains ( "linux" )
1443- {
1444- // Same logic as in Lld::run
1466+ if llvm_output. link_shared ( ) {
14451467 // We inform libomptarget.so where it can find LLVM's libraries
14461468 // by adding an rpath entry to the expected parent `lib` directory.
1447- //
1448- // Be careful when changing this path, we need to ensure it's quoted or escaped:
1449- // `$ORIGIN` would otherwise be expanded when the `LdFlags` are passed verbatim to
1450- // cmake.
1451- ldflags. push_all ( "-Wl,-rpath,'$ORIGIN/../../../'" ) ;
1469+ if let Some ( rpath) = llvm_lib_rpath ( builder, target) {
1470+ ldflags. push_all ( rpath) ;
1471+ }
14521472 }
14531473
14541474 configure_cmake ( builder, target, & mut cfg, true , ldflags, cflags, & [ ] ) ;
@@ -1724,21 +1744,11 @@ impl CommandLineStep for Lld {
17241744 // component installed, one can successfully invoke `rust-lld` directly without rustup's
17251745 // `LD_LIBRARY_PATH` overrides)
17261746 //
1727- if builder. config . rpath_enabled ( target)
1728- && helpers:: use_host_linker ( target)
1729- && llvm_output. link_shared ( )
1730- {
1747+ if llvm_output. link_shared ( ) {
17311748 // So we inform LLD where it can find LLVM's libraries by adding an rpath entry to the
17321749 // expected parent `lib` directory.
1733- //
1734- // Be careful when changing this path, we need to ensure it's quoted or escaped:
1735- // `$ORIGIN` would otherwise be expanded when the `LdFlags` are passed verbatim to
1736- // cmake.
1737- if target. contains ( "linux" ) {
1738- ldflags. push_all ( "-Wl,-rpath,'$ORIGIN/../../../'" ) ;
1739- }
1740- if target. contains ( "apple-darwin" ) {
1741- ldflags. push_all ( "-Wl,-rpath,'@loader_path/../../../'" ) ;
1750+ if let Some ( rpath) = llvm_lib_rpath ( builder, target) {
1751+ ldflags. push_all ( rpath) ;
17421752 }
17431753 }
17441754
0 commit comments