feat: add stamping ability to run_binary

Fixes #185
This commit is contained in:
Alex Eagle 2022-07-14 22:35:00 -07:00 committed by Greg Magolan
parent de081fb72e
commit 56bc408d94
2 changed files with 35 additions and 4 deletions

3
docs/run_binary.md generated
View File

@ -11,7 +11,7 @@ This fork of bazel-skylib's run_binary adds directory output support and better
<pre>
run_binary(<a href="#run_binary-name">name</a>, <a href="#run_binary-tool">tool</a>, <a href="#run_binary-srcs">srcs</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-out_dirs">out_dirs</a>, <a href="#run_binary-mnemonic">mnemonic</a>, <a href="#run_binary-progress_message">progress_message</a>,
<a href="#run_binary-execution_requirements">execution_requirements</a>, <a href="#run_binary-output_dir">output_dir</a>, <a href="#run_binary-kwargs">kwargs</a>)
<a href="#run_binary-execution_requirements">execution_requirements</a>, <a href="#run_binary-stamp">stamp</a>, <a href="#run_binary-output_dir">output_dir</a>, <a href="#run_binary-kwargs">kwargs</a>)
</pre>
Runs a binary as a build action.
@ -34,6 +34,7 @@ This rule does not require Bash (unlike `native.genrule`).
| <a id="run_binary-mnemonic"></a>mnemonic | A one-word description of the action, for example, CppCompile or GoLink. | <code>"RunBinary"</code> |
| <a id="run_binary-progress_message"></a>progress_message | Progress message to show to the user during the build, for example, "Compiling foo.cc to create foo.o". The message may contain %{label}, %{input}, or %{output} patterns, which are substituted with label string, first input, or output's path, respectively. Prefer to use patterns instead of static strings, because the former are more efficient. | <code>None</code> |
| <a id="run_binary-execution_requirements"></a>execution_requirements | Information for scheduling the action.<br><br>For example,<br><br><pre><code> execution_requirements = { "no-cache": "1", }, </code></pre><br><br>See https://docs.bazel.build/versions/main/be/common-definitions.html#common.tags for useful keys. | <code>None</code> |
| <a id="run_binary-stamp"></a>stamp | Whether to include build status files as inputs to the tool. Possible values:<br><br>- <code>stamp = 1</code>: Always include build status files as inputs to the tool, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it is non-deterministic. It potentially causes remote cache misses for the target and any downstream actions that depend on the result. - <code>stamp = 0</code>: Never include build status files as inputs to the tool. This gives good build result caching. - <code>stamp = -1</code>: Inclusion of build status files as inputs is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag. Stamped targets are not rebuilt unless their dependencies change.<br><br>When stamping is enabled, an additional two environment variables will be set for the action: - <code>BAZEL_STABLE_STATUS_FILE</code> - <code>BAZEL_VOLATILE_STATUS_FILE</code><br><br>These files can be read and parsed by the action, for example to pass some values to a linker. | <code>-1</code> |
| <a id="run_binary-output_dir"></a>output_dir | If set to True then an output directory named the same as the target name is added to out_dirs.<br><br>Deprecated. For backward compatability with @aspect_bazel_lib 1.x. Use out_dirs instead. | <code>False</code> |
| <a id="run_binary-kwargs"></a>kwargs | Additional arguments | none |

View File

@ -16,6 +16,7 @@
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("//lib/private:expand_make_vars.bzl", "expand_locations", "expand_variables")
load("//lib:stamping.bzl", "STAMP_ATTRS", "maybe_stamp")
def _impl(ctx):
tool_as_list = [ctx.attr.tool]
@ -57,9 +58,17 @@ Possible fixes:
for k, v in ctx.attr.env.items():
envs[k] = " ".join([expand_variables(ctx, e, outs = outputs, attribute_name = "env") for e in expand_locations(ctx, v, ctx.attr.srcs).split(" ")])
stamp = maybe_stamp(ctx)
if stamp:
inputs = ctx.files.srcs + [stamp.volatile_status_file, stamp.stable_status_file]
envs["BAZEL_STABLE_STATUS_FILE"] = stamp.stable_status_file.path
envs["BAZEL_VOLATILE_STATUS_FILE"] = stamp.volatile_status_file.path
else:
inputs = ctx.files.srcs
ctx.actions.run(
outputs = outputs,
inputs = ctx.files.srcs,
inputs = inputs,
tools = tool_inputs,
executable = ctx.executable.tool,
arguments = [args],
@ -77,7 +86,7 @@ Possible fixes:
_run_binary = rule(
implementation = _impl,
attrs = {
attrs = dict({
"tool": attr.label(
executable = True,
allow_files = True,
@ -94,7 +103,7 @@ _run_binary = rule(
"mnemonic": attr.string(),
"progress_message": attr.string(),
"execution_requirements": attr.string_dict(),
},
}, **STAMP_ATTRS),
)
def run_binary(
@ -108,6 +117,7 @@ def run_binary(
mnemonic = "RunBinary",
progress_message = None,
execution_requirements = None,
stamp = -1,
# TODO: remove output_dir in 2.x release
output_dir = False,
**kwargs):
@ -175,6 +185,25 @@ def run_binary(
Deprecated. For backward compatability with @aspect_bazel_lib 1.x. Use out_dirs instead.
stamp: Whether to include build status files as inputs to the tool. Possible values:
- `stamp = 1`: Always include build status files as inputs to the tool, even in
[--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds.
This setting should be avoided, since it is non-deterministic.
It potentially causes remote cache misses for the target and
any downstream actions that depend on the result.
- `stamp = 0`: Never include build status files as inputs to the tool.
This gives good build result caching.
- `stamp = -1`: Inclusion of build status files as inputs is controlled by the
[--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
Stamped targets are not rebuilt unless their dependencies change.
When stamping is enabled, an additional two environment variables will be set for the action:
- `BAZEL_STABLE_STATUS_FILE`
- `BAZEL_VOLATILE_STATUS_FILE`
These files can be read and parsed by the action, for example to pass some values to a linker.
**kwargs: Additional arguments
"""
_run_binary(
@ -188,5 +217,6 @@ def run_binary(
mnemonic = mnemonic,
progress_message = progress_message,
execution_requirements = execution_requirements,
stamp = stamp,
**kwargs
)