authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2024-01-14 21:21:10+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-15 23:08:11-08:00
log7116b02210bcae7bafac9017a7e47a14b52eafc2
treec5a6b561a4c28a3f57d45dfbeb3d74d92b826b6f
parent88ee323cd79612999f1e7c645df3391849c6d7f4

Allow multiple options using the same enum type


1 files changed, 22 insertions(+), 4 deletions(-)

lib/std/Build/Step/Options.zig+22-4
......@@ -14,6 +14,7 @@ generated_file: GeneratedFile,
1414
1515contents: std.ArrayList(u8),
1616args: std.ArrayList(Arg),
17encountered_types: std.StringHashMap(void),
1718
1819pub fn create(owner: *std.Build) *Options {
1920 const self = owner.allocator.create(Options) catch @panic("OOM");
......@@ -27,6 +28,7 @@ pub fn create(owner: *std.Build) *Options {
2728 .generated_file = undefined,
2829 .contents = std.ArrayList(u8).init(owner.allocator),
2930 .args = std.ArrayList(Arg).init(owner.allocator),
31 .encountered_types = std.StringHashMap(void).init(owner.allocator),
3032 };
3133 self.generated_file = .{ .step = &self.step };
3234
......@@ -101,11 +103,14 @@ fn addOptionFallible(self: *Options, comptime T: type, name: []const u8, value:
101103 }
102104 switch (@typeInfo(T)) {
103105 .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");
107113 }
108 try out.writeAll("};\n");
109114 try out.print("pub const {}: {s} = .{s};\n", .{
110115 std.zig.fmtId(name),
111116 std.zig.fmtId(@typeName(T)),
......@@ -322,6 +327,11 @@ test Options {
322327 @"0.8.1",
323328 };
324329
330 const NormalEnum = enum {
331 foo,
332 bar,
333 };
334
325335 const nested_array = [2][2]u16{
326336 [2]u16{ 300, 200 },
327337 [2]u16{ 300, 200 },
......@@ -338,6 +348,8 @@ test Options {
338348 options.addOption([]const []const u16, "nested_slice", nested_slice);
339349 options.addOption(KeywordEnum, "keyword_enum", .@"0.8.1");
340350 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);
341353
342354 try std.testing.expectEqualStrings(
343355 \\pub const option1: usize = 1;
......@@ -377,6 +389,12 @@ test Options {
377389 \\ .pre = "foo",
378390 \\ .build = "bar",
379391 \\};
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;
380398 \\
381399 , options.contents.items);
382400