C++: Add compound error linked statically but not exported (#16113)
Co-authored-by: kshyanashree <109167932+kshyanashree@users.noreply.github.com>
diff --git a/src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl b/src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl
index 5a605d1..176e434 100644
--- a/src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl
@@ -267,6 +267,7 @@
unaccounted_for_libs = []
exports = {}
owners_seen = {}
+ linked_statically_but_not_exported = {}
for linker_input in dependency_linker_inputs:
owner = str(linker_input.owner)
if owner in owners_seen:
@@ -277,8 +278,7 @@
linker_inputs.append(dynamic_linker_input)
elif owner in link_statically_labels:
if owner in link_once_static_libs_map:
- fail(owner + " is already linked statically in " +
- link_once_static_libs_map[owner] + " but not exported")
+ linked_statically_but_not_exported.setdefault(link_once_static_libs_map[owner], []).append(owner)
is_direct_export = owner in direct_exports
@@ -337,9 +337,27 @@
else:
unaccounted_for_libs.append(linker_input.owner)
+ _throw_linked_but_not_exported_errors(linked_statically_but_not_exported)
_throw_error_if_unaccounted_libs(unaccounted_for_libs)
return (exports, linker_inputs, link_once_static_libs, precompiled_only_dynamic_libraries)
+def _throw_linked_but_not_exported_errors(error_libs_dict):
+ if not error_libs_dict:
+ return
+
+ error_builder = ["The following libraries were linked statically by different cc_shared_libraries but not exported:\n"]
+ for cc_shared_library_target, error_libs in error_libs_dict.items():
+ error_builder.append("cc_shared_library %s:\n" % str(cc_shared_library_target))
+ for error_lib in error_libs:
+ error_builder.append(" \"%s\",\n" % str(error_lib))
+
+ error_builder.append("If you are sure that the previous libraries are exported by the cc_shared_libraries because:\n")
+ error_builder.append(" 1. You have visibility declarations in the source code\n")
+ error_builder.append(" 2. Or you are passing a visibility script to the linker to export symbols from them\n")
+ error_builder.append("then add those libraries to roots or exports_filter for each cc_shared_library.\n")
+
+ fail("".join(error_builder))
+
def _throw_error_if_unaccounted_libs(unaccounted_for_libs):
if not unaccounted_for_libs:
return
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 3fe911d..6d43896 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
@@ -282,6 +282,14 @@
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_binary",
)
+build_failure_test(
+ name = "link_once_repeated_test_shared_lib",
+ messages = [
+ "cc_shared_library/test_cc_shared_library:barX\",",
+ ],
+ target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_shared_lib",
+)
+
paths_test(
name = "path_matching_test",
)
diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test
index 72354b7..dafa066 100644
--- a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test
+++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test
@@ -18,6 +18,25 @@
)
cc_shared_library(
+ name = "should_fail_shared_lib",
+ dynamic_deps = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:bar_so"],
+ roots = [
+ ":intermediate",
+ ],
+ static_deps = [
+ "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:barX",
+ ],
+ tags = TAGS,
+)
+
+cc_library(
+ name = "intermediate",
+ deps = [
+ "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:barX",
+ ],
+)
+
+cc_shared_library(
name = "permissions_fail_so",
roots = [
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:bar",