Merge pull request #22941 from Techatrix/config-header

std.Build.Step.ConfigHeader: improve handling of autoconf style headers
This commit is contained in:
Andrew Kelley 2025-02-22 00:25:09 -05:00 committed by GitHub
commit a7467b9bb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 88 additions and 8 deletions

View File

@ -131,7 +131,7 @@ fn putValue(config_header: *ConfigHeader, field_name: []const u8, comptime T: ty
.comptime_int => {
try config_header.values.put(field_name, .{ .int = v });
},
.enum_literal => {
.@"enum", .enum_literal => {
try config_header.values.put(field_name, .{ .ident = @tagName(v) });
},
.optional => {
@ -264,8 +264,11 @@ fn render_autoconf(
values: std.StringArrayHashMap(Value),
src_path: []const u8,
) !void {
var values_copy = try values.clone();
defer values_copy.deinit();
const build = step.owner;
const allocator = build.allocator;
var is_used: std.DynamicBitSetUnmanaged = try .initEmpty(allocator, values.count());
defer is_used.deinit(allocator);
var any_errors = false;
var line_index: u32 = 0;
@ -283,19 +286,21 @@ fn render_autoconf(
try output.appendSlice("\n");
continue;
}
const name = it.rest();
const kv = values_copy.fetchSwapRemove(name) orelse {
const name = it.next().?;
const index = values.getIndex(name) orelse {
try step.addError("{s}:{d}: error: unspecified config header value: '{s}'", .{
src_path, line_index + 1, name,
});
any_errors = true;
continue;
};
try renderValueC(output, name, kv.value);
is_used.set(index);
try renderValueC(output, name, values.values()[index]);
}
for (values_copy.keys()) |name| {
try step.addError("{s}: error: config header value unused: '{s}'", .{ src_path, name });
var unused_value_it = is_used.iterator(.{ .kind = .unset });
while (unused_value_it.next()) |index| {
try step.addError("{s}: error: config header value unused: '{s}'", .{ src_path, values.keys()[index] });
any_errors = true;
}

View File

@ -186,6 +186,9 @@
.omit_cfi = .{
.path = "omit_cfi",
},
.config_header = .{
.path = "config_header",
},
},
.paths = .{
"build.zig",

View File

@ -0,0 +1,28 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const config_header = b.addConfigHeader(
.{ .style = .{ .autoconf = b.path("config.h.in") } },
.{
.SOME_NO = null,
.SOME_TRUE = true,
.SOME_FALSE = false,
.SOME_ZERO = 0,
.SOME_ONE = 1,
.SOME_TEN = 10,
.SOME_ENUM = @as(enum { foo, bar }, .foo),
.SOME_ENUM_LITERAL = .@"test",
.SOME_STRING = "test",
.PREFIX_SPACE = null,
.PREFIX_TAB = null,
.POSTFIX_SPACE = null,
.POSTFIX_TAB = null,
},
);
const check_config_header = b.addCheckFile(config_header.getOutput(), .{ .expected_exact = @embedFile("config.h") });
const test_step = b.step("test", "Test it");
test_step.dependOn(&check_config_header.step);
}

View File

@ -0,0 +1,23 @@
/* This file was generated by ConfigHeader using the Zig Build System. */
/* Some Comment */
int foo();
/* #undef SOME_NO */
#define SOME_TRUE 1
#define SOME_FALSE 0
#define SOME_ZERO 0
#define SOME_ONE 1
#define SOME_TEN 10
#define SOME_ENUM foo
#define SOME_ENUM_LITERAL test
#define SOME_STRING "test"
// Used twice
#define SOME_TRUE 1
/* #undef PREFIX_SPACE */
/* #undef PREFIX_TAB */
/* #undef POSTFIX_SPACE */
/* #undef POSTFIX_TAB */

View File

@ -0,0 +1,21 @@
/* Some Comment */
int foo();
#undef SOME_NO
#undef SOME_TRUE
#undef SOME_FALSE
#undef SOME_ZERO
#undef SOME_ONE
#undef SOME_TEN
#undef SOME_ENUM
#undef SOME_ENUM_LITERAL
#undef SOME_STRING
// Used twice
#undef SOME_TRUE
#undef PREFIX_SPACE
#undef PREFIX_TAB
#undef POSTFIX_SPACE
#undef POSTFIX_TAB