Skip to content

Commit a7467b9

Browse files
authored
Merge pull request #22941 from Techatrix/config-header
std.Build.Step.ConfigHeader: improve handling of autoconf style headers
2 parents 300cb48 + c1c6f08 commit a7467b9

File tree

5 files changed

+88
-8
lines changed

5 files changed

+88
-8
lines changed

lib/std/Build/Step/ConfigHeader.zig

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn putValue(config_header: *ConfigHeader, field_name: []const u8, comptime T: ty
131131
.comptime_int => {
132132
try config_header.values.put(field_name, .{ .int = v });
133133
},
134-
.enum_literal => {
134+
.@"enum", .enum_literal => {
135135
try config_header.values.put(field_name, .{ .ident = @tagName(v) });
136136
},
137137
.optional => {
@@ -264,8 +264,11 @@ fn render_autoconf(
264264
values: std.StringArrayHashMap(Value),
265265
src_path: []const u8,
266266
) !void {
267-
var values_copy = try values.clone();
268-
defer values_copy.deinit();
267+
const build = step.owner;
268+
const allocator = build.allocator;
269+
270+
var is_used: std.DynamicBitSetUnmanaged = try .initEmpty(allocator, values.count());
271+
defer is_used.deinit(allocator);
269272

270273
var any_errors = false;
271274
var line_index: u32 = 0;
@@ -283,19 +286,21 @@ fn render_autoconf(
283286
try output.appendSlice("\n");
284287
continue;
285288
}
286-
const name = it.rest();
287-
const kv = values_copy.fetchSwapRemove(name) orelse {
289+
const name = it.next().?;
290+
const index = values.getIndex(name) orelse {
288291
try step.addError("{s}:{d}: error: unspecified config header value: '{s}'", .{
289292
src_path, line_index + 1, name,
290293
});
291294
any_errors = true;
292295
continue;
293296
};
294-
try renderValueC(output, name, kv.value);
297+
is_used.set(index);
298+
try renderValueC(output, name, values.values()[index]);
295299
}
296300

297-
for (values_copy.keys()) |name| {
298-
try step.addError("{s}: error: config header value unused: '{s}'", .{ src_path, name });
301+
var unused_value_it = is_used.iterator(.{ .kind = .unset });
302+
while (unused_value_it.next()) |index| {
303+
try step.addError("{s}: error: config header value unused: '{s}'", .{ src_path, values.keys()[index] });
299304
any_errors = true;
300305
}
301306

test/standalone/build.zig.zon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@
186186
.omit_cfi = .{
187187
.path = "omit_cfi",
188188
},
189+
.config_header = .{
190+
.path = "config_header",
191+
},
189192
},
190193
.paths = .{
191194
"build.zig",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const config_header = b.addConfigHeader(
5+
.{ .style = .{ .autoconf = b.path("config.h.in") } },
6+
.{
7+
.SOME_NO = null,
8+
.SOME_TRUE = true,
9+
.SOME_FALSE = false,
10+
.SOME_ZERO = 0,
11+
.SOME_ONE = 1,
12+
.SOME_TEN = 10,
13+
.SOME_ENUM = @as(enum { foo, bar }, .foo),
14+
.SOME_ENUM_LITERAL = .@"test",
15+
.SOME_STRING = "test",
16+
17+
.PREFIX_SPACE = null,
18+
.PREFIX_TAB = null,
19+
.POSTFIX_SPACE = null,
20+
.POSTFIX_TAB = null,
21+
},
22+
);
23+
24+
const check_config_header = b.addCheckFile(config_header.getOutput(), .{ .expected_exact = @embedFile("config.h") });
25+
26+
const test_step = b.step("test", "Test it");
27+
test_step.dependOn(&check_config_header.step);
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* This file was generated by ConfigHeader using the Zig Build System. */
2+
/* Some Comment */
3+
4+
int foo();
5+
6+
/* #undef SOME_NO */
7+
#define SOME_TRUE 1
8+
#define SOME_FALSE 0
9+
#define SOME_ZERO 0
10+
#define SOME_ONE 1
11+
#define SOME_TEN 10
12+
#define SOME_ENUM foo
13+
#define SOME_ENUM_LITERAL test
14+
#define SOME_STRING "test"
15+
16+
// Used twice
17+
#define SOME_TRUE 1
18+
19+
/* #undef PREFIX_SPACE */
20+
/* #undef PREFIX_TAB */
21+
/* #undef POSTFIX_SPACE */
22+
/* #undef POSTFIX_TAB */
23+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* Some Comment */
2+
3+
int foo();
4+
5+
#undef SOME_NO
6+
#undef SOME_TRUE
7+
#undef SOME_FALSE
8+
#undef SOME_ZERO
9+
#undef SOME_ONE
10+
#undef SOME_TEN
11+
#undef SOME_ENUM
12+
#undef SOME_ENUM_LITERAL
13+
#undef SOME_STRING
14+
15+
// Used twice
16+
#undef SOME_TRUE
17+
18+
#undef PREFIX_SPACE
19+
#undef PREFIX_TAB
20+
#undef POSTFIX_SPACE
21+
#undef POSTFIX_TAB

0 commit comments

Comments
 (0)