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,...@@ -14,6 +14,7 @@ generated_file: GeneratedFile,
1414
15contents: std.ArrayList(u8),15contents: std.ArrayList(u8),
16args: std.ArrayList(Arg),16args: std.ArrayList(Arg),
17encountered_types: std.StringHashMap(void),
1718
18pub fn create(owner: *std.Build) *Options {19pub fn create(owner: *std.Build) *Options {
19 const self = owner.allocator.create(Options) catch @panic("OOM");20 const self = owner.allocator.create(Options) catch @panic("OOM");
...@@ -27,6 +28,7 @@ pub fn create(owner: *std.Build) *Options {...@@ -27,6 +28,7 @@ pub fn create(owner: *std.Build) *Options {
27 .generated_file = undefined,28 .generated_file = undefined,
28 .contents = std.ArrayList(u8).init(owner.allocator),29 .contents = std.ArrayList(u8).init(owner.allocator),
29 .args = std.ArrayList(Arg).init(owner.allocator),30 .args = std.ArrayList(Arg).init(owner.allocator),
31 .encountered_types = std.StringHashMap(void).init(owner.allocator),
30 };32 };
31 self.generated_file = .{ .step = &self.step };33 self.generated_file = .{ .step = &self.step };
3234
...@@ -101,11 +103,14 @@ fn addOptionFallible(self: *Options, comptime T: type, name: []const u8, value:...@@ -101,11 +103,14 @@ fn addOptionFallible(self: *Options, comptime T: type, name: []const u8, value:
101 }103 }
102 switch (@typeInfo(T)) {104 switch (@typeInfo(T)) {
103 .Enum => |enum_info| {105 .Enum => |enum_info| {
104 try out.print("pub const {} = enum {{\n", .{std.zig.fmtId(@typeName(T))});106 const gop = try self.encountered_types.getOrPut(@typeName(T));
105 inline for (enum_info.fields) |field| {107 if (!gop.found_existing) {
106 try out.print(" {},\n", .{std.zig.fmtId(field.name)});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 try out.print("pub const {}: {s} = .{s};\n", .{114 try out.print("pub const {}: {s} = .{s};\n", .{
110 std.zig.fmtId(name),115 std.zig.fmtId(name),
111 std.zig.fmtId(@typeName(T)),116 std.zig.fmtId(@typeName(T)),
...@@ -322,6 +327,11 @@ test Options {...@@ -322,6 +327,11 @@ test Options {
322 @"0.8.1",327 @"0.8.1",
323 };328 };
324329
330 const NormalEnum = enum {
331 foo,
332 bar,
333 };
334
325 const nested_array = [2][2]u16{335 const nested_array = [2][2]u16{
326 [2]u16{ 300, 200 },336 [2]u16{ 300, 200 },
327 [2]u16{ 300, 200 },337 [2]u16{ 300, 200 },
...@@ -338,6 +348,8 @@ test Options {...@@ -338,6 +348,8 @@ test Options {
338 options.addOption([]const []const u16, "nested_slice", nested_slice);348 options.addOption([]const []const u16, "nested_slice", nested_slice);
339 options.addOption(KeywordEnum, "keyword_enum", .@"0.8.1");349 options.addOption(KeywordEnum, "keyword_enum", .@"0.8.1");
340 options.addOption(std.SemanticVersion, "semantic_version", try std.SemanticVersion.parse("0.1.2-foo+bar"));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);
341353
342 try std.testing.expectEqualStrings(354 try std.testing.expectEqualStrings(
343 \\pub const option1: usize = 1;355 \\pub const option1: usize = 1;
...@@ -377,6 +389,12 @@ test Options {...@@ -377,6 +389,12 @@ test Options {
377 \\ .pre = "foo",389 \\ .pre = "foo",
378 \\ .build = "bar",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 , options.contents.items);399 , options.contents.items);
382400