Skip to content

Commit a733a95

Browse files
committed
Avoid requiring protoc unless protos change
1 parent cad9b70 commit a733a95

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

objdiff-cli/build.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,33 @@ fn main() {
1010
println!("cargo:rustc-rerun-if-changed=.git/HEAD");
1111

1212
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
13+
let descriptor_path = root.join("proto_descriptor.bin");
14+
println!("cargo:rerun-if-changed={}", descriptor_path.display());
15+
let descriptor_mtime = std::fs::metadata(&descriptor_path)
16+
.map(|m| m.modified().unwrap())
17+
.unwrap_or(std::time::SystemTime::UNIX_EPOCH);
18+
let mut run_protoc = false;
1319
let proto_files = vec![root.join("report.proto")];
14-
15-
// Tell cargo to recompile if any of these proto files are changed
1620
for proto_file in &proto_files {
1721
println!("cargo:rerun-if-changed={}", proto_file.display());
22+
let mtime = match std::fs::metadata(proto_file) {
23+
Ok(m) => m.modified().unwrap(),
24+
Err(e) => panic!("Failed to stat proto file {}: {:?}", proto_file.display(), e),
25+
};
26+
if mtime > descriptor_mtime {
27+
run_protoc = true;
28+
}
1829
}
1930

20-
let descriptor_path =
21-
PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("proto_descriptor.bin");
22-
23-
prost_build::Config::new()
24-
.file_descriptor_set_path(&descriptor_path)
25-
.compile_protos(&proto_files, &[root])
26-
.expect("Failed to compile protos");
31+
let mut config = prost_build::Config::new();
32+
config.file_descriptor_set_path(&descriptor_path);
33+
// If our cached descriptor is up-to-date, we don't need to run protoc.
34+
// This is helpful so that users don't need to have protoc installed
35+
// unless they're updating the protos.
36+
if !run_protoc {
37+
config.skip_protoc_run();
38+
}
39+
config.compile_protos(&proto_files, &[root]).expect("Failed to compile protos");
2740

2841
let descriptor_set = std::fs::read(descriptor_path).expect("Failed to read descriptor set");
2942
pbjson_build::Builder::new()
5.84 KB
Binary file not shown.

0 commit comments

Comments
 (0)