lumine
UEFI x86_64 bootloader for linux kernel images
build.zig
2239 bytes raw
1const std = @import("std");
2
3const Arch = enum {
4 x86_64,
5 aarch64,
6
7 pub fn stdlib(self: Arch) std.Target.Cpu.Arch {
8 return switch (self) {
9 .x86_64 => .x86_64,
10 .aarch64 => .aarch64,
11 };
12 }
13
14 pub fn efiName(self: Arch, b: *std.Build) []const u8 {
15 const suffix = switch (self) {
16 .x86_64 => "X64",
17 .aarch64 => "AA64",
18 };
19 return b.fmt("BOOT{s}", .{suffix});
20 }
21};
22
23pub fn build(b: *std.Build) void {
24 const optimize = b.standardOptimizeOption(.{});
25
26 const cpu_arch = b.option(Arch, "arch", "cpu architecture") orelse .x86_64;
27
28 const efi_name = cpu_arch.efiName(b);
29
30 const lumine_exe = b.addExecutable(.{
31 .name = efi_name,
32 .root_module = b.createModule(.{
33 .root_source_file = b.path("src/main.zig"),
34 .target = b.resolveTargetQuery(.{
35 .os_tag = .uefi,
36 .cpu_arch = cpu_arch.stdlib(),
37 }),
38 .optimize = optimize,
39 }),
40 });
41
42 const install_step = b.getInstallStep();
43
44 const lumine_install = b.addInstallArtifact(lumine_exe, .{});
45 install_step.dependOn(&lumine_install.step);
46
47 const qemu_exe = b.option([]const u8, "qemu", "path to qemu binary") orelse b.fmt("qemu-system-{t}", .{cpu_arch});
48 const qemu_cmd = b.addSystemCommand(&.{ qemu_exe, "-m", "512M", "-serial", "mon:stdio", "-nographic", "-no-reboot", "-no-shutdown" });
49
50 {
51 const ovmf_dep = b.dependency("ovmf", .{});
52 const ovmf_code = ovmf_dep.path(b.fmt("ovmf-code-{t}.fd", .{cpu_arch}));
53
54 switch (cpu_arch) {
55 .aarch64 => {
56 qemu_cmd.addArgs(&.{ "-M", "virt", "-cpu", "max" });
57 },
58 .x86_64 => {},
59 }
60
61 qemu_cmd.addArg("-bios");
62 qemu_cmd.addFileArg(ovmf_code);
63 }
64
65 {
66 const boot_dir = b.addTempFiles();
67 _ = boot_dir.addCopyFile(lumine_exe.getEmittedBin(), b.fmt("EFI/BOOT/{s}.EFI", .{efi_name}));
68
69 qemu_cmd.addArg("-drive");
70 qemu_cmd.addPrefixedDirectoryArg("format=raw,file=fat:rw:", boot_dir.getDirectory());
71 }
72
73 const run_step = b.step("run", "run in qemu");
74 run_step.dependOn(&qemu_cmd.step);
75}