Skip to content

Commit

Permalink
Remove NO_EXPORTING tag from cc_shared_library
Browse files Browse the repository at this point in the history
The same behavior can be achieved via an indirect cc_library (i.e. not placed
in cc_shared_library.deps) that is LINKABLE_MORE_THAN_ONCE

RELNOTES:none
PiperOrigin-RevId: 514727112
Change-Id: Ic5053f7b534d3bd69b4c61639b299936dc9990eb
  • Loading branch information
oquenchil authored and Copybara-Service committed Mar 7, 2023
1 parent f785f28 commit e79de51
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,12 @@ load(":common/cc/cc_common.bzl", "cc_common")
# used sparingly after making sure it's safe to use.
LINKABLE_MORE_THAN_ONCE = "LINKABLE_MORE_THAN_ONCE"

# Add this as a tag to any static lib target that doesn't export any symbols,
# thus can be statically linked more than once. This is useful in some cases,
# for example, a static lib has a constructor that needs to be run during
# loading time of the shared lib that has it linked into, which is how the
# code gets called by the OS. This static lib might need to be linked as a
# whole archive dep for multiple shared libs, otherwise this static lib will
# be dropped by the linker since there are no incoming symbol references.
NO_EXPORTING = "NO_EXPORTING"

GraphNodeInfo = provider(
"Nodes in the graph of shared libraries.",
fields = {
"children": "Other GraphNodeInfo from dependencies of this target",
"label": "Label of the target visited",
"linkable_more_than_once": "Linkable into more than a single cc_shared_library",
"no_exporting": "The static lib doesn't export any symbols so don't export it",
},
)
CcSharedLibraryInfo = provider(
Expand Down Expand Up @@ -504,12 +494,7 @@ def _cc_shared_library_impl(ctx):
runfiles = runfiles.merge(ctx.runfiles(files = precompiled_only_dynamic_libraries_runfiles))

for export in deps:
export_label = str(export.label)
if GraphNodeInfo in export and export[GraphNodeInfo].no_exporting:
if export_label in curr_link_once_static_libs_set:
curr_link_once_static_libs_set.remove(export_label)
continue
exports[export_label] = True
exports[str(export.label)] = True

debug_files = []
exports_debug_file = ctx.actions.declare_file(ctx.label.name + "_exports.txt")
Expand Down Expand Up @@ -577,19 +562,15 @@ def _graph_structure_aspect_impl(target, ctx):
# TODO(bazel-team): Add flag to Bazel that can toggle the initialization of
# linkable_more_than_once.
linkable_more_than_once = False
no_exporting = False
if hasattr(ctx.rule.attr, "tags"):
for tag in ctx.rule.attr.tags:
if tag == LINKABLE_MORE_THAN_ONCE:
linkable_more_than_once = True
elif tag == NO_EXPORTING:
no_exporting = True

return [GraphNodeInfo(
label = ctx.label,
children = children,
linkable_more_than_once = linkable_more_than_once,
no_exporting = no_exporting,
)]

graph_structure_aspect = aspect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ load(
"linking_suffix_test",
"paths_test",
"runfiles_test",
"no_exporting_static_lib_test",
"check_already_linked_inputs_are_not_passed_to_linking_action_test",
)

Expand Down Expand Up @@ -386,51 +385,6 @@ cc_library(
],
)

cc_library(
name = "static_lib_no_exporting",
srcs = [
"bar.cc",
"bar.h",
],
tags = ["NO_EXPORTING"],
)

cc_library(
name = "static_lib_exporting",
srcs = [
"bar2.cc",
"bar2.h",
],
)

cc_shared_library(
name = "lib_with_no_exporting_roots_1",
deps = [":static_lib_no_exporting"],
)

cc_shared_library(
name = "lib_with_no_exporting_roots_2",
deps = [":static_lib_no_exporting"],
dynamic_deps = [":lib_with_no_exporting_roots_3"],
)

cc_shared_library(
name = "lib_with_no_exporting_roots_3",
deps = [":static_lib_no_exporting"],
)

cc_shared_library(
name = "lib_with_no_exporting_roots",
deps = [
":static_lib_no_exporting",
":static_lib_exporting",
],
dynamic_deps = [
":lib_with_no_exporting_roots_1",
":lib_with_no_exporting_roots_2",
],
)

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 @@ -466,11 +420,6 @@ runfiles_test(
target_under_test = ":python_test",
)

no_exporting_static_lib_test(
name = "no_exporting_static_lib_test",
target_under_test = ":lib_with_no_exporting_roots",
)

check_already_linked_inputs_are_not_passed_to_linking_action_test(
name = "check_binary_doesnt_take_already_linked_in_libs",
target_under_test = ":binary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,3 @@ check_already_linked_inputs_are_not_passed_to_linking_action_test = analysistest
"libs_that_shouldnt_be_present": attr.string_list(),
},
)

def _no_exporting_static_lib_test_impl(ctx):
env = analysistest.begin(ctx)

target_under_test = analysistest.target_under_test(env)

# There should be only one exported file
actual_file = target_under_test[CcSharedLibraryInfo].exports[0]

# Sometimes "@" is prefixed in some test environments
expected = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:static_lib_exporting"
asserts.true(env, actual_file.endswith(expected))

return analysistest.end(env)

no_exporting_static_lib_test = analysistest.make(
_no_exporting_static_lib_test_impl,
)

0 comments on commit e79de51

Please sign in to comment.