authorgravatar for asaizeren@gmail.comAsa Zeren <asaizeren@gmail.com> 2021-03-23 18:58:24-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-11 20:18:19+03:00
log6cc88458029759bbedcb4d949deb887d464cdd60
tree1e31fcbe7cd4a2cbb2d1d652d6358825554ad1f0
parent0bde5ce369ba59f7bc33e97cd5b03d69913108c5

Change defineCMacro to take separate name and value arugments

Before this change, when one or more of name or value are not known at comptime, build.zig files must allocate and do the concatanation, which can be cumbersome, and also adds a redundant allocation when name and value are slices. The new version only does a single allocation directly in the builder's allocator to concatonate name and value. The origional behavior is available in defineCMacroRaw, for use in situations such as parseing c compiler arguments. Additionally, several places have been updated to use the new funtions.

2 files changed, 19 insertions(+), 4 deletions(-)

build.zig+1-1
...@@ -117,7 +117,7 @@ pub fn build(b: *Builder) !void {...@@ -117,7 +117,7 @@ pub fn build(b: *Builder) !void {
117 // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that117 // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that
118 // is pointless.118 // is pointless.
119 exe.addPackagePath("compiler_rt", "src/empty.zig");119 exe.addPackagePath("compiler_rt", "src/empty.zig");
120 exe.defineCMacro("ZIG_LINK_MODE=Static");120 exe.defineCMacro("ZIG_LINK_MODE", "Static");
121121
122 const softfloat = b.addStaticLibrary("softfloat", null);122 const softfloat = b.addStaticLibrary("softfloat", null);
123 softfloat.setBuildMode(.ReleaseFast);123 softfloat.setBuildMode(.ReleaseFast);
lib/std/build.zig+18-3
...@@ -1700,8 +1700,23 @@ pub const LibExeObjStep = struct {...@@ -1700,8 +1700,23 @@ pub const LibExeObjStep = struct {
1700 }1700 }
1701 }1701 }
17021702
1703 /// If the value is omitted, it is set to 1.
1704 /// `name` and `value` need not live longer than the function call.
1705 pub fn defineCMacro(self: *LibExeObjStep, name: []const u8, value: ?[]const u8) void {
1706 var macro = self.builder.allocator.alloc(
1707 u8,
1708 name.len + if (value) |value_slice| value_slice.len + 1 else 0,
1709 ) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable;
1710 mem.copy(u8, macro, name);
1711 if (value) |value_slice| {
1712 macro[name.len] = '=';
1713 mem.copy(u8, macro[name.len + 1 ..], value_slice);
1714 }
1715 self.c_macros.append(macro) catch unreachable;
1716 }
1717
1703 /// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1.1718 /// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1.
1704 pub fn defineCMacro(self: *LibExeObjStep, name_and_value: []const u8) void {1719 pub fn defineCMacroRaw(self: *LibExeObjStep, name_and_value: []const u8) void {
1705 self.c_macros.append(self.builder.dupe(name_and_value)) catch unreachable;1720 self.c_macros.append(self.builder.dupe(name_and_value)) catch unreachable;
1706 }1721 }
17071722
...@@ -1790,9 +1805,9 @@ pub const LibExeObjStep = struct {...@@ -1790,9 +1805,9 @@ pub const LibExeObjStep = struct {
1790 self.linkSystemLibraryName(tok["-l".len..]);1805 self.linkSystemLibraryName(tok["-l".len..]);
1791 } else if (mem.eql(u8, tok, "-D")) {1806 } else if (mem.eql(u8, tok, "-D")) {
1792 const macro = it.next() orelse return error.PkgConfigInvalidOutput;1807 const macro = it.next() orelse return error.PkgConfigInvalidOutput;
1793 self.defineCMacro(macro);1808 self.defineCMacroRaw(macro);
1794 } else if (mem.startsWith(u8, tok, "-D")) {1809 } else if (mem.startsWith(u8, tok, "-D")) {
1795 self.defineCMacro(tok["-D".len..]);1810 self.defineCMacroRaw(tok["-D".len..]);
1796 } else if (mem.eql(u8, tok, "-pthread")) {1811 } else if (mem.eql(u8, tok, "-pthread")) {
1797 self.linkLibC();1812 self.linkLibC();
1798 } else if (self.builder.verbose) {1813 } else if (self.builder.verbose) {