|
4 | 4 | * [rust_grpc_library](#rust_grpc_library)
|
5 | 5 | * [rust_proto_library](#rust_proto_library)
|
6 | 6 | * [rust_proto_repositories](#rust_proto_repositories)
|
| 7 | +* [rust_proto_transitive_repositories](#rust_proto_transitive_repositories) |
7 | 8 | * [rust_proto_toolchain](#rust_proto_toolchain)
|
8 | 9 |
|
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +These build rules are used for building [protobufs][protobuf]/[gRPC][grpc] in [Rust][rust] with Bazel. |
| 14 | + |
| 15 | +[rust]: http://www.rust-lang.org/ |
| 16 | +[protobuf]: https://developers.google.com/protocol-buffers/ |
| 17 | +[grpc]: https://grpc.io |
| 18 | + |
| 19 | +See the [protobuf example](../examples/proto) for a more complete example of use. |
| 20 | + |
| 21 | +### Setup |
| 22 | + |
| 23 | +To use the Rust proto rules, add the following to your `WORKSPACE` file to add the |
| 24 | +external repositories for the Rust proto toolchain (in addition to the [rust rules setup](..)): |
| 25 | + |
| 26 | +```python |
| 27 | +load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories") |
| 28 | + |
| 29 | +rust_proto_repositories() |
| 30 | + |
| 31 | +load("@rules_rust//proto:transitive_repositories.bzl", "rust_proto_transitive_repositories") |
| 32 | + |
| 33 | +rust_proto_transitive_repositories() |
| 34 | +``` |
| 35 | + |
| 36 | +[raze]: https://github.com/google/cargo-raze |
| 37 | + |
| 38 | +This will load crate dependencies of protobuf that are generated using |
| 39 | +[cargo raze][raze] inside the rules_rust |
| 40 | +repository. However, using those dependencies might conflict with other uses |
| 41 | +of [cargo raze][raze]. If you need to change |
| 42 | +those dependencies, please see the [dedicated section below](#custom-deps). |
| 43 | + |
| 44 | +For additional information about Bazel toolchains, see [here](https://docs.bazel.build/versions/master/toolchains.html). |
| 45 | + |
| 46 | +## <a name="custom-deps">Customizing dependencies |
| 47 | + |
| 48 | +These rules depends on the [`protobuf`](https://crates.io/crates/protobuf) and |
| 49 | +the [`grpc`](https://crates.io/crates/grpc) crates in addition to the [protobuf |
| 50 | +compiler](https://github.com/google/protobuf). To obtain these crates, |
| 51 | +`rust_proto_repositories` imports the given crates using BUILD files generated with |
| 52 | +[`cargo raze`][raze]. |
| 53 | + |
| 54 | +If you want to either change the protobuf and gRPC rust compilers, or to |
| 55 | +simply use [`cargo raze`][raze] in a more |
| 56 | +complex scenario (with more dependencies), you must redefine those |
| 57 | +dependencies. |
| 58 | + |
| 59 | +To do this, once you've imported the needed dependencies (see our |
| 60 | +[Cargo.toml](raze/Cargo.toml) file to see the default dependencies), you |
| 61 | +need to create your own toolchain. To do so you can create a BUILD |
| 62 | +file with your toolchain definition, for example: |
| 63 | + |
| 64 | +```python |
| 65 | +load("@rules_rust//proto:toolchain.bzl", "rust_proto_toolchain") |
| 66 | + |
| 67 | +rust_proto_toolchain( |
| 68 | + name = "proto-toolchain-impl", |
| 69 | + # Path to the protobuf compiler. |
| 70 | + protoc = "@com_google_protobuf//:protoc", |
| 71 | + # Protobuf compiler plugin to generate rust gRPC stubs. |
| 72 | + grpc_plugin = "//cargo_raze/remote:cargo_bin_protoc_gen_rust_grpc", |
| 73 | + # Protobuf compiler plugin to generate rust protobuf stubs. |
| 74 | + proto_plugin = "//cargo_raze/remote:cargo_bin_protoc_gen_rust", |
| 75 | +) |
| 76 | + |
| 77 | +toolchain( |
| 78 | + name = "proto-toolchain", |
| 79 | + toolchain = ":proto-toolchain-impl", |
| 80 | + toolchain_type = "@rules_rust//proto:toolchain", |
| 81 | +) |
| 82 | +``` |
| 83 | + |
| 84 | +Now that you have your own toolchain, you need to register it by |
| 85 | +inserting the following statement in your `WORKSPACE` file: |
| 86 | + |
| 87 | +```python |
| 88 | +register_toolchains("//my/toolchains:proto-toolchain") |
| 89 | +``` |
| 90 | + |
| 91 | +Finally, you might want to set the `rust_deps` attribute in |
| 92 | +`rust_proto_library` and `rust_grpc_library` to change the compile-time |
| 93 | +dependencies: |
| 94 | + |
| 95 | +```python |
| 96 | +rust_proto_library( |
| 97 | + ... |
| 98 | + rust_deps = ["//cargo_raze/remote:protobuf"], |
| 99 | + ... |
| 100 | +) |
| 101 | + |
| 102 | +rust_grpc_library( |
| 103 | + ... |
| 104 | + rust_deps = [ |
| 105 | + "//cargo_raze/remote:protobuf", |
| 106 | + "//cargo_raze/remote:grpc", |
| 107 | + "//cargo_raze/remote:tls_api", |
| 108 | + "//cargo_raze/remote:tls_api_stub", |
| 109 | + ], |
| 110 | + ... |
| 111 | +) |
| 112 | +``` |
| 113 | + |
| 114 | +__Note__: Ideally, we would inject those dependencies from the toolchain, |
| 115 | +but due to [bazelbuild/bazel#6889](https://github.com/bazelbuild/bazel/issues/6889) |
| 116 | +all dependencies added via the toolchain ends-up being in the wrong |
| 117 | +configuration. |
| 118 | + |
| 119 | + |
9 | 120 | <a id="#rust_grpc_library"></a>
|
10 | 121 |
|
11 | 122 | ## rust_grpc_library
|
@@ -167,3 +278,17 @@ Declare dependencies needed for proto compilation.
|
167 | 278 | | <a id="rust_proto_repositories-register_default_toolchain"></a>register_default_toolchain | If True, the default [rust_proto_toolchain](#rust_proto_toolchain) (<code>@rules_rust//proto:default-proto-toolchain</code>) is registered. This toolchain requires a set of dependencies that were generated using [cargo raze](https://github.com/google/cargo-raze). These will also be loaded. | <code>True</code> |
|
168 | 279 |
|
169 | 280 |
|
| 281 | +<a id="#rust_proto_transitive_repositories"></a> |
| 282 | + |
| 283 | +## rust_proto_transitive_repositories |
| 284 | + |
| 285 | +<pre> |
| 286 | +rust_proto_transitive_repositories() |
| 287 | +</pre> |
| 288 | + |
| 289 | +Load transitive dependencies of the `@rules_rust//proto` rules. |
| 290 | + |
| 291 | +This macro should be called immediately after the `rust_proto_repositories` macro. |
| 292 | + |
| 293 | + |
| 294 | + |
0 commit comments