build.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. let out_var = env::var("OUT_DIR").unwrap();
  9. // Clone the Wormhole repository, which we need to access the protobuf definitions for Wormhole
  10. // P2P message types.
  11. //
  12. // TODO: This is ugly and costly, and requires git. Instead of this we should have our own tool
  13. // build process that can generate protobuf definitions for this and other user cases. For now
  14. // this is easy and works and matches upstream Wormhole's `Makefile`.
  15. let _ = Command::new("git")
  16. .args([
  17. "clone",
  18. "https://github.com/wormhole-foundation/wormhole",
  19. "wormhole",
  20. ])
  21. .output()
  22. .expect("failed to execute process");
  23. // Move the tools directory to the root of the repo because that's where the build script
  24. // expects it to be, paths get hardcoded into the binaries.
  25. let _ = Command::new("mv")
  26. .args(["wormhole/tools", "tools"])
  27. .output()
  28. .expect("failed to execute process");
  29. // Move the protobuf definitions to the src/network directory, we don't have to do this
  30. // but it is more intuitive when debugging.
  31. let _ = Command::new("mv")
  32. .args([
  33. "wormhole/proto/gossip/v1/gossip.proto",
  34. "src/network/p2p.proto",
  35. ])
  36. .output()
  37. .expect("failed to execute process");
  38. // Build the protobuf compiler.
  39. let _ = Command::new("./build.sh")
  40. .current_dir("tools")
  41. .output()
  42. .expect("failed to execute process");
  43. // Make the protobuf compiler executable.
  44. let _ = Command::new("chmod")
  45. .args(["+x", "tools/bin/*"])
  46. .output()
  47. .expect("failed to execute process");
  48. // Generate the protobuf definitions. See buf.gen.yaml to see how we rename the module for our
  49. // particular use case.
  50. let _ = Command::new("./tools/bin/buf")
  51. .args(["generate", "--path", "src"])
  52. .output()
  53. .expect("failed to execute process");
  54. // Build the Go library.
  55. let mut cmd = Command::new("go");
  56. cmd.arg("build")
  57. .arg("-buildmode=c-archive")
  58. .arg("-o")
  59. .arg(out_dir.join("libpythnet.a"))
  60. .arg("src/network/p2p.go")
  61. .arg("src/network/p2p.pb.go");
  62. // Tell Rust to link our Go library at compile time.
  63. println!("cargo:rustc-link-search=native={out_var}");
  64. println!("cargo:rustc-link-lib=static=pythnet");
  65. #[cfg(target_arch = "aarch64")]
  66. println!("cargo:rustc-link-lib=resolv");
  67. let status = cmd.status().unwrap();
  68. assert!(status.success());
  69. }