Skip to content

Commit 3a472e5

Browse files
authored
bazel: Migrate py_proto_library to @com_github_grpc_grpc (#74)
py_proto_library provided by @com_google_protobuf//:protobuf.bzl has been deprecated for a while now: This is provided for backwards compatibility only. Bazel 5.3 will introduce support for py_proto_library, which should be used instead. https://github.com/protocolbuffers/protobuf/blob/32af7d211b85f71920acdfa9b796db008f8c2a45/protobuf.bzl#L642-L644 However, native py_proto_library has never been provided, see bazelbuild/bazel#3935. Instead @rules_python//python:proto.bzl is recommended. I attempted switching to this library, but it's not compatible with @com_google_googleapis's py_proto_library targets, see #69. I found a hacky workaround by using cc_proto_library to generate python targets, but downstream integration into Envoy failed (envoyproxy/envoy#30159). This PR migrates py_proto_library implementation to to @com_github_grpc_grpc. This implementation is used by @com_google_googleapis's, and, more importantly, uses bazel aspects. Which decouples cncf/xds and Envoy's dependencies from concrete upstream py_proto_library implementations. This resulted in a significant code improvement of bazel/api_build_system.bzl: No more custom @com_google_googleapis dependency mapping needed via py_proto_library rules override. No more hardcoded dependencies _xds_py_proto_library - proto dependency tree is determined from proto_library definitions via Basel aspects. No more EXTERNAL_PROTO_PY_BAZEL_DEP_MAP dependency map needed - for the same reason. Similar work in Envoy: envoyproxy/envoy#30834. Signed-off-by: Sergii Tkachenko <[email protected]>
1 parent 523115e commit 3a472e5

39 files changed

+71
-111
lines changed

bazel/api_build_system.bzl

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
load("@com_envoyproxy_protoc_gen_validate//bazel:pgv_proto_library.bzl", "pgv_cc_proto_library")
2-
load("@com_google_protobuf//:protobuf.bzl", _py_proto_library = "py_proto_library")
2+
load("@com_github_grpc_grpc//bazel:python_rules.bzl", _py_proto_library = "py_proto_library")
33
load("@io_bazel_rules_go//go:def.bzl", "go_test")
44
load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library", "go_proto_library")
55
load("@rules_proto//proto:defs.bzl", "proto_library")
66
load(
77
"//bazel:external_proto_deps.bzl",
88
"EXTERNAL_PROTO_CC_BAZEL_DEP_MAP",
99
"EXTERNAL_PROTO_GO_BAZEL_DEP_MAP",
10-
"EXTERNAL_PROTO_PY_BAZEL_DEP_MAP",
1110
)
1211

1312
_PY_PROTO_SUFFIX = "_py_proto"
@@ -43,65 +42,6 @@ def _go_proto_mapping(dep):
4342
def _cc_proto_mapping(dep):
4443
return _proto_mapping(dep, EXTERNAL_PROTO_CC_BAZEL_DEP_MAP, _CC_PROTO_SUFFIX)
4544

46-
def _py_proto_mapping(dep):
47-
return _proto_mapping(dep, EXTERNAL_PROTO_PY_BAZEL_DEP_MAP, _PY_PROTO_SUFFIX)
48-
49-
# TODO(htuch): Convert this to native py_proto_library once
50-
# https://github.com/bazelbuild/bazel/issues/3935 and/or
51-
# https://github.com/bazelbuild/bazel/issues/2626 are resolved.
52-
def _xds_py_proto_library(name, srcs = [], deps = []):
53-
mapped_deps = [_py_proto_mapping(dep) for dep in deps]
54-
mapped_unique_deps = []
55-
[mapped_unique_deps.append(d) for d in mapped_deps if d not in mapped_unique_deps]
56-
_py_proto_library(
57-
name = name + _PY_PROTO_SUFFIX,
58-
srcs = srcs,
59-
default_runtime = "@com_google_protobuf//:protobuf_python",
60-
protoc = "@com_google_protobuf//:protoc",
61-
deps = mapped_unique_deps + [
62-
"@com_envoyproxy_protoc_gen_validate//validate:validate_py",
63-
"@com_google_googleapis//google/rpc:status_py_proto",
64-
"@com_google_googleapis//google/api:annotations_py_proto",
65-
"@com_google_googleapis//google/api:http_py_proto",
66-
"@com_google_googleapis//google/api:httpbody_py_proto",
67-
],
68-
visibility = ["//visibility:public"],
69-
)
70-
71-
# This defines googleapis py_proto_library. The repository does not provide its definition and requires
72-
# overriding it in the consuming project (see https://github.com/grpc/grpc/issues/19255 for more details).
73-
def py_proto_library(name, deps = [], plugin = None):
74-
srcs = [dep[:-6] + ".proto" if dep.endswith("_proto") else dep for dep in deps]
75-
proto_deps = []
76-
77-
# py_proto_library in googleapis specifies *_proto rules in dependencies.
78-
# By rewriting *_proto to *.proto above, the dependencies in *_proto rules are not preserved.
79-
# As a workaround, manually specify the proto dependencies for the imported python rules.
80-
if name == "annotations_py_proto":
81-
proto_deps = proto_deps + [":http_py_proto"]
82-
83-
# checked.proto depends on syntax.proto, we have to add this dependency manually as well.
84-
if name == "checked_py_proto":
85-
proto_deps = proto_deps + [":syntax_py_proto"]
86-
87-
# Special handling for expr_proto target
88-
if srcs[0] == ":expr_moved.proto":
89-
srcs = ["checked.proto", "eval.proto", "explain.proto", "syntax.proto", "value.proto",]
90-
proto_deps = proto_deps + ["@com_google_googleapis//google/rpc:status_py_proto"]
91-
92-
93-
# py_proto_library does not support plugin as an argument yet at gRPC v1.25.0:
94-
# https://github.com/grpc/grpc/blob/v1.25.0/bazel/python_rules.bzl#L72.
95-
# plugin should also be passed in here when gRPC version is greater than v1.25.x.
96-
_py_proto_library(
97-
name = name,
98-
srcs = srcs,
99-
default_runtime = "@com_google_protobuf//:protobuf_python",
100-
protoc = "@com_google_protobuf//:protoc",
101-
deps = proto_deps + ["@com_google_protobuf//:protobuf_python"],
102-
visibility = ["//visibility:public"],
103-
)
104-
10545
def _xds_cc_py_proto_library(
10646
name,
10747
visibility = ["//visibility:private"],
@@ -129,7 +69,15 @@ def _xds_cc_py_proto_library(
12969
deps = [relative_name],
13070
visibility = ["//visibility:public"],
13171
)
132-
_xds_py_proto_library(name, srcs, deps)
72+
73+
# Uses gRPC implementation of py_proto_library.
74+
# https://github.com/grpc/grpc/blob/v1.59.1/bazel/python_rules.bzl#L160
75+
_py_proto_library(
76+
name = name + _PY_PROTO_SUFFIX,
77+
# Actual dependencies are resolved automatically from the proto_library dep tree.
78+
deps = [relative_name],
79+
visibility = ["//visibility:public"],
80+
)
13381

13482
# Optionally define gRPC services
13583
if has_services:

bazel/dependency_imports.bzl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,34 @@ load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
33
load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
44
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
55
load("@com_envoyproxy_protoc_gen_validate//bazel:repositories.bzl", "pgv_dependencies")
6+
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
67

78
# go version for rules_go
89
GO_VERSION = "1.20.2"
910

1011
def xds_dependency_imports(go_version = GO_VERSION):
12+
rules_proto_dependencies()
13+
rules_proto_toolchains()
1114
protobuf_deps()
1215
go_rules_dependencies()
1316
go_register_toolchains(go_version)
1417
gazelle_dependencies()
1518
pgv_dependencies()
1619

20+
# Needed for grpc's @com_github_grpc_grpc//bazel:python_rules.bzl
21+
# Used in place of calling grpc_deps() because it needs to be called before
22+
# loading `grpc_extra_deps.bzl` - which is not allowed in a method def context.
23+
native.bind(
24+
name = "protocol_compiler",
25+
actual = "@com_google_protobuf//:protoc",
26+
)
27+
1728
switched_rules_by_language(
1829
name = "com_google_googleapis_imports",
1930
cc = True,
2031
go = True,
32+
python = True,
2133
grpc = True,
22-
rules_override = {
23-
"py_proto_library": ["@com_github_cncf_xds//bazel:api_build_system.bzl", "",],
24-
},
2534
)
2635

2736
# These dependencies, like most of the Go in this repository, exist only for the API.

bazel/external_proto_deps.bzl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,3 @@ EXTERNAL_PROTO_CC_BAZEL_DEP_MAP = {
3636
"@com_google_googleapis//google/api/expr/v1alpha1:checked_proto": "@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto",
3737
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_proto": "@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
3838
}
39-
40-
# This maps from the Bazel proto_library target to the Python language binding target for external dependencies.
41-
EXTERNAL_PROTO_PY_BAZEL_DEP_MAP = {
42-
"@com_google_googleapis//google/api/expr/v1alpha1:checked_proto": "@com_google_googleapis//google/api/expr/v1alpha1:expr_py_proto",
43-
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_proto": "@com_google_googleapis//google/api/expr/v1alpha1:expr_py_proto",
44-
}

bazel/repositories.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def xds_api_dependencies():
2626
"io_bazel_rules_go",
2727
locations = REPOSITORY_LOCATIONS,
2828
)
29+
xds_http_archive(
30+
"rules_proto",
31+
locations = REPOSITORY_LOCATIONS,
32+
)
2933

3034
# Old name for backward compatibility.
3135
# TODO(roth): Remove once all callers are updated to use the new name.

bazel/repository_locations.bzl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ REPOSITORY_LOCATIONS = dict(
1313
urls = ["https://github.com/envoyproxy/protoc-gen-validate/archive/refs/tags/v0.6.1.tar.gz"],
1414
),
1515
com_github_grpc_grpc = dict(
16-
sha256 = "13e7c6460cd979726e5b3b129bb01c34532f115883ac696a75eb7f1d6a9765ed",
17-
strip_prefix = "grpc-1.40.0",
18-
urls = ["https://github.com/grpc/grpc/archive/refs/tags/v1.40.0.tar.gz"],
16+
sha256 = "916f88a34f06b56432611aaa8c55befee96d0a7b7d7457733b9deeacbc016f99",
17+
strip_prefix = "grpc-1.59.1",
18+
urls = ["https://github.com/grpc/grpc/archive/refs/tags/v1.59.1.tar.gz"],
1919
),
2020
com_google_googleapis = dict(
2121
# TODO(dio): Consider writing a Skylark macro for importing Google API proto.
@@ -24,9 +24,9 @@ REPOSITORY_LOCATIONS = dict(
2424
urls = ["https://github.com/googleapis/googleapis/archive/114a745b2841a044e98cdbb19358ed29fcf4a5f1.tar.gz"],
2525
),
2626
com_google_protobuf = dict(
27-
sha256 = "52b6160ae9266630adb5e96a9fc645215336371a740e87d411bfb63ea2f268a0",
28-
strip_prefix = "protobuf-3.18.0",
29-
urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v3.18.0/protobuf-all-3.18.0.tar.gz"],
27+
sha256 = "8242327e5df8c80ba49e4165250b8f79a76bd11765facefaaecfca7747dc8da2",
28+
strip_prefix = "protobuf-3.21.5",
29+
urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.21.5.zip"],
3030
),
3131
io_bazel_rules_go = dict(
3232
sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
@@ -35,4 +35,9 @@ REPOSITORY_LOCATIONS = dict(
3535
"https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
3636
],
3737
),
38+
rules_proto = dict(
39+
sha256 = "80d3a4ec17354cccc898bfe32118edd934f851b03029d63ef3fc7c8663a7415c",
40+
strip_prefix = "rules_proto-5.3.0-21.5",
41+
urls = ["https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.5.tar.gz",],
42+
),
3843
)

go/udpa/annotations/migrate.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/udpa/annotations/security.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/udpa/annotations/sensitive.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/udpa/annotations/status.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/udpa/annotations/versioning.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/udpa/data/orca/v1/orca_load_report.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/udpa/service/orca/v1/orca.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/udpa/type/v1/typed_struct.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/annotations/v3/migrate.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/annotations/v3/security.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/annotations/v3/sensitive.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/annotations/v3/status.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/annotations/v3/versioning.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/core/v3/authority.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/core/v3/cidr.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/core/v3/collection_entry.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/core/v3/context_params.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/core/v3/extension.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/core/v3/resource.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/core/v3/resource_locator.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/core/v3/resource_name.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/data/orca/v3/orca_load_report.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/xds/service/orca/v3/orca.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)