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

     1  """Go rules."""
     2  
     3  load("@bazel_gazelle//:def.bzl", _gazelle = "gazelle")
     4  load("@io_bazel_rules_go//go:def.bzl", "GoLibrary", _go_binary = "go_binary", _go_context = "go_context", _go_embed_data = "go_embed_data", _go_library = "go_library", _go_path = "go_path", _go_test = "go_test")
     5  load("@io_bazel_rules_go//proto:def.bzl", _go_grpc_library = "go_grpc_library", _go_proto_library = "go_proto_library")
     6  load("//tools/bazeldefs:defs.bzl", "select_arch", "select_system")
     7  
     8  gazelle = _gazelle
     9  go_embed_data = _go_embed_data
    10  go_path = _go_path
    11  bazel_worker_proto = "//tools/bazeldefs:worker_protocol_go_proto"
    12  
    13  def _go_proto_or_grpc_library(go_library_func, name, **kwargs):
    14      if "importpath" in kwargs:
    15          # If importpath is explicit, pass straight through.
    16          go_library_func(name = name, **kwargs)
    17          return
    18      deps = [
    19          dep.replace("_proto", "_go_proto")
    20          for dep in (kwargs.pop("deps", []) or [])
    21      ]
    22      go_library_func(
    23          name = name + "_go_proto",
    24          importpath = "gvisor.dev/gvisor/" + native.package_name() + "/" + name + "_go_proto",
    25          proto = ":" + name + "_proto",
    26          deps = deps,
    27          **kwargs
    28      )
    29  
    30  def go_proto_library(name, **kwargs):
    31      _go_proto_or_grpc_library(_go_proto_library, name, **kwargs)
    32  
    33  def go_grpc_and_proto_libraries(name, **kwargs):
    34      _go_proto_or_grpc_library(_go_grpc_library, name, **kwargs)
    35  
    36  def go_binary(name, static = False, pure = False, x_defs = None, system_malloc = False, **kwargs):
    37      """Build a go binary.
    38  
    39      Args:
    40          name: name of the target.
    41          static: build a static binary.
    42          pure: build without cgo.
    43          x_defs: additional definitions.
    44          **kwargs: rest of the arguments are passed to _go_binary.
    45      """
    46      if static:
    47          kwargs["static"] = "on"
    48      if pure:
    49          kwargs["pure"] = "on"
    50      _go_binary(
    51          name = name,
    52          x_defs = x_defs,
    53          **kwargs
    54      )
    55  
    56  def go_importpath(target):
    57      """Returns the importpath for the target."""
    58      return target[GoLibrary].importpath
    59  
    60  def go_library(name, arch_deps = [], **kwargs):
    61      _go_library(
    62          name = name,
    63          importpath = "gvisor.dev/gvisor/" + native.package_name(),
    64          **kwargs
    65      )
    66  
    67  def go_test(name, pure = False, library = None, **kwargs):
    68      """Build a go test.
    69  
    70      Args:
    71          name: name of the output binary.
    72          pure: should it be built without cgo.
    73          library: the library to embed.
    74          **kwargs: rest of the arguments to pass to _go_test.
    75      """
    76      if pure:
    77          kwargs["pure"] = "on"
    78      if library:
    79          kwargs["embed"] = [library]
    80      _go_test(
    81          name = name,
    82          **kwargs
    83      )
    84  
    85  def go_rule(rule, implementation, **kwargs):
    86      """Wraps a rule definition with Go attributes.
    87  
    88      Args:
    89        rule: rule function (typically rule or aspect).
    90        implementation: implementation function.
    91        **kwargs: other arguments to pass to rule.
    92  
    93      Returns:
    94          The result of invoking the rule.
    95      """
    96      attrs = kwargs.pop("attrs", dict())
    97      attrs["_go_context_data"] = attr.label(default = "@io_bazel_rules_go//:go_context_data")
    98      attrs["_stdlib"] = attr.label(default = "@io_bazel_rules_go//:stdlib")
    99      toolchains = kwargs.get("toolchains", []) + ["@io_bazel_rules_go//go:toolchain"]
   100      return rule(implementation, attrs = attrs, toolchains = toolchains, **kwargs)
   101  
   102  def go_embed_libraries(target):
   103      if hasattr(target.attr, "embed"):
   104          return target.attr.embed
   105      return []
   106  
   107  def go_context(ctx, goos = None, goarch = None, std = False):
   108      """Extracts a standard Go context struct.
   109  
   110      Args:
   111        ctx: the starlark context (required).
   112        goos: the GOOS value.
   113        goarch: the GOARCH value.
   114        std: ignored.
   115  
   116      Returns:
   117        A context Go struct with pointers to Go toolchain components.
   118      """
   119  
   120      # We don't change anything for the standard library analysis. All Go files
   121      # are available in all instances. Note that this includes the standard
   122      # library sources, which are analyzed by nogo.
   123      go_ctx = _go_context(ctx)
   124      if goos == None:
   125          goos = go_ctx.sdk.goos
   126      elif goos != go_ctx.sdk.goos:
   127          fail("Internal GOOS (%s) doesn't match GoSdk GOOS (%s)." % (goos, go_ctx.sdk.goos))
   128      if goarch == None:
   129          goarch = go_ctx.sdk.goarch
   130      elif goarch != go_ctx.sdk.goarch:
   131          fail("Internal GOARCH (%s) doesn't match GoSdk GOARCH (%s)." % (goarch, go_ctx.sdk.goarch))
   132      return struct(
   133          go = go_ctx.go,
   134          env = go_ctx.env,
   135          nogo_args = [],
   136          stdlib_srcs = go_ctx.sdk.srcs,
   137          runfiles = depset([go_ctx.go] + go_ctx.sdk.srcs + go_ctx.sdk.tools + go_ctx.stdlib.libs),
   138          goos = go_ctx.sdk.goos,
   139          goarch = go_ctx.sdk.goarch,
   140          gotags = go_ctx.tags,
   141      )
   142  
   143  def select_goarch():
   144      return select_arch(arm64 = "arm64", amd64 = "amd64")
   145  
   146  def select_goos():
   147      return select_system(linux = "linux")