Skip to content

Commit

Permalink
Switch to new Go protobuf generators
Browse files Browse the repository at this point in the history
Switch from github.com/golang/protobuf/protoc-gen-go
to google.golang.org/protobuf/cmd/protoc-gen-go
and google.golang.org/grpc/cmd/protoc-gen-go-grpc.

Add APIv2 protoc-gen-go and protoc-gen-go-grpc bazel rules
  • Loading branch information
johanbrandhorst committed May 28, 2020
1 parent f0cca7d commit 88c0bef
Show file tree
Hide file tree
Showing 43 changed files with 3,475 additions and 3,342 deletions.
58 changes: 57 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler")

buildifier(
name = "buildifier",
Expand All @@ -11,9 +12,10 @@ buildifier(
)

# gazelle:exclude third_party
# gazelle:exclude vendor
# gazelle:exclude _output
# gazelle:prefix github.com/grpc-ecosystem/grpc-gateway/v2
# gazelle:go_proto_compilers //:go_apiv2
# gazelle:go_grpc_compilers //:go_apiv2, //:go_grpc

gazelle(name = "gazelle")

Expand All @@ -24,3 +26,57 @@ package_group(
"//protoc-gen-openapiv2/...",
],
)

go_proto_compiler(
name = "go_apiv2",
options = [
"paths=source_relative",
],
plugin = "@org_golang_google_protobuf//cmd/protoc-gen-go",
suffix = ".pb.go",
visibility = ["//visibility:public"],
deps = [
"@com_github_golang_protobuf//proto:go_default_library",
"@io_bazel_rules_go//proto/wkt:any_go_proto",
"@io_bazel_rules_go//proto/wkt:api_go_proto",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
"@io_bazel_rules_go//proto/wkt:field_mask_go_proto",
"@io_bazel_rules_go//proto/wkt:source_context_go_proto",
"@io_bazel_rules_go//proto/wkt:struct_go_proto",
"@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
"@io_bazel_rules_go//proto/wkt:type_go_proto",
"@io_bazel_rules_go//proto/wkt:wrappers_go_proto",
"@org_golang_google_protobuf//reflect/protoreflect:go_default_library",
"@org_golang_google_protobuf//runtime/protoimpl:go_default_library",
],
)

go_proto_compiler(
name = "go_grpc",
options = [
"paths=source_relative",
],
plugin = "@org_golang_google_grpc_cmd_protoc_gen_go_grpc//:protoc-gen-go-grpc",
suffix = "_grpc.pb.go",
visibility = ["//visibility:public"],
deps = [
"@io_bazel_rules_go//proto/wkt:any_go_proto",
"@io_bazel_rules_go//proto/wkt:api_go_proto",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
"@io_bazel_rules_go//proto/wkt:field_mask_go_proto",
"@io_bazel_rules_go//proto/wkt:source_context_go_proto",
"@io_bazel_rules_go//proto/wkt:struct_go_proto",
"@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
"@io_bazel_rules_go//proto/wkt:type_go_proto",
"@io_bazel_rules_go//proto/wkt:wrappers_go_proto",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",
"@org_golang_google_grpc//status:go_default_library",
],
)
25 changes: 14 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# grpc-gateway itself.

GO_PLUGIN=bin/protoc-gen-go
GO_PROTOBUF_REPO=github.com/golang/protobuf
GO_PLUGIN_PKG=$(GO_PROTOBUF_REPO)/protoc-gen-go
GO_PTYPES_ANY_PKG=$(GO_PROTOBUF_REPO)/ptypes/any
GO_PLUGIN_PKG=google.golang.org/protobuf/cmd/protoc-gen-go
GO_GRPC_PLUGIN=bin/protoc-gen-go-grpc
GO_GRPC_PLUGIN_PKG=google.golang.org/grpc/cmd/protoc-gen-go-grpc
OPENAPI_PLUGIN=bin/protoc-gen-openapiv2
OPENAPI_PLUGIN_SRC= ./utilities/doc.go \
./utilities/pattern.go \
Expand Down Expand Up @@ -147,6 +147,9 @@ PROTOC_INC_PATH=$(dir $(shell which protoc))/../include
$(GO_PLUGIN):
go build -o $(GO_PLUGIN) $(GO_PLUGIN_PKG)

$(GO_GRPC_PLUGIN):
go build -o $(GO_GRPC_PLUGIN) $(GO_GRPC_PLUGIN_PKG)

$(OPENAPIV2_GO): $(OPENAPIV2_PROTO) $(GO_PLUGIN)
protoc -I $(PROTOC_INC_PATH) --plugin=$(GO_PLUGIN) -I. --go_out=paths=source_relative:. $(OPENAPIV2_PROTO)

Expand All @@ -156,15 +159,15 @@ $(GATEWAY_PLUGIN): $(GATEWAY_PLUGIN_SRC)
$(OPENAPI_PLUGIN): $(OPENAPI_PLUGIN_SRC) $(OPENAPIV2_GO)
go build -o $@ $(OPENAPI_PLUGIN_PKG)

$(EXAMPLE_SVCSRCS): $(GO_PLUGIN) $(EXAMPLES)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=plugins=grpc,paths=source_relative:. $(EXAMPLES)
$(EXAMPLE_DEPSRCS): $(GO_PLUGIN) $(EXAMPLE_DEPS)
$(EXAMPLE_SVCSRCS): $(GO_PLUGIN) $(GO_GRPC_PLUGIN) $(EXAMPLES)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --plugin=$(GO_GRPC_PLUGIN) --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. $(EXAMPLES)
$(EXAMPLE_DEPSRCS): $(GO_PLUGIN) $(GO_GRPC_PLUGIN) $(EXAMPLE_DEPS)
mkdir -p $(OUTPUT_DIR)
protoc -I $(PROTOC_INC_PATH) -I. --plugin=$(GO_PLUGIN) --go_out=plugins=grpc,paths=source_relative:$(OUTPUT_DIR) $(@:.pb.go=.proto)
protoc -I $(PROTOC_INC_PATH) -I. --plugin=$(GO_PLUGIN) --plugin=$(GO_GRPC_PLUGIN) --go_out=paths=source_relative:$(OUTPUT_DIR) --go-grpc_out=paths=source_relative:$(OUTPUT_DIR) $(@:.pb.go=.proto)
cp $(OUTPUT_DIR)/$@ $@ || cp $(OUTPUT_DIR)/$@ $@

$(RUNTIME_TEST_SRCS): $(GO_PLUGIN) $(RUNTIME_TEST_PROTO)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=plugins=grpc,paths=source_relative:. $(RUNTIME_TEST_PROTO)
$(RUNTIME_TEST_SRCS): $(GO_PLUGIN) $(GO_GRPC_PLUGIN) $(RUNTIME_TEST_PROTO)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --plugin=$(GO_GRPC_PLUGIN) --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. $(RUNTIME_TEST_PROTO)

$(APICONFIG_SRCS): $(GO_PLUGIN) $(APICONFIG_PROTO)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=paths=source_relative:. $(APICONFIG_PROTO)
Expand All @@ -181,8 +184,8 @@ $(EXAMPLE_OPENAPISRCS): ADDITIONAL_SWG_FLAGS:=$(ADDITIONAL_SWG_FLAGS),grpc_api_c
$(EXAMPLE_OPENAPISRCS): $(OPENAPI_PLUGIN) $(OPENAPI_EXAMPLES)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(OPENAPI_PLUGIN) --openapiv2_out=logtostderr=true,allow_repeated_fields_in_body=true,use_go_templates=true$(ADDITIONAL_SWG_FLAGS):. $(OPENAPI_EXAMPLES)

$(HELLOWORLD_SVCSRCS): $(GO_PLUGIN) $(HELLOWORLD)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=plugins=grpc,paths=source_relative:. $(HELLOWORLD)
$(HELLOWORLD_SVCSRCS): $(GO_PLUGIN) $(GO_GRPC_PLUGIN) $(HELLOWORLD)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --plugin=$(GO_GRPC_PLUGIN) --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. $(HELLOWORLD)

$(HELLOWORLD_GWSRCS):
$(HELLOWORLD_GWSRCS): $(GATEWAY_PLUGIN) $(HELLOWORLD)
Expand Down
1 change: 1 addition & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/protoc-gen-go
/protoc-gen-go-grpc
/protoc-gen-grpc-gateway
/protoc-gen-openapiv2
27 changes: 14 additions & 13 deletions examples/internal/helloworld/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

# gazelle:exclude helloworld.pb.gw.go
# gazelle:exclude helloworld_grpc.pb.go
# gazelle:go_grpc_compilers //:go_apiv2, //:go_grpc, //protoc-gen-grpc-gateway:go_gen_grpc_gateway

proto_library(
name = "helloworld_proto",
srcs = ["helloworld.proto"],
Expand All @@ -14,26 +18,23 @@ proto_library(

go_proto_library(
name = "helloworld_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
compilers = [
"//:go_apiv2",
"//:go_grpc",
"//protoc-gen-grpc-gateway:go_gen_grpc_gateway",
],
importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/helloworld",
proto = ":helloworld_proto",
visibility = ["//visibility:public"],
deps = ["@go_googleapis//google/api:annotations_go_proto"],
deps = [
"@go_googleapis//google/api:annotations_go_proto",
"@org_golang_google_protobuf//proto:go_default_library", #keep
],
)

go_library(
name = "go_default_library",
srcs = ["helloworld.pb.gw.go"],
embed = [":helloworld_go_proto"],
importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/helloworld",
visibility = ["//visibility:public"],
deps = [
"//runtime:go_default_library",
"//utilities:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",
"@org_golang_google_grpc//grpclog:go_default_library",
"@org_golang_google_grpc//status:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",
],
visibility = ["//examples:__subpackages__"],
)
84 changes: 0 additions & 84 deletions examples/internal/helloworld/helloworld.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions examples/internal/helloworld/helloworld_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 88c0bef

Please sign in to comment.