Skip to content

Commit c62496f

Browse files
oquenchilShreeM01
andauthored
C++: Add compound error linked statically but not exported (bazelbuild#16113)
Co-authored-by: kshyanashree <[email protected]>
1 parent 83041b1 commit c62496f

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl

+20-2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def _filter_inputs(
267267
unaccounted_for_libs = []
268268
exports = {}
269269
owners_seen = {}
270+
linked_statically_but_not_exported = {}
270271
for linker_input in dependency_linker_inputs:
271272
owner = str(linker_input.owner)
272273
if owner in owners_seen:
@@ -277,8 +278,7 @@ def _filter_inputs(
277278
linker_inputs.append(dynamic_linker_input)
278279
elif owner in link_statically_labels:
279280
if owner in link_once_static_libs_map:
280-
fail(owner + " is already linked statically in " +
281-
link_once_static_libs_map[owner] + " but not exported")
281+
linked_statically_but_not_exported.setdefault(link_once_static_libs_map[owner], []).append(owner)
282282

283283
is_direct_export = owner in direct_exports
284284

@@ -337,9 +337,27 @@ def _filter_inputs(
337337
else:
338338
unaccounted_for_libs.append(linker_input.owner)
339339

340+
_throw_linked_but_not_exported_errors(linked_statically_but_not_exported)
340341
_throw_error_if_unaccounted_libs(unaccounted_for_libs)
341342
return (exports, linker_inputs, link_once_static_libs, precompiled_only_dynamic_libraries)
342343

344+
def _throw_linked_but_not_exported_errors(error_libs_dict):
345+
if not error_libs_dict:
346+
return
347+
348+
error_builder = ["The following libraries were linked statically by different cc_shared_libraries but not exported:\n"]
349+
for cc_shared_library_target, error_libs in error_libs_dict.items():
350+
error_builder.append("cc_shared_library %s:\n" % str(cc_shared_library_target))
351+
for error_lib in error_libs:
352+
error_builder.append(" \"%s\",\n" % str(error_lib))
353+
354+
error_builder.append("If you are sure that the previous libraries are exported by the cc_shared_libraries because:\n")
355+
error_builder.append(" 1. You have visibility declarations in the source code\n")
356+
error_builder.append(" 2. Or you are passing a visibility script to the linker to export symbols from them\n")
357+
error_builder.append("then add those libraries to roots or exports_filter for each cc_shared_library.\n")
358+
359+
fail("".join(error_builder))
360+
343361
def _throw_error_if_unaccounted_libs(unaccounted_for_libs):
344362
if not unaccounted_for_libs:
345363
return

src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test

+8
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ build_failure_test(
282282
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_binary",
283283
)
284284

285+
build_failure_test(
286+
name = "link_once_repeated_test_shared_lib",
287+
messages = [
288+
"cc_shared_library/test_cc_shared_library:barX\",",
289+
],
290+
target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_shared_lib",
291+
)
292+
285293
paths_test(
286294
name = "path_matching_test",
287295
)

src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ cc_binary(
1717
],
1818
)
1919

20+
cc_shared_library(
21+
name = "should_fail_shared_lib",
22+
dynamic_deps = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:bar_so"],
23+
roots = [
24+
":intermediate",
25+
],
26+
static_deps = [
27+
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:barX",
28+
],
29+
tags = TAGS,
30+
)
31+
32+
cc_library(
33+
name = "intermediate",
34+
deps = [
35+
"//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:barX",
36+
],
37+
)
38+
2039
cc_shared_library(
2140
name = "permissions_fail_so",
2241
roots = [

0 commit comments

Comments
 (0)