From 2f5e877589cc764b1122421b3bba1d826c2b9fc8 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 21 Sep 2022 00:26:53 -0700 Subject: [PATCH] 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: https://github.com/bazelbuild/bazel/issues/16301 PiperOrigin-RevId: 475752728 Change-Id: Ie8698f34aa2c27e624276f36b7f4965be637dde9 --- .../builtins_bzl/common/cc/cc_test.bzl | 50 +++++-------------- .../common/cc/cc_test_no_linkstatic.bzl | 19 +++++++ .../cc/cc_test_no_linkstatic_aspects.bzl | 19 +++++++ .../common/cc/cc_test_with_linkstatic.bzl | 19 +++++++ .../cc/cc_test_with_linkstatic_aspects.bzl | 19 +++++++ .../common/cc/cc_test_wrapper.bzl | 45 +++++++++++++++++ .../starlark/builtins_bzl/common/exports.bzl | 2 +- 7 files changed, 134 insertions(+), 39 deletions(-) create mode 100644 src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic.bzl create mode 100644 src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic_aspects.bzl create mode 100644 src/main/starlark/builtins_bzl/common/cc/cc_test_with_linkstatic.bzl create mode 100644 src/main/starlark/builtins_bzl/common/cc/cc_test_with_linkstatic_aspects.bzl create mode 100644 src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl index 4a9ae6e57a9545..f35caccd4c87e0 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl @@ -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) @@ -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 = { @@ -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) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic.bzl new file mode 100644 index 00000000000000..bf979c873ee236 --- /dev/null +++ b/src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic.bzl @@ -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() diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic_aspects.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic_aspects.bzl new file mode 100644 index 00000000000000..074519bd6d3ab0 --- /dev/null +++ b/src/main/starlark/builtins_bzl/common/cc/cc_test_no_linkstatic_aspects.bzl @@ -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) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test_with_linkstatic.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test_with_linkstatic.bzl new file mode 100644 index 00000000000000..379927070607d0 --- /dev/null +++ b/src/main/starlark/builtins_bzl/common/cc/cc_test_with_linkstatic.bzl @@ -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) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test_with_linkstatic_aspects.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test_with_linkstatic_aspects.bzl new file mode 100644 index 00000000000000..172c040250e567 --- /dev/null +++ b/src/main/starlark/builtins_bzl/common/cc/cc_test_with_linkstatic_aspects.bzl @@ -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) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl new file mode 100644 index 00000000000000..a708f33a93b495 --- /dev/null +++ b/src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl @@ -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) diff --git a/src/main/starlark/builtins_bzl/common/exports.bzl b/src/main/starlark/builtins_bzl/common/exports.bzl index b795decc4a192f..84bd4fba9183f9 100755 --- a/src/main/starlark/builtins_bzl/common/exports.bzl +++ b/src/main/starlark/builtins_bzl/common/exports.bzl @@ -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")