Skip to content

Commit

Permalink
Fix naming of ambiguous libs (bazelbuild#1625)
Browse files Browse the repository at this point in the history
Currently, we emit symlinks that disambiguate native libs in the form of `libfoo.pic-{hash}.a` when building shared libraries in `opt` mode. I am not sure if anything depends on these output files to end with `.pic.a` instead, however I beileve it `libfoo-{hash}.pic.a` is a better name.
  • Loading branch information
scentini authored Nov 16, 2022
1 parent ff314d4 commit aa0815d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
10 changes: 8 additions & 2 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,17 @@ def _symlink_for_ambiguous_lib(actions, toolchain, crate_info, lib):
path_hash = abs(hash(lib.path))
lib_name = get_lib_name_for_windows(lib) if toolchain.os.startswith("windows") else get_lib_name_default(lib)

prefix = "lib"
extension = ".a"
if toolchain.os.startswith("windows"):
prefix = ""
extension = ".lib"
elif lib_name.endswith(".pic"):
# Strip the .pic suffix
lib_name = lib_name[:-4]
prefix = "lib"
extension = ".pic.a"
else:
prefix = "lib"
extension = ".a"

# Ensure the symlink follows the lib<name>.a pattern on Unix-like platforms
# or <name>.lib on Windows.
Expand Down
43 changes: 38 additions & 5 deletions test/unit/ambiguous_libs/ambiguous_libs_test.bzl
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
"""Unittests for ambiguous native dependencies."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_proc_macro", "rust_shared_library", "rust_static_library")
load(
"//rust:defs.bzl",
"rust_binary",
"rust_common",
"rust_library",
"rust_proc_macro",
"rust_shared_library",
"rust_static_library",
)

def _get_crate_info(target):
return target[rust_common.crate_info] if rust_common.crate_info in target else target[rust_common.test_crate_info].crate

def _ambiguous_deps_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
rustc_action = [action for action in tut.actions if action.mnemonic == "Rustc"][0]

# We depend on two C++ libraries named "native_dep", which we need to pass to the command line
# in the form of "-lstatic=native-dep-{hash}.a or "-lstatic=native-dep.pic-{hash}.a.
# Note that the second form should actually be "-lstatic=native-dep-{hash}.pic.a instead.
link_args = [arg for arg in rustc_action.argv if arg.startswith("-lstatic=native_dep")]
# in the form of "-lstatic=native-dep-{hash} "-lstatic=native-dep-{hash}.pic.
link_args = [arg for arg in rustc_action.argv if arg.startswith("-lstatic=native_dep-")]
asserts.equals(env, 2, len(link_args))
asserts.false(env, link_args[0] == link_args[1])

for_shared_library = _get_crate_info(tut).type in ("dylib", "cdylib", "proc-macro")
extension = _get_pic_suffix(ctx, for_shared_library)

asserts.true(env, link_args[0].endswith(extension))
asserts.true(env, link_args[1].endswith(extension))

return analysistest.end(env)

ambiguous_deps_test = analysistest.make(_ambiguous_deps_test_impl)
def _get_pic_suffix(ctx, for_shared_library):
if ctx.target_platform_has_constraint(
ctx.attr._windows_constraint[platform_common.ConstraintValueInfo],
) or ctx.target_platform_has_constraint(
ctx.attr._macos_constraint[platform_common.ConstraintValueInfo],
):
return ""
else:
compilation_mode = ctx.var["COMPILATION_MODE"]
return ".pic" if compilation_mode == "opt" and for_shared_library else ""

ambiguous_deps_test = analysistest.make(
_ambiguous_deps_test_impl,
attrs = {
"_macos_constraint": attr.label(default = Label("@platforms//os:macos")),
"_windows_constraint": attr.label(default = Label("@platforms//os:windows")),
},
)

def _create_test_targets():
rust_library(
Expand Down

0 comments on commit aa0815d

Please sign in to comment.