| ... | ... | @@ -66,7 +66,7 @@ pub fn getDarwinSDK(allocator: Allocator, target: Target) ?DarwinSDK { |
| 66 | 66 | return null; |
| 67 | 67 | } |
| 68 | 68 | const raw_version = mem.trimRight(u8, result.stdout, "\r\n"); |
| 69 | | const version = Version.parse(raw_version) catch Version{ |
| 69 | const version = parseSdkVersion(raw_version) orelse Version{ |
| 70 | 70 | .major = 0, |
| 71 | 71 | .minor = 0, |
| 72 | 72 | .patch = 0, |
| ... | ... | @@ -79,6 +79,23 @@ pub fn getDarwinSDK(allocator: Allocator, target: Target) ?DarwinSDK { |
| 79 | 79 | }; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | // Versions reported by Apple aren't exactly semantically valid as they usually omit |
| 83 | // the patch component. Hence, we do a simple check for the number of components and |
| 84 | // add the missing patch value if needed. |
| 85 | fn parseSdkVersion(raw: []const u8) ?Version { |
| 86 | var buffer: [128]u8 = undefined; |
| 87 | if (raw.len > buffer.len) return null; |
| 88 | @memcpy(buffer[0..raw.len], raw); |
| 89 | const dots_count = mem.count(u8, raw, "."); |
| 90 | if (dots_count < 1) return null; |
| 91 | const len = if (dots_count < 2) blk: { |
| 92 | const patch_suffix = ".0"; |
| 93 | buffer[raw.len..][0..patch_suffix.len].* = patch_suffix.*; |
| 94 | break :blk raw.len + patch_suffix.len; |
| 95 | } else raw.len; |
| 96 | return Version.parse(buffer[0..len]) catch null; |
| 97 | } |
| 98 | |
| 82 | 99 | pub const DarwinSDK = struct { |
| 83 | 100 | path: []const u8, |
| 84 | 101 | version: Version, |
| ... | ... | @@ -91,3 +108,23 @@ pub const DarwinSDK = struct { |
| 91 | 108 | test { |
| 92 | 109 | _ = macos; |
| 93 | 110 | } |
| 111 | |
| 112 | const expect = std.testing.expect; |
| 113 | const expectEqual = std.testing.expectEqual; |
| 114 | |
| 115 | fn testParseSdkVersionSuccess(exp: Version, raw: []const u8) !void { |
| 116 | const maybe_ver = parseSdkVersion(raw); |
| 117 | try expect(maybe_ver != null); |
| 118 | const ver = maybe_ver.?; |
| 119 | try expectEqual(exp.major, ver.major); |
| 120 | try expectEqual(exp.minor, ver.minor); |
| 121 | try expectEqual(exp.patch, ver.patch); |
| 122 | } |
| 123 | |
| 124 | test "parseSdkVersion" { |
| 125 | try testParseSdkVersionSuccess(.{ .major = 13, .minor = 4, .patch = 0 }, "13.4"); |
| 126 | try testParseSdkVersionSuccess(.{ .major = 13, .minor = 4, .patch = 1 }, "13.4.1"); |
| 127 | try testParseSdkVersionSuccess(.{ .major = 11, .minor = 15, .patch = 0 }, "11.15"); |
| 128 | |
| 129 | try expect(parseSdkVersion("11") == null); |
| 130 | } |