build.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. extern crate cbindgen;
  2. extern crate cc;
  3. use std::{env, fs, fs::DirEntry, path::Path};
  4. // include src/header directories that don't start with '_'
  5. fn include_dir(d: &DirEntry) -> bool {
  6. d.metadata().map(|m| m.is_dir()).unwrap_or(false)
  7. && d.path()
  8. .iter()
  9. .nth(2)
  10. .map_or(false, |c| c.to_str().map_or(false, |x| !x.starts_with("_")))
  11. }
  12. // fn generate_bindings(cbindgen_config_path: &Path) {
  13. // let relative_path = cbindgen_config_path
  14. // .strip_prefix("src/header")
  15. // .ok()
  16. // .and_then(|p| p.parent())
  17. // .and_then(|p| p.to_str())
  18. // .unwrap()
  19. // .replace("_", "/");
  20. // let header_path = Path::new("target/include")
  21. // .join(&relative_path)
  22. // .with_extension("h");
  23. // let mod_path = cbindgen_config_path.with_file_name("mod.rs");
  24. // let config = cbindgen::Config::from_file(cbindgen_config_path).unwrap();
  25. // cbindgen::Builder::new()
  26. // .with_config(config)
  27. // .with_src(mod_path)
  28. // .generate()
  29. // .expect("Unable to generate bindings")
  30. // .write_to_file(header_path);
  31. // }
  32. fn main() {
  33. let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
  34. // Generate C includes
  35. // - based on contents of src/header/**
  36. // - headers written to target/include
  37. // fs::read_dir(&Path::new("src/header"))
  38. // .unwrap()
  39. // .into_iter()
  40. // .filter_map(Result::ok)
  41. // .filter(|d| include_dir(d))
  42. // .map(|d| d.path().as_path().join("cbindgen.toml"))
  43. // .filter(|p| p.exists())
  44. // .for_each(|p| {
  45. // println!("cargo:rerun-if-changed={:?}", p.parent().unwrap());
  46. // println!("cargo:rerun-if-changed={:?}", p);
  47. // println!("cargo:rerun-if-changed={:?}", p.with_file_name("mod.rs"));
  48. // generate_bindings(&p);
  49. // });
  50. // // 指定链接搜索路径
  51. // println!("cargo:rustc-link-search=native=./dlibc");
  52. // // 链接 dlibc 库
  53. // println!("cargo:rustc-link-lib=dlibc");
  54. }