Skip to content

Commit aa14aa4

Browse files
committed
Setup Prost and Tonic rules.
1 parent 9442aed commit aa14aa4

File tree

157 files changed

+17082
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+17082
-14
lines changed

WORKSPACE.bazel

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencie
1010

1111
crate_universe_dependencies(bootstrap = True)
1212

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

15-
rust_proto_repositories()
15+
rust_proto_dependencies()
16+
17+
rust_proto_register_toolchains()
1618

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

crate_universe/private/crates_vendor.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ bazel run //3rdparty:crates_vendor -- --repin
346346
```
347347
348348
Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html)
349-
call against the generated workspace. The following table describes how to controll particular values passed to the
349+
call against the generated workspace. The following table describes how to control particular values passed to the
350350
`cargo update` command.
351351
352352
| Value | Cargo command |

proto/defs.bzl

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Rust proto rules."""
2+
3+
load(
4+
"//proto/prost:defs.bzl",
5+
_rust_prost_library = "rust_prost_library",
6+
_rust_tonic_library = "rust_tonic_library",
7+
)
8+
load(
9+
":proto.bzl",
10+
_rust_grpc_library = "rust_grpc_library",
11+
_rust_proto_library = "rust_proto_library",
12+
)
13+
14+
rust_proto_library = _rust_proto_library
15+
rust_grpc_library = _rust_grpc_library
16+
17+
rust_prost_library = _rust_prost_library
18+
rust_tonic_library = _rust_tonic_library

proto/prost/BUILD.bazel

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load("//rust:defs.bzl", "rust_library_group")
3+
load(":defs.bzl", "rust_prost_toolchain")
4+
5+
package(default_visibility = ["//visibility:public"])
6+
7+
toolchain_type(
8+
name = "toolchain_type",
9+
)
10+
11+
rust_library_group(
12+
name = "prost_runtime",
13+
deps = [
14+
"//proto/prost/private/3rdparty/crates:prost",
15+
],
16+
)
17+
18+
rust_library_group(
19+
name = "tonic_runtime",
20+
deps = [
21+
":prost_runtime",
22+
"//proto/prost/private/3rdparty/crates:tonic",
23+
],
24+
)
25+
26+
rust_prost_toolchain(
27+
name = "default_prost_toolchain_impl",
28+
prost_plugin = "//proto/prost/private/3rdparty/crates:protoc-gen-prost__protoc-gen-prost",
29+
prost_plugin_flag = "--plugin=protoc-gen-prost=%s",
30+
prost_runtime = ":prost_runtime",
31+
proto_compiler = "@com_google_protobuf//:protoc",
32+
tonic_plugin = "//proto/prost/private/3rdparty/crates:protoc-gen-tonic__protoc-gen-tonic",
33+
tonic_plugin_flag = "--plugin=protoc-gen-tonic=%s",
34+
tonic_runtime = ":tonic_runtime",
35+
)
36+
37+
toolchain(
38+
name = "default_prost_toolchain",
39+
toolchain = "default_prost_toolchain_impl",
40+
toolchain_type = "//proto/prost:toolchain_type",
41+
)
42+
43+
bzl_library(
44+
name = "bzl_lib",
45+
srcs = glob(["**/*.bzl"]),
46+
deps = [
47+
"//proto/prost/private:bzl_lib",
48+
"//rust:bzl_lib",
49+
],
50+
)

proto/prost/defs.bzl

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Rules for building proto libraries in Rust."""
2+
3+
load(
4+
"//proto/prost/private:prost.bzl",
5+
_rust_prost_library = "rust_prost_library",
6+
_rust_prost_toolchain = "rust_prost_toolchain",
7+
_rust_tonic_library = "rust_tonic_library",
8+
)
9+
10+
def rust_prost_library(name, **kwargs):
11+
"""A rule for generating a Rust library using Prost.
12+
13+
Args:
14+
name (str): The name of the target.
15+
**kwargs (dict): Additional keyword arguments for the underlying
16+
`rust_prost_library` rule.
17+
"""
18+
19+
# Clippy and Rustfmt will attempt to run on these targets.
20+
# This is not correct and likely a bug in target detection.
21+
tags = kwargs.pop("tags", [])
22+
if "no-clippy" not in tags:
23+
tags.append("no-clippy")
24+
if "no-rustfmt" not in tags:
25+
tags.append("no-rustfmt")
26+
27+
_rust_prost_library(
28+
name = name,
29+
tags = tags,
30+
**kwargs
31+
)
32+
33+
def rust_tonic_library(name, **kwargs):
34+
"""A rule for generating a Rust library using Prost and Tonic.
35+
36+
Args:
37+
name (str): The name of the target.
38+
**kwargs (dict): Additional keyword arguments for the underlying
39+
`rust_tonic_library` rule.
40+
"""
41+
42+
# Clippy and Rustfmt will attempt to run on these targets.
43+
# This is not correct and likely a bug in target detection.
44+
tags = kwargs.pop("tags", [])
45+
if "no-clippy" not in tags:
46+
tags.append("no-clippy")
47+
if "no-rustfmt" not in tags:
48+
tags.append("no-rustfmt")
49+
50+
_rust_tonic_library(
51+
name = name,
52+
tags = tags,
53+
**kwargs
54+
)
55+
56+
rust_prost_toolchain = _rust_prost_toolchain
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load("//crate_universe:defs.bzl", "crate", "crates_vendor")
3+
4+
crates_vendor(
5+
name = "crates_vendor",
6+
annotations = {
7+
"protoc-gen-prost": [crate.annotation(
8+
gen_binaries = ["protoc-gen-prost"],
9+
)],
10+
"protoc-gen-tonic": [crate.annotation(
11+
gen_binaries = ["protoc-gen-tonic"],
12+
)],
13+
},
14+
cargo_lockfile = "Cargo.Bazel.lock",
15+
mode = "remote",
16+
packages = {
17+
"h2": crate.spec(
18+
version = "0.3.19",
19+
),
20+
"prost": crate.spec(
21+
version = "0.11.9",
22+
),
23+
"prost-types": crate.spec(
24+
version = "0.11.9",
25+
),
26+
"protoc-gen-prost": crate.spec(
27+
version = "0.2.2",
28+
),
29+
"protoc-gen-tonic": crate.spec(
30+
version = "0.2.2",
31+
),
32+
"tokio": crate.spec(
33+
features = ["full"],
34+
version = "1.28.2",
35+
),
36+
"tokio-stream": crate.spec(
37+
version = "0.1.14",
38+
),
39+
"tonic": crate.spec(
40+
version = "0.9.2",
41+
),
42+
},
43+
repository_name = "rules_rust_prost",
44+
tags = ["manual"],
45+
)
46+
47+
bzl_library(
48+
name = "bzl_lib",
49+
srcs = [
50+
"//proto/prost/private/3rdparty/crates:crates.bzl",
51+
"//proto/prost/private/3rdparty/crates:defs.bzl",
52+
],
53+
visibility = ["//proto/prost/private:__pkg__"],
54+
)

0 commit comments

Comments
 (0)