diff options
| author | rohlem <rohlemF@gmail.com> | 2022-02-04 19:44:38 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-02-05 03:17:07 -0500 |
| commit | fbc06f9c9151205896fb167b087506d6580946c4 (patch) | |
| tree | 606a4577836fa35a6ca12b3838af8cc86d7861cb /lib | |
| parent | 7d04ab1f14269b25a7ac03c3f787b3f3ee3453c3 (diff) | |
| download | zig-fbc06f9c9151205896fb167b087506d6580946c4.tar.gz zig-fbc06f9c9151205896fb167b087506d6580946c4.zip | |
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.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/build.zig | 26 | ||||
| -rw-r--r-- | lib/std/build/TranslateCStep.zig | 19 |
2 files changed, 36 insertions, 9 deletions
diff --git a/lib/std/build.zig b/lib/std/build.zig index 395ccc5cb4..104aceea76 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -1862,15 +1862,7 @@ pub const LibExeObjStep = struct { /// If the value is omitted, it is set to 1. /// `name` and `value` need not live longer than the function call. pub fn defineCMacro(self: *LibExeObjStep, name: []const u8, value: ?[]const u8) void { - var macro = self.builder.allocator.alloc( - u8, - name.len + if (value) |value_slice| value_slice.len + 1 else 0, - ) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable; - mem.copy(u8, macro, name); - if (value) |value_slice| { - macro[name.len] = '='; - mem.copy(u8, macro[name.len + 1 ..], value_slice); - } + const macro = constructCMacro(self.builder.allocator, name, value); self.c_macros.append(macro) catch unreachable; } @@ -2934,6 +2926,22 @@ pub const LibExeObjStep = struct { } }; +/// Allocates a new string for assigning a value to a named macro. +/// If the value is omitted, it is set to 1. +/// `name` and `value` need not live longer than the function call. +pub fn constructCMacro(allocator: Allocator, name: []const u8, value: ?[]const u8) []const u8 { + var macro = allocator.alloc( + u8, + name.len + if (value) |value_slice| value_slice.len + 1 else 0, + ) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable; + mem.copy(u8, macro, name); + if (value) |value_slice| { + macro[name.len] = '='; + mem.copy(u8, macro[name.len + 1 ..], value_slice); + } + return macro; +} + pub const InstallArtifactStep = struct { pub const base_id = .install_artifact; diff --git a/lib/std/build/TranslateCStep.zig b/lib/std/build/TranslateCStep.zig index 0d44ebd80a..1f9bee463c 100644 --- a/lib/std/build/TranslateCStep.zig +++ b/lib/std/build/TranslateCStep.zig @@ -16,6 +16,7 @@ step: Step, builder: *Builder, source: build.FileSource, include_dirs: std.ArrayList([]const u8), +c_macros: std.ArrayList([]const u8), output_dir: ?[]const u8, out_basename: []const u8, target: CrossTarget = CrossTarget{}, @@ -28,6 +29,7 @@ pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep { .builder = builder, .source = source, .include_dirs = std.ArrayList([]const u8).init(builder.allocator), + .c_macros = std.ArrayList([]const u8).init(builder.allocator), .output_dir = null, .out_basename = undefined, .output_file = build.GeneratedFile{ .step = &self.step }, @@ -53,6 +55,18 @@ pub fn addCheckFile(self: *TranslateCStep, expected_matches: []const []const u8) return CheckFileStep.create(self.builder, .{ .generated = &self.output_file }, self.builder.dupeStrings(expected_matches)); } +/// If the value is omitted, it is set to 1. +/// `name` and `value` need not live longer than the function call. +pub fn defineCMacro(self: *TranslateCStep, name: []const u8, value: ?[]const u8) void { + const macro = build.constructCMacro(self.builder.allocator, name, value); + self.c_macros.append(macro) catch unreachable; +} + +/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1. +pub fn defineCMacroRaw(self: *TranslateCStep, name_and_value: []const u8) void { + self.c_macros.append(self.builder.dupe(name_and_value)) catch unreachable; +} + fn make(step: *Step) !void { const self = @fieldParentPtr(TranslateCStep, "step", step); @@ -73,6 +87,11 @@ fn make(step: *Step) !void { try argv_list.append(include_dir); } + for (self.c_macros.items) |c_macro| { + try argv_list.append("-D"); + try argv_list.append(c_macro); + } + try argv_list.append(self.source.getPath(self.builder)); const output_path_nl = try self.builder.execFromStep(argv_list.items, &self.step); |
