1 extern crate bindgen;
2 
3 extern crate cbindgen;
4 
5 // use ::std::env;
6 
7 use std::path::PathBuf;
8 
main()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             .clang_arg("-I./src/include")
24             .clang_arg("-I./src/arch/x86_64/include")   // todo: 当引入多种架构之后,需要修改这里,对于不同的架构编译时,include不同的路径
25             // The input header we would like to generate
26             // bindings for.
27             .header("src/include/bindings/wrapper.h")
28             .blocklist_file("src/include/bindings/bindings.h")
29             .clang_arg("--target=x86_64-none-none")
30             .clang_arg("-v")
31             // 使用core,并将c语言的类型改为core::ffi,而不是使用std库。
32             .use_core()
33             .ctypes_prefix("::core::ffi")
34             .generate_inline_functions(true)
35             .raw_line("#![allow(dead_code)]")
36             .raw_line("#![allow(non_upper_case_globals)]")
37             .raw_line("#![allow(non_camel_case_types)]")
38             // Tell cargo to invalidate the built crate whenever any of the
39             // included header files changed.
40             .parse_callbacks(Box::new(bindgen::CargoCallbacks))
41             // Finish the builder and generate the bindings.
42             .generate()
43             // Unwrap the Result and panic on failure.
44             .expect("Unable to generate bindings");
45 
46         bindings
47             .write_to_file(out_path.join("bindings.rs"))
48             .expect("Couldn't write bindings!");
49     }
50 }
51