zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 0c41edf4978cc45b59909fce69885e310360cbfa (tree)
parent 45c4f781a5a831dd08cbab745a55834d514e45f5
Author: Alex Rønne Petersen <alex@alexrp.com>
Date:   Mon,  1 Jun 2026 19:20:03 +0200

Merge pull request 'Some follow-up work for #35502' (#35565) from alexrp/zig:35502-followup into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35565

Diffstat:
Mlib/std/c.zig | 23++++++++++++++++++++---
Mtest/behavior/call.zig | 8++++----
Mtest/c/math.zig | 3+++
Mtest/c/pthread.zig | 1+
Mtest/c/stdlib.zig | 14++++++++++++++
Mtest/c/strings.zig | 4++++
Mtest/c/unistd.zig | 1+
Mtest/c_abi/cfuncs.c | 14++++++++++++++
Mtest/c_abi/main.zig | 12++++++++++++
Mtest/tests.zig | 125++++++++++++++++++++++++++++++++++++++++++-------------------------------------
10 files changed, 140 insertions(+), 65 deletions(-)

diff --git a/lib/std/c.zig b/lib/std/c.zig @@ -11134,14 +11134,23 @@ pub const ioctl = switch (native_os) { pub extern "c" fn bzero(s: *anyopaque, n: usize) void; -pub extern "c" fn swab(noalias from: *const anyopaque, noalias to: *anyopaque, n: isize) void; +pub const swab = switch (builtin.abi) { + .msvc => private._swab, + else => private.swab, +}; pub extern "c" fn strncmp(a: [*:0]const c_char, b: [*:0]const c_char, max: usize) c_int; pub extern "c" fn strcasecmp(a: [*:0]const c_char, b: [*:0]const c_char) c_int; pub extern "c" fn strncasecmp(a: [*:0]const c_char, b: [*:0]const c_char, max: usize) c_int; -pub extern "c" fn strdup(s: [*:0]const c_char) ?[*:0]c_char; +pub const strdup = switch (builtin.abi) { + .msvc => private._strdup, + else => private.strdup, +}; pub extern "c" fn strndup(s: [*:0]const c_char, n: usize) ?[*:0]c_char; -pub extern "c" fn wcsdup(s: [*:0]const wchar_t) ?[*:0]wchar_t; +pub const wcsdup = switch (builtin.abi) { + .msvc => private._wcsdup, + else => private.wcsdup, +}; pub extern "c" fn ffs(i: c_int) c_int; pub extern "c" fn ffsl(i: c_long) c_long; @@ -11555,6 +11564,14 @@ pub const setkeymap = serenity.setkeymap; /// External definitions shared by two or more operating systems. const private = struct { + pub extern "c" fn strdup(s: [*:0]const c_char) ?[*:0]c_char; + pub extern "c" fn _strdup(s: [*:0]const c_char) ?[*:0]c_char; + pub extern "c" fn wcsdup(s: [*:0]const wchar_t) ?[*:0]wchar_t; + pub extern "c" fn _wcsdup(s: [*:0]const wchar_t) ?[*:0]wchar_t; + + pub extern "c" fn swab(noalias from: *const anyopaque, noalias to: *anyopaque, n: isize) void; + pub extern "c" fn _swab(noalias from: *const anyopaque, noalias to: *anyopaque, n: isize) void; + extern "c" fn close(fd: fd_t) c_int; extern "c" fn clock_getres(clk_id: clockid_t, tp: *timespec) c_int; extern "c" fn clock_gettime(clk_id: clockid_t, tp: *timespec) c_int; diff --git a/test/behavior/call.zig b/test/behavior/call.zig @@ -277,7 +277,7 @@ test "forced tail call" { if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_llvm) { + if (builtin.zig_backend == .stage2_llvm or builtin.zig_backend == .stage2_c) { if (builtin.cpu.arch.isMIPS() or builtin.cpu.arch.isPowerPC() or builtin.cpu.arch.isWasm()) { return error.SkipZigTest; } @@ -312,7 +312,7 @@ test "inline call preserves tail call" { if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_llvm) { + if (builtin.zig_backend == .stage2_llvm or builtin.zig_backend == .stage2_c) { if (builtin.cpu.arch.isMIPS() or builtin.cpu.arch.isPowerPC() or builtin.cpu.arch.isWasm()) { return error.SkipZigTest; } @@ -708,7 +708,7 @@ test "tail call function pointer" { if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_llvm) { + if (builtin.zig_backend == .stage2_llvm or builtin.zig_backend == .stage2_c) { if (builtin.cpu.arch.isMIPS() or builtin.cpu.arch.isPowerPC() or builtin.cpu.arch.isWasm()) { return error.SkipZigTest; } @@ -736,7 +736,7 @@ test "tail call with potentially extended types" { if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_llvm) { + if (builtin.zig_backend == .stage2_llvm or builtin.zig_backend == .stage2_c) { if (builtin.cpu.arch.isMIPS() or builtin.cpu.arch.isPowerPC() or builtin.cpu.arch.isWasm()) { return error.SkipZigTest; } diff --git a/test/c/math.zig b/test/c/math.zig @@ -61,10 +61,13 @@ test "modf" { } test "modff" { + if (builtin.cpu.arch == .x86 and builtin.target.os.tag == .windows and builtin.target.abi != .gnu) return; // mingw-only + try testModf(f32); } test "modfl" { + if (builtin.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi != .gnu) return; // mingw-only if (builtin.target.cpu.arch.isPowerPC()) return error.SkipZigTest; // TODO: see https://codeberg.org/ziglang/zig/issues/30976 try testModf(c_longdouble); diff --git a/test/c/pthread.zig b/test/c/pthread.zig @@ -7,6 +7,7 @@ const testing = std.testing; test "pthread_spinlock_t" { if (builtin.target.os.tag.isDarwin()) return; // Darwin doesn't have `pthread_spin_*` + if (builtin.target.os.tag == .windows and builtin.target.abi != .gnu) return; // winpthreads-only var spin: c.pthread_spinlock_t = undefined; _ = c.pthread_spin_init(&spin, c.PTHREAD_PROCESS_PRIVATE); diff --git a/test/c/stdlib.zig b/test/c/stdlib.zig @@ -33,6 +33,7 @@ test "div" { if (builtin.target.cpu.arch.isMIPS64()) return error.SkipZigTest; // TODO if (builtin.target.cpu.arch.isPowerPC()) return error.SkipZigTest; // TODO if (builtin.target.cpu.arch == .s390x) return error.SkipZigTest; // TODO + if (builtin.target.cpu.arch == .x86 and builtin.target.os.tag == .windows) return error.SkipZigTest; // TODO const expected: c.div_t = .{ .quot = 5, .rem = 5 }; try testing.expectEqual(expected, c.div(55, 10)); @@ -42,6 +43,7 @@ test "ldiv" { if (builtin.target.cpu.arch.isMIPS64() and @sizeOf(usize) == 4) return error.SkipZigTest; // TODO if (builtin.target.cpu.arch.isPowerPC32()) return error.SkipZigTest; // TODO if (builtin.target.cpu.arch == .s390x) return error.SkipZigTest; // TODO + if (builtin.target.cpu.arch == .x86 and builtin.target.os.tag == .windows) return error.SkipZigTest; // TODO const expected: c.ldiv_t = .{ .quot = -6, .rem = 2 }; try testing.expectEqual(expected, c.ldiv(38, -6)); @@ -129,6 +131,8 @@ fn testStrToLLikeFunctionAnyErrno( } test "strtol" { + if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO + c._errno().* = @intFromEnum(c.E.SUCCESS); try testStrToLLikeFunctionAnyErrno(c.strtol, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); try testStrToLLikeFunction(c.strtol, @ptrCast("42true"), 0, 42, 2, .SUCCESS); @@ -167,6 +171,8 @@ test "strtol" { } test "strtoll" { + if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO + c._errno().* = @intFromEnum(c.E.SUCCESS); try testStrToLLikeFunctionAnyErrno(c.strtoll, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); try testStrToLLikeFunction(c.strtoll, @ptrCast("42true"), 0, 42, 2, .SUCCESS); @@ -205,6 +211,8 @@ test "strtoll" { } test "strtoul" { + if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO + c._errno().* = @intFromEnum(c.E.SUCCESS); try testStrToLLikeFunctionAnyErrno(c.strtoul, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); try testStrToLLikeFunction(c.strtoul, @ptrCast("42true"), 0, 42, 2, .SUCCESS); @@ -241,6 +249,8 @@ test "strtoul" { } test "strtoull" { + if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO + c._errno().* = @intFromEnum(c.E.SUCCESS); try testStrToLLikeFunctionAnyErrno(c.strtoull, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); try testStrToLLikeFunction(c.strtoull, @ptrCast("42true"), 0, 42, 2, .SUCCESS); @@ -277,6 +287,8 @@ test "strtoull" { } test "strtoimax" { + if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO + c._errno().* = @intFromEnum(c.E.SUCCESS); try testStrToLLikeFunctionAnyErrno(c.strtoimax, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); try testStrToLLikeFunction(c.strtoimax, @ptrCast("42true"), 0, 42, 2, .SUCCESS); @@ -315,6 +327,8 @@ test "strtoimax" { } test "strtoumax" { + if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO + c._errno().* = @intFromEnum(c.E.SUCCESS); try testStrToLLikeFunctionAnyErrno(c.strtoumax, @ptrCast("stop42true"), 0, 0, 0, &.{ .SUCCESS, .INVAL }); try testStrToLLikeFunction(c.strtoumax, @ptrCast("42true"), 0, 42, 2, .SUCCESS); diff --git a/test/c/strings.zig b/test/c/strings.zig @@ -48,6 +48,8 @@ test "ffs" { } test "strcasecmp" { + if (builtin.target.os.tag == .windows and builtin.target.abi != .gnu) return; // mingw-only + try testing.expect(c.strcasecmp(@ptrCast("a"), @ptrCast("b")) < 0); try testing.expect(c.strcasecmp(@ptrCast("b"), @ptrCast("a")) > 0); try testing.expect(c.strcasecmp(@ptrCast("A"), @ptrCast("b")) < 0); @@ -58,6 +60,8 @@ test "strcasecmp" { } test "strncasecmp" { + if (builtin.target.os.tag == .windows and builtin.target.abi != .gnu) return; // mingw-only + try testing.expect(c.strncasecmp(@ptrCast("a"), @ptrCast("b"), 1) < 0); try testing.expect(c.strncasecmp(@ptrCast("b"), @ptrCast("a"), 1) > 0); try testing.expect(c.strncasecmp(@ptrCast("A"), @ptrCast("b"), 1) < 0); diff --git a/test/c/unistd.zig b/test/c/unistd.zig @@ -8,6 +8,7 @@ test "swab" { if (builtin.target.cpu.arch.isMIPS64() and @sizeOf(usize) == 4) return error.SkipZigTest; // TODO if (builtin.target.cpu.arch == .x86_64 and @sizeOf(usize) == 4) return error.SkipZigTest; // TODO if (builtin.target.os.tag == .netbsd) return error.SkipZigTest; // TODO + if (builtin.target.cpu.arch.isX86() and builtin.target.os.tag == .windows and builtin.target.abi == .msvc) return error.SkipZigTest; // TODO var a: [4]u8 = undefined; @memset(a[0..], '\x00'); diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c @@ -2846,6 +2846,7 @@ void run_c_tests(void) { #if !defined(__mips64) #if !defined(ZIG_PPC32) #if !defined(__s390x__) +#if !(defined(_WIN32) && defined(__i386__)) { struct Struct_u8 s = zig_ret_struct_u8(); assert_or_panic(s.a == 1); @@ -2854,10 +2855,12 @@ void run_c_tests(void) { #endif #endif #endif +#endif #if !defined(__mips64) #if !defined(ZIG_PPC32) #if !defined(__s390x__) +#if !(defined(_WIN32) && defined(__i386__)) { struct Struct_u16 s = zig_ret_struct_u16(); assert_or_panic(s.a == 7); @@ -2866,10 +2869,12 @@ void run_c_tests(void) { #endif #endif #endif +#endif #if !defined(__mips64) #if !defined(ZIG_PPC32) #if !defined(__s390x__) +#if !(defined(_WIN32) && defined(__i386__)) { struct Struct_u32 s = zig_ret_struct_u32(); assert_or_panic(s.a == 13); @@ -2878,10 +2883,12 @@ void run_c_tests(void) { #endif #endif #endif +#endif #if !defined(ZIG_PPC32) #if !defined(ZIG_RISCV32) #if !defined(__s390x__) +#if !(defined(_WIN32) && defined(__i386__)) { struct Struct_u64 s = zig_ret_struct_u64(); assert_or_panic(s.a == 19); @@ -2890,6 +2897,7 @@ void run_c_tests(void) { #endif #endif #endif +#endif #if !defined(ZIG_PPC32) && !defined(__hexagon__) && !defined(__s390x__) { @@ -2908,15 +2916,18 @@ void run_c_tests(void) { } #if !defined(__mips64__) +#if !(defined(_WIN32) && defined(__i386__)) { struct Struct_f32 s = zig_ret_struct_f32(); assert_or_panic(s.a == 2.5f); zig_struct_f32((struct Struct_f32){ .a = 2.5f }); } #endif +#endif #if !(defined(__arm__) && defined(__SOFTFP__)) #if !defined(ZIG_RISCV32) +#if !(defined(_WIN32) && defined(__i386__)) { struct Struct_f64 s = zig_ret_struct_f64(); assert_or_panic(s.a == 2.5); @@ -2924,6 +2935,7 @@ void run_c_tests(void) { } #endif #endif +#endif #if !defined(__arm__) #if !defined(__loongarch__) @@ -2987,6 +2999,7 @@ void run_c_tests(void) { #endif #if !defined(__powerpc__) && !defined(__loongarch__) && !defined(__mips64__) +#if !(defined(_WIN32) && defined(__i386__)) { struct Struct_u32_Union_u32_u32u32 s = zig_ret_struct_u32_union_u32_u32u32(); assert_or_panic(s.a == 1); @@ -3000,6 +3013,7 @@ void run_c_tests(void) { zig_struct_i32_i32(s); } #endif +#endif #if !defined(__powerpc64__) && !defined(__loongarch__) && !defined(__mips64__) { diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig @@ -291,6 +291,7 @@ test "C ABI struct u8" { if (builtin.cpu.arch.isMIPS64()) return error.SkipZigTest; if (builtin.cpu.arch.isPowerPC32()) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const s = c_ret_struct_u8(); try expect(s.a == 4); @@ -318,6 +319,7 @@ test "C ABI struct u16" { if (builtin.cpu.arch.isMIPS64()) return error.SkipZigTest; if (builtin.cpu.arch.isPowerPC32()) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const s = c_ret_struct_u16(); try expect(s.a == 10); @@ -345,6 +347,7 @@ test "C ABI struct u32" { if (builtin.cpu.arch.isMIPS64()) return error.SkipZigTest; if (builtin.cpu.arch.isPowerPC32()) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const s = c_ret_struct_u32(); try expect(s.a == 16); @@ -372,6 +375,7 @@ test "C ABI struct u64" { if (builtin.cpu.arch.isPowerPC32()) return error.SkipZigTest; if (builtin.cpu.arch == .riscv32) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const s = c_ret_struct_u64(); try expect(s.a == 22); @@ -485,6 +489,7 @@ test "C ABI struct f32" { if (builtin.cpu.arch.isMIPS64()) return error.SkipZigTest; if (builtin.cpu.arch.isPowerPC32()) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const s = c_ret_struct_f32(); try expect(s.a == 2.5); @@ -513,6 +518,7 @@ test "C ABI struct f64" { if (builtin.cpu.arch.isArm() and builtin.abi.float() == .soft) return error.SkipZigTest; if (builtin.cpu.arch == .riscv32) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const s = c_ret_struct_f64(); try expect(s.a == 2.5); @@ -705,6 +711,7 @@ test "C ABI struct i32 i32" { if (builtin.cpu.arch == .riscv32) return error.SkipZigTest; if (builtin.cpu.arch.isLoongArch()) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const s: Struct_i32_i32 = .{ .a = 1, @@ -5681,6 +5688,7 @@ test "C ABI pointer sized float struct" { if (builtin.cpu.arch.isArm() and builtin.abi.float() == .soft) return error.SkipZigTest; if (builtin.cpu.arch.isLoongArch() and builtin.abi.float() == .soft) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; c_ptr_size_float_struct(.{ .x = 1, .y = 2 }); @@ -5812,6 +5820,7 @@ test "PD: Zig passes to C" { if (builtin.cpu.arch.isLoongArch()) return error.SkipZigTest; if (builtin.cpu.arch == .hexagon) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; try expectOk(c_assert_PD(.{ .v1 = null, .v2 = 0.5 })); } test "PD: Zig returns to C" { @@ -5827,6 +5836,7 @@ test "PD: C passes to Zig" { if (builtin.cpu.arch.isLoongArch()) return error.SkipZigTest; if (builtin.cpu.arch == .hexagon) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; try expectOk(c_send_PD()); } test "PD: C returns to Zig" { @@ -5936,6 +5946,7 @@ test "f16 struct" { if (builtin.target.cpu.arch.isPowerPC32()) return error.SkipZigTest; if (builtin.cpu.arch.isArm() and builtin.mode != .Debug) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const a = c_f16_struct(.{ .a = 12 }); try expect(a.a == 34); @@ -6045,6 +6056,7 @@ test "Stdcall ABI structs" { if (builtin.cpu.arch.isLoongArch()) return error.SkipZigTest; if (builtin.cpu.arch == .hexagon) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (builtin.cpu.arch == .x86 and builtin.os.tag == .windows) return error.SkipZigTest; const res = stdcall_coord2( .{ .x = 0x1111, .y = 0x2222 }, diff --git a/test/tests.zig b/test/tests.zig @@ -1611,8 +1611,6 @@ const module_test_targets = blk: { .abi = .msvc, }, .link_libc = true, - // https://codeberg.org/ziglang/zig/issues/35517 - .skip_modules = &.{"libc"}, }, .{ .target = .{ @@ -1628,8 +1626,6 @@ const module_test_targets = blk: { .abi = .gnu, }, .link_libc = true, - // https://codeberg.org/ziglang/zig/issues/35517 - .skip_modules = &.{"libc"}, }, .{ @@ -1657,8 +1653,6 @@ const module_test_targets = blk: { .abi = .msvc, }, .link_libc = true, - // https://codeberg.org/ziglang/zig/issues/35517 - .skip_modules = &.{"libc"}, }, .{ .target = .{ @@ -2001,14 +1995,13 @@ const c_abi_targets = blk: { // Windows Targets - // https://codeberg.org/ziglang/zig/issues/35521 - //.{ - // .target = .{ - // .cpu_arch = .x86, - // .os_tag = .windows, - // .abi = .gnu, - // }, - //}, + .{ + .target = .{ + .cpu_arch = .x86, + .os_tag = .windows, + .abi = .gnu, + }, + }, .{ .target = .{ .cpu_arch = .x86_64, @@ -2650,11 +2643,6 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { const target = &resolved_target.result; - if (target.cpu.arch == .powerpc64le and target.ofmt == .c) { - // https://codeberg.org/ziglang/zig/issues/35522 - continue; - } - if (target.cpu.arch == .s390x and target.ofmt == .c) { // https://codeberg.org/ziglang/zig/issues/35523 continue; @@ -2811,45 +2799,66 @@ fn addOneModuleTest( compile_c.addCSourceFile(.{ .file = these_tests.getEmittedBin(), - .flags = &.{ - // Tracking issue for making the C backend generate C89 compatible code: - // https://github.com/ziglang/zig/issues/19468 - "-std=c99", - "-Werror", - - "-Wall", - "-Wembedded-directive", - "-Wempty-translation-unit", - "-Wextra", - "-Wgnu", - "-Winvalid-utf8", - "-Wkeyword-macro", - "-Woverlength-strings", - - // Tracking issue for making the C backend generate code - // that does not trigger warnings: - // https://github.com/ziglang/zig/issues/19467 - - // spotted everywhere - "-Wno-builtin-requires-header", - - // spotted on linux - "-Wno-braced-scalar-init", - "-Wno-excess-initializers", - "-Wno-incompatible-pointer-types-discards-qualifiers", - "-Wno-unused", - "-Wno-unused-parameter", - - // spotted on darwin - "-Wno-incompatible-pointer-types", - - // https://github.com/llvm/llvm-project/issues/153314 - "-Wno-unterminated-string-initialization", - - // In both Zig and C it is legal to return a pointer to a - // local. The C backend lowers such thing directly, so the - // corresponding warning in C must be disabled. - "-Wno-return-stack-address", + .flags = blk: { + const invariant_cflags: []const []const u8 = &.{ + // Tracking issue for making the C backend generate C89 compatible code: + // https://github.com/ziglang/zig/issues/19468 + "-std=c99", + "-Werror", + + "-Wall", + "-Wembedded-directive", + "-Wempty-translation-unit", + "-Wextra", + "-Wgnu", + "-Winvalid-utf8", + "-Wkeyword-macro", + "-Woverlength-strings", + + // Tracking issue for making the C backend generate code + // that does not trigger warnings: + // https://github.com/ziglang/zig/issues/19467 + + // spotted everywhere + "-Wno-builtin-requires-header", + + // spotted on linux + "-Wno-braced-scalar-init", + "-Wno-excess-initializers", + "-Wno-incompatible-pointer-types-discards-qualifiers", + "-Wno-unused", + "-Wno-unused-parameter", + + // spotted on darwin + "-Wno-incompatible-pointer-types", + + // https://github.com/llvm/llvm-project/issues/153314 + "-Wno-unterminated-string-initialization", + + // In both Zig and C it is legal to return a pointer to a + // local. The C backend lowers such thing directly, so the + // corresponding warning in C must be disabled. + "-Wno-return-stack-address", + }; + + const function_data_sections = switch (target.cpu.arch) { + .arm, + .armeb, + .thumb, + .thumbeb, + .hexagon, + .powerpc, + .powerpcle, + .powerpc64, + .powerpc64le, + => true, + else => false, + }; + + break :blk if (function_data_sections) invariant_cflags ++ &[_][]const u8{ + "-ffunction-sections", + "-fdata-sections", + } else invariant_cflags; }, }); compile_c.addIncludePath(b.path("lib")); // for zig.h