Skip to content

Commit 0c6e76e

Browse files
freeformstuillicitonion
authored andcommitted
Add Prost and Tonic rules. (bazelbuild#2033)
* Setup Prost and Tonic rules. * Regenerate documentation * Add more tests. * Add more tests and address feedback. * Regenerate documentation * Add to proto docs page. * Bump min supported bazel version. * buildifier * Always enable backtracing. * Add more info to failing rename. * Set min rust version to 1.62.0 * Handle rust keywords as package names. * exclude windows from prost toolchain support. * buildifier * redundant * Use prost-types to parse the file descriptor set. * Cleanup and more tests. * Move prost-types to toolchain definition. * fix rustfmt * Add example of building protos with complex imports * impl Display * Fix all tests * Add rust checks for the complex import protos. * Address feedback * Fix buildifier * Depend on remote-apis repo. * Remove bazel remote apis due to file length and transitive dependency issues. * Update patch and docs. * Regenerate documentation * Regenerate documentation * Update docs. --------- Co-authored-by: Daniel Wagner-Hall <[email protected]>
1 parent 997beb4 commit 0c6e76e

File tree

185 files changed

+18628
-40
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+18628
-40
lines changed

.bazelci/presubmit.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ aspects_flags: &aspects_flags
33
- "--config=rustfmt"
44
- "--config=clippy"
55
min_rust_version_shell_commands: &min_rust_version_shell_commands
6-
- sed -i 's|^rust_register_toolchains(|rust_register_toolchains(versions = ["1.59.0"],\n|' WORKSPACE.bazel
6+
- sed -i 's|^rust_register_toolchains(|rust_register_toolchains(versions = ["1.62.0"],\n|' WORKSPACE.bazel
77
nightly_flags: &nightly_flags
88
- "--//rust/toolchain/channel=nightly"
99
nightly_aspects_flags: &nightly_aspects_flags
@@ -205,15 +205,15 @@ tasks:
205205
ubuntu1804:
206206
name: "Min Bazel Version"
207207
# If updating the minimum bazel version, please also update /docs/index.md
208-
bazel: "5.2.0"
208+
bazel: "6.0.0"
209209
platform: ubuntu1804
210210
build_targets: *default_linux_targets
211211
test_targets: *default_linux_targets
212212
coverage_targets: *default_linux_targets
213213
post_shell_commands: *coverage_validation_post_shell_commands
214214
ubuntu1804_with_aspects:
215215
name: "Min Bazel Version With Aspects"
216-
bazel: "5.2.0"
216+
bazel: "6.0.0"
217217
platform: ubuntu1804
218218
build_targets: *default_linux_targets
219219
test_targets: *default_linux_targets

WORKSPACE.bazel

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencie
1010

1111
crate_universe_dependencies(bootstrap = True)
1212

13-
load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories")
13+
load("@rules_rust//proto:repositories.bzl", "rust_proto_dependencies", "rust_proto_register_toolchains")
1414

15-
rust_proto_repositories()
15+
rust_proto_dependencies()
16+
17+
rust_proto_register_toolchains()
1618

1719
load("@rules_rust//proto:transitive_repositories.bzl", "rust_proto_transitive_repositories")
1820

1921
rust_proto_transitive_repositories()
2022

23+
register_toolchains("@rules_rust//proto/prost/private/...")
24+
2125
load("@rules_rust//bindgen:repositories.bzl", "rust_bindgen_dependencies", "rust_bindgen_register_toolchains")
2226

2327
rust_bindgen_dependencies()

crate_universe/private/crates_vendor.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ bazel run //3rdparty:crates_vendor -- --repin
346346
```
347347
348348
Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html)
349-
call against the generated workspace. The following table describes how to controll particular values passed to the
349+
call against the generated workspace. The following table describes how to control particular values passed to the
350350
`cargo update` command.
351351
352352
| Value | Cargo command |

docs/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ PAGES = dict([
121121
"rust_proto_repositories",
122122
"rust_proto_transitive_repositories",
123123
"rust_proto_toolchain",
124+
"rust_prost_library",
125+
"rust_tonic_library",
124126
],
125127
),
126128
page(

docs/crate_universe.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ bazel run //3rdparty:crates_vendor -- --repin
462462
```
463463

464464
Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html)
465-
call against the generated workspace. The following table describes how to controll particular values passed to the
465+
call against the generated workspace. The following table describes how to control particular values passed to the
466466
`cargo update` command.
467467

468468
| Value | Cargo command |

docs/flatten.md

+40
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* [rust_library](#rust_library)
3232
* [rust_library_group](#rust_library_group)
3333
* [rust_proc_macro](#rust_proc_macro)
34+
* [rust_prost_library](#rust_prost_library)
3435
* [rust_proto_library](#rust_proto_library)
3536
* [rust_proto_repositories](#rust_proto_repositories)
3637
* [rust_proto_toolchain](#rust_proto_toolchain)
@@ -43,6 +44,7 @@
4344
* [rust_stdlib_filegroup](#rust_stdlib_filegroup)
4445
* [rust_test](#rust_test)
4546
* [rust_test_suite](#rust_test_suite)
47+
* [rust_tonic_library](#rust_tonic_library)
4648
* [rust_toolchain](#rust_toolchain)
4749
* [rust_toolchain_repository](#rust_toolchain_repository)
4850
* [rust_toolchain_repository_proxy](#rust_toolchain_repository_proxy)
@@ -1794,6 +1796,25 @@ Registers the default toolchains for the `rules_rust` [bindgen][bg] rules.
17941796
| <a id="rust_bindgen_register_toolchains-register_toolchains"></a>register_toolchains | Whether or not to register toolchains. | `True` |
17951797

17961798

1799+
<a id="rust_prost_library"></a>
1800+
1801+
## rust_prost_library
1802+
1803+
<pre>
1804+
rust_prost_library(<a href="#rust_prost_library-name">name</a>, <a href="#rust_prost_library-kwargs">kwargs</a>)
1805+
</pre>
1806+
1807+
A rule for generating a Rust library using Prost.
1808+
1809+
**PARAMETERS**
1810+
1811+
1812+
| Name | Description | Default Value |
1813+
| :------------- | :------------- | :------------- |
1814+
| <a id="rust_prost_library-name"></a>name | The name of the target. | none |
1815+
| <a id="rust_prost_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying <code>rust_prost_library</code> rule. | none |
1816+
1817+
17971818
<a id="rust_proto_repositories"></a>
17981819

17991820
## rust_proto_repositories
@@ -1998,6 +2019,25 @@ rust_test_suite(
19982019
| <a id="rust_test_suite-kwargs"></a>kwargs | Additional keyword arguments for the underyling [rust_test](#rust_test) targets. The <code>tags</code> argument is also passed to the generated <code>test_suite</code> target. | none |
19992020

20002021

2022+
<a id="rust_tonic_library"></a>
2023+
2024+
## rust_tonic_library
2025+
2026+
<pre>
2027+
rust_tonic_library(<a href="#rust_tonic_library-name">name</a>, <a href="#rust_tonic_library-kwargs">kwargs</a>)
2028+
</pre>
2029+
2030+
A rule for generating a Rust library using Prost and Tonic.
2031+
2032+
**PARAMETERS**
2033+
2034+
2035+
| Name | Description | Default Value |
2036+
| :------------- | :------------- | :------------- |
2037+
| <a id="rust_tonic_library-name"></a>name | The name of the target. | none |
2038+
| <a id="rust_tonic_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying <code>rust_tonic_library</code> rule. | none |
2039+
2040+
20012041
<a id="rust_toolchain_repository"></a>
20022042

20032043
## rust_toolchain_repository

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Failure to do so will result in rules attempting to match a `stable` toolchain w
9393

9494
## Supported bazel versions
9595

96-
The oldest version of Bazel the `main` branch is tested against is `5.2.0`. Previous versions may still be functional in certain environments, but this is the minimum version we strive to fully support.
96+
The oldest version of Bazel the `main` branch is tested against is `6.0.0`. Previous versions may still be functional in certain environments, but this is the minimum version we strive to fully support.
9797

9898
We test these rules against the latest rolling releases of Bazel, and aim for compatibility with them, but prioritise stable releases over rolling releases where necessary.
9999

docs/rust_proto.md

+162-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66
* [rust_proto_repositories](#rust_proto_repositories)
77
* [rust_proto_transitive_repositories](#rust_proto_transitive_repositories)
88
* [rust_proto_toolchain](#rust_proto_toolchain)
9+
* [rust_prost_library](#rust_prost_library)
10+
* [rust_tonic_library](#rust_tonic_library)
911

1012

1113
## Overview
12-
1314
These build rules are used for building [protobufs][protobuf]/[gRPC][grpc] in [Rust][rust] with Bazel.
1415

16+
There are two rule sets. The first ruleset defines the `rust_proto_library` and `rust_grpc_library`
17+
rules which generate Rust code using the [`rust-protobuf`] dependencies. The second ruleset defines
18+
the `rust_prost_library` and `rust_tonic_library` rules which generate Rust code using the [`prost`]
19+
and [`tonic`] dependencies respectively.
20+
1521
[rust]: http://www.rust-lang.org/
1622
[protobuf]: https://developers.google.com/protocol-buffers/
1723
[grpc]: https://grpc.io
24+
[`rust-protobuf`]: https://github.com/stepancheg/rust-protobuf/
1825

1926
See the [protobuf example](../examples/proto) for a more complete example of use.
2027

@@ -33,17 +40,21 @@ load("@rules_rust//proto:transitive_repositories.bzl", "rust_proto_transitive_re
3340
rust_proto_transitive_repositories()
3441
```
3542

36-
This will load crate dependencies of protobuf that are generated using
37-
[crate_universe](./crate_universe.md) inside the rules_rust
38-
repository. However, using those dependencies might conflict with other uses
39-
of [crate_universe](./crate_universe.md). If you need to change
40-
those dependencies, please see the [dedicated section below](#custom-deps).
43+
This will load the required dependencies for the Proto, Prost, and Tonic rules. It will also
44+
register a default toolchain for the `rust_proto_library` and `rust_grpc_library` rules. The
45+
`prost` and `tonic` rules do not specify a default toolchain in order to avoid mismatched
46+
dependency issues.
47+
48+
To customize the `rust_proto_library` and `rust_grpc_library` toolchain, please see the section
49+
[Customizing `rust-protobuf` Dependencies](#custom-proto-deps).
50+
51+
To setup the `prost` and `tonic` toolchain, please see the section [Customizing `prost` and `tonic` Dependencies](#custom-prost-deps).
4152

4253
For additional information about Bazel toolchains, see [here](https://docs.bazel.build/versions/master/toolchains.html).
4354

44-
## <a name="custom-deps">Customizing dependencies
55+
## <a name="custom-proto-deps">Customizing `rust-protobuf` Dependencies
4556

46-
These rules depends on the [`protobuf`](https://crates.io/crates/protobuf) and
57+
These rules depend on the [`protobuf`](https://crates.io/crates/protobuf) and
4758
the [`grpc`](https://crates.io/crates/grpc) crates in addition to the [protobuf
4859
compiler](https://github.com/google/protobuf). To obtain these crates,
4960
`rust_proto_repositories` imports the given crates using BUILD files generated with
@@ -114,6 +125,111 @@ but due to [bazelbuild/bazel#6889](https://github.com/bazelbuild/bazel/issues/68
114125
all dependencies added via the toolchain ends-up being in the wrong
115126
configuration.
116127

128+
## <a name="custom-prost-deps">Customizing `prost` and `tonic` Dependencies
129+
130+
These rules depend on the [`prost`] and [`tonic`] dependencies. To setup the necessary toolchain
131+
for these rules, you must define a toolchain with the [`prost`], [`prost-types`], [`tonic`],[`protoc-gen-prost`], and [`protoc-gen-tonic`] crates as well as the [`protoc`] binary.
132+
133+
[`prost`]: https://crates.io/crates/prost
134+
[`prost-types`]: https://crates.io/crates/prost-types
135+
[`protoc-gen-prost`]: https://crates.io/crates/protoc-gen-prost
136+
[`protoc-gen-tonic`]: https://crates.io/crates/protoc-gen-tonic
137+
[`tonic`]: https://crates.io/crates/tonic
138+
[`protoc`]: https://github.com/protocolbuffers/protobuf
139+
140+
To get access to these crates, you can use the [crate_universe](./crate_universe.md) repository
141+
rules. For example:
142+
143+
```python
144+
load("//crate_universe:defs.bzl", "crate", "crates_repository")
145+
146+
crates_repository(
147+
name = "crates_io",
148+
annotations = {
149+
"protoc-gen-prost": [crate.annotation(
150+
gen_binaries = ["protoc-gen-prost"],
151+
patch_args = [
152+
"-p1",
153+
],
154+
patches = [
155+
# Note: You will need to use this patch until a version greater than `0.2.2` of
156+
# `protoc-gen-prost` is released.
157+
"@rules_rust//proto/prost/private/3rdparty/patches:protoc-gen-prost.patch",
158+
],
159+
)],
160+
"protoc-gen-tonic": [crate.annotation(
161+
gen_binaries = ["protoc-gen-tonic"],
162+
)],
163+
},
164+
cargo_lockfile = "Cargo.Bazel.lock",
165+
mode = "remote",
166+
packages = {
167+
"prost": crate.spec(
168+
version = "0",
169+
),
170+
"prost-types": crate.spec(
171+
version = "0",
172+
),
173+
"protoc-gen-prost": crate.spec(
174+
version = "0",
175+
),
176+
"protoc-gen-tonic": crate.spec(
177+
version = "0",
178+
),
179+
"tonic": crate.spec(
180+
version = "0",
181+
),
182+
},
183+
repository_name = "rules_rust_prost",
184+
tags = ["manual"],
185+
)
186+
```
187+
188+
You can then define a toolchain with the `rust_prost_toolchain` rule which uses the crates
189+
defined above. For example:
190+
191+
```python
192+
load("@rules_rust//proto/prost:defs.bzl", "rust_prost_toolchain")
193+
load("@rules_rust//rust:defs.bzl", "rust_library_group")
194+
195+
rust_library_group(
196+
name = "prost_runtime",
197+
deps = [
198+
"@crates_io//:prost",
199+
],
200+
)
201+
202+
rust_library_group(
203+
name = "tonic_runtime",
204+
deps = [
205+
":prost_runtime",
206+
"@crates_io//:tonic",
207+
],
208+
)
209+
210+
rust_prost_toolchain(
211+
name = "prost_toolchain_impl",
212+
prost_plugin = "@crates_io//:protoc-gen-prost__protoc-gen-prost",
213+
prost_runtime = ":prost_runtime",
214+
prost_types = "@crates_io//:prost-types",
215+
proto_compiler = "@com_google_protobuf//:protoc",
216+
tonic_plugin = "@crates_io//:protoc-gen-tonic__protoc-gen-tonic",
217+
tonic_runtime = ":tonic_runtime",
218+
)
219+
220+
toolchain(
221+
name = "prost_toolchain",
222+
toolchain = "default_prost_toolchain_impl",
223+
toolchain_type = "//proto/prost:toolchain_type",
224+
)
225+
```
226+
227+
Lastly, you must register the toolchain in your `WORKSPACE` file. For example:
228+
229+
```python
230+
register_toolchains("//toolchains:prost_toolchain")
231+
```
232+
117233

118234
<a id="rust_grpc_library"></a>
119235

@@ -260,6 +376,25 @@ See @rules_rust//proto:BUILD for examples of defining the toolchain.
260376
| <a id="rust_proto_toolchain-protoc"></a>protoc | The location of the <code>protoc</code> binary. It should be an executable target. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>@com_google_protobuf//:protoc</code> |
261377

262378

379+
<a id="rust_prost_library"></a>
380+
381+
## rust_prost_library
382+
383+
<pre>
384+
rust_prost_library(<a href="#rust_prost_library-name">name</a>, <a href="#rust_prost_library-kwargs">kwargs</a>)
385+
</pre>
386+
387+
A rule for generating a Rust library using Prost.
388+
389+
**PARAMETERS**
390+
391+
392+
| Name | Description | Default Value |
393+
| :------------- | :------------- | :------------- |
394+
| <a id="rust_prost_library-name"></a>name | The name of the target. | none |
395+
| <a id="rust_prost_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying <code>rust_prost_library</code> rule. | none |
396+
397+
263398
<a id="rust_proto_repositories"></a>
264399

265400
## rust_proto_repositories
@@ -292,3 +427,22 @@ This macro should be called immediately after the `rust_proto_repositories` macr
292427

293428

294429

430+
<a id="rust_tonic_library"></a>
431+
432+
## rust_tonic_library
433+
434+
<pre>
435+
rust_tonic_library(<a href="#rust_tonic_library-name">name</a>, <a href="#rust_tonic_library-kwargs">kwargs</a>)
436+
</pre>
437+
438+
A rule for generating a Rust library using Prost and Tonic.
439+
440+
**PARAMETERS**
441+
442+
443+
| Name | Description | Default Value |
444+
| :------------- | :------------- | :------------- |
445+
| <a id="rust_tonic_library-name"></a>name | The name of the target. | none |
446+
| <a id="rust_tonic_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying <code>rust_tonic_library</code> rule. | none |
447+
448+

0 commit comments

Comments
 (0)