1<!-- Generated with Stardoc: http://skydoc.bazel.build --> 2# Rust Proto 3 4* [rust_prost_library](#rust_prost_library) 5* [rust_prost_toolchain](#rust_prost_toolchain) 6* [rust_prost_dependencies](#rust_prost_dependencies) 7* [rust_prost_transitive_repositories](#rust_prost_transitive_repositories) 8* [rust_proto_library](#rust_proto_library) 9* [rust_grpc_library](#rust_grpc_library) 10* [rust_proto_protobuf_toolchain](#rust_proto_protobuf_toolchain) 11* [rust_proto_protobuf_dependencies](#rust_proto_protobuf_dependencies) 12* [rust_proto_protobuf_register_toolchains](#rust_proto_protobuf_register_toolchains) 13* [rust_proto_protobuf_transitive_repositories](#rust_proto_protobuf_transitive_repositories) 14 15 16## Overview 17These build rules are used for building [protobufs][protobuf]/[gRPC][grpc] in [Rust][rust] with Bazel. 18 19There are two rule sets. The first ruleset defines the `rust_prost_library` which generates Rust code 20using the [`prost`] and [`tonic`] dependencies. The second ruleset defines the `rust_proto_library` and 21`rust_grpc_library` rules which generate Rust code using the [`rust-protobuf`] dependencies. 22 23[rust]: http://www.rust-lang.org/ 24[protobuf]: https://developers.google.com/protocol-buffers/ 25[grpc]: https://grpc.io 26[`rust-protobuf`]: https://github.com/stepancheg/rust-protobuf/ 27 28See the [protobuf example](../examples/proto) for a more complete example of use. 29 30### Prost Setup 31 32```python 33load("@rules_rust//proto/prost:repositories.bzl", "rust_prost_dependencies") 34 35rust_prost_dependencies() 36 37load("@rules_rust//proto/prost:transitive_repositories.bzl", "rust_prost_transitive_repositories") 38 39rust_prost_transitive_repositories() 40``` 41 42The `prost` and `tonic` rules do not specify a default toolchain in order to avoid mismatched 43dependency issues. To setup the `prost` and `tonic` toolchain, please see the section 44[Customizing `prost` and `tonic` Dependencies](#custom-prost-deps). 45 46For additional information about Bazel toolchains, see [here](https://docs.bazel.build/versions/master/toolchains.html). 47 48#### <a name="custom-prost-deps">Customizing `prost` and `tonic` Dependencies 49 50These rules depend on the [`prost`] and [`tonic`] dependencies. To setup the necessary toolchain 51for 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. 52 53[`prost`]: https://crates.io/crates/prost 54[`prost-types`]: https://crates.io/crates/prost-types 55[`protoc-gen-prost`]: https://crates.io/crates/protoc-gen-prost 56[`protoc-gen-tonic`]: https://crates.io/crates/protoc-gen-tonic 57[`tonic`]: https://crates.io/crates/tonic 58[`protoc`]: https://github.com/protocolbuffers/protobuf 59 60To get access to these crates, you can use the [crate_universe](./crate_universe.md) repository 61rules. For example: 62 63```python 64load("//crate_universe:defs.bzl", "crate", "crates_repository") 65 66crates_repository( 67 name = "crates_io", 68 annotations = { 69 "protoc-gen-prost": [crate.annotation( 70 gen_binaries = ["protoc-gen-prost"], 71 patch_args = [ 72 "-p1", 73 ], 74 patches = [ 75 # Note: You will need to use this patch until a version greater than `0.2.2` of 76 # `protoc-gen-prost` is released. 77 "@rules_rust//proto/prost/private/3rdparty/patches:protoc-gen-prost.patch", 78 ], 79 )], 80 "protoc-gen-tonic": [crate.annotation( 81 gen_binaries = ["protoc-gen-tonic"], 82 )], 83 }, 84 cargo_lockfile = "Cargo.Bazel.lock", 85 mode = "remote", 86 packages = { 87 "prost": crate.spec( 88 version = "0", 89 ), 90 "prost-types": crate.spec( 91 version = "0", 92 ), 93 "protoc-gen-prost": crate.spec( 94 version = "0", 95 ), 96 "protoc-gen-tonic": crate.spec( 97 version = "0", 98 ), 99 "tonic": crate.spec( 100 version = "0", 101 ), 102 }, 103 repository_name = "rules_rust_prost", 104 tags = ["manual"], 105) 106``` 107 108You can then define a toolchain with the `rust_prost_toolchain` rule which uses the crates 109defined above. For example: 110 111```python 112load("@rules_rust//proto/prost:defs.bzl", "rust_prost_toolchain") 113load("@rules_rust//rust:defs.bzl", "rust_library_group") 114 115rust_library_group( 116 name = "prost_runtime", 117 deps = [ 118 "@crates_io//:prost", 119 ], 120) 121 122rust_library_group( 123 name = "tonic_runtime", 124 deps = [ 125 ":prost_runtime", 126 "@crates_io//:tonic", 127 ], 128) 129 130rust_prost_toolchain( 131 name = "prost_toolchain_impl", 132 prost_plugin = "@crates_io//:protoc-gen-prost__protoc-gen-prost", 133 prost_runtime = ":prost_runtime", 134 prost_types = "@crates_io//:prost-types", 135 proto_compiler = "@com_google_protobuf//:protoc", 136 tonic_plugin = "@crates_io//:protoc-gen-tonic__protoc-gen-tonic", 137 tonic_runtime = ":tonic_runtime", 138) 139 140toolchain( 141 name = "prost_toolchain", 142 toolchain = "prost_toolchain_impl", 143 toolchain_type = "@rules_rust//proto/prost:toolchain_type", 144) 145``` 146 147Lastly, you must register the toolchain in your `WORKSPACE` file. For example: 148 149```python 150register_toolchains("//toolchains:prost_toolchain") 151``` 152 153## Rust-Protobuf Setup 154 155To use the Rust proto rules, add the following to your `WORKSPACE` file to add the 156external repositories for the Rust proto toolchain (in addition to the [rust rules setup](..)): 157 158```python 159load("@rules_rust//proto/protobuf:repositories.bzl", "rust_proto_protobuf_dependencies", "rust_proto_protobuf_register_toolchains") 160 161rust_proto_protobuf_dependencies() 162 163rust_proto_protobuf_register_toolchains() 164 165load("@rules_rust//proto/protobuf:transitive_repositories.bzl", "rust_proto_protobuf_transitive_repositories") 166 167rust_proto_protobuf_transitive_repositories() 168``` 169 170This will load the required dependencies for the [`rust-protobuf`] rules. It will also 171register a default toolchain for the `rust_proto_library` and `rust_grpc_library` rules. 172 173To customize the `rust_proto_library` and `rust_grpc_library` toolchain, please see the section 174[Customizing `rust-protobuf` Dependencies](#custom-proto-deps). 175 176For additional information about Bazel toolchains, see [here](https://docs.bazel.build/versions/master/toolchains.html). 177 178#### <a name="custom-proto-deps">Customizing `rust-protobuf` Dependencies 179 180These rules depend on the [`protobuf`](https://crates.io/crates/protobuf) and 181the [`grpc`](https://crates.io/crates/grpc) crates in addition to the [protobuf 182compiler](https://github.com/google/protobuf). To obtain these crates, 183`rust_proto_repositories` imports the given crates using BUILD files generated with 184[crate_universe](./crate_universe.md). 185 186If you want to either change the protobuf and gRPC rust compilers, or to 187simply use [crate_universe](./crate_universe.md) in a more 188complex scenario (with more dependencies), you must redefine those 189dependencies. 190 191To do this, once you've imported the needed dependencies (see our 192[@rules_rust//proto/3rdparty/BUILD.bazel](https://github.com/bazelbuild/rules_rust/blob/main/proto/3rdparty/BUILD.bazel) 193file to see the default dependencies), you need to create your own toolchain. 194To do so you can create a BUILD file with your toolchain definition, for example: 195 196```python 197load("@rules_rust//proto:toolchain.bzl", "rust_proto_toolchain") 198 199rust_proto_toolchain( 200 name = "proto-toolchain-impl", 201 # Path to the protobuf compiler. 202 protoc = "@com_google_protobuf//:protoc", 203 # Protobuf compiler plugin to generate rust gRPC stubs. 204 grpc_plugin = "//3rdparty/crates:cargo_bin_protoc_gen_rust_grpc", 205 # Protobuf compiler plugin to generate rust protobuf stubs. 206 proto_plugin = "//3rdparty/crates:cargo_bin_protoc_gen_rust", 207) 208 209toolchain( 210 name = "proto-toolchain", 211 toolchain = ":proto-toolchain-impl", 212 toolchain_type = "@rules_rust//proto/protobuf:toolchain_type", 213) 214``` 215 216Now that you have your own toolchain, you need to register it by 217inserting the following statement in your `WORKSPACE` file: 218 219```python 220register_toolchains("//my/toolchains:proto-toolchain") 221``` 222 223Finally, you might want to set the `rust_deps` attribute in 224`rust_proto_library` and `rust_grpc_library` to change the compile-time 225dependencies: 226 227```python 228rust_proto_library( 229 ... 230 rust_deps = ["//3rdparty/crates:protobuf"], 231 ... 232) 233 234rust_grpc_library( 235 ... 236 rust_deps = [ 237 "//3rdparty/crates:protobuf", 238 "//3rdparty/crates:grpc", 239 "//3rdparty/crates:tls_api", 240 "//3rdparty/crates:tls_api_stub", 241 ], 242 ... 243) 244``` 245 246__Note__: Ideally, we would inject those dependencies from the toolchain, 247but due to [bazelbuild/bazel#6889](https://github.com/bazelbuild/bazel/issues/6889) 248all dependencies added via the toolchain ends-up being in the wrong 249configuration. 250 251--- 252--- 253 254 255 256<a id="rust_grpc_library"></a> 257 258## rust_grpc_library 259 260<pre> 261rust_grpc_library(<a href="#rust_grpc_library-name">name</a>, <a href="#rust_grpc_library-crate_name">crate_name</a>, <a href="#rust_grpc_library-deps">deps</a>, <a href="#rust_grpc_library-rust_deps">rust_deps</a>, <a href="#rust_grpc_library-rustc_flags">rustc_flags</a>) 262</pre> 263 264Builds a Rust library crate from a set of `proto_library`s suitable for gRPC. 265 266Example: 267 268```python 269load("@rules_rust//proto/protobuf:defs.bzl", "rust_grpc_library") 270 271proto_library( 272 name = "my_proto", 273 srcs = ["my.proto"] 274) 275 276rust_grpc_library( 277 name = "rust", 278 deps = [":my_proto"], 279) 280 281rust_binary( 282 name = "my_service", 283 srcs = ["my_service.rs"], 284 deps = [":rust"], 285) 286``` 287 288 289**ATTRIBUTES** 290 291 292| Name | Description | Type | Mandatory | Default | 293| :------------- | :------------- | :------------- | :------------- | :------------- | 294| <a id="rust_grpc_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | | 295| <a id="rust_grpc_library-crate_name"></a>crate_name | Crate name to use for this target.<br><br> This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | <code>""</code> | 296| <a id="rust_grpc_library-deps"></a>deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding gRPC stubs. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | | 297| <a id="rust_grpc_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> | 298| <a id="rust_grpc_library-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>.<br><br> These strings are subject to Make variable expansion for predefined source/output path variables like <code>$location</code>, <code>$execpath</code>, and <code>$rootpath</code>. This expansion is useful if you wish to pass a generated file of arguments to rustc: <code>@$(location //package:target)</code>. | List of strings | optional | <code>[]</code> | 299 300 301<a id="rust_prost_toolchain"></a> 302 303## rust_prost_toolchain 304 305<pre> 306rust_prost_toolchain(<a href="#rust_prost_toolchain-name">name</a>, <a href="#rust_prost_toolchain-prost_opts">prost_opts</a>, <a href="#rust_prost_toolchain-prost_plugin">prost_plugin</a>, <a href="#rust_prost_toolchain-prost_plugin_flag">prost_plugin_flag</a>, <a href="#rust_prost_toolchain-prost_runtime">prost_runtime</a>, <a href="#rust_prost_toolchain-prost_types">prost_types</a>, 307 <a href="#rust_prost_toolchain-proto_compiler">proto_compiler</a>, <a href="#rust_prost_toolchain-tonic_opts">tonic_opts</a>, <a href="#rust_prost_toolchain-tonic_plugin">tonic_plugin</a>, <a href="#rust_prost_toolchain-tonic_plugin_flag">tonic_plugin_flag</a>, <a href="#rust_prost_toolchain-tonic_runtime">tonic_runtime</a>) 308</pre> 309 310Rust Prost toolchain rule. 311 312**ATTRIBUTES** 313 314 315| Name | Description | Type | Mandatory | Default | 316| :------------- | :------------- | :------------- | :------------- | :------------- | 317| <a id="rust_prost_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | | 318| <a id="rust_prost_toolchain-prost_opts"></a>prost_opts | Additional options to add to Prost. | List of strings | optional | <code>[]</code> | 319| <a id="rust_prost_toolchain-prost_plugin"></a>prost_plugin | Additional plugins to add to Prost. | <a href="https://bazel.build/concepts/labels">Label</a> | required | | 320| <a id="rust_prost_toolchain-prost_plugin_flag"></a>prost_plugin_flag | Prost plugin flag format. (e.g. <code>--plugin=protoc-gen-prost=%s</code>) | String | optional | <code>"--plugin=protoc-gen-prost=%s"</code> | 321| <a id="rust_prost_toolchain-prost_runtime"></a>prost_runtime | The Prost runtime crates to use. | <a href="https://bazel.build/concepts/labels">Label</a> | required | | 322| <a id="rust_prost_toolchain-prost_types"></a>prost_types | The Prost types crates to use. | <a href="https://bazel.build/concepts/labels">Label</a> | required | | 323| <a id="rust_prost_toolchain-proto_compiler"></a>proto_compiler | The protoc compiler to use. | <a href="https://bazel.build/concepts/labels">Label</a> | required | | 324| <a id="rust_prost_toolchain-tonic_opts"></a>tonic_opts | Additional options to add to Tonic. | List of strings | optional | <code>[]</code> | 325| <a id="rust_prost_toolchain-tonic_plugin"></a>tonic_plugin | Additional plugins to add to Tonic. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> | 326| <a id="rust_prost_toolchain-tonic_plugin_flag"></a>tonic_plugin_flag | Tonic plugin flag format. (e.g. <code>--plugin=protoc-gen-tonic=%s</code>)) | String | optional | <code>"--plugin=protoc-gen-tonic=%s"</code> | 327| <a id="rust_prost_toolchain-tonic_runtime"></a>tonic_runtime | The Tonic runtime crates to use. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> | 328 329 330<a id="rust_proto_library"></a> 331 332## rust_proto_library 333 334<pre> 335rust_proto_library(<a href="#rust_proto_library-name">name</a>, <a href="#rust_proto_library-crate_name">crate_name</a>, <a href="#rust_proto_library-deps">deps</a>, <a href="#rust_proto_library-rust_deps">rust_deps</a>, <a href="#rust_proto_library-rustc_flags">rustc_flags</a>) 336</pre> 337 338Builds a Rust library crate from a set of `proto_library`s. 339 340Example: 341 342```python 343load("@rules_rust//proto/protobuf:defs.bzl", "rust_proto_library") 344 345proto_library( 346 name = "my_proto", 347 srcs = ["my.proto"] 348) 349 350rust_proto_library( 351 name = "rust", 352 deps = [":my_proto"], 353) 354 355rust_binary( 356 name = "my_proto_binary", 357 srcs = ["my_proto_binary.rs"], 358 deps = [":rust"], 359) 360``` 361 362 363**ATTRIBUTES** 364 365 366| Name | Description | Type | Mandatory | Default | 367| :------------- | :------------- | :------------- | :------------- | :------------- | 368| <a id="rust_proto_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | | 369| <a id="rust_proto_library-crate_name"></a>crate_name | Crate name to use for this target.<br><br> This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | <code>""</code> | 370| <a id="rust_proto_library-deps"></a>deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding stubs. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | | 371| <a id="rust_proto_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> | 372| <a id="rust_proto_library-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>.<br><br> These strings are subject to Make variable expansion for predefined source/output path variables like <code>$location</code>, <code>$execpath</code>, and <code>$rootpath</code>. This expansion is useful if you wish to pass a generated file of arguments to rustc: <code>@$(location //package:target)</code>. | List of strings | optional | <code>[]</code> | 373 374 375<a id="rust_prost_dependencies"></a> 376 377## rust_prost_dependencies 378 379<pre> 380rust_prost_dependencies(<a href="#rust_prost_dependencies-bzlmod">bzlmod</a>) 381</pre> 382 383Declares repositories needed for prost. 384 385**PARAMETERS** 386 387 388| Name | Description | Default Value | 389| :------------- | :------------- | :------------- | 390| <a id="rust_prost_dependencies-bzlmod"></a>bzlmod | Whether bzlmod is enabled. | `False` | 391 392**RETURNS** 393 394list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories 395 defined by this macro. 396 397 398<a id="rust_prost_library"></a> 399 400## rust_prost_library 401 402<pre> 403rust_prost_library(<a href="#rust_prost_library-name">name</a>, <a href="#rust_prost_library-kwargs">kwargs</a>) 404</pre> 405 406A rule for generating a Rust library using Prost. 407 408**PARAMETERS** 409 410 411| Name | Description | Default Value | 412| :------------- | :------------- | :------------- | 413| <a id="rust_prost_library-name"></a>name | The name of the target. | none | 414| <a id="rust_prost_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying <code>rust_prost_library</code> rule. | none | 415 416 417<a id="rust_prost_transitive_repositories"></a> 418 419## rust_prost_transitive_repositories 420 421<pre> 422rust_prost_transitive_repositories() 423</pre> 424 425Load transitive dependencies of the `@rules_rust//proto/protobuf` rules. 426 427This macro should be called immediately after the `rust_protobuf_dependencies` macro. 428 429 430 431<a id="rust_proto_protobuf_dependencies"></a> 432 433## rust_proto_protobuf_dependencies 434 435<pre> 436rust_proto_protobuf_dependencies() 437</pre> 438 439 440 441 442 443<a id="rust_proto_protobuf_register_toolchains"></a> 444 445## rust_proto_protobuf_register_toolchains 446 447<pre> 448rust_proto_protobuf_register_toolchains(<a href="#rust_proto_protobuf_register_toolchains-register_toolchains">register_toolchains</a>) 449</pre> 450 451Register toolchains for proto compilation. 452 453**PARAMETERS** 454 455 456| Name | Description | Default Value | 457| :------------- | :------------- | :------------- | 458| <a id="rust_proto_protobuf_register_toolchains-register_toolchains"></a>register_toolchains | <p align="center"> - </p> | `True` | 459 460 461<a id="rust_proto_protobuf_transitive_repositories"></a> 462 463## rust_proto_protobuf_transitive_repositories 464 465<pre> 466rust_proto_protobuf_transitive_repositories() 467</pre> 468 469Load transitive dependencies of the `@rules_rust//proto/protobuf` rules. 470 471This macro should be called immediately after the `rust_protobuf_dependencies` macro. 472 473 474 475