std.Build.ConfigHeaderStep: support outputting assembly config files

This commit is contained in:
Andrew Kelley
2023-02-05 16:45:26 -07:00
parent dad6039092
commit b061cc9e3f

View File

@@ -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));
@@ -164,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);
@@ -253,7 +268,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| {
@@ -304,7 +319,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| {
@@ -338,7 +353,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 /* ");
@@ -346,7 +361,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 ");
@@ -376,3 +398,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) });
},
}
}