zos/build.zig

63 lines
2 KiB
Zig

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 {
const initramfs = addNasmFiles(b, AddNasmFilesOptions{
.filename = "src/initramfs.asm",
.outputname = "initramfs.bin",
.addAsObjFile = false,
.flags = &.{"-f bin"},
});
const initramfs_install = b.addInstallBinFile(initramfs.obj, "initramfs.img");
initramfs_install.step.dependOn(&truncate(b, initramfs).step);
b.getInstallStep().dependOn(&initramfs_install.step);
const run = b.addSystemCommand(&.{ "qemu-system-i386", "-fda" });
run.addFileArg(b.path("zig-out/bin/initramfs.img"));
const run_step = b.step("run", "run the os in qemu");
run_step.dependOn(&run.step);
}
const AddNasmFilesOptions = struct {
filename: []const u8,
outputname: ?[]const u8 = null,
addAsObjFile: bool = true,
flags: []const []const u8 = &.{},
};
const NasmFile = struct {
run: *std.Build.Step.Run,
obj: std.Build.LazyPath,
};
// adapted from https://codeberg.org/raddari/zig-nasm-lib.git
fn addNasmFiles(b: *std.Build, options: AddNasmFilesOptions) NasmFile {
std.debug.assert(!std.fs.path.isAbsolute(options.filename));
const src_file = b.path(options.filename);
const output = options.outputname orelse b.fmt("{s}.o", .{std.mem.sliceTo(options.filename, '.')});
const nasm = b.addSystemCommand(&.{"nasm"});
nasm.addArgs(options.flags);
nasm.addPrefixedDirectoryArg("-i", b.path("src"));
const obj = nasm.addPrefixedOutputFileArg("-o", output);
nasm.addFileArg(src_file);
return .{
.run = nasm,
.obj = obj,
};
}
fn truncate(b: *std.Build, bin: NasmFile) *std.Build.Step.Run {
const exec = b.addSystemCommand(&.{"truncate"});
exec.addArgs(&.{ "-s", "1440k" });
exec.addFileArg(bin.obj);
exec.step.dependOn(&bin.run.step);
return exec;
}