Skip to content

Commit

Permalink
Consolidate rust_prost_library and fix extension-only proto generatio…
Browse files Browse the repository at this point in the history
…n. (bazelbuild#2047)

This PR consolidates rust_prost_library and rust_tonic_library into one rule; `rust_prost_library`. `rust_prost_library` will generate tonic services if you provide a `tonic_plugin` in the toolchain definition. If you do not provide that plugin and you try to build a proto with services, it will print a warning that you should add a `tonic_plugin`.

This PR also handles extension-only proto files. Prost does not generate a file if there are no messages, enums, or services and it appears that Prost doesn't even support proto2 extensions. So to work around this issue, protoc_wrapper will generate an empty `.rs` file in the case that there are only extensions defined in a file.

Closes: bazelbuild#2046
  • Loading branch information
freeformstu authored Jul 5, 2023
1 parent 6118c81 commit 7f751cd
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 255 deletions.
4 changes: 4 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ load("//test:deps.bzl", "rules_rust_test_deps")

rules_rust_test_deps()

load("//test:deps_transitive.bzl", "rules_rust_test_deps_transitive")

rules_rust_test_deps_transitive()

# --- end stardoc

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
Expand Down
1 change: 0 additions & 1 deletion docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ PAGES = dict([
"rust_proto_transitive_repositories",
"rust_proto_toolchain",
"rust_prost_library",
"rust_tonic_library",
],
),
page(
Expand Down
20 changes: 0 additions & 20 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
* [rust_stdlib_filegroup](#rust_stdlib_filegroup)
* [rust_test](#rust_test)
* [rust_test_suite](#rust_test_suite)
* [rust_tonic_library](#rust_tonic_library)
* [rust_toolchain](#rust_toolchain)
* [rust_toolchain_repository](#rust_toolchain_repository)
* [rust_toolchain_repository_proxy](#rust_toolchain_repository_proxy)
Expand Down Expand Up @@ -2017,25 +2016,6 @@ rust_test_suite(
| <a id="rust_test_suite-kwargs"></a>kwargs | Additional keyword arguments for the underyling [rust_test](#rust_test) targets. The <code>tags</code> argument is also passed to the generated <code>test_suite</code> target. | none |


<a id="rust_tonic_library"></a>

## rust_tonic_library

<pre>
rust_tonic_library(<a href="#rust_tonic_library-name">name</a>, <a href="#rust_tonic_library-kwargs">kwargs</a>)
</pre>

A rule for generating a Rust library using Prost and Tonic.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="rust_tonic_library-name"></a>name | The name of the target. | none |
| <a id="rust_tonic_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying <code>rust_tonic_library</code> rule. | none |


<a id="rust_toolchain_repository"></a>

## rust_toolchain_repository
Expand Down
23 changes: 1 addition & 22 deletions docs/rust_proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
* [rust_proto_transitive_repositories](#rust_proto_transitive_repositories)
* [rust_proto_toolchain](#rust_proto_toolchain)
* [rust_prost_library](#rust_prost_library)
* [rust_tonic_library](#rust_tonic_library)


## Overview
These build rules are used for building [protobufs][protobuf]/[gRPC][grpc] in [Rust][rust] with Bazel.

There are two rule sets. The first ruleset defines the `rust_proto_library` and `rust_grpc_library`
rules which generate Rust code using the [`rust-protobuf`] dependencies. The second ruleset defines
the `rust_prost_library` and `rust_tonic_library` rules which generate Rust code using the [`prost`]
and [`tonic`] dependencies respectively.
the `rust_prost_library` which generates Rust code using the [`prost`] and [`tonic`] dependencies.

[rust]: http://www.rust-lang.org/
[protobuf]: https://developers.google.com/protocol-buffers/
Expand Down Expand Up @@ -427,22 +425,3 @@ This macro should be called immediately after the `rust_proto_repositories` macr



<a id="rust_tonic_library"></a>

## rust_tonic_library

<pre>
rust_tonic_library(<a href="#rust_tonic_library-name">name</a>, <a href="#rust_tonic_library-kwargs">kwargs</a>)
</pre>

A rule for generating a Rust library using Prost and Tonic.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="rust_tonic_library-name"></a>name | The name of the target. | none |
| <a id="rust_tonic_library-kwargs"></a>kwargs | Additional keyword arguments for the underlying <code>rust_tonic_library</code> rule. | none |


3 changes: 1 addition & 2 deletions docs/rust_proto.vm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ These build rules are used for building [protobufs][protobuf]/[gRPC][grpc] in [R

There are two rule sets. The first ruleset defines the `rust_proto_library` and `rust_grpc_library`
rules which generate Rust code using the [`rust-protobuf`] dependencies. The second ruleset defines
the `rust_prost_library` and `rust_tonic_library` rules which generate Rust code using the [`prost`]
and [`tonic`] dependencies respectively.
the `rust_prost_library` which generates Rust code using the [`prost`] and [`tonic`] dependencies.

[rust]: http://www.rust-lang.org/
[protobuf]: https://developers.google.com/protocol-buffers/
Expand Down
2 changes: 0 additions & 2 deletions docs/symbols.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ load(
_rust_grpc_library = "rust_grpc_library",
_rust_prost_library = "rust_prost_library",
_rust_proto_library = "rust_proto_library",
_rust_tonic_library = "rust_tonic_library",
)
load(
"@rules_rust//proto:repositories.bzl",
Expand Down Expand Up @@ -127,7 +126,6 @@ rust_doc_test = _rust_doc_test
rust_proto_library = _rust_proto_library
rust_grpc_library = _rust_grpc_library
rust_prost_library = _rust_prost_library
rust_tonic_library = _rust_tonic_library

rust_bindgen = _rust_bindgen
rust_bindgen_dependencies = _rust_bindgen_dependencies
Expand Down
2 changes: 0 additions & 2 deletions proto/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
load(
"//proto/prost:defs.bzl",
_rust_prost_library = "rust_prost_library",
_rust_tonic_library = "rust_tonic_library",
)
load(
":proto.bzl",
Expand All @@ -15,4 +14,3 @@ rust_proto_library = _rust_proto_library
rust_grpc_library = _rust_grpc_library

rust_prost_library = _rust_prost_library
rust_tonic_library = _rust_tonic_library
24 changes: 0 additions & 24 deletions proto/prost/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ load(
"//proto/prost/private:prost.bzl",
_rust_prost_library = "rust_prost_library",
_rust_prost_toolchain = "rust_prost_toolchain",
_rust_tonic_library = "rust_tonic_library",
)

def rust_prost_library(name, **kwargs):
Expand All @@ -30,27 +29,4 @@ def rust_prost_library(name, **kwargs):
**kwargs
)

def rust_tonic_library(name, **kwargs):
"""A rule for generating a Rust library using Prost and Tonic.
Args:
name (str): The name of the target.
**kwargs (dict): Additional keyword arguments for the underlying
`rust_tonic_library` rule.
"""

# Clippy and Rustfmt will attempt to run on these targets.
# This is not correct and likely a bug in target detection.
tags = kwargs.pop("tags", [])
if "no-clippy" not in tags:
tags.append("no-clippy")
if "no-rustfmt" not in tags:
tags.append("no-rustfmt")

_rust_tonic_library(
name = name,
tags = tags,
**kwargs
)

rust_prost_toolchain = _rust_prost_toolchain
Loading

0 comments on commit 7f751cd

Please sign in to comment.