Skip to content

Commit

Permalink
Remove use of rule.name from cc_test
Browse files Browse the repository at this point in the history
The pair (resolved .bzl label, rule class name) ought to uniquely identify a starlark-defined rule class.

Related: #16301
PiperOrigin-RevId: 475752728
Change-Id: Ie8698f34aa2c27e624276f36b7f4965be637dde9
  • Loading branch information
comius authored and copybara-github committed Sep 21, 2022
1 parent 36afffa commit 2cde45e
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 39 deletions.
50 changes: 12 additions & 38 deletions src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ def _impl(ctx):
providers.extend(test_providers)
return _handle_legacy_return(ctx, cc_info, providers)

def _make_cc_test(with_linkstatic = False, with_aspects = False):
def make_cc_test(with_linkstatic = False, with_aspects = False):
"""Makes one of the cc_test rule variants.
This function shall only be used internally in CC ruleset.
Args:
with_linkstatic: sets value _linkstatic_explicitly_set attribute
with_aspects: Attaches graph_structure_aspect to `deps` attribute and
implicit deps.
Returns:
A cc_test rule class.
"""
_cc_test_attrs = None
if with_aspects:
_cc_test_attrs = dict(cc_binary_attrs_with_aspects)
Expand Down Expand Up @@ -119,7 +130,6 @@ def _make_cc_test(with_linkstatic = False, with_aspects = False):
_linkstatic_explicitly_set = attr.bool(default = with_linkstatic),
)
return rule(
name = "cc_test",
implementation = _impl,
attrs = _cc_test_attrs,
outputs = {
Expand All @@ -135,39 +145,3 @@ def _make_cc_test(with_linkstatic = False, with_aspects = False):
incompatible_use_toolchain_transition = True,
test = True,
)

_cc_test_variants = struct(
with_aspects = struct(
explicit_linkstatic = _make_cc_test(with_linkstatic = True, with_aspects = True),
default_linkstatic = _make_cc_test(with_aspects = True),
),
without_aspects = struct(
explicit_linkstatic = _make_cc_test(with_linkstatic = True),
default_linkstatic = _make_cc_test(),
),
)

def cc_test_wrapper(**kwargs):
"""Entry point for cc_test rules.
This avoids propagating aspects on certain attributes if dynamic_deps attribute is unset.
It also serves to detect if the `linkstatic` attribute was explicitly set or not.
This is to workaround a deficiency in Starlark attributes.
(See: https://github.com/bazelbuild/bazel/issues/14434)
Args:
**kwargs: Arguments suitable for cc_test.
"""
cc_test_aspects = None

# Propagate an aspect if dynamic_deps attribute is specified.
if "dynamic_deps" in kwargs and cc_helper.is_non_empty_list_or_select(kwargs["dynamic_deps"], "dynamic_deps"):
cc_test_aspects = _cc_test_variants.with_aspects
else:
cc_test_aspects = _cc_test_variants.without_aspects

if "linkstatic" in kwargs:
cc_test_aspects.explicit_linkstatic(**kwargs)
else:
cc_test_aspects.default_linkstatic(**kwargs)
19 changes: 19 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""cc_test Starlark implementation."""

load(":common/cc/cc_test.bzl", "make_cc_test")

cc_test = make_cc_test()
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""cc_test Starlark implementation."""

load(":common/cc/cc_test.bzl", "make_cc_test")

cc_test = make_cc_test(with_aspects = True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""cc_test Starlark implementation."""

load(":common/cc/cc_test.bzl", "make_cc_test")

cc_test = make_cc_test(with_linkstatic = True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""cc_test Starlark implementation."""

load(":common/cc/cc_test.bzl", "make_cc_test")

cc_test = make_cc_test(with_linkstatic = True, with_aspects = True)
45 changes: 45 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""cc_test Starlark implementation."""

load(":common/cc/cc_helper.bzl", "cc_helper")
load(":common/cc/cc_test_no_linkstatic.bzl", _cc_test_no_linkstatic = "cc_test")
load(":common/cc/cc_test_with_linkstatic.bzl", _cc_test_with_linkstatic = "cc_test")
load(":common/cc/cc_test_no_linkstatic_aspects.bzl", _cc_test_no_linkstatic_aspects = "cc_test")
load(":common/cc/cc_test_with_linkstatic_aspects.bzl", _cc_test_with_linkstatic_aspects = "cc_test")

def cc_test_wrapper(**kwargs):
"""Entry point for cc_test rules.
This avoids propagating aspects on certain attributes if dynamic_deps attribute is unset.
It also serves to detect if the `linkstatic` attribute was explicitly set or not.
This is to workaround a deficiency in Starlark attributes.
(See: https://github.com/bazelbuild/bazel/issues/14434)
Args:
**kwargs: Arguments suitable for cc_test.
"""

# Propagate an aspect if dynamic_deps attribute is specified.
if "dynamic_deps" in kwargs and cc_helper.is_non_empty_list_or_select(kwargs["dynamic_deps"], "dynamic_deps"):
if "linkstatic" in kwargs:
_cc_test_with_linkstatic_aspects(**kwargs)
else:
_cc_test_no_linkstatic_aspects(**kwargs)
elif "linkstatic" in kwargs:
_cc_test_with_linkstatic(**kwargs)
else:
_cc_test_no_linkstatic(**kwargs)
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/exports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

load("@_builtins//:common/cc/cc_import.bzl", "cc_import")
load("@_builtins//:common/cc/cc_binary_wrapper.bzl", "cc_binary")
load("@_builtins//:common/cc/cc_test.bzl", cc_test = "cc_test_wrapper")
load("@_builtins//:common/cc/cc_test_wrapper.bzl", cc_test = "cc_test_wrapper")
load("@_builtins//:common/cc/experimental_cc_shared_library.bzl", "CcSharedLibraryInfo", "cc_shared_library", "cc_shared_library_permissions")
load("@_builtins//:common/objc/objc_import.bzl", "objc_import")
load("@_builtins//:common/objc/objc_library.bzl", "objc_library")
Expand Down

0 comments on commit 2cde45e

Please sign in to comment.