• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 compound relationship between features.
51
52This rule can be used with [`cc_args.require_any_of`](#cc_args-require_any_of) to specify that a set
53of arguments are only enabled when a constraint is met. Both `all_of` and `none_of` must be
54satisfied simultaneously.
55
56This is basically a `cc_feature_set` that supports `none_of` expressions. This extra flexibility
57is why this rule may only be used by [`cc_args.require_any_of`](#cc_args-require_any_of).
58
59Example:
60```
61load("//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint")
62
63# A constraint that requires a `linker_supports_thinlto` feature to be enabled,
64# AND a `no_optimization` to be disabled.
65cc_feature_constraint(
66    name = "thinlto_constraint",
67    all_of = [":linker_supports_thinlto"],
68    none_of = [":no_optimization"],
69)
70```
71""",
72)
73