Skip to content

Commit 84e98e4

Browse files
krasimirgghlopko
andauthored
don't emit --codegen={metadata,extra-filename} for rust_static_library and rust_shared_library (#1222)
Extracted from https://github.com/bazelbuild/rules_rust/pull/1219/files, addressing this part: * currently we don't include the output hash for cdylib, but we do for staticlib. It's more accurate to not include the output hash for staticlib as well. Related to, but does not address #1063. @scentini suggested to not even emit --codegen flags in these cases. Co-authored-by: Marcel Hlopko <[email protected]>
1 parent e48c834 commit 84e98e4

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

rust/private/rust.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ def _rust_library_common(ctx, crate_type):
245245
toolchain = find_toolchain(ctx)
246246

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

257257
crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)
258258
rust_lib_name = _determine_lib_name(

rust/private/rustc.bzl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,12 +714,20 @@ def construct_arguments(
714714
if hasattr(attr, "_error_format"):
715715
rustc_flags.add("--error-format=" + attr._error_format[ErrorFormatInfo].error_format)
716716

717-
# Mangle symbols to disambiguate crates with the same name
718-
extra_filename = "-" + output_hash if output_hash else ""
719-
rustc_flags.add("--codegen=metadata=" + extra_filename)
717+
# Mangle symbols to disambiguate crates with the same name. This could
718+
# happen only for non-final artifacts where we compute an output_hash,
719+
# e.g., rust_library.
720+
#
721+
# For "final" artifacts and ones intended for distribution outside of
722+
# Bazel, such as rust_binary, rust_static_library and rust_shared_library,
723+
# where output_hash is None we don't need to add these flags.
724+
if output_hash:
725+
extra_filename = "-" + output_hash
726+
rustc_flags.add("--codegen=metadata=" + extra_filename)
727+
rustc_flags.add("--codegen=extra-filename=" + extra_filename)
728+
720729
if output_dir:
721730
rustc_flags.add("--out-dir=" + output_dir)
722-
rustc_flags.add("--codegen=extra-filename=" + extra_filename)
723731

724732
compilation_mode = get_compilation_mode_opts(ctx, toolchain)
725733
rustc_flags.add("--codegen=opt-level=" + compilation_mode.opt_level)

test/unit/crate_name/crate_name_test.bzl

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Unit tests for crate names."""
22

33
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4-
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
5-
load("//test/unit:common.bzl", "assert_argv_contains")
4+
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_shared_library", "rust_static_library", "rust_test")
5+
load("//test/unit:common.bzl", "assert_argv_contains", "assert_argv_contains_prefix_not")
66

77
def _default_crate_name_library_test_impl(ctx):
88
env = analysistest.begin(ctx)
@@ -68,9 +68,22 @@ def _slib_library_name_test_impl(ctx):
6868
"""
6969
env = analysistest.begin(ctx)
7070
tut = analysistest.target_under_test(env)
71+
assert_argv_contains(env, tut.actions[0], "--codegen=metadata=-2102077805")
7172
assert_argv_contains(env, tut.actions[0], "--codegen=extra-filename=-2102077805")
7273
return analysistest.end(env)
7374

75+
def _no_extra_filename_test_impl(ctx):
76+
"""Check that no extra filename is used.
77+
78+
Args:
79+
ctx: rule context.
80+
"""
81+
env = analysistest.begin(ctx)
82+
tut = analysistest.target_under_test(env)
83+
assert_argv_contains_prefix_not(env, tut.actions[0], "--codegen=metadata=")
84+
assert_argv_contains_prefix_not(env, tut.actions[0], "--codegen=extra-filename=")
85+
return analysistest.end(env)
86+
7487
default_crate_name_library_test = analysistest.make(
7588
_default_crate_name_library_test_impl,
7689
)
@@ -100,6 +113,9 @@ invalid_custom_crate_name_test = analysistest.make(
100113
slib_library_name_test = analysistest.make(
101114
_slib_library_name_test_impl,
102115
)
116+
no_extra_filename_test = analysistest.make(
117+
_no_extra_filename_test_impl,
118+
)
103119

104120
def _crate_name_test():
105121
rust_library(
@@ -153,6 +169,16 @@ def _crate_name_test():
153169
srcs = ["slib.rs"],
154170
)
155171

172+
rust_shared_library(
173+
name = "shared_lib",
174+
srcs = ["lib.rs"],
175+
)
176+
177+
rust_static_library(
178+
name = "static_lib",
179+
srcs = ["lib.rs"],
180+
)
181+
156182
slib_library_name_test(
157183
name = "slib_library_name_test",
158184
target_under_test = ":slib",
@@ -198,6 +224,16 @@ def _crate_name_test():
198224
target_under_test = ":invalid-custom-crate-name",
199225
)
200226

227+
no_extra_filename_test(
228+
name = "no_extra_filename_for_shared_library_test",
229+
target_under_test = ":shared_lib",
230+
)
231+
232+
no_extra_filename_test(
233+
name = "no_extra_filename_for_static_library_test",
234+
target_under_test = ":static_lib",
235+
)
236+
201237
def crate_name_test_suite(name):
202238
"""Entry-point macro called from the BUILD file.
203239

0 commit comments

Comments
 (0)