-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.rs
70 lines (63 loc) · 2.07 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
#[cfg(unix)]
fn is_defined(m: &'static str) -> bool {
let file = format!("{}/test_{}.c", std::env::var("OUT_DIR").unwrap(), m);
std::fs::write(
&file,
format!(
r#"
#include <time.h>
#ifdef {}
MACRO_IS_DEFINED
#endif
"#,
m
),
)
.unwrap();
let check = cc::Build::new().file(&file).expand();
let check = String::from_utf8(check).unwrap();
check.contains("MACRO_IS_DEFINED")
}
#[cfg(not(unix))]
fn is_defined(_m: &'static str) -> bool {
false
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
fn gen_darwin_binding() {
use std::path::PathBuf;
println!("cargo:rerun-if-changed=src/clock/darwin_wrapper.h");
let bindings = bindgen::Builder::default()
.header("src/clock/darwin_wrapper.h")
.allowlist_function("mach_absolute_time")
.allowlist_function("mach_timebase_info")
.allowlist_function("pthread_mach_thread_np")
.allowlist_function("pthread_self")
.allowlist_function("thread_info")
.allowlist_type("mach_timebase_info_data_t")
.allowlist_type("thread_basic_info_data_t")
.allowlist_var("THREAD_BASIC_INFO")
.allowlist_var("__THREAD_BASIC_INFO_COUNT")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.layout_tests(false)
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("darwin_bindings.rs"))
.expect("Couldn't write bindings!");
}
fn main() {
let have_steady_clock = if cfg!(any(target_os = "macos", target_os = "ios", windows)) {
true
} else {
is_defined("CLOCK_MONOTONIC")
};
if have_steady_clock {
println!("cargo:rustc-cfg=have_steady_clock");
}
if cfg!(unix) && is_defined("CLOCK_THREAD_CPUTIME_ID") {
println!("cargo:rustc-cfg=have_clock_thread_cputime_id");
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
gen_darwin_binding();
}