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

     1  """Marshal is a tool for generating marshalling interfaces for Go types."""
     2  
     3  def _go_marshal_impl(ctx):
     4      """Execute the go_marshal tool."""
     5      output = ctx.outputs.lib
     6      output_test = ctx.outputs.test
     7      output_test_unconditional = ctx.outputs.test_unconditional
     8  
     9      # Run the marshal command.
    10      args = ["-output=%s" % output.path]
    11      args.append("-pkg=%s" % ctx.attr.package)
    12      args.append("-output_test=%s" % output_test.path)
    13      args.append("-output_test_unconditional=%s" % output_test_unconditional.path)
    14  
    15      if ctx.attr.debug:
    16          args += ["-debug"]
    17  
    18      args += ["--"]
    19      for src in ctx.attr.srcs:
    20          args += [f.path for f in src.files.to_list()]
    21      ctx.actions.run(
    22          inputs = ctx.files.srcs,
    23          outputs = [output, output_test, output_test_unconditional],
    24          mnemonic = "GoMarshal",
    25          progress_message = "go_marshal: %s" % ctx.label,
    26          arguments = args,
    27          executable = ctx.executable._tool,
    28      )
    29  
    30  # Generates save and restore logic from a set of Go files.
    31  #
    32  # Args:
    33  #   name: the name of the rule.
    34  #   srcs: the input source files. These files should include all structs in the
    35  #         package that need to be saved.
    36  #   imports: an optional list of extra, non-aliased, Go-style absolute import
    37  #            paths.
    38  #   out: the name of the generated file output. This must not conflict with any
    39  #        other files and must be added to the srcs of the relevant go_library.
    40  #   package: the package name for the input sources.
    41  go_marshal = rule(
    42      implementation = _go_marshal_impl,
    43      attrs = {
    44          "srcs": attr.label_list(mandatory = True, allow_files = True),
    45          "imports": attr.string_list(mandatory = False),
    46          "package": attr.string(mandatory = True),
    47          "debug": attr.bool(doc = "enable debugging output from the go_marshal tool"),
    48          "_tool": attr.label(executable = True, cfg = "host", default = Label("//tools/go_marshal:go_marshal")),
    49      },
    50      outputs = {
    51          "lib": "%{name}_unsafe.go",
    52          "test": "%{name}_test.go",
    53          "test_unconditional": "%{name}_unconditional_test.go",
    54      },
    55  )
    56  
    57  # marshal_deps are the dependencies requied by generated code.
    58  marshal_deps = [
    59      "//pkg/gohacks",
    60      "//pkg/hostarch",
    61      "//pkg/marshal",
    62  ]
    63  
    64  # marshal_test_deps are required by test targets.
    65  marshal_test_deps = [
    66      "//tools/go_marshal/analysis",
    67  ]