constellation/bazel/proto/rules.bzl
renovate[bot] 22d093cc6f
deps: update bazel (core) (#3581)
* deps: update bazel (core)

* bazel: depset -> list

To comply with some breaking changes in rules_go v0.51, we explicitly
need to type-cast the depsets to lists here.

* bazel: migrate deprecated GoLibrary usage

In rules_go v0.51.0, `GoLibrary` was deprecated and replaced by
`GoInfo`. This adjusts our `protoc-gen-go` rule to use the new `GoInfo`.

* deps: tidy all modules

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Moritz Sanft <58110325+msanft@users.noreply.github.com>
Co-authored-by: edgelessci <edgelessci@users.noreply.github.com>
2025-01-20 15:17:05 +01:00

86 lines
2.7 KiB
Python

"""
Rules for generating Go source files from proto files.
based on https://github.com/bazelbuild/rules_go/issues/2111#issuecomment-1355927231
"""
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@io_bazel_rules_go//go:def.bzl", "GoInfo")
load("@io_bazel_rules_go//proto:compiler.bzl", "GoProtoCompiler")
def _output_go_library_srcs_impl(ctx):
srcs_of_library = []
importpath = ""
for src in ctx.attr.deps:
lib = src[GoInfo]
if importpath and lib.importpath != importpath:
fail(
"importpath of all deps must match, got {} and {}",
importpath,
lib.importpath,
)
importpath = lib.importpath
srcs_of_library.extend(lib.srcs)
if len(srcs_of_library) != 1:
fail("expected exactly one src for library, got {}", len(srcs_of_library))
if not ctx.attr.out:
fail("must specify out for now")
# Run a command to copy the src file to the out location.
_copy(ctx, srcs_of_library[0], ctx.outputs.out)
def _copy(ctx, in_file, out_file):
# based on https://github.com/bazelbuild/examples/blob/main/rules/shell_command/rules.bzl
ctx.actions.run_shell(
# Input files visible to the action.
inputs = [in_file],
# Output files that must be created by the action.
outputs = [out_file],
progress_message = "Copying {} to {}".format(in_file.path, out_file.path),
arguments = [in_file.path, out_file.path],
command = """cp "$1" "$2" """,
)
output_go_library_srcs = rule(
implementation = _output_go_library_srcs_impl,
attrs = {
"compiler": attr.label(
providers = [GoProtoCompiler],
default = "@io_bazel_rules_go//proto:go_proto",
),
"deps": attr.label_list(
providers = [GoInfo],
aspects = [],
),
"out": attr.output(
doc = ("Name of output .go file. If not specified, the file name " +
"of the generated source file will be used."),
mandatory = False,
),
"_go_context_data": attr.label(
default = "@io_bazel_rules_go//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)
def write_go_proto_srcs(name, go_proto_library, src, visibility = None):
generated_src = "__generated_" + src
output_go_library_srcs(
name = name + "_generated",
deps = [go_proto_library],
out = generated_src,
visibility = ["//visibility:private"],
)
write_source_files(
name = name,
files = {
src: generated_src,
},
diff_test = False,
visibility = visibility,
)