forked from redox-os/kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
120 lines (100 loc) · 3.51 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::env;
use std::fs;
use std::io::{Error, Write};
use std::path::Path;
use std::collections::HashMap;
// View loc folder with subfolders, get listings
// Returns touple (folder_map, file_list)
// folder_map keys are folders, and values are lists of direct childs
// file_list is a vector of all detected files with full path
fn scan_folder(loc: &Path) -> (HashMap<String, Vec<String>>, Vec<String>) {
let mut folders: HashMap<String, Vec<String>> = HashMap::new();
let mut files: Vec<String> = Vec::new();
let mut current = Vec::new();
if loc.is_dir() {
for entry in fs::read_dir(loc).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
let path_str = String::from(path.to_str().unwrap()).replace("\\", "/");
current.push(path_str.clone());
// if folder then scan recursively
if path.is_dir() {
let (d, mut f) = scan_folder(&path);
for (key, value) in d.into_iter() {
folders.insert(key, value);
}
files.append(&mut f);
} else {
files.push(path_str);
}
}
current.sort();
folders.entry(String::from(loc.to_str().unwrap()).replace("\\", "/")).or_insert(current);
} else {
panic!("{:?} is not a folder!", loc);
}
(folders, files)
}
// Write folder/file information to output file
fn fill_from_location(f: &mut fs::File, loc: &Path ) -> Result<(), (Error)> {
let (folders, mut files) = scan_folder(loc);
let mut folder_it:Vec<_> = folders.keys().collect();
let loc_str = loc.to_str().unwrap();
let mut idx = loc_str.len();
if !loc_str.ends_with("/") {
idx += 1;
}
folder_it.sort();
files.sort();
for dir in folder_it.iter() {
let strip: String = dir.chars().skip(idx).collect();
write!(f, " files.insert(b\"{}\", (b\"", strip)?;
// Write child elements separated with \n
let sub = folders.get(*dir).unwrap();
let mut first = true;
for child in sub.iter() {
let idx = child.rfind('/').unwrap() + 1;
let (_, c) = child.split_at(idx);
if first {
write!(f, "{}", c)?;
first = false;
} else {
write!(f, "\\n{}", c)?;
}
}
write!(f, "\", true));\n")?;
}
for name in files.iter() {
let (_, strip) = name.split_at(idx);
write!(f, " files.insert(b\"{}\", (include_bytes!(\"{}\"), false));\n", strip, name)?;
}
Ok(())
}
fn main() {
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("gen.rs");
let mut f = fs::File::create(&dest_path).unwrap();
let src = env::var("INITFS_FOLDER");
// Write header
f.write_all(b"
mod gen {
use alloc::BTreeMap;
pub fn gen() -> BTreeMap<&'static [u8], (&'static [u8], bool)> {
let mut files: BTreeMap<&'static [u8], (&'static [u8], bool)> = BTreeMap::new();
").unwrap();
match src {
Ok(v) => fill_from_location(&mut f, Path::new(&v)).unwrap(),
Err(e) => {
f.write_all(
b" files.clear();" // Silence mutability warning
).unwrap();
println!("cargo:warning=location not found: {}, please set proper INITFS_FOLDER.", e);
}
}
f.write_all(b"
files
}
}
").unwrap();
}