-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
85 lines (75 loc) · 2.53 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::{
hash::Hasher,
path::{Path, PathBuf},
};
const VERSION_TEMPLATE: &str = r#"
pub const VERSION: &str = "{version}";
pub const CHIP: &str = "{chip}";
pub const FIRMWARE: &str = "{firmware}";
"#;
fn main() {
println!("cargo:rerun-if-changed=*.env*");
if let Ok(mut iter) = dotenvy::dotenv_iter() {
while let Some(Ok((key, value))) = iter.next() {
println!("cargo:rustc-env={key}={value}");
}
}
println!("cargo:rustc-link-arg-bins=-Tlinkall.x");
println!("cargo:rustc-cfg=feature=\"gen_version\"");
let mut hasher = crc32fast::Hasher::new();
crc_walkdir(PathBuf::from("src"), &mut hasher);
let src_crc = hasher.finalize();
let version_str = if let Ok(rel) = std::env::var("RELEASE_BUILD") {
rel
} else {
let epoch = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
if let Ok(crc_string) = std::fs::read_to_string(std::env::temp_dir().join("fkm-build-crc"))
{
let split = crc_string.split_once('|');
if let Some((crc_str, ver)) = split {
let crc: u32 = crc_str.parse().unwrap_or(0);
if crc == src_crc {
ver.to_string()
} else {
format!("D{epoch}")
}
} else {
format!("D{epoch}")
}
} else {
format!("D{epoch}")
}
};
let chip = if cfg!(feature = "esp32") {
"esp32"
} else if cfg!(feature = "esp32c3") {
"esp32c3"
} else {
"unknown"
};
let gen = VERSION_TEMPLATE
.replace("{version}", &version_str)
.replace("{chip}", chip)
.replace("{firmware}", "STATION");
std::fs::write(Path::new("src").join("version.rs"), gen.trim()).unwrap();
_ = std::fs::write(
std::env::temp_dir().join("fkm-build-crc"),
format!("{src_crc}|{version_str}"),
);
}
fn crc_walkdir(path: PathBuf, hasher: &mut crc32fast::Hasher) {
if let Ok(mut dir) = path.read_dir() {
while let Some(Ok(entry)) = dir.next() {
let file_type = entry.file_type().unwrap();
if file_type.is_dir() {
crc_walkdir(entry.path(), hasher);
} else if file_type.is_file() && entry.file_name().to_string_lossy() != "version.rs" {
let string = std::fs::read_to_string(entry.path()).unwrap();
hasher.write(string.as_bytes());
}
}
}
}