const std = @import("std"); // 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: *std.Build) void { // Standard target options allows the person running `zig build` to choose // what target to build for. Here we do not override the defaults, which // means any target is allowed, and the default is native. Other options // for restricting supported target set are available. const target = b.standardTargetOptions(.{}); // Standard optimization options allow the person running `zig build` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); // This creates a "module", which represents a collection of source files alongside // some compilation options, such as optimization mode and linked system libraries. // Every executable or library we compile will be based on one or more modules. const mod = b.addModule("bootloader", .{ // `root_source_file` is the Zig "entry point" of the module. If a module // only contains e.g. external object files, you can make this `null`. // In this case the main source file is merely a path, however, in more // complicated build scripts, this could be a generated file. .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); const kernel = b.option([]const u8, "kernel", "Path to kernel executable"); const output = b.option([]const u8, "output", "Output image name"); const image = buildBootableImage(b, b.path(kernel orelse "kernel"), target.result.cpu.arch); const install = b.addInstallFile(image, output orelse "bootimage.iso"); b.getInstallStep().dependOn(&install.step); // Creates a step for unit testing. This only builds the test executable // but does not run it. const unit_tests = b.addTest(.{ .root_module = mod, }); const run_unit_tests = b.addRunArtifact(unit_tests); // Similar to creating the run step earlier, this exposes a `test` step to // the `zig build --help` menu, providing a way for the user to request // running the unit tests. const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_unit_tests.step); } pub fn buildBootableImage(b: *std.Build, kernel: std.Build.LazyPath, arch: std.Target.Cpu.Arch) std.Build.LazyPath { // Add Limine to the dependency tree. const limine = b.dependency("limine", .{}); const rootfs = b.addNamedWriteFiles("rootfs"); const exe = b.addExecutable(.{ .name = "limine", .target = b.resolveTargetQuery(.{}), .optimize = .ReleaseSafe, }); exe.addCSourceFile(.{ .file = limine.path("limine.c"), .flags = &.{"-std=c99"}, }); exe.linkLibC(); _ = rootfs.addCopyFile(b.path("limine.conf"), "boot/limine/limine.conf"); _ = rootfs.addCopyFile(kernel, "boot/kernel"); const run_cmd = b.addSystemCommand(switch (arch) { .aarch64 => block: { _ = rootfs.addCopyFile(limine.path("limine-uefi-cd.bin"), "boot/limine/limine-uefi-cd.bin"); _ = rootfs.addCopyFile(limine.path("BOOTAA64.EFI"), "EFI/BOOT/BOOTAA64.EFI"); break :block &.{ "xorriso", "-as", "mkisofs", "-R", "-r", "-J", "-hfsplus", "-apm-block-size", "2048", "--efi-boot", "boot/limine/limine-uefi-cd.bin", "-efi-boot-part", "--efi-boot-image", "--protective-msdos-label", }; }, .loongarch64 => block: { _ = rootfs.addCopyFile(limine.path("limine-uefi-cd.bin"), "boot/limine/limine-uefi-cd.bin"); _ = rootfs.addCopyFile(limine.path("BOOTLOONGARCH64.EFI"), "EFI/BOOT/BOOTLOONGARCH64.EFI"); break :block &.{ "xorriso", "-as", "mkisofs", "-R", "-r", "-J", "-hfsplus", "-apm-block-size", "2048", "--efi-boot", "boot/limine/limine-uefi-cd.bin", "-efi-boot-part", "--efi-boot-image", "--protective-msdos-label", }; }, .riscv64 => block: { _ = rootfs.addCopyFile(limine.path("limine-uefi-cd.bin"), "boot/limine/limine-uefi-cd.bin"); _ = rootfs.addCopyFile(limine.path("BOOTRISCV64.EFI"), "EFI/BOOT/BOOTRISCV64.EFI"); break :block &.{ "xorriso", "-as", "mkisofs", "-R", "-r", "-J", "-hfsplus", "-apm-block-size", "2048", "--efi-boot", "boot/limine/limine-uefi-cd.bin", "-efi-boot-part", "--efi-boot-image", "--protective-msdos-label", }; }, .x86_64 => block: { _ = rootfs.addCopyFile(limine.path("limine-bios.sys"), "boot/limine/limine-bios.sys"); _ = rootfs.addCopyFile(limine.path("limine-bios-cd.bin"), "boot/limine/limine-bios-cd.bin"); _ = rootfs.addCopyFile(limine.path("limine-uefi-cd.bin"), "boot/limine/limine-uefi-cd.bin"); _ = rootfs.addCopyFile(limine.path("BOOTX64.EFI"), "EFI/BOOT/BOOTX64.EFI"); _ = rootfs.addCopyFile(limine.path("BOOTIA32.EFI"), "EFI/BOOT/BOOTIA32.EFI"); break :block &.{ "xorriso", "-as", "mkisofs", "-R", "-r", "-J", "-b", "boot/limine/limine-bios-cd.bin", "-no-emul-boot", "-boot-load-size", "4", "-boot-info-table", "-hfsplus", "-apm-block-size", "2048", "--efi-boot", "boot/limine/limine-uefi-cd.bin", "-efi-boot-part", "--efi-boot-image", "--protective-msdos-label", }; }, else => unreachable, }); run_cmd.step.dependOn(&rootfs.step); run_cmd.addDirectoryArg(rootfs.getDirectory()); run_cmd.addArg("-o"); // Retrieve the bootable image from command. const raw = run_cmd.addOutputFileArg("raw.iso"); // Install Limine stage 1 and 2 for legacy BIOS boot. // This is only required on x86_64, but we are deploying for all anyway. const deploy = b.addRunArtifact(exe); deploy.step.dependOn(&run_cmd.step); deploy.addArg("bios-install"); deploy.addFileArg(raw); const wf = b.addWriteFiles(); const image = wf.addCopyFile(raw, "image.iso"); wf.step.dependOn(&deploy.step); return image; }