github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/bazeldefs/defs.bzl (about)

     1  """Meta and miscellaneous rules."""
     2  
     3  load("@bazel_skylib//rules:build_test.bzl", _build_test = "build_test")
     4  load("@bazel_skylib//:bzl_library.bzl", _bzl_library = "bzl_library")
     5  
     6  build_test = _build_test
     7  bzl_library = _bzl_library
     8  more_shards = 4
     9  most_shards = 8
    10  version = "//tools/bazeldefs:version"
    11  
    12  def short_path(path):
    13      return path
    14  
    15  def proto_library(name, has_services = None, **kwargs):
    16      native.proto_library(
    17          name = name,
    18          **kwargs
    19      )
    20  
    21  def select_arch(amd64 = "amd64", arm64 = "arm64", default = None, **kwargs):
    22      values = {
    23          "@bazel_tools//src/conditions:linux_x86_64": amd64,
    24          "@bazel_tools//src/conditions:linux_aarch64": arm64,
    25      }
    26      if default:
    27          values["//conditions:default"] = default
    28      return select(values, **kwargs)
    29  
    30  def select_system(linux = ["__linux__"], **kwargs):
    31      return linux  # Only Linux supported.
    32  
    33  def default_installer():
    34      return None
    35  
    36  def default_net_util():
    37      return []  # Nothing needed.
    38  
    39  def coreutil():
    40      return []  # Nothing needed.
    41  
    42  def select_native_vs_cross(native = [], amd64 = [], arm64 = [], cross = []):
    43      values = {
    44          "//tools/bazeldefs:linux_arm64_cross": arm64 + cross,
    45          "//tools/bazeldefs:linux_amd64_cross": amd64 + cross,
    46          "//conditions:default": native,
    47      }
    48      return select(values)
    49  
    50  def arch_genrule(name, srcs, outs, cmd, tools):
    51      """Runs a gen command on the target architecture.
    52  
    53      If the target architecture isn't match the host architecture, it will build
    54      a command for the target architecture and run it via qemu.
    55  
    56      The native genrule runs the command on the host architecture.
    57  
    58      Args:
    59       name: name of generated target.
    60       srcs: A list of inputs for this rule.
    61       cmd: The command to run. It has to contain " QEMU " before executed binaries.
    62       outs: A list of files generated by this rule.
    63       tools: A list of tool dependencies for this rule.
    64      """
    65      qemu_arm64 = "qemu-aarch64-static"
    66      qemu_amd64 = "qemu-x86_64-static"
    67      srcs = select_native_vs_cross(
    68          cross = srcs + tools,
    69          native = srcs,
    70      )
    71      tools = select_native_vs_cross(
    72          cross = [],
    73          native = tools,
    74      )
    75      cmd = select_native_vs_cross(
    76          arm64 = cmd.replace("QEMU", qemu_arm64),
    77          amd64 = cmd.replace("QEMU", qemu_amd64),
    78          native = cmd.replace("QEMU", ""),
    79          cross = "",
    80      )
    81      native.genrule(name = name, srcs = srcs, outs = outs, cmd = cmd, tools = tools)