summaryrefslogtreecommitdiff
path: root/build.rs
blob: 18c690adfa4a2f4863497049efad3b2cea87ca6d (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
54
55
use std::env;
use std::path::PathBuf;
use std::process::Command;
use bindgen;

fn run_command(cmd: &str, args: &[&str]) -> Option<()> {
    let status = Command::new(cmd).args(args).status().expect("Failed to execute process");
    if status.success() {
        Some(())
    } else {
        None
    }
}

fn check_output(cmd: &str, args: &[&str]) -> Option<String> {
    let output = Command::new(cmd).args(args).output().expect("Failed to execute process");
    if output.status.success() {
        Some(String::from_utf8(output.stdout).unwrap())
    } else {
        None
    }
}

fn main() {
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    run_command("git", &["-C", out_dir.to_str().unwrap(), "submodule", "update", "--init", "--recursive"])
        .expect("Failed to clone git submodules");

    let status_output =
            check_output("git", &["-C", out_dir.to_str().unwrap(), "submodule", "status"])
                .expect("Failed to check git submodule status");

    let sub_repo =
            out_dir.join(PathBuf::from(status_output.split_whitespace().skip(1).next().unwrap()));

    run_command("make", &["-C", sub_repo.to_str().unwrap()])
        .expect("Failed to build termio library");

    let bindings = bindgen::Builder::default()
            .header(sub_repo.join("termio.h").to_str().unwrap())
            .blacklist_item("true_")
            .blacklist_item("false_")
            .blacklist_item("__bool_true_false_are_defined")
            .blacklist_function("tprintf")
            .blacklist_function("lgw_addf")
            .generate()
            .expect("Error generating bindings using bindgen");

    bindings
        .write_to_file(out_dir.join("bindings.rs"))
        .expect("Error writing bindings to file");

    println!("cargo:rustc-link-lib=static=termio");
    println!("cargo:rustc-link-search={}", sub_repo.to_str().unwrap());
}