build.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. extern crate protobuf_build;
  2. use std::env;
  3. use std::path::PathBuf;
  4. use std::fs::File;
  5. use std::io::{Read, Write};
  6. fn main() {
  7. let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
  8. let out = PathBuf::from(env::var("OUT_DIR").unwrap());
  9. let proto = root.join("proto");
  10. let mut compiler = protobuf_build::Compiler::new(&proto, &out);
  11. let files = ["keyexchange",
  12. "authentication",
  13. "mercury",
  14. "metadata",
  15. "pubsub",
  16. "spirc"];
  17. for file in &files {
  18. compiler.compile(&((*file).to_owned() + ".proto")).unwrap();
  19. // Hack for rust-lang/rust#18810
  20. // Wrap the generated rust files with "pub mod { ... }", so they
  21. // can be included.
  22. let path = out.join(&((*file).to_owned() + ".rs"));
  23. let contents = {
  24. let mut src = File::open(path).unwrap();
  25. let mut contents = Vec::new();
  26. src.read_to_end(&mut contents).unwrap();
  27. contents
  28. };
  29. let mut dst = File::create(out.join(&((*file).to_owned() + ".rs"))).unwrap();
  30. dst.write_all(format!("pub mod {} {{\n", file).as_bytes()).unwrap();
  31. dst.write_all(&contents).unwrap();
  32. dst.write_all("}".as_bytes()).unwrap();
  33. }
  34. }