authorgravatar for rohlemF@gmail.comrohlem <rohlemF@gmail.com> 2022-02-04 19:44:38+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-05 03:17:07-05:00
logfbc06f9c9151205896fb167b087506d6580946c4
tree606a4577836fa35a6ca12b3838af8cc86d7861cb
parent7d04ab1f14269b25a7ac03c3f787b3f3ee3453c3

std.build.TranslateCStep: add C macro support

The string construction code is moved out of std.build.LibExeObjStep into std.build.constructCMacroArg, to allow reusing it elsewhere.

2 files changed, 36 insertions(+), 9 deletions(-)

lib/std/build.zig+17-9
......@@ -1862,15 +1862,7 @@ pub const LibExeObjStep = struct {
18621862 /// If the value is omitted, it is set to 1.
18631863 /// `name` and `value` need not live longer than the function call.
18641864 pub fn defineCMacro(self: *LibExeObjStep, name: []const u8, value: ?[]const u8) void {
1865 var macro = self.builder.allocator.alloc(
1866 u8,
1867 name.len + if (value) |value_slice| value_slice.len + 1 else 0,
1868 ) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable;
1869 mem.copy(u8, macro, name);
1870 if (value) |value_slice| {
1871 macro[name.len] = '=';
1872 mem.copy(u8, macro[name.len + 1 ..], value_slice);
1873 }
1865 const macro = constructCMacro(self.builder.allocator, name, value);
18741866 self.c_macros.append(macro) catch unreachable;
18751867 }
18761868
......@@ -2934,6 +2926,22 @@ pub const LibExeObjStep = struct {
29342926 }
29352927};
29362928
2929/// Allocates a new string for assigning a value to a named macro.
2930/// If the value is omitted, it is set to 1.
2931/// `name` and `value` need not live longer than the function call.
2932pub fn constructCMacro(allocator: Allocator, name: []const u8, value: ?[]const u8) []const u8 {
2933 var macro = allocator.alloc(
2934 u8,
2935 name.len + if (value) |value_slice| value_slice.len + 1 else 0,
2936 ) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable;
2937 mem.copy(u8, macro, name);
2938 if (value) |value_slice| {
2939 macro[name.len] = '=';
2940 mem.copy(u8, macro[name.len + 1 ..], value_slice);
2941 }
2942 return macro;
2943}
2944
29372945pub const InstallArtifactStep = struct {
29382946 pub const base_id = .install_artifact;
29392947
lib/std/build/TranslateCStep.zig+19
......@@ -16,6 +16,7 @@ step: Step,
1616builder: *Builder,
1717source: build.FileSource,
1818include_dirs: std.ArrayList([]const u8),
19c_macros: std.ArrayList([]const u8),
1920output_dir: ?[]const u8,
2021out_basename: []const u8,
2122target: CrossTarget = CrossTarget{},
......@@ -28,6 +29,7 @@ pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {
2829 .builder = builder,
2930 .source = source,
3031 .include_dirs = std.ArrayList([]const u8).init(builder.allocator),
32 .c_macros = std.ArrayList([]const u8).init(builder.allocator),
3133 .output_dir = null,
3234 .out_basename = undefined,
3335 .output_file = build.GeneratedFile{ .step = &self.step },
......@@ -53,6 +55,18 @@ pub fn addCheckFile(self: *TranslateCStep, expected_matches: []const []const u8)
5355 return CheckFileStep.create(self.builder, .{ .generated = &self.output_file }, self.builder.dupeStrings(expected_matches));
5456}
5557
58/// If the value is omitted, it is set to 1.
59/// `name` and `value` need not live longer than the function call.
60pub fn defineCMacro(self: *TranslateCStep, name: []const u8, value: ?[]const u8) void {
61 const macro = build.constructCMacro(self.builder.allocator, name, value);
62 self.c_macros.append(macro) catch unreachable;
63}
64
65/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1.
66pub fn defineCMacroRaw(self: *TranslateCStep, name_and_value: []const u8) void {
67 self.c_macros.append(self.builder.dupe(name_and_value)) catch unreachable;
68}
69
5670fn make(step: *Step) !void {
5771 const self = @fieldParentPtr(TranslateCStep, "step", step);
5872
......@@ -73,6 +87,11 @@ fn make(step: *Step) !void {
7387 try argv_list.append(include_dir);
7488 }
7589
90 for (self.c_macros.items) |c_macro| {
91 try argv_list.append("-D");
92 try argv_list.append(c_macro);
93 }
94
7695 try argv_list.append(self.source.getPath(self.builder));
7796
7897 const output_path_nl = try self.builder.execFromStep(argv_list.items, &self.step);