1 extern crate bindgen; 2 3 use std::path::PathBuf; 4 5 fn main() { 6 // Tell cargo to look for shared libraries in the specified directory 7 println!("cargo:rustc-link-search=src"); 8 println!("cargo:rerun-if-changed=src/include/bindings/wrapper.h"); 9 let binding_file_path = "src/include/bindings/bindings.rs"; 10 // The bindgen::Builder is the main entry point 11 // to bindgen, and lets you build up options for 12 // the resulting bindings. 13 { 14 let bindings = bindgen::Builder::default() 15 .clang_arg("-I./src") 16 // The input header we would like to generate 17 // bindings for. 18 .header("src/include/bindings/wrapper.h") 19 .clang_arg("--target=x86_64-none-none") 20 .clang_arg("-v") 21 // 使用core,并将c语言的类型改为core::ffi,而不是使用std库。 22 .use_core() 23 .ctypes_prefix("::core::ffi") 24 .generate_inline_functions(true) 25 // Tell cargo to invalidate the built crate whenever any of the 26 // included header files changed. 27 .parse_callbacks(Box::new(bindgen::CargoCallbacks)) 28 // Finish the builder and generate the bindings. 29 .generate() 30 // Unwrap the Result and panic on failure. 31 .expect("Unable to generate bindings"); 32 33 // Write the bindings to the $OUT_DIR/bindings.rs file. 34 let out_path = PathBuf::from(String::from(".")); 35 36 bindings 37 .write_to_file(out_path.join(binding_file_path)) 38 .expect("Couldn't write bindings!"); 39 } 40 } 41