zig-bare-bones/build.zig

99 lines
3.4 KiB
Zig
Raw Normal View History

2024-08-22 21:57:22 +00:00
const std = @import("std");
pub fn build(b: *std.Build) void {
var disabled_features = std.Target.Cpu.Feature.Set.empty;
var enabled_features = std.Target.Cpu.Feature.Set.empty;
disabled_features.addFeature(@intFromEnum(std.Target.x86.Feature.mmx));
disabled_features.addFeature(@intFromEnum(std.Target.x86.Feature.sse));
disabled_features.addFeature(@intFromEnum(std.Target.x86.Feature.sse2));
disabled_features.addFeature(@intFromEnum(std.Target.x86.Feature.avx));
disabled_features.addFeature(@intFromEnum(std.Target.x86.Feature.avx2));
enabled_features.addFeature(@intFromEnum(std.Target.x86.Feature.soft_float));
const target_query = std.Target.Query{
.cpu_arch = std.Target.Cpu.Arch.x86,
.os_tag = std.Target.Os.Tag.freestanding,
.abi = std.Target.Abi.none,
.cpu_features_sub = disabled_features,
.cpu_features_add = enabled_features,
};
const optimize = b.standardOptimizeOption(.{});
2024-09-14 22:31:20 +00:00
const kernel_elf = b.addExecutable(.{
2024-08-22 21:57:22 +00:00
.name = "kernel.elf",
.root_source_file = b.path("src/main.zig"),
.target = b.resolveTargetQuery(target_query),
.optimize = optimize,
.code_model = .kernel,
});
2024-09-14 22:31:20 +00:00
kernel_elf.setLinkerScript(b.path("src/linker.ld"));
2024-08-22 21:57:22 +00:00
2024-09-14 22:31:20 +00:00
const kernel_elf_artifact = b.addInstallArtifact(kernel_elf, .{});
2024-08-22 21:57:22 +00:00
2024-09-14 22:31:20 +00:00
const kernel_bin = b.addObjCopy(kernel_elf.getEmittedBin(), .{
.format = .bin,
.only_section = ".blob",
});
kernel_bin.step.dependOn(&kernel_elf_artifact.step);
const kernel_bin_copy = b.addInstallBinFile(
kernel_bin.getOutput(),
"kernel.bin",
);
kernel_bin_copy.step.dependOn(&kernel_bin.step);
const kernel_step_elf = b.step("elf", "Build the kernel ELF");
kernel_step_elf.dependOn(&kernel_elf.step);
const kernel_step_bin = b.step("bin", "Build the kernel BIN (with objcopy)");
kernel_step_bin.dependOn(&kernel_bin_copy.step);
const iso_dir_path = b.fmt("{s}/iso_dir", .{b.cache_root.path.?});
const iso_dir_boot_path = b.fmt("{s}/boot", .{iso_dir_path});
const grub_dir_path = b.fmt("{s}/{s}", .{ iso_dir_boot_path, "grub" });
const iso_make_dir = b.addSystemCommand(&[_][]const u8{
"mkdir",
"-p",
grub_dir_path,
});
const iso_copy_kernel = b.addSystemCommand(&[_][]const u8{
"cp",
2024-09-15 14:41:54 +00:00
b.fmt("{s}/bin/{s}", .{ b.install_path, "kernel.bin" }),
2024-09-14 22:31:20 +00:00
iso_dir_boot_path,
});
iso_copy_kernel.step.dependOn(&iso_make_dir.step);
2024-09-15 14:41:54 +00:00
iso_copy_kernel.step.dependOn(kernel_step_bin);
2024-09-14 22:31:20 +00:00
const iso_copy_grubcfg = b.addSystemCommand(&[_][]const u8{
"cp",
"src/grub.cfg",
grub_dir_path,
});
iso_copy_grubcfg.step.dependOn(&iso_make_dir.step);
const iso_make_iso = b.addSystemCommand(&[_][]const u8{
"grub2-mkrescue",
"-o",
b.fmt("{s}/{s}", .{ b.install_path, "kernel.iso" }),
iso_dir_path,
});
iso_make_iso.step.dependOn(&iso_copy_grubcfg.step);
iso_make_iso.step.dependOn(&iso_copy_kernel.step);
const iso_step = b.step("iso", "Create bootable iso using GRUB2");
iso_step.dependOn(&iso_make_iso.step);
const run_qemu = b.addSystemCommand(&[_][]const u8{
"qemu-system-i386",
"-cdrom",
b.fmt("{s}/{s}", .{ b.install_path, "kernel.iso" }),
});
run_qemu.step.dependOn(&iso_make_iso.step);
const run_qemu_step = b.step("run", "Run in qemu");
run_qemu_step.dependOn(&run_qemu.step);
2024-08-22 21:57:22 +00:00
}