summaryrefslogtreecommitdiff
path: root/glfw/build.rs
blob: 18ac677a02dabefd92a4ba3983d31d86159a68a7 (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
use std::env;
use std::error::Error;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;

use bindgen::{Abi, MacroTypeVariation};

fn main() -> Result<(), Box<dyn Error>>
{
    println!("cargo:rustc-link-lib=glfw");

    println!("cargo:rerun-if-changed=glfw.h");

    let bindings = bindgen::Builder::default()
        .header("glfw.h")
        .clang_arg("-fretain-comments-from-system-headers")
        .generate_comments(true)
        .allowlist_function("glfw.*")
        .allowlist_type("GLFW.*")
        .allowlist_var("GLFW.*")
        .blocklist_type("GLFWglproc")
        .default_macro_constant_type(MacroTypeVariation::Signed)
        .override_abi(Abi::CUnwind, ".*")
        .generate()?;

    let out_path = PathBuf::from(env::var("OUT_DIR")?);

    let bindings_file_path = out_path.join("bindings.rs");

    bindings.write_to_file(&bindings_file_path)?;

    let mut bindings_file = OpenOptions::new().append(true).open(bindings_file_path)?;

    // Cannot be C-unwind :(
    writeln!(
        bindings_file,
        "pub type GLFWglproc = ::std::option::Option<unsafe extern \"C\" fn()>;"
    )?;

    Ok(())
}