Commit 38d8aab4d2

Michael Holmes <i.am@ceph3.us>
2021-04-05 23:54:04
std/build: fix ?[:0]const u8 build options
As per the other string types, `?[:0]const u8` needs its own case as otherwise it will raise an error about using `{}` with slices. There's no reasonable workaround for this, as you would have to either discount the use of the empty string value or manually rework the string to be sentinel-terminated at runtime. It's useful for passing build options to code making use of C libraries that make strong use of sentinel-terminated arrays for strings.
1 parent 89df41e
Changed files (1)
lib
lib/std/build.zig
@@ -1939,6 +1939,15 @@ pub const LibExeObjStep = struct {
                 out.print("pub const {}: []const u8 = \"{}\";\n", .{ std.zig.fmtId(name), std.zig.fmtEscapes(value) }) catch unreachable;
                 return;
             },
+            ?[:0]const u8 => {
+                out.print("pub const {}: ?[:0]const u8 = ", .{std.zig.fmtId(name)}) catch unreachable;
+                if (value) |payload| {
+                    out.print("\"{}\";\n", .{std.zig.fmtEscapes(payload)}) catch unreachable;
+                } else {
+                    out.writeAll("null;\n") catch unreachable;
+                }
+                return;
+            },
             ?[]const u8 => {
                 out.print("pub const {}: ?[]const u8 = ", .{std.zig.fmtId(name)}) catch unreachable;
                 if (value) |payload| {