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.
|
|
|
|
"""All providers for rule-based bazel toolchain config."""
|
|
|
|
|
2024-04-05 08:47:05 +00:00
|
|
|
load("//cc/toolchains/impl:args_utils.bzl", "validate_nested_args")
|
2024-02-21 01:58:00 +00:00
|
|
|
load(
|
|
|
|
"//cc/toolchains/impl:collect.bzl",
|
|
|
|
"collect_action_types",
|
|
|
|
"collect_files",
|
|
|
|
"collect_provider",
|
|
|
|
)
|
2024-04-05 08:47:05 +00:00
|
|
|
load(
|
|
|
|
"//cc/toolchains/impl:nested_args.bzl",
|
|
|
|
"NESTED_ARGS_ATTRS",
|
|
|
|
"nested_args_provider_from_ctx",
|
|
|
|
)
|
2024-02-21 01:58:00 +00:00
|
|
|
load(
|
|
|
|
":cc_toolchain_info.bzl",
|
|
|
|
"ActionTypeSetInfo",
|
|
|
|
"ArgsInfo",
|
|
|
|
"ArgsListInfo",
|
2024-04-05 08:47:05 +00:00
|
|
|
"BuiltinVariablesInfo",
|
2024-02-21 01:58:00 +00:00
|
|
|
"FeatureConstraintInfo",
|
|
|
|
)
|
|
|
|
|
|
|
|
visibility("public")
|
|
|
|
|
|
|
|
def _cc_args_impl(ctx):
|
|
|
|
actions = collect_action_types(ctx.attr.actions)
|
2024-04-05 08:47:05 +00:00
|
|
|
|
2024-03-19 08:09:17 +00:00
|
|
|
nested = None
|
2024-04-05 08:47:05 +00:00
|
|
|
if ctx.attr.args or ctx.attr.nested:
|
|
|
|
nested = nested_args_provider_from_ctx(ctx)
|
|
|
|
validate_nested_args(
|
|
|
|
variables = ctx.attr._variables[BuiltinVariablesInfo].variables,
|
|
|
|
nested_args = nested,
|
|
|
|
actions = actions.to_list(),
|
2024-03-14 23:26:51 +00:00
|
|
|
label = ctx.label,
|
|
|
|
)
|
2024-04-05 08:47:05 +00:00
|
|
|
files = nested.files
|
|
|
|
else:
|
|
|
|
files = collect_files(ctx.attr.data)
|
|
|
|
|
|
|
|
requires = collect_provider(ctx.attr.requires_any_of, FeatureConstraintInfo)
|
2024-03-14 23:26:51 +00:00
|
|
|
|
2024-02-21 01:58:00 +00:00
|
|
|
args = ArgsInfo(
|
|
|
|
label = ctx.label,
|
|
|
|
actions = actions,
|
|
|
|
requires_any_of = tuple(requires),
|
2024-03-19 08:09:17 +00:00
|
|
|
nested = nested,
|
2024-02-21 01:58:00 +00:00
|
|
|
env = ctx.attr.env,
|
2024-03-14 23:26:51 +00:00
|
|
|
files = files,
|
2024-02-21 01:58:00 +00:00
|
|
|
)
|
|
|
|
return [
|
|
|
|
args,
|
2024-02-23 21:54:01 +00:00
|
|
|
ArgsListInfo(
|
|
|
|
label = ctx.label,
|
|
|
|
args = tuple([args]),
|
|
|
|
files = files,
|
|
|
|
by_action = tuple([
|
2024-02-27 12:44:55 +00:00
|
|
|
struct(action = action, args = tuple([args]), files = files)
|
2024-02-23 21:54:01 +00:00
|
|
|
for action in actions.to_list()
|
|
|
|
]),
|
|
|
|
),
|
2024-02-21 01:58:00 +00:00
|
|
|
]
|
|
|
|
|
2024-04-05 08:47:05 +00:00
|
|
|
_cc_args = rule(
|
2024-02-21 01:58:00 +00:00
|
|
|
implementation = _cc_args_impl,
|
|
|
|
attrs = {
|
|
|
|
"actions": attr.label_list(
|
|
|
|
providers = [ActionTypeSetInfo],
|
|
|
|
mandatory = True,
|
|
|
|
doc = """A list of action types that this flag set applies to.
|
|
|
|
|
|
|
|
See @rules_cc//cc/toolchains/actions:all for valid options.
|
|
|
|
""",
|
|
|
|
),
|
|
|
|
"env": attr.string_dict(
|
|
|
|
doc = "Environment variables to be added to the command-line.",
|
|
|
|
),
|
|
|
|
"requires_any_of": attr.label_list(
|
|
|
|
providers = [FeatureConstraintInfo],
|
|
|
|
doc = """This will be enabled when any of the constraints are met.
|
|
|
|
|
|
|
|
If omitted, this flag set will be enabled unconditionally.
|
|
|
|
""",
|
|
|
|
),
|
2024-04-05 08:47:05 +00:00
|
|
|
"_variables": attr.label(
|
|
|
|
default = "//cc/toolchains/variables:variables",
|
|
|
|
),
|
|
|
|
} | NESTED_ARGS_ATTRS,
|
2024-02-21 01:58:00 +00:00
|
|
|
provides = [ArgsInfo],
|
|
|
|
doc = """Declares a list of arguments bound to a set of actions.
|
|
|
|
|
|
|
|
Roughly equivalent to ctx.actions.args()
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
cc_args(
|
|
|
|
name = "warnings_as_errors",
|
|
|
|
args = ["-Werror"],
|
|
|
|
)
|
|
|
|
""",
|
|
|
|
)
|
2024-04-05 08:47:05 +00:00
|
|
|
|
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
|
|
|
def cc_args(name, format = {}, **kwargs):
|
|
|
|
return _cc_args(
|
|
|
|
name = name,
|
|
|
|
format = {k: v for v, k in format.items()},
|
|
|
|
**kwargs
|
|
|
|
)
|