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