forked from parallaxsecond/parsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
85 lines (75 loc) · 2.78 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#![allow(clippy::multiple_crate_versions, unused)]
use std::env;
use std::fs::read_dir;
use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};
#[cfg(feature = "trusted-service-provider")]
fn generate_ts_bindings(ts_include_dir: String) -> Result<()> {
let header = ts_include_dir.clone() + "/components/service/locator/interface/service_locator.h";
let encoding_header = ts_include_dir.clone() + "/protocols/rpc/common/packed-c/encoding.h";
if !Path::new(&header).exists() {
return Err(Error::new(
ErrorKind::Other,
"Trusted Services Locator header is missing. Have you run 'git submodule update --init'?",
));
}
println!("cargo:rerun-if-changed={}", header);
let bindings = bindgen::Builder::default()
.clang_arg(format!(
"-I{}",
ts_include_dir + "/components/rpc/common/interface"
))
.rustfmt_bindings(true)
.header(header)
.header(encoding_header)
.generate_comments(false)
.size_t_is_usize(true)
.derive_default(true)
.generate()
.map_err(|_| {
Error::new(
ErrorKind::Other,
"Unable to generate bindings to trusted services locator",
)
})?;
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path.join("ts_bindings.rs"))?;
println!("cargo:rustc-link-lib=dylib=ts");
Ok(())
}
#[cfg(feature = "trusted-service-provider")]
fn generate_proto_sources(contract_dir: String) -> Result<()> {
let crypto_pb_dir = contract_dir.clone() + "/service/crypto/protobuf";
let dir_entries = read_dir(Path::new(&crypto_pb_dir))?;
let files: Result<Vec<String>> = dir_entries
.map(|protos_file| {
protos_file?
.path()
.into_os_string()
.into_string()
.map_err(|_| {
Error::new(
ErrorKind::InvalidData,
"conversion from OsString to String failed",
)
})
})
// Fail the entire operation if there was an error.
.collect();
let proto_files: Vec<String> = files?
.into_iter()
.filter(|string| string.ends_with(".proto"))
.collect();
let files_slices: Vec<&str> = proto_files.iter().map(|file| &file[..]).collect();
prost_build::compile_protos(&files_slices, &[&contract_dir])
}
#[cfg(feature = "trusted-service-provider")]
fn main() -> Result<()> {
{
generate_ts_bindings(String::from("trusted-services-vendor"))?;
generate_proto_sources(String::from("trusted-services-vendor/protocols"))?;
}
Ok(())
}
#[cfg(not(feature = "trusted-service-provider"))]
fn main() {}