From e9bceb55ff5ad71524aa0b777b4c37e997b9b9eb Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 31 Oct 2023 01:55:22 -0700 Subject: [PATCH] Fix cc_shared_library error with hdr-only libraries cc_libraries with a linker_input with no libraries (either because they contain only headers or only linkopts) caused errors in cc_shared_libraries triggered by a check that made sure that a cc_library wasn't linked more than once into different shared libraries. That check can be ignored in those cases. Fixes #19920 RELNOTES:none PiperOrigin-RevId: 578101246 Change-Id: I61ff349411b51ea01314f3782d79b2f540a63856 --- .../builtins_bzl/common/cc/cc_shared_library.bzl | 5 +++++ .../test_cc_shared_library/BUILD.builtin_test | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) 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 65e1e004c04214..a6405207165c93 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 @@ -454,6 +454,11 @@ def _filter_inputs( _add_linker_input_to_dict(linker_input.owner, transitive_exports[owner]) linker_inputs_count += 1 elif owner in targets_to_be_linked_statically_map: + if not linker_input.libraries: + # TODO(bazel-team): semantics.should_create_empty_archive() should be + # cleaned up and return False in every case. cc_libraries shouldn't + # produce empty archives. For now issue #19920 is only fixed in Bazel. + continue if owner in link_once_static_libs_map: # We are building a dictionary that will allow us to give # proper errors for libraries that have been linked multiple 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 c60473a3c432e3..dd99fbbfd35b77 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 @@ -156,7 +156,7 @@ cc_library( "//conditions:default": [], }), deps = select({ - ":is_bazel": ["qux2"], + ":is_bazel": ["qux2", "hdr_only"], "//conditions:default": [], }) + [ "bar", @@ -298,7 +298,7 @@ cc_library( deps = [ "barX", ] + select({ - ":is_bazel": ["qux2"], + ":is_bazel": ["qux2", "hdr_only"], "//conditions:default": [], }), ) @@ -449,6 +449,17 @@ cc_library( srcs = [":private_cc_library.cc"] ) +genrule( + name = "hdr_only_hdr", + outs = ["hdr_only_hdr.h"], + cmd = "touch $@", +) + +cc_library( + name = "hdr_only", + hdrs = [":hdr_only_hdr"], +) + build_failure_test( name = "two_dynamic_deps_same_export_in_so_test", message = "Two shared libraries in dependencies export the same symbols",