Transitive shared libraries not added to rpath of otherwise static binaries
- Dominant language
- Rust
- Stars
- 4.4k
- Forks
- 394
- PR merge metrics
- No merged PRs in 30d
Description
This is sort of a follow up to https://github.com/facebook/buck2/issues/571.
When building a target with `link_style = "static"`, where a transitive dependency is a shared library due to its usage of `preferred_linkage = "shared"`, this shared library does not get added to the rpath of the top-level target.
In other words, given `binary` -> `static.a` -> `shared.so`, `shared.so` will not be available to `binary`.
Here is a minimal reproduction on top of a `buck2 init --git` with the latest release:
```
diff --git a/BUCK b/BUCK
index 1cb6b38..94b9ee5 100644
--- a/BUCK
+++ b/BUCK
@@ -1,7 +1,20 @@
-# A list of available rules and their signatures can be found here: https://buck2.build/docs/api/rules/
+cxx_binary(
+ name = "test",
+ srcs = ["main.cpp"],
+ deps = [":static_lib"],
+ link_style = "static",
+)
+
+cxx_library(
+ name = "static_lib",
+ srcs = ["static_lib.cpp"],
+ deps = [":shared_lib"],
+)
-genrule(
- name = "hello_world",
- out = "out.txt",
- cmd = "echo BUILT BY BUCK2> $OUT",
+cxx_library(
+ name = "shared_lib",
+ srcs = ["shared_lib.cpp"],
+ preferred_linkage = "shared",
)
+
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..01d671a
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,6 @@
+void static_lib_fn();
+
+int main()
+{
+ static_lib_fn();
+}
diff --git a/shared_lib.cpp b/shared_lib.cpp
new file mode 100644
index 0000000..6451836
--- /dev/null
+++ b/shared_lib.cpp
@@ -0,0 +1,3 @@
+void dynamic_lib_fn()
+{
+}
diff --git a/static_lib.cpp b/static_lib.cpp
new file mode 100644
index 0000000..39e88ae
--- /dev/null
+++ b/static_lib.cpp
@@ -0,0 +1,6 @@
+void dynamic_lib_fn();
+
+void static_lib_fn()
+{
+ dynamic_lib_fn();
+}
```
Running `readelf -d $(buck2 build --show-full-simple-output :test)` shows that the resulting binary has no rpath because link_style was set to `static`. As expected, `buck2 run :test` will fail due to the shared library not being found.
Is this just a current limitation of the prelude, or is there something fundamentally wrong with my approach here?
Contributor guide
Assessment
This issue has not been assessed yet.