build.rs 1021 B

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