forked from hermeticbuild/hermetic-llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsh_script.bzl
More file actions
53 lines (46 loc) · 1.46 KB
/
sh_script.bzl
File metadata and controls
53 lines (46 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
load("@bazel_lib//lib:run_binary.bzl", "run_binary")
_SH_TOOLCHAIN_TYPE = Label("@rules_shell//shell:toolchain_type")
def _sh_script_impl(ctx):
shell_path = ctx.toolchains[_SH_TOOLCHAIN_TYPE].path
if not shell_path:
fail("No sh_toolchain.path is set for the execution platform")
ctx.actions.write(
output = ctx.outputs.out,
content = "\n".join([
"#!{}".format(shell_path),
"set -euo pipefail",
ctx.attr.cmd,
"",
]),
is_executable = True,
)
return DefaultInfo(
executable = ctx.outputs.out,
files = depset([ctx.outputs.out]),
)
_sh_script = rule(
implementation = _sh_script_impl,
attrs = {
"cmd": attr.string(mandatory = True),
"out": attr.output(mandatory = True),
},
toolchains = [_SH_TOOLCHAIN_TYPE],
)
def sh_script(name, cmd, env = {}, **kwargs):
_sh_script(
name = name + "_bin",
out = name + ".sh",
cmd = cmd,
)
run_binary(
name = name,
tool = name + "_bin",
env = {
# Some default paths that are likely to be needed for genrule-type scripts.
# We must specify this because otherwise we leak the host's PATH, which may
# not be compatible with the execution platform...
# See https://github.com/bazelbuild/bazel/issues/28065
"PATH": "/usr/bin:/bin:/usr/sbin:/sbin",
} | env,
**kwargs
)