From bc33ac2d4f0ce39524ca88833aaf26427a3d7e00 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 20 Jun 2023 05:16:48 -0700 Subject: [PATCH] Fix exports filter in cc_shared_library The indirect top level targets were being ignored by exports filter. RELNOTES:none PiperOrigin-RevId: 541882070 Change-Id: I9d2751d8bf343335b0f69949b8fc7d7e7fed9e1b --- .../common/cc/cc_shared_library.bzl | 2 +- .../test_cc_shared_library/BUILD.builtin_test | 18 ++++++++++++++ .../test_cc_shared_library/starlark_tests.bzl | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl index 1876ab57fc5355..485f2261287fb5 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl @@ -370,7 +370,7 @@ def _filter_inputs( cc_toolchain, linker_input, ) - elif _check_if_target_should_be_exported_with_filter( + if _check_if_target_should_be_exported_with_filter( linker_input.owner, ctx.label, ctx.attr.exports_filter, diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test index 6ed490762f3bd0..1815d295e5983b 100644 --- a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test @@ -10,6 +10,7 @@ load( "forwarding_cc_lib", "nocode_cc_lib", "wrapped_cc_lib", + "exports_test", ) load("@rules_testing//lib:analysis_test.bzl", "analysis_test") @@ -117,6 +118,9 @@ cc_shared_library( "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:diff_pkg_so" ], features = ["windows_export_all_symbols"], + exports_filter = [ + ":indirect_dep2", + ], deps = [ "baz", "foo", @@ -472,3 +476,17 @@ build_failure_test( message = "Do not place libraries which only contain a precompiled dynamic library", target = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:failing_only_dynamic_lib", ) + +exports_test( + name = "exports_foo_test", + target = ":foo_so", + targets_that_should_be_claimed_to_be_exported = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:indirect_dep2", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:baz", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:foo", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:cc_lib_with_no_srcs", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:nocode_cc_lib", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:should_not_be_linked_cc_lib", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:a_suffix", + ], +) diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl index 942a60f408b00f..8de5f54ed59e8d 100644 --- a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl @@ -331,3 +331,27 @@ nocode_cc_lib = rule( }, provides = [CcInfo], ) + +def _exports_test_impl(env, target): + actual = list(target[CcSharedLibraryInfo].exports) + + # Remove the @ prefix on Bazel + for i in range(len(actual)): + if actual[i][0] == "@": + actual[i] = actual[i][1:] + 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): + analysis_test( + name = name, + impl = _exports_test_impl, + target = target, + attrs = { + "_targets_that_should_be_claimed_to_be_exported": attr.string_list(default = targets_that_should_be_claimed_to_be_exported), + }, + ) + +exports_test = _exports_test_macro