Add flags to cc_shared_library for easier debugging

--//examples:incompatible_link_once=False[default = False]
When True, it will be an error to link the same library more than once
unless it has the tag LINKABLE_MORE_THAN_ONCE

--//examples:experimental_debug=True[default = False]
When True, it will generate files listing the exports of each cc_shared_library
and which libraries are linked to it statically.

RELNOTES:none
PiperOrigin-RevId: 311323625
Change-Id: I340cc71965650f7c9dd7ef7fb9656da362021527
This commit is contained in:
Googler 2020-05-13 07:23:47 -07:00 committed by Copybara-Service
parent 8c31dd406c
commit 818289e561
3 changed files with 34 additions and 1 deletions

View File

@ -53,8 +53,10 @@ tasks:
- "//examples/test_cc_shared_library/..." - "//examples/test_cc_shared_library/..."
build_flags: build_flags:
- "--experimental_cc_shared_library" - "--experimental_cc_shared_library"
- "--//examples:incompatible_link_once=True"
test_flags: test_flags:
- "--test_timeout=120" - "--test_timeout=120"
- "--experimental_cc_shared_library" - "--experimental_cc_shared_library"
- "--//examples:incompatible_link_once=True"
test_targets: test_targets:
- "//examples/test_cc_shared_library/..." - "//examples/test_cc_shared_library/..."

View File

@ -12,5 +12,19 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
# A collection of examples showing the usage of rules_cc # A collection of examples showing the usage of rules_cc
licenses(["notice"]) licenses(["notice"])
bool_flag(
name = "incompatible_link_once",
build_setting_default = False,
visibility = ["//visibility:public"],
)
bool_flag(
name = "experimental_debug",
build_setting_default = False,
visibility = ["//visibility:public"],
)

View File

@ -5,6 +5,7 @@ rely on this. It requires bazel >1.2 and passing the flag
--experimental_cc_shared_library --experimental_cc_shared_library
""" """
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("//cc:find_cc_toolchain.bzl", "find_cc_toolchain") load("//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
# TODO(#5200): Add export_define to library_to_link and cc_library # TODO(#5200): Add export_define to library_to_link and cc_library
@ -363,9 +364,23 @@ def _cc_shared_library_impl(ctx):
for export in ctx.attr.roots: for export in ctx.attr.roots:
exports[str(export.label)] = True exports[str(export.label)] = True
debug_files = []
if ctx.attr._experimental_debug[BuildSettingInfo].value:
exports_debug_file = ctx.actions.declare_file(ctx.label.name + "_exports.txt")
ctx.actions.write(content = "\n".join(exports.keys()), output = exports_debug_file)
link_once_static_libs_debug_file = ctx.actions.declare_file(ctx.label.name + "_link_once_static_libs.txt")
ctx.actions.write(content = "\n".join(link_once_static_libs), output = link_once_static_libs_debug_file)
debug_files.append(exports_debug_file)
debug_files.append(link_once_static_libs_debug_file)
if not ctx.attr._incompatible_link_once[BuildSettingInfo].value:
link_once_static_libs = []
return [ return [
DefaultInfo( DefaultInfo(
files = depset([linking_outputs.library_to_link.resolved_symlink_dynamic_library]), files = depset([linking_outputs.library_to_link.resolved_symlink_dynamic_library] + debug_files),
runfiles = runfiles, runfiles = runfiles,
), ),
CcSharedLibraryInfo( CcSharedLibraryInfo(
@ -439,6 +454,8 @@ cc_shared_library = rule(
"static_deps": attr.string_list(), "static_deps": attr.string_list(),
"user_link_flags": attr.string_list(), "user_link_flags": attr.string_list(),
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"), "_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
"_experimental_debug": attr.label(default = "//examples:experimental_debug"),
"_incompatible_link_once": attr.label(default = "//examples:incompatible_link_once"),
}, },
toolchains = ["@rules_cc//cc:toolchain_type"], # copybara-use-repo-external-label toolchains = ["@rules_cc//cc:toolchain_type"], # copybara-use-repo-external-label
fragments = ["cpp"], fragments = ["cpp"],