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
extern crate bindgen;

use std::env;
use std::path::PathBuf;

//use std::io::prelude::*;
use std::process::Command;

const H_FILE_NAME: &str = "/libpros.h";
const CC: &str = "gcc";

pub fn bindgen(){
    // We only want to rerun the build process if the include dir has changes
    println!("cargo:rerun-if-changed=include");

    println!("Generating PROS bindings");

    let out_dir =env::var("OUT_DIR").unwrap();
    let out_h_loc = out_dir + H_FILE_NAME;

    // Preprocess the c code
    Command::new(CC)
        // Set the include path
        .arg("-I").arg("./include/")
        // Input rust.h
        .arg("-E").arg("include/rust.h")
        // Output to somewhere in target
        .arg("-o").arg(out_h_loc.clone())
        .output()
        .expect("failed to preprocess code");


    let bindings = bindgen::Builder::default()
        .header(out_h_loc.clone())
        .generate()
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");

    println!("Generated PROS bindings")
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}