Skip to content

Commit d28d71c

Browse files
committed
CR
1 parent 640cf52 commit d28d71c

5 files changed

Lines changed: 97 additions & 6 deletions

File tree

cc/toolchains/impl/nested_args.bzl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,21 @@ def _escape(s):
294294

295295
_SUPPORTED_BUILD_SETTING_TYPES = ["string", "bool", "int", "Label"]
296296

297-
def _format_build_setting(target, fail = fail):
298-
value = target[BuildSettingInfo].value
299-
297+
def _format_build_setting(value, label, fail = fail):
300298
if type(value) in _SUPPORTED_BUILD_SETTING_TYPES:
301299
return _escape(str(value))
302300

303-
fail("%s had an unsupported build setting type %s. Only string, bool, int, or Label values may be formatted." % (target.label, type(value)))
301+
fail("%s had an unsupported build setting type %s. Only string, bool, int, or Label values may be formatted." % (label, type(value)))
304302

305303
def _format_target(target, fail = fail):
306304
if VariableInfo in target:
307305
return "%%{%s}" % target[VariableInfo].name
308306
elif BuildSettingInfo in target:
309-
return _format_build_setting(target, fail = fail)
307+
return _format_build_setting(
308+
target[BuildSettingInfo].value,
309+
target.label,
310+
fail = fail,
311+
)
310312
elif DirectoryInfo in target:
311313
return _escape(target[DirectoryInfo].path)
312314

tests/rule_based_toolchain/analysis_test_suite.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,25 @@ def analysis_test_suite(name, tests, targets = [_DEFAULT_TARGET]):
3333
targets = [native.package_relative_label(target) for target in targets]
3434

3535
test_case_names = []
36-
for test_name, impl in tests.items():
36+
for test_name, test in tests.items():
3737
if not test_name.endswith("_test"):
3838
fail("Expected test keys to end with '_test', got test case %r" % test_name)
3939
test_case_names.append(":" + test_name)
40+
config_settings = {}
41+
if type(test) == "struct":
42+
if not hasattr(test, "impl"):
43+
fail("Expected struct tests to define an 'impl' field, got %r" % test_name)
44+
impl = test.impl
45+
if hasattr(test, "config_settings"):
46+
config_settings = test.config_settings
47+
else:
48+
impl = test
4049
analysis_test(
4150
name = test_name,
4251
impl = impl,
4352
provider_subject_factories = FACTORIES,
4453
targets = {label.name: label for label in targets},
54+
config_settings = config_settings,
4555
)
4656

4757
native.test_suite(

tests/rule_based_toolchain/args/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load("//cc/toolchains/impl:variables.bzl", "cc_variable", "types")
55
load("//tests/rule_based_toolchain:analysis_test_suite.bzl", "analysis_test_suite")
66
load("//tests/rule_based_toolchain:testing_rules.bzl", "expect_failure_test")
77
load(":args_test.bzl", "TARGETS", "TESTS")
8+
load(":override_args.bzl", "override_args")
89

910
cc_variable(
1011
name = "some_variable",
@@ -70,6 +71,12 @@ util.helper_target(
7071
format = {"min_os_flag": ":macos_min_os_flag"},
7172
)
7273

74+
override_args(
75+
name = "build_setting_format_override",
76+
target = ":build_setting_format",
77+
value = "-mmacosx-version-min=13.0",
78+
)
79+
7380
util.helper_target(
7481
cc_args,
7582
name = "with_dir",

tests/rule_based_toolchain/args/args_test.bzl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ _SIMPLE_FILES = [
5050
"tests/rule_based_toolchain/testdata/multiple2",
5151
]
5252
_TOOL_DIRECTORY = "tests/rule_based_toolchain/testdata"
53+
_OVERRIDDEN_MIN_OS = "-mmacosx-version-min=13.0"
5354

5455
_CONVERTED_ARGS = subjects.struct(
5556
flag_sets = subjects.collection,
@@ -190,12 +191,34 @@ def _build_setting_format_test(env, targets):
190191
flag_groups = [flag_group(flags = ["-mmacosx-version-min=12.0"])],
191192
)])
192193

194+
def _build_setting_format_override_test(env, targets):
195+
build_setting = env.expect.that_target(targets.build_setting_format_override).provider(ArgsInfo)
196+
build_setting.actions().contains_exactly([
197+
targets.c_compile.label,
198+
targets.cpp_compile.label,
199+
])
200+
build_setting.env().entries().contains_exactly({"APPLE_MIN_OS": _OVERRIDDEN_MIN_OS})
201+
202+
converted = env.expect.that_value(
203+
convert_args(targets.build_setting_format_override[ArgsInfo]),
204+
factory = _CONVERTED_ARGS,
205+
)
206+
converted.env_sets().contains_exactly([env_set(
207+
actions = ["c_compile", "cpp_compile"],
208+
env_entries = [env_entry(key = "APPLE_MIN_OS", value = _OVERRIDDEN_MIN_OS)],
209+
)])
210+
converted.flag_sets().contains_exactly([flag_set(
211+
actions = ["c_compile", "cpp_compile"],
212+
flag_groups = [flag_group(flags = [_OVERRIDDEN_MIN_OS])],
213+
)])
214+
193215
TARGETS = [
194216
":simple",
195217
":some_variable",
196218
":env_only",
197219
":env_only_requires",
198220
":build_setting_format",
221+
":build_setting_format_override",
199222
":with_dir",
200223
":with_dir_and_data",
201224
":iterate_over_optional",
@@ -384,6 +407,7 @@ TESTS = {
384407
"with_dir_test": _with_dir_test,
385408
"with_dir_and_data_test": _with_dir_and_data_test,
386409
"build_setting_format_test": _build_setting_format_test,
410+
"build_setting_format_override_test": _build_setting_format_override_test,
387411
"good_env_format_test": _good_env_format_test,
388412
"good_env_format_optional_test": _good_env_format_optional_test,
389413
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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-only helper to apply a build setting override."""
15+
16+
load("//cc/toolchains:cc_toolchain_info.bzl", "ArgsInfo", "ArgsListInfo")
17+
18+
_MIN_OS_FLAG = "//tests/rule_based_toolchain/args:macos_min_os_flag"
19+
20+
def _override_min_os_transition_impl(settings, attr):
21+
return {_MIN_OS_FLAG: attr.value}
22+
23+
_override_min_os_transition = transition(
24+
implementation = _override_min_os_transition_impl,
25+
inputs = [],
26+
outputs = [_MIN_OS_FLAG],
27+
)
28+
29+
def _override_args_impl(ctx):
30+
target = ctx.attr.target
31+
if type(target) == "list":
32+
target = target[0]
33+
return [
34+
target[ArgsInfo],
35+
target[ArgsListInfo],
36+
]
37+
38+
override_args = rule(
39+
implementation = _override_args_impl,
40+
attrs = {
41+
"target": attr.label(
42+
cfg = _override_min_os_transition,
43+
providers = [ArgsInfo],
44+
mandatory = True,
45+
),
46+
"value": attr.string(mandatory = True),
47+
},
48+
)

0 commit comments

Comments
 (0)