1 extern crate bindgen; 2 // use ::std::env; 3 4 use std::path::PathBuf; 5 6 fn main() { 7 // Tell cargo to look for shared libraries in the specified directory 8 println!("cargo:rustc-link-search=src"); 9 println!("cargo:rerun-if-changed=src/include/bindings/wrapper.h"); 10 11 // let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 12 let out_path = PathBuf::from(String::from("src/include/bindings/")); 13 14 // The bindgen::Builder is the main entry point 15 // to bindgen, and lets you build up options for 16 // the resulting bindings. 17 { 18 let bindings = bindgen::Builder::default() 19 .clang_arg("-I./src") 20 .clang_arg("-I./src/include") 21 .clang_arg("-I./src/arch/x86_64/include") // todo: 当引入多种架构之后,需要修改这里,对于不同的架构编译时,include不同的路径 22 // The input header we would like to generate 23 // bindings for. 24 .header("src/include/bindings/wrapper.h") 25 .blocklist_file("src/include/bindings/bindings.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 .raw_line("#![allow(dead_code)]") 33 .raw_line("#![allow(non_upper_case_globals)]") 34 .raw_line("#![allow(non_camel_case_types)]") 35 // Tell cargo to invalidate the built crate whenever any of the 36 // included header files changed. 37 .parse_callbacks(Box::new(bindgen::CargoCallbacks)) 38 // Finish the builder and generate the bindings. 39 .generate() 40 // Unwrap the Result and panic on failure. 41 .expect("Unable to generate bindings"); 42 43 bindings 44 .write_to_file(out_path.join("bindings.rs")) 45 .expect("Couldn't write bindings!"); 46 } 47 } 48