build.rs 2.0 KB

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