Skip to content

Commit

Permalink
Setup Prost and Tonic rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
freeformstu committed Jun 25, 2023
1 parent 9442aed commit faf828f
Show file tree
Hide file tree
Showing 157 changed files with 17,060 additions and 14 deletions.
6 changes: 4 additions & 2 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencie

crate_universe_dependencies(bootstrap = True)

load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories")
load("@rules_rust//proto:repositories.bzl", "rust_proto_dependencies", "rust_proto_register_toolchains")

rust_proto_repositories()
rust_proto_dependencies()

rust_proto_register_toolchains()

load("@rules_rust//proto:transitive_repositories.bzl", "rust_proto_transitive_repositories")

Expand Down
2 changes: 1 addition & 1 deletion crate_universe/private/crates_vendor.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ bazel run //3rdparty:crates_vendor -- --repin
```
Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html)
call against the generated workspace. The following table describes how to controll particular values passed to the
call against the generated workspace. The following table describes how to control particular values passed to the
`cargo update` command.
| Value | Cargo command |
Expand Down
18 changes: 18 additions & 0 deletions proto/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Rust proto rules."""

load(
"//proto/prost:defs.bzl",
_rust_prost_library = "rust_prost_library",
_rust_tonic_library = "rust_tonic_library",
)
load(
":proto.bzl",
_rust_grpc_library = "rust_grpc_library",
_rust_proto_library = "rust_proto_library",
)

rust_proto_library = _rust_proto_library
rust_grpc_library = _rust_grpc_library

rust_prost_library = _rust_prost_library
rust_tonic_library = _rust_tonic_library
50 changes: 50 additions & 0 deletions proto/prost/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//rust:defs.bzl", "rust_library_group")
load(":defs.bzl", "rust_prost_toolchain")

package(default_visibility = ["//visibility:public"])

toolchain_type(
name = "toolchain_type",
)

rust_library_group(
name = "prost_runtime",
deps = [
"//proto/prost/private/3rdparty/crates:prost",
],
)

rust_library_group(
name = "tonic_runtime",
deps = [
":prost_runtime",
"//proto/prost/private/3rdparty/crates:tonic",
],
)

rust_prost_toolchain(
name = "default_prost_toolchain_impl",
prost_plugin = "//proto/prost/private/3rdparty/crates:protoc-gen-prost__protoc-gen-prost",
prost_plugin_flag = "--plugin=protoc-gen-prost=%s",
prost_runtime = ":prost_runtime",
proto_compiler = "@com_google_protobuf//:protoc",
tonic_plugin = "//proto/prost/private/3rdparty/crates:protoc-gen-tonic__protoc-gen-tonic",
tonic_plugin_flag = "--plugin=protoc-gen-tonic=%s",
tonic_runtime = ":tonic_runtime",
)

toolchain(
name = "default_prost_toolchain",
toolchain = "default_prost_toolchain_impl",
toolchain_type = "//proto/prost:toolchain_type",
)

bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
deps = [
"//proto/prost/private:bzl_lib",
"//rust:bzl_lib",
],
)
56 changes: 56 additions & 0 deletions proto/prost/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Rules for building proto libraries in Rust."""

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):
"""A rule for generating a Rust library using Prost.
Args:
name (str): The name of the target.
**kwargs (dict): Additional keyword arguments for the underlying
`rust_prost_library` rule.
"""

# Clippy and Rustfmt will attempt to run on these targets.
# This is not correct and likeyl 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_prost_library(
name = name,
tags = tags,
**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 likeyl 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
54 changes: 54 additions & 0 deletions proto/prost/private/3rdparty/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//crate_universe:defs.bzl", "crate", "crates_vendor")

crates_vendor(
name = "crates_vendor",
annotations = {
"protoc-gen-prost": [crate.annotation(
gen_binaries = ["protoc-gen-prost"],
)],
"protoc-gen-tonic": [crate.annotation(
gen_binaries = ["protoc-gen-tonic"],
)],
},
cargo_lockfile = "Cargo.Bazel.lock",
mode = "remote",
packages = {
"h2": crate.spec(
version = "0.3.19",
),
"prost": crate.spec(
version = "0.11.9",
),
"prost-types": crate.spec(
version = "0.11.9",
),
"protoc-gen-prost": crate.spec(
version = "0.2.2",
),
"protoc-gen-tonic": crate.spec(
version = "0.2.2",
),
"tokio": crate.spec(
features = ["full"],
version = "1.28.2",
),
"tokio-stream": crate.spec(
version = "0.1.14",
),
"tonic": crate.spec(
version = "0.9.2",
),
},
repository_name = "rules_rust_prost",
tags = ["manual"],
)

bzl_library(
name = "bzl_lib",
srcs = [
"//proto/prost/private/3rdparty/crates:crates.bzl",
"//proto/prost/private/3rdparty/crates:defs.bzl",
],
visibility = ["//proto/prost/private:__pkg__"],
)
Loading

0 comments on commit faf828f

Please sign in to comment.