-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
46 lines (42 loc) · 1.61 KB
/
Copy pathbuild.rs
File metadata and controls
46 lines (42 loc) · 1.61 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
use std::fs;
use std::path::PathBuf;
fn main() {
// Ensure a bundle exists for include_bytes! in wasm builds.
// The bundle format is the same single 64K image produced by `chipcade build`.
// Priority:
// 1) use CHIPCADE_BUNDLE if set
// 2) use crate-root build/program.bin if it exists
// 3) fallback: create a placeholder image with header at 0xF000
let bundle_env = std::env::var("CHIPCADE_BUNDLE").ok();
let src_candidates: Vec<PathBuf> = bundle_env
.into_iter()
.map(PathBuf::from)
.chain(std::iter::once(PathBuf::from("build/program.bin")).filter(|p| p.exists()))
.collect();
let build_dir = PathBuf::from("build");
let dest_bundle = build_dir.join("program.bin");
let _ = fs::create_dir_all(&build_dir);
if let Some(src) = src_candidates.into_iter().find(|p| p.exists()) {
if let Err(e) = fs::copy(&src, &dest_bundle) {
eprintln!(
"build.rs: failed to copy bundle from {} to {}: {e}",
src.display(),
dest_bundle.display()
);
}
return;
}
// Placeholder: flat 64K image with empty header at 0xF000.
let mut image = vec![0u8; 0x10000];
let meta_addr = 0xF000;
if meta_addr + 8 <= image.len() {
image[meta_addr..meta_addr + 4].copy_from_slice(b"CHPC");
image[meta_addr + 4..meta_addr + 8].copy_from_slice(&(0u32).to_le_bytes());
}
if let Err(e) = fs::write(&dest_bundle, &image) {
eprintln!(
"build.rs: failed to write placeholder bundle to {}: {e}",
dest_bundle.display()
);
}
}