build.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. extern crate cbindgen;
  2. use cbindgen::{Config, Language, MacroExpansionConfig};
  3. use std::env;
  4. use std::path::PathBuf;
  5. fn main() {
  6. #[cfg(feature = "ztcontroller")]
  7. {
  8. let mut prost_build = prost_build::Config::new();
  9. prost_build
  10. .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
  11. .compile_protos(
  12. &[
  13. "src/pubsub/network.proto",
  14. "src/pubsub/member.proto",
  15. "src/pubsub/member_status.proto",
  16. ],
  17. &["src/pubsub/"],
  18. )
  19. .expect("Failed to compile protobuf files");
  20. }
  21. let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
  22. let package_name = env::var("CARGO_PKG_NAME").unwrap();
  23. let output_file = target_dir().join(format!("{package_name}.h")).display().to_string();
  24. let meconfig = MacroExpansionConfig { bitflags: true, ..Default::default() };
  25. let config = Config {
  26. language: Language::C,
  27. cpp_compat: true,
  28. namespace: Some(String::from("rustybits")),
  29. macro_expansion: meconfig,
  30. ..Default::default()
  31. };
  32. cbindgen::generate_with_config(&crate_dir, config)
  33. .unwrap()
  34. .write_to_file(&output_file);
  35. }
  36. /// Find the location of the `target/` directory. Note that this may be
  37. /// overridden by `cmake`, so we also need to check the `CARGO_TARGET_DIR`
  38. /// variable.
  39. fn target_dir() -> PathBuf {
  40. if let Ok(target) = env::var("CARGO_TARGET_DIR") {
  41. PathBuf::from(target)
  42. } else {
  43. PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target")
  44. }
  45. }