Skip to content

Commit 81b9d50

Browse files
Generate options.h based on options.h.in
1 parent d0e7556 commit 81b9d50

File tree

2 files changed

+133
-8
lines changed

2 files changed

+133
-8
lines changed

build.zig

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const LibCTest = struct {
55
target: std.Build.ResolvedTarget,
66
optimize: std.builtin.OptimizeMode,
77
src: std.Build.LazyPath,
8+
options_include: std.Build.LazyPath,
89
libtest: *std.Build.Step.Compile,
910
test_step: *std.Build.Step,
1011
unstable: bool,
@@ -125,6 +126,7 @@ pub fn build(b: *std.Build) !void {
125126
.target = target,
126127
.optimize = optimize,
127128
.src = src,
129+
.options_include = try generateOptionsInclude(b, target, src),
128130
.libtest = libtest,
129131
.test_step = test_step,
130132
.unstable = unstable,
@@ -171,7 +173,13 @@ pub fn build(b: *std.Build) !void {
171173
.wasi = .passes,
172174
});
173175
installApiTestCase(&libc_test, "api/errno.c", .passes);
174-
// TODO: api/fcntl.c requires common/options.h
176+
installApiTestCase(&libc_test, "api/fcntl.c", .{
177+
.darwin = .unsupported,
178+
.gnu = .unsupported,
179+
.musl = .passes,
180+
.mingw = .unsupported,
181+
.wasi = .unsupported,
182+
});
175183
installApiTestCase(&libc_test, "api/fenv.c", .passes);
176184
installApiTestCase(&libc_test, "api/float.c", .passes);
177185
installApiTestCase(&libc_test, "api/fmtmsg.c", .passes);
@@ -214,7 +222,13 @@ pub fn build(b: *std.Build) !void {
214222
.wasi = .passes,
215223
});
216224
installApiTestCase(&libc_test, "api/libgen.c", .passes);
217-
// TODO: api/limits.c requires common/options.h
225+
installApiTestCase(&libc_test, "api/limits.c", .{
226+
.darwin = .passes,
227+
.gnu = .unsupported,
228+
.musl = .passes,
229+
.mingw = .unsupported,
230+
.wasi = .unsupported,
231+
});
218232
installApiTestCase(&libc_test, "api/locale.c", .{
219233
.darwin = .passes,
220234
.gnu = .passes,
@@ -236,7 +250,7 @@ pub fn build(b: *std.Build) !void {
236250
.mingw = .unsupported,
237251
.wasi = .passes,
238252
});
239-
// TODO: api/mqueue.c requires common/options.h
253+
installApiTestCase(&libc_test, "api/mqueue.c", .passes);
240254
installApiTestCase(&libc_test, "api/ndbm.c", .passes);
241255
installApiTestCase(&libc_test, "api/net_if.c", .{
242256
.darwin = .passes,
@@ -301,7 +315,13 @@ pub fn build(b: *std.Build) !void {
301315
.mingw = .unsupported,
302316
.wasi = .passes,
303317
});
304-
// TODO: api/sched.c requires common/options.h
318+
installApiTestCase(&libc_test, "api/sched.c", .{
319+
.darwin = .unsupported,
320+
.gnu = .passes,
321+
.musl = .passes,
322+
.mingw = .unsupported,
323+
.wasi = .unsupported,
324+
});
305325
installApiTestCase(&libc_test, "api/search.c", .{
306326
.darwin = .passes,
307327
.gnu = .passes,
@@ -330,7 +350,13 @@ pub fn build(b: *std.Build) !void {
330350
.mingw = .passes,
331351
.wasi = .unsupported,
332352
});
333-
// TODO: api/spawn.c requires common/options.h
353+
installApiTestCase(&libc_test, "api/spawn.c", .{
354+
.darwin = .passes,
355+
.gnu = .passes,
356+
.musl = .passes,
357+
.mingw = .unsupported,
358+
.wasi = .unsupported,
359+
});
334360
installApiTestCase(&libc_test, "api/stdarg.c", .passes);
335361
installApiTestCase(&libc_test, "api/stdbool.c", .passes);
336362
installApiTestCase(&libc_test, "api/stddef.c", .passes);
@@ -370,7 +396,13 @@ pub fn build(b: *std.Build) !void {
370396
.mingw = .unsupported,
371397
.wasi = .passes,
372398
});
373-
// TODO: api/sys_mman.c requires common/options.h
399+
installApiTestCase(&libc_test, "api/sys_mman.c", .{
400+
.darwin = .passes,
401+
.gnu = .passes,
402+
.musl = .passes,
403+
.mingw = .unsupported,
404+
.wasi = .unsupported,
405+
});
374406
installApiTestCase(&libc_test, "api/sys_msg.c", .{
375407
.darwin = .passes,
376408
.gnu = .passes,
@@ -413,7 +445,13 @@ pub fn build(b: *std.Build) !void {
413445
.mingw = .unsupported,
414446
.wasi = .unsupported,
415447
});
416-
// TODO: api/sys_stat.c requires common/options.h
448+
installApiTestCase(&libc_test, "api/sys_stat.c", .{
449+
.darwin = .unsupported,
450+
.gnu = .unsupported,
451+
.musl = .passes,
452+
.mingw = .unsupported,
453+
.wasi = .unsupported,
454+
});
417455
installApiTestCase(&libc_test, "api/sys_statvfs.c", .{
418456
.darwin = .passes,
419457
.gnu = .passes,
@@ -499,7 +537,13 @@ pub fn build(b: *std.Build) !void {
499537
.mingw = .passes,
500538
.wasi = .unsupported,
501539
});
502-
// TODO: api/unistd.c requires common/options.h
540+
installApiTestCase(&libc_test, "api/unistd.c", .{
541+
.darwin = .unsupported,
542+
.gnu = .unsupported,
543+
.musl = .unsupported,
544+
.mingw = .unsupported,
545+
.wasi = .unsupported,
546+
});
503547
installApiTestCase(&libc_test, "api/utmpx.c", .{
504548
.darwin = .passes,
505549
.gnu = .passes,
@@ -2362,6 +2406,33 @@ pub fn build(b: *std.Build) !void {
23622406
// "regression/tls_get_new-dtv_dso.c"
23632407
}
23642408

2409+
/// Generate options.h based on libc-test/src/common/options.h.in
2410+
fn generateOptionsInclude(b: *std.Build, target: std.Build.ResolvedTarget, src: std.Build.LazyPath) !std.Build.LazyPath {
2411+
var zig_run = b.addSystemCommand(&.{ b.graph.zig_exe, "cc", "-E", "-x", "c" });
2412+
if (!target.query.isNative()) {
2413+
zig_run.addArg("-target");
2414+
zig_run.addArg(try target.query.zigTriple(b.allocator));
2415+
}
2416+
zig_run.addArg("-o");
2417+
const preprocessed = zig_run.addOutputFileArg("options-in.h");
2418+
zig_run.addFileArg(src.path(b, "common/options.h.in"));
2419+
2420+
const exe = b.addExecutable(.{
2421+
.name = "generate_options",
2422+
.root_module = b.createModule(.{
2423+
.root_source_file = b.path("tools/generate_options.zig"),
2424+
.target = b.graph.host,
2425+
.optimize = .Debug, // for error checking
2426+
}),
2427+
});
2428+
const exe_run = b.addRunArtifact(exe);
2429+
exe_run.addFileArg(preprocessed);
2430+
const include = exe_run.addOutputDirectoryArg("options-include");
2431+
exe_run.addArg("options.h");
2432+
2433+
return include;
2434+
}
2435+
23652436
fn installApiTestCase(libc_test: *const LibCTest, case: []const u8, support: LibCImpl.Support) void {
23662437
if (support.shouldSkip(libc_test)) return;
23672438

@@ -2373,6 +2444,7 @@ fn installApiTestCase(libc_test: *const LibCTest, case: []const u8, support: Lib
23732444
});
23742445

23752446
test_mod.addIncludePath(libc_test.src.path(b, "common"));
2447+
test_mod.addIncludePath(libc_test.options_include);
23762448

23772449
test_mod.addCSourceFile(.{
23782450
.file = libc_test.src.path(b, "api/main.c"),

tools/generate_options.zig

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
var it = try std.process.argsWithAllocator(std.heap.page_allocator);
5+
defer it.deinit();
6+
7+
_ = it.next();
8+
const in_path = it.next().?;
9+
const out_path = it.next().?;
10+
const out_name = it.next().?;
11+
12+
const in_file = try std.fs.cwd().openFile(in_path, .{});
13+
defer in_file.close();
14+
15+
var out_dir = try std.fs.cwd().openDir(out_path, .{});
16+
defer out_dir.close();
17+
18+
const out_file = try out_dir.createFile(out_name, .{});
19+
defer out_file.close();
20+
21+
var line_buf: [512]u8 = undefined;
22+
var file_reader = in_file.reader(&line_buf);
23+
var reader = &file_reader.interface;
24+
25+
var found_unistd_end = false;
26+
27+
while (true) {
28+
const line = reader.takeDelimiterExclusive('\n') catch |err| switch (err) {
29+
error.EndOfStream => break,
30+
error.ReadFailed => return error.ReadFailed,
31+
error.StreamTooLong => {
32+
// Skip lines that are too long
33+
_ = reader.discardDelimiterInclusive('\n') catch |e| switch (e) {
34+
error.EndOfStream => break,
35+
error.ReadFailed => return error.ReadFailed,
36+
};
37+
continue;
38+
},
39+
};
40+
41+
if (std.mem.eql(u8, line, "optiongroups_unistd_end")) {
42+
found_unistd_end = true;
43+
continue;
44+
}
45+
46+
if (!found_unistd_end or line.len == 0 or line[0] == '#')
47+
continue;
48+
49+
try out_file.writeAll("#define ");
50+
try out_file.writeAll(line);
51+
try out_file.writeAll("\n");
52+
}
53+
}

0 commit comments

Comments
 (0)