librespot/protocol/build.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

extern crate protobuf_build;
use std::env;
use std::path::PathBuf;
use std::fs::File;
use std::io::{Read, Write};
fn main() {
let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let proto = root.join("proto");
2015-06-23 14:38:29 +00:00
let mut compiler = protobuf_build::Compiler::new(&proto, &out);
let files = ["keyexchange",
"authentication",
"mercury",
"metadata",
"pubsub",
"spirc"];
for file in &files {
compiler.compile(&((*file).to_owned() + ".proto")).unwrap();
// Hack for rust-lang/rust#18810
// Wrap the generated rust files with "pub mod { ... }", so they
// can be included.
let path = out.join(&((*file).to_owned() + ".rs"));
let contents = {
let mut src = File::open(path).unwrap();
let mut contents = Vec::new();
src.read_to_end(&mut contents).unwrap();
contents
};
let mut dst = File::create(out.join(&((*file).to_owned() + ".rs"))).unwrap();
dst.write_all(format!("pub mod {} {{\n", file).as_bytes()).unwrap();
dst.write_all(&contents).unwrap();
dst.write_all("}".as_bytes()).unwrap();
}
}