Skip to content

Commit e59f67b

Browse files
committed
Support passing arguments to run_binary via param file
Allows to use run_binary in the case when the size of the command line can grow longer than the maximum size allowed by the system.
1 parent e60cf00 commit e59f67b

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

docs/run_binary_doc.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Runs a binary as a build action. This rule does not require Bash (unlike native.
1111
## run_binary
1212

1313
<pre>
14-
run_binary(<a href="#run_binary-name">name</a>, <a href="#run_binary-args">args</a>, <a href="#run_binary-env">env</a>, <a href="#run_binary-outs">outs</a>, <a href="#run_binary-srcs">srcs</a>, <a href="#run_binary-tool">tool</a>)
14+
run_binary(<a href="#run_binary-name">name</a>, <a href="#run_binary-args">args</a>, <a href="#run_binary-env">env</a>, <a href="#run_binary-outs">outs</a>, <a href="#run_binary-srcs">srcs</a>, <a href="#run_binary-tool">tool</a>, <a href="#run_binary-param_file_format">param_file_format</a>)
1515
</pre>
1616

1717
Runs a binary as a build action.
@@ -29,5 +29,6 @@ This rule does not require Bash (unlike `native.genrule`).
2929
| <a id="run_binary-outs"></a>outs | Output files generated by the action.<br><br>These labels are available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | List of labels | required | |
3030
| <a id="run_binary-srcs"></a>srcs | Additional inputs of the action.<br><br>These labels are available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
3131
| <a id="run_binary-tool"></a>tool | The tool to run in the action.<br><br>Must be the label of a *_binary rule, of a rule that generates an executable file, or of a file that can be executed as a subprocess (e.g. an .exe or .bat file on Windows or a binary with executable permission on Linux). This label is available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
32+
| <a id="run_binary-param_file_format"></a>param_file_format | If provided, Bazel can pass command-line arguments to the tool via file.<br><br>Should be used when the size of the command line can grow longer than the maximum size allowed by the system. The format is the same as the format of <code>param_file_arg</code> in [`Args.use_param_file`](https://bazel.build/rules/lib/builtins/Args#use_param_file). The <code>tool</code> has to support reading arguments from the file for this to work. | <a href="https://bazel.build/rules/lib/string">String</a> | optional | <code>""</code> |
3233

3334

rules/run_binary.bzl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ load("//lib:dicts.bzl", "dicts")
2222

2323
def _impl(ctx):
2424
tool_as_list = [ctx.attr.tool]
25-
args = [
25+
26+
args = ctx.actions.args()
27+
if ctx.attr.param_file_format != "":
28+
args.set_param_file_format("multiline")
29+
args.use_param_file(param_file_arg = ctx.attr.param_file_format, use_always = False)
30+
31+
for arg in ctx.attr.args:
2632
# Expand $(location) / $(locations) in args.
2733
#
2834
# To keep the rule simple, do not expand Make Variables (like *_binary.args usually would).
@@ -33,9 +39,8 @@ def _impl(ctx):
3339
# tokenization they would have to write args=["'a b'"] or args=["a\\ b"]. There's no
3440
# documented tokenization function anyway (as of 2019-05-21 ctx.tokenize exists but is
3541
# undocumented, see https://github.com/bazelbuild/bazel/issues/8389).
36-
ctx.expand_location(a, tool_as_list) if "$(location" in a else a
37-
for a in ctx.attr.args
38-
]
42+
args.add(ctx.expand_location(arg, tool_as_list) if "$(location" in arg else arg)
43+
3944
envs = {
4045
# Expand $(location) / $(locations) in the values.
4146
k: ctx.expand_location(v, tool_as_list) if "$(location" in v else v
@@ -46,7 +51,7 @@ def _impl(ctx):
4651
inputs = ctx.files.srcs,
4752
tools = [ctx.executable.tool],
4853
executable = ctx.executable.tool,
49-
arguments = args,
54+
arguments = [args],
5055
mnemonic = "RunBinary",
5156
use_default_shell_env = False,
5257
env = dicts.add(ctx.configuration.default_shell_env, envs),
@@ -92,5 +97,13 @@ run_binary = rule(
9297
" [`$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" +
9398
" expansion.",
9499
),
100+
"param_file_format": attr.string(
101+
doc = "If provided, Bazel can pass command-line arguments to the tool via file. " +
102+
"Should be used when the size of the command line can grow longer than the " +
103+
"maximum size allowed by the system. The format is the same as the format of " +
104+
"`param_file_arg` in [`param_file_arg`](https://bazel.build/rules/lib/builtins/Args#use_param_file). " +
105+
"The `tool` has to support reading arguments from the file for this to work.",
106+
default = "",
107+
),
95108
},
96109
)

0 commit comments

Comments
 (0)