Commit 7566a8fbd8

Jakub Konka <kubkon@jakubkonka.com>
2023-11-10 13:44:43
test/link: spawn ELF and MachO tests from the same root test/link/link.zig
1 parent 197fd41
Changed files (4)
test/link/elf.zig
@@ -2,9 +2,8 @@
 //! Currently, we support linking x86_64 Linux, but in the future we
 //! will progressively relax those to exercise more combinations.
 
-pub fn build(b: *Build) void {
+pub fn testAll(b: *Build) *Step {
     const elf_step = b.step("test-elf", "Run ELF tests");
-    b.default_step = elf_step;
 
     const default_target = CrossTarget{
         .cpu_arch = .x86_64, // TODO relax this once ELF linker is able to handle other archs
@@ -123,6 +122,8 @@ pub fn build(b: *Build) void {
     elf_step.dependOn(testZNow(b, .{ .target = glibc_target }));
     elf_step.dependOn(testZStackSize(b, .{ .target = glibc_target }));
     elf_step.dependOn(testZText(b, .{ .target = glibc_target }));
+
+    return elf_step;
 }
 
 fn testAbsSymbols(b: *Build, opts: Options) *Step {
@@ -3582,7 +3583,6 @@ const std = @import("std");
 const Build = std.Build;
 const Compile = Step.Compile;
 const CrossTarget = std.zig.CrossTarget;
-const LazyPath = Build.LazyPath;
 const Run = Step.Run;
 const Step = Build.Step;
 const WriteFile = Step.WriteFile;
test/link/link.zig
@@ -0,0 +1,114 @@
+pub fn build(b: *Build) void {
+    const test_step = b.step("test-link", "Run link tests");
+    b.default_step = test_step;
+
+    test_step.dependOn(@import("elf.zig").testAll(b));
+    test_step.dependOn(@import("macho.zig").testAll(b));
+}
+
+pub const Options = struct {
+    target: CrossTarget,
+    optimize: std.builtin.OptimizeMode = .Debug,
+    use_llvm: bool = true,
+    use_lld: bool = false,
+};
+
+pub fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
+    const target = opts.target.zigTriple(b.allocator) catch @panic("OOM");
+    const optimize = @tagName(opts.optimize);
+    const use_llvm = if (opts.use_llvm) "llvm" else "no-llvm";
+    const name = std.fmt.allocPrint(b.allocator, "test-" ++ prefix ++ "-{s}-{s}-{s}", .{
+        target,
+        optimize,
+        use_llvm,
+    }) catch @panic("OOM");
+    return b.step(name, "");
+}
+
+pub fn addExecutable(b: *Build, name: []const u8, opts: Options) *Compile {
+    return b.addExecutable(.{
+        .name = name,
+        .target = opts.target,
+        .optimize = opts.optimize,
+        .use_llvm = opts.use_llvm,
+        .use_lld = opts.use_lld,
+    });
+}
+
+pub fn addObject(b: *Build, name: []const u8, opts: Options) *Compile {
+    return b.addObject(.{
+        .name = name,
+        .target = opts.target,
+        .optimize = opts.optimize,
+        .use_llvm = opts.use_llvm,
+        .use_lld = opts.use_lld,
+    });
+}
+
+pub fn addStaticLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
+    return b.addStaticLibrary(.{
+        .name = name,
+        .target = opts.target,
+        .optimize = opts.optimize,
+        .use_llvm = opts.use_llvm,
+        .use_lld = opts.use_lld,
+    });
+}
+
+pub fn addSharedLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
+    return b.addSharedLibrary(.{
+        .name = name,
+        .target = opts.target,
+        .optimize = opts.optimize,
+        .use_llvm = opts.use_llvm,
+        .use_lld = opts.use_lld,
+    });
+}
+
+pub fn addRunArtifact(comp: *Compile) *Run {
+    const b = comp.step.owner;
+    const run = b.addRunArtifact(comp);
+    run.skip_foreign_checks = true;
+    return run;
+}
+
+pub fn addZigSourceBytes(comp: *Compile, bytes: []const u8) void {
+    const b = comp.step.owner;
+    const file = WriteFile.create(b).add("a.zig", bytes);
+    file.addStepDependencies(&comp.step);
+    comp.root_src = file;
+}
+
+pub fn addCSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
+    const b = comp.step.owner;
+    const file = WriteFile.create(b).add("a.c", bytes);
+    comp.addCSourceFile(.{ .file = file, .flags = flags });
+}
+
+pub fn addCppSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
+    const b = comp.step.owner;
+    const file = WriteFile.create(b).add("a.cpp", bytes);
+    comp.addCSourceFile(.{ .file = file, .flags = flags });
+}
+
+pub fn addAsmSourceBytes(comp: *Compile, bytes: []const u8) void {
+    const b = comp.step.owner;
+    const actual_bytes = std.fmt.allocPrint(b.allocator, "{s}\n", .{bytes}) catch @panic("OOM");
+    const file = WriteFile.create(b).add("a.s", actual_bytes);
+    comp.addAssemblyFile(file);
+}
+
+pub fn expectLinkErrors(comp: *Compile, test_step: *Step, expected_errors: Compile.ExpectedCompileErrors) void {
+    comp.expect_errors = expected_errors;
+    const bin_file = comp.getEmittedBin();
+    bin_file.addStepDependencies(test_step);
+}
+
+const std = @import("std");
+
+const Build = std.Build;
+const Compile = Step.Compile;
+const CrossTarget = std.zig.CrossTarget;
+const Run = Step.Run;
+const Step = Build.Step;
+const WriteFile = Step.WriteFile;
test/link/macho.zig
@@ -0,0 +1,93 @@
+//! Here we test our MachO linker for correctness and functionality.
+//! TODO migrate standalone tests from test/link/macho/* to here.
+
+pub fn testAll(b: *Build) *Step {
+    const macho_step = b.step("test-macho", "Run MachO tests");
+
+    const default_target = CrossTarget{ .os_tag = .macos };
+
+    macho_step.dependOn(testResolvingBoundarySymbols(b, .{ .target = default_target }));
+
+    return macho_step;
+}
+
+fn testResolvingBoundarySymbols(b: *Build, opts: Options) *Step {
+    const test_step = addTestStep(b, "macho-resolving-boundary-symbols", opts);
+
+    const obj1 = addObject(b, "obj1", opts);
+    addCppSourceBytes(obj1,
+        \\constexpr const char* MESSAGE __attribute__((used, section("__DATA_CONST,__message_ptr"))) = "codebase";
+    , &.{});
+
+    const main_o = addObject(b, "main", opts);
+    addZigSourceBytes(main_o,
+        \\const std = @import("std");
+        \\extern fn interop() [*:0]const u8;
+        \\pub fn main() !void {
+        \\    std.debug.print("All your {s} are belong to us.\n", .{
+        \\        std.mem.span(interop()),
+        \\    });
+        \\}
+    );
+
+    {
+        const obj2 = addObject(b, "obj2", opts);
+        addCppSourceBytes(obj2,
+            \\extern const char* message_pointer __asm("section$start$__DATA_CONST$__message_ptr");
+            \\extern "C" const char* interop() {
+            \\  return message_pointer;
+            \\}
+        , &.{});
+
+        const exe = addExecutable(b, "test", opts);
+        exe.addObject(obj1);
+        exe.addObject(obj2);
+        exe.addObject(main_o);
+
+        const run = addRunArtifact(exe);
+        run.expectStdErrEqual("All your codebase are belong to us.\n");
+        test_step.dependOn(&run.step);
+
+        const check = exe.checkObject();
+        check.checkInSymtab();
+        check.checkNotPresent("section$start$__DATA_CONST$__message_ptr");
+        test_step.dependOn(&check.step);
+    }
+
+    {
+        const obj3 = addObject(b, "obj3", opts);
+        addCppSourceBytes(obj3,
+            \\extern const char* message_pointer __asm("section$start$__DATA$__message_ptr");
+            \\extern "C" const char* interop() {
+            \\  return message_pointer;
+            \\}
+        , &.{});
+
+        const exe = addExecutable(b, "test", opts);
+        exe.addObject(obj1);
+        exe.addObject(obj3);
+        exe.addObject(main_o);
+
+        expectLinkErrors(exe, test_step, .{ .exact = &.{
+            "section not found: __DATA,__message_ptr",
+            "note: while resolving section$start$__DATA$__message_ptr",
+        } });
+    }
+
+    return test_step;
+}
+
+const addCppSourceBytes = link.addCppSourceBytes;
+const addExecutable = link.addExecutable;
+const addObject = link.addObject;
+const addRunArtifact = link.addRunArtifact;
+const addTestStep = link.addTestStep;
+const addZigSourceBytes = link.addZigSourceBytes;
+const expectLinkErrors = link.expectLinkErrors;
+const link = @import("link.zig");
+const std = @import("std");
+
+const Build = std.Build;
+const CrossTarget = std.zig.CrossTarget;
+const Options = link.Options;
+const Step = Build.Step;
test/link.zig
@@ -4,6 +4,11 @@ pub const Case = struct {
 };
 
 pub const cases = [_]Case{
+    .{
+        .build_root = "test/link",
+        .import = @import("link/link.zig"),
+    },
+
     .{
         .build_root = "test/link/bss",
         .import = @import("link/bss/build.zig"),
@@ -29,12 +34,6 @@ pub const cases = [_]Case{
         .import = @import("link/glibc_compat/build.zig"),
     },
 
-    // Elf Cases
-    .{
-        .build_root = "test/link",
-        .import = @import("link/elf.zig"),
-    },
-
     // WASM Cases
     // https://github.com/ziglang/zig/issues/16938
     //.{