xref: /aosp_15_r20/external/bazelbuild-rules_cc/cc/toolchains/feature_constraint.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1*eed53cd4SHONG Yifan# Copyright 2024 The Bazel Authors. All rights reserved.
2*eed53cd4SHONG Yifan#
3*eed53cd4SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License");
4*eed53cd4SHONG Yifan# you may not use this file except in compliance with the License.
5*eed53cd4SHONG Yifan# You may obtain a copy of the License at
6*eed53cd4SHONG Yifan#
7*eed53cd4SHONG Yifan#     http://www.apache.org/licenses/LICENSE-2.0
8*eed53cd4SHONG Yifan#
9*eed53cd4SHONG Yifan# Unless required by applicable law or agreed to in writing, software
10*eed53cd4SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS,
11*eed53cd4SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*eed53cd4SHONG Yifan# See the License for the specific language governing permissions and
13*eed53cd4SHONG Yifan# limitations under the License.
14*eed53cd4SHONG Yifan"""Implementation of the cc_feature_constraint rule."""
15*eed53cd4SHONG Yifan
16*eed53cd4SHONG Yifanload(
17*eed53cd4SHONG Yifan    "//cc/toolchains/impl:collect.bzl",
18*eed53cd4SHONG Yifan    "collect_features",
19*eed53cd4SHONG Yifan    "collect_provider",
20*eed53cd4SHONG Yifan)
21*eed53cd4SHONG Yifanload(
22*eed53cd4SHONG Yifan    ":cc_toolchain_info.bzl",
23*eed53cd4SHONG Yifan    "FeatureConstraintInfo",
24*eed53cd4SHONG Yifan    "FeatureSetInfo",
25*eed53cd4SHONG Yifan)
26*eed53cd4SHONG Yifan
27*eed53cd4SHONG Yifandef _cc_feature_constraint_impl(ctx):
28*eed53cd4SHONG Yifan    if ctx.attr.features:
29*eed53cd4SHONG Yifan        fail("Features is a reserved attribute in bazel. Use the attributes `all_of` and `none_of` for feature constraints")
30*eed53cd4SHONG Yifan    all_of = collect_provider(ctx.attr.all_of, FeatureConstraintInfo)
31*eed53cd4SHONG Yifan    none_of = [collect_features(ctx.attr.none_of)]
32*eed53cd4SHONG Yifan    none_of.extend([fc.none_of for fc in all_of])
33*eed53cd4SHONG Yifan    return [FeatureConstraintInfo(
34*eed53cd4SHONG Yifan        label = ctx.label,
35*eed53cd4SHONG Yifan        all_of = depset(transitive = [fc.all_of for fc in all_of]),
36*eed53cd4SHONG Yifan        none_of = depset(transitive = none_of),
37*eed53cd4SHONG Yifan    )]
38*eed53cd4SHONG Yifan
39*eed53cd4SHONG Yifancc_feature_constraint = rule(
40*eed53cd4SHONG Yifan    implementation = _cc_feature_constraint_impl,
41*eed53cd4SHONG Yifan    attrs = {
42*eed53cd4SHONG Yifan        "all_of": attr.label_list(
43*eed53cd4SHONG Yifan            providers = [FeatureConstraintInfo],
44*eed53cd4SHONG Yifan        ),
45*eed53cd4SHONG Yifan        "none_of": attr.label_list(
46*eed53cd4SHONG Yifan            providers = [FeatureSetInfo],
47*eed53cd4SHONG Yifan        ),
48*eed53cd4SHONG Yifan    },
49*eed53cd4SHONG Yifan    provides = [FeatureConstraintInfo],
50*eed53cd4SHONG Yifan    doc = """Defines a constraint on features.
51*eed53cd4SHONG Yifan
52*eed53cd4SHONG YifanCan be used with require_any_of to specify that something is only enabled when
53*eed53cd4SHONG Yifana constraint is met.""",
54*eed53cd4SHONG Yifan)
55