@@ -10,20 +10,33 @@ fn main() {
10
10
println ! ( "cargo:rustc-rerun-if-changed=.git/HEAD" ) ;
11
11
12
12
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 ;
13
19
let proto_files = vec ! [ root. join( "report.proto" ) ] ;
14
-
15
- // Tell cargo to recompile if any of these proto files are changed
16
20
for proto_file in & proto_files {
17
21
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
+ }
18
29
}
19
30
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" ) ;
27
40
28
41
let descriptor_set = std:: fs:: read ( descriptor_path) . expect ( "Failed to read descriptor set" ) ;
29
42
pbjson_build:: Builder :: new ( )
0 commit comments