| ... | @@ -4,13 +4,70 @@ | ... | @@ -4,13 +4,70 @@ |
| 4 | pub fn testAll(b: *std.Build) *Step { | 4 | pub fn testAll(b: *std.Build) *Step { |
| 5 | const macho_step = b.step("test-macho", "Run MachO tests"); | 5 | const macho_step = b.step("test-macho", "Run MachO tests"); |
| 6 | | 6 | |
| 7 | macho_step.dependOn(testSectionBoundarySymbols(b, .{ | 7 | const default_target = b.resolveTargetQuery(.{ |
| 8 | .target = b.resolveTargetQuery(.{ .os_tag = .macos }), | 8 | .os_tag = .macos, |
| 9 | })); | 9 | }); |
| | 10 | |
| | 11 | macho_step.dependOn(testEntryPointDylib(b, .{ .target = default_target })); |
| | 12 | macho_step.dependOn(testSectionBoundarySymbols(b, .{ .target = default_target })); |
| 10 | | 13 | |
| 11 | return macho_step; | 14 | return macho_step; |
| 12 | } | 15 | } |
| 13 | | 16 | |
| | 17 | fn testEntryPointDylib(b: *std.Build, opts: Options) *Step { |
| | 18 | const test_step = addTestStep(b, "macho-entry-point-dylib", opts); |
| | 19 | |
| | 20 | const dylib = addSharedLibrary(b, opts, .{ .name = "liba.dylib" }); |
| | 21 | addCSourceBytes(dylib, |
| | 22 | \\extern int my_main(); |
| | 23 | \\int bootstrap() { |
| | 24 | \\ return my_main(); |
| | 25 | \\} |
| | 26 | , &.{}); |
| | 27 | dylib.linker_allow_shlib_undefined = true; |
| | 28 | |
| | 29 | const exe = addExecutable(b, opts, .{ .name = "main" }); |
| | 30 | addCSourceBytes(dylib, |
| | 31 | \\#include<stdio.h> |
| | 32 | \\int my_main() { |
| | 33 | \\ fprintf(stdout, "Hello!\n"); |
| | 34 | \\ return 0; |
| | 35 | \\} |
| | 36 | , &.{}); |
| | 37 | exe.linkLibrary(dylib); |
| | 38 | exe.entry = .{ .symbol_name = "_bootstrap" }; |
| | 39 | exe.forceUndefinedSymbol("_my_main"); |
| | 40 | |
| | 41 | const check = exe.checkObject(); |
| | 42 | check.checkInHeaders(); |
| | 43 | check.checkExact("segname __TEXT"); |
| | 44 | check.checkExtract("vmaddr {text_vmaddr}"); |
| | 45 | check.checkInHeaders(); |
| | 46 | check.checkExact("sectname __stubs"); |
| | 47 | check.checkExtract("addr {stubs_vmaddr}"); |
| | 48 | check.checkInHeaders(); |
| | 49 | check.checkExact("sectname __stubs"); |
| | 50 | check.checkExtract("size {stubs_vmsize}"); |
| | 51 | check.checkInHeaders(); |
| | 52 | check.checkExact("cmd MAIN"); |
| | 53 | check.checkExtract("entryoff {entryoff}"); |
| | 54 | check.checkComputeCompare("text_vmaddr entryoff +", .{ |
| | 55 | .op = .gte, |
| | 56 | .value = .{ .variable = "stubs_vmaddr" }, // The entrypoint should be a synthetic stub |
| | 57 | }); |
| | 58 | check.checkComputeCompare("text_vmaddr entryoff + stubs_vmaddr -", .{ |
| | 59 | .op = .lt, |
| | 60 | .value = .{ .variable = "stubs_vmsize" }, // The entrypoint should be a synthetic stub |
| | 61 | }); |
| | 62 | test_step.dependOn(&check.step); |
| | 63 | |
| | 64 | const run = addRunArtifact(exe); |
| | 65 | run.expectStdOutEqual("Hello!\n"); |
| | 66 | test_step.dependOn(&run.step); |
| | 67 | |
| | 68 | return test_step; |
| | 69 | } |
| | 70 | |
| 14 | fn testSectionBoundarySymbols(b: *std.Build, opts: Options) *Step { | 71 | fn testSectionBoundarySymbols(b: *std.Build, opts: Options) *Step { |
| 15 | const test_step = addTestStep(b, "macho-section-boundary-symbols", opts); | 72 | const test_step = addTestStep(b, "macho-section-boundary-symbols", opts); |
| 16 | | 73 | |
| ... | @@ -95,8 +152,11 @@ fn addTestStep(b: *std.Build, comptime prefix: []const u8, opts: Options) *Step | ... | @@ -95,8 +152,11 @@ fn addTestStep(b: *std.Build, comptime prefix: []const u8, opts: Options) *Step |
| 95 | return link.addTestStep(b, "macho-" ++ prefix, opts); | 152 | return link.addTestStep(b, "macho-" ++ prefix, opts); |
| 96 | } | 153 | } |
| 97 | | 154 | |
| | 155 | const addCSourceBytes = link.addCSourceBytes; |
| | 156 | const addRunArtifact = link.addRunArtifact; |
| 98 | const addObject = link.addObject; | 157 | const addObject = link.addObject; |
| 99 | const addExecutable = link.addExecutable; | 158 | const addExecutable = link.addExecutable; |
| | 159 | const addSharedLibrary = link.addSharedLibrary; |
| 100 | const expectLinkErrors = link.expectLinkErrors; | 160 | const expectLinkErrors = link.expectLinkErrors; |
| 101 | const link = @import("link.zig"); | 161 | const link = @import("link.zig"); |
| 102 | const std = @import("std"); | 162 | const std = @import("std"); |