fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
2bad96d9
main
1""" # rules_rust_prost 2 3These build rules are used for building [protobufs][protobuf]/[gRPC][grpc] in [Rust][rust] with Bazel 4using [Prost][prost] and [Tonic][tonic] 5 6[rust]: http://www.rust-lang.org/ 7[protobuf]: https://developers.google.com/protocol-buffers/ 8[grpc]: https://grpc.io 9[prost]: https://github.com/tokio-rs/prost 10[tonic]: https://github.com/tokio-rs/tonic 11 12## Rules 13 14- [rust_prost_library](#rust_prost_library) 15- [rust_prost_toolchain](#rust_prost_toolchain) 16 17## Setup 18 19```python 20load("@rules_rust//proto/prost:repositories.bzl", "rust_prost_dependencies") 21 22rust_prost_dependencies() 23 24load("@rules_rust//proto/prost:transitive_repositories.bzl", "rust_prost_transitive_repositories") 25 26rust_prost_transitive_repositories() 27``` 28 29The `prost` and `tonic` rules do not specify a default toolchain in order to avoid mismatched 30dependency issues. To setup the `prost` and `tonic` toolchain, please see the section 31[Customizing `prost` and `tonic` Dependencies](#custom-prost-deps). 32 33For additional information about Bazel toolchains, see [here](https://docs.bazel.build/versions/master/toolchains.html). 34 35#### <a name="custom-prost-deps">Customizing `prost` and `tonic` Dependencies 36 37These rules depend on the [`prost`] and [`tonic`] dependencies. To setup the necessary toolchain 38for 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. 39 40[`prost`]: https://crates.io/crates/prost 41[`prost-types`]: https://crates.io/crates/prost-types 42[`protoc-gen-prost`]: https://crates.io/crates/protoc-gen-prost 43[`protoc-gen-tonic`]: https://crates.io/crates/protoc-gen-tonic 44[`tonic`]: https://crates.io/crates/tonic 45[`protoc`]: https://github.com/protocolbuffers/protobuf 46 47To get access to these crates, you can use the [crate_universe](./crate_universe.md) repository 48rules. For example: 49 50```python 51load("//crate_universe:defs.bzl", "crate", "crates_repository") 52 53crates_repository( 54name = "crates_io", 55annotations = { 56"protoc-gen-prost": [crate.annotation( 57gen_binaries = ["protoc-gen-prost"], 58patch_args = [ 59"-p1", 60], 61patches = [ 62# Note: You will need to use this patch until a version greater than `0.2.2` of 63# `protoc-gen-prost` is released. 64"@rules_rust//proto/prost/private/3rdparty/patches:protoc-gen-prost.patch", 65], 66)], 67"protoc-gen-tonic": [crate.annotation( 68gen_binaries = ["protoc-gen-tonic"], 69)], 70}, 71cargo_lockfile = "Cargo.Bazel.lock", 72mode = "remote", 73packages = { 74"prost": crate.spec( 75version = "0", 76), 77"prost-types": crate.spec( 78version = "0", 79), 80"protoc-gen-prost": crate.spec( 81version = "0", 82), 83"protoc-gen-tonic": crate.spec( 84version = "0", 85), 86"tonic": crate.spec( 87version = "0", 88), 89}, 90repository_name = "rules_rust_prost", 91tags = ["manual"], 92) 93``` 94 95You can then define a toolchain with the `rust_prost_toolchain` rule which uses the crates 96defined above. For example: 97 98```python 99load("@rules_rust//proto/prost:defs.bzl", "rust_prost_toolchain") 100load("@rules_rust//rust:defs.bzl", "rust_library_group") 101 102rust_library_group( 103name = "prost_runtime", 104deps = [ 105"@crates_io//:prost", 106], 107) 108 109rust_library_group( 110name = "tonic_runtime", 111deps = [ 112":prost_runtime", 113"@crates_io//:tonic", 114], 115) 116 117rust_prost_toolchain( 118name = "prost_toolchain_impl", 119prost_plugin = "@crates_io//:protoc-gen-prost__protoc-gen-prost", 120prost_runtime = ":prost_runtime", 121prost_types = "@crates_io//:prost-types", 122proto_compiler = "@com_google_protobuf//:protoc", 123tonic_plugin = "@crates_io//:protoc-gen-tonic__protoc-gen-tonic", 124tonic_runtime = ":tonic_runtime", 125) 126 127toolchain( 128name = "prost_toolchain", 129toolchain = "prost_toolchain_impl", 130toolchain_type = "@rules_rust//proto/prost:toolchain_type", 131) 132``` 133 134Lastly, you must register the toolchain in your `WORKSPACE` file. For example: 135 136```python 137register_toolchains("//toolchains:prost_toolchain") 138``` 139 140--- 141--- 142""" 143 144load ( 145"//private:prost.bzl" , 146_rust_prost_library = "rust_prost_library" , 147_rust_prost_toolchain = "rust_prost_toolchain" , 148) 149 150def rust_prost_library ( name , ** kwargs ): 151"""A rule for generating a Rust library using Prost. 152 153Args: 154name (str): The name of the target. 155**kwargs (dict): Additional keyword arguments for the underlying 156`rust_prost_library` rule. 157""" 158 159# Clippy and Rustfmt will attempt to run on these targets. 160# This is not correct and likely a bug in target detection. 161tags = kwargs . pop ( "tags" , []) 162if "no-clippy" not in tags : 163tags . append ( "no-clippy" ) 164if "no-rustfmt" not in tags : 165tags . append ( "no-rustfmt" ) 166 167_rust_prost_library ( 168name = name , 169tags = tags , 170** kwargs 171) 172 173rust_prost_toolchain = _rust_prost_toolchain