commit a7467b9bb2aa667a8d34bc8b678ce35fcb19ebd4 (tree)
parent 300cb4881f675140d89a8bbd889d51490701fbac
Author: Andrew Kelley <andrew@ziglang.org>
Date: Sat, 22 Feb 2025 00:25:09 -0500
Merge pull request #22941 from Techatrix/config-header
std.Build.Step.ConfigHeader: improve handling of autoconf style headers
Diffstat:
5 files changed, 88 insertions(+), 8 deletions(-)
diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig
@@ -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;
}
diff --git a/test/standalone/build.zig.zon b/test/standalone/build.zig.zon
@@ -186,6 +186,9 @@
.omit_cfi = .{
.path = "omit_cfi",
},
+ .config_header = .{
+ .path = "config_header",
+ },
},
.paths = .{
"build.zig",
diff --git a/test/standalone/config_header/build.zig b/test/standalone/config_header/build.zig
@@ -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);
+}
diff --git a/test/standalone/config_header/config.h b/test/standalone/config_header/config.h
@@ -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 */
+
diff --git a/test/standalone/config_header/config.h.in b/test/standalone/config_header/config.h.in
@@ -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