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 [extern module list ...] // Generate a startup module function that invokes // startup module for each zig function // and generates an api var writer = std.ArrayList(u8){}; // 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.appendSlice(allocator, \\ pub const core = @import("core").module; \\ \\ pub fn loadDynamicModules() !void { \\ ); if (!static) { for (modlist) |mod| { try writer.print(allocator, "try core.loadModule(\"{s}\", true);\n", .{mod}); } } if (static) { _ = try writer.appendSlice(allocator, \\ const args = core.externModule.getModuleLoaderArgs(true); ); if (modlist.len == 0) { _ = try writer.appendSlice(allocator, \\ _ = args; ); } for (modlist) |mod| { try writer.print(allocator, "try {s}.start_module(args);\n", .{mod}); } } _ = try writer.appendSlice(allocator, "}\n"); if (static) { for (modlist) |mod| { try writer.print(allocator, "const {s} = @import(\"{s}\");\n", .{ mod, mod }); } } try writer.print(allocator, "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 writer.append(allocator, 0); 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); }