| ... | ... | @@ -6,11 +6,30 @@ const Version = std.builtin.Version; |
| 6 | 6 | |
| 7 | 7 | pub const macos = @import("darwin/macos.zig"); |
| 8 | 8 | |
| 9 | /// Check if SDK is installed on Darwin without triggering CLT installation popup window. |
| 10 | /// Note: simply invoking `xcrun` will inevitably trigger the CLT installation popup. |
| 11 | /// Therefore, we resort to the same tool used by Homebrew, namely, invoking `xcode-select --print-path` |
| 12 | /// and checking if the status is nonzero or the returned string in nonempty. |
| 13 | /// https://github.com/Homebrew/brew/blob/e119bdc571dcb000305411bc1e26678b132afb98/Library/Homebrew/brew.sh#L630 |
| 14 | pub fn isDarwinSDKInstalled(allocator: *Allocator) bool { |
| 15 | const argv = &[_][]const u8{ "/usr/bin/xcode-select", "--print-path" }; |
| 16 | const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return false; |
| 17 | defer { |
| 18 | allocator.free(result.stderr); |
| 19 | allocator.free(result.stdout); |
| 20 | } |
| 21 | if (result.stderr.len != 0 or result.term.Exited != 0) { |
| 22 | // We don't actually care if there were errors as this is best-effort check anyhow. |
| 23 | return false; |
| 24 | } |
| 25 | return result.stdout.len > 0; |
| 26 | } |
| 27 | |
| 9 | 28 | /// Detect SDK on Darwin. |
| 10 | 29 | /// Calls `xcrun --sdk <target_sdk> --show-sdk-path` which fetches the path to the SDK sysroot (if any). |
| 11 | 30 | /// Subsequently calls `xcrun --sdk <target_sdk> --show-sdk-version` which fetches version of the SDK. |
| 12 | 31 | /// The caller needs to deinit the resulting struct. |
| 13 | | pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK { |
| 32 | pub fn getDarwinSDK(allocator: *Allocator, target: Target) ?DarwinSDK { |
| 14 | 33 | const is_simulator_abi = target.abi == .simulator; |
| 15 | 34 | const sdk = switch (target.os.tag) { |
| 16 | 35 | .macos => "macosx", |
| ... | ... | @@ -20,8 +39,8 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK { |
| 20 | 39 | else => return null, |
| 21 | 40 | }; |
| 22 | 41 | const path = path: { |
| 23 | | const argv = &[_][]const u8{ "xcrun", "--sdk", sdk, "--show-sdk-path" }; |
| 24 | | const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }); |
| 42 | const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-path" }; |
| 43 | const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return null; |
| 25 | 44 | defer { |
| 26 | 45 | allocator.free(result.stderr); |
| 27 | 46 | allocator.free(result.stdout); |
| ... | ... | @@ -31,12 +50,12 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK { |
| 31 | 50 | // and in the worst case the user can specify the sysroot manually. |
| 32 | 51 | return null; |
| 33 | 52 | } |
| 34 | | const path = try allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n")); |
| 53 | const path = allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n")) catch return null; |
| 35 | 54 | break :path path; |
| 36 | 55 | }; |
| 37 | 56 | const version = version: { |
| 38 | | const argv = &[_][]const u8{ "xcrun", "--sdk", sdk, "--show-sdk-version" }; |
| 39 | | const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }); |
| 57 | const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-version" }; |
| 58 | const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return null; |
| 40 | 59 | defer { |
| 41 | 60 | allocator.free(result.stderr); |
| 42 | 61 | allocator.free(result.stdout); |