1- use std:: path:: Path ;
1+ use std:: {
2+ hash:: Hasher ,
3+ path:: { Path , PathBuf } ,
4+ } ;
25
36const VERSION_TEMPLATE : & ' static str = r#"
47pub const VERSION: &'static str = "{version}";
@@ -16,15 +19,34 @@ fn main() {
1619
1720 println ! ( "cargo:rustc-link-arg-bins=-Tlinkall.x" ) ;
1821
19- let version_str = if let Ok ( rel) = std:: env:: var ( "RELEASE_BUILD" ) {
22+ let mut hasher = crc32fast:: Hasher :: new ( ) ;
23+ crc_walkdir ( PathBuf :: from ( "src" ) , & mut hasher) ;
24+ let src_crc = hasher. finalize ( ) ;
25+
26+ let mut version_str = if let Ok ( rel) = std:: env:: var ( "RELEASE_BUILD" ) {
2027 rel
2128 } else {
2229 let epoch = std:: time:: SystemTime :: now ( )
2330 . duration_since ( std:: time:: UNIX_EPOCH )
2431 . unwrap ( )
2532 . as_secs ( ) ;
2633
27- format ! ( "D{epoch}" )
34+ if let Ok ( crc_string) = std:: fs:: read_to_string ( std:: env:: temp_dir ( ) . join ( "fkm-build-crc" ) )
35+ {
36+ let split = crc_string. split_once ( '|' ) ;
37+ if let Some ( ( crc_str, ver) ) = split {
38+ let crc: u32 = crc_str. parse ( ) . unwrap_or ( 0 ) ;
39+ if crc == src_crc {
40+ ver. to_string ( )
41+ } else {
42+ format ! ( "D{epoch}" )
43+ }
44+ } else {
45+ format ! ( "D{epoch}" )
46+ }
47+ } else {
48+ format ! ( "D{epoch}" )
49+ }
2850 } ;
2951
3052 let chip = if cfg ! ( feature = "esp32" ) {
@@ -41,4 +63,22 @@ fn main() {
4163 . replace ( "{firmware}" , "STATION" ) ;
4264
4365 std:: fs:: write ( Path :: new ( "src" ) . join ( "version.rs" ) , gen. trim ( ) ) . unwrap ( ) ;
66+ _ = std:: fs:: write (
67+ std:: env:: temp_dir ( ) . join ( "fkm-build-crc" ) ,
68+ format ! ( "{src_crc}|{version_str}" ) ,
69+ ) ;
70+ }
71+
72+ fn crc_walkdir ( path : PathBuf , hasher : & mut crc32fast:: Hasher ) {
73+ if let Ok ( mut dir) = path. read_dir ( ) {
74+ while let Some ( Ok ( entry) ) = dir. next ( ) {
75+ let file_type = entry. file_type ( ) . unwrap ( ) ;
76+ if file_type. is_dir ( ) {
77+ crc_walkdir ( entry. path ( ) , hasher) ;
78+ } else if file_type. is_file ( ) && entry. file_name ( ) . to_string_lossy ( ) != "version.rs" {
79+ let string = std:: fs:: read_to_string ( entry. path ( ) ) . unwrap ( ) ;
80+ hasher. write ( string. as_bytes ( ) ) ;
81+ }
82+ }
83+ }
4484}
0 commit comments