Skip to content

Commit acac685

Browse files
authored
std.Build.ConfigHeader: override include guard option for blank style (#17310)
This commit adds an option to allow for overriding the default header guard that is generated from the output file path.
1 parent 8f2f12f commit acac685

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lib/std/Build/Step/ConfigHeader.zig

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ output_file: std.Build.GeneratedFile,
4242
style: Style,
4343
max_bytes: usize,
4444
include_path: []const u8,
45+
include_guard_override: ?[]const u8,
4546

4647
pub const base_id: Step.Id = .config_header;
4748

@@ -50,6 +51,7 @@ pub const Options = struct {
5051
max_bytes: usize = 2 * 1024 * 1024,
5152
include_path: ?[]const u8 = null,
5253
first_ret_addr: ?usize = null,
54+
include_guard_override: ?[]const u8 = null,
5355
};
5456

5557
pub fn create(owner: *std.Build, options: Options) *ConfigHeader {
@@ -91,6 +93,7 @@ pub fn create(owner: *std.Build, options: Options) *ConfigHeader {
9193

9294
.max_bytes = options.max_bytes,
9395
.include_path = include_path,
96+
.include_guard_override = options.include_guard_override,
9497
.output_file = .{ .step = &self.step },
9598
};
9699

@@ -201,7 +204,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
201204
},
202205
.blank => {
203206
try output.appendSlice(c_generated_line);
204-
try render_blank(&output, self.values, self.include_path);
207+
try render_blank(&output, self.values, self.include_path, self.include_guard_override);
205208
},
206209
.nasm => {
207210
try output.appendSlice(asm_generated_line);
@@ -415,15 +418,19 @@ fn render_blank(
415418
output: *std.ArrayList(u8),
416419
defines: std.StringArrayHashMap(Value),
417420
include_path: []const u8,
421+
include_guard_override: ?[]const u8,
418422
) !void {
419-
const include_guard_name = try output.allocator.dupe(u8, include_path);
420-
for (include_guard_name) |*byte| {
421-
switch (byte.*) {
422-
'a'...'z' => byte.* = byte.* - 'a' + 'A',
423-
'A'...'Z', '0'...'9' => continue,
424-
else => byte.* = '_',
423+
const include_guard_name = include_guard_override orelse blk: {
424+
const name = try output.allocator.dupe(u8, include_path);
425+
for (name) |*byte| {
426+
switch (byte.*) {
427+
'a'...'z' => byte.* = byte.* - 'a' + 'A',
428+
'A'...'Z', '0'...'9' => continue,
429+
else => byte.* = '_',
430+
}
425431
}
426-
}
432+
break :blk name;
433+
};
427434

428435
try output.appendSlice("#ifndef ");
429436
try output.appendSlice(include_guard_name);

0 commit comments

Comments
 (0)