Created
November 4, 2026 22:50
-
-
Save binarycraft007/0bd895fccd9773381c8ad619e47e44a7 to your computer and use it in GitHub Desktop.
kbuild parser in zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const std = @import("std"); | |
| pub const Operator = enum { | |
| assign, // = or := | |
| append, // += | |
| }; | |
| pub const Condition = union(enum) { | |
| always: void, // e.g. lib-y | |
| config: []const u8, // e.g. CONFIG_CHCON in lib-$(CONFIG_CHCON) | |
| }; | |
| pub const Entry = struct { | |
| target: []const u8, // e.g. "lib" or "obj" | |
| condition: Condition, // always or config name | |
| op: Operator, | |
| objects: [][]const u8, // e.g. ["setfiles.o"] | |
| }; | |
| pub const Kbuild = struct { | |
| entries: []Entry, | |
| gpa: std.mem.Allocator, | |
| pub fn deinit(self: *Kbuild) void { | |
| for (self.entries) |entry| { | |
| self.gpa.free(entry.objects); | |
| } | |
| self.gpa.free(self.entries); | |
| } | |
| /// Evaluates entries against enabled configuration flags | |
| /// and collects all object files for a given target (e.g. "lib"). | |
| pub fn resolveObjects( | |
| self: Kbuild, | |
| target: []const u8, | |
| enabled_configs: *const std.StringHashMap(void), | |
| gpa: std.mem.Allocator, | |
| ) ![][]const u8 { | |
| var result: std.ArrayList([]const u8) = .empty; | |
| errdefer result.deinit(gpa); | |
| for (self.entries) |entry| { | |
| if (!std.mem.eql(u8, entry.target, target)) continue; | |
| const is_enabled = switch (entry.condition) { | |
| .always => true, | |
| .config => |cfg| enabled_configs.contains(cfg), | |
| }; | |
| if (is_enabled) { | |
| for (entry.objects) |obj| { | |
| try result.append(gpa, obj); | |
| } | |
| } | |
| } | |
| return result.toOwnedSlice(gpa); | |
| } | |
| pub fn parse(gpa: std.mem.Allocator, content: []const u8) !Kbuild { | |
| var entry_list: std.ArrayList(Entry) = .empty; | |
| errdefer { | |
| for (entry_list.items) |entry| gpa.free(entry.objects); | |
| entry_list.deinit(gpa); | |
| } | |
| var line_iter = std.mem.splitScalar(u8, content, '\n'); | |
| while (line_iter.next()) |raw_line| { | |
| // 1. Strip comments (everything after '#') | |
| const line_no_comment = if (std.mem.findScalar(u8, raw_line, '#')) |idx| | |
| raw_line[0..idx] | |
| else | |
| raw_line; | |
| // 2. Trim whitespace | |
| const line = std.mem.trim(u8, line_no_comment, " \t\r"); | |
| if (line.len == 0) continue; | |
| // 3. Find assignment operator (+=, :=, or =) | |
| var op: Operator = .assign; | |
| var op_idx: usize = 0; | |
| var op_len: usize = 0; | |
| if (std.mem.find(u8, line, "+=")) |idx| { | |
| op = .append; | |
| op_idx = idx; | |
| op_len = 2; | |
| } else if (std.mem.find(u8, line, ":=")) |idx| { | |
| op = .assign; | |
| op_idx = idx; | |
| op_len = 2; | |
| } else if (std.mem.find(u8, line, "=")) |idx| { | |
| op = .assign; | |
| op_idx = idx; | |
| op_len = 1; | |
| } else { | |
| continue; // Skip lines without assignments | |
| } | |
| const lhs = std.mem.trim(u8, line[0..op_idx], " \t"); | |
| const rhs = std.mem.trim(u8, line[op_idx + op_len ..], " \t"); | |
| // 4. Parse Left-Hand Side (e.g., "lib-$(CONFIG_CHCON)" or "lib-y") | |
| const dash_idx = std.mem.indexOfScalar(u8, lhs, '-') orelse continue; | |
| const target = lhs[0..dash_idx]; | |
| const cond_raw = lhs[dash_idx + 1 ..]; | |
| const condition: Condition = if (std.mem.eql(u8, cond_raw, "y")) | |
| .{ .always = {} } | |
| else if (std.mem.startsWith(u8, cond_raw, "$(") and std.mem.endsWith(u8, cond_raw, ")")) | |
| .{ .config = cond_raw[2 .. cond_raw.len - 1] } | |
| else | |
| .{ .config = cond_raw }; | |
| // 5. Parse Right-Hand Side (tokenized object filenames) | |
| var obj_list: std.ArrayList([]const u8) = .empty; | |
| errdefer obj_list.deinit(gpa); | |
| var obj_iter = std.mem.tokenizeAny(u8, rhs, " \t"); | |
| while (obj_iter.next()) |obj| { | |
| try obj_list.append(gpa, obj); | |
| } | |
| try entry_list.append(gpa, .{ | |
| .target = target, | |
| .condition = condition, | |
| .op = op, | |
| .objects = try obj_list.toOwnedSlice(gpa), | |
| }); | |
| } | |
| return Kbuild{ | |
| .entries = try entry_list.toOwnedSlice(gpa), | |
| .gpa = gpa, | |
| }; | |
| } | |
| }; | |
| // --- Example & Tests --- | |
| pub fn main(init: std.process.Init) !void { | |
| const gpa = init.gpa; | |
| const kbuild_sample = | |
| \\# DO NOT EDIT. This file is generated from Kbuild.src | |
| \\# Makefile for busybox | |
| \\ | |
| \\lib-y:= | |
| \\ | |
| \\lib-$(CONFIG_CHCON) += chcon.o | |
| \\lib-$(CONFIG_GETENFORCE) += getenforce.o | |
| \\lib-$(CONFIG_GETSEBOOL) += getsebool.o | |
| \\lib-$(CONFIG_LOAD_POLICY) += load_policy.o | |
| \\lib-$(CONFIG_MATCHPATHCON) += matchpathcon.o | |
| \\lib-$(CONFIG_RUNCON) += runcon.o | |
| \\lib-$(CONFIG_SELINUXENABLED) += selinuxenabled.o | |
| \\lib-$(CONFIG_SESTATUS) += sestatus.o | |
| \\lib-$(CONFIG_SETENFORCE) += setenforce.o | |
| \\lib-$(CONFIG_SETFILES) += setfiles.o | |
| \\lib-$(CONFIG_RESTORECON) += setfiles.o | |
| \\lib-$(CONFIG_SETSEBOOL) += setsebool.o | |
| ; | |
| // 1. Parse Kbuild | |
| var kbuild: Kbuild = try .parse(gpa, kbuild_sample); | |
| defer kbuild.deinit(); | |
| std.debug.print("Parsed {d} entries successfully!\n\n", .{kbuild.entries.len}); | |
| // 2. Simulate enabled options in .config | |
| var enabled_configs = std.StringHashMap(void).init(gpa); | |
| defer enabled_configs.deinit(); | |
| try enabled_configs.put("CONFIG_CHCON", {}); | |
| try enabled_configs.put("CONFIG_SETFILES", {}); | |
| try enabled_configs.put("CONFIG_SESTATUS", {}); | |
| // 3. Resolve which .o files need to be compiled for "lib" | |
| const resolved_objects = try kbuild.resolveObjects("lib", &enabled_configs, gpa); | |
| defer gpa.free(resolved_objects); | |
| std.debug.print("Objects to compile for 'lib':\n", .{}); | |
| for (resolved_objects) |obj| { | |
| std.debug.print(" - {s}\n", .{obj}); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment