2024-04-05 08:47:05 +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."""
|
|
|
|
|
|
|
|
load(
|
|
|
|
"//cc/toolchains/impl:nested_args.bzl",
|
|
|
|
"NESTED_ARGS_ATTRS",
|
|
|
|
"nested_args_provider_from_ctx",
|
|
|
|
)
|
|
|
|
load(
|
|
|
|
":cc_toolchain_info.bzl",
|
|
|
|
"NestedArgsInfo",
|
|
|
|
)
|
|
|
|
|
|
|
|
visibility("public")
|
|
|
|
|
|
|
|
_cc_nested_args = rule(
|
|
|
|
implementation = lambda ctx: [nested_args_provider_from_ctx(ctx)],
|
|
|
|
attrs = NESTED_ARGS_ATTRS,
|
|
|
|
provides = [NestedArgsInfo],
|
|
|
|
doc = """Declares a list of arguments bound to a set of actions.
|
|
|
|
|
|
|
|
Roughly equivalent to ctx.actions.args()
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
cc_nested_args(
|
|
|
|
name = "warnings_as_errors",
|
|
|
|
args = ["-Werror"],
|
|
|
|
)
|
|
|
|
""",
|
|
|
|
)
|
|
|
|
|
2024-09-12 18:08:30 +00:00
|
|
|
def cc_nested_args(
|
|
|
|
*,
|
|
|
|
name,
|
|
|
|
args = None,
|
|
|
|
data = None,
|
|
|
|
format = {},
|
|
|
|
iterate_over = None,
|
|
|
|
nested = None,
|
|
|
|
requires_not_none = None,
|
|
|
|
requires_none = None,
|
|
|
|
requires_true = None,
|
|
|
|
requires_false = None,
|
|
|
|
requires_equal = None,
|
|
|
|
requires_equal_value = None,
|
|
|
|
**kwargs):
|
2024-09-30 16:14:24 +00:00
|
|
|
"""Nested arguments for use in more complex `cc_args` expansions.
|
2024-09-12 18:08:30 +00:00
|
|
|
|
2024-09-30 16:14:24 +00:00
|
|
|
While this rule is very similar in shape to `cc_args`, it is intended to be used as a
|
|
|
|
dependency of `cc_args` to provide additional arguments that should be applied to the
|
|
|
|
same actions as defined by the parent `cc_args` rule. The key motivation for this rule
|
2024-09-12 18:08:30 +00:00
|
|
|
is to allow for more complex variable-based argument expensions.
|
|
|
|
|
2024-09-30 16:14:24 +00:00
|
|
|
Prefer expressing collections of arguments as `cc_args` and
|
|
|
|
`cc_args_list` rules when possible.
|
2024-09-12 18:08:30 +00:00
|
|
|
|
|
|
|
For living examples of how this rule is used, see the usages here:
|
2024-09-30 16:14:24 +00:00
|
|
|
https://github.com/bazelbuild/rules_cc/tree/main/cc/toolchains/args/runtime_library_search_directories/BUILD
|
|
|
|
https://github.com/bazelbuild/rules_cc/tree/main/cc/toolchains/args/libraries_to_link/BUILD
|
2024-09-12 18:08:30 +00:00
|
|
|
|
|
|
|
Note: These examples are non-trivial, but they illustrate when it is absolutely necessary to
|
|
|
|
use this rule.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: (str) The name of the target.
|
|
|
|
args: (List[str]) The command-line arguments that are applied by using this rule. This is
|
|
|
|
mutually exclusive with [nested](#cc_nested_args-nested).
|
|
|
|
data: (List[Label]) A list of runtime data dependencies that are required for these
|
|
|
|
arguments to work as intended.
|
|
|
|
format: (Dict[str, Label]) A mapping of format strings to the label of the corresponding
|
2024-09-30 16:14:24 +00:00
|
|
|
`cc_variable` that the value should be pulled from. All instances of
|
|
|
|
`{variable_name}` will be replaced with the expanded value of `variable_name` in this
|
|
|
|
dictionary. The complete list of possible variables can be found in
|
|
|
|
https://github.com/bazelbuild/rules_cc/tree/main/cc/toolchains/variables/BUILD.
|
|
|
|
It is not possible to declare custom variables--these are inherent to Bazel itself.
|
|
|
|
iterate_over: (Label) The label of a `cc_variable` that should be iterated
|
|
|
|
over. This is intended for use with built-in variables that are lists.
|
|
|
|
nested: (List[Label]) A list of `cc_nested_args` rules that should be
|
2024-09-12 18:08:30 +00:00
|
|
|
expanded to command-line arguments when this rule is used. This is mutually exclusive
|
|
|
|
with [args](#cc_nested_args-args).
|
2024-09-30 16:14:24 +00:00
|
|
|
requires_not_none: (Label) The label of a `cc_variable` that should be checked
|
|
|
|
for existence before expanding this rule. If the variable is None, this rule will be
|
2024-09-12 18:08:30 +00:00
|
|
|
ignored.
|
2024-09-30 16:14:24 +00:00
|
|
|
requires_none: (Label) The label of a `cc_variable` that should be checked for
|
|
|
|
non-existence before expanding this rule. If the variable is not None, this rule will be
|
|
|
|
ignored.
|
|
|
|
requires_true: (Label) The label of a `cc_variable` that should be checked for
|
|
|
|
truthiness before expanding this rule. If the variable is false, this rule will be
|
|
|
|
ignored.
|
|
|
|
requires_false: (Label) The label of a `cc_variable` that should be checked
|
|
|
|
for falsiness before expanding this rule. If the variable is true, this rule will be
|
|
|
|
ignored.
|
|
|
|
requires_equal: (Label) The label of a `cc_variable` that should be checked
|
|
|
|
for equality before expanding this rule. If the variable is not equal to
|
2024-09-12 18:08:30 +00:00
|
|
|
(requires_equal_value)[#cc_nested_args-requires_equal_value], this rule will be ignored.
|
|
|
|
requires_equal_value: (str) The value to compare
|
|
|
|
(requires_equal)[#cc_nested_args-requires_equal] against.
|
|
|
|
**kwargs: [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) that should be applied to this rule.
|
|
|
|
"""
|
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
|
|
|
return _cc_nested_args(
|
|
|
|
name = name,
|
2024-09-12 18:08:30 +00:00
|
|
|
args = args,
|
|
|
|
data = data,
|
|
|
|
# We flip the key/value pairs in the dictionary here because Bazel doesn't have a
|
|
|
|
# string-keyed label dict attribute type.
|
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
|
|
|
format = {k: v for v, k in format.items()},
|
2024-09-12 18:08:30 +00:00
|
|
|
iterate_over = iterate_over,
|
|
|
|
nested = nested,
|
|
|
|
requires_not_none = requires_not_none,
|
|
|
|
requires_none = requires_none,
|
|
|
|
requires_true = requires_true,
|
|
|
|
requires_false = requires_false,
|
|
|
|
requires_equal = requires_equal,
|
|
|
|
requires_equal_value = requires_equal_value,
|
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
|
|
|
**kwargs
|
|
|
|
)
|