Skip to content

Commit 58c344e

Browse files
authored
Rollup merge of #164134 - danieljhkim:fix-rust-objcopy-rpath, r=Kobzol
bootstrap: add an rpath to LLVM library for LLVM tools Closes [#164063](#164063) by adding rpath to the LLVM tool executables on both Linux and macOS, using the same approach as Lld::run. This PR is now written by a human, resubmitting [#164064](#164064) which was diagnosed and written by Claude. **Validation:** macOS ```bash ❯ otool -l ~/workspace/repos/rust/build/aarch64-apple-darwin/llvm/bin/llvm-objcopy | grep -A2 LC_RPATH cmd LC_RPATH cmdsize 40 path @loader_path/../../../ (offset 12) -- cmd LC_RPATH cmdsize 32 path @loader_path/../lib (offset 12) ❯ otool -L build/aarch64-apple-darwin/llvm/lib/libLLVM.dylib build/aarch64-apple-darwin/llvm/lib/libLLVM.dylib: @rpath/libLLVM.dylib (compatibility version 1.0.0, current version 23.1.3) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1359.0.0) /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.12) /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 2200.27.0) ``` linux ```bash readelf -d build/x86_64-unknown-linux-gnu/llvm/bin/llvm-objcopy | grep RUNPATH 0x000000000000001d (RUNPATH) Library runpath: [$ORIGIN/../../../:$ORIGIN/../lib:$ORIGIN/../lib/x86_64-unknown-linux-gnu] ```
2 parents 8ac481b + 46395c5 commit 58c344e

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Change this file to make users of the `download-ci-llvm` configuration download
22
a new version of LLVM from CI, even if the LLVM submodule hasn’t changed.
33

4-
Last change is for: https://github.com/rust-lang/rust/pull/163954
4+
Last change is for: https://github.com/rust-lang/rust/pull/164134

‎src/bootstrap/src/core/build_steps/llvm.rs‎

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
11661193
pub 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

Comments
 (0)