Skip to content

Commit

Permalink
Update bazel and deps (#1111)
Browse files Browse the repository at this point in the history
This modernizes the bazel setup to support 7.x, still using the
WORKSPACE
  • Loading branch information
keith authored Nov 26, 2024
1 parent 36fe3c8 commit bbd0e25
Show file tree
Hide file tree
Showing 25 changed files with 221 additions and 155 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example-workspace
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# TODO: Add support for bzlmod
common --enable_bzlmod=false
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.2.0
7.1.2
13 changes: 10 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ load("//bazel:repositories.bzl", "pgv_dependencies")

pgv_dependencies()

load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

load("//bazel:dependency_imports.bzl", "pgv_dependency_imports")

pgv_dependency_imports()

load("//:dependencies.bzl", "go_third_party")
load("//bazel:extra_dependency_imports.bzl", "pgv_extra_dependency_imports")

pgv_extra_dependency_imports()

load("@maven//:defs.bzl", "pinned_maven_install")

# gazelle:repository_macro dependencies.bzl%go_third_party
go_third_party()
pinned_maven_install()
9 changes: 5 additions & 4 deletions bazel/dependency_imports.bzl
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
load("@rules_python//python:pip.bzl", "pip_install")
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")
load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
load("@rules_python//python:pip.bzl", "pip_parse")

def _pgv_pip_dependencies():
# This rule translates the specified requirements.in (which must be same as install_requires from setup.cfg)
# into @pgv_pip_deps//:requirements.bzl.
pip_install(
pip_parse(
name = "pgv_pip_deps",
requirements = "@com_envoyproxy_protoc_gen_validate//python:requirements.in",
requirements_lock = "@com_envoyproxy_protoc_gen_validate//python:requirements.txt",
)

def _pgv_go_dependencies():
Expand Down
22 changes: 22 additions & 0 deletions bazel/extra_dependency_imports.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@bazel_features//:deps.bzl", "bazel_features_deps")
load("@com_google_protobuf//:protobuf_deps.bzl", "PROTOBUF_MAVEN_ARTIFACTS")
load("@pgv_pip_deps//:requirements.bzl", "install_deps")
load("@rules_jvm_external//:defs.bzl", "maven_install")
load("//:dependencies.bzl", "go_third_party")

def pgv_extra_dependency_imports():
bazel_features_deps()

install_deps()

# gazelle:repository_macro dependencies.bzl%go_third_party
go_third_party()

maven_install(
artifacts = PROTOBUF_MAVEN_ARTIFACTS,
maven_install_json = "@com_google_protobuf//:maven_install.json",
repositories = [
"https://repo1.maven.org/maven2",
"https://repo.maven.apache.org/maven2",
],
)
1 change: 1 addition & 0 deletions bazel/protobuf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,5 @@ java_proto_gen_validate = rule(
"srcjar": "lib%{name}-src.jar",
},
implementation = _java_proto_gen_validate_impl,
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
)
82 changes: 22 additions & 60 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")

Expand All @@ -8,29 +7,26 @@ def pgv_dependencies(maven_repos = _DEFAULT_REPOSITORIES):
if not native.existing_rule("io_bazel_rules_go"):
http_archive(
name = "io_bazel_rules_go",
sha256 = "91585017debb61982f7054c9688857a2ad1fd823fc3f9cb05048b0025c47d023",
sha256 = "33acc4ae0f70502db4b893c9fc1dd7a9bf998c23e7ff2c4517741d4049a976f8",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.42.0/rules_go-v0.42.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.42.0/rules_go-v0.42.0.zip",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
],
)

if not native.existing_rule("bazel_gazelle"):
http_archive(
name = "bazel_gazelle",
sha256 = "d3fa66a39028e97d76f9e2db8f1b0c11c099e8e01bf363a923074784e451f809",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.33.0/bazel-gazelle-v0.33.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.33.0/bazel-gazelle-v0.33.0.tar.gz",
],
sha256 = "d76bf7a60fd8b050444090dfa2837a4eaf9829e1165618ee35dceca5cbdf58d5",
url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
)

if not native.existing_rule("com_google_protobuf"):
http_archive(
name = "com_google_protobuf",
url = "https://github.com/protocolbuffers/protobuf/archive/v3.15.3.tar.gz",
sha256 = "b10bf4e2d1a7586f54e64a5d9e7837e5188fc75ae69e36f215eb01def4f9721b",
strip_prefix = "protobuf-3.15.3",
url = "https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protobuf-27.0.tar.gz",
sha256 = "da288bf1daa6c04d03a9051781caa52aceb9163586bff9aa6cfb12f69b9395aa",
strip_prefix = "protobuf-27.0",
)

# TODO(akonradi): This shouldn't be necessary since the same http_archive block is imported by
Expand All @@ -51,14 +47,6 @@ def pgv_dependencies(maven_repos = _DEFAULT_REPOSITORIES):
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz"],
)

if not native.existing_rule("six"):
http_archive(
name = "six",
build_file = "@com_google_protobuf//:third_party/six.BUILD",
sha256 = "d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73",
urls = ["https://pypi.python.org/packages/source/s/six/six-1.12.0.tar.gz"],
)

if not native.existing_rule("com_google_re2j"):
jvm_maven_import_external(
name = "com_google_re2j",
Expand All @@ -83,40 +71,6 @@ def pgv_dependencies(maven_repos = _DEFAULT_REPOSITORIES):
server_urls = maven_repos,
)

if not native.existing_rule("guava"):
native.bind(
name = "guava",
actual = "@com_google_guava//jar",
)

if not native.existing_rule("com_google_gson"):
jvm_maven_import_external(
name = "com_google_gson",
artifact = "com.google.code.gson:gson:2.8.5",
artifact_sha256 = "233a0149fc365c9f6edbd683cfe266b19bdc773be98eabdaf6b3c924b48e7d81",
server_urls = maven_repos,
)

if not native.existing_rule("gson"):
native.bind(
name = "gson",
actual = "@com_google_gson//jar",
)

if not native.existing_rule("error_prone_annotations_maven"):
jvm_maven_import_external(
name = "error_prone_annotations_maven",
artifact = "com.google.errorprone:error_prone_annotations:2.3.2",
artifact_sha256 = "357cd6cfb067c969226c442451502aee13800a24e950fdfde77bcdb4565a668d",
server_urls = maven_repos,
)

if not native.existing_rule("error_prone_annotations"):
native.bind(
name = "error_prone_annotations",
actual = "@error_prone_annotations_maven//jar",
)

if not native.existing_rule("org_apache_commons_validator"):
jvm_maven_import_external(
name = "org_apache_commons_validator",
Expand All @@ -128,15 +82,23 @@ def pgv_dependencies(maven_repos = _DEFAULT_REPOSITORIES):
if not native.existing_rule("rules_python"):
http_archive(
name = "rules_python",
sha256 = "b593d13bb43c94ce94b483c2858e53a9b811f6f10e1e0eedc61073bd90e58d9c",
strip_prefix = "rules_python-0.12.0",
url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.12.0.tar.gz",
sha256 = "4912ced70dc1a2a8e4b86cec233b192ca053e82bc72d877b98e126156e8f228d",
strip_prefix = "rules_python-0.32.2",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.32.2/rules_python-0.32.2.tar.gz",
)

if not native.existing_rule("rules_proto"):
http_archive(
name = "rules_proto",
sha256 = "2490dca4f249b8a9a3ab07bd1ba6eca085aaf8e45a734af92aad0c42d9dc7aaf",
strip_prefix = "rules_proto-218ffa7dfa5408492dc86c01ee637614f8695c45",
urls = ["https://github.com/bazelbuild/rules_proto/archive/218ffa7dfa5408492dc86c01ee637614f8695c45.tar.gz"],
sha256 = "303e86e722a520f6f326a50b41cfc16b98fe6d1955ce46642a5b7a67c11c0f5d",
strip_prefix = "rules_proto-6.0.0",
url = "https://github.com/bazelbuild/rules_proto/releases/download/6.0.0/rules_proto-6.0.0.tar.gz",
)

if not native.existing_rule("rules_cc"):
http_archive(
name = "rules_cc",
sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
strip_prefix = "rules_cc-0.0.9",
url = "https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz",
)
1 change: 0 additions & 1 deletion example-workspace/.bazelrc

This file was deleted.

1 change: 1 addition & 0 deletions example-workspace/.bazelrc
1 change: 1 addition & 0 deletions example-workspace/.bazelversion
27 changes: 15 additions & 12 deletions example-workspace/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
load("@rules_python//python:defs.bzl", "py_binary")
# Example python binary that uses the dynamic python validation code.
#
# Exactly as example_cc above except different label. Example:
#
# bazel run //:example_py -- $(pwd)/valid.textproto

# Example C++ binary that uses the generated validation code.
#
# This binary attempts to read files named on the command line as binary protos.
Expand Down Expand Up @@ -26,37 +33,33 @@ cc_binary(
deps = ["//foo:bar_cc_proto"],
)

# Example python binary that uses the dynamic python validation code.
#
# Exactly as example_cc above except different label. Example:
#
# bazel run //:example_py -- $(pwd)/valid.textproto

py_binary(
name = "example_py",
srcs = ["example.py"],
main = "example.py",
srcs_version = "PY3",
deps = [
"@com_google_protobuf//:protobuf_python",
"@com_envoyproxy_protoc_gen_validate//python:validator_py",
"//foo:bar_py_proto",
"@com_envoyproxy_protoc_gen_validate//python:validator_py",
],
)

# Test that the example textproto inputs evoke the right responses.
[
sh_test(
name = "example_{lang}_test_{which}".format(lang=lang, which=which),
name = "example_{lang}_test_{which}".format(
lang = lang,
which = which,
),
srcs = ["example_test.sh"],
args = [
"$(location :example_{lang})".format(lang=lang),
"$(location :example_{lang})".format(lang = lang),
str(code),
"$(location :{which})".format(which=which),
"$(location :{which})".format(which = which),
],
data = [
which,
":example_{lang}".format(lang=lang),
":example_{lang}".format(lang = lang),
],
)
for lang in (
Expand Down
9 changes: 6 additions & 3 deletions example-workspace/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ load("@com_envoyproxy_protoc_gen_validate//bazel:repositories.bzl", "pgv_depende

pgv_dependencies()

load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

# Perform any necessary actions to initialize dependencies.
load("@com_envoyproxy_protoc_gen_validate//bazel:dependency_imports.bzl", "pgv_dependency_imports")

pgv_dependency_imports()

load("@com_envoyproxy_protoc_gen_validate//:dependencies.bzl", "go_third_party")
load("@com_envoyproxy_protoc_gen_validate//bazel:extra_dependency_imports.bzl", "pgv_extra_dependency_imports")

# gazelle:repository_macro dependencies.bzl%go_third_party
go_third_party()
pgv_extra_dependency_imports()
8 changes: 2 additions & 6 deletions example-workspace/foo/BUILD
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")
load("@rules_python//python:proto.bzl", "py_proto_library")

package(
default_visibility = ["//visibility:public"],
Expand Down Expand Up @@ -35,9 +35,5 @@ cc_test(

py_proto_library(
name = "bar_py_proto",
srcs = ["bar.proto"],
deps = [
"@com_envoyproxy_protoc_gen_validate//validate:validate_py",
"@com_google_protobuf//:protobuf_python",
],
deps = ["bar_proto"],
)
2 changes: 1 addition & 1 deletion java/pgv-java-stub/src/main/java/io/envoyproxy/pgv/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ java_library(
srcs = glob(["*.java"]),
visibility = ["//visibility:public"],
deps = [
"//external:guava",
"@com_google_guava//jar",
"@com_google_protobuf//:protobuf_java",
"@com_google_protobuf//:protobuf_java_util",
"@com_google_re2j//jar",
Expand Down
9 changes: 8 additions & 1 deletion python/BUILD
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
load("@rules_python//python:defs.bzl", "py_library")
load("@pgv_pip_deps//:requirements.bzl", "all_requirements")
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

exports_files([
"requirements.txt",
"requirements.in",
"setup.cfg",
])

compile_pip_requirements(
name = "requirements",
src = "requirements.in",
)

py_library(
name = "validator_py",
srcs = glob(["**/*.py"]),
Expand Down
2 changes: 1 addition & 1 deletion python/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# See requirements_test.py for more context.
validate-email>=1.3
Jinja2>=2.11.1
protobuf>=3.6.1
protobuf>=5.27.0
astunparse>=1.6.3
Loading

0 comments on commit bbd0e25

Please sign in to comment.