65 lines
1.9 KiB
Zig
65 lines
1.9 KiB
Zig
const std = @import("std");
|
|
|
|
pub fn usage() void {
|
|
std.debug.print("generate-program-spec <output file name> <spec_name> <module list>\n", .{});
|
|
}
|
|
|
|
pub fn main() !void {
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
defer arena.deinit();
|
|
const allocator = arena.allocator();
|
|
|
|
// Get output filename from build system
|
|
const args = try std.process.argsAlloc(allocator);
|
|
if (args.len < 3) {
|
|
usage();
|
|
@panic("Missing output filename\n");
|
|
}
|
|
const out_path = args[1];
|
|
|
|
const programName = args[2];
|
|
const modules = args[3..];
|
|
|
|
// Generate Zig code content
|
|
var writer = std.ArrayList(u8){};
|
|
_ = try writer.appendSlice(allocator,
|
|
\\const core = @import("core").module;
|
|
\\
|
|
\\ const std = @import("std");
|
|
\\ pub fn getSpec() !*core.SpecVariantMap {
|
|
\\ const spec = try std.heap.smp_allocator.create(core.SpecVariantMap);
|
|
\\ spec.* = try core.createSpecVariant(.{
|
|
\\ .useGPA = true,
|
|
);
|
|
|
|
try writer.print(allocator, ".name = \"{s}\",\n", .{programName});
|
|
|
|
for (modules) |mod| {
|
|
try writer.print(allocator, ".{s} = true,\n", .{mod});
|
|
}
|
|
|
|
_ = try writer.appendSlice(allocator,
|
|
\\ }, std.heap.smp_allocator);
|
|
\\
|
|
\\ return spec;
|
|
\\ }
|
|
\\
|
|
);
|
|
|
|
try writer.append(allocator, 0);
|
|
|
|
// Write to specified output file
|
|
// Open or create the file for writing (overwrites if it exists)
|
|
const file = try std.fs.cwd().createFile(out_path, .{});
|
|
defer file.close();
|
|
|
|
var ast = try std.zig.Ast.parse(allocator, @as([:0]const u8, @ptrCast(writer.items[0 .. writer.items.len - 1])), .zig);
|
|
|
|
// std.debug.print("output=\n{s}", .{content.items[0 .. content.items.len - 1]});
|
|
defer ast.deinit(allocator);
|
|
const out = try ast.renderAlloc(allocator);
|
|
|
|
// Write the content to the file
|
|
try file.writeAll(out);
|
|
}
|