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

cleanup
5.0 KiB173 linesraw
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(
54    name = "crates_io",
55    annotations = {
56        "protoc-gen-prost": [crate.annotation(
57            gen_binaries = ["protoc-gen-prost"],
58            patch_args = [
59                "-p1",
60            ],
61            patches = [
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(
68            gen_binaries = ["protoc-gen-tonic"],
69        )],
70    },
71    cargo_lockfile = "Cargo.Bazel.lock",
72    mode = "remote",
73    packages = {
74        "prost": crate.spec(
75            version = "0",
76        ),
77        "prost-types": crate.spec(
78            version = "0",
79        ),
80        "protoc-gen-prost": crate.spec(
81            version = "0",
82        ),
83        "protoc-gen-tonic": crate.spec(
84            version = "0",
85        ),
86        "tonic": crate.spec(
87            version = "0",
88        ),
89    },
90    repository_name = "rules_rust_prost",
91    tags = ["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(
103    name = "prost_runtime",
104    deps = [
105        "@crates_io//:prost",
106    ],
107)
108
109rust_library_group(
110    name = "tonic_runtime",
111    deps = [
112        ":prost_runtime",
113        "@crates_io//:tonic",
114    ],
115)
116
117rust_prost_toolchain(
118    name = "prost_toolchain_impl",
119    prost_plugin = "@crates_io//:protoc-gen-prost__protoc-gen-prost",
120    prost_runtime = ":prost_runtime",
121    prost_types = "@crates_io//:prost-types",
122    proto_compiler = "@com_google_protobuf//:protoc",
123    tonic_plugin = "@crates_io//:protoc-gen-tonic__protoc-gen-tonic",
124    tonic_runtime = ":tonic_runtime",
125)
126
127toolchain(
128    name = "prost_toolchain",
129    toolchain = "prost_toolchain_impl",
130    toolchain_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
153    Args:
154        name (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.
161    tags = kwargs.pop("tags", [])
162    if "no-clippy" not in tags:
163        tags.append("no-clippy")
164    if "no-rustfmt" not in tags:
165        tags.append("no-rustfmt")
166
167    _rust_prost_library(
168        name = name,
169        tags = tags,
170        **kwargs
171    )
172
173rust_prost_toolchain = _rust_prost_toolchain