|
13 | 13 | # limitations under the License.
|
14 | 14 | """Helper functions for working with args."""
|
15 | 15 |
|
| 16 | +load("@bazel_skylib//lib:structs.bzl", "structs") |
16 | 17 | load("//cc:cc_toolchain_config_lib.bzl", "flag_group", "variable_with_value")
|
17 |
| -load("//cc/toolchains:cc_toolchain_info.bzl", "NestedArgsInfo") |
| 18 | +load("//cc/toolchains:cc_toolchain_info.bzl", "NestedArgsInfo", "VariableInfo") |
| 19 | +load(":collect.bzl", "collect_files", "collect_provider") |
18 | 20 |
|
19 | 21 | visibility([
|
20 | 22 | "//cc/toolchains",
|
@@ -48,6 +50,126 @@ cc_args(
|
48 | 50 | iterate_over = "//toolchains/variables:foo_list",
|
49 | 51 | """
|
50 | 52 |
|
| 53 | +# @unsorted-dict-items. |
| 54 | +NESTED_ARGS_ATTRS = { |
| 55 | + "args": attr.string_list( |
| 56 | + doc = """json-encoded arguments to be added to the command-line. |
| 57 | +
|
| 58 | +Usage: |
| 59 | +cc_args( |
| 60 | + ..., |
| 61 | + args = ["--foo", format_arg("%s", "//cc/toolchains/variables:foo")] |
| 62 | +) |
| 63 | +
|
| 64 | +This is equivalent to flag_group(flags = ["--foo", "%{foo}"]) |
| 65 | +
|
| 66 | +Mutually exclusive with nested. |
| 67 | +""", |
| 68 | + ), |
| 69 | + "nested": attr.label_list( |
| 70 | + providers = [NestedArgsInfo], |
| 71 | + doc = """nested_args that should be added on the command-line. |
| 72 | +
|
| 73 | +Mutually exclusive with args.""", |
| 74 | + ), |
| 75 | + "data": attr.label_list( |
| 76 | + allow_files = True, |
| 77 | + doc = """Files required to add this argument to the command-line. |
| 78 | +
|
| 79 | +For example, a flag that sets the header directory might add the headers in that |
| 80 | +directory as additional files. |
| 81 | +""", |
| 82 | + ), |
| 83 | + "variables": attr.label_list( |
| 84 | + providers = [VariableInfo], |
| 85 | + doc = "Variables to be used in substitutions", |
| 86 | + ), |
| 87 | + "iterate_over": attr.label(providers = [VariableInfo], doc = "Replacement for flag_group.iterate_over"), |
| 88 | + "requires_not_none": attr.label(providers = [VariableInfo], doc = "Replacement for flag_group.expand_if_available"), |
| 89 | + "requires_none": attr.label(providers = [VariableInfo], doc = "Replacement for flag_group.expand_if_not_available"), |
| 90 | + "requires_true": attr.label(providers = [VariableInfo], doc = "Replacement for flag_group.expand_if_true"), |
| 91 | + "requires_false": attr.label(providers = [VariableInfo], doc = "Replacement for flag_group.expand_if_false"), |
| 92 | + "requires_equal": attr.label(providers = [VariableInfo], doc = "Replacement for flag_group.expand_if_equal"), |
| 93 | + "requires_equal_value": attr.string(), |
| 94 | +} |
| 95 | + |
| 96 | +def args_wrapper_macro(*, name, rule, args = [], **kwargs): |
| 97 | + """Invokes a rule by converting args to attributes. |
| 98 | +
|
| 99 | + Args: |
| 100 | + name: (str) The name of the target. |
| 101 | + rule: (rule) The rule to invoke. Either cc_args or cc_nested_args. |
| 102 | + args: (List[str|Formatted]) A list of either strings, or function calls |
| 103 | + from format.bzl. For example: |
| 104 | + ["--foo", format_arg("--sysroot=%s", "//cc/toolchains/variables:sysroot")] |
| 105 | + **kwargs: kwargs to pass through into the rule invocation. |
| 106 | + """ |
| 107 | + out_args = [] |
| 108 | + vars = [] |
| 109 | + if type(args) != "list": |
| 110 | + fail("Args must be a list in %s" % native.package_relative_label(name)) |
| 111 | + for arg in args: |
| 112 | + if type(arg) == "string": |
| 113 | + out_args.append(raw_string(arg)) |
| 114 | + elif getattr(arg, "format_type") == "format_arg": |
| 115 | + arg = structs.to_dict(arg) |
| 116 | + if arg["value"] == None: |
| 117 | + out_args.append(arg) |
| 118 | + else: |
| 119 | + var = arg.pop("value") |
| 120 | + |
| 121 | + # Swap the variable from a label to an index. This allows us to |
| 122 | + # actually get the providers in a rule. |
| 123 | + out_args.append(struct(value = len(vars), **arg)) |
| 124 | + vars.append(var) |
| 125 | + else: |
| 126 | + fail("Invalid type of args in %s. Expected either a string or format_args(format_string, variable_label), got value %r" % (native.package_relative_label(name), arg)) |
| 127 | + |
| 128 | + rule( |
| 129 | + name = name, |
| 130 | + args = [json.encode(arg) for arg in out_args], |
| 131 | + variables = vars, |
| 132 | + **kwargs |
| 133 | + ) |
| 134 | + |
| 135 | +def _var(target): |
| 136 | + if target == None: |
| 137 | + return None |
| 138 | + return target[VariableInfo].name |
| 139 | + |
| 140 | +# TODO: Consider replacing this with a subrule in the future. However, maybe not |
| 141 | +# for a long time, since it'll break compatibility with all bazel versions < 7. |
| 142 | +def nested_args_provider_from_ctx(ctx): |
| 143 | + """Gets the nested args provider from a rule that has NESTED_ARGS_ATTRS. |
| 144 | +
|
| 145 | + Args: |
| 146 | + ctx: The rule context |
| 147 | + Returns: |
| 148 | + NestedArgsInfo |
| 149 | + """ |
| 150 | + variables = collect_provider(ctx.attr.variables, VariableInfo) |
| 151 | + args = [] |
| 152 | + for arg in ctx.attr.args: |
| 153 | + arg = json.decode(arg) |
| 154 | + if "value" in arg: |
| 155 | + if arg["value"] != None: |
| 156 | + arg["value"] = variables[arg["value"]] |
| 157 | + args.append(struct(**arg)) |
| 158 | + |
| 159 | + return nested_args_provider( |
| 160 | + label = ctx.label, |
| 161 | + args = args, |
| 162 | + nested = collect_provider(ctx.attr.nested, NestedArgsInfo), |
| 163 | + files = collect_files(ctx.attr.data), |
| 164 | + iterate_over = _var(ctx.attr.iterate_over), |
| 165 | + requires_not_none = _var(ctx.attr.requires_not_none), |
| 166 | + requires_none = _var(ctx.attr.requires_none), |
| 167 | + requires_true = _var(ctx.attr.requires_true), |
| 168 | + requires_false = _var(ctx.attr.requires_false), |
| 169 | + requires_equal = _var(ctx.attr.requires_equal), |
| 170 | + requires_equal_value = ctx.attr.requires_equal_value, |
| 171 | + ) |
| 172 | + |
51 | 173 | def raw_string(s):
|
52 | 174 | """Constructs metadata for creating a raw string.
|
53 | 175 |
|
|
0 commit comments