Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
52 lines (49 loc) · 2.56 KB
/
Copy pathbuild.rs
File metadata and controls
52 lines (49 loc) · 2.56 KB
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
//! Embeds `lib/*.bas` and `devices/<part>.toml` in the compiler (see `src/embedded.rs`), so a
//! `basil` binary carries the modules and device descriptions it was built with.
use std::fmt::Write as _;
use std::path::Path;
/// Files of `dir` with extension `ext`, sorted by name; `skip` drops names ending in it.
fn files(dir: &str, ext: &str, skip: &str) -> Vec<String> {
let mut v: Vec<String> = std::fs::read_dir(dir)
.map(|rd| rd.flatten().filter_map(|e| e.file_name().into_string().ok()).collect())
.unwrap_or_default();
v.retain(|n| n.ends_with(ext) && (skip.is_empty() || !n.ends_with(skip)));
v.sort();
v
}
fn main() {
// PIC16 code generation for larger programs can exceed Windows' default
// 1 MiB main-thread stack, especially in debug builds. Reserve 8 MiB for
// the compiler executable (pages are committed only as they are used).
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
let arg = if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") {
"/STACK:8388608"
} else {
"-Wl,--stack,8388608"
};
println!("cargo:rustc-link-arg-bin=basil={arg}");
}
let root = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let mut out = String::new();
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for (konst, dir, ext, skip) in [("LIB", "lib", ".bas", ""), ("DEVICES", "devices", ".toml", ".expect.toml")] {
println!("cargo:rerun-if-changed={dir}");
writeln!(out, "pub const {konst}: &[(&str, &str)] = &[").unwrap();
for name in files(&format!("{root}/{dir}"), ext, skip) {
let path = format!("{root}/{dir}/{name}");
println!("cargo:rerun-if-changed={dir}/{name}");
// Each field is delimited (a zero byte after the directory and the name, the length before the content), so
// moving bytes from one file's name or content to the next cannot give the same hash.
let data = std::fs::read(&path).unwrap();
let fields = dir.bytes().chain([0]).chain(name.bytes()).chain([0]).chain((data.len() as u64).to_le_bytes());
for b in fields.chain(data) {
hash = (hash ^ u64::from(b)).wrapping_mul(0x100_0000_01b3);
}
writeln!(out, " ({name:?}, include_str!({path:?})),").unwrap();
}
out.push_str("];\n");
}
writeln!(out, "pub const HASH: &str = \"{hash:016x}\";").unwrap();
let dest = Path::new(&std::env::var("OUT_DIR").unwrap()).join("embedded.rs");
std::fs::write(dest, out).unwrap();
}