fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreMoved rust extension rules into a separate `rules_rust_ext` workspace. (#3007)2bad96d9

main
4.0 KiB120 linesraw
1"""# rules_rust_protobuf
2
3These build rules are used for building [protobufs][protobuf]/[gRPC][grpc] in [Rust][rust] with Bazel
4using [`rust-protobuf`].
5
6[rust]: http://www.rust-lang.org/
7[protobuf]: https://developers.google.com/protocol-buffers/
8[grpc]: https://grpc.io
9[`rust-protobuf`]: https://github.com/stepancheg/rust-protobuf/
10
11## Setup
12
13To use the Rust proto rules, add the following to your `WORKSPACE` file to add the
14external repositories for the Rust proto toolchain (in addition to the [rust rules setup](..)):
15
16```python
17load("@rules_rust//proto/protobuf:repositories.bzl", "rust_proto_protobuf_dependencies", "rust_proto_protobuf_register_toolchains")
18
19rust_proto_protobuf_dependencies()
20
21rust_proto_protobuf_register_toolchains()
22
23load("@rules_rust//proto/protobuf:transitive_repositories.bzl", "rust_proto_protobuf_transitive_repositories")
24
25rust_proto_protobuf_transitive_repositories()
26```
27
28This will load the required dependencies for the [`rust-protobuf`] rules. It will also
29register a default toolchain for the `rust_proto_library` and `rust_grpc_library` rules.
30
31To customize the `rust_proto_library` and `rust_grpc_library` toolchain, please see the section
32[Customizing `rust-protobuf` Dependencies](#custom-proto-deps).
33
34For additional information about Bazel toolchains, see [here](https://docs.bazel.build/versions/master/toolchains.html).
35
36#### <a name="custom-proto-deps">Customizing `rust-protobuf` Dependencies
37
38These rules depend on the [`protobuf`](https://crates.io/crates/protobuf) and
39the [`grpc`](https://crates.io/crates/grpc) crates in addition to the [protobuf
40compiler](https://github.com/google/protobuf). To obtain these crates,
41`rust_proto_repositories` imports the given crates using BUILD files generated with
42[crate_universe](./crate_universe.md).
43
44If you want to either change the protobuf and gRPC rust compilers, or to
45simply use [crate_universe](./crate_universe.md) in a more
46complex scenario (with more dependencies), you must redefine those
47dependencies.
48
49To do this, once you've imported the needed dependencies (see our
50[@rules_rust//proto/protobuf/3rdparty/BUILD.bazel](https://github.com/bazelbuild/rules_rust/blob/main/proto/protobuf/3rdparty/BUILD.bazel)
51file to see the default dependencies), you need to create your own toolchain.
52To do so you can create a BUILD file with your toolchain definition, for example:
53
54```python
55load("@rules_rust//proto:toolchain.bzl", "rust_proto_toolchain")
56
57rust_proto_toolchain(
58    name = "proto-toolchain-impl",
59    # Path to the protobuf compiler.
60    protoc = "@com_google_protobuf//:protoc",
61    # Protobuf compiler plugin to generate rust gRPC stubs.
62    grpc_plugin = "//3rdparty/crates:cargo_bin_protoc_gen_rust_grpc",
63    # Protobuf compiler plugin to generate rust protobuf stubs.
64    proto_plugin = "//3rdparty/crates:cargo_bin_protoc_gen_rust",
65)
66
67toolchain(
68    name = "proto-toolchain",
69    toolchain = ":proto-toolchain-impl",
70    toolchain_type = "@rules_rust//proto/protobuf:toolchain_type",
71)
72```
73
74Now that you have your own toolchain, you need to register it by
75inserting the following statement in your `WORKSPACE` file:
76
77```python
78register_toolchains("//my/toolchains:proto-toolchain")
79```
80
81Finally, you might want to set the `rust_deps` attribute in
82`rust_proto_library` and `rust_grpc_library` to change the compile-time
83dependencies:
84
85```python
86rust_proto_library(
87    ...
88    rust_deps = ["//3rdparty/crates:protobuf"],
89    ...
90)
91
92rust_grpc_library(
93    ...
94    rust_deps = [
95        "//3rdparty/crates:protobuf",
96        "//3rdparty/crates:grpc",
97        "//3rdparty/crates:tls_api",
98        "//3rdparty/crates:tls_api_stub",
99    ],
100    ...
101)
102```
103
104__Note__: Ideally, we would inject those dependencies from the toolchain,
105but due to [bazelbuild/bazel#6889](https://github.com/bazelbuild/bazel/issues/6889)
106all dependencies added via the toolchain ends-up being in the wrong
107configuration.
108
109---
110---
111"""
112
113load(
114    ":proto.bzl",
115    _rust_grpc_library = "rust_grpc_library",
116    _rust_proto_library = "rust_proto_library",
117)
118
119rust_grpc_library = _rust_grpc_library
120rust_proto_library = _rust_proto_library