-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
78 lines (69 loc) · 2.58 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
/*
build.rs for shapelib-rs
(with C/C++ Bridge)
cc-rs bindgen
and generate link option
*/
extern crate cc;
extern crate bindgen;
use std::path::PathBuf;
fn main() {
let s_path: String = if cfg!(docsrs) {
std::env::var("OUT_DIR").unwrap()
}else{
".".to_string()
}; // to keep lifetime
let o_path: &str = s_path.as_str();
let c_opt = if o_path == "." { "-std:c11" }else{ "-std=c++11" };
// default bindgen should not be inline otherwise link error
// cc with no default inline when bindgen .generate_inline_functions(true)
// -fno-implement-inlines : #pragma implementation
// -fkeep-inline-functions : ***CAUTION***
// -finline-functions : assume all inline ***CAUTION***
// -fno-inline : ignore all inline ***CAUTION***
// -fno-default-inline : not assume inline defined in the class
// let c_inl = if o_path == "." { "-Ob0" }else{ "-fno-default-inline" };
// let c_asm = if o_path == "." { "-Fa" }else{ "-S" };
let mk_cc = |dname: &str, sname: &str, iname: &str, oname: &str| {
let sd = PathBuf::from(dname);
let fname = format!("{}", sd.join(sname).to_str().expect("invalid path"));
println!("cargo:rerun-if-changed={}", fname);
cc::Build::new()
.file(fname)
.flag(c_opt)
// .flag("-std=c++11") // gcc
// .flag("-std:c11") // cl
// .flag("-std:c++14") // cl
// .flag(c_inl)
// .flag(c_asm)
.include(iname)
.compile(oname)
};
mk_cc("./src", "bridge.cpp", "./include", "bridge");
let mk_bindings = |hdd: &str, header: &str, rsd: &str, rsfile: &str,
binl: bool, bcmt: bool| { // inline, comment
let hd = PathBuf::from(hdd);
let hf = format!("{}", hd.join(header).to_str().expect("invalid path"));
println!("cargo:rerun-if-changed={}", hf);
let bindings = bindgen::Builder::default()
.header(hf)
.generate_inline_functions(binl) // default: false (.hpp inline if true)
.generate_comments(bcmt) // default: true
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
let rs = PathBuf::from(rsd);
bindings
.write_to_file(rs.join(rsfile))
.expect("Could not write bindings!");
};
if o_path == "." {
mk_bindings("./include", "bridge.hpp", "./include", "bridge_bindings.rs",
false, true); // cc should be compiled with option no default inline
mk_bindings("./shapelib/include", "shapefil.h",
"./shapelib/include", "shapefil_bindings.rs",
false, true);
}
println!("cargo:rustc-link-search=./shapelib/lib");
println!("cargo:rustc-link-lib=shapelib_i");
}