Skip to content

Add canonical CEL support: dev.cel.expr (rules_python approach) #69

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
63 changes: 11 additions & 52 deletions bazel/api_build_system.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@com_envoyproxy_protoc_gen_validate//bazel:pgv_proto_library.bzl", "pgv_cc_proto_library")
load("@com_google_protobuf//:protobuf.bzl", _py_proto_library = "py_proto_library")
load("@rules_python//python:proto.bzl", _py_proto_library = "py_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_test")
load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
Expand Down Expand Up @@ -43,66 +43,24 @@ def _go_proto_mapping(dep):
def _cc_proto_mapping(dep):
return _proto_mapping(dep, EXTERNAL_PROTO_CC_BAZEL_DEP_MAP, _CC_PROTO_SUFFIX)

def _py_proto_mapping(dep):
return _proto_mapping(dep, EXTERNAL_PROTO_PY_BAZEL_DEP_MAP, _PY_PROTO_SUFFIX)

# TODO(htuch): Convert this to native py_proto_library once
# https://github.com/bazelbuild/bazel/issues/3935 and/or
# https://github.com/bazelbuild/bazel/issues/2626 are resolved.
def _xds_py_proto_library(name, srcs = [], deps = []):
mapped_deps = [_py_proto_mapping(dep) for dep in deps]
mapped_unique_deps = []
[mapped_unique_deps.append(d) for d in mapped_deps if d not in mapped_unique_deps]
_py_proto_library(
name = name + _PY_PROTO_SUFFIX,
srcs = srcs,
default_runtime = "@com_google_protobuf//:protobuf_python",
protoc = "@com_google_protobuf//:protoc",
deps = mapped_unique_deps + [
"@com_envoyproxy_protoc_gen_validate//validate:validate_py",
"@com_google_googleapis//google/rpc:status_py_proto",
"@com_google_googleapis//google/api:annotations_py_proto",
"@com_google_googleapis//google/api:http_py_proto",
"@com_google_googleapis//google/api:httpbody_py_proto",
deps = deps + [
"@com_google_googleapis//google/api:http_proto",
],
visibility = ["//visibility:public"],
)

# This defines googleapis py_proto_library. The repository does not provide its definition and requires
# overriding it in the consuming project (see https://github.com/grpc/grpc/issues/19255 for more details).
def py_proto_library(name, deps = [], plugin = None):
srcs = [dep[:-6] + ".proto" if dep.endswith("_proto") else dep for dep in deps]
proto_deps = []

# py_proto_library in googleapis specifies *_proto rules in dependencies.
# By rewriting *_proto to *.proto above, the dependencies in *_proto rules are not preserved.
# As a workaround, manually specify the proto dependencies for the imported python rules.
if name == "annotations_py_proto":
proto_deps = proto_deps + [":http_py_proto"]

# checked.proto depends on syntax.proto, we have to add this dependency manually as well.
if name == "checked_py_proto":
proto_deps = proto_deps + [":syntax_py_proto"]

# Special handling for expr_proto target
if srcs[0] == ":expr_moved.proto":
srcs = ["checked.proto", "eval.proto", "explain.proto", "syntax.proto", "value.proto",]
proto_deps = proto_deps + ["@com_google_googleapis//google/rpc:status_py_proto"]


# py_proto_library does not support plugin as an argument yet at gRPC v1.25.0:
# https://github.com/grpc/grpc/blob/v1.25.0/bazel/python_rules.bzl#L72.
# plugin should also be passed in here when gRPC version is greater than v1.25.x.
def cc_proto_library(name, deps = [], **kwargs):
native.cc_proto_library(name=name, deps=deps, **kwargs)
_py_proto_library(
name = name,
srcs = srcs,
default_runtime = "@com_google_protobuf//:protobuf_python",
protoc = "@com_google_protobuf//:protoc",
deps = proto_deps + ["@com_google_protobuf//:protobuf_python"],
name = name.replace('_cc_proto', '') + "_py_pb2",
deps = deps,
visibility = ["//visibility:public"],
)

def _xds_cc_py_proto_library(
def _xds_cc_proto_library(
name,
visibility = ["//visibility:private"],
srcs = [],
Expand All @@ -129,7 +87,6 @@ def _xds_cc_py_proto_library(
deps = [relative_name],
visibility = ["//visibility:public"],
)
_xds_py_proto_library(name, srcs, deps)

# Optionally define gRPC services
if has_services:
Expand All @@ -146,14 +103,16 @@ def xds_proto_package(
srcs = native.glob(["*.proto"])

name = "pkg"
_xds_cc_py_proto_library(
_xds_cc_proto_library(
name = name,
visibility = visibility,
srcs = srcs,
deps = deps,
has_services = has_services,
)

_xds_py_proto_library(name = name, srcs = srcs, deps = deps)

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"]
Expand Down
8 changes: 7 additions & 1 deletion bazel/dependency_imports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@com_envoyproxy_protoc_gen_validate//bazel:repositories.bzl", "pgv_dependencies")
load("@rules_python//python:repositories.bzl", "py_repositories")
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")


# go version for rules_go
GO_VERSION = "1.20.2"

def xds_dependency_imports(go_version = GO_VERSION):
py_repositories()
rules_proto_dependencies()
rules_proto_toolchains()
protobuf_deps()
go_rules_dependencies()
go_register_toolchains(go_version)
Expand All @@ -20,7 +26,7 @@ def xds_dependency_imports(go_version = GO_VERSION):
go = True,
grpc = True,
rules_override = {
"py_proto_library": ["@com_github_cncf_xds//bazel:api_build_system.bzl", "",],
"cc_proto_library": ["@com_github_cncf_xds//bazel:api_build_system.bzl", "",],
},
)

Expand Down
6 changes: 4 additions & 2 deletions bazel/external_proto_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ EXTERNAL_PROTO_GO_BAZEL_DEP_MAP = {
# go_googleapis in https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#overriding-dependencies
"@com_google_googleapis//google/api/expr/v1alpha1:checked_proto": "@go_googleapis//google/api/expr/v1alpha1:expr_go_proto",
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_proto": "@go_googleapis//google/api/expr/v1alpha1:expr_go_proto",
"@dev_cel//proto/cel/expr:checked_proto": "@dev_cel//proto/cel/expr:checked_go_proto",
"@dev_cel//proto/cel/expr:syntax_proto": "@dev_cel//proto/cel/expr:syntax_go_proto",
}

# This maps from the Bazel proto_library target to the C++ language binding target for external dependencies.
EXTERNAL_PROTO_CC_BAZEL_DEP_MAP = {
"@com_google_googleapis//google/api/expr/v1alpha1:checked_proto": "@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto",
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_proto": "@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
"@dev_cel//proto/cel/expr:checked_proto": "@dev_cel//proto/cel/expr:checked_cc_proto",
"@dev_cel//proto/cel/expr:syntax_proto": "@dev_cel//proto/cel/expr:syntax_cc_proto",
}

# This maps from the Bazel proto_library target to the Python language binding target for external dependencies.
EXTERNAL_PROTO_PY_BAZEL_DEP_MAP = {
"@com_google_googleapis//google/api/expr/v1alpha1:checked_proto": "@com_google_googleapis//google/api/expr/v1alpha1:expr_py_proto",
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_proto": "@com_google_googleapis//google/api/expr/v1alpha1:expr_py_proto",
}
12 changes: 12 additions & 0 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,22 @@ def xds_api_dependencies():
"com_google_protobuf",
locations = REPOSITORY_LOCATIONS,
)
xds_http_archive(
name = "dev_cel",
locations = REPOSITORY_LOCATIONS,
)
xds_http_archive(
"io_bazel_rules_go",
locations = REPOSITORY_LOCATIONS,
)
xds_http_archive(
"rules_python",
locations = REPOSITORY_LOCATIONS,
)
xds_http_archive(
"rules_proto",
locations = REPOSITORY_LOCATIONS,
)

# Old name for backward compatibility.
# TODO(roth): Remove once all callers are updated to use the new name.
Expand Down
20 changes: 18 additions & 2 deletions bazel/repository_locations.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ REPOSITORY_LOCATIONS = dict(
urls = ["https://github.com/googleapis/googleapis/archive/114a745b2841a044e98cdbb19358ed29fcf4a5f1.tar.gz"],
),
com_google_protobuf = dict(
sha256 = "52b6160ae9266630adb5e96a9fc645215336371a740e87d411bfb63ea2f268a0",
sha256 = "bb1ddd8172b745cbdc75f06841bd9e7c9de0b3956397723d883423abfab8e176",
strip_prefix = "protobuf-3.18.0",
urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v3.18.0/protobuf-all-3.18.0.tar.gz"],
urls = ["https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.18.0.zip"],
),
dev_cel = dict(
sha256 = "3de60ea3a29b6246faf04d206b7ee4633c155042e040086205c83edf713aee29",
strip_prefix = "cel-spec-2657e88023e5e7dfbb62e74de7a7cfd6e8284d7b",
urls = ["https://github.com/google/cel-spec/archive/2657e88023e5e7dfbb62e74de7a7cfd6e8284d7b.zip"],
),
io_bazel_rules_go = dict(
sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
Expand All @@ -35,4 +40,15 @@ REPOSITORY_LOCATIONS = dict(
"https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
],
),
# used by dev_cel
rules_python = dict(
sha256 = "9d04041ac92a0985e344235f5d946f71ac543f1b1565f2cdbc9a2aaee8adf55b",
strip_prefix = "rules_python-0.26.0",
urls = ["https://github.com/bazelbuild/rules_python/releases/download/0.26.0/rules_python-0.26.0.tar.gz"],
),
rules_proto = dict(
sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd",
strip_prefix = "rules_proto-5.3.0-21.7",
urls = ["https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz"],
),
)
Loading