Skip to content

Commit d59a9e1

Browse files
zhangskzcopybara-github
authored andcommitted
Add protobuf_extra_deps.bzl for loading dependencies necessary for external 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: 700810708
1 parent 37cc68f commit d59a9e1

File tree

5 files changed

+107
-37
lines changed

5 files changed

+107
-37
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,48 @@ of your project, you should pin to a release commit on a release branch.
3333
This is because even release branches can experience some instability in between
3434
release commits.
3535

36+
### Bazel with Bzlmod
37+
38+
Protobuf supports
39+
[Bzlmod](https://bazel.build/external/module) with Bazel 7 +.
40+
Users should specify a dependency on protobuf in their MODULE.bazel file as
41+
follows.
42+
43+
```
44+
bazel_dep(name = "protobuf", version = <VERSION>)
45+
```
46+
47+
Users can optionally override the repo name, such as for compatibility with
48+
WORKSPACE.
49+
50+
```
51+
bazel_dep(name = "protobuf", version = <VERSION>, repo_name = "com_google_protobuf")
52+
```
53+
54+
### Bazel with WORKSPACE
55+
56+
Users can also add the following to their legacy
57+
[WORKSPACE](https://bazel.build/external/overview#workspace-system) file.
58+
59+
Note that the `protobuf_extra_deps.bzl` is added in the `v30.x` release.
60+
61+
```
62+
http_archive(
63+
name = "com_google_protobuf",
64+
strip_prefix = "protobuf-VERSION",
65+
sha256 = ...,
66+
url = ...,
67+
)
68+
69+
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
70+
71+
protobuf_deps()
72+
73+
load("@com_google_protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")
74+
75+
protobuf_extra_deps();
76+
```
77+
3678
Protobuf Compiler Installation
3779
------------------------------
3880

WORKSPACE

+26-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ workspace(name = "com_google_protobuf")
33
# An explicit self-reference to work around changes in Bazel 7.0
44
# See https://github.com/bazelbuild/bazel/issues/19973#issuecomment-1787814450
55
# buildifier: disable=duplicated-name
6-
local_repository(name = "com_google_protobuf", path = ".")
6+
local_repository(
7+
name = "com_google_protobuf",
8+
path = ".",
9+
)
710

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

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

1821
protobuf_deps()
1922

23+
load("//:protobuf_extra_deps.bzl", "protobuf_extra_deps")
24+
25+
protobuf_extra_deps()
26+
2027
load("@rules_python//python:repositories.bzl", "py_repositories")
2128

2229
py_repositories()
@@ -28,19 +35,19 @@ pip_install_dependencies()
2835
# Bazel platform rules.
2936
http_archive(
3037
name = "platforms",
38+
sha256 = "3a561c99e7bdbe9173aa653fd579fe849f1d8d67395780ab4770b1f381431d51",
3139
urls = [
3240
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.7/platforms-0.0.7.tar.gz",
3341
"https://github.com/bazelbuild/platforms/releases/download/0.0.7/platforms-0.0.7.tar.gz",
3442
],
35-
sha256 = "3a561c99e7bdbe9173aa653fd579fe849f1d8d67395780ab4770b1f381431d51",
3643
)
3744

3845
http_archive(
3946
name = "com_google_googletest",
4047
sha256 = "7315acb6bf10e99f332c8a43f00d5fbb1ee6ca48c52f6b936991b216c586aaad",
4148
strip_prefix = "googletest-1.15.0",
4249
urls = [
43-
"https://github.com/google/googletest/releases/download/v1.15.0/googletest-1.15.0.tar.gz" # 2024-07-15
50+
"https://github.com/google/googletest/releases/download/v1.15.0/googletest-1.15.0.tar.gz", # 2024-07-15
4451
],
4552
)
4653

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

9299
apple_support_dependencies()
93100

94-
load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
95-
96-
rules_java_dependencies()
97-
98-
rules_java_toolchains()
99-
100101
load("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies")
101102

102103
rules_cc_dependencies()
@@ -112,11 +113,11 @@ kt_register_toolchains()
112113

113114
http_archive(
114115
name = "rules_ruby",
116+
integrity = "sha256-Lh/xxR6WsKJnS92sYkpJDBtdS6DNrCbi0kuUxBffG6E=",
117+
strip_prefix = "rules_ruby-588d9dd40487277e2560ece09fe310d7c0ecb4a6",
115118
urls = [
116-
"https://github.com/protocolbuffers/rules_ruby/archive/588d9dd40487277e2560ece09fe310d7c0ecb4a6.zip"
119+
"https://github.com/protocolbuffers/rules_ruby/archive/588d9dd40487277e2560ece09fe310d7c0ecb4a6.zip",
117120
],
118-
strip_prefix = "rules_ruby-588d9dd40487277e2560ece09fe310d7c0ecb4a6",
119-
integrity = "sha256-Lh/xxR6WsKJnS92sYkpJDBtdS6DNrCbi0kuUxBffG6E=",
120121
)
121122

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

158159
http_archive(
159160
name = "com_github_google_benchmark",
160-
urls = ["https://github.com/google/benchmark/archive/0baacde3618ca617da95375e0af13ce1baadea47.zip"],
161-
strip_prefix = "benchmark-0baacde3618ca617da95375e0af13ce1baadea47",
162161
sha256 = "62e2f2e6d8a744d67e4bbc212fcfd06647080de4253c97ad5c6749e09faf2cb0",
162+
strip_prefix = "benchmark-0baacde3618ca617da95375e0af13ce1baadea47",
163+
urls = ["https://github.com/google/benchmark/archive/0baacde3618ca617da95375e0af13ce1baadea47.zip"],
163164
)
164165

165166
http_archive(
166167
name = "com_google_googleapis",
167-
urls = ["https://github.com/googleapis/googleapis/archive/d81d0b9e6993d6ab425dff4d7c3d05fb2e59fa57.zip"],
168-
strip_prefix = "googleapis-d81d0b9e6993d6ab425dff4d7c3d05fb2e59fa57",
169-
sha256 = "d986023c3d8d2e1b161e9361366669cac9fb97c2a07e656c2548aca389248bb4",
170168
build_file = "//benchmarks:BUILD.googleapis",
171169
patch_cmds = ["find google -type f -name BUILD.bazel -delete"],
170+
sha256 = "d986023c3d8d2e1b161e9361366669cac9fb97c2a07e656c2548aca389248bb4",
171+
strip_prefix = "googleapis-d81d0b9e6993d6ab425dff4d7c3d05fb2e59fa57",
172+
urls = ["https://github.com/googleapis/googleapis/archive/d81d0b9e6993d6ab425dff4d7c3d05fb2e59fa57.zip"],
172173
)
173174

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

185186
http_archive(
186187
name = "rules_fuzzing",
188+
patch_args = ["-p1"],
189+
patches = ["//third_party:rules_fuzzing.patch"],
187190
sha256 = "77206c54b71f4dd5335123a6ff2a8ea688eca5378d34b4838114dff71652cf26",
188191
strip_prefix = "rules_fuzzing-0.5.1",
189192
urls = ["https://github.com/bazelbuild/rules_fuzzing/releases/download/v0.5.1/rules_fuzzing-0.5.1.zip"],
190-
patches = ["//third_party:rules_fuzzing.patch"],
191-
patch_args = ["-p1"],
192193
)
193194

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

216217
rust_register_toolchains(edition = "2021")
218+
217219
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository")
220+
218221
# to repin, invoke `CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index`
219222
crates_repository(
220223
name = "crate_index",
@@ -226,12 +229,13 @@ crates_repository(
226229
rev = "b407f3b5774defb8917d714bfb7af485e117d621",
227230
),
228231
"paste": crate.spec(
229-
version = ">=1",
232+
version = ">=1",
230233
),
231234
},
232235
)
233236

234237
load("@crate_index//:defs.bzl", "crate_repositories")
238+
235239
crate_repositories()
236240

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

244248
# Needed as a dependency of @com_google_protobuf_v25.0
245-
load("@com_google_protobuf_v25.0//:protobuf_deps.bzl", protobuf_v25_deps="protobuf_deps")
249+
load("@com_google_protobuf_v25.0//:protobuf_deps.bzl", protobuf_v25_deps = "protobuf_deps")
250+
246251
protobuf_v25_deps()
247252

248-
# Needed for testing only
249-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
250253
http_archive(
251254
name = "rules_testing",
252255
sha256 = "02c62574631876a4e3b02a1820cb51167bb9cdcdea2381b2fa9d9b8b11c407c4",

examples/WORKSPACE

+2-12
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
4141

4242
protobuf_deps()
4343

44-
http_archive(
45-
name = "rules_java",
46-
sha256 = "9b9614f8a7f7b7ed93cb7975d227ece30fe7daed2c0a76f03a5ee37f69e437de",
47-
urls = [
48-
"https://github.com/bazelbuild/rules_java/releases/download/8.3.2/rules_java-8.3.2.tar.gz",
49-
],
50-
)
51-
52-
load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
53-
54-
rules_java_dependencies()
44+
load("@com_google_protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")
5545

56-
rules_java_toolchains()
46+
protobuf_extra_deps()

protobuf_deps.bzl

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ and sets up the repositories protobuf needs:
55
66
```
77
http_archive(
8-
name = "protobuf",
8+
name = "com_google_protobuf",
99
strip_prefix = "protobuf-VERSION",
1010
sha256 = ...,
1111
url = ...,
1212
)
1313
14-
load("@protobuf//:protobuf_deps.bzl", "protobuf_deps")
14+
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
1515
1616
protobuf_deps()
17+
18+
load("@com_google_protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")
19+
20+
protobuf_extra_deps();
1721
```
1822
"""
1923

protobuf_extra_deps.bzl

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Loads the dependencies necessary for the external repositories defined in protobuf_deps.bzl.
2+
3+
The consumers should use the following WORKSPACE snippet, which loads dependencies
4+
and sets up the repositories protobuf needs:
5+
6+
```
7+
http_archive(
8+
name = "com_google_protobuf",
9+
strip_prefix = "protobuf-VERSION",
10+
sha256 = ...,
11+
url = ...,
12+
)
13+
14+
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
15+
16+
protobuf_deps()
17+
18+
load("@com_google_protobuf//:protobuf_extra_deps.bzl", "protobuf_extra_deps")
19+
20+
protobuf_extra_deps();
21+
```
22+
"""
23+
24+
load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
25+
26+
def protobuf_extra_deps():
27+
"""Loads extra dependencies needed for the external repositories defined in protobuf_deps.bzl."""
28+
29+
rules_java_dependencies()
30+
31+
rules_java_toolchains()

0 commit comments

Comments
 (0)