xref: /aosp_15_r20/external/bazelbuild-rules_cc/tests/rule_based_toolchain/subjects.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"""Test subjects for cc_toolchain_info providers."""
15
16load("@bazel_skylib//lib:structs.bzl", "structs")
17load("@rules_testing//lib:truth.bzl", _subjects = "subjects")
18load(
19    "//cc/toolchains:cc_toolchain_info.bzl",
20    "ActionTypeConfigInfo",
21    "ActionTypeConfigSetInfo",
22    "ActionTypeInfo",
23    "ActionTypeSetInfo",
24    "ArgsInfo",
25    "ArgsListInfo",
26    "FeatureConstraintInfo",
27    "FeatureInfo",
28    "FeatureSetInfo",
29    "MutuallyExclusiveCategoryInfo",
30    "NestedArgsInfo",
31    "ToolInfo",
32    "ToolchainConfigInfo",
33)
34load(":generate_factory.bzl", "ProviderDepset", "ProviderSequence", "generate_factory")
35load(":generics.bzl", "dict_key_subject", "optional_subject", "result_subject", "struct_subject", _result_fn_wrapper = "result_fn_wrapper")
36
37visibility("//tests/rule_based_toolchain/...")
38
39# The default runfiles subject uses path instead of short_path.
40# This makes it rather awkward for copybara.
41runfiles_subject = lambda value, meta: _subjects.depset_file(value.files, meta = meta)
42
43# The string type has .equals(), which is all we can really do for an unknown
44# type.
45unknown_subject = _subjects.str
46
47# buildifier: disable=name-conventions
48_ActionTypeFactory = generate_factory(
49    ActionTypeInfo,
50    "ActionTypeInfo",
51    dict(
52        name = _subjects.str,
53    ),
54)
55
56# buildifier: disable=name-conventions
57_ActionTypeSetFactory = generate_factory(
58    ActionTypeSetInfo,
59    "ActionTypeInfo",
60    dict(
61        actions = ProviderDepset(_ActionTypeFactory),
62    ),
63)
64
65# buildifier: disable=name-conventions
66_MutuallyExclusiveCategoryFactory = generate_factory(
67    MutuallyExclusiveCategoryInfo,
68    "MutuallyExclusiveCategoryInfo",
69    dict(name = _subjects.str),
70)
71
72_FEATURE_FLAGS = dict(
73    name = _subjects.str,
74    enabled = _subjects.bool,
75    args = None,
76    implies = None,
77    requires_any_of = None,
78    mutually_exclusive = ProviderSequence(_MutuallyExclusiveCategoryFactory),
79    overridable = _subjects.bool,
80    external = _subjects.bool,
81    overrides = None,
82)
83
84# Break the dependency loop.
85# buildifier: disable=name-conventions
86_FakeFeatureFactory = generate_factory(
87    FeatureInfo,
88    "FeatureInfo",
89    _FEATURE_FLAGS,
90)
91
92# buildifier: disable=name-conventions
93_FeatureSetFactory = generate_factory(
94    FeatureSetInfo,
95    "FeatureSetInfo",
96    dict(features = ProviderDepset(_FakeFeatureFactory)),
97)
98
99# buildifier: disable=name-conventions
100_FeatureConstraintFactory = generate_factory(
101    FeatureConstraintInfo,
102    "FeatureConstraintInfo",
103    dict(
104        all_of = ProviderDepset(_FakeFeatureFactory),
105        none_of = ProviderDepset(_FakeFeatureFactory),
106    ),
107)
108
109_NESTED_ARGS_FLAGS = dict(
110    nested = None,
111    files = _subjects.depset_file,
112    iterate_over = optional_subject(_subjects.str),
113    legacy_flag_group = unknown_subject,
114    requires_types = _subjects.dict,
115    unwrap_options = _subjects.collection,
116)
117
118# buildifier: disable=name-conventions
119_FakeNestedArgsFactory = generate_factory(
120    NestedArgsInfo,
121    "NestedArgsInfo",
122    _NESTED_ARGS_FLAGS,
123)
124
125# buildifier: disable=name-conventions
126_NestedArgsFactory = generate_factory(
127    NestedArgsInfo,
128    "NestedArgsInfo",
129    _NESTED_ARGS_FLAGS | dict(
130        nested = ProviderSequence(_FakeNestedArgsFactory),
131    ),
132)
133
134# buildifier: disable=name-conventions
135_ArgsFactory = generate_factory(
136    ArgsInfo,
137    "ArgsInfo",
138    dict(
139        actions = ProviderDepset(_ActionTypeFactory),
140        env = _subjects.dict,
141        files = _subjects.depset_file,
142        # Use .factory so it's not inlined.
143        nested = optional_subject(_NestedArgsFactory.factory),
144        requires_any_of = ProviderSequence(_FeatureConstraintFactory),
145    ),
146)
147
148# buildifier: disable=name-conventions
149_ArgsListFactory = generate_factory(
150    ArgsListInfo,
151    "ArgsListInfo",
152    dict(
153        args = ProviderSequence(_ArgsFactory),
154        by_action = lambda values, *, meta: dict_key_subject(struct_subject(
155            args = _subjects.collection,
156            files = _subjects.depset_file,
157        ))({value.action: value for value in values}, meta = meta),
158        files = _subjects.depset_file,
159    ),
160)
161
162# buildifier: disable=name-conventions
163_FeatureFactory = generate_factory(
164    FeatureInfo,
165    "FeatureInfo",
166    _FEATURE_FLAGS | dict(
167        # Use .factory so it's not inlined.
168        args = _ArgsListFactory.factory,
169        implies = ProviderDepset(_FakeFeatureFactory),
170        requires_any_of = ProviderSequence(_FeatureSetFactory),
171        overrides = optional_subject(_FakeFeatureFactory.factory),
172    ),
173)
174
175# buildifier: disable=name-conventions
176_ToolFactory = generate_factory(
177    ToolInfo,
178    "ToolInfo",
179    dict(
180        exe = _subjects.file,
181        runfiles = runfiles_subject,
182        requires_any_of = ProviderSequence(_FeatureConstraintFactory),
183        execution_requirements = _subjects.collection,
184    ),
185)
186
187# buildifier: disable=name-conventions
188_ActionTypeConfigFactory = generate_factory(
189    ActionTypeConfigInfo,
190    "ActionTypeConfigInfo",
191    dict(
192        action_type = _ActionTypeFactory,
193        tools = ProviderSequence(_ToolFactory),
194        args = ProviderSequence(_ArgsFactory),
195        implies = ProviderDepset(_FeatureFactory),
196        files = runfiles_subject,
197    ),
198)
199
200# buildifier: disable=name-conventions
201_ActionTypeConfigSetFactory = generate_factory(
202    ActionTypeConfigSetInfo,
203    "ActionTypeConfigSetInfo",
204    dict(
205        configs = dict_key_subject(_ActionTypeConfigFactory.factory),
206    ),
207)
208
209# buildifier: disable=name-conventions
210_ToolchainConfigFactory = generate_factory(
211    ToolchainConfigInfo,
212    "ToolchainConfigInfo",
213    dict(
214        features = ProviderDepset(_FeatureFactory),
215        action_type_configs = dict_key_subject(_ActionTypeConfigFactory.factory),
216        args = ProviderSequence(_ArgsFactory),
217        files = dict_key_subject(_subjects.depset_file),
218    ),
219)
220
221FACTORIES = [
222    _ActionTypeFactory,
223    _ActionTypeSetFactory,
224    _NestedArgsFactory,
225    _ArgsFactory,
226    _ArgsListFactory,
227    _MutuallyExclusiveCategoryFactory,
228    _FeatureFactory,
229    _FeatureConstraintFactory,
230    _FeatureSetFactory,
231    _ToolFactory,
232    _ActionTypeConfigSetFactory,
233    _ToolchainConfigFactory,
234]
235
236result_fn_wrapper = _result_fn_wrapper
237
238subjects = struct(
239    **(structs.to_dict(_subjects) | dict(
240        unknown = unknown_subject,
241        result = result_subject,
242        optional = optional_subject,
243        struct = struct_subject,
244        runfiles = runfiles_subject,
245        dict_key = dict_key_subject,
246    ) | {factory.name: factory.factory for factory in FACTORIES})
247)
248