authorgravatar for aaron@sikes.ioAaron Sikes <aaron@sikes.io> 2021-10-23 14:23:58-04:00
committergravatar for aaron@sikes.ioAaron Sikes <aaron@sikes.io> 2021-10-23 14:25:02-04:00
log76d4b1b823a6fbca20e24b37c4be97a5541eebf4
tree8ff6350ed52358cb8fa12948416baef70321257d
parent808d1b84a8349dc62597b22c7093df61fa3585d3

Better erroring for unsupported build option types


1 files changed, 17 insertions(+), 2 deletions(-)

lib/std/build/OptionsStep.zig+17-2
...@@ -129,11 +129,11 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value:...@@ -129,11 +129,11 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value:
129 out.writeAll(";\n") catch unreachable;129 out.writeAll(";\n") catch unreachable;
130}130}
131131
132// TODO: non-recursive?
132fn printLiteral(out: anytype, val: anytype, indent: u8) !void {133fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
133 const T = @TypeOf(val);134 const T = @TypeOf(val);
134 switch (@typeInfo(T)) {135 switch (@typeInfo(T)) {
135 .Array => {136 .Array => {
136 // TODO: non-recursive?
137 try out.print("{s} {{\n", .{@typeName(T)});137 try out.print("{s} {{\n", .{@typeName(T)});
138 for (val) |item| {138 for (val) |item| {
139 try out.writeByteNTimes(' ', indent + 4);139 try out.writeByteNTimes(' ', indent + 4);
...@@ -156,7 +156,20 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void {...@@ -156,7 +156,20 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
156 try out.writeByteNTimes(' ', indent);156 try out.writeByteNTimes(' ', indent);
157 try out.writeAll("}");157 try out.writeAll("}");
158 },158 },
159 else => try out.print("{any}", .{val}),159 .Optional => {
160 if (val) |inner| {
161 return printLiteral(out, inner, indent);
162 } else {
163 return out.writeAll("null");
164 }
165 },
166 .Void,
167 .Bool,
168 .Int,
169 .Float,
170 .Null,
171 => try out.print("{any}", .{val}),
172 else => @compileError(comptime std.fmt.comptimePrint("`{s}` are not yet supported as build options", .{@tagName(@typeInfo(T))})),
160 }173 }
161}174}
162175
...@@ -283,6 +296,7 @@ test "OptionsStep" {...@@ -283,6 +296,7 @@ test "OptionsStep" {
283296
284 options.addOption(usize, "option1", 1);297 options.addOption(usize, "option1", 1);
285 options.addOption(?usize, "option2", null);298 options.addOption(?usize, "option2", null);
299 options.addOption(?usize, "option3", 3);
286 options.addOption([]const u8, "string", "zigisthebest");300 options.addOption([]const u8, "string", "zigisthebest");
287 options.addOption(?[]const u8, "optional_string", null);301 options.addOption(?[]const u8, "optional_string", null);
288 options.addOption([2][2]u16, "nested_array", nested_array);302 options.addOption([2][2]u16, "nested_array", nested_array);
...@@ -294,6 +308,7 @@ test "OptionsStep" {...@@ -294,6 +308,7 @@ test "OptionsStep" {
294 try std.testing.expectEqualStrings(308 try std.testing.expectEqualStrings(
295 \\pub const option1: usize = 1;309 \\pub const option1: usize = 1;
296 \\pub const option2: ?usize = null;310 \\pub const option2: ?usize = null;
311 \\pub const option3: ?usize = 3;
297 \\pub const string: []const u8 = "zigisthebest";312 \\pub const string: []const u8 = "zigisthebest";
298 \\pub const optional_string: ?[]const u8 = null;313 \\pub const optional_string: ?[]const u8 = null;
299 \\pub const nested_array: [2][2]u16 = [2][2]u16 {314 \\pub const nested_array: [2][2]u16 = [2][2]u16 {