Skip to content

Commit 2cde45e

Browse files
comiuscopybara-github
authored andcommitted
Remove use of rule.name from cc_test
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
1 parent 36afffa commit 2cde45e

7 files changed

+134
-39
lines changed

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

+12-38
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,18 @@ def _impl(ctx):
8383
providers.extend(test_providers)
8484
return _handle_legacy_return(ctx, cc_info, providers)
8585

86-
def _make_cc_test(with_linkstatic = False, with_aspects = False):
86+
def make_cc_test(with_linkstatic = False, with_aspects = False):
87+
"""Makes one of the cc_test rule variants.
88+
89+
This function shall only be used internally in CC ruleset.
90+
91+
Args:
92+
with_linkstatic: sets value _linkstatic_explicitly_set attribute
93+
with_aspects: Attaches graph_structure_aspect to `deps` attribute and
94+
implicit deps.
95+
Returns:
96+
A cc_test rule class.
97+
"""
8798
_cc_test_attrs = None
8899
if with_aspects:
89100
_cc_test_attrs = dict(cc_binary_attrs_with_aspects)
@@ -119,7 +130,6 @@ def _make_cc_test(with_linkstatic = False, with_aspects = False):
119130
_linkstatic_explicitly_set = attr.bool(default = with_linkstatic),
120131
)
121132
return rule(
122-
name = "cc_test",
123133
implementation = _impl,
124134
attrs = _cc_test_attrs,
125135
outputs = {
@@ -135,39 +145,3 @@ def _make_cc_test(with_linkstatic = False, with_aspects = False):
135145
incompatible_use_toolchain_transition = True,
136146
test = True,
137147
)
138-
139-
_cc_test_variants = struct(
140-
with_aspects = struct(
141-
explicit_linkstatic = _make_cc_test(with_linkstatic = True, with_aspects = True),
142-
default_linkstatic = _make_cc_test(with_aspects = True),
143-
),
144-
without_aspects = struct(
145-
explicit_linkstatic = _make_cc_test(with_linkstatic = True),
146-
default_linkstatic = _make_cc_test(),
147-
),
148-
)
149-
150-
def cc_test_wrapper(**kwargs):
151-
"""Entry point for cc_test rules.
152-
153-
This avoids propagating aspects on certain attributes if dynamic_deps attribute is unset.
154-
155-
It also serves to detect if the `linkstatic` attribute was explicitly set or not.
156-
This is to workaround a deficiency in Starlark attributes.
157-
(See: https://github.com/bazelbuild/bazel/issues/14434)
158-
159-
Args:
160-
**kwargs: Arguments suitable for cc_test.
161-
"""
162-
cc_test_aspects = None
163-
164-
# Propagate an aspect if dynamic_deps attribute is specified.
165-
if "dynamic_deps" in kwargs and cc_helper.is_non_empty_list_or_select(kwargs["dynamic_deps"], "dynamic_deps"):
166-
cc_test_aspects = _cc_test_variants.with_aspects
167-
else:
168-
cc_test_aspects = _cc_test_variants.without_aspects
169-
170-
if "linkstatic" in kwargs:
171-
cc_test_aspects.explicit_linkstatic(**kwargs)
172-
else:
173-
cc_test_aspects.default_linkstatic(**kwargs)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""cc_test Starlark implementation."""
16+
17+
load(":common/cc/cc_test.bzl", "make_cc_test")
18+
19+
cc_test = make_cc_test()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""cc_test Starlark implementation."""
16+
17+
load(":common/cc/cc_test.bzl", "make_cc_test")
18+
19+
cc_test = make_cc_test(with_aspects = True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""cc_test Starlark implementation."""
16+
17+
load(":common/cc/cc_test.bzl", "make_cc_test")
18+
19+
cc_test = make_cc_test(with_linkstatic = True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""cc_test Starlark implementation."""
16+
17+
load(":common/cc/cc_test.bzl", "make_cc_test")
18+
19+
cc_test = make_cc_test(with_linkstatic = True, with_aspects = True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""cc_test Starlark implementation."""
16+
17+
load(":common/cc/cc_helper.bzl", "cc_helper")
18+
load(":common/cc/cc_test_no_linkstatic.bzl", _cc_test_no_linkstatic = "cc_test")
19+
load(":common/cc/cc_test_with_linkstatic.bzl", _cc_test_with_linkstatic = "cc_test")
20+
load(":common/cc/cc_test_no_linkstatic_aspects.bzl", _cc_test_no_linkstatic_aspects = "cc_test")
21+
load(":common/cc/cc_test_with_linkstatic_aspects.bzl", _cc_test_with_linkstatic_aspects = "cc_test")
22+
23+
def cc_test_wrapper(**kwargs):
24+
"""Entry point for cc_test rules.
25+
26+
This avoids propagating aspects on certain attributes if dynamic_deps attribute is unset.
27+
28+
It also serves to detect if the `linkstatic` attribute was explicitly set or not.
29+
This is to workaround a deficiency in Starlark attributes.
30+
(See: https://github.com/bazelbuild/bazel/issues/14434)
31+
32+
Args:
33+
**kwargs: Arguments suitable for cc_test.
34+
"""
35+
36+
# Propagate an aspect if dynamic_deps attribute is specified.
37+
if "dynamic_deps" in kwargs and cc_helper.is_non_empty_list_or_select(kwargs["dynamic_deps"], "dynamic_deps"):
38+
if "linkstatic" in kwargs:
39+
_cc_test_with_linkstatic_aspects(**kwargs)
40+
else:
41+
_cc_test_no_linkstatic_aspects(**kwargs)
42+
elif "linkstatic" in kwargs:
43+
_cc_test_with_linkstatic(**kwargs)
44+
else:
45+
_cc_test_no_linkstatic(**kwargs)

src/main/starlark/builtins_bzl/common/exports.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
load("@_builtins//:common/cc/cc_import.bzl", "cc_import")
1818
load("@_builtins//:common/cc/cc_binary_wrapper.bzl", "cc_binary")
19-
load("@_builtins//:common/cc/cc_test.bzl", cc_test = "cc_test_wrapper")
19+
load("@_builtins//:common/cc/cc_test_wrapper.bzl", cc_test = "cc_test_wrapper")
2020
load("@_builtins//:common/cc/experimental_cc_shared_library.bzl", "CcSharedLibraryInfo", "cc_shared_library", "cc_shared_library_permissions")
2121
load("@_builtins//:common/objc/objc_import.bzl", "objc_import")
2222
load("@_builtins//:common/objc/objc_library.bzl", "objc_library")

0 commit comments

Comments
 (0)