| ... | @@ -1,144 +0,0 @@ |
| 1 | const std = @import("std"); |
| 2 | const FileSource = std.Build.FileSource; |
| 3 | const Step = std.Build.Step; |
| 4 | |
| 5 | pub const requires_symlinks = true; |
| 6 | |
| 7 | pub fn build(b: *std.Build) void { |
| 8 | const test_step = b.step("test", "Test"); |
| 9 | b.default_step = test_step; |
| 10 | |
| 11 | // We force cross-compilation to ensure we always pick a generic CPU with |
| 12 | // constant set of CPU features. |
| 13 | const aarch64_macos = std.zig.CrossTarget{ |
| 14 | .cpu_arch = .aarch64, |
| 15 | .os_tag = .macos, |
| 16 | }; |
| 17 | |
| 18 | testUuid(b, test_step, .ReleaseSafe, aarch64_macos); |
| 19 | testUuid(b, test_step, .ReleaseFast, aarch64_macos); |
| 20 | testUuid(b, test_step, .ReleaseSmall, aarch64_macos); |
| 21 | |
| 22 | const x86_64_macos = std.zig.CrossTarget{ |
| 23 | .cpu_arch = .x86_64, |
| 24 | .os_tag = .macos, |
| 25 | }; |
| 26 | |
| 27 | testUuid(b, test_step, .ReleaseSafe, x86_64_macos); |
| 28 | testUuid(b, test_step, .ReleaseFast, x86_64_macos); |
| 29 | testUuid(b, test_step, .ReleaseSmall, x86_64_macos); |
| 30 | } |
| 31 | |
| 32 | fn testUuid( |
| 33 | b: *std.Build, |
| 34 | test_step: *std.Build.Step, |
| 35 | optimize: std.builtin.OptimizeMode, |
| 36 | target: std.zig.CrossTarget, |
| 37 | ) void { |
| 38 | // The calculated UUID value is independent of debug info and so it should |
| 39 | // stay the same across builds. |
| 40 | { |
| 41 | const dylib = simpleDylib(b, optimize, target); |
| 42 | const install_step = b.addInstallArtifact(dylib); |
| 43 | install_step.dest_sub_path = "test1.dylib"; |
| 44 | install_step.step.dependOn(&dylib.step); |
| 45 | } |
| 46 | { |
| 47 | const dylib = simpleDylib(b, optimize, target); |
| 48 | dylib.strip = true; |
| 49 | const install_step = b.addInstallArtifact(dylib); |
| 50 | install_step.dest_sub_path = "test2.dylib"; |
| 51 | install_step.step.dependOn(&dylib.step); |
| 52 | } |
| 53 | |
| 54 | const cmp_step = CompareUuid.create(b, "test1.dylib", "test2.dylib"); |
| 55 | test_step.dependOn(&cmp_step.step); |
| 56 | } |
| 57 | |
| 58 | fn simpleDylib( |
| 59 | b: *std.Build, |
| 60 | optimize: std.builtin.OptimizeMode, |
| 61 | target: std.zig.CrossTarget, |
| 62 | ) *std.Build.Step.Compile { |
| 63 | const dylib = b.addSharedLibrary(.{ |
| 64 | .name = "test", |
| 65 | .version = .{ .major = 1, .minor = 0, .patch = 0 }, |
| 66 | .optimize = optimize, |
| 67 | .target = target, |
| 68 | }); |
| 69 | dylib.addCSourceFile("test.c", &.{}); |
| 70 | dylib.linkLibC(); |
| 71 | return dylib; |
| 72 | } |
| 73 | |
| 74 | const CompareUuid = struct { |
| 75 | pub const base_id = .custom; |
| 76 | |
| 77 | step: Step, |
| 78 | lhs: []const u8, |
| 79 | rhs: []const u8, |
| 80 | |
| 81 | pub fn create(owner: *std.Build, lhs: []const u8, rhs: []const u8) *CompareUuid { |
| 82 | const self = owner.allocator.create(CompareUuid) catch @panic("OOM"); |
| 83 | self.* = CompareUuid{ |
| 84 | .step = Step.init(.{ |
| 85 | .id = base_id, |
| 86 | .name = owner.fmt("compare uuid: {s} and {s}", .{ |
| 87 | lhs, |
| 88 | rhs, |
| 89 | }), |
| 90 | .owner = owner, |
| 91 | .makeFn = make, |
| 92 | }), |
| 93 | .lhs = lhs, |
| 94 | .rhs = rhs, |
| 95 | }; |
| 96 | return self; |
| 97 | } |
| 98 | |
| 99 | fn make(step: *Step, prog_node: *std.Progress.Node) anyerror!void { |
| 100 | _ = prog_node; |
| 101 | const b = step.owner; |
| 102 | const self = @fieldParentPtr(CompareUuid, "step", step); |
| 103 | const gpa = b.allocator; |
| 104 | |
| 105 | var lhs_uuid: [16]u8 = undefined; |
| 106 | const lhs_path = b.getInstallPath(.lib, self.lhs); |
| 107 | try parseUuid(gpa, lhs_path, &lhs_uuid); |
| 108 | |
| 109 | var rhs_uuid: [16]u8 = undefined; |
| 110 | const rhs_path = b.getInstallPath(.lib, self.rhs); |
| 111 | try parseUuid(gpa, rhs_path, &rhs_uuid); |
| 112 | |
| 113 | try std.testing.expectEqualStrings(&lhs_uuid, &rhs_uuid); |
| 114 | } |
| 115 | |
| 116 | fn parseUuid(gpa: std.mem.Allocator, path: []const u8, uuid: *[16]u8) anyerror!void { |
| 117 | const max_bytes: usize = 20 * 1024 * 1024; |
| 118 | const data = try std.fs.cwd().readFileAllocOptions( |
| 119 | gpa, |
| 120 | path, |
| 121 | max_bytes, |
| 122 | null, |
| 123 | @alignOf(u64), |
| 124 | null, |
| 125 | ); |
| 126 | var stream = std.io.fixedBufferStream(data); |
| 127 | const reader = stream.reader(); |
| 128 | |
| 129 | const hdr = try reader.readStruct(std.macho.mach_header_64); |
| 130 | if (hdr.magic != std.macho.MH_MAGIC_64) { |
| 131 | return error.InvalidMagicNumber; |
| 132 | } |
| 133 | |
| 134 | var it = std.macho.LoadCommandIterator{ |
| 135 | .ncmds = hdr.ncmds, |
| 136 | .buffer = data[@sizeOf(std.macho.mach_header_64)..][0..hdr.sizeofcmds], |
| 137 | }; |
| 138 | const cmd = while (it.next()) |cmd| switch (cmd.cmd()) { |
| 139 | .UUID => break cmd.cast(std.macho.uuid_command).?, |
| 140 | else => {}, |
| 141 | } else return error.UuidLoadCommandNotFound; |
| 142 | std.mem.copy(u8, uuid, &cmd.uuid); |
| 143 | } |
| 144 | }; |