build.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use std::env;
  2. use std::path::PathBuf;
  3. use walkdir::WalkDir;
  4. fn compile_libunwind() {
  5. let mut cfg = cc::Build::new();
  6. cfg.warnings(false);
  7. cfg.cpp_set_stdlib(None);
  8. cfg.cpp(true);
  9. cfg.flag("-std=c++11");
  10. cfg.flag("-fno-stack-protector");
  11. cfg.flag("-fno-exceptions");
  12. cfg.flag("-fno-rtti");
  13. cfg.flag("-fstrict-aliasing");
  14. cfg.flag("-fvisibility=hidden");
  15. cfg.flag("-funwind-tables");
  16. cfg.flag("-mpreferred-stack-boundary=3");
  17. cfg.define("_LIBUNWIND_NO_HEAP", None);
  18. cfg.define("_LIBUNWIND_IS_BAREMETAL", None);
  19. cfg.define("_LIBUNWIND_IS_NATIVE_ONLY", None);
  20. cfg.define("_LIBUNWIND_HAS_NO_THREADS", None);
  21. cfg.define("NDEBUG", None);
  22. cfg.include("llvm-libunwind/include");
  23. cfg.include("include");
  24. let libunwind_sources = [
  25. "llvm-libunwind/src/UnwindRegistersRestore.S",
  26. "llvm-libunwind/src/UnwindRegistersSave.S",
  27. "llvm-libunwind/src/libunwind.cpp",
  28. // Needed on ARM targets for EHABI unwinding
  29. "llvm-libunwind/src/Unwind-EHABI.cpp",
  30. ];
  31. for source in &libunwind_sources {
  32. cfg.file(source);
  33. }
  34. cfg.compile("llvm_libunwind");
  35. }
  36. fn gen_libunwind_bindings() {
  37. let args = vec![
  38. "-nostdlibinc".to_string(),
  39. "-ffreestanding".to_string(),
  40. "-I".to_string(),
  41. "llvm-libunwind/include".to_string(),
  42. "-D_LIBUNWIND_IS_NATIVE_ONLY".to_string(),
  43. ];
  44. let bindings = bindgen::Builder::default()
  45. .header("llvm-libunwind/include/libunwind.h")
  46. .use_core()
  47. .ctypes_prefix("::cty")
  48. .prepend_enum_name(false)
  49. .parse_callbacks(Box::new(bindgen::CargoCallbacks))
  50. .clang_args(args)
  51. .generate()
  52. .expect("Unable to generate bindings");
  53. let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
  54. bindings
  55. .write_to_file(out_path.join("bindings.rs"))
  56. .expect("Couldn't write bindings!");
  57. }
  58. fn main() {
  59. compile_libunwind();
  60. gen_libunwind_bindings();
  61. for entry in WalkDir::new("llvm-libunwind")
  62. .into_iter()
  63. .chain(WalkDir::new("include").into_iter())
  64. {
  65. println!(
  66. "cargo:rerun-if-changed={}",
  67. entry.unwrap().path().to_str().unwrap()
  68. );
  69. }
  70. }