@@ -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+
4440def _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