gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/embeddedbinary/defs.bzl (about)

     1  """BUILD rule for embedded binaries."""
     2  
     3  load("//tools:defs.bzl", "go_library")
     4  load("//tools/go_generics:defs.bzl", "go_template_instance")
     5  
     6  _EMBEDDED_BINARY_TEMPLATE = "//tools/embeddedbinary:embeddedbinary_template"
     7  _FLATECOMPRESS = "//tools/embeddedbinary:flatecompress"
     8  
     9  def embedded_binary_go_library(
    10          name,
    11          binary,
    12          binary_name = None,
    13          out = None,
    14          go_package_name = None,
    15          visibility = None):
    16      """Embed a binary and generate a go_library target that can execute it.
    17  
    18      The binary will be compressed, and needs temporary space to be available
    19      when executing.
    20  
    21      Args:
    22          name: The name of the go_library rule.
    23          binary: Binary BUILD target that should be embedded.
    24          binary_name: The name (i.e. typical argv[0]) of the binary being
    25            embedded, defaults to `name`.
    26          out: Output filename of the go_library rule, defaults to `name + ".go"`.
    27          go_package_name: Package name of the go_library, defaults to `name`.
    28          visibility: Visibility of the go_library rule.
    29      """
    30      if binary_name == None:
    31          binary_name = name
    32      if out == None:
    33          out = name + ".go"
    34      if go_package_name == None:
    35          go_package_name = name
    36      compressed_binary = binary_name + ".flate"
    37      uncompressed_binary = binary_name + ".bin"
    38      native.genrule(
    39          name = name + "_flate",
    40          outs = [compressed_binary],
    41          cmd = "$(location %s) < $(SRCS) > $(OUTS)" % (_FLATECOMPRESS,),
    42          srcs = [binary],
    43          tools = [_FLATECOMPRESS],
    44      )
    45      native.genrule(
    46          name = name + "_noflate",
    47          outs = [uncompressed_binary],
    48          cmd = "cat < $(SRCS) > $(OUTS)",
    49          srcs = [binary],
    50      )
    51      go_template_instance(
    52          name = name + "_lib",
    53          template = _EMBEDDED_BINARY_TEMPLATE,
    54          package = go_package_name,
    55          out = out,
    56          substrs = select({
    57              "//tools/embeddedbinary:compilation_mode_opt": {
    58                  "embedded.bin.name": binary_name,
    59                  "//go:embed embedded.bin.flate": "//go:embed %s" % (compressed_binary,),
    60              },
    61              "//conditions:default": {
    62                  "embedded.bin.name": binary_name,
    63                  "//go:embed embedded.bin.flate": "//go:embed %s" % (uncompressed_binary,),
    64                  "flate.NewReader": "io.Reader",
    65              },
    66          }),
    67      )
    68      go_library(
    69          name = name,
    70          srcs = [out],
    71          embedsrcs = select({
    72              "//tools/embeddedbinary:compilation_mode_opt": [compressed_binary],
    73              "//conditions:default": [uncompressed_binary],
    74          }),
    75          deps = ["@org_golang_x_sys//unix:go_default_library"],
    76          visibility = visibility,
    77      )