| ... | @@ -4,15 +4,22 @@ const Step = std.Build.Step; | ... | @@ -4,15 +4,22 @@ const Step = std.Build.Step; |
| 4 | | 4 | |
| 5 | pub const base_id: Step.Id = .config_header; | 5 | pub const base_id: Step.Id = .config_header; |
| 6 | | 6 | |
| 7 | pub const Style = enum { | 7 | pub const Style = union(enum) { |
| 8 | /// The configure format supported by autotools. It uses `#undef foo` to | 8 | /// The configure format supported by autotools. It uses `#undef foo` to |
| 9 | /// mark lines that can be substituted with different values. | 9 | /// mark lines that can be substituted with different values. |
| 10 | autoconf, | 10 | autoconf: std.Build.FileSource, |
| 11 | /// The configure format supported by CMake. It uses `@@FOO@@` and | 11 | /// The configure format supported by CMake. It uses `@@FOO@@` and |
| 12 | /// `#cmakedefine` for template substitution. | 12 | /// `#cmakedefine` for template substitution. |
| 13 | cmake, | 13 | cmake: std.Build.FileSource, |
| 14 | /// Generate a c header from scratch with the values passed. | 14 | /// Instead of starting with an input file, start with nothing. |
| 15 | generated, | 15 | blank, |
| | 16 | |
| | 17 | pub fn getFileSource(style: Style) ?std.Build.FileSource { |
| | 18 | switch (style) { |
| | 19 | .autoconf, .cmake => |s| return s, |
| | 20 | .blank => return null, |
| | 21 | } |
| | 22 | } |
| 16 | }; | 23 | }; |
| 17 | | 24 | |
| 18 | pub const Value = union(enum) { | 25 | pub const Value = union(enum) { |
| ... | @@ -26,91 +33,96 @@ pub const Value = union(enum) { | ... | @@ -26,91 +33,96 @@ pub const Value = union(enum) { |
| 26 | | 33 | |
| 27 | step: Step, | 34 | step: Step, |
| 28 | builder: *std.Build, | 35 | builder: *std.Build, |
| 29 | source: std.Build.FileSource, | 36 | values: std.StringArrayHashMap(Value), |
| | 37 | output_file: std.Build.GeneratedFile, |
| | 38 | |
| 30 | style: Style, | 39 | style: Style, |
| 31 | values: std.StringHashMap(Value), | 40 | max_bytes: usize, |
| 32 | gen_keys: std.ArrayList([]const u8), | 41 | include_path: []const u8, |
| 33 | gen_values: std.ArrayList(Value), | 42 | |
| 34 | max_bytes: usize = 2 * 1024 * 1024, | 43 | pub const Options = struct { |
| 35 | output_dir: []const u8, | 44 | style: Style = .blank, |
| 36 | output_path: []const u8, | 45 | max_bytes: usize = 2 * 1024 * 1024, |
| 37 | output_gen: std.build.GeneratedFile, | 46 | include_path: ?[]const u8 = null, |
| 38 | | 47 | }; |
| 39 | pub fn create(builder: *std.Build, source: std.Build.FileSource, style: Style) *ConfigHeaderStep { | 48 | |
| | 49 | pub fn create(builder: *std.Build, options: Options) *ConfigHeaderStep { |
| 40 | const self = builder.allocator.create(ConfigHeaderStep) catch @panic("OOM"); | 50 | const self = builder.allocator.create(ConfigHeaderStep) catch @panic("OOM"); |
| 41 | const name = builder.fmt("configure header {s}", .{source.getDisplayName()}); | 51 | const name = if (options.style.getFileSource()) |s| |
| | 52 | builder.fmt("configure {s} header {s}", .{ @tagName(options.style), s.getDisplayName() }) |
| | 53 | else |
| | 54 | builder.fmt("configure {s} header", .{@tagName(options.style)}); |
| 42 | self.* = .{ | 55 | self.* = .{ |
| 43 | .builder = builder, | 56 | .builder = builder, |
| 44 | .step = Step.init(base_id, name, builder.allocator, make), | 57 | .step = Step.init(base_id, name, builder.allocator, make), |
| 45 | .source = source, | 58 | .style = options.style, |
| 46 | .style = style, | 59 | .values = std.StringArrayHashMap(Value).init(builder.allocator), |
| 47 | .values = std.StringHashMap(Value).init(builder.allocator), | 60 | |
| 48 | .gen_keys = std.ArrayList([]const u8).init(builder.allocator), | 61 | .max_bytes = options.max_bytes, |
| 49 | .gen_values = std.ArrayList(Value).init(builder.allocator), | 62 | .include_path = "config.h", |
| 50 | .output_dir = undefined, | 63 | .output_file = .{ .step = &self.step }, |
| 51 | .output_path = "config.h", | | |
| 52 | .output_gen = std.build.GeneratedFile{ .step = &self.step }, | | |
| 53 | }; | 64 | }; |
| 54 | | 65 | |
| 55 | switch (source) { | 66 | if (options.style.getFileSource()) |s| switch (s) { |
| 56 | .path => |p| { | 67 | .path => |p| { |
| 57 | self.output_path = p; | 68 | const basename = std.fs.path.basename(p); |
| 58 | | 69 | if (std.mem.endsWith(u8, basename, ".h.in")) { |
| 59 | switch (style) { | 70 | self.include_path = basename[0 .. basename.len - 3]; |
| 60 | .autoconf, .cmake => { | | |
| 61 | if (std.mem.endsWith(u8, p, ".h.in")) { | | |
| 62 | self.output_path = p[0 .. p.len - 3]; | | |
| 63 | } | | |
| 64 | }, | | |
| 65 | else => {}, | | |
| 66 | } | 71 | } |
| 67 | }, | 72 | }, |
| 68 | else => {}, | 73 | else => {}, |
| | 74 | }; |
| | 75 | |
| | 76 | if (options.include_path) |include_path| { |
| | 77 | self.include_path = include_path; |
| 69 | } | 78 | } |
| 70 | | 79 | |
| 71 | return self; | 80 | return self; |
| 72 | } | 81 | } |
| 73 | | 82 | |
| 74 | pub fn getOutputSource(self: *ConfigHeaderStep) std.build.FileSource { | | |
| 75 | return std.build.FileSource{ .generated = &self.output_gen }; | | |
| 76 | } | | |
| 77 | | | |
| 78 | pub fn addValues(self: *ConfigHeaderStep, values: anytype) void { | 83 | pub fn addValues(self: *ConfigHeaderStep, values: anytype) void { |
| 79 | return addValuesInner(self, values) catch @panic("OOM"); | 84 | return addValuesInner(self, values) catch @panic("OOM"); |
| 80 | } | 85 | } |
| 81 | | 86 | |
| 82 | fn addValuesInner(self: *ConfigHeaderStep, values: anytype) !void { | 87 | fn addValuesInner(self: *ConfigHeaderStep, values: anytype) !void { |
| 83 | inline for (@typeInfo(@TypeOf(values)).Struct.fields) |field| { | 88 | inline for (@typeInfo(@TypeOf(values)).Struct.fields) |field| { |
| 84 | const val = try getValue(self, field.type, @field(values, field.name)); | 89 | try putValue(self, field.name, field.type, @field(values, field.name)); |
| 85 | switch (self.style) { | | |
| 86 | .generated => { | | |
| 87 | try self.gen_keys.append(field.name); | | |
| 88 | try self.gen_values.append(val); | | |
| 89 | }, | | |
| 90 | else => try self.values.put(field.name, val), | | |
| 91 | } | | |
| 92 | } | 90 | } |
| 93 | } | 91 | } |
| 94 | | 92 | |
| 95 | fn getValue(self: *ConfigHeaderStep, comptime T: type, v: T) !Value { | 93 | fn putValue(self: *ConfigHeaderStep, field_name: []const u8, comptime T: type, v: T) !void { |
| 96 | switch (@typeInfo(T)) { | 94 | switch (@typeInfo(T)) { |
| 97 | .Null => return .undef, | 95 | .Null => { |
| 98 | .Void => return .defined, | 96 | try self.values.put(field_name, .undef); |
| 99 | .Bool => return .{ .boolean = v }, | 97 | }, |
| 100 | .Int, .ComptimeInt => return .{ .int = v }, | 98 | .Void => { |
| 101 | .EnumLiteral => return .{ .ident = @tagName(v) }, | 99 | try self.values.put(field_name, .defined); |
| | 100 | }, |
| | 101 | .Bool => { |
| | 102 | try self.values.put(field_name, .{ .boolean = v }); |
| | 103 | }, |
| | 104 | .Int => { |
| | 105 | try self.values.put(field_name, .{ .int = v }); |
| | 106 | }, |
| | 107 | .ComptimeInt => { |
| | 108 | try self.values.put(field_name, .{ .int = v }); |
| | 109 | }, |
| | 110 | .EnumLiteral => { |
| | 111 | try self.values.put(field_name, .{ .ident = @tagName(v) }); |
| | 112 | }, |
| 102 | .Optional => { | 113 | .Optional => { |
| 103 | if (v) |x| { | 114 | if (v) |x| { |
| 104 | return getValue(self, @TypeOf(x), x); | 115 | return putValue(self, field_name, @TypeOf(x), x); |
| 105 | } else { | 116 | } else { |
| 106 | return .undef; | 117 | try self.values.put(field_name, .undef); |
| 107 | } | 118 | } |
| 108 | }, | 119 | }, |
| 109 | .Pointer => |ptr| { | 120 | .Pointer => |ptr| { |
| 110 | switch (@typeInfo(ptr.child)) { | 121 | switch (@typeInfo(ptr.child)) { |
| 111 | .Array => |array| { | 122 | .Array => |array| { |
| 112 | if (ptr.size == .One and array.child == u8) { | 123 | if (ptr.size == .One and array.child == u8) { |
| 113 | return .{ .string = v }; | 124 | try self.values.put(field_name, .{ .string = v }); |
| | 125 | return; |
| 114 | } | 126 | } |
| 115 | }, | 127 | }, |
| 116 | else => {}, | 128 | else => {}, |
| ... | @@ -125,11 +137,6 @@ fn getValue(self: *ConfigHeaderStep, comptime T: type, v: T) !Value { | ... | @@ -125,11 +137,6 @@ fn getValue(self: *ConfigHeaderStep, comptime T: type, v: T) !Value { |
| 125 | fn make(step: *Step) !void { | 137 | fn make(step: *Step) !void { |
| 126 | const self = @fieldParentPtr(ConfigHeaderStep, "step", step); | 138 | const self = @fieldParentPtr(ConfigHeaderStep, "step", step); |
| 127 | const gpa = self.builder.allocator; | 139 | const gpa = self.builder.allocator; |
| 128 | const src_path = self.source.getPath(self.builder); | | |
| 129 | const contents = switch (self.style) { | | |
| 130 | .generated => src_path, | | |
| 131 | else => try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes), | | |
| 132 | }; | | |
| 133 | | 140 | |
| 134 | // The cache is used here not really as a way to speed things up - because writing | 141 | // The cache is used here not really as a way to speed things up - because writing |
| 135 | // the data to a file would probably be very fast - but as a way to find a canonical | 142 | // the data to a file would probably be very fast - but as a way to find a canonical |
| ... | @@ -146,9 +153,30 @@ fn make(step: *Step) !void { | ... | @@ -146,9 +153,30 @@ fn make(step: *Step) !void { |
| 146 | // Random bytes to make ConfigHeaderStep unique. Refresh this with new | 153 | // Random bytes to make ConfigHeaderStep unique. Refresh this with new |
| 147 | // random bytes when ConfigHeaderStep implementation is modified in a | 154 | // random bytes when ConfigHeaderStep implementation is modified in a |
| 148 | // non-backwards-compatible way. | 155 | // non-backwards-compatible way. |
| 149 | var hash = Hasher.init("X1pQzdDt91Zlh7Eh"); | 156 | var hash = Hasher.init("PGuDTpidxyMqnkGM"); |
| 150 | hash.update(self.source.getDisplayName()); | 157 | |
| 151 | hash.update(contents); | 158 | var output = std.ArrayList(u8).init(gpa); |
| | 159 | defer output.deinit(); |
| | 160 | |
| | 161 | try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n"); |
| | 162 | |
| | 163 | switch (self.style) { |
| | 164 | .autoconf => |file_source| { |
| | 165 | const src_path = file_source.getPath(self.builder); |
| | 166 | const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes); |
| | 167 | try render_autoconf(contents, &output, self.values, src_path); |
| | 168 | }, |
| | 169 | .cmake => |file_source| { |
| | 170 | const src_path = file_source.getPath(self.builder); |
| | 171 | const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes); |
| | 172 | try render_cmake(contents, &output, self.values, src_path); |
| | 173 | }, |
| | 174 | .blank => { |
| | 175 | try render_blank(&output, self.values, self.include_path); |
| | 176 | }, |
| | 177 | } |
| | 178 | |
| | 179 | hash.update(output.items); |
| 152 | | 180 | |
| 153 | var digest: [16]u8 = undefined; | 181 | var digest: [16]u8 = undefined; |
| 154 | hash.final(&digest); | 182 | hash.final(&digest); |
| ... | @@ -159,7 +187,7 @@ fn make(step: *Step) !void { | ... | @@ -159,7 +187,7 @@ fn make(step: *Step) !void { |
| 159 | .{std.fmt.fmtSliceHexLower(&digest)}, | 187 | .{std.fmt.fmtSliceHexLower(&digest)}, |
| 160 | ) catch unreachable; | 188 | ) catch unreachable; |
| 161 | | 189 | |
| 162 | self.output_dir = try std.fs.path.join(gpa, &[_][]const u8{ | 190 | const output_dir = try std.fs.path.join(gpa, &[_][]const u8{ |
| 163 | self.builder.cache_root, "o", &hash_basename, | 191 | self.builder.cache_root, "o", &hash_basename, |
| 164 | }); | 192 | }); |
| 165 | | 193 | |
| ... | @@ -168,45 +196,33 @@ fn make(step: *Step) !void { | ... | @@ -168,45 +196,33 @@ fn make(step: *Step) !void { |
| 168 | // output_path is libavutil/avconfig.h | 196 | // output_path is libavutil/avconfig.h |
| 169 | // We want to open directory zig-cache/o/HASH/libavutil/ | 197 | // We want to open directory zig-cache/o/HASH/libavutil/ |
| 170 | // but keep output_dir as zig-cache/o/HASH for -I include | 198 | // but keep output_dir as zig-cache/o/HASH for -I include |
| 171 | var outdir = self.output_dir; | 199 | const sub_dir_path = if (std.fs.path.dirname(self.include_path)) |d| |
| 172 | var outpath = self.output_path; | 200 | try std.fs.path.join(gpa, &.{ output_dir, d }) |
| 173 | if (std.fs.path.dirname(self.output_path)) |d| { | 201 | else |
| 174 | outdir = try std.fs.path.join(gpa, &[_][]const u8{ self.output_dir, d }); | 202 | output_dir; |
| 175 | outpath = std.fs.path.basename(self.output_path); | | |
| 176 | } | | |
| 177 | | 203 | |
| 178 | var dir = std.fs.cwd().makeOpenPath(outdir, .{}) catch |err| { | 204 | var dir = std.fs.cwd().makeOpenPath(sub_dir_path, .{}) catch |err| { |
| 179 | std.debug.print("unable to make path {s}: {s}\n", .{ outdir, @errorName(err) }); | 205 | std.debug.print("unable to make path {s}: {s}\n", .{ output_dir, @errorName(err) }); |
| 180 | return err; | 206 | return err; |
| 181 | }; | 207 | }; |
| 182 | defer dir.close(); | 208 | defer dir.close(); |
| 183 | | 209 | |
| 184 | var values_copy = try self.values.clone(); | 210 | try dir.writeFile(std.fs.path.basename(self.include_path), output.items); |
| 185 | defer values_copy.deinit(); | | |
| 186 | | | |
| 187 | var output = std.ArrayList(u8).init(gpa); | | |
| 188 | defer output.deinit(); | | |
| 189 | try output.ensureTotalCapacity(contents.len); | | |
| 190 | | | |
| 191 | try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n"); | | |
| 192 | | | |
| 193 | switch (self.style) { | | |
| 194 | .autoconf => try render_autoconf(contents, &output, &values_copy, src_path), | | |
| 195 | .cmake => try render_cmake(contents, &output, &values_copy, src_path), | | |
| 196 | .generated => try render_generated(gpa, &output, &self.gen_keys, &self.gen_values, self.source.getDisplayName()), | | |
| 197 | } | | |
| 198 | | 211 | |
| 199 | try dir.writeFile(outpath, output.items); | 212 | self.output_file.path = try std.fs.path.join(self.builder.allocator, &.{ |
| 200 | | 213 | output_dir, self.include_path, |
| 201 | self.output_gen.path = try std.fs.path.join(gpa, &[_][]const u8{ self.output_dir, self.output_path }); | 214 | }); |
| 202 | } | 215 | } |
| 203 | | 216 | |
| 204 | fn render_autoconf( | 217 | fn render_autoconf( |
| 205 | contents: []const u8, | 218 | contents: []const u8, |
| 206 | output: *std.ArrayList(u8), | 219 | output: *std.ArrayList(u8), |
| 207 | values_copy: *std.StringHashMap(Value), | 220 | values: std.StringArrayHashMap(Value), |
| 208 | src_path: []const u8, | 221 | src_path: []const u8, |
| 209 | ) !void { | 222 | ) !void { |
| | 223 | var values_copy = try values.clone(); |
| | 224 | defer values_copy.deinit(); |
| | 225 | |
| 210 | var any_errors = false; | 226 | var any_errors = false; |
| 211 | var line_index: u32 = 0; | 227 | var line_index: u32 = 0; |
| 212 | var line_it = std.mem.split(u8, contents, "\n"); | 228 | var line_it = std.mem.split(u8, contents, "\n"); |
| ... | @@ -224,7 +240,7 @@ fn render_autoconf( | ... | @@ -224,7 +240,7 @@ fn render_autoconf( |
| 224 | continue; | 240 | continue; |
| 225 | } | 241 | } |
| 226 | const name = it.rest(); | 242 | const name = it.rest(); |
| 227 | const kv = values_copy.fetchRemove(name) orelse { | 243 | const kv = values_copy.fetchSwapRemove(name) orelse { |
| 228 | std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{ | 244 | std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{ |
| 229 | src_path, line_index + 1, name, | 245 | src_path, line_index + 1, name, |
| 230 | }); | 246 | }); |
| ... | @@ -234,12 +250,8 @@ fn render_autoconf( | ... | @@ -234,12 +250,8 @@ fn render_autoconf( |
| 234 | try renderValue(output, name, kv.value); | 250 | try renderValue(output, name, kv.value); |
| 235 | } | 251 | } |
| 236 | | 252 | |
| 237 | { | 253 | for (values_copy.keys()) |name| { |
| 238 | var it = values_copy.iterator(); | 254 | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); |
| 239 | while (it.next()) |entry| { | | |
| 240 | const name = entry.key_ptr.*; | | |
| 241 | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); | | |
| 242 | } | | |
| 243 | } | 255 | } |
| 244 | | 256 | |
| 245 | if (any_errors) { | 257 | if (any_errors) { |
| ... | @@ -250,9 +262,12 @@ fn render_autoconf( | ... | @@ -250,9 +262,12 @@ fn render_autoconf( |
| 250 | fn render_cmake( | 262 | fn render_cmake( |
| 251 | contents: []const u8, | 263 | contents: []const u8, |
| 252 | output: *std.ArrayList(u8), | 264 | output: *std.ArrayList(u8), |
| 253 | values_copy: *std.StringHashMap(Value), | 265 | values: std.StringArrayHashMap(Value), |
| 254 | src_path: []const u8, | 266 | src_path: []const u8, |
| 255 | ) !void { | 267 | ) !void { |
| | 268 | var values_copy = try values.clone(); |
| | 269 | defer values_copy.deinit(); |
| | 270 | |
| 256 | var any_errors = false; | 271 | var any_errors = false; |
| 257 | var line_index: u32 = 0; | 272 | var line_index: u32 = 0; |
| 258 | var line_it = std.mem.split(u8, contents, "\n"); | 273 | var line_it = std.mem.split(u8, contents, "\n"); |
| ... | @@ -276,7 +291,7 @@ fn render_cmake( | ... | @@ -276,7 +291,7 @@ fn render_cmake( |
| 276 | any_errors = true; | 291 | any_errors = true; |
| 277 | continue; | 292 | continue; |
| 278 | }; | 293 | }; |
| 279 | const kv = values_copy.fetchRemove(name) orelse { | 294 | const kv = values_copy.fetchSwapRemove(name) orelse { |
| 280 | std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{ | 295 | std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{ |
| 281 | src_path, line_index + 1, name, | 296 | src_path, line_index + 1, name, |
| 282 | }); | 297 | }); |
| ... | @@ -286,12 +301,8 @@ fn render_cmake( | ... | @@ -286,12 +301,8 @@ fn render_cmake( |
| 286 | try renderValue(output, name, kv.value); | 301 | try renderValue(output, name, kv.value); |
| 287 | } | 302 | } |
| 288 | | 303 | |
| 289 | { | 304 | for (values_copy.keys()) |name| { |
| 290 | var it = values_copy.iterator(); | 305 | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); |
| 291 | while (it.next()) |entry| { | | |
| 292 | const name = entry.key_ptr.*; | | |
| 293 | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); | | |
| 294 | } | | |
| 295 | } | 306 | } |
| 296 | | 307 | |
| 297 | if (any_errors) { | 308 | if (any_errors) { |
| ... | @@ -299,6 +310,36 @@ fn render_cmake( | ... | @@ -299,6 +310,36 @@ fn render_cmake( |
| 299 | } | 310 | } |
| 300 | } | 311 | } |
| 301 | | 312 | |
| | 313 | fn render_blank( |
| | 314 | output: *std.ArrayList(u8), |
| | 315 | defines: std.StringArrayHashMap(Value), |
| | 316 | include_path: []const u8, |
| | 317 | ) !void { |
| | 318 | const include_guard_name = try output.allocator.dupe(u8, include_path); |
| | 319 | for (include_guard_name) |*byte| { |
| | 320 | switch (byte.*) { |
| | 321 | 'a'...'z' => byte.* = byte.* - 'a' + 'A', |
| | 322 | 'A'...'Z', '0'...'9' => continue, |
| | 323 | else => byte.* = '_', |
| | 324 | } |
| | 325 | } |
| | 326 | |
| | 327 | try output.appendSlice("#ifndef "); |
| | 328 | try output.appendSlice(include_guard_name); |
| | 329 | try output.appendSlice("\n#define "); |
| | 330 | try output.appendSlice(include_guard_name); |
| | 331 | try output.appendSlice("\n"); |
| | 332 | |
| | 333 | const values = defines.values(); |
| | 334 | for (defines.keys()) |name, i| { |
| | 335 | try renderValue(output, name, values[i]); |
| | 336 | } |
| | 337 | |
| | 338 | try output.appendSlice("#endif /* "); |
| | 339 | try output.appendSlice(include_guard_name); |
| | 340 | try output.appendSlice(" */\n"); |
| | 341 | } |
| | 342 | |
| 302 | fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { | 343 | fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { |
| 303 | switch (value) { | 344 | switch (value) { |
| 304 | .undef => { | 345 | .undef => { |
| ... | @@ -329,31 +370,3 @@ fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void | ... | @@ -329,31 +370,3 @@ fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void |
| 329 | }, | 370 | }, |
| 330 | } | 371 | } |
| 331 | } | 372 | } |
| 332 | | | |
| 333 | fn render_generated( | | |
| 334 | gpa: std.mem.Allocator, | | |
| 335 | output: *std.ArrayList(u8), | | |
| 336 | keys: *std.ArrayList([]const u8), | | |
| 337 | values: *std.ArrayList(Value), | | |
| 338 | src_path: []const u8, | | |
| 339 | ) !void { | | |
| 340 | var include_guard = try gpa.dupe(u8, src_path); | | |
| 341 | defer gpa.free(include_guard); | | |
| 342 | | | |
| 343 | for (include_guard) |*ch| { | | |
| 344 | if (ch.* == '.' or std.fs.path.isSep(ch.*)) { | | |
| 345 | ch.* = '_'; | | |
| 346 | } else { | | |
| 347 | ch.* = std.ascii.toUpper(ch.*); | | |
| 348 | } | | |
| 349 | } | | |
| 350 | | | |
| 351 | try output.writer().print("#ifndef {s}\n", .{include_guard}); | | |
| 352 | try output.writer().print("#define {s}\n", .{include_guard}); | | |
| 353 | | | |
| 354 | for (keys.items) |k, i| { | | |
| 355 | try renderValue(output, k, values.items[i]); | | |
| 356 | } | | |
| 357 | | | |
| 358 | try output.writer().print("#endif /* {s} */\n", .{include_guard}); | | |
| 359 | } | | |