Change the syntax for labels in static_deps and exported_by

The syntax now matches visibility's. This is less confusing than
before, we now have:
 - targets like //foo, //foo:foo or //foo:bar
 - same package match like //foo:__pkg__
 - subpackages like //foo:__subpackages__

RELNOTES:none
PiperOrigin-RevId: 298598757
Change-Id: I6d02cf03c3c67d78998dda27a6529d53d0ef2708
This commit is contained in:
Googler 2020-03-03 07:11:22 -08:00 committed by Copybara-Service
parent d4357efea4
commit 20bff9088b
3 changed files with 40 additions and 19 deletions

View File

@ -143,21 +143,15 @@ def _wrap_static_library_with_alwayslink(ctx, feature_configuration, cc_toolchai
additional_inputs = depset(direct = linker_input.additional_inputs),
)
def _check_if_target_under_path(path, target, target_specified):
if not _same_package_or_above(path, target):
def _check_if_target_under_path(value, pattern):
if pattern.workspace_name != value.workspace_name:
return False
if target_specified:
return path.name == target.name
return True
if pattern.name == "__pkg__":
return pattern.package == value.package
if pattern.name == "__subpackages__":
return _same_package_or_above(pattern, value)
def _is_target_specified(path):
if path.startswith("//") or path.startswith("@"):
if path.find(":") != -1:
return True
else:
return False
else:
return True
return pattern.package == value.package and pattern.name == value.name
def _filter_inputs(
ctx,
@ -218,10 +212,9 @@ def _filter_inputs(
can_be_linked_statically = False
for static_dep_path in ctx.attr.static_deps:
target_specified = _is_target_specified(static_dep_path)
static_dep_path_label = ctx.label.relative(static_dep_path)
owner_label = linker_input.owner
if _check_if_target_under_path(linker_input.owner, static_dep_path_label, target_specified):
if _check_if_target_under_path(linker_input.owner, static_dep_path_label):
can_be_linked_statically = True
break
if can_be_linked_statically:
@ -267,9 +260,8 @@ def _cc_shared_library_impl(ctx):
if not can_be_exported:
for exported_by in export[GraphNodeInfo].exported_by:
target_specified = _is_target_specified(exported_by)
exported_by_label = Label(exported_by)
if _check_if_target_under_path(ctx.label, exported_by_label, target_specified):
if _check_if_target_under_path(ctx.label, exported_by_label):
can_be_exported = True
break
if not can_be_exported:
@ -393,3 +385,5 @@ cc_shared_library = rule(
toolchains = ["@rules_cc//cc:toolchain_type"], # copybara-use-repo-external-label
fragments = ["cpp"],
)
for_testing_dont_use_check_if_target_under_path = _check_if_target_under_path

View File

@ -1,6 +1,6 @@
load("//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("//examples:experimental_cc_shared_library.bzl", "LINKABLE_MORE_THAN_ONCE", "cc_shared_library")
load(":starlark_tests.bzl", "additional_inputs_test", "link_once_repeated_test", "linking_suffix_test")
load(":starlark_tests.bzl", "additional_inputs_test", "link_once_repeated_test", "linking_suffix_test", "paths_test")
package(
default_visibility = ["//examples/test_cc_shared_library:__subpackages__"],
@ -178,3 +178,7 @@ link_once_repeated_test(
name = "link_once_repeated_test",
target_under_test = "//examples/test_cc_shared_library/failing_targets:should_fail_binary",
)
paths_test(
name = "path_matching_test",
)

View File

@ -1,6 +1,7 @@
"""Starlark tests for cc_shared_library"""
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "unittest")
load("//examples:experimental_cc_shared_library.bzl", "for_testing_dont_use_check_if_target_under_path")
def _linking_suffix_test_impl(ctx):
env = analysistest.begin(ctx)
@ -43,3 +44,25 @@ def _link_once_repeated_test_impl(ctx):
return analysistest.end(env)
link_once_repeated_test = analysistest.make(_link_once_repeated_test_impl, expect_failure = True)
def _paths_test_impl(ctx):
env = unittest.begin(ctx)
asserts.false(env, for_testing_dont_use_check_if_target_under_path(Label("//foo"), Label("//bar")))
asserts.false(env, for_testing_dont_use_check_if_target_under_path(Label("@foo//foo"), Label("@bar//bar")))
asserts.false(env, for_testing_dont_use_check_if_target_under_path(Label("//bar"), Label("@foo//bar")))
asserts.true(env, for_testing_dont_use_check_if_target_under_path(Label("@foo//bar"), Label("@foo//bar")))
asserts.true(env, for_testing_dont_use_check_if_target_under_path(Label("@foo//bar:bar"), Label("@foo//bar")))
asserts.true(env, for_testing_dont_use_check_if_target_under_path(Label("//bar:bar"), Label("//bar")))
asserts.false(env, for_testing_dont_use_check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar")))
asserts.false(env, for_testing_dont_use_check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar:__pkg__")))
asserts.true(env, for_testing_dont_use_check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar:__subpackages__")))
asserts.true(env, for_testing_dont_use_check_if_target_under_path(Label("@foo//bar:qux"), Label("@foo//bar:__pkg__")))
asserts.false(env, for_testing_dont_use_check_if_target_under_path(Label("@foo//bar"), Label("@foo//bar/baz:__subpackages__")))
asserts.false(env, for_testing_dont_use_check_if_target_under_path(Label("//bar"), Label("//bar/baz:__pkg__")))
return unittest.end(env)
paths_test = unittest.make(_paths_test_impl)