-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.rs
64 lines (58 loc) · 1.76 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#[cfg(feature = "protoc-rust")]
use protoc_rust::{Codegen, Customize};
#[cfg(feature = "protoc-rust")]
use std::{env, ffi::OsStr, fs, path::Path};
#[cfg(feature = "protoc-rust")]
fn proto_modules(proto_dir: &Path) -> Vec<String> {
fs::read_dir(proto_dir)
.expect("Could not read protobuf directory")
.filter_map(|entry| {
let path = entry.ok()?.path();
if path.is_file() && path.extension() == Some(OsStr::new("proto")) {
path.file_stem()
.and_then(|n| n.to_os_string().into_string().ok())
} else {
None
}
})
.collect()
}
#[cfg(feature = "protoc-rust")]
fn main() {
let in_dir = "./s2client-proto/s2clientprotocol";
let out_dir = &env::var("OUT_DIR").unwrap();
// Read list of all input protobuf files
let input_mods = proto_modules(Path::new(in_dir));
let input_files: Vec<String> = input_mods
.iter()
.map(|s| format!("{}/{}.proto", in_dir, s))
.collect();
// Compile protocol buffers
if let Err(e) = Codegen::new()
.out_dir(out_dir)
.include("s2client-proto/")
.inputs(input_files)
.customize(Customize {
expose_fields: Some(true),
..Default::default()
})
.run()
{
panic!("{:#?}", e);
}
println!("protobufs were generated successfully");
// Generate the lib.rs source code
fs::write(
format!("{}/{}", out_dir, "lib.rs"),
input_mods
.iter()
.map(|s| format!("pub mod {};", s))
.collect::<Vec<_>>()
.join("\n"),
)
.unwrap();
}
#[cfg(not(feature = "protoc-rust"))]
fn main() {
println!("using pre-generated *.rs files in 'src/'");
}