| 1 |
""" |
| 2 |
Internal tools to migrate shell commands to Bazel as an intermediate step |
| 3 |
to wider Bazelification. |
| 4 |
""" |
| 5 |
|
| 6 |
def inline_sh_binary( |
| 7 |
name, |
| 8 |
srcs = [], |
| 9 |
tools = [], |
| 10 |
deps = [], |
| 11 |
cmd = "", |
| 12 |
**kwargs): |
| 13 |
"""Bazel rule to wrap up an inline bash script in a binary. |
| 14 |
|
| 15 |
This is most useful as a stop-gap solution for migrating off Autotools. |
| 16 |
These binaries are likely to be non-hermetic, with implicit system |
| 17 |
dependencies. |
| 18 |
|
| 19 |
NOTE: the rule is only an internal workaround. The interface may change and |
| 20 |
the rule may be removed when everything is properly "Bazelified". |
| 21 |
|
| 22 |
Args: |
| 23 |
name: the name of the inline_sh_binary. |
| 24 |
srcs: the files used directly by the script. |
| 25 |
tools: the executable tools used directly by the script. Any target used |
| 26 |
with rootpath/execpath/location must be declared here or in `srcs`. |
| 27 |
deps: a list of dependency labels that are required to run this binary. |
| 28 |
cmd: the inline sh command to run. |
| 29 |
**kwargs: other keyword arguments that are passed to sh_binary. |
| 30 |
""" |
| 31 |
|
| 32 |
native.genrule( |
| 33 |
name = name + "_genrule", |
| 34 |
srcs = srcs, |
| 35 |
tools = tools, |
| 36 |
outs = [name + ".sh"], |
| 37 |
cmd = "cat <<'EOF' >$(OUTS)\n#!/bin/bash -exu\n%s\nEOF\n" % cmd, |
| 38 |
visibility = ["//visibility:private"], |
| 39 |
tags = kwargs["tags"] if "tags" in kwargs else None, |
| 40 |
target_compatible_with = kwargs["target_compatible_with"] if "target_compatible_with" in kwargs else None, |
| 41 |
testonly = kwargs["testonly"] if "testonly" in kwargs else None, |
| 42 |
) |
| 43 |
|
| 44 |
native.sh_binary( |
| 45 |
name = name, |
| 46 |
srcs = [name + "_genrule"], |
| 47 |
data = srcs + tools + deps, |
| 48 |
**kwargs |
| 49 |
) |
| 50 |
|
| 51 |
def inline_sh_test( |
| 52 |
name, |
| 53 |
srcs = [], |
| 54 |
tools = [], |
| 55 |
deps = [], |
| 56 |
cmd = "", |
| 57 |
**kwargs): |
| 58 |
"""Bazel rule to wrap up an inline bash script in a test. |
| 59 |
|
| 60 |
This is most useful as a stop-gap solution for migrating off Autotools. |
| 61 |
These tests are likely to be non-hermetic, with implicit system dependencies. |
| 62 |
|
| 63 |
NOTE: the rule is only an internal workaround. The interface may change and |
| 64 |
the rule may be removed when everything is properly "Bazelified". |
| 65 |
|
| 66 |
Args: |
| 67 |
name: the name of the inline_sh_binary. |
| 68 |
srcs: the files used directly by the script. |
| 69 |
tools: the executable tools used directly by the script. Any target used |
| 70 |
with rootpath/execpath/location must be declared here or in `srcs`. |
| 71 |
deps: a list of dependency labels that are required to run this binary. |
| 72 |
cmd: the inline sh command to run. |
| 73 |
**kwargs: other keyword arguments that are passed to sh_binary. |
| 74 |
https://bazel.build/reference/be/common-definitions#common-attributes) |
| 75 |
""" |
| 76 |
|
| 77 |
native.genrule( |
| 78 |
name = name + "_genrule", |
| 79 |
srcs = srcs, |
| 80 |
tools = tools, |
| 81 |
outs = [name + ".sh"], |
| 82 |
cmd = "cat <<'EOF' >$(OUTS)\n#!/bin/bash -exu\n%s\nEOF\n" % cmd, |
| 83 |
visibility = ["//visibility:private"], |
| 84 |
tags = kwargs["tags"] if "tags" in kwargs else None, |
| 85 |
target_compatible_with = kwargs["target_compatible_with"] if "target_compatible_with" in kwargs else None, |
| 86 |
testonly = kwargs["testonly"] if "testonly" in kwargs else None, |
| 87 |
) |
| 88 |
|
| 89 |
native.sh_test( |
| 90 |
name = name, |
| 91 |
srcs = [name + "_genrule"], |
| 92 |
data = srcs + tools + deps, |
| 93 |
**kwargs |
| 94 |
) |