2022-04-01 03:04:35 +00:00
|
|
|
# Copyright 2019 The Bazel Authors. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""run_binary implementation"""
|
|
|
|
|
|
|
|
load("@bazel_skylib//lib:dicts.bzl", "dicts")
|
|
|
|
load("//lib/private:expand_make_vars.bzl", "expand_locations", "expand_variables")
|
2022-07-15 05:35:00 +00:00
|
|
|
load("//lib:stamping.bzl", "STAMP_ATTRS", "maybe_stamp")
|
2022-04-01 03:04:35 +00:00
|
|
|
|
|
|
|
def _impl(ctx):
|
|
|
|
tool_as_list = [ctx.attr.tool]
|
|
|
|
tool_inputs, tool_input_mfs = ctx.resolve_tools(tools = tool_as_list)
|
|
|
|
args = ctx.actions.args()
|
|
|
|
|
2022-06-16 23:26:29 +00:00
|
|
|
outputs = []
|
|
|
|
outputs.extend(ctx.outputs.outs)
|
|
|
|
for _out_dir in ctx.attr.out_dirs:
|
|
|
|
out_dir = ctx.actions.declare_directory(_out_dir)
|
|
|
|
for output in outputs:
|
|
|
|
if output.path.startswith(out_dir.path + "/"):
|
|
|
|
fail("output {} is nested within output directory {}; outputs cannot be nested within each other!".format(output.path, out_dir.path))
|
|
|
|
if output.is_directory and out_dir.path.startswith(output.path + "/"):
|
|
|
|
fail("output directory {} is nested within output directory {}; outputs cannot be nested within each other!".format(out_dir.path, output.path))
|
|
|
|
outputs.append(out_dir)
|
2022-06-27 19:11:04 +00:00
|
|
|
if len(outputs) < 1:
|
|
|
|
fail("""\
|
|
|
|
ERROR: target {target} is not configured to produce any outputs.
|
|
|
|
|
|
|
|
Bazel only executes actions when their outputs are required, so it's never correct to create an action with no outputs.
|
|
|
|
|
|
|
|
Possible fixes:
|
|
|
|
- Predict what outputs are created, and list them in the outs and out_dirs attributes.
|
|
|
|
- If {rule_kind} is a binary, and you meant to run it for its side-effects,
|
|
|
|
then call it directly with `bazel run` and don't wrap it in a run_binary rule.
|
|
|
|
""".format(
|
|
|
|
target = str(ctx.label),
|
|
|
|
rule_kind = str(ctx.attr.tool.label),
|
|
|
|
))
|
2022-06-16 23:26:29 +00:00
|
|
|
|
2022-04-01 03:04:35 +00:00
|
|
|
# `expand_locations(...).split(" ")` is a work-around https://github.com/bazelbuild/bazel/issues/10309
|
|
|
|
# _expand_locations returns an array of args to support $(execpaths) expansions.
|
|
|
|
# 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
|
|
|
|
for a in ctx.attr.args:
|
2022-06-16 23:26:29 +00:00
|
|
|
args.add_all([expand_variables(ctx, e, outs = outputs) for e in expand_locations(ctx, a, ctx.attr.srcs).split(" ")])
|
2022-04-01 03:04:35 +00:00
|
|
|
envs = {}
|
|
|
|
for k, v in ctx.attr.env.items():
|
2022-06-16 23:26:29 +00:00
|
|
|
envs[k] = " ".join([expand_variables(ctx, e, outs = outputs, attribute_name = "env") for e in expand_locations(ctx, v, ctx.attr.srcs).split(" ")])
|
|
|
|
|
2022-07-15 05:35:00 +00:00
|
|
|
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
|
|
|
|
|
2022-04-01 03:04:35 +00:00
|
|
|
ctx.actions.run(
|
|
|
|
outputs = outputs,
|
2022-07-15 05:35:00 +00:00
|
|
|
inputs = inputs,
|
2022-04-01 03:04:35 +00:00
|
|
|
tools = tool_inputs,
|
|
|
|
executable = ctx.executable.tool,
|
|
|
|
arguments = [args],
|
2022-06-16 23:58:41 +00:00
|
|
|
mnemonic = ctx.attr.mnemonic if ctx.attr.mnemonic else None,
|
|
|
|
progress_message = ctx.attr.progress_message if ctx.attr.progress_message else None,
|
|
|
|
execution_requirements = ctx.attr.execution_requirements if ctx.attr.execution_requirements else None,
|
2022-04-01 03:04:35 +00:00
|
|
|
use_default_shell_env = False,
|
|
|
|
env = dicts.add(ctx.configuration.default_shell_env, envs),
|
|
|
|
input_manifests = tool_input_mfs,
|
|
|
|
)
|
|
|
|
return DefaultInfo(
|
|
|
|
files = depset(outputs),
|
|
|
|
runfiles = ctx.runfiles(files = outputs),
|
|
|
|
)
|
|
|
|
|
2022-06-16 23:26:29 +00:00
|
|
|
_run_binary = rule(
|
2022-04-01 03:04:35 +00:00
|
|
|
implementation = _impl,
|
2022-07-15 05:35:00 +00:00
|
|
|
attrs = dict({
|
2022-04-01 03:04:35 +00:00
|
|
|
"tool": attr.label(
|
|
|
|
executable = True,
|
|
|
|
allow_files = True,
|
|
|
|
mandatory = True,
|
2022-04-14 17:40:39 +00:00
|
|
|
cfg = "exec",
|
2022-04-01 03:04:35 +00:00
|
|
|
),
|
2022-06-16 23:26:29 +00:00
|
|
|
"env": attr.string_dict(),
|
2022-04-01 03:04:35 +00:00
|
|
|
"srcs": attr.label_list(
|
|
|
|
allow_files = True,
|
|
|
|
),
|
2022-06-16 23:26:29 +00:00
|
|
|
"out_dirs": attr.string_list(),
|
|
|
|
"outs": attr.output_list(),
|
|
|
|
"args": attr.string_list(),
|
2022-06-16 23:58:41 +00:00
|
|
|
"mnemonic": attr.string(),
|
|
|
|
"progress_message": attr.string(),
|
|
|
|
"execution_requirements": attr.string_dict(),
|
2022-07-15 05:35:00 +00:00
|
|
|
}, **STAMP_ATTRS),
|
2022-04-01 03:04:35 +00:00
|
|
|
)
|
2022-06-16 23:26:29 +00:00
|
|
|
|
|
|
|
def run_binary(
|
|
|
|
name,
|
|
|
|
tool,
|
|
|
|
srcs = [],
|
|
|
|
args = [],
|
|
|
|
env = {},
|
|
|
|
outs = [],
|
|
|
|
out_dirs = [],
|
2022-06-16 23:58:41 +00:00
|
|
|
mnemonic = "RunBinary",
|
|
|
|
progress_message = None,
|
|
|
|
execution_requirements = None,
|
2022-07-15 16:20:43 +00:00
|
|
|
stamp = 0,
|
2022-06-16 23:26:29 +00:00
|
|
|
# TODO: remove output_dir in 2.x release
|
|
|
|
output_dir = False,
|
|
|
|
**kwargs):
|
|
|
|
"""Runs a binary as a build action.
|
|
|
|
|
|
|
|
This rule does not require Bash (unlike `native.genrule`).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: The target name
|
|
|
|
|
|
|
|
tool: The tool to run in the action.
|
|
|
|
|
|
|
|
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 `$(location)`
|
|
|
|
expansion in `args` and `env`.
|
|
|
|
srcs: Additional inputs of the action.
|
|
|
|
|
|
|
|
These labels are available for `$(location)` expansion in `args` and `env`.
|
|
|
|
|
|
|
|
args: Command line arguments of the binary.
|
|
|
|
|
|
|
|
Subject to `$(location)` and makevar expansions.
|
|
|
|
|
|
|
|
env: Environment variables of the action.
|
|
|
|
|
|
|
|
Subject to `$(location)` and makevar expansions.
|
|
|
|
|
|
|
|
outs: Output files generated by the action.
|
|
|
|
|
|
|
|
These labels are available for `$(location)` expansion in `args` and `env`.
|
|
|
|
|
|
|
|
Output files cannot be nested within output directories in out_dirs.
|
|
|
|
|
|
|
|
out_dirs: Output directories generated by the action.
|
|
|
|
|
|
|
|
These labels are _not_ available for `$(location)` expansion in `args` and `env` sincew
|
|
|
|
they are not pre-declared labels created via attr.output_list(). Output directories are
|
|
|
|
declared instead by `ctx.actions.declare_directory`.
|
|
|
|
|
|
|
|
Output directories cannot be nested within other output directories in out_dirs.
|
|
|
|
|
2022-06-16 23:58:41 +00:00
|
|
|
mnemonic: A one-word description of the action, for example, CppCompile or GoLink.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
execution_requirements: Information for scheduling the action.
|
|
|
|
|
|
|
|
For example,
|
|
|
|
|
|
|
|
```
|
|
|
|
execution_requirements = {
|
|
|
|
"no-cache": "1",
|
|
|
|
},
|
|
|
|
```
|
|
|
|
|
|
|
|
See https://docs.bazel.build/versions/main/be/common-definitions.html#common.tags for useful keys.
|
|
|
|
|
2022-06-16 23:26:29 +00:00
|
|
|
output_dir: If set to True then an output directory named the same as the target name
|
|
|
|
is added to out_dirs.
|
|
|
|
|
|
|
|
Deprecated. For backward compatability with @aspect_bazel_lib 1.x. Use out_dirs instead.
|
|
|
|
|
2022-07-15 05:35:00 +00:00
|
|
|
stamp: Whether to include build status files as inputs to the tool. Possible values:
|
|
|
|
|
2022-07-15 16:20:43 +00:00
|
|
|
- `stamp = 0` (default): Never include build status files as inputs to the tool.
|
|
|
|
This gives good build result caching.
|
|
|
|
Most tools don't use the status files, so including them in `--stamp` builds makes those
|
|
|
|
builds have many needless cache misses.
|
|
|
|
(Note: this default is different from most rules with an integer-typed `stamp` attribute.)
|
2022-07-15 05:35:00 +00:00
|
|
|
- `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 = -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.
|
|
|
|
|
2022-06-16 23:26:29 +00:00
|
|
|
**kwargs: Additional arguments
|
|
|
|
"""
|
|
|
|
_run_binary(
|
|
|
|
name = name,
|
|
|
|
tool = tool,
|
|
|
|
srcs = srcs,
|
|
|
|
args = args,
|
|
|
|
env = env,
|
|
|
|
outs = outs,
|
|
|
|
out_dirs = out_dirs + ([name] if output_dir else []),
|
2022-06-16 23:58:41 +00:00
|
|
|
mnemonic = mnemonic,
|
|
|
|
progress_message = progress_message,
|
|
|
|
execution_requirements = execution_requirements,
|
2022-07-15 05:35:00 +00:00
|
|
|
stamp = stamp,
|
2022-06-16 23:26:29 +00:00
|
|
|
**kwargs
|
|
|
|
)
|