build.rs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use std::env;
  2. use std::process::{Command, Stdio};
  3. use std::path::Path;
  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 prefix = Path::new("protocol");
  32. compile(&prefix, &[
  33. &prefix.join("keyexchange.proto"),
  34. &prefix.join("authentication.proto")
  35. ]).unwrap();
  36. }