zig

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

commit 82e8ed66ecaa3fc88267bfcb2850c50fe9a4b955 (tree)
parent 4a2a9e9d24b71bd0bea80aaf88205039ae77c8df
Author: Matthew Lugg <mlugg@mlugg.co.uk>
Date:   Tue,  2 Jun 2026 09:41:22 +0100

Elf2: add basic standalone test coverage

This is just a temporary measure before we have better test coverage set
up for this. I figure that in the name of incremental improvements, it
makes sense to get at least a little bit of coverage in now to prevent
basic use cases from regressing---because right now the old coverage of
Elf2 in CI is the incremental tests, which only cover compiling for
x86_64-linux without libc.

In the future, this standalone test should be replaced with both of:
* entries in the main test target matrix
* linker tests (related: https://codeberg.org/ziglang/zig/pulls/32065)

Diffstat:
Mtest/standalone/build.zig.zon | 3+++
Atest/standalone/elf2/build.zig | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Atest/standalone/elf2/hello.zig | 6++++++
3 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/test/standalone/build.zig.zon b/test/standalone/build.zig.zon @@ -193,6 +193,9 @@ .debug_io_color = .{ .path = "debug_io_color", }, + .elf2 = .{ + .path = "elf2", + }, }, .paths = .{ "build.zig", diff --git a/test/standalone/elf2/build.zig b/test/standalone/elf2/build.zig @@ -0,0 +1,51 @@ +pub fn build(b: *Build) void { + const test_step = b.step("test", "Test the new ELF linker"); + b.default_step = test_step; + + if (b.graph.host.result.cpu.arch == .x86_64 and b.graph.host.result.os.tag == .linux) { + addOne(b, test_step, b.graph.host, false, .static, "elf2-hello-native-selfhosted-static"); + addOne(b, test_step, b.graph.host, false, .dynamic, "elf2-hello-native-selfhosted-dynamic"); + addOne(b, test_step, b.graph.host, true, .static, "elf2-hello-native-llvm-static"); + addOne(b, test_step, b.graph.host, true, .dynamic, "elf2-hello-native-llvm-dynamic"); + } + + const x86_64_linux_target: Build.ResolvedTarget = b.resolveTargetQuery(.{ + .cpu_arch = .x86_64, + .os_tag = .linux, + }); + addOne(b, test_step, x86_64_linux_target, false, .static, "elf2-hello-selfhosted-static"); + addOne(b, test_step, x86_64_linux_target, true, .static, "elf2-hello-llvm-static"); +} + +fn addOne( + b: *Build, + test_step: *Build.Step, + target: Build.ResolvedTarget, + use_llvm: bool, + link_mode: std.lang.LinkMode, + name: []const u8, +) void { + const mod = b.createModule(.{ + .root_source_file = b.path("hello.zig"), + .target = target, + .optimize = .Debug, + .link_libc = link_mode == .dynamic, + }); + const exe = b.addExecutable(.{ + .name = name, + .root_module = mod, + .linkage = link_mode, + }); + exe.use_new_linker = true; + exe.use_llvm = use_llvm; + + const run = b.addRunArtifact(exe); + run.expectExitCode(0); + run.expectStdOutEqual("Hello, World!\n"); + run.skip_foreign_checks = true; + + test_step.dependOn(&run.step); +} + +const std = @import("std"); +const Build = std.Build; diff --git a/test/standalone/elf2/hello.zig b/test/standalone/elf2/hello.zig @@ -0,0 +1,6 @@ +pub fn main(init: std.process.Init) !void { + const stdout: std.Io.File = .stdout(); + try stdout.writeStreamingAll(init.io, "Hello, World!\n"); +} + +const std = @import("std");