-
Notifications
You must be signed in to change notification settings - Fork 0
/
acorn_build.rs
331 lines (299 loc) · 8.85 KB
/
acorn_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#![feature(vec_into_raw_parts)]
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
mod path {
use super::*;
pub fn create_paths() {
assert!(
Command::new("rm")
.arg("-rf")
.arg("isoroot")
.status()
.expect("failed to run command")
.success(),
"failed to remove {}",
"isoroot"
);
fs::create_dir_all("isoroot").expect("failed to create isoroot directory");
fs::create_dir_all("build").expect("failed to create build directory");
}
pub fn build() -> PathBuf {
PathBuf::from("build")
.canonicalize()
.expect("failed to canonicalize build path")
}
pub fn isoroot() -> PathBuf {
PathBuf::from("isoroot")
.canonicalize()
.expect("failed to canonicalize isoroot path")
}
}
fn cargo() -> Command {
Command::new("cargo")
}
fn tool(name: &str) -> Command {
Command::new(format!("{}/tools/{name}", path::build().display()))
}
mod thirdparty {
macro_rules! thirdparty_str {
($name:literal) => {
concat!("thirdparty/", $name)
};
}
pub mod limine {
macro_rules! limine_str {
($name:literal) => {
concat!(thirdparty_str!("limine/"), $name)
};
}
pub const LIMINE_CFG: &str = limine_str!("limine.cfg");
pub const LIMINE_SYS: &str = limine_str!("limine.sys");
}
}
struct Builder {
path: PathBuf,
out_path: Option<PathBuf>,
unstable: bool,
}
struct BuildOutput {}
impl Builder {
fn set_from_flags(&self, cmd: &mut Command) {
if self.unstable {
cmd.args(["-Z", "unstable-options"]);
}
if let Some(out_path) = self.out_path.as_ref() {
cmd.arg("--out-dir").arg(out_path);
}
}
fn build(&self) -> BuildOutput {
assert!(
{
let mut cmd = cargo();
cmd.arg("build").current_dir(&self.path);
self.set_from_flags(&mut cmd);
cmd.status().expect("failed to build kernel").success()
},
"failed to build '{}': failed status",
self.path.display()
);
BuildOutput {}
}
}
struct Projects {
kernel_elf: PathBuf,
usrspc_modules: Vec<PathBuf>,
}
impl Projects {
pub fn build() -> Self {
let kernel_out_path = path::build();
Builder {
path: PathBuf::from("kernel"),
unstable: true,
out_path: Some(kernel_out_path.clone()),
}
.build();
let tools_out_path: PathBuf = format!("{}/tools", path::build().display()).into();
Builder {
path: PathBuf::from("tools"),
unstable: true,
out_path: Some(tools_out_path),
}
.build();
let usrspc_out_path: PathBuf = format!("{}/usrspc", path::build().display()).into();
Builder {
path: PathBuf::from("usrspc"),
unstable: true,
out_path: Some(usrspc_out_path),
}
.build();
Self {
kernel_elf: format!("{}/kernel", kernel_out_path.display()).into(),
usrspc_modules: vec![path::build().join("usrspc/ps2")],
}
}
}
struct ISORoot;
impl ISORoot {
const USRSPC_PATH: &str = "usrspc";
const BOOT_PATH: &str = "boot";
fn boot_path(path: &str) -> String {
format!("{}/{path}", Self::BOOT_PATH)
}
fn create(
kernel_elf: &Path,
usrspc_modules: &Vec<PathBuf>,
limine_cfg: &Path,
limine_sys: &Path,
) -> Self {
let isoroot = Self;
isoroot.mkdir(Self::BOOT_PATH);
isoroot.mkdir(Self::USRSPC_PATH);
isoroot.put(Self::boot_path("acorn.elf"), kernel_elf);
isoroot.put(Self::boot_path("limine.cfg"), limine_cfg);
isoroot.put(Self::boot_path("limine.sys"), limine_sys);
for module in usrspc_modules {
isoroot.put_module(module)
}
isoroot
}
fn mkdir(&self, path: impl AsRef<str>) {
fs::create_dir_all(path::isoroot().join(path.as_ref()))
.expect("failed to create boot subdirectory");
}
fn put_module(&self, file: &Path) {
let path = format!(
"{}/{}",
Self::USRSPC_PATH,
file.file_name().expect("invalid file").to_string_lossy()
);
if self.exists(&path) {
panic!("overwriting module");
}
self.put(&path, file);
}
fn put(&self, path: impl AsRef<str>, file: &Path) {
let path = path.as_ref();
fs::copy(
file.clone(),
format!("{}/{path}", path::isoroot().display()),
)
.expect(&format!(
"failed to copy file {} to {}/{path}",
file.display(),
path::isoroot().display()
));
}
fn exists(&self, path: impl AsRef<str>) -> bool {
let path = path.as_ref();
PathBuf::from(format!("{}/{path}", path::isoroot().display())).exists()
}
fn path(&self) -> PathBuf {
path::isoroot()
}
}
pub struct Initrd(PathBuf);
impl Initrd {
pub fn create(files: Vec<impl Into<PathBuf>>, out_file: PathBuf) -> Initrd {
assert!(
tool("initrd")
.arg("--output")
.arg(&out_file)
.args(files.into_iter().map(|e| Into::<PathBuf>::into(e)))
.status()
.expect("failed to start initrd tool")
.success(),
"initrd tool ran unsuccessfully"
);
Self(out_file)
}
pub fn output(&self) -> &Path {
&self.0
}
}
struct Image(PathBuf);
impl Image {
pub fn create(iso_root: ISORoot) -> Self {
let image = PathBuf::from(format!("{}/image", path::build().display()));
assert!(
Command::new("fallocate")
.args(["-l", "1G"])
.arg(&image)
.status()
.expect("failed to run command")
.success(),
"failed to fallocate {}",
image.display()
);
assert!(
Command::new("parted")
.arg("-s")
.arg(&image)
.args(["mklabel", "msdos", "mkpart", "primary", "ext2", "1", "100%"])
.status()
.expect("failed to run command")
.success(),
"failed call to parted"
);
assert!(
Command::new("mkfs.ext2")
.arg(&image)
.arg("-d")
.arg(iso_root.path())
.args(["-E", "offset=1048576"])
.status()
.expect("failed to run command")
.success(),
"failed to format partition"
);
assert!(
Command::new("thirdparty/limine/limine-deploy")
.arg(&image)
.arg("--quiet")
.status()
.expect("failed to run command")
.success(),
"failed to deploy limine"
);
Self(image)
}
pub fn path(&self) -> &Path {
self.0.as_path()
}
}
fn run_qemu(image: Image) {
let mut qemu_command = Command::new("qemu-system-x86_64");
qemu_command
.arg("-drive")
.arg(format!("format=raw,file={}", image.path().display()))
.arg("-s")
.arg("-S")
.arg("--no-reboot")
.args(["-m", "4G"])
.args(["-monitor", "stdio"])
.args(["-d", "int"])
//.args(["-d", "int,unimp,mmu,cpu_reset,guest_errors,page"])
.args(["-D", "qemu.log"])
.args(["-serial", "file:qemu.serial.log"])
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
println!("running qemu: {qemu_command:?}",);
assert!(
qemu_command
.status()
.expect("failed to run command")
.success(),
"failed to run qemu"
);
}
#[test]
fn test() {
fn cargo_test(dir: &str) {
let cmd = cargo()
.arg("test")
.current_dir(dir)
.status()
.expect(&format!("failed to test '{dir}'"));
assert!(cmd.success(), "failed test '{dir}'",);
}
cargo_test("tools");
cargo_test("dep");
}
fn main() {
path::create_paths();
let projects = Projects::build();
let iso_root = ISORoot::create(
&projects.kernel_elf,
&projects.usrspc_modules,
&PathBuf::from(thirdparty::limine::LIMINE_CFG),
&PathBuf::from(thirdparty::limine::LIMINE_SYS),
);
let initrd = Initrd::create(
vec!["build/usrspc/ps2"],
format!("{}/initrd", path::build().display()).into(),
);
iso_root.put_module(initrd.output());
let image = Image::create(iso_root);
run_qemu(image);
}