Skip to content

Commit d589371

Browse files
committed
feat: only generate new version if source files are changed
1 parent 93fb001 commit d589371

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ opt-level = 3
6363
overflow-checks = false
6464

6565
[build-dependencies]
66+
crc32fast = "1.4.2"
6667
dotenvy = "0.15.7"

build.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::path::Path;
1+
use std::{
2+
hash::Hasher,
3+
path::{Path, PathBuf},
4+
};
25

36
const VERSION_TEMPLATE: &'static str = r#"
47
pub 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
}

dev-esp32c3.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
source ~/export-esp.sh
3+
cargo build --no-default-features --features=esp32c3 --target=riscv32imc-unknown-none-elf -r && espflash save-image --chip esp32c3 ./target/riscv32imc-unknown-none-elf/release/fkm-firmware "/tmp/fkm-build/esp32c3_STATION_$(cat ./src/version.rs | grep VERSION | cut -d'"' -f 2).bin"

0 commit comments

Comments
 (0)