Skip to content

Commit

Permalink
Add support for select'ing on cc_args(args=...).
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matts1 authored and copybara-github committed Jul 26, 2024
1 parent 0d1b084 commit e1c7ebb
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 268 deletions.
11 changes: 6 additions & 5 deletions cc/toolchains/args.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ load(
load(
"//cc/toolchains/impl:nested_args.bzl",
"NESTED_ARGS_ATTRS",
"args_wrapper_macro",
"nested_args_provider_from_ctx",
)
load(
Expand All @@ -40,9 +39,6 @@ visibility("public")
def _cc_args_impl(ctx):
actions = collect_action_types(ctx.attr.actions)

if not ctx.attr.args and not ctx.attr.nested and not ctx.attr.env:
fail("cc_args requires at least one of args, nested, and env")

nested = None
if ctx.attr.args or ctx.attr.nested:
nested = nested_args_provider_from_ctx(ctx)
Expand Down Expand Up @@ -117,4 +113,9 @@ Examples:
""",
)

cc_args = lambda **kwargs: args_wrapper_macro(rule = _cc_args, **kwargs)
def cc_args(name, format = {}, **kwargs):
return _cc_args(
name = name,
format = {k: v for v, k in format.items()},
**kwargs
)
26 changes: 0 additions & 26 deletions cc/toolchains/format.bzl

This file was deleted.

Loading

0 comments on commit e1c7ebb

Please sign in to comment.