diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-05-16 12:40:06 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-16 12:40:06 -0400 |
| commit | 79462bb5916d1e6459e73950dbab444e5b949853 (patch) | |
| tree | b21df77ca4f84cdb6df417c88e6015a29637913a /lib | |
| parent | 74f7d710bb18b2ea40235af0ee25acdf189ea089 (diff) | |
| parent | 72b72faa0b055f08a01d19ae26180dd4df6fe383 (diff) | |
| download | zig-79462bb5916d1e6459e73950dbab444e5b949853.tar.gz zig-79462bb5916d1e6459e73950dbab444e5b949853.zip | |
Merge pull request #5354 from DrDeano/master
Add enum to addBuildOption
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/build.zig | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/std/build.zig b/lib/std/build.zig index 67f2af1047..a2da6315fe 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -105,6 +105,7 @@ pub const Builder = struct { Bool, Int, Float, + Enum, String, List, }; @@ -450,6 +451,27 @@ pub const Builder = struct { }, TypeId.Int => panic("TODO integer options to build script", .{}), TypeId.Float => panic("TODO float options to build script", .{}), + TypeId.Enum => switch (entry.value.value) { + UserValue.Flag => { + warn("Expected -D{} to be a string, but received a boolean.\n", .{name}); + self.markInvalidUserInput(); + return null; + }, + UserValue.Scalar => |s| { + if (std.meta.stringToEnum(T, s)) |enum_lit| { + return enum_lit; + } else { + warn("Expected -D{} to be of type {}.\n", .{ name, @typeName(T) }); + self.markInvalidUserInput(); + return null; + } + }, + UserValue.List => { + warn("Expected -D{} to be a string, but received a list.\n", .{name}); + self.markInvalidUserInput(); + return null; + }, + }, TypeId.String => switch (entry.value.value) { UserValue.Flag => { warn("Expected -D{} to be a string, but received a boolean.\n", .{name}); @@ -681,6 +703,7 @@ pub const Builder = struct { .Int => .Int, .Float => .Float, .Bool => .Bool, + .Enum => .Enum, else => switch (T) { []const u8 => .String, []const []const u8 => .List, @@ -698,6 +721,7 @@ pub const Builder = struct { .Bool => "bool", .Int => "int", .Float => "float", + .Enum => "enum", .String => "string", .List => "list", }; @@ -1678,6 +1702,16 @@ pub const LibExeObjStep = struct { pub fn addBuildOption(self: *LibExeObjStep, comptime T: type, name: []const u8, value: T) void { const out = self.build_options_contents.outStream(); + switch (@typeInfo(T)) { + .Enum => |enum_info| { + out.print("const {} = enum {{\n", .{@typeName(T)}) catch unreachable; + inline for (enum_info.fields) |field| { + out.print(" {},\n", .{ field.name }) catch unreachable; + } + out.print("}};\n", .{}) catch unreachable; + }, + else => {}, + } out.print("pub const {} = {};\n", .{ name, value }) catch unreachable; } |
