Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133const std = @import("std");const u = @import("utils.zig");const Zmd = @import("../zmd/Config.zig");const Zmpl = @import("../zmpl/Config.zig");
const Writer = std.Io.Writer;const Build = std.Build;const Module = Build.Module;const Import = Module.Import;
pub fn createConfig( b: *Build, comptime zmd: Zmd, comptime zmpl: Zmpl, imports: []const Import,) !*Module { var buffer: Writer.Allocating = .init(b.allocator); defer buffer.deinit(); try writeConfig(&buffer.writer, "zmd", Zmd, zmd); try writeConfig(&buffer.writer, "zmpl", Zmpl, zmpl); const content = try buffer.toOwnedSlice(); defer b.allocator.free(content); const file = b.addWriteFiles(); const path = file.add("compiler_config", content); return b.createModule(.{ .root_source_file = path, .imports = imports, });}
fn writeConfig(writer: *Writer, name: []const u8, comptime T: type, comptime config: T) !void { try writer.print("{[name]s}: @import(\"{[name]s}\").Config = ", .{ .name = name }); try writeZigLiteral(writer, T, config, 0); try writer.writeAll(",\n");}
// fn generateConfig(// self: *Jetzig,// comptime zmpl: Zmpl,// comptime zmd: Zmd,// ) !*Module {// var buffer: Writer.Allocating = .init(self.b.allocator);// defer buffer.deinit();// const writer = &buffer.writer;// try writeConfig2(writer, "zmpl", "zmpl", Zmpl, zmpl);// try writeConfig2(writer, "zmd", "zmd", Zmd, zmd);// const written = try buffer.toOwnedSlice();// defer self.b.allocator.free(written);// const write_file = self.b.addWriteFiles();// const config_path = write_file.add("jetzig_compiler_config.zig", written);// return self.b.createModule(.{// .root_source_file = config_path,// .imports = &.{.{ .name = "jet", .module = self.module }},// });// }
// fn writeConfig2(writer: *Writer, name: []const u8, n: []const u8, comptime T: type, comptime config: T) !void {// try writer.print("{[name]s}: @import(\"jet\").{[n]s}.Config = ", .{ .name = name, .n = n });// // try writer.writeAll("zmpl: @import(\"zmpl\").Config = ");// try writeZigLiteral(writer, T, config, 0);// try writer.writeAll(",\n");// }
//// fn writeZmdConfig(writer: *Writer, comptime T: type, comptime config: T) !void {// try writer.writeAll("zmd: @import(\"zmd\").Config = ");// try writeZigLiteral(writer, T, config, 0);// try writer.writeAll(",\n");// }
fn writeZigLiteral(writer: *Writer, comptime T: type, comptime value: T, indent: usize) !void { switch (@typeInfo(T)) { .type => try writer.writeAll(@typeName(value)), .bool => try writer.print("{}", .{value}), .int, .comptime_int => try writer.print("{d}", .{value}), .float, .comptime_float => try writer.print("{d}", .{value}), .@"enum" => try writer.print(".{s}", .{@tagName(value)}), .optional => |info| if (value) |v| try writeZigLiteral(writer, info.child, v, indent) else try writer.writeAll("null"), .pointer => |info| switch (info.size) { .slice => if (info.child == u8) { try writeStringLiteral(writer, value); } else { try writer.writeAll("&.{\n"); inline for (value) |item| { try writeIndent(writer, indent + 1); try writeZigLiteral(writer, info.child, item, indent + 1); try writer.writeAll(",\n"); } try writeIndent(writer, indent); try writer.writeAll("}"); }, else => @compileError("codegen: unsupported pointer size for " ++ @typeName(T)), }, .@"struct" => |info| { try writer.writeAll(".{\n"); inline for (info.fields) |field| { try writeIndent(writer, indent + 1); try writer.print(".{s} = ", .{field.name}); try writeZigLiteral(writer, field.type, @field(value, field.name), indent + 1); try writer.writeAll(",\n"); } try writeIndent(writer, indent); try writer.writeAll("}"); }, else => @compileError("codegen: unsupported type " ++ @typeName(T)), }}
fn writeIndent(writer: *Writer, n: usize) !void { for (0..n) |_| try writer.writeAll(" ");}
fn writeStringLiteral(writer: *Writer, s: []const u8) !void { try writer.writeByte('"'); for (s) |c| { switch (c) { '\\' => try writer.writeAll("\\\\"), '"' => try writer.writeAll("\\\""), '\n' => try writer.writeAll("\\n"), '\r' => try writer.writeAll("\\r"), '\t' => try writer.writeAll("\\t"), else => if (c < 0x20) try writer.print("\\x{x:0>2}", .{c}) else try writer.writeByte(c), } } try writer.writeByte('"');}