Skip to content

Commit 34b8cf4

Browse files
committed
feat(builtin): add support for predefined variables and custom variable to params_file
``` args: Arguments to concatenate into a params file. 1. Subject to $(location) substitutions. NB: This substition returns the manifest file path which differs from the *_binary & *_test args and genrule bazel substitions. This will be fixed in a future major release. See docs string of `expand_location_into_runfiles` macro in `internal/common/expand_into_runfiles.bzl` for more info. 2. Subject to predefined variables & custom variable substitutions. See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_variables and https://docs.bazel.build/versions/master/be/make-variables.html#custom_variables. Predefined genrule variables are not supported in this context. ```
1 parent 569914c commit 34b8cf4

2 files changed

Lines changed: 51 additions & 26 deletions

File tree

internal/common/expand_into_runfiles.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ def expand_location_into_runfiles(ctx, input, targets = []):
5353
Path is returned in runfiles manifest path format such as `repo/path/to/file`. This differs from how $(location)
5454
and $(locations) expansion behaves in expansion the `args` attribute of a *_binary or *_test which returns
5555
the runfiles short path of the format `./path/to/file` for user repo and `../external_repo/path/to/file` for external
56-
repositories. We may change this behavior in the future with $(mlocation) and $(mlocations) used to expand
57-
to the runfiles manifest path.
58-
See https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-binaries.
56+
repositories.
57+
58+
This will be fixed in a future release major release as well as adding support for $(execpath) and $(rootpath)
59+
substitions: https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables.
5960
6061
Args:
6162
ctx: context

internal/common/params_file.bzl

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,24 @@ load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfile
1919

2020
_DOC = """Generates a params file from a list of arguments."""
2121

22+
# See params_file macro below for docstrings
2223
_ATTRS = {
23-
"out": attr.output(
24-
doc = """Path of the output file, relative to this package.""",
25-
mandatory = True,
26-
),
27-
"args": attr.string_list(
28-
doc = """Arguments to concatenate into a params file.
29-
Subject to $(location) substitutions""",
30-
),
31-
"data": attr.label_list(
32-
doc = """Data for $(location) expansions in args.""",
33-
allow_files = True,
34-
),
24+
"out": attr.output(mandatory = True),
25+
"args": attr.string_list(),
26+
"data": attr.label_list(allow_files = True),
3527
"is_windows": attr.bool(mandatory = True),
3628
"newline": attr.string(
37-
doc = """one of ["auto", "unix", "windows"]: line endings to use. "auto"
38-
for platform-determined, "unix" for LF, and "windows" for CRLF.""",
3929
values = ["unix", "windows", "auto"],
4030
default = "auto",
4131
),
4232
}
4333

34+
def _expand_location_into_runfiles(ctx, s):
35+
# `.split(" ")` is a work-around https://github.com/bazelbuild/bazel/issues/10309
36+
# TODO: If the string has intentional spaces or if one or more of the expanded file
37+
# locations has a space in the name, we will incorrectly split it into multiple arguments
38+
return expand_location_into_runfiles(ctx, s, targets = ctx.attr.data).split(" ")
39+
4440
def _impl(ctx):
4541
if ctx.attr.newline == "auto":
4642
newline = "\r\n" if ctx.attr.is_windows else "\n"
@@ -49,10 +45,19 @@ def _impl(ctx):
4945
else:
5046
newline = "\n"
5147

48+
expanded_args = []
49+
50+
# First expand $(location) args
51+
for a in ctx.attr.args:
52+
expanded_args += _expand_location_into_runfiles(ctx, a)
53+
54+
# Next expand predefined variables & custom variables
55+
expanded_args = [ctx.expand_make_variables("args", e, {}) for e in expanded_args]
56+
5257
# ctx.actions.write creates a FileWriteAction which uses UTF-8 encoding.
5358
ctx.actions.write(
5459
output = ctx.outputs.out,
55-
content = newline.join([expand_location_into_runfiles(ctx, a, ctx.attr.data) for a in ctx.attr.args]),
60+
content = newline.join(expanded_args),
5661
is_executable = False,
5762
)
5863
files = depset(direct = [ctx.outputs.out])
@@ -70,25 +75,44 @@ def params_file(
7075
name,
7176
out,
7277
args = [],
78+
data = [],
7379
newline = "auto",
7480
**kwargs):
7581
"""Generates a UTF-8 encoded params file from a list of arguments.
7682
77-
Handles $(location) expansions for arguments.
83+
Handles variable substitutions for args.
7884
7985
Args:
80-
name: Name of the rule.
81-
out: Path of the output file, relative to this package.
82-
args: Arguments to concatenate into a params file.
83-
Subject to $(location) substitutions
84-
newline: one of ["auto", "unix", "windows"]: line endings to use. "auto"
85-
for platform-determined, "unix" for LF, and "windows" for CRLF.
86-
**kwargs: further keyword arguments, e.g. <code>visibility</code>
86+
name: Name of the rule.
87+
out: Path of the output file, relative to this package.
88+
args: Arguments to concatenate into a params file.
89+
90+
1. Subject to $(location) substitutions.
91+
92+
NB: This substition returns the manifest file path which differs from the *_binary & *_test
93+
args and genrule bazel substitions. This will be fixed in a future major release.
94+
See docs string of `expand_location_into_runfiles` macro in
95+
`internal/common/expand_into_runfiles.bzl` for more info.
96+
97+
2. Subject to predefined variables & custom variable substitutions.
98+
99+
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_variables
100+
and https://docs.bazel.build/versions/master/be/make-variables.html#custom_variables.
101+
102+
Predefined genrule variables are not supported in this context.
103+
104+
data: Data for $(location) expansions in args.
105+
newline: Line endings to use. One of ["auto", "unix", "windows"].
106+
107+
"auto" for platform-determined
108+
"unix" for LF
109+
"windows" for CRLF
87110
"""
88111
_params_file(
89112
name = name,
90113
out = out,
91114
args = args,
115+
data = data,
92116
newline = newline or "auto",
93117
is_windows = select({
94118
"@bazel_tools//src/conditions:host_windows": True,

0 commit comments

Comments
 (0)