Skip to content

Commit f0a26b5

Browse files
authored
*: fix builds (#351)
This PR breaks the circle dependencies between grpcio and grpcio-proto by adding a new crate named tests and examples. We can bring things back once rust-lang/cargo#6915 is fixed. The new structure also fixes a problem that crates.io denies to accept circle dependencies in dev-dependencies. More see rust-lang/cargo#4242. This PR also updates protobuf-build to the latest version.
1 parent fd9b6c6 commit f0a26b5

24 files changed

+96
-245
lines changed

Cargo.toml

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bytes = { version = "0.4.11", optional = true }
2626
log = "0.4"
2727

2828
[workspace]
29-
members = ["proto", "benchmark", "compiler", "interop"]
29+
members = ["proto", "benchmark", "compiler", "interop", "tests-and-examples"]
3030

3131
[features]
3232
default = ["protobuf-codec", "secure"]
@@ -37,34 +37,6 @@ openssl = ["secure", "grpcio-sys/openssl"]
3737
openssl-vendored = ["secure", "grpcio-sys/openssl-vendored"]
3838
no-omit-frame-pointer = ["grpcio-sys/no-omit-frame-pointer"]
3939

40-
[[example]]
41-
name = "route_guide_client"
42-
path = "examples/route_guide/client.rs"
43-
44-
[[example]]
45-
name = "route_guide_server"
46-
path = "examples/route_guide/server.rs"
47-
48-
[[example]]
49-
name = "greeter_client"
50-
path = "examples/hello_world/client.rs"
51-
52-
[[example]]
53-
name = "greeter_server"
54-
path = "examples/hello_world/server.rs"
55-
56-
[dev-dependencies]
57-
serde_json = "1.0"
58-
serde = "1.0"
59-
serde_derive = "1.0"
60-
grpcio-proto = { path = "proto", version = "0.5.0-alpha.2", default-features = false }
61-
rand = "0.4"
62-
slog = "2.0"
63-
slog-async = "2.1"
64-
slog-stdlog = "3.0"
65-
slog-scope = "4.0"
66-
slog-term = "2.2"
67-
6840
[profile.release]
6941
debug = true
7042

benchmark/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::time::{Duration, Instant};
1717
use grpc_proto::testing::stats::HistogramData;
1818
use grpc_sys;
1919

20-
#[path = "../../examples/log_util.rs"]
20+
#[path = "../../tests-and-examples/examples/log_util.rs"]
2121
pub mod log_util;
2222

2323
pub struct Sample {

proto/Cargo.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ build = "build.rs"
1414

1515
[features]
1616
default = ["protobuf-codec"]
17-
protobuf-codec = ["grpcio/protobuf-codec", "protobuf-codegen", "protobuf", "grpcio-compiler/protobuf-codec"]
18-
prost-codec = ["prost-derive", "bytes", "lazy_static", "grpcio/prost-codec", "prost-types", "prost", "grpcio-compiler/prost-codec"]
17+
protobuf-codec = ["grpcio/protobuf-codec", "grpcio-compiler/protobuf-codec", "protobuf-build/grpcio-protobuf-codec"]
18+
prost-codec = ["prost-derive", "bytes", "lazy_static", "grpcio/prost-codec", "prost", "grpcio-compiler/prost-codec", "protobuf-build/grpcio-prost-codec"]
1919

2020
[dependencies]
2121
futures = "0.1"
@@ -27,9 +27,6 @@ protobuf = "2"
2727
lazy_static = { version = "1.3", optional = true }
2828

2929
[build-dependencies]
30-
protobuf-build = "0.7"
30+
protobuf-build = { version = "0.8", default-features = false }
3131
grpcio-compiler = { path = "../compiler", version = "0.5.0-alpha.2", default-features = false }
32-
prost = { version = "0.5", optional = true }
33-
prost-types = { version = "0.5", optional = true }
34-
protobuf = { version = "2", optional = true }
35-
protobuf-codegen = { version = "2", optional = true }
32+
walkdir = "2.2"

proto/build.rs

Lines changed: 17 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -11,144 +11,27 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
use std::path::Path;
14+
use std::env;
1515

1616
fn main() {
17-
for package in &["testing", "example", "health"] {
18-
compile(format!("{}.desc", package), package);
19-
}
20-
}
21-
22-
#[cfg(feature = "prost-codec")]
23-
fn compile<P: AsRef<Path>>(desc_path: P, _package: &str) {
24-
prost::desc_to_module(desc_path.as_ref());
25-
}
26-
27-
#[cfg(feature = "prost-codec")]
28-
mod prost {
29-
use std::env;
30-
use std::fs;
31-
use std::path::{Path, PathBuf};
32-
33-
use prost::Message;
34-
use prost_types::FileDescriptorSet as ProstFileDescriptorSet;
35-
36-
/// Descriptor file to module file using Prost.
37-
pub fn desc_to_module(descriptor: &Path) {
38-
let buf = fs::read(descriptor).unwrap();
39-
let proto_set: ProstFileDescriptorSet = Message::decode(buf).unwrap();
40-
let files: Vec<_> = proto_set
41-
.file
17+
let out_dir = env::var("OUT_DIR").unwrap();
18+
let modules = &[
19+
("grpc/testing", "testing"),
20+
("grpc/health/v1/", "health"),
21+
("grpc/example", "example"),
22+
];
23+
for (dir, package) in modules {
24+
let out_dir = format!("{}/{}", out_dir, package);
25+
let files: Vec<_> = walkdir::WalkDir::new(format!("proto/{}", dir))
4226
.into_iter()
43-
.map(|f| {
44-
let mut path = PathBuf::new();
45-
path.push("proto");
46-
path.push(f.name.unwrap());
47-
path.to_str().unwrap().to_owned()
27+
.filter_map(|p| {
28+
let dent = p.expect("Error happened when search protos");
29+
if !dent.file_type().is_file() {
30+
return None;
31+
}
32+
Some(format!("{}", dent.path().display()))
4833
})
4934
.collect();
50-
51-
let packages = protobuf_build::generate_prost_files(&files, &env::var("OUT_DIR").unwrap());
52-
assert!(!packages.is_empty());
53-
protobuf_build::generate_wrappers(
54-
&packages
55-
.iter()
56-
.map(|m| format!("{}/{}.rs", env::var("OUT_DIR").unwrap(), m))
57-
.collect::<Vec<_>>(),
58-
&env::var("OUT_DIR").unwrap(),
59-
protobuf_build::GenOpt::all(),
60-
);
61-
}
62-
}
63-
64-
#[cfg(feature = "protobuf-codec")]
65-
fn compile<P: AsRef<Path>>(desc_path: P, package: &str) {
66-
protobuf::compile(desc_path, package);
67-
}
68-
69-
#[cfg(feature = "protobuf-codec")]
70-
mod protobuf {
71-
use std::env;
72-
use std::fs::{self, File};
73-
use std::io::Write;
74-
use std::path::Path;
75-
76-
use grpcio_compiler::codegen as grpc_gen;
77-
use protobuf::compiler_plugin::GenResult;
78-
use protobuf::descriptor::{FileDescriptorProto, FileDescriptorSet};
79-
use protobuf_codegen::{self as pb_gen, Customize};
80-
81-
/// Compile all related proto file to `FileDescriptorSet` and use it to generate
82-
/// rust source using rust-protobuf.
83-
///
84-
/// Using `FileDescriptorSet` here so we don't need to compile the binaries like
85-
/// protoc-gen-rust and grpc_rust_plugin.
86-
pub fn compile<P: AsRef<Path>>(desc_path: P, package: &str) {
87-
let out_dir = env::var("OUT_DIR").unwrap();
88-
let module_path = Path::new(&out_dir).join(package);
89-
if !module_path.exists() {
90-
fs::create_dir_all(&module_path).unwrap();
91-
}
92-
93-
let mod_rs = module_path.join("mod.rs");
94-
let mut module_file = File::create(mod_rs).unwrap();
95-
96-
desc_to_module(
97-
desc_path.as_ref(),
98-
&module_path,
99-
|a, b| {
100-
let c = Customize::default();
101-
pb_gen::gen(a, b, &c)
102-
},
103-
&mut module_file,
104-
);
105-
desc_to_module(
106-
desc_path.as_ref(),
107-
&module_path,
108-
grpc_gen::gen,
109-
&mut module_file,
110-
);
111-
}
112-
113-
/// Descriptor file to module file using rust-protobuf.
114-
fn desc_to_module<G, W>(descriptor: &Path, output_dir: &Path, mut generate_files: G, out: W)
115-
where
116-
G: FnMut(&[FileDescriptorProto], &[String]) -> Vec<GenResult>,
117-
W: Write,
118-
{
119-
let mut f = File::open(descriptor).unwrap();
120-
let proto_set: FileDescriptorSet = protobuf::parse_from_reader(&mut f).unwrap();
121-
122-
let files: Vec<_> = proto_set
123-
.get_file()
124-
.into_iter()
125-
.map(|f| f.get_name().to_owned())
126-
.collect();
127-
128-
// All files need to be generated in our case.
129-
let results = generate_files(proto_set.get_file(), &files);
130-
write_files(
131-
results.into_iter().map(|res| (res.name, res.content)),
132-
output_dir,
133-
out,
134-
);
135-
}
136-
137-
fn write_files<W: Write>(
138-
results: impl Iterator<Item = (String, Vec<u8>)>,
139-
output_dir: &Path,
140-
mut out: W,
141-
) {
142-
if !output_dir.exists() {
143-
fs::create_dir_all(output_dir).unwrap();
144-
}
145-
146-
for (name, content) in results {
147-
let out_file = output_dir.join(&name);
148-
let mut f = File::create(&out_file).unwrap();
149-
f.write_all(&content).unwrap();
150-
let (module_name, _) = name.split_at(name.len() - 3); // ".rs".len() == 3
151-
writeln!(out, "pub mod {};", module_name).unwrap();
152-
}
35+
protobuf_build::generate_files(&["proto".to_owned()], &files, &out_dir);
15336
}
15437
}

proto/build.sh

Lines changed: 0 additions & 26 deletions
This file was deleted.

proto/example.desc

-1.07 KB
Binary file not shown.

proto/health.desc

-374 Bytes
Binary file not shown.

proto/src/lib.rs

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,31 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
pub mod testing {
15+
include!(concat!(env!("OUT_DIR"), "/testing/mod.rs"));
16+
17+
#[cfg(feature = "prost-codec")]
18+
pub use self::grpc::testing::*;
19+
}
20+
21+
pub mod example {
22+
include!(concat!(env!("OUT_DIR"), "/example/mod.rs"));
23+
}
24+
25+
pub mod health {
26+
pub mod v1 {
27+
include!(concat!(env!("OUT_DIR"), "/health/mod.rs"));
28+
29+
#[cfg(feature = "prost-codec")]
30+
pub use self::grpc::health::v1::*;
31+
}
32+
}
33+
1434
#[cfg(feature = "prost-codec")]
1535
#[allow(clippy::large_enum_variant)]
16-
pub mod testing {
17-
include!(concat!(env!("OUT_DIR"), "/grpc.testing.rs"));
18-
include!(concat!(env!("OUT_DIR"), "/wrapper_grpc.testing.rs"));
36+
pub mod help {
37+
38+
use super::testing::*;
1939

2040
// Wrapper functions for oneof fields.
2141
impl ClientArgs {
@@ -114,49 +134,4 @@ pub mod testing {
114134
}
115135
}
116136

117-
#[cfg(feature = "prost-codec")]
118-
pub mod example {
119-
pub mod helloworld {
120-
include!(concat!(env!("OUT_DIR"), "/helloworld.rs"));
121-
include!(concat!(env!("OUT_DIR"), "/wrapper_helloworld.rs"));
122-
}
123-
pub mod route_guide {
124-
include!(concat!(env!("OUT_DIR"), "/routeguide.rs"));
125-
include!(concat!(env!("OUT_DIR"), "/wrapper_routeguide.rs"));
126-
}
127-
}
128-
129-
#[cfg(feature = "prost-codec")]
130-
pub mod health {
131-
pub mod v1 {
132-
pub mod health {
133-
include!(concat!(env!("OUT_DIR"), "/grpc.health.v1.rs"));
134-
include!(concat!(env!("OUT_DIR"), "/wrapper_grpc.health.v1.rs"));
135-
}
136-
}
137-
}
138-
139-
#[cfg(feature = "protobuf-codec")]
140-
#[allow(bare_trait_objects)]
141-
#[allow(renamed_and_removed_lints)]
142-
pub mod testing {
143-
include!(concat!(env!("OUT_DIR"), "/testing/mod.rs"));
144-
}
145-
146-
#[cfg(feature = "protobuf-codec")]
147-
#[allow(bare_trait_objects)]
148-
#[allow(renamed_and_removed_lints)]
149-
pub mod example {
150-
include!(concat!(env!("OUT_DIR"), "/example/mod.rs"));
151-
}
152-
153-
#[cfg(feature = "protobuf-codec")]
154-
pub mod health {
155-
#[allow(bare_trait_objects)]
156-
#[allow(renamed_and_removed_lints)]
157-
pub mod v1 {
158-
include!(concat!(env!("OUT_DIR"), "/health/mod.rs"));
159-
}
160-
}
161-
162137
pub mod util;

proto/testing.desc

-9.42 KB
Binary file not shown.

0 commit comments

Comments
 (0)