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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
protoA proto_library target for which to generate Rust gencode.Labelrequired

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
include_transitive_depsWhether to include transitive dependencies. If set to True, all transitive dependencies will directly accessible by the dependent crate.BooleanoptionalFalse
prost_optsAdditional options to add to Prost.List of stringsoptional[]
prost_pluginAdditional plugins to add to Prost.Labelrequired
prost_plugin_flagProst plugin flag format. (e.g. --plugin=protoc-gen-prost=%s)Stringoptional"--plugin=protoc-gen-prost=%s"
prost_runtimeThe Prost runtime crates to use.Labelrequired
prost_typesThe Prost types crates to use.Labelrequired
proto_compilerThe protoc compiler to use. Note that this attribute is deprecated - prefer to use --incompatible_enable_proto_toolchain_resolution.LabeloptionalNone
tonic_optsAdditional options to add to Tonic.List of stringsoptional[]
tonic_pluginAdditional plugins to add to Tonic.LabeloptionalNone
tonic_plugin_flagTonic plugin flag format. (e.g. --plugin=protoc-gen-tonic=%s))Stringoptional"--plugin=protoc-gen-tonic=%s"
tonic_runtimeThe Tonic runtime crates to use.LabeloptionalNone

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsAdditional dependencies to add to the compiled crate.List of labelsoptional[]
srcsAdditional source files to include in generated Prost source code.List of labelsoptional[]
prost_optsAdditional options to add to Prost.List of stringsoptional[]
tonic_optsAdditional options to add to Tonic.List of stringsoptional[]