-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
112 lines (94 loc) · 3.66 KB
/
build.zig
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
const std = @import("std");
const Build = std.Build;
const Step = Build.Step;
const os_name = "myos";
fn get_grub_cfg(b: *Build, kernel_filename: []const u8) []const u8 {
const grub_cfg =
\\set default=0
\\set timeout=0
\\
\\menuentry "{s}" {{
\\ multiboot /boot/{s}
\\ boot
\\}}
;
return b.fmt(grub_cfg, .{ os_name, kernel_filename });
}
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *Build) !void {
const target = std.Target.Query{
.cpu_arch = .x86,
.cpu_model = .{ .explicit = &std.Target.x86.cpu.i686 },
.os_tag = .freestanding,
.abi = .none,
.ofmt = .elf,
};
const optimize = b.standardOptimizeOption(.{
.preferred_optimize_mode = .ReleaseSafe,
});
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = b.path("src/kernel.zig"),
.target = b.resolveTargetQuery(target),
.optimize = optimize,
.use_lld = true,
});
kernel.setLinkerScriptPath(b.path("src/linker.ld"));
kernel.root_module.code_model = .kernel;
// @NOTE: stack overflow/smash protector only works on targets with a libc (https://github.com/ziglang/zig/issues/4542),
// but stack overflow detection is enabled by default in safe build modes.
// We use ReleaseSafe because of this.
// kernel.stack_protector = true;
b.installArtifact(kernel);
{
const kernel_qemu_cmd = b.addSystemCommand(&.{
"qemu-system-i386",
"-no-reboot",
"-no-shutdown",
"-serial",
"stdio",
"-D",
"./qemu.log",
"-kernel",
});
kernel_qemu_cmd.addArtifactArg(kernel);
if (b.option([]const u8, "qemu-d", "Pass '-d' flags to QEMU. (Must be comma seperated with no spaces)")) |args| {
kernel_qemu_cmd.addArgs(&.{ "-d", args });
}
const kernel_qemu_step = b.step("run-kernel", "Executes raw kernel binary through QEMU");
kernel_qemu_step.dependOn(&kernel_qemu_cmd.step);
}
const use_wsl = b.option(bool, "wsl", "Run GRUB commands through WSL") orelse false;
if (b.option(bool, "verify-mboot", "Verify kernel is Multiboot compliant") orelse false) {
// [wsl] grub-file --is-x86-multiboot zig-out/bin/kernel.bin
const verify_cmd = b.addSystemCommand(&.{
if (use_wsl) "wsl" else "",
"grub2-file",
"--is-x86-multiboot",
});
verify_cmd.addArtifactArg(kernel);
verify_cmd.expectExitCode(0);
}
// Build and run OS ISO
{
const isodir = b.addWriteFiles();
_ = isodir.add("boot/grub/grub.cfg", get_grub_cfg(b, kernel.name));
_ = isodir.addCopyFile(kernel.getEmittedBin(), b.fmt("boot/{s}", .{kernel.name}));
const iso_name = os_name ++ ".iso";
const mkrescue_cmd = b.addSystemCommand(&.{
if (use_wsl) "wsl" else "",
"grub2-mkrescue",
"-o",
});
const iso = mkrescue_cmd.addOutputFileArg(iso_name);
mkrescue_cmd.addDirectoryArg(isodir.getDirectory());
const build_iso = b.step("iso", "Build operating system ISO image (requires GRUB)");
build_iso.dependOn(&b.addInstallBinFile(iso, iso_name).step);
const iso_run_cmd = b.addSystemCommand(&.{ "qemu-system-i386", "-cdrom" });
iso_run_cmd.addFileArg(iso);
const iso_run_step = b.step("run-iso", "Executes ISO image through QEMU");
iso_run_step.dependOn(&iso_run_cmd.step);
}
}