Skip to content

Commit 8bc679c

Browse files
committed
Give every bundled cdylib artifact a soname or install name
A library shipped under lib/ needs a layout-neutral identity whether or not an extension in the same wheel links it yet. Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
1 parent 0ae7445 commit 8bc679c

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ Every Cargo invocation sees `HATCH_RS_LIBRARY_SOURCE`. An artifact with
241241

242242
In `bundled` mode hatch-rs builds the library first, gives it a layout-neutral
243243
identity (`-soname libfoo.so` on Linux, `@rpath/libfoo.dylib` as the install
244-
name on macOS), copies it to its destination, and links the extension with
244+
name on macOS; every `cdylib` artifact gets this, linked or not), copies it to
245+
its destination, and links the extension with
245246
`-L`/`-l dylib` plus an rpath relative to the extension (`$ORIGIN/lib` or
246247
`@loader_path/lib` for the layout above). On Windows the import library is
247248
packaged next to the DLL, and the package must call

‎hatch_rs/structs.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ def _library_rustc_args(self, artifact: RustArtifactConfig, resolved_target: Res
12541254
mode = self._resolved_library_source
12551255
platform = resolved_target.platform
12561256
args = []
1257-
if artifact.name in self._linked_names and mode == "bundled":
1257+
if artifact.name and mode == "bundled" and self._artifact_role(artifact) == "cdylib":
12581258
shared_library = shared_library_name(artifact.name, platform=platform)
12591259
if platform == "darwin":
12601260
args.extend(("-C", f"link-arg=-Wl,-install_name,@rpath/{shared_library}"))

‎hatch_rs/tests/test_structs.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def test_build_plan_generates_explicit_artifacts(tmp_path):
542542

543543
assert plan.generate() == [
544544
"cargo rustc --manifest-path Cargo.toml --release --target x86_64-unknown-linux-gnu -- --crate-type cdylib",
545-
"cargo rustc --manifest-path rust/Cargo.toml --release --target x86_64-unknown-linux-gnu -- -C opt-level=3 --crate-type cdylib",
545+
"cargo rustc --manifest-path rust/Cargo.toml --release --target x86_64-unknown-linux-gnu -- -C link-arg=-Wl,-soname,libproject_ffi.so -C opt-level=3 --crate-type cdylib",
546546
]
547547

548548
first_target_dir = plan.cargo_invocations[0].env["CARGO_TARGET_DIR"]
@@ -576,7 +576,7 @@ def test_build_plan_skips_artifact_when_skip_if_env_is_truthy(tmp_path, monkeypa
576576
monkeypatch.delenv("PROJECT_SKIP_SHARED_LIBRARY", raising=False)
577577
assert plan.generate() == [
578578
"cargo rustc --manifest-path Cargo.toml --release --target x86_64-unknown-linux-gnu -- --crate-type cdylib",
579-
"cargo rustc --manifest-path rust/Cargo.toml --release --target x86_64-unknown-linux-gnu -- --crate-type cdylib",
579+
"cargo rustc --manifest-path rust/Cargo.toml --release --target x86_64-unknown-linux-gnu -- -C link-arg=-Wl,-soname,libproject_ffi.so --crate-type cdylib",
580580
]
581581

582582
monkeypatch.setenv("PROJECT_SKIP_SHARED_LIBRARY", "1")
@@ -1065,6 +1065,19 @@ def test_build_plan_bundled_library_sets_install_name_on_darwin(tmp_path, monkey
10651065
assert "-C link-arg=-Wl,-rpath,@loader_path/lib" in extension
10661066

10671067

1068+
def test_build_plan_bundled_cdylib_gets_identity_without_consumer(tmp_path, monkeypatch):
1069+
monkeypatch.delenv("HATCH_RS_LIBRARY_SOURCE", raising=False)
1070+
plan = HatchRustBuildPlan(
1071+
module="project",
1072+
path=tmp_path,
1073+
target="x86_64-unknown-linux-gnu",
1074+
artifacts=[{"name": "project_ffi", "manifest": "rust/Cargo.toml", "destination": "project/lib/{shared_library}"}],
1075+
)
1076+
(library,) = plan.generate()
1077+
1078+
assert "-C link-arg=-Wl,-soname,libproject_ffi.so" in library
1079+
1080+
10681081
def test_build_plan_bundled_library_links_import_library_on_windows(tmp_path, monkeypatch):
10691082
monkeypatch.chdir(tmp_path)
10701083
monkeypatch.delenv("HATCH_RS_LIBRARY_SOURCE", raising=False)

0 commit comments

Comments
 (0)