Commit b01b972999

Jakub Konka <kubkon@jakubkonka.com>
2023-09-25 19:50:12
elf: test linking against empty C object
1 parent eb497c5
Changed files (1)
test
link
test/link/elf.zig
@@ -12,24 +12,45 @@ 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.expectExitCode(0);
+    test_step.dependOn(&run.step);
+
+    return test_step;
+}
+
+fn testLinkingC(b: *Build, opts: Options) *Step {
+    const test_step = addTestStep(b, "linking-c-static", opts);
+
+    const exe = addExecutable(b, opts);
+    addCSourceBytes(exe,
+        \\#include <stdio.h>
+        \\int main() {
+        \\  printf("Hello World!\n");
+        \\  return 0;
         \\}
     );
+    exe.is_linking_libc = true;
 
     const run = b.addRunArtifact(exe);
-    run.expectStdErrEqual("Hello World!\n");
+    run.expectStdOutEqual("Hello World!\n");
     test_step.dependOn(&run.step);
 
     const check = exe.checkObject();
@@ -44,21 +65,18 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {
     return test_step;
 }
 
-fn testLinkingC(b: *Build, opts: Options) *Step {
-    const test_step = addTestStep(b, "linking-c-static", opts);
+fn testLinkingZig(b: *Build, opts: Options) *Step {
+    const test_step = addTestStep(b, "linking-zig-static", opts);
 
     const exe = addExecutable(b, opts);
-    addCSourceBytes(exe,
-        \\#include <stdio.h>
-        \\int main() {
-        \\  printf("Hello World!\n");
-        \\  return 0;
+    addZigSourceBytes(exe,
+        \\pub fn main() void {
+        \\    @import("std").debug.print("Hello World!\n", .{});
         \\}
     );
-    exe.is_linking_libc = true;
 
     const run = b.addRunArtifact(exe);
-    run.expectStdOutEqual("Hello World!\n");
+    run.expectStdErrEqual("Hello World!\n");
     test_step.dependOn(&run.step);
 
     const check = exe.checkObject();