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