summaryrefslogtreecommitdiffstats
path: root/build.rs
blob: 58cbd4be78c7c14c6385a9a705218a1b6168cc1c (plain)
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
use std::env;
use std::path::PathBuf;

fn main() {
    let libdir_path = PathBuf::from("ext")
        .canonicalize()
        .expect("cannot canonicalize ext/ path");

    let obj_path = libdir_path.join("hap.o");
    let lib_path = libdir_path.join("libhap.a");

    println!("cargo:rustc-link-search={}", libdir_path.display());
    println!("cargo:rustc-link-lib=static=hap");
    println!("cargo:rustc-link-lib=snappy");
    println!("cargo:rerun-if-changed=ext/hap.c");
    println!("cargo:rerun-if-changed=ext/hap.h");

    if !std::process::Command::new("clang")
        .arg("-c")
        .arg("-o")
        .arg(&obj_path)
        .arg(libdir_path.join("hap.c"))
        .output()
        .expect("could not spawn `clang`")
        .status
        .success()
    {
        panic!("could not compile hap.c");
    }

    if !std::process::Command::new("ar")
        .arg("rcs")
        .arg(&lib_path)
        .arg(&obj_path)
        .output()
        .expect("could not spawn `ar`")
        .status
        .success()
    {
        panic!("could not create libhap.a");
    }

    let bindings = bindgen::Builder::default()
        .header(libdir_path.join("hap.h").to_str().unwrap())
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .generate()
        .expect("unable to generate HAP bindings");

    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("hap_bindings.rs");
    bindings
        .write_to_file(out_path)
        .expect("couldn't write HAP bindings");
}