github.com/prysmaticlabs/prysm@v1.4.4/tools/ssz.bzl (about)

     1  load(
     2      "@io_bazel_rules_go//go:def.bzl",
     3      "GoLibrary",
     4      "GoSource",
     5  )
     6  
     7  def _ssz_go_proto_library_impl(ctx):
     8      if ctx.attr.go_proto != None:
     9          go_proto = ctx.attr.go_proto
    10          input_files = go_proto[OutputGroupInfo].go_generated_srcs.to_list()
    11          package_path = input_files[0].dirname
    12      elif hasattr(ctx.attr, "srcs") and len(ctx.attr.srcs) > 0:
    13          package_path = ctx.attr.srcs[0].files.to_list()[0].dirname
    14          input_files = ctx.attr.srcs[0].files.to_list()
    15      else:
    16          fail("Must have go_proto or srcs")
    17  
    18      # Run the tool.
    19      output = ctx.outputs.out
    20      args = [
    21          "--output=%s" % output.path,
    22          "--path=%s" % package_path,
    23      ]
    24      if hasattr(ctx.attr, "includes") and len(ctx.attr.includes) > 0:
    25          incs = []
    26          for include in ctx.attr.includes:
    27              incs.append(include[GoSource].srcs[0].dirname)
    28              input_files += include[GoSource].srcs
    29          args.append("--include=%s" % ",".join(incs))
    30  
    31      if len(ctx.attr.objs) > 0:
    32          args += ["--objs=%s" % ",".join(ctx.attr.objs)]
    33  
    34      ctx.actions.run(
    35          executable = ctx.executable.sszgen,
    36          progress_message = "Generating ssz marshal and unmarshal functions",
    37          inputs = input_files,
    38          arguments = args,
    39          outputs = [output],
    40      )
    41  
    42  """
    43  A rule that uses the generated pb.go files from a go_proto_library target to generate SSZ marshal
    44  and unmarshal functions as pointer receivers on the specified objects. To use this rule, provide a
    45  go_proto_library target and specify the structs to generate methods in the "objs" field. Lastly,
    46  include your new target as a source for the go_library that embeds the go_proto_library.
    47  Example:
    48  go_proto_library(
    49    name = "example_go_proto",
    50     ...
    51  )
    52  ssz_gen_marshal(
    53    name = "ssz_generated_sources",
    54    go_proto = ":example_go_proto",
    55    objs = [ # omit this field to generate for all structs in the package.
    56      "AddressBook",
    57      "Person",
    58    ],
    59  )
    60  go_library(
    61    name = "go_default_library",
    62    srcs = [":ssz_generated_sources"],
    63    embed = [":example_go_proto"],
    64    deps = SSZ_DEPS,
    65  )
    66  """
    67  ssz_gen_marshal = rule(
    68      implementation = _ssz_go_proto_library_impl,
    69      attrs = {
    70          "srcs": attr.label_list(allow_files = True),
    71          "go_proto": attr.label(providers = [GoLibrary]),
    72          "sszgen": attr.label(
    73              default = Label("@com_github_ferranbt_fastssz//sszgen:sszgen"),
    74              executable = True,
    75              cfg = "host",
    76          ),
    77          "objs": attr.string_list(),
    78          "includes": attr.label_list(providers = [GoLibrary]),
    79      },
    80      outputs = {"out": "generated.ssz.go"},
    81  )
    82  
    83  SSZ_DEPS = ["@com_github_ferranbt_fastssz//:go_default_library"]