Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support modern location expansions for run_binary #490

Merged
merged 8 commits into from
Apr 30, 2024
20 changes: 18 additions & 2 deletions rules/run_binary.bzl
Expand Up @@ -20,6 +20,22 @@ Runs a binary as a build action. This rule does not require Bash (unlike native.

load("//lib:dicts.bzl", "dicts")

_NOOPS = ("$(rlocationpath", "$(rootpath")
_EXPANDS = ("$(execpath", "$(location")

def _is_expandable(value):
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
for noop in _NOOPS:
if noop in value:
# buildifier: disable=print
print("The `{}` expansion value is unused by `run_binary`. It's use will not be expanded.".format(
noop,
))
for prefix in _EXPANDS:
if prefix in value:
return True

return False

def _impl(ctx):
tool_as_list = [ctx.attr.tool]
args = [
Expand All @@ -33,12 +49,12 @@ def _impl(ctx):
# 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
ctx.expand_location(a, tool_as_list) if _is_expandable("$(location") else a
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
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
k: ctx.expand_location(v, tool_as_list) if _is_expandable(v) else v
for k, v in ctx.attr.env.items()
}
ctx.actions.run(
Expand Down