Avoid requiring protoc unless protos change

This commit is contained in:
Luke Street 2024-08-18 13:40:49 -06:00
parent cad9b70632
commit a733a950a3
2 changed files with 22 additions and 9 deletions

View File

@ -10,20 +10,33 @@ fn main() {
println!("cargo:rustc-rerun-if-changed=.git/HEAD"); println!("cargo:rustc-rerun-if-changed=.git/HEAD");
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos"); let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
let descriptor_path = root.join("proto_descriptor.bin");
println!("cargo:rerun-if-changed={}", descriptor_path.display());
let descriptor_mtime = std::fs::metadata(&descriptor_path)
.map(|m| m.modified().unwrap())
.unwrap_or(std::time::SystemTime::UNIX_EPOCH);
let mut run_protoc = false;
let proto_files = vec![root.join("report.proto")]; let proto_files = vec![root.join("report.proto")];
// Tell cargo to recompile if any of these proto files are changed
for proto_file in &proto_files { for proto_file in &proto_files {
println!("cargo:rerun-if-changed={}", proto_file.display()); println!("cargo:rerun-if-changed={}", proto_file.display());
let mtime = match std::fs::metadata(proto_file) {
Ok(m) => m.modified().unwrap(),
Err(e) => panic!("Failed to stat proto file {}: {:?}", proto_file.display(), e),
};
if mtime > descriptor_mtime {
run_protoc = true;
}
} }
let descriptor_path = let mut config = prost_build::Config::new();
PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("proto_descriptor.bin"); config.file_descriptor_set_path(&descriptor_path);
// If our cached descriptor is up-to-date, we don't need to run protoc.
prost_build::Config::new() // This is helpful so that users don't need to have protoc installed
.file_descriptor_set_path(&descriptor_path) // unless they're updating the protos.
.compile_protos(&proto_files, &[root]) if !run_protoc {
.expect("Failed to compile protos"); config.skip_protoc_run();
}
config.compile_protos(&proto_files, &[root]).expect("Failed to compile protos");
let descriptor_set = std::fs::read(descriptor_path).expect("Failed to read descriptor set"); let descriptor_set = std::fs::read(descriptor_path).expect("Failed to read descriptor set");
pbjson_build::Builder::new() pbjson_build::Builder::new()

Binary file not shown.