rules_rust_prost
These build rules are used for building protobufs/gRPC in Rust with Bazel using Prost and Tonic
Rules
Setup
load("@rules_rust//proto/prost:repositories.bzl", "rust_prost_dependencies")
rust_prost_dependencies()
load("@rules_rust//proto/prost:transitive_repositories.bzl", "rust_prost_transitive_repositories")
rust_prost_transitive_repositories()
The prost
and tonic
rules do not specify a default toolchain in order to avoid mismatched
dependency issues. To setup the prost
and tonic
toolchain, please see the section
Customizing prost
and tonic
Dependencies.
For additional information about Bazel toolchains, see here.
Customizing prost
and tonic
Dependencies
These rules depend on the prost
and tonic
dependencies. To setup the necessary toolchain
for 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.
To get access to these crates, you can use the crate_universe repository rules. For example:
load("//crate_universe:defs.bzl", "crate", "crates_repository")
crates_repository(
name = "crates_io",
annotations = {
"protoc-gen-prost": [crate.annotation(
gen_binaries = ["protoc-gen-prost"],
patch_args = [
"-p1",
],
patches = [
# Note: You will need to use this patch until a version greater than `0.2.2` of
# `protoc-gen-prost` is released.
"@rules_rust//proto/prost/private/3rdparty/patches:protoc-gen-prost.patch",
],
)],
"protoc-gen-tonic": [crate.annotation(
gen_binaries = ["protoc-gen-tonic"],
)],
},
cargo_lockfile = "Cargo.Bazel.lock",
mode = "remote",
packages = {
"prost": crate.spec(
version = "0",
),
"prost-types": crate.spec(
version = "0",
),
"protoc-gen-prost": crate.spec(
version = "0",
),
"protoc-gen-tonic": crate.spec(
version = "0",
),
"tonic": crate.spec(
version = "0",
),
},
repository_name = "rules_rust_prost",
tags = ["manual"],
)
You can then define a toolchain with the rust_prost_toolchain
rule which uses the crates
defined above. For example:
load("@rules_rust//proto/prost:defs.bzl", "rust_prost_toolchain")
load("@rules_rust//rust:defs.bzl", "rust_library_group")
rust_library_group(
name = "prost_runtime",
deps = [
"@crates_io//:prost",
],
)
rust_library_group(
name = "tonic_runtime",
deps = [
":prost_runtime",
"@crates_io//:tonic",
],
)
rust_prost_toolchain(
name = "prost_toolchain_impl",
prost_plugin = "@crates_io//:protoc-gen-prost__protoc-gen-prost",
prost_runtime = ":prost_runtime",
prost_types = "@crates_io//:prost-types",
proto_compiler = "@com_google_protobuf//:protoc",
tonic_plugin = "@crates_io//:protoc-gen-tonic__protoc-gen-tonic",
tonic_runtime = ":tonic_runtime",
)
toolchain(
name = "prost_toolchain",
toolchain = "prost_toolchain_impl",
toolchain_type = "@rules_rust//proto/prost:toolchain_type",
)
Lastly, you must register the toolchain in your WORKSPACE
file. For example:
register_toolchains("//toolchains:prost_toolchain")
rust_prost_library
load("@rules_rust_prost//:defs.bzl", "rust_prost_library") rust_prost_library(name, proto)
A rule for generating a Rust library using Prost.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
proto | A proto_library target for which to generate Rust gencode. | Label | required |
rust_prost_toolchain
load("@rules_rust_prost//:defs.bzl", "rust_prost_toolchain") rust_prost_toolchain(name, include_transitive_deps, prost_opts, prost_plugin, prost_plugin_flag, prost_runtime, prost_types, proto_compiler, tonic_opts, tonic_plugin, tonic_plugin_flag, tonic_runtime)
Rust Prost toolchain rule.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
include_transitive_deps | Whether to include transitive dependencies. If set to True, all transitive dependencies will directly accessible by the dependent crate. | Boolean | optional | False |
prost_opts | Additional options to add to Prost. | List of strings | optional | [] |
prost_plugin | Additional plugins to add to Prost. | Label | required | |
prost_plugin_flag | Prost plugin flag format. (e.g. --plugin=protoc-gen-prost=%s ) | String | optional | "--plugin=protoc-gen-prost=%s" |
prost_runtime | The Prost runtime crates to use. | Label | required | |
prost_types | The Prost types crates to use. | Label | required | |
proto_compiler | The protoc compiler to use. Note that this attribute is deprecated - prefer to use --incompatible_enable_proto_toolchain_resolution. | Label | optional | None |
tonic_opts | Additional options to add to Tonic. | List of strings | optional | [] |
tonic_plugin | Additional plugins to add to Tonic. | Label | optional | None |
tonic_plugin_flag | Tonic plugin flag format. (e.g. --plugin=protoc-gen-tonic=%s )) | String | optional | "--plugin=protoc-gen-tonic=%s" |
tonic_runtime | The Tonic runtime crates to use. | Label | optional | None |
rust_prost_transform
load("@rules_rust_prost//:defs.bzl", "rust_prost_transform") rust_prost_transform(name, deps, srcs, prost_opts, tonic_opts)
A rule for transforming the outputs of ProstGenProto
actions.
This rule is used by adding it to the data
attribute of proto_library
targets. E.g.
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_rust_prost//:defs.bzl", "rust_prost_library", "rust_prost_transform")
rust_prost_transform(
name = "a_transform",
srcs = [
"a_src.rs",
],
)
proto_library(
name = "a_proto",
srcs = [
"a.proto",
],
data = [
":transform",
],
)
rust_prost_library(
name = "a_rs_proto",
proto = ":a_proto",
)
The rust_prost_library
will spawn an action on the a_proto
target which consumes the
a_transform
rule to provide a means of granularly modifying a proto library for ProstGenProto
actions with minimal impact to other consumers.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
deps | Additional dependencies to add to the compiled crate. | List of labels | optional | [] |
srcs | Additional source files to include in generated Prost source code. | List of labels | optional | [] |
prost_opts | Additional options to add to Prost. | List of strings | optional | [] |
tonic_opts | Additional options to add to Tonic. | List of strings | optional | [] |