• 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_external_feature rule."""
15
16load(
17    "//cc/toolchains:cc_toolchain_info.bzl",
18    "ArgsListInfo",
19    "FeatureConstraintInfo",
20    "FeatureInfo",
21    "FeatureSetInfo",
22)
23
24visibility([
25    "//cc/toolchains/...",
26    "//tests/rule_based_toolchain/...",
27])
28
29def _cc_external_feature_impl(ctx):
30    feature = FeatureInfo(
31        label = ctx.label,
32        name = ctx.attr.feature_name,
33        enabled = False,
34        args = ArgsListInfo(
35            label = ctx.label,
36            args = (),
37            files = depset([]),
38            by_action = (),
39        ),
40        implies = depset([]),
41        requires_any_of = (),
42        mutually_exclusive = (),
43        external = True,
44        overridable = ctx.attr.overridable,
45        overrides = None,
46        allowlist_include_directories = depset(),
47    )
48    providers = [
49        feature,
50        FeatureSetInfo(label = ctx.label, features = depset([feature])),
51        FeatureConstraintInfo(
52            label = ctx.label,
53            all_of = depset([feature]),
54            none_of = depset([]),
55        ),
56    ]
57    return providers
58
59cc_external_feature = rule(
60    implementation = _cc_external_feature_impl,
61    attrs = {
62        "feature_name": attr.string(
63            mandatory = True,
64            doc = "The name of the feature",
65        ),
66        "overridable": attr.bool(
67            doc = "Whether the feature can be overridden",
68            mandatory = True,
69        ),
70    },
71    provides = [FeatureInfo, FeatureSetInfo, FeatureConstraintInfo],
72    doc = """A declaration that a [feature](https://bazel.build/docs/cc-toolchain-config-reference#features) with this name is defined elsewhere.
73
74This rule communicates that a feature has been defined externally to make it possible to reference
75features that live outside the rule-based cc toolchain ecosystem. This allows various toolchain
76rules to reference the external feature without accidentally re-defining said feature.
77
78This rule is currently considered a private API of the toolchain rules to encourage the Bazel
79ecosystem to migrate to properly defining their features as rules.
80
81Example:
82```
83load("//cc/toolchains:external_feature.bzl", "cc_external_feature")
84
85# rules_rust defines a feature that is disabled whenever rust artifacts are being linked using
86# the cc toolchain to signal that incompatible flags should be disabled as well.
87cc_external_feature(
88    name = "rules_rust_unsupported_feature",
89    feature_name = "rules_rust_unsupported_feature",
90    overridable = False,
91)
92```
93""",
94)
95