Backlog/build/generateLoadDynamics.zig

75 lines
2.3 KiB
Zig

const std = @import("std");
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) @panic("invalid args");
const out_path = args[1];
const static = std.mem.eql(u8, args[2], "static");
const modlist: []const []const u8 = if (args.len > 3) args[3..args.len] else &.{};
// exe <outputpath> <static/dynamic> [extern module list ...]
// Generate a startup module function that invokes
// startup module for each zig function
// and generates an api
var content = std.ArrayList(u8).init(allocator);
var writer = content.writer();
// const content_path = args[2];
// std.debug.print("content path: {s}\n", .{content_path});
// check the content directory and search for all _spv files
_ = try writer.write(
\\ pub const core = @import("core").module;
\\
\\ pub fn loadDynamicModules() !void {
\\
);
if (!static) {
for (modlist) |mod| {
try writer.print("try core.loadModule(\"{s}\", true);\n", .{mod});
}
}
if (static) {
_ = try writer.write(
\\ const args = core.externModule.getModuleLoaderArgs(true);
);
for (modlist) |mod| {
try writer.print("try {s}.start_module(args);\n", .{mod});
}
}
_ = try writer.write("}\n");
if (static) {
for (modlist) |mod| {
try writer.print("const {s} = @import(\"{s}\");\n", .{ mod, mod });
}
}
try writer.print("const std = @import(\"std\");", .{});
// 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();
try content.append(0);
var ast = try std.zig.Ast.parse(allocator, @as([:0]const u8, @ptrCast(content.items[0 .. content.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.render(allocator);
// Write the content to the file
try file.writeAll(out);
}