2019-05-21 12:46:09 +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() build rule implementation.
|
|
|
|
|
|
|
|
Runs a binary as a build action. This rule does not require Bash (unlike native.genrule()).
|
|
|
|
"""
|
|
|
|
|
|
|
|
load("//lib:dicts.bzl", "dicts")
|
|
|
|
|
|
|
|
def _impl(ctx):
|
|
|
|
tool_as_list = [ctx.attr.tool]
|
2019-05-23 18:07:27 +00:00
|
|
|
args = [
|
2019-05-21 12:46:09 +00:00
|
|
|
# Expand $(location) / $(locations) in args.
|
|
|
|
#
|
|
|
|
# To keep the rule simple, do not expand Make Variables (like *_binary.args usually would).
|
|
|
|
# (We can add this feature later if users ask for it.)
|
|
|
|
#
|
|
|
|
# Also for simple implementation and usage, do not Bash-tokenize the arguments. Without
|
|
|
|
# tokenization the user can write args=["a b"] to pass (a b) as one argument, but with
|
|
|
|
# tokenization they would have to write args=["'a b'"] or args=["a\\ b"]. There's no
|
|
|
|
# documented tokenization function anyway (as of 2019-05-21 ctx.tokenize exists but is
|
|
|
|
# undocumented, see https://github.com/bazelbuild/bazel/issues/8389).
|
|
|
|
ctx.expand_location(a, tool_as_list) if "$(location" in a else a
|
|
|
|
for a in ctx.attr.args
|
|
|
|
]
|
|
|
|
envs = {
|
|
|
|
# Expand $(location) / $(locations) in the values.
|
|
|
|
k: ctx.expand_location(v, tool_as_list) if "$(location" in v else v
|
|
|
|
for k, v in ctx.attr.env.items()
|
|
|
|
}
|
|
|
|
ctx.actions.run(
|
|
|
|
outputs = ctx.outputs.outs,
|
2019-05-23 18:07:27 +00:00
|
|
|
inputs = ctx.files.srcs,
|
2023-08-28 18:39:43 +00:00
|
|
|
tools = [ctx.executable.tool],
|
2019-05-21 12:46:09 +00:00
|
|
|
executable = ctx.executable.tool,
|
|
|
|
arguments = args,
|
|
|
|
mnemonic = "RunBinary",
|
|
|
|
use_default_shell_env = False,
|
|
|
|
env = dicts.add(ctx.configuration.default_shell_env, envs),
|
|
|
|
)
|
|
|
|
return DefaultInfo(
|
2020-02-03 15:45:44 +00:00
|
|
|
files = depset(ctx.outputs.outs),
|
2019-05-21 12:46:09 +00:00
|
|
|
runfiles = ctx.runfiles(files = ctx.outputs.outs),
|
|
|
|
)
|
|
|
|
|
|
|
|
run_binary = rule(
|
|
|
|
implementation = _impl,
|
2022-10-03 09:13:46 +00:00
|
|
|
doc = "Runs a binary as a build action.\n\nThis rule does not require Bash (unlike" +
|
|
|
|
" `native.genrule`).",
|
2019-05-21 12:46:09 +00:00
|
|
|
attrs = {
|
|
|
|
"tool": attr.label(
|
2022-10-03 09:13:46 +00:00
|
|
|
doc = "The tool to run in the action.\n\nMust be the label of a *_binary rule," +
|
2019-05-21 12:46:09 +00:00
|
|
|
" 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" +
|
2022-10-03 09:13:46 +00:00
|
|
|
" `$(location)` expansion in `args` and `env`.",
|
2019-05-21 12:46:09 +00:00
|
|
|
executable = True,
|
|
|
|
allow_files = True,
|
|
|
|
mandatory = True,
|
2022-04-29 17:44:59 +00:00
|
|
|
cfg = "exec",
|
2019-05-21 12:46:09 +00:00
|
|
|
),
|
|
|
|
"env": attr.string_dict(
|
2022-10-03 09:13:46 +00:00
|
|
|
doc = "Environment variables of the action.\n\nSubject to " +
|
|
|
|
" [`$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" +
|
2019-05-21 12:46:09 +00:00
|
|
|
" expansion.",
|
|
|
|
),
|
|
|
|
"srcs": attr.label_list(
|
|
|
|
allow_files = True,
|
2022-10-03 09:13:46 +00:00
|
|
|
doc = "Additional inputs of the action.\n\nThese labels are available for" +
|
|
|
|
" `$(location)` expansion in `args` and `env`.",
|
2019-05-21 12:46:09 +00:00
|
|
|
),
|
|
|
|
"outs": attr.output_list(
|
|
|
|
mandatory = True,
|
2022-10-03 09:13:46 +00:00
|
|
|
doc = "Output files generated by the action.\n\nThese labels are available for" +
|
|
|
|
" `$(location)` expansion in `args` and `env`.",
|
2019-05-21 12:46:09 +00:00
|
|
|
),
|
|
|
|
"args": attr.string_list(
|
2022-10-03 09:13:46 +00:00
|
|
|
doc = "Command line arguments of the binary.\n\nSubject to" +
|
|
|
|
" [`$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" +
|
2019-05-21 12:46:09 +00:00
|
|
|
" expansion.",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|