Skip to content

Commit

Permalink
Add protobuf_extra_deps.bzl for loading dependencies necessary for ex…
Browse files Browse the repository at this point in the history
…ternal repositories in protobuf_deps.bzl.

This is needed for `rules_java` which requires additional loads for Bazel 8. This provides a single entrypoint for extra dependencies so we can add these moving forward without breaking users, and is similar to https://github.com/grpc/grpc/blob/master/bazel/grpc_extra_deps.bzl.

Non-bzlmod users should make sure to set the following in their WORKSPACE file:
```
http_archive(
    name = "protobuf",
    strip_prefix = "protobuf-VERSION",
    sha256 = ...,
    url = ...,
)

load("@protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")

protobuf_extra_deps();
```

PiperOrigin-RevId: 700448687
  • Loading branch information
zhangskz authored and copybara-github committed Nov 27, 2024
1 parent 37cc68f commit 235897e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 37 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,48 @@ of your project, you should pin to a release commit on a release branch.
This is because even release branches can experience some instability in between
release commits.

### Bazel with Bzlmod

Protobuf supports
[Bzlmod](https://bazel.build/external/module) with Bazel 7 +.
Users should specify a dependency on protobuf in their MODULE.bazel file as
follows.

```
bazel_dep(name = "protobuf", version = <VERSION>)
```

Users can optionally override the repo name, such as for compatibility with
WORKSPACE.

```
bazel_dep(name = "protobuf", version = <VERSION>, repo_name = "com_google_protobuf")
```

### Bazel with WORKSPACE

Users can also add the following to their legacy
[WORKSPACE](https://bazel.build/external/overview#workspace-system) file.

Note that the `protobuf_extra_deps.bzl` is added in the `v30.x` release.

```
http_archive(
name = "com_google_protobuf",
strip_prefix = "protobuf-VERSION",
sha256 = ...,
url = ...,
)
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()
load("@com_google_protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")
protobuf_extra_deps();
```

Protobuf Compiler Installation
------------------------------

Expand Down
49 changes: 26 additions & 23 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ workspace(name = "com_google_protobuf")
# An explicit self-reference to work around changes in Bazel 7.0
# See https://github.com/bazelbuild/bazel/issues/19973#issuecomment-1787814450
# buildifier: disable=duplicated-name
local_repository(name = "com_google_protobuf", path = ".")
local_repository(
name = "com_google_protobuf",
path = ".",
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand All @@ -17,6 +20,10 @@ load("//:protobuf_deps.bzl", "PROTOBUF_MAVEN_ARTIFACTS", "protobuf_deps")

protobuf_deps()

load("//:protobuf_extra_deps.bzl", "protobuf_extra_deps")

protobuf_extra_deps()

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

py_repositories()
Expand All @@ -28,19 +35,19 @@ pip_install_dependencies()
# Bazel platform rules.
http_archive(
name = "platforms",
sha256 = "3a561c99e7bdbe9173aa653fd579fe849f1d8d67395780ab4770b1f381431d51",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.7/platforms-0.0.7.tar.gz",
"https://github.com/bazelbuild/platforms/releases/download/0.0.7/platforms-0.0.7.tar.gz",
],
sha256 = "3a561c99e7bdbe9173aa653fd579fe849f1d8d67395780ab4770b1f381431d51",
)

http_archive(
name = "com_google_googletest",
sha256 = "7315acb6bf10e99f332c8a43f00d5fbb1ee6ca48c52f6b936991b216c586aaad",
strip_prefix = "googletest-1.15.0",
urls = [
"https://github.com/google/googletest/releases/download/v1.15.0/googletest-1.15.0.tar.gz" # 2024-07-15
"https://github.com/google/googletest/releases/download/v1.15.0/googletest-1.15.0.tar.gz", # 2024-07-15
],
)

Expand Down Expand Up @@ -91,12 +98,6 @@ load("@build_bazel_apple_support//lib:repositories.bzl", "apple_support_dependen

apple_support_dependencies()

load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")

rules_java_dependencies()

rules_java_toolchains()

load("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies")

rules_cc_dependencies()
Expand All @@ -112,11 +113,11 @@ kt_register_toolchains()

http_archive(
name = "rules_ruby",
integrity = "sha256-Lh/xxR6WsKJnS92sYkpJDBtdS6DNrCbi0kuUxBffG6E=",
strip_prefix = "rules_ruby-588d9dd40487277e2560ece09fe310d7c0ecb4a6",
urls = [
"https://github.com/protocolbuffers/rules_ruby/archive/588d9dd40487277e2560ece09fe310d7c0ecb4a6.zip"
"https://github.com/protocolbuffers/rules_ruby/archive/588d9dd40487277e2560ece09fe310d7c0ecb4a6.zip",
],
strip_prefix = "rules_ruby-588d9dd40487277e2560ece09fe310d7c0ecb4a6",
integrity = "sha256-Lh/xxR6WsKJnS92sYkpJDBtdS6DNrCbi0kuUxBffG6E=",
)

load("@rules_ruby//ruby:defs.bzl", "ruby_runtime")
Expand Down Expand Up @@ -157,18 +158,18 @@ http_archive(

http_archive(
name = "com_github_google_benchmark",
urls = ["https://github.com/google/benchmark/archive/0baacde3618ca617da95375e0af13ce1baadea47.zip"],
strip_prefix = "benchmark-0baacde3618ca617da95375e0af13ce1baadea47",
sha256 = "62e2f2e6d8a744d67e4bbc212fcfd06647080de4253c97ad5c6749e09faf2cb0",
strip_prefix = "benchmark-0baacde3618ca617da95375e0af13ce1baadea47",
urls = ["https://github.com/google/benchmark/archive/0baacde3618ca617da95375e0af13ce1baadea47.zip"],
)

http_archive(
name = "com_google_googleapis",
urls = ["https://github.com/googleapis/googleapis/archive/d81d0b9e6993d6ab425dff4d7c3d05fb2e59fa57.zip"],
strip_prefix = "googleapis-d81d0b9e6993d6ab425dff4d7c3d05fb2e59fa57",
sha256 = "d986023c3d8d2e1b161e9361366669cac9fb97c2a07e656c2548aca389248bb4",
build_file = "//benchmarks:BUILD.googleapis",
patch_cmds = ["find google -type f -name BUILD.bazel -delete"],
sha256 = "d986023c3d8d2e1b161e9361366669cac9fb97c2a07e656c2548aca389248bb4",
strip_prefix = "googleapis-d81d0b9e6993d6ab425dff4d7c3d05fb2e59fa57",
urls = ["https://github.com/googleapis/googleapis/archive/d81d0b9e6993d6ab425dff4d7c3d05fb2e59fa57.zip"],
)

load("@system_python//:pip.bzl", "pip_parse")
Expand All @@ -184,11 +185,11 @@ install_deps()

http_archive(
name = "rules_fuzzing",
patch_args = ["-p1"],
patches = ["//third_party:rules_fuzzing.patch"],
sha256 = "77206c54b71f4dd5335123a6ff2a8ea688eca5378d34b4838114dff71652cf26",
strip_prefix = "rules_fuzzing-0.5.1",
urls = ["https://github.com/bazelbuild/rules_fuzzing/releases/download/v0.5.1/rules_fuzzing-0.5.1.zip"],
patches = ["//third_party:rules_fuzzing.patch"],
patch_args = ["-p1"],
)

load("@rules_fuzzing//fuzzing:repositories.bzl", "rules_fuzzing_dependencies")
Expand All @@ -214,7 +215,9 @@ load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_regi
rules_rust_dependencies()

rust_register_toolchains(edition = "2021")

load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository")

# to repin, invoke `CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index`
crates_repository(
name = "crate_index",
Expand All @@ -226,12 +229,13 @@ crates_repository(
rev = "b407f3b5774defb8917d714bfb7af485e117d621",
),
"paste": crate.spec(
version = ">=1",
version = ">=1",
),
},
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

# For testing runtime against old gencode from a previous major version.
Expand All @@ -242,11 +246,10 @@ http_archive(
)

# Needed as a dependency of @com_google_protobuf_v25.0
load("@com_google_protobuf_v25.0//:protobuf_deps.bzl", protobuf_v25_deps="protobuf_deps")
load("@com_google_protobuf_v25.0//:protobuf_deps.bzl", protobuf_v25_deps = "protobuf_deps")

protobuf_v25_deps()

# Needed for testing only
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_testing",
sha256 = "02c62574631876a4e3b02a1820cb51167bb9cdcdea2381b2fa9d9b8b11c407c4",
Expand Down
14 changes: 2 additions & 12 deletions examples/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

http_archive(
name = "rules_java",
sha256 = "9b9614f8a7f7b7ed93cb7975d227ece30fe7daed2c0a76f03a5ee37f69e437de",
urls = [
"https://github.com/bazelbuild/rules_java/releases/download/8.3.2/rules_java-8.3.2.tar.gz",
],
)

load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")

rules_java_dependencies()
load("@com_google_protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")

rules_java_toolchains()
protobuf_extra_deps()
8 changes: 6 additions & 2 deletions protobuf_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ and sets up the repositories protobuf needs:
```
http_archive(
name = "protobuf",
name = "com_google_protobuf",
strip_prefix = "protobuf-VERSION",
sha256 = ...,
url = ...,
)
load("@protobuf//:protobuf_deps.bzl", "protobuf_deps")
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()
load("@com_google_protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")
protobuf_extra_deps();
```
"""

Expand Down
31 changes: 31 additions & 0 deletions protobuf_extra_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Loads the dependencies necessary for the external repositories defined in protobuf_deps.bzl.
The consumers should use the following WORKSPACE snippet, which loads dependencies
and sets up the repositories protobuf needs:
```
http_archive(
name = "com_google_protobuf",
strip_prefix = "protobuf-VERSION",
sha256 = ...,
url = ...,
)
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()
load("@com_google_protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")
protobuf_extra_deps();
```
"""

load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")

def protobuf_extra_deps():
"""Loads extra dependencies needed for the external repositories defined in protobuf_deps.bzl."""

rules_java_dependencies()

rules_java_toolchains()

0 comments on commit 235897e

Please sign in to comment.