authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-13 17:22:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-15 10:33:08-07:00
log4a233d18717e0f5729218e02eebf29ad46449a38
tree1613cacd1dc7db37b9884d4c33d77e67e9760e55
parent3c93c1664a3c3546076b39c2d40405f5db537dae

CI: more C backend test coverage

The CI now runs C backend tests in addition to compiling them. It uses -std=c99 -pedantic -Werror in order to catch non-conformant C code. This necessitated disabling a test case that caused a C compile error, in addition to disabling a handful of warnings that are already being triggered by Zig's C backend output for the behavior tests. The upshot is that I was able to, very cleanly, integrate the C backend tests into the build system, so that it communicates via the test runner protocol along with all the other behavior tests.

5 files changed, 62 insertions(+), 15 deletions(-)

lib/std/Build.zig+1-2
......@@ -679,8 +679,7 @@ pub fn addRunArtifact(b: *Build, exe: *CompileStep) *RunStep {
679679 run_step.addArtifactArg(exe);
680680
681681 if (exe.kind == .@"test") {
682 run_step.stdio = .zig_test;
683 run_step.addArgs(&.{"--listen=-"});
682 run_step.enableTestRunnerMode();
684683 }
685684
686685 if (exe.vcpkg_bin_path) |path| {
lib/std/Build/RunStep.zig+5
......@@ -140,6 +140,11 @@ pub fn setName(self: *RunStep, name: []const u8) void {
140140 self.rename_step_with_output_arg = false;
141141}
142142
143pub fn enableTestRunnerMode(rs: *RunStep) void {
144 rs.stdio = .zig_test;
145 rs.addArgs(&.{"--listen=-"});
146}
147
143148pub fn addArtifactArg(self: *RunStep, artifact: *CompileStep) void {
144149 self.argv.append(Arg{ .artifact = artifact }) catch @panic("OOM");
145150 self.step.dependOn(&artifact.step);
lib/std/zig/Server.zig+4-8
......@@ -104,11 +104,9 @@ pub fn receiveMessage(s: *Server) !InMessage.Header {
104104 const buf = fifo.readableSlice(0);
105105 assert(fifo.readableLength() == buf.len);
106106 if (buf.len >= @sizeOf(Header)) {
107 const header = @ptrCast(*align(1) const Header, buf[0..@sizeOf(Header)]);
108107 // workaround for https://github.com/ziglang/zig/issues/14904
109 const bytes_len = bswap_and_workaround_u32(&header.bytes_len);
110 // workaround for https://github.com/ziglang/zig/issues/14904
111 const tag = bswap_and_workaround_tag(&header.tag);
108 const bytes_len = bswap_and_workaround_u32(buf[4..][0..4]);
109 const tag = bswap_and_workaround_tag(buf[0..][0..4]);
112110
113111 if (buf.len - @sizeOf(Header) >= bytes_len) {
114112 fifo.discard(@sizeOf(Header));
......@@ -281,14 +279,12 @@ fn bswap_u32_array(slice: []u32) void {
281279}
282280
283281/// workaround for https://github.com/ziglang/zig/issues/14904
284fn bswap_and_workaround_u32(x: *align(1) const u32) u32 {
285 const bytes_ptr = @ptrCast(*const [4]u8, x);
282fn bswap_and_workaround_u32(bytes_ptr: *const [4]u8) u32 {
286283 return std.mem.readIntLittle(u32, bytes_ptr);
287284}
288285
289286/// workaround for https://github.com/ziglang/zig/issues/14904
290fn bswap_and_workaround_tag(x: *align(1) const InMessage.Tag) InMessage.Tag {
291 const bytes_ptr = @ptrCast(*const [4]u8, x);
287fn bswap_and_workaround_tag(bytes_ptr: *const [4]u8) InMessage.Tag {
292288 const int = std.mem.readIntLittle(u32, bytes_ptr);
293289 return @intToEnum(InMessage.Tag, int);
294290}
test/behavior/atomics.zig+6
......@@ -209,6 +209,12 @@ test "atomicrmw with floats" {
209209 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
210210 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
211211
212 if (builtin.zig_backend == .stage2_c) {
213 // TODO: test.c:34929:7: error: address argument to atomic operation must be a pointer to integer or pointer ('zig_f32 *' (aka 'float *') invalid
214 // when compiling with -std=c99 -pedantic
215 return error.SkipZigTest;
216 }
217
212218 if ((builtin.zig_backend == .stage2_llvm or builtin.zig_backend == .stage2_c) and
213219 builtin.cpu.arch == .aarch64)
214220 {
test/tests.zig+46-5
......@@ -988,18 +988,59 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
988988 these_tests.overrideZigLibDir("lib");
989989 these_tests.addIncludePath("test");
990990
991 const run = b.addRunArtifact(these_tests);
992 run.skip_foreign_checks = true;
993 run.setName(b.fmt("run test {s}-{s}-{s}{s}{s}{s}", .{
991 const qualified_name = b.fmt("{s}-{s}-{s}{s}{s}{s}", .{
994992 options.name,
995993 triple_txt,
996994 @tagName(test_target.optimize_mode),
997995 libc_suffix,
998996 single_threaded_suffix,
999997 backend_suffix,
1000 }));
998 });
999
1000 if (test_target.target.ofmt == std.Target.ObjectFormat.c) {
1001 var altered_target = test_target.target;
1002 altered_target.ofmt = null;
10011003
1002 step.dependOn(&run.step);
1004 const compile_c = b.addExecutable(.{
1005 .name = qualified_name,
1006 .link_libc = test_target.link_libc,
1007 .target = altered_target,
1008 });
1009 compile_c.overrideZigLibDir("lib");
1010 compile_c.addCSourceFileSource(.{
1011 .source = these_tests.getOutputSource(),
1012 .args = &.{
1013 // TODO output -std=c89 compatible C code
1014 "-std=c99",
1015 "-pedantic",
1016 "-Werror",
1017 // TODO stop violating these pedantic errors
1018 "-Wno-address-of-packed-member",
1019 "-Wno-gnu-folding-constant",
1020 "-Wno-incompatible-pointer-types",
1021 "-Wno-overlength-strings",
1022 },
1023 });
1024 compile_c.addIncludePath("lib"); // for zig.h
1025 if (test_target.link_libc == false and test_target.target.getOsTag() == .windows) {
1026 compile_c.subsystem = .Console;
1027 compile_c.linkSystemLibrary("kernel32");
1028 compile_c.linkSystemLibrary("ntdll");
1029 }
1030
1031 const run = b.addRunArtifact(compile_c);
1032 run.skip_foreign_checks = true;
1033 run.enableTestRunnerMode();
1034 run.setName(b.fmt("run test {s}", .{qualified_name}));
1035
1036 step.dependOn(&run.step);
1037 } else {
1038 const run = b.addRunArtifact(these_tests);
1039 run.skip_foreign_checks = true;
1040 run.setName(b.fmt("run test {s}", .{qualified_name}));
1041
1042 step.dependOn(&run.step);
1043 }
10031044 }
10041045 return step;
10051046}