-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
266 lines (220 loc) · 9.3 KB
/
build.zig
File metadata and controls
266 lines (220 loc) · 9.3 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
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
const std = @import("std");
const builtin = @import("builtin");
const Xorriso = @import("build/steps/Xorriso.zig");
const base_qemu_args = .{
"-chardev", "stdio,id=char0,logfile=logs/serial.log,signal=off",
"-serial", "chardev:char0",
// "-daemonize",
"-smp", "1",
"-D", "logs/qemu.log", // Log to logs/qemu.log
"-m", "1G",
};
const qemu_debug_args = .{
"-s", // Enable the gdb stub
"-S", // Start on paused state
"-no-reboot", "-no-shutdown", // Do not restart and hang after a triple fault
"-M", "accel=tcg,smm=off"
};
const qemu_release_args = .{
"-M", "accel=kvm,smm=off",
};
const Step = std.Build.Step;
const Dependency = std.Build.Dependency;
pub fn build(b: *std.Build) !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
const allocator = gpa.allocator();
defer {
const status = gpa.deinit();
if (status == .leak) @panic("Memory leak");
}
const arch = b.option(std.Target.Cpu.Arch, "arch", "The target kernel architecture") orelse .x86_64;
var code_model: std.builtin.CodeModel = .default;
var linker_script_path: std.Build.LazyPath = undefined;
var target_query: std.Target.Query = .{
.cpu_arch = arch,
.os_tag = .freestanding,
.abi = .none,
};
var qemu_cmdline: []const []const u8 = undefined;
switch (arch) {
.x86_64 => {
const Feature = std.Target.x86.Feature;
target_query.cpu_features_add.addFeature(@intFromEnum(Feature.soft_float));
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.mmx));
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.sse));
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.sse2));
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.avx));
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.avx2));
code_model = .kernel;
linker_script_path = b.path("build/linker-x86_64.ld");
qemu_cmdline = &(.{
"qemu-system-x86_64",
"-cpu", "max,migratable=off,+invtsc,+tsc-deadline",
"-M", "q35",
"-drive", "if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-x86_64.fd,readonly=on",
"-d", "int,page,cpu_reset,mmu", // Log useful info
} ++ base_qemu_args);
},
.aarch64 => {
const Feature = std.Target.aarch64.Feature;
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.fp_armv8));
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.crypto));
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.neon));
linker_script_path = b.path("build/linker-aarch64.ld");
qemu_cmdline = &(.{
"qemu-system-aarch64",
"-M", "virt",
"-cpu", "cortex-a72",
"-device", "ramfb",
"-device", "qemu-xhci",
"-device", "usb-kbd",
"-device", "usb-mouse",
"-drive", "if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-aarch64.fd,readonly=on",
} ++ base_qemu_args);
},
.riscv64 => {
const Feature = std.Target.riscv.Feature;
target_query.cpu_features_sub.addFeature(@intFromEnum(Feature.d));
linker_script_path = b.path("build/linker-riscv64.ld");
qemu_cmdline = &(.{
"qemu-system-riscv64",
"-M", "virt",
"-cpu", "rv64",
"-device", "ramfb",
"-device", "qemu-xhci",
"-device", "usb-kbd",
"-device", "usb-mouse",
"-drive", "if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-riscv64.fd,readonly=on",
} ++ base_qemu_args);
},
else => std.debug.panic("Unsupported architecture: {s}", .{@tagName(arch)}),
}
const target = b.resolveTargetQuery(target_query);
const optimize = b.standardOptimizeOption(.{});
const kernel_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.code_model = code_model,
});
kernel_module.addImport("kernel", kernel_module);
const test_module = b.createModule(.{
.target = target,
.optimize = optimize,
.code_model = code_model,
.root_source_file = b.path("src/main.zig"),
});
test_module.addImport("kernel", test_module);
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_module = kernel_module,
.use_llvm = true, // Needed for now as the self hosted backed crashes
// TODO remove this when the self hosted backend works well enough for Debug
});
const kernel_test = b.addTest(.{
.name = "kernel.elf",
.root_module = test_module,
.use_llvm = true,
.test_runner = .{ .path = b.path("build/runner.zig"), .mode = .simple },
.emit_object = false,
});
const kernel_check = b.addExecutable(.{
.name = "kernel.elf",
.root_module = kernel_module,
.use_llvm = true,
});
kernel.setLinkerScript(linker_script_path);
kernel_test.setLinkerScript(linker_script_path);
kernel_check.setLinkerScript(linker_script_path);
const kernel_step = b.step("kernel", "Build the kernel");
kernel_step.dependOn(&kernel.step);
const kernel_test_step = b.step("kernel-test", "Build the kernel with tests");
kernel_test_step.dependOn(&kernel_test.step);
const kernel_check_step = b.step("check", "Build the kernel without emitting binaries");
kernel_check_step.dependOn(&kernel_check.step);
// Limine
var limine_dep = b.dependency("limine", .{
.target = target,
.optimize = optimize,
});
const translate_header = b.addTranslateC(.{
.root_source_file = limine_dep.path("limine.h"),
.target = target,
.optimize = optimize,
.link_libc = false,
});
translate_header.defineCMacro("LIMINE_API_REVISION", "2");
const limine_module = translate_header.addModule("limine");
kernel_module.addImport("limine", limine_module);
test_module.addImport("limine", limine_module);
// Zuacpi
const zuacpi = b.dependency("zuacpi", .{
.log_level = .info,
.override_arch_helpers = false,
});
const zuacpi_module = zuacpi.module("zuacpi");
kernel_module.addImport("zuacpi", zuacpi_module);
test_module.addImport("zuacpi", zuacpi_module);
const xorriso = Xorriso.create(b, arch, kernel, limine_dep);
const xorriso_test = Xorriso.create(b, arch, kernel_test, limine_dep);
const limine = addLimineSteps(b, limine_dep, xorriso, "kernel.iso");
const limine_test = addLimineSteps(b, limine_dep, xorriso_test, "kernel-test.iso");
const other_args: []const []const u8 = switch (optimize) {
.Debug => &qemu_debug_args,
else => &qemu_release_args,
};
const qemu_args = try std.mem.concat(allocator, []const u8, &.{qemu_cmdline, other_args});
defer allocator.free(qemu_args);
addQemuSteps(b, limine, limine_test, xorriso, xorriso_test, qemu_args, kernel, kernel_test);
}
fn addLimineSteps(b: *std.Build, dep: *Dependency, xorriso_step: *Xorriso, install_name: []const u8) *Step {
const module = b.createModule(.{
.target = b.graph.host,
.optimize = .ReleaseSafe,
});
const limine_build = b.addExecutable(.{
.name = "limine",
.root_module = module,
});
limine_build.linkLibC();
limine_build.addCSourceFiles(.{ .files = &.{"limine.c"}, .root = dep.path("") });
const limine_run = b.addRunArtifact(limine_build);
limine_run.addArg("bios-install");
limine_run.addFileArg(xorriso_step.output_path);
const install = b.addInstallFile(xorriso_step.output_path, install_name);
install.step.dependOn(&limine_run.step);
b.getInstallStep().dependOn(&install.step);
limine_run.step.dependOn(&limine_build.step);
limine_run.step.dependOn(&xorriso_step.step);
return &limine_run.step;
}
fn addQemuSteps(
b: *std.Build,
limine_step: *Step,
limine_test_step: *Step,
xorriso: *Xorriso,
xorriso_test: *Xorriso,
qemu_cmdline: []const []const u8,
kernel: *Step.Compile,
kernel_test: *Step.Compile,
) void {
const ovmf = b.addSystemCommand(&.{ "./scripts/fetch_ovmf.sh" });
const ovmf_step = b.step("ovmf", "Fetch OVMF");
ovmf_step.dependOn(&ovmf.step);
const qemu = b.addSystemCommand(qemu_cmdline);
qemu.addArg("-cdrom");
qemu.addFileArg(xorriso.output_path);
qemu.step.dependOn(limine_step);
const qemu_step = b.step("qemu", "Run in QEMU");
qemu_step.dependOn(&qemu.step);
const qemu_install_step = b.step("qemu-install", "Run in QEMU and output the ELF");
qemu_install_step.dependOn(&qemu.step);
qemu_install_step.dependOn(&b.addInstallArtifact(kernel, .{}).step);
const qemu_test = b.addSystemCommand(qemu_cmdline);
qemu_test.addArg("-cdrom");
qemu_test.addFileArg(xorriso_test.output_path);
qemu_test.step.dependOn(limine_test_step);
const qemu_test_step = b.step("qemu-test", "Run tests in QEMU and output the ELF");
qemu_test_step.dependOn(&qemu_test.step);
qemu_test_step.dependOn(&b.addInstallArtifact(kernel_test, .{}).step);
}