Skip to content

Commit 38211c2

Browse files
authored
Don't use hash for cdylib (#1066)
1 parent 4128266 commit 38211c2

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

rust/private/rust.bzl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ def _assert_correct_dep_mapping(ctx):
6161
),
6262
)
6363

64-
def _determine_lib_name(name, crate_type, toolchain, lib_hash = ""):
64+
def _determine_lib_name(name, crate_type, toolchain, lib_hash = None):
6565
"""See https://github.com/bazelbuild/rules_rust/issues/405
6666
6767
Args:
6868
name (str): The name of the current target
6969
crate_type (str): The `crate_type`
7070
toolchain (rust_toolchain): The current `rust_toolchain`
71-
lib_hash (str, optional): The hashed crate root path. Defaults to "".
71+
lib_hash (str, optional): The hashed crate root path
7272
7373
Returns:
7474
str: A unique library name
@@ -97,10 +97,10 @@ def _determine_lib_name(name, crate_type, toolchain, lib_hash = ""):
9797
if toolchain.target_arch == "wasm32" and crate_type == "cdylib":
9898
prefix = ""
9999

100-
return "{prefix}{name}-{lib_hash}{extension}".format(
100+
return "{prefix}{name}{lib_hash}{extension}".format(
101101
prefix = prefix,
102102
name = name,
103-
lib_hash = lib_hash,
103+
lib_hash = "-" + lib_hash if lib_hash else "",
104104
extension = extension,
105105
)
106106

@@ -243,8 +243,15 @@ def _rust_library_common(ctx, crate_type):
243243

244244
toolchain = find_toolchain(ctx)
245245

246-
# Determine unique hash for this rlib
247-
output_hash = determine_output_hash(crate_root)
246+
# Determine unique hash for this rlib.
247+
# Note that we don't include a hash for `cdylib` since they are meant to be consumed externally and having a
248+
# deterministic name is important since it ends up embedded in the executable. This is problematic when one needs
249+
# to include the library with a specific filename into a larger application.
250+
# (see https://github.com/bazelbuild/rules_rust/issues/405#issuecomment-993089889 for more details)
251+
if crate_type != "cdylib":
252+
output_hash = determine_output_hash(crate_root)
253+
else:
254+
output_hash = None
248255

249256
crate_name = crate_name_from_attr(ctx.attr)
250257
rust_lib_name = _determine_lib_name(

test/unit/cdylib_name/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
load(":cdylib_name_analysis_test.bzl", "cdylib_name_analysis_test_suite")
2+
3+
############################ UNIT TESTS #############################
4+
cdylib_name_analysis_test_suite(name = "cdylib_name_analysis_test_suite")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Analysis tests for the name we assign to cdylib libraries."""
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4+
load("//rust:defs.bzl", "rust_shared_library")
5+
6+
def _cdylib_name_test_impl(ctx):
7+
env = analysistest.begin(ctx)
8+
target = analysistest.target_under_test(env)
9+
10+
# We're expecting the `.dylib`/`.so` to be the only file on Unix and Windows to
11+
# contain a pair of `.dll` and `.lib` files.
12+
files = target[DefaultInfo].files.to_list()
13+
if len(files) == 1:
14+
asserts.true(env, files[0].extension in ("so", "dylib"))
15+
if files[0].extension == "so":
16+
asserts.equals(env, files[0].basename, "libsomething.so")
17+
elif files[0].extension == "dylib":
18+
asserts.equals(env, files[0].basename, "libsomething.dylib")
19+
elif len(files) == 2:
20+
expected_filenames = ["something.dll", "something.dll.lib"]
21+
for file in files:
22+
asserts.true(env, file.basename in expected_filenames)
23+
expected_filenames.remove(file.basename)
24+
25+
return analysistest.end(env)
26+
27+
cdylib_name_test = analysistest.make(_cdylib_name_test_impl)
28+
29+
def cdylib_name_analysis_test_suite(name):
30+
"""Analysis tests for the name we assign to cdylib libraries.
31+
32+
Args:
33+
name: the test suite name
34+
"""
35+
rust_shared_library(
36+
name = "something",
37+
srcs = ["lib.rs"],
38+
)
39+
40+
cdylib_name_test(
41+
name = "cdylib_name_test",
42+
target_under_test = ":something",
43+
)
44+
45+
native.test_suite(
46+
name = name,
47+
tests = [":cdylib_name_test"],
48+
)

test/unit/cdylib_name/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[no_mangle]
2+
pub extern "C" fn say_hello() {
3+
println!("Hello");
4+
}

0 commit comments

Comments
 (0)