Skip to content

Commit

Permalink
Merge pull request #2 from nareddyt/go_protos
Browse files Browse the repository at this point in the history
Currently, this repository only generates cc and py protos. This change adds the ability to generate *.pb.go and *.pb.validate.go files for each package.

Example: bazel build //udpa/data/orca/v1:pkg_go
Note that the option go_package is no longer needed in the proto files (and should not be added!). The go_package will be set automatically to match the path.

Example: udpa_service_orca_v1 in the generate proto
Please see my comments in this change for more details.

Note: This change is largely based on envoyproxy/envoy#8003

Signed-off-by: Teju Nareddy [email protected]
  • Loading branch information
htuch authored Aug 26, 2019
2 parents 4cbdcb9 + 391ae42 commit 9432480
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 20 deletions.
Empty file added BUILD
Empty file.
12 changes: 12 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler")

licenses(["notice"]) # Apache 2

go_proto_compiler(
name = "pgv_plugin_go",
options = ["lang=go"],
plugin = "@com_envoyproxy_protoc_gen_validate//:protoc-gen-validate",
suffix = ".pb.validate.go",
valid_archive = False,
visibility = ["//visibility:public"],
)
84 changes: 71 additions & 13 deletions bazel/api_build_system.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
load("@com_google_protobuf//:protobuf.bzl", _py_proto_library = "py_proto_library")
load("@com_envoyproxy_protoc_gen_validate//bazel:pgv_proto_library.bzl", "pgv_cc_proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_test")

_PY_SUFFIX = "_py"
_CC_SUFFIX = "_cc"
_GO_SUFFIX = "_go"
_GO_IMPORTPATH_PREFIX = "github.com/cncf/udpa/"

_COMMON_PROTO_DEPS = [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:struct_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_googleapis//google/api:http_proto",
"@com_google_googleapis//google/api:annotations_proto",
"@com_google_googleapis//google/rpc:status_proto",
"@com_envoyproxy_protoc_gen_validate//validate:validate_proto",
]

def _Suffix(d, suffix):
return d + suffix
Expand All @@ -25,19 +43,7 @@ def udpa_proto_library(
native.proto_library(
name = name,
srcs = srcs,
deps = deps + [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:struct_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:wrappers_proto",
"@com_google_googleapis//google/api:http_proto",
"@com_google_googleapis//google/api:annotations_proto",
"@com_google_googleapis//google/rpc:status_proto",
"@com_envoyproxy_protoc_gen_validate//validate:validate_proto",
],
deps = deps + _COMMON_PROTO_DEPS,
visibility = visibility,
)
pgv_cc_proto_library(
Expand Down Expand Up @@ -102,3 +108,55 @@ def py_proto_library(name, deps = []):
deps = proto_deps + ["@com_google_protobuf//:protobuf_python"],
visibility = ["//visibility:public"],
)

def udpa_proto_package(name = "pkg", srcs = [], deps = [], has_services = False, visibility = ["//visibility:public"]):
"""
Generate a single go_proto for all protos in the package.
Some packages may have multiple proto files, and therefore multiple blaze proto targets.
For golang, this is not ideal. Non-bazel users will have to manually generate the protos in a different structure.
Instead of generating a go_proto target per proto, this rule generates a single go_proto target
for the entire package. This better adheres to golang's import structure in the OSS world.
More information: https://github.com/envoyproxy/envoy/pull/8003
"""
if srcs == []:
srcs = native.glob(["*.proto"])

native.proto_library(
name = name,
srcs = srcs,
deps = deps + _COMMON_PROTO_DEPS,
visibility = visibility,
)

compilers = ["@io_bazel_rules_go//proto:go_proto", "//bazel:pgv_plugin_go"]
if has_services:
compilers = ["@io_bazel_rules_go//proto:go_grpc", "//bazel:pgv_plugin_go"]

go_proto_library(
name = _Suffix(name, _GO_SUFFIX),
compilers = compilers,
importpath = _Suffix(_GO_IMPORTPATH_PREFIX, native.package_name()),
proto = name,
visibility = ["//visibility:public"],
deps = [_LibrarySuffix(d, _GO_SUFFIX) for d in deps] + [
"@com_github_golang_protobuf//ptypes:go_default_library",
"@com_github_golang_protobuf//ptypes/any:go_default_library",
"@com_github_golang_protobuf//ptypes/duration:go_default_library",
"@com_github_golang_protobuf//ptypes/struct:go_default_library",
"@com_github_golang_protobuf//ptypes/timestamp:go_default_library",
"@com_github_golang_protobuf//ptypes/wrappers:go_default_library",
"@com_envoyproxy_protoc_gen_validate//validate:go_default_library",
"@com_google_googleapis//google/api:annotations_go_proto",
"@com_google_googleapis//google/rpc:status_go_proto",
],
)

def udpa_go_test(name, srcs, importpath, proto_deps):
go_test(
name = name,
srcs = srcs,
importpath = _GO_IMPORTPATH_PREFIX + importpath,
deps = [_LibrarySuffix(d, _GO_SUFFIX) for d in proto_deps],
)
22 changes: 22 additions & 0 deletions bazel/dependency_imports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@ def udpa_dependency_imports(go_version = GO_VERSION):
"py_proto_library": "@com_github_cncf_udpa//bazel:api_build_system.bzl",
},
)

go_repository(
name = "org_golang_google_grpc",
build_file_proto_mode = "disable",
importpath = "google.golang.org/grpc",
sum = "h1:AzbTB6ux+okLTzP8Ru1Xs41C303zdcfEht7MQnYJt5A=",
version = "v1.23.0",
)

go_repository(
name = "org_golang_x_net",
importpath = "golang.org/x/net",
sum = "h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA=",
version = "v0.0.0-20190813141303-74dc4d7220e7",
)

go_repository(
name = "org_golang_x_text",
importpath = "golang.org/x/text",
sum = "h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=",
version = "v0.3.0",
)
12 changes: 11 additions & 1 deletion test/build/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//bazel:api_build_system.bzl", "udpa_cc_test")
load("//bazel:api_build_system.bzl", "udpa_cc_test", "udpa_go_test")

licenses(["notice"]) # Apache 2

Expand All @@ -9,3 +9,13 @@ udpa_cc_test(
"//udpa/service/orca/v1:orca",
],
)

udpa_go_test(
name = "go_build_test",
srcs = ["go_build_test.go"],
importpath = "go_build_test",
proto_deps = [
"//udpa/data/orca/v1:pkg",
"//udpa/service/orca/v1:pkg",
],
)
12 changes: 12 additions & 0 deletions test/build/go_build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package go_build_test

import (
"testing"

_ "github.com/cncf/udpa/udpa/data/orca/v1"
_ "github.com/cncf/udpa/udpa/service/orca/v1"
)

func TestNoop(t *testing.T) {
// Noop test that verifies the successful importation of UDPA protos
}
4 changes: 3 additions & 1 deletion udpa/data/orca/v1/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//bazel:api_build_system.bzl", "udpa_proto_library")
load("//bazel:api_build_system.bzl", "udpa_proto_library", "udpa_proto_package")

licenses(["notice"]) # Apache 2

Expand All @@ -7,3 +7,5 @@ udpa_proto_library(
srcs = ["orca_load_report.proto"],
visibility = ["//visibility:public"],
)

udpa_proto_package()
1 change: 0 additions & 1 deletion udpa/data/orca/v1/orca_load_report.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package udpa.data.orca.v1;
option java_outer_classname = "OrcaLoadReportProto";
option java_multiple_files = true;
option java_package = "com.github.udpa.udpa.data.orca.v1";
option go_package = "v1";

import "validate/validate.proto";

Expand Down
15 changes: 12 additions & 3 deletions udpa/service/orca/v1/BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
load("//bazel:api_build_system.bzl", "udpa_proto_library")
load("//bazel:api_build_system.bzl", "udpa_proto_library", "udpa_proto_package")

licenses(["notice"]) # Apache 2

udpa_proto_library(
name = "orca",
srcs = ["orca.proto"],
has_services = 1,
deps = ["//udpa/data/orca/v1:orca_load_report"],
has_services = True,
deps = [
"//udpa/data/orca/v1:orca_load_report",
],
)

udpa_proto_package(
has_services = True,
deps = [
"//udpa/data/orca/v1:pkg",
],
)
1 change: 0 additions & 1 deletion udpa/service/orca/v1/orca.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package udpa.service.orca.v1;
option java_outer_classname = "OrcaProto";
option java_multiple_files = true;
option java_package = "com.github.udpa.udpa.service.orca.v1";
option go_package = "v1";

import "udpa/data/orca/v1/orca_load_report.proto";

Expand Down

0 comments on commit 9432480

Please sign in to comment.