build: switch to zonfig

This commit is contained in:
Loïc Grégoire
2026-04-24 10:35:53 -04:00
parent 36690a8149
commit a46782adca
18 changed files with 179 additions and 247 deletions
+27 -77
View File
@@ -1,7 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
pub const Config = std.StringHashMap([]const u8);
const zonfig = @import("zonfig");
pub const Bootloader = enum {
Limine,
@@ -14,88 +13,39 @@ pub const Platform = struct {
bootloader: ?Bootloader = null,
};
const ArchEnum = enum {
amd64,
aarch64,
um,
};
const BootloaderEnum = enum {
limine,
};
pub const Config = struct {
arch: ArchEnum,
bootloader: BootloaderEnum,
};
pub fn parseConfig(b: *std.Build) !Config {
const file = try std.Io.Dir.cwd().readFileAlloc(b.graph.io, ".config", b.allocator, std.Io.Limit.unlimited);
var lines = std.mem.splitScalar(u8, file, '\n');
var map = std.StringHashMap([]const u8).init(b.allocator);
while (lines.next()) |line| {
const trimmed = std.mem.trim(u8, line, " \r\t");
if (trimmed.len == 0 or std.mem.startsWith(u8, trimmed, "#")) {
continue;
}
var kv = std.mem.splitScalar(u8, trimmed, '=');
const key = kv.first();
const value = kv.rest();
try map.put(key, value);
}
return map;
}
pub fn configStep(b: *std.Build) *std.Build.Step {
return &b.addSystemCommand(&.{ "bash", "build/menuconfig" }).step;
}
fn isInteger(str: []const u8) bool {
_ = std.fmt.parseInt(i64, str, 10) catch return false;
return true;
}
pub fn generateConfig(b: *std.Build, config: Config) !*std.Build.Step.WriteFile {
var out = std.Io.Writer.Allocating.init(b.allocator);
var it = config.iterator();
while (it.next()) |entry| {
const key = entry.key_ptr.*;
const value = entry.value_ptr.*;
if (std.mem.eql(u8, value, "y")) {
try out.writer.print("pub const {s}: bool = true;\n", .{key});
} else if (isInteger(value)) {
try out.writer.print("pub const {s}: i64 = {s};\n", .{ key, value });
} else {
try out.writer.print("pub const {s} = \"{s}\";\n", .{ key, value });
}
}
const wf = b.addWriteFiles();
_ = wf.add("config.gen.zig", out.written());
_ = wf.addCopyFile(b.path(".config"), ".config");
return wf;
const file = try std.Io.Dir.cwd().readFileAllocOptions(b.graph.io, ".config.zig.zon", b.allocator, std.Io.Limit.unlimited, .@"1", 0);
return std.zon.parse.fromSlice(Config, b.allocator, file, null, .{ .ignore_unknown_fields = true });
}
pub fn getPlatform(config: Config) !Platform {
const keys = .{
.{ "CONFIG_ARCH_AMD64", Platform{ .arch = .x86_64, .os = .freestanding } },
.{ "CONFIG_ARCH_AARCH64", Platform{ .arch = .aarch64, .os = .freestanding } },
.{ "CONFIG_ARCH_RISCV64", Platform{ .arch = .riscv64, .os = .freestanding } },
.{ "CONFIG_ARCH_UM", Platform{ .arch = builtin.cpu.arch, .os = .linux } },
};
var ret = blk: {
inline for (keys) |entry| {
if (config.get(entry[0])) |val| {
if (std.mem.eql(u8, val, "y")) break :blk entry[1];
}
}
return error.NoArchConfigured;
var ret = Platform{
.arch = switch (config.arch) {
.amd64 => .x86_64,
.aarch64 => .aarch64,
.um => builtin.cpu.arch,
},
.os = if (config.arch == .um) .linux else .freestanding,
};
if (ret.os == .freestanding and (ret.arch == .x86_64 or ret.arch == .aarch64)) {
ret.bootloader = blk: {
if (config.get("CONFIG_BOOT_LIMINE")) |val| {
if (std.mem.eql(u8, val, "y")) break :blk .Limine;
}
return error.NoBootloaderConfigured;
ret.bootloader = switch (config.bootloader) {
.limine => .Limine,
};
}