2024-02-21 01:58:00 +00:00
|
|
|
# Copyright 2024 The Bazel Authors. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
"""Tests for the cc_args rule."""
|
|
|
|
|
2024-03-14 23:26:51 +00:00
|
|
|
load(
|
|
|
|
"//cc:cc_toolchain_config_lib.bzl",
|
|
|
|
"env_entry",
|
|
|
|
"env_set",
|
|
|
|
"flag_group",
|
|
|
|
"flag_set",
|
|
|
|
)
|
2024-02-21 01:58:00 +00:00
|
|
|
load(
|
|
|
|
"//cc/toolchains:cc_toolchain_info.bzl",
|
2024-02-23 21:54:01 +00:00
|
|
|
"ActionTypeInfo",
|
2024-02-21 01:58:00 +00:00
|
|
|
"ArgsInfo",
|
2024-02-23 21:54:01 +00:00
|
|
|
"ArgsListInfo",
|
2024-02-21 01:58:00 +00:00
|
|
|
)
|
2024-03-14 23:26:51 +00:00
|
|
|
load(
|
|
|
|
"//cc/toolchains/impl:legacy_converter.bzl",
|
|
|
|
"convert_args",
|
|
|
|
)
|
|
|
|
load("//tests/rule_based_toolchain:subjects.bzl", "subjects")
|
2024-02-21 01:58:00 +00:00
|
|
|
|
2024-02-22 11:05:41 +00:00
|
|
|
visibility("private")
|
2024-02-21 01:58:00 +00:00
|
|
|
|
2024-02-23 21:54:01 +00:00
|
|
|
_SIMPLE_FILES = [
|
|
|
|
"tests/rule_based_toolchain/testdata/file1",
|
|
|
|
"tests/rule_based_toolchain/testdata/multiple1",
|
|
|
|
"tests/rule_based_toolchain/testdata/multiple2",
|
|
|
|
]
|
2024-09-05 16:05:23 +00:00
|
|
|
_TOOL_DIRECTORY = "tests/rule_based_toolchain/testdata"
|
2024-02-23 21:54:01 +00:00
|
|
|
|
2024-03-14 23:26:51 +00:00
|
|
|
_CONVERTED_ARGS = subjects.struct(
|
|
|
|
flag_sets = subjects.collection,
|
|
|
|
env_sets = subjects.collection,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _simple_test(env, targets):
|
2024-02-21 01:58:00 +00:00
|
|
|
simple = env.expect.that_target(targets.simple).provider(ArgsInfo)
|
|
|
|
simple.actions().contains_exactly([
|
|
|
|
targets.c_compile.label,
|
|
|
|
targets.cpp_compile.label,
|
|
|
|
])
|
|
|
|
simple.env().contains_exactly({"BAR": "bar"})
|
2024-02-23 21:54:01 +00:00
|
|
|
simple.files().contains_exactly(_SIMPLE_FILES)
|
|
|
|
|
|
|
|
c_compile = env.expect.that_target(targets.simple).provider(ArgsListInfo).by_action().get(
|
|
|
|
targets.c_compile[ActionTypeInfo],
|
|
|
|
)
|
|
|
|
c_compile.args().contains_exactly([targets.simple[ArgsInfo]])
|
|
|
|
c_compile.files().contains_exactly(_SIMPLE_FILES)
|
2024-02-21 01:58:00 +00:00
|
|
|
|
2024-03-14 23:26:51 +00:00
|
|
|
converted = env.expect.that_value(
|
|
|
|
convert_args(targets.simple[ArgsInfo]),
|
|
|
|
factory = _CONVERTED_ARGS,
|
|
|
|
)
|
|
|
|
converted.env_sets().contains_exactly([env_set(
|
|
|
|
actions = ["c_compile", "cpp_compile"],
|
|
|
|
env_entries = [env_entry(key = "BAR", value = "bar")],
|
|
|
|
)])
|
|
|
|
|
|
|
|
converted.flag_sets().contains_exactly([flag_set(
|
|
|
|
actions = ["c_compile", "cpp_compile"],
|
|
|
|
flag_groups = [flag_group(flags = ["--foo", "foo"])],
|
|
|
|
)])
|
|
|
|
|
|
|
|
def _env_only_test(env, targets):
|
|
|
|
env_only = env.expect.that_target(targets.env_only).provider(ArgsInfo)
|
|
|
|
env_only.actions().contains_exactly([
|
|
|
|
targets.c_compile.label,
|
|
|
|
targets.cpp_compile.label,
|
|
|
|
])
|
|
|
|
env_only.env().contains_exactly({"BAR": "bar"})
|
|
|
|
env_only.files().contains_exactly(_SIMPLE_FILES)
|
|
|
|
|
|
|
|
c_compile = env.expect.that_target(targets.simple).provider(ArgsListInfo).by_action().get(
|
|
|
|
targets.c_compile[ActionTypeInfo],
|
|
|
|
)
|
|
|
|
c_compile.files().contains_exactly(_SIMPLE_FILES)
|
|
|
|
|
|
|
|
converted = env.expect.that_value(
|
|
|
|
convert_args(targets.env_only[ArgsInfo]),
|
|
|
|
factory = _CONVERTED_ARGS,
|
|
|
|
)
|
|
|
|
converted.env_sets().contains_exactly([env_set(
|
|
|
|
actions = ["c_compile", "cpp_compile"],
|
|
|
|
env_entries = [env_entry(key = "BAR", value = "bar")],
|
|
|
|
)])
|
|
|
|
|
|
|
|
converted.flag_sets().contains_exactly([])
|
|
|
|
|
2024-09-05 16:05:23 +00:00
|
|
|
def _with_dir_test(env, targets):
|
|
|
|
with_dir = env.expect.that_target(targets.with_dir).provider(ArgsInfo)
|
|
|
|
with_dir.allowlist_include_directories().contains_exactly([_TOOL_DIRECTORY])
|
|
|
|
with_dir.files().contains_at_least(_SIMPLE_FILES)
|
|
|
|
|
|
|
|
c_compile = env.expect.that_target(targets.with_dir).provider(ArgsListInfo).by_action().get(
|
|
|
|
targets.c_compile[ActionTypeInfo],
|
|
|
|
)
|
|
|
|
c_compile.files().contains_at_least(_SIMPLE_FILES)
|
|
|
|
|
2024-02-21 01:58:00 +00:00
|
|
|
TARGETS = [
|
|
|
|
":simple",
|
2024-03-14 23:26:51 +00:00
|
|
|
":env_only",
|
2024-09-05 16:05:23 +00:00
|
|
|
":with_dir",
|
2024-02-21 01:58:00 +00:00
|
|
|
"//tests/rule_based_toolchain/actions:c_compile",
|
|
|
|
"//tests/rule_based_toolchain/actions:cpp_compile",
|
|
|
|
]
|
|
|
|
|
2024-03-14 23:26:51 +00:00
|
|
|
# @unsorted-dict-items
|
2024-02-21 01:58:00 +00:00
|
|
|
TESTS = {
|
2024-03-14 23:26:51 +00:00
|
|
|
"simple_test": _simple_test,
|
Add support for select'ing on cc_args(args=...).
This CL is an alternative to unknown commit. I left the other CL seperately, because I wasn't 100% sure that we'd agree to this, since this is an API change.
I did it this way because I believe it's much less hacky, and it also allows us to format things that aren't variables.
BEGIN_PUBLIC
Add support for select'ing on cc_args(args=...).
This is quite tricky because the one parameter was being split into two in a macro, one of type label and the other of type string.
For example, `args = ["--foo", format_arg("--bar=%s", "//path/to:bar")]` was rewritten by the macro to `args = [json.encode(struct(format_type="raw", format="foo")), json.encode(struct(format_type="format_arg", format="--bar=%s", value=0))], variables = ["//path/to:bar"]`.
To allow it to work with selects, we need to ensure that we don't perform post-processing on the inside of the select. To solve this, we:
* Ensure that args only take strings
* Provide a seperate parameter for substitutions.
This new mechanism also has the useful property that we can now format things that are not variables. For example, I can do the following:
```
directory(name = "sysroot", ...)
cc_args(
name = "sysroot_arg",
args = ["--sysroot={sysroot}"],
format = {
":sysroot": "sysroot"
}
)
```
END_PUBLIC
PiperOrigin-RevId: 656211278
Change-Id: If83f1ea5a99090c18f2a561c51ec6d39ce9fe419
2024-07-26 03:13:10 +00:00
|
|
|
"env_only_test": _env_only_test,
|
2024-09-05 16:05:23 +00:00
|
|
|
"with_dir_test": _with_dir_test,
|
2024-02-21 01:58:00 +00:00
|
|
|
}
|