aboutsummaryrefslogtreecommitdiffstats
path: root/build.rs
blob: 55ea62064c0d37ab858087c8b79aa6c4c456f524 (plain) (blame)
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
extern crate bindgen;

use bindgen::Builder;

use std::env;
use std::fs::File;
use std::io::Write;
use std::error::Error;
use std::path::PathBuf;

static HEADERS: &'static [&'static str] = &["linux/if_tun.h", "uapi.h"];

fn main() {
    run().expect("Could not execute build script.");
}

fn run() -> Result<(), Box<Error>> {
    // Create a wrapper header file
    let out_path = PathBuf::from(env::var("OUT_DIR")?);
    let wrapper_path = out_path.join("wrapper.h");
    let mut wrapper = File::create(&wrapper_path)?;
    for header in HEADERS {
        writeln!(wrapper, "#include <{}>", header)?;
    }

    // Generate the bindungs
    let wrapper_path_str = wrapper_path.to_str().expect("Wrapper include path corrupt.");
    let bindings = Builder::default()
        .no_unstable_rust()
        .generate_comments(true)
        .hide_type("pthread_mutex_t")
        .header(wrapper_path_str)
        .clang_arg("-I./src/wireguard/src")
        .generate()
        .expect("Unable to generate bindings");

    // Write the bindungs to the output directory
    bindings.write_to_file(out_path.join("bindings.rs"))?;
    Ok(())
}