From 991cdf09a92386d22de007280d72b0aca2fb4c5f Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 25 Mar 2024 23:43:33 -0700 Subject: [PATCH] Implement provides in rule based toolchain. BEGIN_PUBLIC Implement provides in rule based toolchain. END_PUBLIC PiperOrigin-RevId: 619082926 Change-Id: I938640981bd10f0e3d41402d211672a45264df1a --- cc/toolchains/mutually_exclusive_category.bzl | 29 +++++++++++++++++++ tests/rule_based_toolchain/features/BUILD | 10 ++++++- .../features/features_test.bzl | 14 +++++---- 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 cc/toolchains/mutually_exclusive_category.bzl diff --git a/cc/toolchains/mutually_exclusive_category.bzl b/cc/toolchains/mutually_exclusive_category.bzl new file mode 100644 index 0000000..9920290 --- /dev/null +++ b/cc/toolchains/mutually_exclusive_category.bzl @@ -0,0 +1,29 @@ +# Copyright 2024 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. +"""Rule for mutually exclusive categories in the rule based toolchain.""" + +load(":cc_toolchain_info.bzl", "MutuallyExclusiveCategoryInfo") + +def _cc_mutually_exclusive_category_impl(ctx): + return [MutuallyExclusiveCategoryInfo( + label = ctx.label, + name = str(ctx.label), + )] + +cc_mutually_exclusive_category = rule( + implementation = _cc_mutually_exclusive_category_impl, + doc = "A category of features, for which only one can be enabled", + attrs = {}, + provides = [MutuallyExclusiveCategoryInfo], +) diff --git a/tests/rule_based_toolchain/features/BUILD b/tests/rule_based_toolchain/features/BUILD index 95f67aa..cc3c0c7 100644 --- a/tests/rule_based_toolchain/features/BUILD +++ b/tests/rule_based_toolchain/features/BUILD @@ -3,6 +3,7 @@ load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint") load("//cc/toolchains:feature_set.bzl", "cc_feature_set") +load("//cc/toolchains:mutually_exclusive_category.bzl", "cc_mutually_exclusive_category") load("//cc/toolchains/impl:external_feature.bzl", "cc_external_feature") load("//tests/rule_based_toolchain:analysis_test_suite.bzl", "analysis_test_suite") load(":features_test.bzl", "TARGETS", "TESTS") @@ -59,13 +60,20 @@ util.helper_target( implies = [":simple"], ) +cc_mutually_exclusive_category( + name = "category", +) + util.helper_target( cc_feature, name = "mutual_exclusion_feature", args = [":c_compile"], enabled = True, feature_name = "mutual_exclusion", - mutually_exclusive = [":simple"], + mutually_exclusive = [ + ":simple", + ":category", + ], ) util.helper_target( diff --git a/tests/rule_based_toolchain/features/features_test.bzl b/tests/rule_based_toolchain/features/features_test.bzl index f0b15fd..2345cd7 100644 --- a/tests/rule_based_toolchain/features/features_test.bzl +++ b/tests/rule_based_toolchain/features/features_test.bzl @@ -89,6 +89,7 @@ def _feature_collects_mutual_exclusion_test(env, targets): FeatureInfo, ).mutually_exclusive().contains_exactly([ targets.simple.label, + targets.category.label, ]) def _feature_set_collects_features_test(env, targets): @@ -144,17 +145,18 @@ def _feature_can_be_overridden_test(env, targets): overrides.overrides().some().label().equals(targets.builtin_feature.label) TARGETS = [ + ":builtin_feature", ":c_compile", - ":simple", - ":simple2", + ":category", + ":direct_constraint", ":feature_set", - ":requires", ":implies", ":mutual_exclusion_feature", - ":direct_constraint", - ":transitive_constraint", - ":builtin_feature", ":overrides", + ":requires", + ":simple", + ":simple2", + ":transitive_constraint", ] # @unsorted-dict-items