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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.2.0] Apply repo mapping to cc_shared_library's exports_filter #22328

Merged
merged 1 commit into from
May 11, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
"""cc_binary Starlark implementation replacing native"""

load(":common/cc/cc_binary_attrs.bzl", "cc_binary_attrs")
load(":common/cc/cc_shared_library.bzl", "cc_shared_library_initializer")
load(":common/cc/cc_common.bzl", "cc_common")
load(":common/cc/cc_helper.bzl", "cc_helper", "linker_mode")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/cc/cc_shared_library.bzl", "GraphNodeInfo", "add_unused_dynamic_deps", "build_exports_map_from_only_dynamic_deps", "build_link_once_static_libs_map", "merge_cc_shared_library_infos", "separate_static_and_dynamic_link_libraries", "sort_linker_inputs", "throw_linked_but_not_exported_errors")
load(":common/cc/cc_shared_library.bzl", "GraphNodeInfo", "add_unused_dynamic_deps", "build_exports_map_from_only_dynamic_deps", "build_link_once_static_libs_map", "dynamic_deps_initializer", "merge_cc_shared_library_infos", "separate_static_and_dynamic_link_libraries", "sort_linker_inputs", "throw_linked_but_not_exported_errors")
load(":common/cc/semantics.bzl", "semantics")

DebugPackageInfo = _builtins.toplevel.DebugPackageInfo
Expand Down Expand Up @@ -924,7 +923,7 @@ def _impl(ctx):

cc_binary = rule(
implementation = _impl,
initializer = cc_shared_library_initializer,
initializer = dynamic_deps_initializer,
attrs = cc_binary_attrs,
outputs = {
"stripped_binary": "%{name}.stripped",
Expand Down
25 changes: 24 additions & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,31 @@ graph_structure_aspect = aspect(
implementation = _graph_structure_aspect_impl,
)

def _cc_shared_library_initializer(**kwargs):
"""Converts labels in exports_filter into canonical form relative to the current repository.

This conversion can only be done in a macro as it requires access to the repository mapping of
the repository containing the cc_shared_library target. This mapping is automatically
applied to label attributes, but exports_filter is a list of strings attribute.
"""
if "exports_filter" not in kwargs:
return kwargs

raw_exports_filter = kwargs["exports_filter"]
if type(raw_exports_filter) != type([]):
# TODO: Also canonicalize labels in selects once macros can operate on them.
# https://github.com/bazelbuild/bazel/issues/14157
return kwargs

canonical_exports_filter = [
str(_builtins.native.package_relative_label(s))
for s in raw_exports_filter
]
return kwargs | {"exports_filter": canonical_exports_filter}

cc_shared_library = rule(
implementation = _cc_shared_library_impl,
initializer = _cc_shared_library_initializer,
attrs = {
"additional_linker_inputs": attr.label_list(allow_files = True),
"shared_lib_name": attr.string(),
Expand All @@ -858,7 +881,7 @@ cc_shared_library = rule(
fragments = ["cpp"] + semantics.additional_fragments(),
)

def cc_shared_library_initializer(**kwargs):
def dynamic_deps_initializer(**kwargs):
"""Initializes dynamic_deps_attrs"""
if "dynamic_deps" in kwargs and cc_helper.is_non_empty_list_or_select(kwargs["dynamic_deps"], "dynamic_deps"):
# Propagate an aspect if dynamic_deps attribute is specified.
Expand Down
4 changes: 2 additions & 2 deletions src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

load(":common/cc/cc_binary.bzl", "cc_binary_impl")
load(":common/cc/cc_binary_attrs.bzl", "cc_binary_attrs")
load(":common/cc/cc_shared_library.bzl", "cc_shared_library_initializer")
load(":common/cc/cc_helper.bzl", "cc_helper")
load(":common/cc/cc_shared_library.bzl", "dynamic_deps_initializer")
load(":common/cc/semantics.bzl", "semantics")
load(":common/paths.bzl", "paths")

Expand Down Expand Up @@ -100,7 +100,7 @@ _cc_test_attrs.update(semantics.get_test_malloc_attr())
_cc_test_attrs.update(semantics.get_coverage_attrs())

cc_test = rule(
initializer = cc_shared_library_initializer,
initializer = dynamic_deps_initializer,
implementation = _impl,
attrs = _cc_test_attrs,
outputs = {
Expand Down
3 changes: 3 additions & 0 deletions src/main/starlark/tests/builtins_bzl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ sh_test(
"@rules_testing//lib:truth_bzl",
"@rules_testing//lib:util_bzl",
],
tags = [
"requires-network", # for Bzlmod
],
)

sh_library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ cc_shared_library(
"bar",
"bar2",
] + select({
":is_bazel": ["@test_repo//:bar"],
":is_bazel": ["@my_test_repo//:bar"],
"//conditions:default": [],
}),
)
Expand All @@ -296,7 +296,7 @@ cc_library(
srcs = ["barX.cc"],
hdrs = ["barX.h"],
deps = select({
":is_bazel": ["@test_repo//:bar"],
":is_bazel": ["@my_test_repo//:bar"],
"//conditions:default": [],
}),
)
Expand Down Expand Up @@ -473,6 +473,22 @@ cc_library(
hdrs = [":hdr_only_hdr"],
)

cc_library(
name = "external_export",
deps = select({
":is_bazel": ["@my_test_repo//:bar"],
"//conditions:default": [],
}),
)

cc_shared_library(
name = "external_export_so",
exports_filter = ["@my_test_repo//:__pkg__"],
deps = [
":external_export",
],
)

build_failure_test(
name = "two_dynamic_deps_same_export_in_so_test",
message = "Two shared libraries in dependencies export the same symbols",
Expand Down Expand Up @@ -536,6 +552,16 @@ exports_test(
],
)

exports_test(
name = "external_export_exports_test",
target = "external_export_so",
bazel_only = True,
targets_that_should_be_claimed_to_be_exported = [
"@@test_repo~//:bar",
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:external_export",
],
)

pdb_test(
name = "pdb_test",
target = ":foo_so",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,25 +351,36 @@ nocode_cc_lib = rule(
)

def _exports_test_impl(env, target):
if not env.ctx.attr.is_bazel and env.ctx.attr._bazel_only:
return

actual = list(target[CcSharedLibraryInfo].exports)

# Remove the @@ prefix on Bazel
# Remove the @@ prefix for main repo labels on Bazel
for i in range(len(actual)):
if actual[i].startswith("@@"):
if actual[i].startswith("@@//"):
actual[i] = actual[i][2:]
expected = env.ctx.attr._targets_that_should_be_claimed_to_be_exported
env.expect.where(
detail = "Exports lists do not match.",
).that_collection(actual).contains_exactly(expected).in_order()

def _exports_test_macro(name, target, targets_that_should_be_claimed_to_be_exported):
def _exports_test_macro(name, target, targets_that_should_be_claimed_to_be_exported, bazel_only = False):
analysis_test(
name = name,
impl = _exports_test_impl,
target = target,
attrs = {
"is_bazel": attr.bool(),
"_bazel_only": attr.bool(default = bazel_only),
"_targets_that_should_be_claimed_to_be_exported": attr.string_list(default = targets_that_should_be_claimed_to_be_exported),
},
attr_values = {
"is_bazel": select({
":is_bazel": True,
"//conditions:default": False,
}),
},
)

exports_test = _exports_test_macro
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module(name = "test_repo")
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
workspace(name = "test_repo")
7 changes: 4 additions & 3 deletions src/main/starlark/tests/builtins_bzl/cc_builtin_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ function test_starlark_cc() {
mkdir -p "src/conditions"
cp "$(rlocation "io_bazel/src/conditions/BUILD")" "src/conditions/BUILD"

cat >> WORKSPACE<<EOF
local_repository(
name = "test_repo",
cat >> MODULE.bazel<<EOF
bazel_dep(name = "test_repo", repo_name = "my_test_repo")
local_path_override(
module_name = "test_repo",
path = "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2",
)
EOF
Expand Down