99 lines
3.4 KiB
Zig
99 lines
3.4 KiB
Zig
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(.{});
|
|
|
|
const kernel_elf = b.addExecutable(.{
|
|
.name = "kernel.elf",
|
|
.root_source_file = b.path("src/main.zig"),
|
|
.target = b.resolveTargetQuery(target_query),
|
|
.optimize = optimize,
|
|
.code_model = .kernel,
|
|
});
|
|
kernel_elf.setLinkerScript(b.path("src/linker.ld"));
|
|
|
|
const kernel_elf_artifact = b.addInstallArtifact(kernel_elf, .{});
|
|
|
|
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",
|
|
b.fmt("{s}/bin/{s}", .{ b.install_path, "kernel.bin" }),
|
|
iso_dir_boot_path,
|
|
});
|
|
iso_copy_kernel.step.dependOn(&iso_make_dir.step);
|
|
iso_copy_kernel.step.dependOn(kernel_step_bin);
|
|
|
|
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);
|
|
}
|