Skip to content

Commit 8bad4c5

Browse files
authored
Moved proto deps into toolchain to avoid confusing boiler plate code (#678)
1 parent bee1140 commit 8bad4c5

File tree

6 files changed

+85
-37
lines changed

6 files changed

+85
-37
lines changed

docs/flatten.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ Example:
505505

506506
```python
507507
load("//proto:proto.bzl", "rust_grpc_library")
508-
load("//proto:toolchain.bzl", "GRPC_COMPILE_DEPS")
509508

510509
proto_library(
511510
name = "my_proto",
@@ -520,7 +519,7 @@ rust_grpc_library(
520519
rust_binary(
521520
name = "my_service",
522521
srcs = ["my_service.rs"],
523-
deps = [":rust"] + GRPC_COMPILE_DEPS,
522+
deps = [":rust"],
524523
)
525524
```
526525

@@ -532,7 +531,7 @@ rust_binary(
532531
| :------------- | :------------- | :------------- | :------------- | :------------- |
533532
| <a id="rust_grpc_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
534533
| <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/docs/build-ref.html#labels">List of labels</a> | required | |
535-
| <a id="rust_grpc_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf"), Label("//proto/raze:grpc"), Label("//proto/raze:tls_api"), Label("//proto/raze:tls_api_stub")] |
534+
| <a id="rust_grpc_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
536535

537536

538537
<a id="#rust_library"></a>
@@ -682,7 +681,6 @@ Example:
682681

683682
```python
684683
load("@rules_rust//proto:proto.bzl", "rust_proto_library")
685-
load("@rules_rust//proto:toolchain.bzl", "PROTO_COMPILE_DEPS")
686684

687685
proto_library(
688686
name = "my_proto",
@@ -697,7 +695,7 @@ proto_rust_library(
697695
rust_binary(
698696
name = "my_proto_binary",
699697
srcs = ["my_proto_binary.rs"],
700-
deps = [":rust"] + PROTO_COMPILE_DEPS,
698+
deps = [":rust"],
701699
)
702700
```
703701

@@ -709,15 +707,16 @@ rust_binary(
709707
| :------------- | :------------- | :------------- | :------------- | :------------- |
710708
| <a id="rust_proto_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
711709
| <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/docs/build-ref.html#labels">List of labels</a> | required | |
712-
| <a id="rust_proto_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf")] |
710+
| <a id="rust_proto_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
713711

714712

715713
<a id="#rust_proto_toolchain"></a>
716714

717715
## rust_proto_toolchain
718716

719717
<pre>
720-
rust_proto_toolchain(<a href="#rust_proto_toolchain-name">name</a>, <a href="#rust_proto_toolchain-edition">edition</a>, <a href="#rust_proto_toolchain-grpc_plugin">grpc_plugin</a>, <a href="#rust_proto_toolchain-proto_plugin">proto_plugin</a>, <a href="#rust_proto_toolchain-protoc">protoc</a>)
718+
rust_proto_toolchain(<a href="#rust_proto_toolchain-name">name</a>, <a href="#rust_proto_toolchain-edition">edition</a>, <a href="#rust_proto_toolchain-grpc_compile_deps">grpc_compile_deps</a>, <a href="#rust_proto_toolchain-grpc_plugin">grpc_plugin</a>, <a href="#rust_proto_toolchain-proto_compile_deps">proto_compile_deps</a>,
719+
<a href="#rust_proto_toolchain-proto_plugin">proto_plugin</a>, <a href="#rust_proto_toolchain-protoc">protoc</a>)
721720
</pre>
722721

723722
Declares a Rust Proto toolchain for use.
@@ -761,7 +760,9 @@ See @rules_rust//proto:BUILD for examples of defining the toolchain.
761760
| :------------- | :------------- | :------------- | :------------- | :------------- |
762761
| <a id="rust_proto_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
763762
| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "2015" |
763+
| <a id="rust_proto_toolchain-grpc_compile_deps"></a>grpc_compile_deps | The crates the generated grpc libraries depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf"), Label("//proto/raze:grpc"), Label("//proto/raze:tls_api"), Label("//proto/raze:tls_api_stub")] |
764764
| <a id="rust_proto_toolchain-grpc_plugin"></a>grpc_plugin | The location of the Rust protobuf compiler plugin to generate rust gRPC stubs. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //proto:protoc_gen_rust_grpc |
765+
| <a id="rust_proto_toolchain-proto_compile_deps"></a>proto_compile_deps | The crates the generated protobuf libraries depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf")] |
765766
| <a id="rust_proto_toolchain-proto_plugin"></a>proto_plugin | The location of the Rust protobuf compiler plugin used to generate rust sources. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //proto:protoc_gen_rust |
766767
| <a id="rust_proto_toolchain-protoc"></a>protoc | The location of the <code>protoc</code> binary. It should be an executable target. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | @com_google_protobuf//:protoc |
767768

@@ -1362,11 +1363,17 @@ Declare dependencies needed for bindgen.
13621363
## rust_proto_repositories
13631364

13641365
<pre>
1365-
rust_proto_repositories()
1366+
rust_proto_repositories(<a href="#rust_proto_repositories-register_default_toolchain">register_default_toolchain</a>)
13661367
</pre>
13671368

13681369
Declare dependencies needed for proto compilation.
13691370

1371+
**PARAMETERS**
1372+
1373+
1374+
| Name | Description | Default Value |
1375+
| :------------- | :------------- | :------------- |
1376+
| <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> |
13701377

13711378

13721379
<a id="#rust_repositories"></a>

docs/rust_proto.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Example:
1818

1919
```python
2020
load("//proto:proto.bzl", "rust_grpc_library")
21-
load("//proto:toolchain.bzl", "GRPC_COMPILE_DEPS")
2221

2322
proto_library(
2423
name = "my_proto",
@@ -33,7 +32,7 @@ rust_grpc_library(
3332
rust_binary(
3433
name = "my_service",
3534
srcs = ["my_service.rs"],
36-
deps = [":rust"] + GRPC_COMPILE_DEPS,
35+
deps = [":rust"],
3736
)
3837
```
3938

@@ -45,7 +44,7 @@ rust_binary(
4544
| :------------- | :------------- | :------------- | :------------- | :------------- |
4645
| <a id="rust_grpc_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
4746
| <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/docs/build-ref.html#labels">List of labels</a> | required | |
48-
| <a id="rust_grpc_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf"), Label("//proto/raze:grpc"), Label("//proto/raze:tls_api"), Label("//proto/raze:tls_api_stub")] |
47+
| <a id="rust_grpc_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
4948

5049

5150
<a id="#rust_proto_library"></a>
@@ -62,7 +61,6 @@ Example:
6261

6362
```python
6463
load("@rules_rust//proto:proto.bzl", "rust_proto_library")
65-
load("@rules_rust//proto:toolchain.bzl", "PROTO_COMPILE_DEPS")
6664

6765
proto_library(
6866
name = "my_proto",
@@ -77,7 +75,7 @@ proto_rust_library(
7775
rust_binary(
7876
name = "my_proto_binary",
7977
srcs = ["my_proto_binary.rs"],
80-
deps = [":rust"] + PROTO_COMPILE_DEPS,
78+
deps = [":rust"],
8179
)
8280
```
8381

@@ -89,15 +87,16 @@ rust_binary(
8987
| :------------- | :------------- | :------------- | :------------- | :------------- |
9088
| <a id="rust_proto_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
9189
| <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/docs/build-ref.html#labels">List of labels</a> | required | |
92-
| <a id="rust_proto_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf")] |
90+
| <a id="rust_proto_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
9391

9492

9593
<a id="#rust_proto_toolchain"></a>
9694

9795
## rust_proto_toolchain
9896

9997
<pre>
100-
rust_proto_toolchain(<a href="#rust_proto_toolchain-name">name</a>, <a href="#rust_proto_toolchain-edition">edition</a>, <a href="#rust_proto_toolchain-grpc_plugin">grpc_plugin</a>, <a href="#rust_proto_toolchain-proto_plugin">proto_plugin</a>, <a href="#rust_proto_toolchain-protoc">protoc</a>)
98+
rust_proto_toolchain(<a href="#rust_proto_toolchain-name">name</a>, <a href="#rust_proto_toolchain-edition">edition</a>, <a href="#rust_proto_toolchain-grpc_compile_deps">grpc_compile_deps</a>, <a href="#rust_proto_toolchain-grpc_plugin">grpc_plugin</a>, <a href="#rust_proto_toolchain-proto_compile_deps">proto_compile_deps</a>,
99+
<a href="#rust_proto_toolchain-proto_plugin">proto_plugin</a>, <a href="#rust_proto_toolchain-protoc">protoc</a>)
101100
</pre>
102101

103102
Declares a Rust Proto toolchain for use.
@@ -141,7 +140,9 @@ See @rules_rust//proto:BUILD for examples of defining the toolchain.
141140
| :------------- | :------------- | :------------- | :------------- | :------------- |
142141
| <a id="rust_proto_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
143142
| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "2015" |
143+
| <a id="rust_proto_toolchain-grpc_compile_deps"></a>grpc_compile_deps | The crates the generated grpc libraries depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf"), Label("//proto/raze:grpc"), Label("//proto/raze:tls_api"), Label("//proto/raze:tls_api_stub")] |
144144
| <a id="rust_proto_toolchain-grpc_plugin"></a>grpc_plugin | The location of the Rust protobuf compiler plugin to generate rust gRPC stubs. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //proto:protoc_gen_rust_grpc |
145+
| <a id="rust_proto_toolchain-proto_compile_deps"></a>proto_compile_deps | The crates the generated protobuf libraries depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf")] |
145146
| <a id="rust_proto_toolchain-proto_plugin"></a>proto_plugin | The location of the Rust protobuf compiler plugin used to generate rust sources. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //proto:protoc_gen_rust |
146147
| <a id="rust_proto_toolchain-protoc"></a>protoc | The location of the <code>protoc</code> binary. It should be an executable target. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | @com_google_protobuf//:protoc |
147148

@@ -151,10 +152,16 @@ See @rules_rust//proto:BUILD for examples of defining the toolchain.
151152
## rust_proto_repositories
152153

153154
<pre>
154-
rust_proto_repositories()
155+
rust_proto_repositories(<a href="#rust_proto_repositories-register_default_toolchain">register_default_toolchain</a>)
155156
</pre>
156157

157158
Declare dependencies needed for proto compilation.
158159

160+
**PARAMETERS**
161+
162+
163+
| Name | Description | Default Value |
164+
| :------------- | :------------- | :------------- |
165+
| <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> |
159166

160167

proto/proto.bzl

+14-9
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ rust_proto_repositories()
3535
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
3636
load(
3737
"//proto:toolchain.bzl",
38-
"GRPC_COMPILE_DEPS",
39-
"PROTO_COMPILE_DEPS",
4038
_generate_proto = "rust_generate_proto",
4139
_generated_file_stem = "generated_file_stem",
4240
)
@@ -214,6 +212,12 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
214212
output_hash,
215213
))
216214

215+
# Gather all dependencies for compilation
216+
compile_action_deps = depset(
217+
compile_deps +
218+
proto_toolchain.grpc_compile_deps if is_grpc else proto_toolchain.proto_compile_deps,
219+
)
220+
217221
return rustc_compile_action(
218222
ctx = ctx,
219223
toolchain = find_toolchain(ctx),
@@ -223,7 +227,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
223227
type = "rlib",
224228
root = lib_rs,
225229
srcs = depset(srcs),
226-
deps = depset(compile_deps),
230+
deps = compile_action_deps,
227231
proc_macro_deps = depset([]),
228232
aliases = {},
229233
output = rust_lib,
@@ -286,7 +290,6 @@ rust_proto_library = rule(
286290
),
287291
"rust_deps": attr.label_list(
288292
doc = "The crates the generated library depends on.",
289-
default = PROTO_COMPILE_DEPS,
290293
),
291294
"_cc_toolchain": attr.label(
292295
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
@@ -310,6 +313,8 @@ rust_proto_library = rule(
310313
str(Label("//rust:toolchain")),
311314
"@bazel_tools//tools/cpp:toolchain_type",
312315
],
316+
# TODO: Remove once (bazelbuild/bazel#11584) is closed and the rules use
317+
# the version of Bazel that issue was closed on as the min supported version
313318
incompatible_use_toolchain_transition = True,
314319
doc = """\
315320
Builds a Rust library crate from a set of `proto_library`s.
@@ -318,7 +323,6 @@ Example:
318323
319324
```python
320325
load("@rules_rust//proto:proto.bzl", "rust_proto_library")
321-
load("@rules_rust//proto:toolchain.bzl", "PROTO_COMPILE_DEPS")
322326
323327
proto_library(
324328
name = "my_proto",
@@ -333,7 +337,7 @@ proto_rust_library(
333337
rust_binary(
334338
name = "my_proto_binary",
335339
srcs = ["my_proto_binary.rs"],
336-
deps = [":rust"] + PROTO_COMPILE_DEPS,
340+
deps = [":rust"],
337341
)
338342
```
339343
""",
@@ -364,7 +368,6 @@ rust_grpc_library = rule(
364368
),
365369
"rust_deps": attr.label_list(
366370
doc = "The crates the generated library depends on.",
367-
default = GRPC_COMPILE_DEPS,
368371
),
369372
"_cc_toolchain": attr.label(
370373
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
@@ -388,14 +391,16 @@ rust_grpc_library = rule(
388391
str(Label("//rust:toolchain")),
389392
"@bazel_tools//tools/cpp:toolchain_type",
390393
],
394+
# TODO: Remove once (bazelbuild/bazel#11584) is closed and the rules use
395+
# the version of Bazel that issue was closed on as the min supported version
396+
incompatible_use_toolchain_transition = True,
391397
doc = """\
392398
Builds a Rust library crate from a set of `proto_library`s suitable for gRPC.
393399
394400
Example:
395401
396402
```python
397403
load("//proto:proto.bzl", "rust_grpc_library")
398-
load("//proto:toolchain.bzl", "GRPC_COMPILE_DEPS")
399404
400405
proto_library(
401406
name = "my_proto",
@@ -410,7 +415,7 @@ rust_grpc_library(
410415
rust_binary(
411416
name = "my_service",
412417
srcs = ["my_service.rs"],
413-
deps = [":rust"] + GRPC_COMPILE_DEPS,
418+
deps = [":rust"],
414419
)
415420
```
416421
""",

proto/repositories.bzl

+10-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
1818
load("//proto/raze:crates.bzl", "rules_rust_proto_fetch_remote_crates")
1919

2020
# buildifier: disable=unnamed-macro
21-
def rust_proto_repositories():
22-
"""Declare dependencies needed for proto compilation."""
21+
def rust_proto_repositories(register_default_toolchain = True):
22+
"""Declare dependencies needed for proto compilation.
23+
24+
Args:
25+
register_default_toolchain (bool, optional): If True, the default [rust_proto_toolchain](#rust_proto_toolchain)
26+
(`@rules_rust//proto:default-proto-toolchain`) is registered. This toolchain requires a set of dependencies
27+
that were generated using [cargo raze](https://github.com/google/cargo-raze). These will also be loaded.
28+
"""
2329
maybe(
2430
http_archive,
2531
name = "com_google_protobuf",
@@ -69,4 +75,5 @@ def rust_proto_repositories():
6975
rules_rust_proto_fetch_remote_crates()
7076

7177
# Register toolchains
72-
native.register_toolchains(str(Label("//proto:default-proto-toolchain")))
78+
if register_default_toolchain:
79+
native.register_toolchains(str(Label("//proto:default-proto-toolchain")))

proto/toolchain.bzl

+30-8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,21 @@
1717
# buildifier: disable=bzl-visibility
1818
load("//rust/private:utils.bzl", "name_to_crate_name")
1919

20-
def generated_file_stem(f):
21-
basename = f.rsplit("/", 2)[-1]
20+
def generated_file_stem(file_path):
21+
"""Returns the basename of a file without any extensions.
22+
23+
Example:
24+
```python
25+
content.append("pub mod %s;" % _generated_file_stem(f))
26+
```
27+
28+
Args:
29+
file_path (string): A path to a file
30+
31+
Returns:
32+
string: The file stem of the filename
33+
"""
34+
basename = file_path.rsplit("/", 2)[-1]
2235
basename = name_to_crate_name(basename)
2336
return basename.rsplit(".", 2)[0]
2437

@@ -106,10 +119,12 @@ def rust_generate_proto(
106119

107120
def _rust_proto_toolchain_impl(ctx):
108121
return platform_common.ToolchainInfo(
109-
protoc = ctx.executable.protoc,
110-
proto_plugin = ctx.file.proto_plugin,
111-
grpc_plugin = ctx.file.grpc_plugin,
112122
edition = ctx.attr.edition,
123+
grpc_compile_deps = ctx.attr.grpc_compile_deps,
124+
grpc_plugin = ctx.file.grpc_plugin,
125+
proto_compile_deps = ctx.attr.proto_compile_deps,
126+
proto_plugin = ctx.file.proto_plugin,
127+
protoc = ctx.executable.protoc,
113128
)
114129

115130
# Default dependencies needed to compile protobuf stubs.
@@ -124,22 +139,29 @@ GRPC_COMPILE_DEPS = PROTO_COMPILE_DEPS + [
124139
Label("//proto/raze:tls_api_stub"),
125140
]
126141

127-
# TODO(damienmg): Once bazelbuild/bazel#6889 is fixed, reintroduce
128-
# proto_compile_deps and grpc_compile_deps and remove them from the
129-
# rust_proto_library and grpc_proto_library.
130142
rust_proto_toolchain = rule(
131143
implementation = _rust_proto_toolchain_impl,
132144
attrs = {
133145
"edition": attr.string(
134146
doc = "The edition used by the generated rust source.",
135147
default = "2015",
136148
),
149+
"grpc_compile_deps": attr.label_list(
150+
doc = "The crates the generated grpc libraries depends on.",
151+
cfg = "target",
152+
default = GRPC_COMPILE_DEPS,
153+
),
137154
"grpc_plugin": attr.label(
138155
doc = "The location of the Rust protobuf compiler plugin to generate rust gRPC stubs.",
139156
allow_single_file = True,
140157
cfg = "exec",
141158
default = Label("//proto:protoc_gen_rust_grpc"),
142159
),
160+
"proto_compile_deps": attr.label_list(
161+
doc = "The crates the generated protobuf libraries depends on.",
162+
cfg = "target",
163+
default = PROTO_COMPILE_DEPS,
164+
),
143165
"proto_plugin": attr.label(
144166
doc = "The location of the Rust protobuf compiler plugin used to generate rust sources.",
145167
allow_single_file = True,

0 commit comments

Comments
 (0)