Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,4 @@ grpc = { path = "../grpc", optional = true }
[build-dependencies]
tonic-prost-build = { path = "../tonic-prost-build" }
grpc-protobuf-build = { path = "../grpc-protobuf-build", default-features = false }
protoc-gen-rust-grpc = { path = "../protoc-gen-rust-grpc", optional = true }
33 changes: 22 additions & 11 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

Set of examples that show off the features provided by `tonic` and `grpc`.

In order to build these examples, you must have the `protoc` Protocol Buffers compiler
installed, along with the Protocol Buffers resource files.
In order to build these examples, you must have the `protoc` Protocol Buffers
compiler. You need to have installed either:

* the `protoc` binary, made available in your PATH. This is the default.

* A compatible C++ compiler and CMake as described in the
[`protoc-gen-rust-grpc` crate](../protoc-gen-rust-grpc/README.md). Choose
this option by passing `--features protoc-gen-rust-grpc` to `cargo`.

If you choose to install protobuf, here are the steps for a variety of operating
systems.

Ubuntu:

Expand All @@ -28,16 +37,18 @@ brew install protobuf

# `grpc` crate examples

For the examples related to the `grpc` crate, the protobuf generated code is not
regenerated by default when running `cargo`. Instead you must set
`GRPC_RUST_REGENERATE_PROTO=1` in your environment. This requires that you have
the `protoc-gen-rust-grpc` binary installed in your path. This can be
downloaded from [our releases].
For the examples related to the `grpc` crate, the generated code is checked into
the repo to allow building without `protoc`. To rebuild the generated code you
must set `GRPC_RUST_REGENERATE_PROTO=1` in your environment. This requires that
you have installed either:

* the `protoc` and `protoc-gen-rust-grpc` binaries, made available in your
PATH. See above for `protoc`. `protoc-gen-rust-grpc` can be downloaded from
[our releases].

Alternatively, you can also set the `grpc-protobuf-build/build-plugin` feature
flag when building via `cargo` using the `--features` flag. In this case you
will need a compatible C++ compiler and CMake as described in the
`protoc-gen-rust-grpc` crate.
* A compatible C++ compiler and CMake as described in the
[`protoc-gen-rust-grpc` crate](../protoc-gen-rust-grpc/README.md). Choose
this option by passing `--features grpc-protobuf-build/build-plugin` to `cargo`.

[our releases]: https://github.com/grpc/grpc-rust/releases

Expand Down
11 changes: 11 additions & 0 deletions examples/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ use std::{env, path::PathBuf};

fn main() {
println!("cargo:rerun-if-changed=build.rs");

// Optionally use protoc-gen-rust-grpc's protoc for prost. protoc-gen-rust-grpc will skip its
// build when PROTOC_GEN_RUST_GRPC_NO_BUILD=1 (used in gRPC's CI), so we check that the binary
// exists.
#[cfg(feature = "protoc-gen-rust-grpc")]
if protoc_gen_rust_grpc::protoc().exists() {
unsafe {
env::set_var("PROTOC", protoc_gen_rust_grpc::protoc());
}
}

tonic_prost_build::configure()
.compile_protos(&["proto/routeguide/route_guide.proto"], &["proto"])
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions grpc-protobuf-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ impl CodeGen {
// 2. Compiled via protoc-gen-rust-grpc (build-plugin feature)
#[cfg(feature = "build-plugin")]
{
let compiled_protoc = PathBuf::from(protoc_gen_rust_grpc::protoc());
let compiled_plugin = PathBuf::from(protoc_gen_rust_grpc::protoc_gen_rust_grpc());
let compiled_protoc = protoc_gen_rust_grpc::protoc();
let compiled_plugin = protoc_gen_rust_grpc::protoc_gen_rust_grpc();
if compiled_protoc.exists() && compiled_plugin.exists() {
// The files may not exist if a build setting instructed protoc-gen-rust-grpc to
// skip the C++ build (DOCS_RS / our CI setting).
Expand Down
5 changes: 5 additions & 0 deletions protoc-gen-rust-grpc/src/cpp_source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ if(BUILD_PROTOC)
install(PROGRAMS ${CMAKE_BINARY_DIR}/bin/${PROTOC_OUTPUT_NAME}
DESTINATION bin
)

install(DIRECTORY ${protobuf_SOURCE_DIR}/src/google/protobuf/
DESTINATION include/google/protobuf
FILES_MATCHING PATTERN "*.proto"
)
endif()

# Print summary
Expand Down
21 changes: 7 additions & 14 deletions protoc-gen-rust-grpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,26 @@

use std::path::PathBuf;

fn bin_file(file: &str) -> String {
let mut path = PathBuf::from(bin()).join(file);
fn bin_file(file: &str) -> PathBuf {
let mut path = bin().join(file);
if cfg!(target_os = "windows") {
path.set_extension("exe");
}
path.to_str()
.expect("Path contains invalid UTF-8")
.to_owned()
path
}

/// The full path to the `protoc` executable.
pub fn protoc() -> String {
pub fn protoc() -> PathBuf {
bin_file("protoc")
}

/// The full path to the gRPC `protoc` plugin, `protoc-gen-rust-grpc`.
pub fn protoc_gen_rust_grpc() -> String {
pub fn protoc_gen_rust_grpc() -> PathBuf {
bin_file("protoc-gen-rust-grpc")
}

/// The path to the `bin` directory containing the C++ binaries this package
/// builds.
pub fn bin() -> String {
PathBuf::from(env!("OUT_DIR"))
.join("build")
.join("bin")
.to_str()
.expect("Path contains invalid UTF-8")
.to_owned()
pub fn bin() -> PathBuf {
PathBuf::from(env!("OUT_DIR")).join("bin")
}
Loading