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

Add bzlmod support #1112

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example-workspace
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bazel-*
MODULE.bazel.lock
**/.DS_Store
!vendor/*

Expand Down
45 changes: 45 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module(
name = "protoc-gen-validate",
repo_name = "com_envoyproxy_protoc_gen_validate",
version = "0",
compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "gazelle", version = "0.36.0", repo_name = "bazel_gazelle")
bazel_dep(name = "protobuf", version = "27.0-rc2", repo_name = "com_google_protobuf")
bazel_dep(name = "re2", version = "2021-09-01", repo_name = "com_googlesource_code_re2")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_go", version = "0.48.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "rules_java", version = "7.4.0")
bazel_dep(name = "rules_proto", version = "6.0.0")
bazel_dep(name = "rules_python", version = "0.32.2")

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.21.1")

go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"com_github_iancoleman_strcase",
"com_github_lyft_protoc_gen_star_v2",
"org_golang_google_protobuf",
"org_golang_x_net",
)

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "pgv_pip_deps",
python_version = "3.11",
requirements_lock = "//python:requirements.txt",
)
use_repo(pip, "pgv_pip_deps")

non_module_deps = use_extension("//bazel:repositories.bzl", "non_module_deps")
use_repo(
non_module_deps,
"com_google_guava",
"com_google_re2j",
"org_apache_commons_validator",
)
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()
Empty file added WORKSPACE.bzlmod
Empty file.
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"],
)
135 changes: 51 additions & 84 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
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")

_DEFAULT_REPOSITORIES = ["https://repo.maven.apache.org/maven2"]

def _non_module_deps(maven_repos = _DEFAULT_REPOSITORIES):
if not native.existing_rule("com_google_re2j"):
jvm_maven_import_external(
name = "com_google_re2j",
artifact = "com.google.re2j:re2j:1.2",
artifact_sha256 = "e9dc705fd4c570344b54a7146b2e3a819cdc271a29793f4acc1a93b56a388e59",
server_urls = maven_repos,
)

if not native.existing_rule("com_google_guava"):
jvm_maven_import_external(
name = "com_google_guava",
artifact = "com.google.guava:guava:27.0-jre",
artifact_sha256 = "63b09db6861011e7fb2481be7790c7fd4b03f0bb884b3de2ecba8823ad19bf3f",
server_urls = maven_repos,
)

if not native.existing_rule("org_apache_commons_validator"):
jvm_maven_import_external(
name = "org_apache_commons_validator",
artifact = "commons-validator:commons-validator:1.6",
artifact_sha256 = "bd62795d7068a69cbea333f6dbf9c9c1a6ad7521443fb57202a44874f240ba25",
server_urls = maven_repos,
)

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,22 +72,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",
artifact = "com.google.re2j:re2j:1.2",
artifact_sha256 = "e9dc705fd4c570344b54a7146b2e3a819cdc271a29793f4acc1a93b56a388e59",
server_urls = maven_repos,
)

if not native.existing_rule("com_googlesource_code_re2"):
http_archive(
name = "com_googlesource_code_re2",
Expand All @@ -75,68 +80,30 @@ def pgv_dependencies(maven_repos = _DEFAULT_REPOSITORIES):
urls = ["https://github.com/google/re2/archive/2020-07-06.tar.gz"],
)

if not native.existing_rule("com_google_guava"):
jvm_maven_import_external(
name = "com_google_guava",
artifact = "com.google.guava:guava:27.0-jre",
artifact_sha256 = "63b09db6861011e7fb2481be7790c7fd4b03f0bb884b3de2ecba8823ad19bf3f",
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",
artifact = "commons-validator:commons-validator:1.6",
artifact_sha256 = "bd62795d7068a69cbea333f6dbf9c9c1a6ad7521443fb57202a44874f240ba25",
server_urls = maven_repos,
)

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",
)

_non_module_deps(maven_repos)

non_module_deps = module_extension(implementation = lambda _: _non_module_deps())
1 change: 0 additions & 1 deletion example-workspace/.bazelrc

This file was deleted.

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
11 changes: 11 additions & 0 deletions example-workspace/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module(name = "pgv_example")

bazel_dep(name = "protoc-gen-validate", repo_name = "com_envoyproxy_protoc_gen_validate")
local_path_override(
module_name = "protoc-gen-validate",
path = "..",
)

bazel_dep(name = "protobuf", version = "27.0-rc2", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_python", version = "0.32.2")
bazel_dep(name = "re2", version = "2021-09-01", repo_name = "com_googlesource_code_re2")
Loading