-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
58 lines (50 loc) · 1.6 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
use std::env;
use std::path::PathBuf;
fn main() {
let outdir_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let libdir_path = PathBuf::from("lib/survex")
.canonicalize()
.expect("Cannot find survex/src directory");
let headers_path = libdir_path.join("img.h");
let headers_path_str = headers_path
.to_str()
.expect("Cannot convert path to string");
let obj_path = outdir_path.join("img.o");
let lib_path = outdir_path.join("libimg.a");
println!("cargo:rustc-link-search={}", outdir_path.to_str().unwrap());
println!("cargo:rustc-link-lib=img");
println!("cargo:rerun-if-changed={}", headers_path_str);
if !std::process::Command::new("clang")
.arg("-c")
.arg("-o")
.arg(&obj_path)
.arg(libdir_path.join("img.c"))
.output()
.expect("Failed to execute clang")
.status
.success()
{
panic!("Failed to compile img.c");
}
if !std::process::Command::new("ar")
.arg("rcs")
.arg(lib_path)
.arg(obj_path)
.output()
.expect("could not spawn `ar`")
.status
.success()
{
panic!("Failed to create libimg.a");
}
let bindings = bindgen::Builder::default()
.header(headers_path_str)
.allowlist_function("img_.*")
.allowlist_type("img_.*")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(outdir_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}