build.rs 1009 B

12345678910111213141516171819202122232425262728293031323334353637
  1. extern crate cbindgen;
  2. use std::env;
  3. use std::path::PathBuf;
  4. use cbindgen::{Config, Language};
  5. fn main() {
  6. let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
  7. let package_name = env::var("CARGO_PKG_NAME").unwrap();
  8. let output_file = target_dir()
  9. .join(format!("{}.h", package_name))
  10. .display()
  11. .to_string();
  12. let config = Config {
  13. language: Language::C,
  14. cpp_compat: true,
  15. namespace: Some(String::from("zeroidc")),
  16. ..Default::default()
  17. };
  18. cbindgen::generate_with_config(&crate_dir, config)
  19. .unwrap()
  20. .write_to_file(&output_file);
  21. }
  22. /// Find the location of the `target/` directory. Note that this may be
  23. /// overridden by `cmake`, so we also need to check the `CARGO_TARGET_DIR`
  24. /// variable.
  25. fn target_dir() -> PathBuf {
  26. if let Ok(target) = env::var("CARGO_TARGET_DIR") {
  27. PathBuf::from(target)
  28. } else {
  29. PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target")
  30. }
  31. }