diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-02-13 17:23:19 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-13 17:23:19 -0500 |
| commit | a9e1cf3049d1d90d7e62950c3b7f4b088081caa8 (patch) | |
| tree | 22fe9f9f10ab0b6088124949068e6aeb83162a57 /lib/std/Build/ConfigHeaderStep.zig | |
| parent | fc48467a97021cb872ff2a947f96e882274c39c1 (diff) | |
| parent | 35bb823131a93d7407e79897d4cd20ae2a98ca54 (diff) | |
| download | zig-a9e1cf3049d1d90d7e62950c3b7f4b088081caa8.tar.gz zig-a9e1cf3049d1d90d7e62950c3b7f4b088081caa8.zip | |
Merge pull request #14571 from ziglang/more-build-zig
std.Build.ConfigHeaderStep: support sentinel-terminated strings
Diffstat (limited to 'lib/std/Build/ConfigHeaderStep.zig')
| -rw-r--r-- | lib/std/Build/ConfigHeaderStep.zig | 74 |
1 files changed, 65 insertions, 9 deletions
diff --git a/lib/std/Build/ConfigHeaderStep.zig b/lib/std/Build/ConfigHeaderStep.zig index ca4d69dfa9..f8d6f7bd57 100644 --- a/lib/std/Build/ConfigHeaderStep.zig +++ b/lib/std/Build/ConfigHeaderStep.zig @@ -13,11 +13,13 @@ pub const Style = union(enum) { cmake: std.Build.FileSource, /// Instead of starting with an input file, start with nothing. blank, + /// Start with nothing, like blank, and output a nasm .asm file. + nasm, pub fn getFileSource(style: Style) ?std.Build.FileSource { switch (style) { .autoconf, .cmake => |s| return s, - .blank => return null, + .blank, .nasm => return null, } } }; @@ -84,6 +86,10 @@ pub fn addValues(self: *ConfigHeaderStep, values: anytype) void { return addValuesInner(self, values) catch @panic("OOM"); } +pub fn getFileSource(self: *ConfigHeaderStep) std.Build.FileSource { + return .{ .generated = &self.output_file }; +} + fn addValuesInner(self: *ConfigHeaderStep, values: anytype) !void { inline for (@typeInfo(@TypeOf(values)).Struct.fields) |field| { try putValue(self, field.name, field.type, @field(values, field.name)); @@ -125,6 +131,12 @@ fn putValue(self: *ConfigHeaderStep, field_name: []const u8, comptime T: type, v return; } }, + .Int => { + if (ptr.size == .Slice and ptr.child == u8) { + try self.values.put(field_name, .{ .string = v }); + return; + } + }, else => {}, } @@ -158,22 +170,31 @@ fn make(step: *Step) !void { var output = std.ArrayList(u8).init(gpa); defer output.deinit(); - try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n"); + const header_text = "This file was generated by ConfigHeaderStep using the Zig Build System."; + const c_generated_line = "/* " ++ header_text ++ " */\n"; + const asm_generated_line = "; " ++ header_text ++ "\n"; switch (self.style) { .autoconf => |file_source| { + try output.appendSlice(c_generated_line); const src_path = file_source.getPath(self.builder); const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes); try render_autoconf(contents, &output, self.values, src_path); }, .cmake => |file_source| { + try output.appendSlice(c_generated_line); const src_path = file_source.getPath(self.builder); const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes); try render_cmake(contents, &output, self.values, src_path); }, .blank => { + try output.appendSlice(c_generated_line); try render_blank(&output, self.values, self.include_path); }, + .nasm => { + try output.appendSlice(asm_generated_line); + try render_nasm(&output, self.values); + }, } hash.update(output.items); @@ -187,9 +208,7 @@ fn make(step: *Step) !void { .{std.fmt.fmtSliceHexLower(&digest)}, ) catch unreachable; - const output_dir = try std.fs.path.join(gpa, &[_][]const u8{ - self.builder.cache_root, "o", &hash_basename, - }); + const output_dir = try self.builder.cache_root.join(gpa, &.{ "o", &hash_basename }); // If output_path has directory parts, deal with them. Example: // output_dir is zig-cache/o/HASH @@ -247,7 +266,7 @@ fn render_autoconf( any_errors = true; continue; }; - try renderValue(output, name, kv.value); + try renderValueC(output, name, kv.value); } for (values_copy.keys()) |name| { @@ -298,7 +317,7 @@ fn render_cmake( any_errors = true; continue; }; - try renderValue(output, name, kv.value); + try renderValueC(output, name, kv.value); } for (values_copy.keys()) |name| { @@ -332,7 +351,7 @@ fn render_blank( const values = defines.values(); for (defines.keys()) |name, i| { - try renderValue(output, name, values[i]); + try renderValueC(output, name, values[i]); } try output.appendSlice("#endif /* "); @@ -340,7 +359,14 @@ fn render_blank( try output.appendSlice(" */\n"); } -fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { +fn render_nasm(output: *std.ArrayList(u8), defines: std.StringArrayHashMap(Value)) !void { + const values = defines.values(); + for (defines.keys()) |name, i| { + try renderValueNasm(output, name, values[i]); + } +} + +fn renderValueC(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { switch (value) { .undef => { try output.appendSlice("/* #undef "); @@ -370,3 +396,33 @@ fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void }, } } + +fn renderValueNasm(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { + switch (value) { + .undef => { + try output.appendSlice("; %undef "); + try output.appendSlice(name); + try output.appendSlice("\n"); + }, + .defined => { + try output.appendSlice("%define "); + try output.appendSlice(name); + try output.appendSlice("\n"); + }, + .boolean => |b| { + try output.appendSlice("%define "); + try output.appendSlice(name); + try output.appendSlice(if (b) " 1\n" else " 0\n"); + }, + .int => |i| { + try output.writer().print("%define {s} {d}\n", .{ name, i }); + }, + .ident => |ident| { + try output.writer().print("%define {s} {s}\n", .{ name, ident }); + }, + .string => |string| { + // TODO: use nasm-specific escaping instead of zig string literals + try output.writer().print("%define {s} \"{}\"\n", .{ name, std.zig.fmtEscapes(string) }); + }, + } +} |
