rules_rust_protobuf
These build rules are used for building protobufs/gRPC in Rust with Bazel
using rust-protobuf
.
Setup
To use the Rust proto rules, add the following to your WORKSPACE
file to add the
external repositories for the Rust proto toolchain (in addition to the rust rules setup):
load("@rules_rust//proto/protobuf:repositories.bzl", "rust_proto_protobuf_dependencies", "rust_proto_protobuf_register_toolchains")
rust_proto_protobuf_dependencies()
rust_proto_protobuf_register_toolchains()
load("@rules_rust//proto/protobuf:transitive_repositories.bzl", "rust_proto_protobuf_transitive_repositories")
rust_proto_protobuf_transitive_repositories()
This will load the required dependencies for the rust-protobuf
rules. It will also
register a default toolchain for the rust_proto_library
and rust_grpc_library
rules.
To customize the rust_proto_library
and rust_grpc_library
toolchain, please see the section
Customizing rust-protobuf
Dependencies.
For additional information about Bazel toolchains, see here.
Customizing rust-protobuf
Dependencies
These rules depend on the protobuf
and
the grpc
crates in addition to the protobuf
compiler. To obtain these crates,
rust_proto_repositories
imports the given crates using BUILD files generated with
crate_universe.
If you want to either change the protobuf and gRPC rust compilers, or to simply use crate_universe in a more complex scenario (with more dependencies), you must redefine those dependencies.
To do this, once you've imported the needed dependencies (see our @rules_rust//proto/protobuf/3rdparty/BUILD.bazel file to see the default dependencies), you need to create your own toolchain. To do so you can create a BUILD file with your toolchain definition, for example:
load("@rules_rust//proto:toolchain.bzl", "rust_proto_toolchain")
rust_proto_toolchain(
name = "proto-toolchain-impl",
# Path to the protobuf compiler.
protoc = "@com_google_protobuf//:protoc",
# Protobuf compiler plugin to generate rust gRPC stubs.
grpc_plugin = "//3rdparty/crates:cargo_bin_protoc_gen_rust_grpc",
# Protobuf compiler plugin to generate rust protobuf stubs.
proto_plugin = "//3rdparty/crates:cargo_bin_protoc_gen_rust",
)
toolchain(
name = "proto-toolchain",
toolchain = ":proto-toolchain-impl",
toolchain_type = "@rules_rust//proto/protobuf:toolchain_type",
)
Now that you have your own toolchain, you need to register it by
inserting the following statement in your WORKSPACE
file:
register_toolchains("//my/toolchains:proto-toolchain")
Finally, you might want to set the rust_deps
attribute in
rust_proto_library
and rust_grpc_library
to change the compile-time
dependencies:
rust_proto_library(
...
rust_deps = ["//3rdparty/crates:protobuf"],
...
)
rust_grpc_library(
...
rust_deps = [
"//3rdparty/crates:protobuf",
"//3rdparty/crates:grpc",
"//3rdparty/crates:tls_api",
"//3rdparty/crates:tls_api_stub",
],
...
)
Note: Ideally, we would inject those dependencies from the toolchain, but due to bazelbuild/bazel#6889 all dependencies added via the toolchain ends-up being in the wrong configuration.
rust_grpc_library
rust_grpc_library(name, deps, crate_name, rust_deps, rustc_flags)
Builds a Rust library crate from a set of proto_library
s suitable for gRPC.
Example:
load("@rules_rust_protobuf//:defs.bzl", "rust_grpc_library")
proto_library(
name = "my_proto",
srcs = ["my.proto"]
)
rust_grpc_library(
name = "rust",
deps = [":my_proto"],
)
rust_binary(
name = "my_service",
srcs = ["my_service.rs"],
deps = [":rust"],
)
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding gRPC stubs. | List of labels | required | |
crate_name | Crate name to use for this target. This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | "" |
rust_deps | The crates the generated library depends on. | List of labels | optional | [] |
rustc_flags | List of compiler flags passed to rustc .These strings are subject to Make variable expansion for predefined source/output path variables like $location , $execpath , and $rootpath . This expansion is useful if you wish to pass a generated file of arguments to rustc: @$(location //package:target) . | List of strings | optional | [] |
rust_proto_library
rust_proto_library(name, deps, crate_name, rust_deps, rustc_flags)
Builds a Rust library crate from a set of proto_library
s.
Example:
load("@rules_rust_protobuf//:defs.bzl", "rust_proto_library")
proto_library(
name = "my_proto",
srcs = ["my.proto"]
)
rust_proto_library(
name = "rust",
deps = [":my_proto"],
)
rust_binary(
name = "my_proto_binary",
srcs = ["my_proto_binary.rs"],
deps = [":rust"],
)
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding stubs. | List of labels | required | |
crate_name | Crate name to use for this target. This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | "" |
rust_deps | The crates the generated library depends on. | List of labels | optional | [] |
rustc_flags | List of compiler flags passed to rustc .These strings are subject to Make variable expansion for predefined source/output path variables like $location , $execpath , and $rootpath . This expansion is useful if you wish to pass a generated file of arguments to rustc: @$(location //package:target) . | List of strings | optional | [] |