Assert protoc version while building with cargo

This commit is contained in:
Ding Xiang Fei
2018-05-23 18:25:48 +08:00
committed by Romain Ruetschi
parent 622a8d7256
commit 8561d21db2
2 changed files with 31 additions and 5 deletions

View File

@ -24,13 +24,13 @@ serde = { version = "^1.0.55", optional = true }
serde_derive = { version = "^1.0.55", optional = true }
[build-dependencies]
protoc-rust = "1.7.1"
protoc-rust = { version = "1.7.1", optional = true }
[dev-dependencies]
serde_json = "1.0.17"
[features]
serialization-protobuf = [ "protobuf" ]
serialization-protobuf = [ "protobuf", "protoc-rust" ]
serialization-serde = [ "serde", "serde_derive" ]
[package.metadata.release]

View File

@ -1,6 +1,26 @@
#[cfg(feature = "serialization-protobuf")]
extern crate protoc_rust;
fn build_protobuf<'a>(out_dir: &'a str, input: &'a [&'a str], includes: &'a [&'a str]) {
#[cfg(feature = "serialization-protobuf")]
fn assert_protobuf_version(version: &str) {
use std::process::{Command, Stdio};
let protoc = Command::new("protoc")
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.args(&["--version"])
.spawn()
.unwrap();
let version_output = protoc.wait_with_output().unwrap();
assert!(version_output.status.success());
assert_eq!(
String::from_utf8(version_output.stdout).unwrap().trim(),
version
);
}
#[cfg(feature = "serialization-protobuf")]
fn build_protobuf(out_dir: &str, input: &[&str], includes: &[&str]) {
use self::protoc_rust::{run, Args};
run(Args {
out_dir,
@ -9,7 +29,13 @@ fn build_protobuf<'a>(out_dir: &'a str, input: &'a [&'a str], includes: &'a [&'a
}).expect("protoc");
}
fn main() {
#[cfg(feature = "serialization-protobuf")]
#[cfg(feature = "serialization-protobuf")]
fn build_protobuf_schemata() {
assert_protobuf_version("libprotoc 3.5.1");
build_protobuf("src/proto", &["protobuf/proof.proto"], &[]);
}
fn main() {
#[cfg(feature = "serialization-protobuf")]
build_protobuf_schemata();
}