build.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use std::env;
  2. use std::process::{Command, Stdio};
  3. use std::path::{Path,PathBuf};
  4. #[derive(Debug)]
  5. enum ProtobufError {
  6. IoError(::std::io::Error),
  7. Other
  8. }
  9. impl std::convert::From<::std::io::Error> for ProtobufError {
  10. fn from(e: ::std::io::Error) -> ProtobufError {
  11. ProtobufError::IoError(e)
  12. }
  13. }
  14. fn compile(prefix : &Path, files : &[&Path]) -> Result<(),ProtobufError>{
  15. let mut c = Command::new("protoc");
  16. c.arg("--rust_out").arg(env::var("OUT_DIR").unwrap())
  17. .arg("--proto_path").arg(prefix.to_str().unwrap());
  18. for f in files.iter() {
  19. c.arg(f.to_str().unwrap());
  20. }
  21. //c.stdout(Stdio::inherit());
  22. c.stderr(Stdio::inherit());
  23. let mut p = try!(c.spawn());
  24. let r = try!(p.wait());
  25. return match r.success() {
  26. true => Ok(()),
  27. false => Err(ProtobufError::Other),
  28. };
  29. }
  30. fn main() {
  31. let root = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap());
  32. let proto = root.join("proto");
  33. compile(&proto, &[
  34. &proto.join("keyexchange.proto"),
  35. &proto.join("authentication.proto"),
  36. &proto.join("mercury.proto"),
  37. &proto.join("metadata.proto"),
  38. &proto.join("pubsub.proto"),
  39. &proto.join("spirc.proto"),
  40. ]).unwrap();
  41. }