| ... | ... | @@ -14,6 +14,7 @@ generated_file: GeneratedFile, |
| 14 | 14 | |
| 15 | 15 | contents: std.ArrayList(u8), |
| 16 | 16 | args: std.ArrayList(Arg), |
| 17 | encountered_types: std.StringHashMap(void), |
| 17 | 18 | |
| 18 | 19 | pub fn create(owner: *std.Build) *Options { |
| 19 | 20 | const self = owner.allocator.create(Options) catch @panic("OOM"); |
| ... | ... | @@ -27,6 +28,7 @@ pub fn create(owner: *std.Build) *Options { |
| 27 | 28 | .generated_file = undefined, |
| 28 | 29 | .contents = std.ArrayList(u8).init(owner.allocator), |
| 29 | 30 | .args = std.ArrayList(Arg).init(owner.allocator), |
| 31 | .encountered_types = std.StringHashMap(void).init(owner.allocator), |
| 30 | 32 | }; |
| 31 | 33 | self.generated_file = .{ .step = &self.step }; |
| 32 | 34 | |
| ... | ... | @@ -101,11 +103,14 @@ fn addOptionFallible(self: *Options, comptime T: type, name: []const u8, value: |
| 101 | 103 | } |
| 102 | 104 | switch (@typeInfo(T)) { |
| 103 | 105 | .Enum => |enum_info| { |
| 104 | | try out.print("pub const {} = enum {{\n", .{std.zig.fmtId(@typeName(T))}); |
| 105 | | inline for (enum_info.fields) |field| { |
| 106 | | try out.print(" {},\n", .{std.zig.fmtId(field.name)}); |
| 106 | const gop = try self.encountered_types.getOrPut(@typeName(T)); |
| 107 | if (!gop.found_existing) { |
| 108 | try out.print("pub const {} = enum {{\n", .{std.zig.fmtId(@typeName(T))}); |
| 109 | inline for (enum_info.fields) |field| { |
| 110 | try out.print(" {},\n", .{std.zig.fmtId(field.name)}); |
| 111 | } |
| 112 | try out.writeAll("};\n"); |
| 107 | 113 | } |
| 108 | | try out.writeAll("};\n"); |
| 109 | 114 | try out.print("pub const {}: {s} = .{s};\n", .{ |
| 110 | 115 | std.zig.fmtId(name), |
| 111 | 116 | std.zig.fmtId(@typeName(T)), |
| ... | ... | @@ -322,6 +327,11 @@ test Options { |
| 322 | 327 | @"0.8.1", |
| 323 | 328 | }; |
| 324 | 329 | |
| 330 | const NormalEnum = enum { |
| 331 | foo, |
| 332 | bar, |
| 333 | }; |
| 334 | |
| 325 | 335 | const nested_array = [2][2]u16{ |
| 326 | 336 | [2]u16{ 300, 200 }, |
| 327 | 337 | [2]u16{ 300, 200 }, |
| ... | ... | @@ -338,6 +348,8 @@ test Options { |
| 338 | 348 | options.addOption([]const []const u16, "nested_slice", nested_slice); |
| 339 | 349 | options.addOption(KeywordEnum, "keyword_enum", .@"0.8.1"); |
| 340 | 350 | options.addOption(std.SemanticVersion, "semantic_version", try std.SemanticVersion.parse("0.1.2-foo+bar")); |
| 351 | options.addOption(NormalEnum, "normal1", NormalEnum.foo); |
| 352 | options.addOption(NormalEnum, "normal2", NormalEnum.bar); |
| 341 | 353 | |
| 342 | 354 | try std.testing.expectEqualStrings( |
| 343 | 355 | \\pub const option1: usize = 1; |
| ... | ... | @@ -377,6 +389,12 @@ test Options { |
| 377 | 389 | \\ .pre = "foo", |
| 378 | 390 | \\ .build = "bar", |
| 379 | 391 | \\}; |
| 392 | \\pub const @"Build.Step.Options.decltest.Options.NormalEnum" = enum { |
| 393 | \\ foo, |
| 394 | \\ bar, |
| 395 | \\}; |
| 396 | \\pub const normal1: @"Build.Step.Options.decltest.Options.NormalEnum" = .foo; |
| 397 | \\pub const normal2: @"Build.Step.Options.decltest.Options.NormalEnum" = .bar; |
| 380 | 398 | \\ |
| 381 | 399 | , options.contents.items); |
| 382 | 400 | |