Skip to content

Commit

Permalink
Add Rust support
Browse files Browse the repository at this point in the history
  • Loading branch information
damienmg committed Jun 1, 2018
1 parent a2c0e84 commit 20d33f0
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 26 deletions.
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ of this project are to:

### Rules

| Language | Compile <sup>1</sup> | Build <sup>2</sup> | gRPC <sup>3</sup> |
| ---------------------------: | -----------: | --------: | -------- |
| [C++](cpp) | [cc_proto_compile](cpp#cc_proto_compile) | [cc_proto_library](cpp#cc_proto_library) [v3.4.0](https://github.com/grpc/grpc/releases/tag/v1.6.1) | [v1.6.1](https://github.com/grpc/grpc/releases/tag/v1.6.1) |
| [C#](csharp) | [csharp_proto_compile](csharp#csharp_proto_compile) | [csharp_proto_library](csharp#csharp_proto_library) | [1.0.0](https://www.nuget.org/packages/Grpc/) |
| [Closure](closure) | [closure_proto_compile](closure#closure_proto_compile) | [closure_proto_library](closure#closure_proto_library) | |
| [Go](go) | [go_proto_compile](go#go_proto_compile) | [go_proto_library](go#go_proto_library) | [v1.6.0](https://github.com/grpc/grpc-go/releases/tag/v1.6.0) |
| [Go (gogo)](gogo) | [gogo_proto_compile](gogo#gogo_proto_compile) | [gogo_proto_library](gogo#gogo_proto_library) | [fb8a35](https://github.com/gogo/protobuf/commit/fb8a359905af6e2b6517cccda0ba25915322ee88) |
| [gRPC gateway](grpc_gateway) | [grpc_gateway_proto_compile](grpc_gateway#grpc_gateway_proto_compile)<br/>[grpc_gateway_swagger_compile](grpc_gateway#grpc_gateway_swagger_compile) | [grpc_gateway_proto_library](grpc_gateway#grpc_gateway_proto_library)<br/>[grpc_gateway_binary](grpc_gateway#grpc_gateway_binary) | [v1.2.2+ (f2862b)](https://github.com/grpc-ecosystem/grpc-gateway/commit/f2862b476edcef83412c7af8687c9cd8e4097c0f) |
| [Java](java) | [java_proto_compile](java#java_proto_compile) | [java_proto_library](java#java_proto_library) | [v1.7.0](https://github.com/grpc/grpc-java/releases/tag/v1.7.0) |
| [Node](node) | [node_proto_compile](node#node_proto_compile) | [node_proto_library](node#node_proto_library) | [1.6.0](https://www.npmjs.com/package/grpc) |
| [Objective-C](objc) | [objc_proto_compile](objc#objc_proto_compile) | [objc_proto_library](objc#objc_proto_library) <sup>4</sup> | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| [Python](python) | [py_proto_compile](python#py_proto_compile) | [py_proto_library](python#py_proto_library) | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| [Ruby](ruby) | [ruby_proto_compile](ruby#ruby_proto_compile) | | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| Custom [proto_language](protobuf#proto_language) | [proto_compile](protobuf#proto_compile) | | |
| Language | Compile <sup>1</sup> | Build <sup>2</sup> | gRPC <sup>3</sup> |
| -----------------------------------------------: | --------------------------------------------------------------------------------------------------------------------------------------------------: | --------------------------------------------------------------------------------------------------------------------------------: | ------------------------------------------------------------------------------------------------------------------ |
| [C++](cpp) | [cc_proto_compile](cpp#cc_proto_compile) | [cc_proto_library](cpp#cc_proto_library) [v3.4.0](https://github.com/grpc/grpc/releases/tag/v1.6.1) | [v1.6.1](https://github.com/grpc/grpc/releases/tag/v1.6.1) |
| [C#](csharp) | [csharp_proto_compile](csharp#csharp_proto_compile) | [csharp_proto_library](csharp#csharp_proto_library) | [1.0.0](https://www.nuget.org/packages/Grpc/) |
| [Closure](closure) | [closure_proto_compile](closure#closure_proto_compile) | [closure_proto_library](closure#closure_proto_library) | |
| [Go](go) | [go_proto_compile](go#go_proto_compile) | [go_proto_library](go#go_proto_library) | [v1.6.0](https://github.com/grpc/grpc-go/releases/tag/v1.6.0) |
| [Go (gogo)](gogo) | [gogo_proto_compile](gogo#gogo_proto_compile) | [gogo_proto_library](gogo#gogo_proto_library) | [fb8a35](https://github.com/gogo/protobuf/commit/fb8a359905af6e2b6517cccda0ba25915322ee88) |
| [gRPC gateway](grpc_gateway) | [grpc_gateway_proto_compile](grpc_gateway#grpc_gateway_proto_compile)<br/>[grpc_gateway_swagger_compile](grpc_gateway#grpc_gateway_swagger_compile) | [grpc_gateway_proto_library](grpc_gateway#grpc_gateway_proto_library)<br/>[grpc_gateway_binary](grpc_gateway#grpc_gateway_binary) | [v1.2.2+ (f2862b)](https://github.com/grpc-ecosystem/grpc-gateway/commit/f2862b476edcef83412c7af8687c9cd8e4097c0f) |
| [Java](java) | [java_proto_compile](java#java_proto_compile) | [java_proto_library](java#java_proto_library) | [v1.7.0](https://github.com/grpc/grpc-java/releases/tag/v1.7.0) |
| [Node](node) | [node_proto_compile](node#node_proto_compile) | [node_proto_library](node#node_proto_library) | [1.6.0](https://www.npmjs.com/package/grpc) |
| [Objective-C](objc) | [objc_proto_compile](objc#objc_proto_compile) | [objc_proto_library](objc#objc_proto_library) <sup>4</sup> | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| [Python](python) | [py_proto_compile](python#py_proto_compile) | [py_proto_library](python#py_proto_library) | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| [Ruby](ruby) | [ruby_proto_compile](ruby#ruby_proto_compile) | | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| [Rust](rust) | [rust_proto_compile](rust#rust_proto_compile) | | [v1.6.1](https://github.com/grpc/grpc/commit/f5600e99be0fdcada4b3039c0f656a305264884a) |
| Custom [proto_language](protobuf#proto_language) | [proto_compile](protobuf#proto_compile) | | |

> Refer to [`DEPENDENCIES.md`](DEPENDENCIES.md) for a more detailed
> summary of workspace dependencies / versions.
Expand Down Expand Up @@ -101,15 +102,16 @@ go_proto_repositories()
Several languages have other `rules_*` dependencies that you'll need
to load before the `*_proto_repositories()` function is invoked:

| Language | Requires |
| ---: | :--- |
| closure_proto_repositories | [rules_closure](https://github.com/bazelbuild/rules_closure) |
| csharp_proto_repositories | [rules_dotnet](https://github.com/bazelbuild/rules_dotnet) |
| go_proto_repositories | [rules_go](https://github.com/bazelbuild/rules_go) |
| gogo_proto_repositories | [rules_go](https://github.com/bazelbuild/rules_go) |
| grpc_gateway_proto_repositories | [rules_go](https://github.com/bazelbuild/rules_go) |
| node_proto_repositories | [rules_node](https://github.com/pubref/rules_node) |
| py_proto_repositories <sup>1</sup> | [rules_python](https://github.com/bazelbuild/rules_python) |
| Language | Requires |
| ---------------------------------: | :----------------------------------------------------------- |
| closure_proto_repositories | [rules_closure](https://github.com/bazelbuild/rules_closure) |
| csharp_proto_repositories | [rules_dotnet](https://github.com/bazelbuild/rules_dotnet) |
| go_proto_repositories | [rules_go](https://github.com/bazelbuild/rules_go) |
| gogo_proto_repositories | [rules_go](https://github.com/bazelbuild/rules_go) |
| grpc_gateway_proto_repositories | [rules_go](https://github.com/bazelbuild/rules_go) |
| node_proto_repositories | [rules_node](https://github.com/pubref/rules_node) |
| py_proto_repositories <sup>1</sup> | [rules_python](https://github.com/bazelbuild/rules_python) |
| rust_proto_repositories | [rules_rust](https://github.com/bazelbuild/rules_rust) |

> <sup>1</sup> Only needed for python grpc support.
Expand Down
25 changes: 22 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,34 @@ github_archive(
sha256 = "7d06126d0d10ea8e63cc7eaf774d9ecebcd9583094ee8e93b0035da659eab5c1",
)

load("@io_bazel_rules_python//python:pip.bzl", "pip_repositories", "pip_import")
load("@io_bazel_rules_python//python:pip.bzl", "pip_import", "pip_repositories")

pip_repositories()

pip_import(
name = "pip_grpcio",
requirements = "//python:requirements.txt",
name = "pip_grpcio",
requirements = "//python:requirements.txt",
)

load("@pip_grpcio//:requirements.bzl", pip_grpcio_install = "pip_install")

pip_grpcio_install()

# ================================================================
# rust_proto_library support requires rules_rust (HEAD)
# ================================================================

github_archive(
name = "io_bazel_rules_rust",
commit = "67503c5bff487136d12fc530e3760ac7b31d330b",
org = "bazelbuild",
repo = "rules_rust",
sha256 = "6725a05ab1aac5aecb8ab20d1d86af30818bf8e657ee80ea2eba57bf73f6d676",
)

load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")

rust_repositories()

# ================================================================
# Specific Languages Support
Expand Down Expand Up @@ -156,3 +171,7 @@ py_proto_repositories()
load("//ruby:rules.bzl", "ruby_proto_repositories")

ruby_proto_repositories()

load("//rust:rules.bzl", "rust_proto_repositories")

rust_proto_repositories()
12 changes: 12 additions & 0 deletions examples/helloworld/rust/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//rust:rules.bzl", "rust_proto_library")

rust_proto_library(
name = "helloworld",
proto_deps = [
"//examples/proto:rust",
],
protos = ["//examples/helloworld/proto:protos"],
verbose = 0,
visibility = ["//examples/helloworld/rust:__subpackages__"],
with_grpc = True,
)
8 changes: 8 additions & 0 deletions examples/helloworld/rust/greeter_client/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")
load("//rust:rules.bzl", "GRPC_COMPILE_DEPS", "rust_proto_library")

rust_binary(
name = "greeter_client",
srcs = ["greeter_client.rs"],
deps = ["//examples/helloworld/rust:helloworld"] + GRPC_COMPILE_DEPS,
)
15 changes: 15 additions & 0 deletions examples/helloworld/rust/greeter_client/greeter_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extern crate grpc;
extern crate helloworld;

use std::env;

use helloworld::*;

fn main() {
let name = env::args().nth(1).unwrap_or("world".to_owned());
let client = GreeterClient::new_plain("::1", 50051, Default::default()).unwrap();
let mut req = HelloRequest::new();
req.set_name(name);
let resp = client.say_hello(grpc::RequestOptions::new(), req);
println!("{:?}", resp.wait());
}
8 changes: 8 additions & 0 deletions examples/helloworld/rust/greeter_server/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")
load("//rust:rules.bzl", "GRPC_COMPILE_DEPS", "rust_proto_library")

rust_binary(
name = "greeter_server",
srcs = ["greeter_server.rs"],
deps = ["//examples/helloworld/rust:helloworld"] + GRPC_COMPILE_DEPS,
)
33 changes: 33 additions & 0 deletions examples/helloworld/rust/greeter_server/greeter_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
extern crate helloworld;
extern crate grpc;
extern crate tls_api_stub;

use std::thread;

use helloworld::*;

struct GreeterImpl;

impl Greeter for GreeterImpl {
fn say_hello(&self, _m: grpc::RequestOptions, req: HelloRequest) -> grpc::SingleResponse<HelloReply> {
let mut r = HelloReply::new();
let name = if req.get_name().is_empty() { "world" } else { req.get_name() };
println!("greeting request from {}", name);
r.set_message(format!("Hello {}", name));
grpc::SingleResponse::completed(r)
}
}

fn main() {
let mut server = grpc::ServerBuilder::<tls_api_stub::TlsAcceptor>::new();
server.http.set_port(50051);
server.add_service(GreeterServer::new_service_def(GreeterImpl));
server.http.set_cpu_pool_threads(4);
let _server = server.build().expect("server");

println!("greeter server started on port 50051");

loop {
thread::park();
}
}
7 changes: 7 additions & 0 deletions examples/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ load("//objc:rules.bzl", "objc_proto_compile")
load("//python:rules.bzl", "py_proto_library")
load("//protobuf:rules.bzl", "proto_compile")
load("//ruby:rules.bzl", "ruby_proto_compile")
load("//rust:rules.bzl", "rust_proto_library")

filegroup(
name = "protos",
Expand Down Expand Up @@ -69,6 +70,12 @@ ruby_proto_compile(
with_grpc = False,
)

rust_proto_library(
name = "rust",
protos = [":protos"],
with_grpc = False,
)

# This conflicts with outputs from other rules here but demonstrates
# how to generate multiple language outputs simultaneously.
#
Expand Down
13 changes: 13 additions & 0 deletions protobuf/internal/proto_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def _build_output_files(run, builder):
build_package_path = generated_path + "/" + build_package_path

path = _get_relative_dirname(run, build_package_path, file)
if run.lang.output_to_libsubdir:
path.append(ctx.label.name)

for ext in exts:
temppath = list(path)
Expand All @@ -236,6 +238,15 @@ def _build_output_libdir(run, builder):
_build_output_files(run, builder)


def _build_output_libsubdir(run, builder):
# This is currently rust-specific, to output to a specific crate.
ctx = run.ctx
execdir = run.data.execdir
name = run.lang.name
builder[name + "_outdir"] = _get_offset_path(execdir, run.data.descriptor_set.dirname) + "/" + ctx.label.name
_build_output_files(run, builder)


def _build_descriptor_set(data, builder):
"""Build a list of files we expect to be generated."""
builder["args"] += ["--descriptor_set_out=" + _get_offset_path(data.execdir, data.descriptor_set.path)]
Expand Down Expand Up @@ -687,6 +698,8 @@ def _proto_compile_impl(ctx):
_build_output_library(run, builder)
elif run.lang.output_to_libdir:
_build_output_libdir(run, builder)
elif run.lang.output_to_libsubdir:
_build_output_libsubdir(run, builder)
else:
_build_output_files(run, builder)
if run.lang.go_prefix or ctx.attr.go_importpath: # golang-specific
Expand Down
2 changes: 2 additions & 0 deletions protobuf/internal/proto_language.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def _proto_language_impl(ctx):
output_to_jar = ctx.attr.output_to_jar,
output_to_library = ctx.attr.output_to_library,
output_to_libdir = ctx.attr.output_to_libdir,
output_to_libsubdir = ctx.attr.output_to_libsubdir,
output_file_style = ctx.attr.output_file_style,
supports_pb = ctx.attr.supports_pb,
pb_plugin_implements_grpc = ctx.attr.pb_plugin_implements_grpc,
Expand Down Expand Up @@ -45,6 +46,7 @@ proto_language_attrs = {
"output_to_jar": attr.bool(),
"output_to_library": attr.bool(),
"output_to_libdir": attr.bool(),
"output_to_libsubdir": attr.bool(),
"output_file_style": attr.string(),

"supports_pb": attr.bool(default = True),
Expand Down
14 changes: 14 additions & 0 deletions rust/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package(default_visibility = ["//visibility:public"])

load("//protobuf:rules.bzl", "proto_language")

proto_language(
name = "rust",
grpc_file_extensions = ["_grpc.rs"],
grpc_plugin = "@com_github_stepancheg_grpc_rust//:protoc_gen_rust_grpc",
output_to_libsubdir = True,
pb_file_extensions = [".rs"],
pb_plugin = "@com_github_stepancheg_rust_protobuf//:protoc_gen_rust",
pb_plugin_implements_grpc = False,
supports_grpc = True,
)
Loading

0 comments on commit 20d33f0

Please sign in to comment.