Skip to content

Commit

Permalink
Add gRPC support
Browse files Browse the repository at this point in the history
  • Loading branch information
mering committed Nov 23, 2023
1 parent 318ceed commit b51cfe9
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions proto/def_v2.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ def _library_to_source(_go, attr, source, merge):
if GoSource in compiler:
merge(source, compiler[GoSource])

def _go_proto_aspect_impl(target, ctx):
def _compile_proto(ctx, target, attr):
go = go_context(ctx)
go_srcs = []

proto_info = target[ProtoInfo]
imports = get_imports(target, ctx.rule.attr, go.importpath)
imports = get_imports(target, attr, go.importpath)
compiler = ctx.attr._compiler[GoProtoCompiler]
if proto_info.direct_sources:
go_srcs.extend(compiler.compile(
Expand All @@ -70,7 +69,7 @@ def _go_proto_aspect_impl(target, ctx):
importpath = go.importpath,
))

go_deps = getattr(ctx.rule.attr, "deps", [])
go_deps = getattr(attr, "deps", [])
library = go.new_library(
go,
resolver = _library_to_source,
Expand All @@ -79,15 +78,17 @@ def _go_proto_aspect_impl(target, ctx):
)
source = go.library_to_source(go, ctx.attr, library, False)
archive = go.archive(go, source)
return GoProtoInfo(
imports = imports,
library = library,
source = source,
archive = archive,
)

return [
GoProtoInfo(
imports = imports,
library = library,
source = source,
archive = archive,
),
]
def _go_proto_aspect_impl(target, ctx):
provider = _compile_proto(ctx, target, ctx.rule.attr)

return [provider]

_go_proto_aspect = aspect(
_go_proto_aspect_impl,
Expand Down Expand Up @@ -115,6 +116,7 @@ def _go_proto_library_impl(ctx):
for proto in ctx.attr.protos:
if GoProtoInfo in proto:
providers.extend([
proto[GoProtoInfo],
proto[GoProtoInfo].library,
proto[GoProtoInfo].source,
proto[GoProtoInfo].archive,
Expand All @@ -137,3 +139,41 @@ go_proto_library = rule(
GoArchive,
],
)

def _go_grpc_library_impl(ctx):
if len(ctx.attr.protos) != 1:
fail("srcs attribute must be exactly one target")

provider = _compile_proto(ctx, ctx.attr.protos[0], ctx.attr)

return [
provider.library,
provider.source,
provider.archive,
]

go_grpc_library = rule(
implementation = _go_grpc_library_impl,
attrs = {
"protos": attr.label_list(
providers = [ProtoInfo],
mandatory = True,
),
"deps": attr.label_list(
providers = [GoLibrary],
),
"importpath": attr.string(),
"_compiler": attr.label(
default = "//proto:go_grpc",
),
"_go_context_data": attr.label(
default = "//:go_context_data",
),
},
provides = [
GoLibrary,
GoSource,
GoArchive,
],
toolchains = [GO_TOOLCHAIN],
)

0 comments on commit b51cfe9

Please sign in to comment.