Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Add ability to generate package-level go protos #2

Merged
merged 2 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"],
)
nareddyt marked this conversation as resolved.
Show resolved Hide resolved
75 changes: 62 additions & 13 deletions bazel/api_build_system.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
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")

_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 +42,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 +107,47 @@ 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",
],
)
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(
nareddyt marked this conversation as resolved.
Show resolved Hide resolved
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",
)
nareddyt marked this conversation as resolved.
Show resolved Hide resolved
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",
],
nareddyt marked this conversation as resolved.
Show resolved Hide resolved
)
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