|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::path::PathBuf; |
| 4 | +use subprocess::Exec; |
| 5 | + |
| 6 | +static PROJECT_ROOT: &str = "../../../"; |
| 7 | + |
| 8 | +fn write_bindings(header_path: &PathBuf) { |
| 9 | + let header_path_string = String::from(header_path.to_string_lossy()); |
| 10 | + |
| 11 | + // The bindgen::Builder is the main entry point |
| 12 | + // to bindgen, and lets you build up options for |
| 13 | + // the resulting bindings. |
| 14 | + let bindings = bindgen::Builder::default() |
| 15 | + // The input header we would like to generate |
| 16 | + // bindings for. |
| 17 | + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) |
| 18 | + .rust_target(bindgen::RustTarget::Stable_1_68) |
| 19 | + // .raw_line("#![allow(non_upper_case_globals)]") |
| 20 | + // .raw_line("#![allow(non_camel_case_types)]") |
| 21 | + // .raw_line("#![allow(non_snake_case)]") |
| 22 | + .header(&header_path_string) |
| 23 | + .allowlist_file(&header_path_string) |
| 24 | + .c_naming(false) |
| 25 | + .blocklist_type("struct_IMAGE_TLS_DIRECTORY") |
| 26 | + .blocklist_type("struct_PIMAGE_TLS_DIRECTORY") |
| 27 | + .blocklist_type("struct_IMAGE_TLS_DIRECTORY64") |
| 28 | + .blocklist_type("struct_PIMAGE_TLS_DIRECTORY64") |
| 29 | + .blocklist_type("struct__IMAGE_TLS_DIRECTORY64") |
| 30 | + .blocklist_type("IMAGE_TLS_DIRECTORY") |
| 31 | + .blocklist_type("PIMAGE_TLS_DIRECTORY") |
| 32 | + .blocklist_type("IMAGE_TLS_DIRECTORY64") |
| 33 | + .blocklist_type("PIMAGE_TLS_DIRECTORY64") |
| 34 | + .blocklist_type("_IMAGE_TLS_DIRECTORY64") |
| 35 | + .allowlist_type("evt_.*") |
| 36 | + .allowlist_function("evt_.*") |
| 37 | + .allowlist_var("evt_.*") |
| 38 | + .allowlist_recursively(false) |
| 39 | + .layout_tests(false) |
| 40 | + .merge_extern_blocks(true) |
| 41 | + // Tell cargo to invalidate the built crate whenever any of the |
| 42 | + // included header files changed. |
| 43 | + // .wrap_static_fns(true) |
| 44 | + // Finish the builder and generate the bindings. |
| 45 | + .generate() |
| 46 | + // Unwrap the Result and panic on failure. |
| 47 | + .expect("Unable to generate bindings"); |
| 48 | + |
| 49 | + // // Write the bindings to the $OUT_DIR/bindings.rs file. |
| 50 | + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 51 | + bindings |
| 52 | + .write_to_file(out_path.join("bindings.rs")) |
| 53 | + .expect("Couldn't write bindings!"); |
| 54 | +} |
| 55 | + |
| 56 | +fn main() { |
| 57 | + // Tell cargo to look for shared libraries in the specified directory |
| 58 | + println!("cargo:rustc-link-search=../lib"); |
| 59 | + |
| 60 | + // TODO use clang crate instead of invoking CLI directly |
| 61 | + let header_out = Exec::cmd("clang") |
| 62 | + .arg("-E") |
| 63 | + .arg(PathBuf::from(PROJECT_ROOT).join("lib/include/public/mat.h")) |
| 64 | + .arg("-D") |
| 65 | + .arg("HAVE_DYNAMIC_C_LIB") |
| 66 | + .capture() |
| 67 | + .expect("Failed to open clang process") |
| 68 | + .stdout_str(); |
| 69 | + |
| 70 | + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 71 | + let mat_out_path = out_dir.join("mat.out.h"); |
| 72 | + |
| 73 | + fs::write(&mat_out_path, header_out).unwrap(); |
| 74 | + |
| 75 | + write_bindings(&mat_out_path); |
| 76 | +} |
0 commit comments