authorgravatar for aaron@sikes.ioAaron Sikes <aaron@sikes.io> 2021-10-23 13:30:20-04:00
committergravatar for aaron@sikes.ioAaron Sikes <aaron@sikes.io> 2021-10-23 13:30:20-04:00
log72039f234921884024a9163dd222f433fc6af7c3
treecfbf872e8f92993c887e66efb7ea13ad76c54ed4
parentf98f5e0bcd5b6e726227e3c0c72c2efae8dd21c6

Allow arbitrary arrays in build options


1 files changed, 32 insertions(+), 1 deletions(-)

lib/std/build/OptionsStep.zig+32-1
......@@ -124,7 +124,27 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value:
124124 },
125125 else => {},
126126 }
127 out.print("pub const {}: {s} = {};\n", .{ std.zig.fmtId(name), @typeName(T), value }) catch unreachable;
127 out.print("pub const {}: {s} = ", .{ std.zig.fmtId(name), @typeName(T) }) catch unreachable;
128 printLiteral(out, value, 0) catch unreachable;
129 out.writeAll(";\n") catch unreachable;
130}
131
132fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
133 const T = @TypeOf(val);
134 switch (@typeInfo(T)) {
135 .Array => {
136 // TODO: non-recursive?
137 try out.print("{s}{{\n", .{@typeName(T)});
138 for (val) |item| {
139 try out.writeByteNTimes(' ', indent + 4);
140 try printLiteral(out, item, indent + 4);
141 try out.writeAll(",\n");
142 }
143 try out.writeByteNTimes(' ', indent);
144 try out.writeAll("}");
145 },
146 else => try out.print("{any}", .{val}),
147 }
128148}
129149
130150/// The value is the path in the cache dir.
......@@ -246,6 +266,7 @@ test "OptionsStep" {
246266 options.addOption(?usize, "option2", null);
247267 options.addOption([]const u8, "string", "zigisthebest");
248268 options.addOption(?[]const u8, "optional_string", null);
269 options.addOption([2][2]u16, "array", [2][2]u16{ [2]u16{ 300, 200 }, [2]u16{ 300, 200 } });
249270 options.addOption(KeywordEnum, "keyword_enum", .@"0.8.1");
250271 options.addOption(std.builtin.Version, "version", try std.builtin.Version.parse("0.1.2"));
251272 options.addOption(std.SemanticVersion, "semantic_version", try std.SemanticVersion.parse("0.1.2-foo+bar"));
......@@ -255,6 +276,16 @@ test "OptionsStep" {
255276 \\pub const option2: ?usize = null;
256277 \\pub const string: []const u8 = "zigisthebest";
257278 \\pub const optional_string: ?[]const u8 = null;
279 \\pub const array: [2][2]u16 = [2][2]u16{
280 \\ [2]u16{
281 \\ 300,
282 \\ 200,
283 \\ },
284 \\ [2]u16{
285 \\ 300,
286 \\ 200,
287 \\ },
288 \\};
258289 \\pub const KeywordEnum = enum {
259290 \\ @"0.8.1",
260291 \\};