mirror of
https://github.com/rdmsr/zag.git
synced 2026-10-31 23:38:21 +00:00
initial commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
pub const Config = std.StringHashMap([]const u8);
|
||||
|
||||
pub const Platform = struct {
|
||||
arch: std.Target.Cpu.Arch,
|
||||
os: std.Target.Os.Tag,
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 } },
|
||||
};
|
||||
|
||||
inline for (keys) |entry| {
|
||||
if (config.get(entry[0])) |val| {
|
||||
if (std.mem.eql(u8, val, "y")) return entry[1];
|
||||
}
|
||||
}
|
||||
|
||||
return error.NoArchConfigured;
|
||||
}
|
||||
Reference in New Issue
Block a user