-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.rs
68 lines (64 loc) · 2.33 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
65
66
67
68
use std::{
env, fs,
path::{Path, PathBuf},
};
use prost::Message;
fn compile_protos_with_config<F>(
file_descriptor_path: impl AsRef<Path>,
protos: &[impl AsRef<Path>],
includes: &[impl AsRef<Path>],
config_fn: F,
) -> Result<(), Box<dyn std::error::Error>>
where
F: FnOnce(&mut prost_build::Config) -> Result<(), Box<dyn std::error::Error>>,
{
let mut config = prost_build::Config::new();
config.enable_type_names();
let () = config_fn(&mut config)?;
tonic_build::configure()
.skip_protoc_run()
.file_descriptor_set_path(file_descriptor_path)
.compile_protos_with_config(config, protos, includes)?;
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
const COMMON_PROTO: &str = "cusf_sidechain_proto/proto/cusf/common/v1/common.proto";
const CRYPTO_PROTO: &str = "cusf_sidechain_proto/proto/cusf/crypto/v1/crypto.proto";
const MAINCHAIN_COMMON_PROTO: &str =
"cusf_sidechain_proto/proto/cusf/mainchain/v1/common.proto";
const SIDECHAIN_PROTO: &str = "cusf_sidechain_proto/proto/cusf/sidechain/v1/sidechain.proto";
const VALIDATOR_PROTO: &str = "cusf_sidechain_proto/proto/cusf/mainchain/v1/validator.proto";
const WALLET_PROTO: &str = "cusf_sidechain_proto/proto/cusf/mainchain/v1/wallet.proto";
const ALL_PROTOS: &[&str] = &[
COMMON_PROTO,
CRYPTO_PROTO,
MAINCHAIN_COMMON_PROTO,
SIDECHAIN_PROTO,
VALIDATOR_PROTO,
WALLET_PROTO,
];
const INCLUDES: &[&str] = &["cusf_sidechain_proto/proto"];
let file_descriptors = protox::compile(ALL_PROTOS, INCLUDES)?;
let file_descriptor_path =
PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set"))
.join("file_descriptor_set.bin");
fs::write(&file_descriptor_path, file_descriptors.encode_to_vec())?;
let () =
compile_protos_with_config(&file_descriptor_path, &[COMMON_PROTO], INCLUDES, |_| Ok(()))?;
let () = compile_protos_with_config(
&file_descriptor_path,
&[
CRYPTO_PROTO,
MAINCHAIN_COMMON_PROTO,
SIDECHAIN_PROTO,
VALIDATOR_PROTO,
WALLET_PROTO,
],
INCLUDES,
|config| {
config.extern_path(".cusf.common.v1", "crate::proto::common");
Ok(())
},
)?;
Ok(())
}