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")
|
|
|
|
|
2024-04-30 21:19:34 +00:00
|
|
|
def _run_binary_impl(ctx):
|
2019-05-21 12:46:09 +00:00
|
|
|
tool_as_list = [ctx.attr.tool]
|
2019-05-23 18:07:27 +00:00
|
|
|
args = [
|
2024-04-30 21:19:34 +00:00
|
|
|
# Expand $(execpath ...) / $(execpaths ...) / $(location ...) / $(locations ...) in args.
|
2019-05-21 12:46:09 +00:00
|
|
|
#
|
|
|
|
# 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).
|
2024-04-30 21:19:34 +00:00
|
|
|
ctx.expand_location(a, tool_as_list)
|
2019-05-21 12:46:09 +00:00
|
|
|
for a in ctx.attr.args
|
|
|
|
]
|
|
|
|
envs = {
|
2024-04-30 21:19:34 +00:00
|
|
|
# Expand $(execpath ...) / $(execpaths ...) / $(location ...) / $(locations ...) in the values.
|
|
|
|
k: ctx.expand_location(v, tool_as_list)
|
2019-05-21 12:46:09 +00:00
|
|
|
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(
|
2024-04-30 21:19:34 +00:00
|
|
|
implementation = _run_binary_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" +
|
2024-04-30 21:19:34 +00:00
|
|
|
" `$(execpath)` and `$(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 " +
|
2024-04-30 21:19:34 +00:00
|
|
|
" [`$(execpath)` and `$(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" +
|
2024-04-30 21:19:34 +00:00
|
|
|
" `$(execpath)` and `$(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" +
|
2024-04-30 21:19:34 +00:00
|
|
|
" `$(execpath)` and `$(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" +
|
2024-04-30 21:19:34 +00:00
|
|
|
" [`$(execpath)` and `$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" +
|
2019-05-21 12:46:09 +00:00
|
|
|
" expansion.",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|