Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use output hash in filename for crates of type cdylib #1066

Merged
merged 2 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ def _assert_correct_dep_mapping(ctx):
),
)

def _determine_lib_name(name, crate_type, toolchain, lib_hash = ""):
def _determine_lib_name(name, crate_type, toolchain, lib_hash = None):
"""See https://github.com/bazelbuild/rules_rust/issues/405

Args:
name (str): The name of the current target
crate_type (str): The `crate_type`
toolchain (rust_toolchain): The current `rust_toolchain`
lib_hash (str, optional): The hashed crate root path. Defaults to "".
lib_hash (str, optional): The hashed crate root path

Returns:
str: A unique library name
Expand Down Expand Up @@ -97,10 +97,10 @@ def _determine_lib_name(name, crate_type, toolchain, lib_hash = ""):
if toolchain.target_arch == "wasm32" and crate_type == "cdylib":
prefix = ""

return "{prefix}{name}-{lib_hash}{extension}".format(
return "{prefix}{name}{lib_hash}{extension}".format(
prefix = prefix,
name = name,
lib_hash = lib_hash,
lib_hash = "-" + lib_hash if lib_hash else "",
extension = extension,
)

Expand Down Expand Up @@ -243,8 +243,15 @@ def _rust_library_common(ctx, crate_type):

toolchain = find_toolchain(ctx)

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

crate_name = crate_name_from_attr(ctx.attr)
rust_lib_name = _determine_lib_name(
Expand Down
4 changes: 4 additions & 0 deletions test/unit/cdylib_name/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load(":cdylib_name_analysis_test.bzl", "cdylib_name_analysis_test_suite")

############################ UNIT TESTS #############################
cdylib_name_analysis_test_suite(name = "cdylib_name_analysis_test_suite")
48 changes: 48 additions & 0 deletions test/unit/cdylib_name/cdylib_name_analysis_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Analysis tests for the name we assign to cdylib libraries."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//rust:defs.bzl", "rust_shared_library")

def _cdylib_name_test_impl(ctx):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)

# We're expecting the `.dylib`/`.so` to be the only file on Unix and Windows to
# contain a pair of `.dll` and `.lib` files.
files = target[DefaultInfo].files.to_list()
if len(files) == 1:
asserts.true(env, files[0].extension in ("so", "dylib"))
if files[0].extension == "so":
asserts.equals(env, files[0].basename, "libsomething.so")
elif files[0].extension == "dylib":
asserts.equals(env, files[0].basename, "libsomething.dylib")
elif len(files) == 2:
expected_filenames = ["something.dll", "something.dll.lib"]
for file in files:
asserts.true(env, file.basename in expected_filenames)
expected_filenames.remove(file.basename)

return analysistest.end(env)

cdylib_name_test = analysistest.make(_cdylib_name_test_impl)

def cdylib_name_analysis_test_suite(name):
"""Analysis tests for the name we assign to cdylib libraries.
Args:
name: the test suite name
"""
rust_shared_library(
name = "something",
srcs = ["lib.rs"],
)

cdylib_name_test(
name = "cdylib_name_test",
target_under_test = ":something",
)

native.test_suite(
name = name,
tests = [":cdylib_name_test"],
)
4 changes: 4 additions & 0 deletions test/unit/cdylib_name/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub extern "C" fn say_hello() {
println!("Hello");
}