From b01b972999f024a694734fa9861827e5dc15a88b Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 25 Sep 2023 19:50:12 +0200 Subject: [PATCH] elf: test linking against empty C object --- test/link/elf.zig | 58 +++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/test/link/elf.zig b/test/link/elf.zig index b20498e7df..5c910df1ab 100644 --- a/test/link/elf.zig +++ b/test/link/elf.zig @@ -12,35 +12,27 @@ pub fn build(b: *Build) void { .abi = .musl, }; - elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = true })); + // Exercise linker with self-hosted backend (no LLVM) elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false })); - elf_step.dependOn(testLinkingC(b, .{ .target = musl_target, .use_llvm = true })); - // elf_step.dependOn(testLinkingC(b, .{ .target = musl_target, .use_llvm = false })); // TODO arocc + + // Exercise linker with LLVM backend + elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target })); + elf_step.dependOn(testLinkingC(b, .{ .target = musl_target })); + elf_step.dependOn(testLinkingZig(b, .{})); } -fn testLinkingZig(b: *Build, opts: Options) *Step { - const test_step = addTestStep(b, "linking-zig-static", opts); +fn testEmptyObject(b: *Build, opts: Options) *Step { + const test_step = addTestStep(b, "empty-object", opts); const exe = addExecutable(b, opts); - addZigSourceBytes(exe, - \\pub fn main() void { - \\ @import("std").debug.print("Hello World!\n", .{}); - \\} - ); + addCSourceBytes(exe, "int main() { return 0; }"); + addCSourceBytes(exe, ""); + exe.is_linking_libc = true; const run = b.addRunArtifact(exe); - run.expectStdErrEqual("Hello World!\n"); + run.expectExitCode(0); test_step.dependOn(&run.step); - const check = exe.checkObject(); - check.checkStart(); - check.checkExact("header"); - check.checkExact("type EXEC"); - check.checkStart(); - check.checkExact("section headers"); - check.checkNotPresent("name .dynamic"); - test_step.dependOn(&check.step); - return test_step; } @@ -73,6 +65,32 @@ fn testLinkingC(b: *Build, opts: Options) *Step { return test_step; } +fn testLinkingZig(b: *Build, opts: Options) *Step { + const test_step = addTestStep(b, "linking-zig-static", opts); + + const exe = addExecutable(b, opts); + addZigSourceBytes(exe, + \\pub fn main() void { + \\ @import("std").debug.print("Hello World!\n", .{}); + \\} + ); + + const run = b.addRunArtifact(exe); + run.expectStdErrEqual("Hello World!\n"); + test_step.dependOn(&run.step); + + const check = exe.checkObject(); + check.checkStart(); + check.checkExact("header"); + check.checkExact("type EXEC"); + check.checkStart(); + check.checkExact("section headers"); + check.checkNotPresent("name .dynamic"); + test_step.dependOn(&check.step); + + return test_step; +} + const Options = struct { target: CrossTarget = .{ .cpu_arch = .x86_64, .os_tag = .linux }, optimize: std.builtin.OptimizeMode = .Debug,