xref: /aosp_15_r20/external/bazelbuild-rules_cc/cc/toolchains/feature_constraint.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1# Copyright 2024 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"""Implementation of the cc_feature_constraint rule."""
15
16load(
17    "//cc/toolchains/impl:collect.bzl",
18    "collect_features",
19    "collect_provider",
20)
21load(
22    ":cc_toolchain_info.bzl",
23    "FeatureConstraintInfo",
24    "FeatureSetInfo",
25)
26
27def _cc_feature_constraint_impl(ctx):
28    if ctx.attr.features:
29        fail("Features is a reserved attribute in bazel. Use the attributes `all_of` and `none_of` for feature constraints")
30    all_of = collect_provider(ctx.attr.all_of, FeatureConstraintInfo)
31    none_of = [collect_features(ctx.attr.none_of)]
32    none_of.extend([fc.none_of for fc in all_of])
33    return [FeatureConstraintInfo(
34        label = ctx.label,
35        all_of = depset(transitive = [fc.all_of for fc in all_of]),
36        none_of = depset(transitive = none_of),
37    )]
38
39cc_feature_constraint = rule(
40    implementation = _cc_feature_constraint_impl,
41    attrs = {
42        "all_of": attr.label_list(
43            providers = [FeatureConstraintInfo],
44        ),
45        "none_of": attr.label_list(
46            providers = [FeatureSetInfo],
47        ),
48    },
49    provides = [FeatureConstraintInfo],
50    doc = """Defines a constraint on features.
51
52Can be used with require_any_of to specify that something is only enabled when
53a constraint is met.""",
54)
55