| author | |
| committer | |
| log | fbc06f9c9151205896fb167b087506d6580946c4 |
| tree | 606a4577836fa35a6ca12b3838af8cc86d7861cb |
| parent | 7d04ab1f14269b25a7ac03c3f787b3f3ee3453c3 |
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 { |
| 1862 | 1862 | /// If the value is omitted, it is set to 1. |
| 1863 | 1863 | /// `name` and `value` need not live longer than the function call. |
| 1864 | 1864 | 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); | |
| 1874 | 1866 | self.c_macros.append(macro) catch unreachable; |
| 1875 | 1867 | } |
| 1876 | 1868 | |
| ... | ... | @@ -2934,6 +2926,22 @@ pub const LibExeObjStep = struct { |
| 2934 | 2926 | } |
| 2935 | 2927 | }; |
| 2936 | 2928 | |
| 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. | |
| 2932 | pub 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 | ||
| 2937 | 2945 | pub const InstallArtifactStep = struct { |
| 2938 | 2946 | pub const base_id = .install_artifact; |
| 2939 | 2947 |
lib/std/build/TranslateCStep.zig+19| ... | ... | @@ -16,6 +16,7 @@ step: Step, |
| 16 | 16 | builder: *Builder, |
| 17 | 17 | source: build.FileSource, |
| 18 | 18 | include_dirs: std.ArrayList([]const u8), |
| 19 | c_macros: std.ArrayList([]const u8), | |
| 19 | 20 | output_dir: ?[]const u8, |
| 20 | 21 | out_basename: []const u8, |
| 21 | 22 | target: CrossTarget = CrossTarget{}, |
| ... | ... | @@ -28,6 +29,7 @@ pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep { |
| 28 | 29 | .builder = builder, |
| 29 | 30 | .source = source, |
| 30 | 31 | .include_dirs = std.ArrayList([]const u8).init(builder.allocator), |
| 32 | .c_macros = std.ArrayList([]const u8).init(builder.allocator), | |
| 31 | 33 | .output_dir = null, |
| 32 | 34 | .out_basename = undefined, |
| 33 | 35 | .output_file = build.GeneratedFile{ .step = &self.step }, |
| ... | ... | @@ -53,6 +55,18 @@ pub fn addCheckFile(self: *TranslateCStep, expected_matches: []const []const u8) |
| 53 | 55 | return CheckFileStep.create(self.builder, .{ .generated = &self.output_file }, self.builder.dupeStrings(expected_matches)); |
| 54 | 56 | } |
| 55 | 57 | |
| 58 | /// If the value is omitted, it is set to 1. | |
| 59 | /// `name` and `value` need not live longer than the function call. | |
| 60 | pub 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. | |
| 66 | pub 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 | ||
| 56 | 70 | fn make(step: *Step) !void { |
| 57 | 71 | const self = @fieldParentPtr(TranslateCStep, "step", step); |
| 58 | 72 | |
| ... | ... | @@ -73,6 +87,11 @@ fn make(step: *Step) !void { |
| 73 | 87 | try argv_list.append(include_dir); |
| 74 | 88 | } |
| 75 | 89 | |
| 90 | for (self.c_macros.items) |c_macro| { | |
| 91 | try argv_list.append("-D"); | |
| 92 | try argv_list.append(c_macro); | |
| 93 | } | |
| 94 | ||
| 76 | 95 | try argv_list.append(self.source.getPath(self.builder)); |
| 77 | 96 | |
| 78 | 97 | const output_path_nl = try self.builder.execFromStep(argv_list.items, &self.step); |