From fd24eb952f442b8c4ec8b13c177682b7140d1bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20C=C3=A1ceres?= Date: Wed, 27 Sep 2023 23:03:17 +0000 Subject: [PATCH 1/2] std.Build.ConfigHeader: Override include guard for blank This commit adds an optional field in the blank Style to allow for overriding the default header guard that is generated from the output file path. --- lib/std/Build/Step/ConfigHeader.zig | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig index 35e92ee04ecc..9135f0502920 100644 --- a/lib/std/Build/Step/ConfigHeader.zig +++ b/lib/std/Build/Step/ConfigHeader.zig @@ -11,7 +11,9 @@ pub const Style = union(enum) { /// `#cmakedefine` for template substitution. cmake: std.Build.LazyPath, /// Instead of starting with an input file, start with nothing. - blank, + blank: struct { + include_guard_override: ?[]const u8 = null, + }, /// Start with nothing, like blank, and output a nasm .asm file. nasm, @@ -46,7 +48,7 @@ include_path: []const u8, pub const base_id: Step.Id = .config_header; pub const Options = struct { - style: Style = .blank, + style: Style = .{ .blank = .{} }, max_bytes: usize = 2 * 1024 * 1024, include_path: ?[]const u8 = null, first_ret_addr: ?usize = null, @@ -199,9 +201,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { const contents = try std.fs.cwd().readFileAlloc(arena, src_path, self.max_bytes); try render_cmake(step, contents, &output, self.values, src_path); }, - .blank => { + .blank => |blank| { try output.appendSlice(c_generated_line); - try render_blank(&output, self.values, self.include_path); + try render_blank(&output, self.values, self.include_path, blank.include_guard_override); }, .nasm => { try output.appendSlice(asm_generated_line); @@ -415,15 +417,19 @@ fn render_blank( output: *std.ArrayList(u8), defines: std.StringArrayHashMap(Value), include_path: []const u8, + include_guard_override: ?[]const u8, ) !void { - const include_guard_name = try output.allocator.dupe(u8, include_path); - for (include_guard_name) |*byte| { - switch (byte.*) { - 'a'...'z' => byte.* = byte.* - 'a' + 'A', - 'A'...'Z', '0'...'9' => continue, - else => byte.* = '_', + const include_guard_name = include_guard_override orelse blk: { + const name = try output.allocator.dupe(u8, include_path); + for (name) |*byte| { + switch (byte.*) { + 'a'...'z' => byte.* = byte.* - 'a' + 'A', + 'A'...'Z', '0'...'9' => continue, + else => byte.* = '_', + } } - } + break :blk name; + }; try output.appendSlice("#ifndef "); try output.appendSlice(include_guard_name); From 92d73c3195989e906adf0dd72422b77931db8ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20C=C3=A1ceres?= Date: Thu, 28 Sep 2023 00:04:31 +0000 Subject: [PATCH 2/2] std.Build.ConfigHeader: Backwards compatible guard override As suggested by Andrew, this commit moves the guard override to the Options struct such that existing build scripts remain source backwards compatible. --- lib/std/Build/Step/ConfigHeader.zig | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig index 9135f0502920..f2e90e02f9c6 100644 --- a/lib/std/Build/Step/ConfigHeader.zig +++ b/lib/std/Build/Step/ConfigHeader.zig @@ -11,9 +11,7 @@ pub const Style = union(enum) { /// `#cmakedefine` for template substitution. cmake: std.Build.LazyPath, /// Instead of starting with an input file, start with nothing. - blank: struct { - include_guard_override: ?[]const u8 = null, - }, + blank, /// Start with nothing, like blank, and output a nasm .asm file. nasm, @@ -44,14 +42,16 @@ output_file: std.Build.GeneratedFile, style: Style, max_bytes: usize, include_path: []const u8, +include_guard_override: ?[]const u8, pub const base_id: Step.Id = .config_header; pub const Options = struct { - style: Style = .{ .blank = .{} }, + style: Style = .blank, max_bytes: usize = 2 * 1024 * 1024, include_path: ?[]const u8 = null, first_ret_addr: ?usize = null, + include_guard_override: ?[]const u8 = null, }; pub fn create(owner: *std.Build, options: Options) *ConfigHeader { @@ -93,6 +93,7 @@ pub fn create(owner: *std.Build, options: Options) *ConfigHeader { .max_bytes = options.max_bytes, .include_path = include_path, + .include_guard_override = options.include_guard_override, .output_file = .{ .step = &self.step }, }; @@ -201,9 +202,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { const contents = try std.fs.cwd().readFileAlloc(arena, src_path, self.max_bytes); try render_cmake(step, contents, &output, self.values, src_path); }, - .blank => |blank| { + .blank => { try output.appendSlice(c_generated_line); - try render_blank(&output, self.values, self.include_path, blank.include_guard_override); + try render_blank(&output, self.values, self.include_path, self.include_guard_override); }, .nasm => { try output.appendSlice(asm_generated_line);