authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-16 12:40:06-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-16 12:40:06-04:00
log79462bb5916d1e6459e73950dbab444e5b949853
treeb21df77ca4f84cdb6df417c88e6015a29637913a
parent74f7d710bb18b2ea40235af0ee25acdf189ea089
parent72b72faa0b055f08a01d19ae26180dd4df6fe383
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5354 from DrDeano/master

Add enum to addBuildOption

1 files changed, 34 insertions(+), 0 deletions(-)

lib/std/build.zig+34
......@@ -105,6 +105,7 @@ pub const Builder = struct {
105105 Bool,
106106 Int,
107107 Float,
108 Enum,
108109 String,
109110 List,
110111 };
......@@ -450,6 +451,27 @@ pub const Builder = struct {
450451 },
451452 TypeId.Int => panic("TODO integer options to build script", .{}),
452453 TypeId.Float => panic("TODO float options to build script", .{}),
454 TypeId.Enum => switch (entry.value.value) {
455 UserValue.Flag => {
456 warn("Expected -D{} to be a string, but received a boolean.\n", .{name});
457 self.markInvalidUserInput();
458 return null;
459 },
460 UserValue.Scalar => |s| {
461 if (std.meta.stringToEnum(T, s)) |enum_lit| {
462 return enum_lit;
463 } else {
464 warn("Expected -D{} to be of type {}.\n", .{ name, @typeName(T) });
465 self.markInvalidUserInput();
466 return null;
467 }
468 },
469 UserValue.List => {
470 warn("Expected -D{} to be a string, but received a list.\n", .{name});
471 self.markInvalidUserInput();
472 return null;
473 },
474 },
453475 TypeId.String => switch (entry.value.value) {
454476 UserValue.Flag => {
455477 warn("Expected -D{} to be a string, but received a boolean.\n", .{name});
......@@ -681,6 +703,7 @@ pub const Builder = struct {
681703 .Int => .Int,
682704 .Float => .Float,
683705 .Bool => .Bool,
706 .Enum => .Enum,
684707 else => switch (T) {
685708 []const u8 => .String,
686709 []const []const u8 => .List,
......@@ -698,6 +721,7 @@ pub const Builder = struct {
698721 .Bool => "bool",
699722 .Int => "int",
700723 .Float => "float",
724 .Enum => "enum",
701725 .String => "string",
702726 .List => "list",
703727 };
......@@ -1678,6 +1702,16 @@ pub const LibExeObjStep = struct {
16781702
16791703 pub fn addBuildOption(self: *LibExeObjStep, comptime T: type, name: []const u8, value: T) void {
16801704 const out = self.build_options_contents.outStream();
1705 switch (@typeInfo(T)) {
1706 .Enum => |enum_info| {
1707 out.print("const {} = enum {{\n", .{@typeName(T)}) catch unreachable;
1708 inline for (enum_info.fields) |field| {
1709 out.print(" {},\n", .{ field.name }) catch unreachable;
1710 }
1711 out.print("}};\n", .{}) catch unreachable;
1712 },
1713 else => {},
1714 }
16811715 out.print("pub const {} = {};\n", .{ name, value }) catch unreachable;
16821716 }
16831717