forked from SheldonNico/ctp-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
executable file
·228 lines (205 loc) · 7.29 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use lazy_static::lazy_static;
use regex::Regex;
use std::env;
use std::path::{Path, PathBuf};
fn main() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let platform = if cfg!(target_family = "windows") {
"windows"
} else {
"unix"
};
let arch = if cfg!(target_arch = "x86_64") {
"x86_64"
} else if cfg!(target_arch = "x86") {
"x86"
} else {
panic!("can not build on this platform.")
};
cc::Build::new()
.cpp(true)
.file("src/wrapper.cpp")
.flag_if_supported("-std=c++17")
.flag_if_supported("-w")
.compile("wrapper");
println!(
"cargo:rustc-link-search={}",
root.join("shared/md")
.join(format!("{}.{}", platform, arch))
.display()
);
println!(
"cargo:rustc-link-search={}",
root.join("shared/td")
.join(format!("{}.{}", platform, arch))
.display()
);
println!(
"cargo:rustc-link-search={}",
root.join("shared/data_collect")
.join(format!("{}.{}", platform, arch))
.display()
);
if platform == "unix" {
println!("cargo:rustc-link-lib=dylib=LinuxDataCollect");
} else {
println!("cargo:rustc-link-lib=dylib=WinDataCollect");
}
println!("cargo:rustc-link-lib=dylib=thostmduserapi_se");
println!("cargo:rustc-link-lib=dylib=thosttraderapi_se");
// Tell cargo to invalidate the built crate whenever the wrapper changes
// println!("cargo:rerun-if-changed=src/wrapper.hpp"); // 2022.11.15 delete
println!("cargo:rerun-if-changed=src/wrapper.cpp");
// ctp api header is clean enough, we will use blacklist instead whitelist
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("src/wrapper.cpp")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.derive_debug(true)
.derive_default(true)
// make output smaller
.layout_tests(false)
.generate_comments(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// we will handle class mannually by `autobind.py`
// function defined in rust
.opaque_type("CThostFtdcTraderApi")
.opaque_type("CThostFtdcTraderSpi")
.opaque_type("CThostFtdcMdApi")
.opaque_type("CThostFtdcMdSpi")
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
let output = bindings.to_string().replace("u8", "i8");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let outdir = PathBuf::from("src").join("generated");
std::fs::create_dir_all(outdir).expect("Couldn't create dir generated!");
let outfile = PathBuf::from("src").join("generated/mod.rs");
std::fs::write(&outfile, &output).expect("Couldn't write bindings!");
let mut buf = replace_trait(
&outfile,
&[
"Rust_CThostFtdcMdSpi_Trait",
"Rust_CThostFtdcTraderSpi_Trait",
],
)
.expect("Fail to replace trait!");
buf = format!(
r#"#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
{}"#,
buf
);
std::fs::write(&outfile, &buf).expect("Fail to write converted bindings!");
}
fn camel_to_snake<'t>(name: &'t str) -> String {
lazy_static! {
static ref PATTERN1: Regex = Regex::new(r"(.)([A-Z][a-z]+)").unwrap();
static ref PATTERN2: Regex = Regex::new(r"([a-z0-9])([A-Z])").unwrap();
}
PATTERN2
.replace_all(
PATTERN1.replace_all(name, r"${1}_${2}").as_ref(),
r"${1}_${2}",
)
.to_lowercase()
}
fn replace_trait(fname: &Path, traits: &[&str]) -> Result<String, Box<dyn std::error::Error>> {
let mut buf = std::fs::read_to_string(fname)?;
for trait_extern in traits {
let pattern = Regex::new(&format!(
r#"extern \s*"C"\s*\{{\s*pub\s+fn\s+{}_(\w+)\s*\(([^)]*)\)([^;]*);\s*}}\s*"#,
trait_extern
))
.unwrap();
let pattern_arg = Regex::new(r"\s*(\w+)\s*:\s*(.*)\s*").unwrap();
let mut exports = vec![];
let mut traitfuns = vec![];
assert!(
pattern.captures(&buf).is_some(),
"`{}` not found in source code",
trait_extern
);
for cap in pattern.captures_iter(&buf) {
let fname = cap.get(1).unwrap().as_str().trim();
let args: Vec<_> = cap
.get(2)
.unwrap()
.as_str()
.split(",")
.filter(|s| s.trim().len() > 0)
.map(|s| {
let c = pattern_arg.captures(s).unwrap();
(c.get(1).unwrap().as_str(), c.get(2).unwrap().as_str())
})
.collect();
let rtn = cap.get(3).unwrap().as_str();
let fname_camel = camel_to_snake(fname);
if fname_camel == "drop" {
continue;
}
assert!(args[0].1.trim().ends_with("c_void"));
let mut tmp = args[1..]
.iter()
.map(|s| format!("{}: {}", s.0, s.1))
.collect::<Vec<_>>();
tmp.insert(0, "trait_obj: *mut ::std::os::raw::c_void".into());
let args_repl = tmp.join(", ");
let argv_repl = args[1..].iter().map(|s| s.0).collect::<Vec<_>>().join(", ");
let export = format!(
r#"#[no_mangle]
pub extern "C" fn {trait_extern}_{fname}({args_repl}){rtn} {{
let trait_obj = trait_obj as *mut Box<dyn {trait_extern}>;
let trait_obj: &mut dyn {trait_extern} = unsafe {{ &mut **trait_obj }};
trait_obj.{fname_camel}({argv_repl})
}}
"#,
trait_extern = trait_extern,
fname = fname,
args_repl = args_repl,
rtn = rtn,
fname_camel = fname_camel,
argv_repl = argv_repl
);
exports.push(export);
let mut tmp = args[1..]
.iter()
.map(|s| format!("{}: {}", s.0, s.1))
.collect::<Vec<_>>();
tmp.insert(0, "&mut self".into());
let args_repl = tmp.join(", ");
let traitfun = format!(
r" fn {fname_camel}({args_repl}){rtn} {{ }}",
fname_camel = fname_camel,
args_repl = args_repl,
rtn = rtn
);
traitfuns.push(traitfun);
}
let exports_repl = exports.join("\n");
let traitfuns_repl = traitfuns.join("\n");
buf = format!(
r#"{ori}
#[allow(unused)]
pub trait {trait_extern} {{
{traitfuns_repl}
}}
{exports_repl}
#[no_mangle]
pub extern "C" fn {trait_extern}_Drop(trait_obj: *mut ::std::os::raw::c_void) {{
let trait_obj = trait_obj as *mut Box<dyn {trait_extern}>;
let _r: Box<Box<dyn {trait_extern}>> = unsafe {{ Box::from_raw(trait_obj) }};
}}
"#,
ori = pattern.replace_all(&buf, "").to_string(),
exports_repl = exports_repl,
trait_extern = trait_extern,
traitfuns_repl = traitfuns_repl
);
}
Ok(buf)
}