Skip to content

Commit

Permalink
feat(builtin): add support for predefined variables and custom variab…
Browse files Browse the repository at this point in the history
…le 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.
```
  • Loading branch information
gregmagolan committed Mar 26, 2020
1 parent 569914c commit 34b8cf4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
7 changes: 4 additions & 3 deletions internal/common/expand_into_runfiles.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ def expand_location_into_runfiles(ctx, input, targets = []):
Path is returned in runfiles manifest path format such as `repo/path/to/file`. This differs from how $(location)
and $(locations) expansion behaves in expansion the `args` attribute of a *_binary or *_test which returns
the runfiles short path of the format `./path/to/file` for user repo and `../external_repo/path/to/file` for external
repositories. We may change this behavior in the future with $(mlocation) and $(mlocations) used to expand
to the runfiles manifest path.
See https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-binaries.
repositories.
This will be fixed in a future release major release as well as adding support for $(execpath) and $(rootpath)
substitions: https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables.
Args:
ctx: context
Expand Down
70 changes: 47 additions & 23 deletions internal/common/params_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,24 @@ load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfile

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

# See params_file macro below for docstrings
_ATTRS = {
"out": attr.output(
doc = """Path of the output file, relative to this package.""",
mandatory = True,
),
"args": attr.string_list(
doc = """Arguments to concatenate into a params file.
Subject to $(location) substitutions""",
),
"data": attr.label_list(
doc = """Data for $(location) expansions in args.""",
allow_files = True,
),
"out": attr.output(mandatory = True),
"args": attr.string_list(),
"data": attr.label_list(allow_files = True),
"is_windows": attr.bool(mandatory = True),
"newline": attr.string(
doc = """one of ["auto", "unix", "windows"]: line endings to use. "auto"
for platform-determined, "unix" for LF, and "windows" for CRLF.""",
values = ["unix", "windows", "auto"],
default = "auto",
),
}

def _expand_location_into_runfiles(ctx, s):
# `.split(" ")` is a work-around https://github.com/bazelbuild/bazel/issues/10309
# TODO: If the string has intentional spaces or if one or more of the expanded file
# locations has a space in the name, we will incorrectly split it into multiple arguments
return expand_location_into_runfiles(ctx, s, targets = ctx.attr.data).split(" ")

def _impl(ctx):
if ctx.attr.newline == "auto":
newline = "\r\n" if ctx.attr.is_windows else "\n"
Expand All @@ -49,10 +45,19 @@ def _impl(ctx):
else:
newline = "\n"

expanded_args = []

# First expand $(location) args
for a in ctx.attr.args:
expanded_args += _expand_location_into_runfiles(ctx, a)

# Next expand predefined variables & custom variables
expanded_args = [ctx.expand_make_variables("args", e, {}) for e in expanded_args]

# ctx.actions.write creates a FileWriteAction which uses UTF-8 encoding.
ctx.actions.write(
output = ctx.outputs.out,
content = newline.join([expand_location_into_runfiles(ctx, a, ctx.attr.data) for a in ctx.attr.args]),
content = newline.join(expanded_args),
is_executable = False,
)
files = depset(direct = [ctx.outputs.out])
Expand All @@ -70,25 +75,44 @@ def params_file(
name,
out,
args = [],
data = [],
newline = "auto",
**kwargs):
"""Generates a UTF-8 encoded params file from a list of arguments.
Handles $(location) expansions for arguments.
Handles variable substitutions for args.
Args:
name: Name of the rule.
out: Path of the output file, relative to this package.
args: Arguments to concatenate into a params file.
Subject to $(location) substitutions
newline: one of ["auto", "unix", "windows"]: line endings to use. "auto"
for platform-determined, "unix" for LF, and "windows" for CRLF.
**kwargs: further keyword arguments, e.g. <code>visibility</code>
name: Name of the rule.
out: Path of the output file, relative to this package.
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.
data: Data for $(location) expansions in args.
newline: Line endings to use. One of ["auto", "unix", "windows"].
"auto" for platform-determined
"unix" for LF
"windows" for CRLF
"""
_params_file(
name = name,
out = out,
args = args,
data = data,
newline = newline or "auto",
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
Expand Down

0 comments on commit 34b8cf4

Please sign in to comment.