-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
86 lines (73 loc) · 2.4 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
use std::{
collections::HashSet,
env,
path::PathBuf,
sync::{Arc, RwLock},
};
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
// build script heavily inspired by proj-sys crate
// some parts of code from rust-bindgen
// currently the latest in apt on Github Actions
const MINIMUM_ECCODES_VERSION: &str = "2.24.0";
const PROBLEMATIC_MACROS: [&str; 5] = [
"FP_NAN",
"FP_INFINITE",
"FP_ZERO",
"FP_SUBNORMAL",
"FP_NORMAL",
];
#[derive(Debug)]
struct MacroCallback {
macros: Arc<RwLock<HashSet<String>>>,
}
impl ParseCallbacks for MacroCallback {
fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior {
self.macros.write().unwrap().insert(name.into());
if PROBLEMATIC_MACROS.contains(&name) {
return MacroParsingBehavior::Ignore;
}
MacroParsingBehavior::Default
}
}
fn main() {
if cfg!(feature = "docs") {
return;
};
let lib_result = pkg_config::Config::new()
.atleast_version(MINIMUM_ECCODES_VERSION)
.probe("eccodes");
let include_path = match lib_result {
Ok(pk) => {
eprintln!(
"Found installed ecCodes library to link at: {:?}",
pk.link_paths[0]
);
println!("cargo:rustc-link-search={:?}", pk.link_paths[0]);
println!("cargo:rustc-link-lib=eccodes");
pk.include_paths[0].clone()
}
Err(err) => {
panic!(
"Cannot find existing ecCodes library.
Please check the README for information how to correctly install ecCodes.
Additional error information: {}",
err
);
}
};
//bindgen magic to avoid duplicate math.h type definitions
let macros = Arc::new(RwLock::new(HashSet::new()));
let tests = cfg!(feature = "tests");
let bindings = bindgen::Builder::default()
.clang_arg(format!("-I{}", include_path.to_string_lossy()))
.trust_clang_mangling(false)
.header("wrapper.h")
.layout_tests(tests) //avoiding tests with UB
.parse_callbacks(Box::new(MacroCallback { macros }))
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Failed to write bindings to file");
}