build.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. use std::{
  2. env,
  3. path::PathBuf,
  4. process::Command,
  5. };
  6. fn main() {
  7. let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
  8. // Print OUT_DIR for debugging build issues.
  9. println!("OUT_DIR={}", out_dir.display());
  10. // We'll use git to pull in protobuf dependencies. This trick lets us use the Rust OUT_DIR
  11. // directory as a mini-repo with wormhole and googleapis as remotes, so we can copy out the
  12. // TREEISH paths we want.
  13. let protobuf_setup = r#"
  14. set -e
  15. git init .
  16. git clean -df
  17. git remote add wormhole https://github.com/wormhole-foundation/wormhole.git || true
  18. git remote add googleapis https://github.com/googleapis/googleapis.git || true
  19. git fetch --depth=1 wormhole main
  20. git fetch --depth=1 googleapis master
  21. git reset
  22. rm -rf proto/
  23. git read-tree --prefix=proto/ -u wormhole/main:proto
  24. git read-tree --prefix=proto/google/api/ -u googleapis/master:google/api
  25. "#;
  26. // Run each command to prepare the OUT_DIR with the protobuf definitions. We need to make sure
  27. // to change the working directory to OUT_DIR, otherwise git will complain.
  28. let output = Command::new("sh")
  29. .args(["-c", protobuf_setup])
  30. .current_dir(&out_dir)
  31. .output()
  32. .expect("failed to run protobuf setup commands");
  33. if !output.status.success() {
  34. panic!(
  35. "failed to setup protobuf definitions: {}",
  36. String::from_utf8_lossy(&output.stderr)
  37. );
  38. }
  39. // We build the resulting protobuf definitions using Rust's prost_build crate, which generates
  40. // Rust code from the protobuf definitions.
  41. tonic_build::configure()
  42. .build_server(false)
  43. .compile(
  44. &[
  45. out_dir.join("proto/spy/v1/spy.proto"),
  46. out_dir.join("proto/gossip/v1/gossip.proto"),
  47. out_dir.join("proto/node/v1/node.proto"),
  48. out_dir.join("proto/publicrpc/v1/publicrpc.proto"),
  49. ],
  50. &[out_dir.join("proto")],
  51. )
  52. .expect("failed to compile protobuf definitions");
  53. }