| ... | @@ -6,11 +6,30 @@ const Version = std.builtin.Version; | ... | @@ -6,11 +6,30 @@ const Version = std.builtin.Version; |
| 6 | | 6 | |
| 7 | pub const macos = @import("darwin/macos.zig"); | 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 | /// Detect SDK on Darwin. | 28 | /// Detect SDK on Darwin. |
| 10 | /// Calls `xcrun --sdk <target_sdk> --show-sdk-path` which fetches the path to the SDK sysroot (if any). | 29 | /// Calls `xcrun --sdk <target_sdk> --show-sdk-path` which fetches the path to the SDK sysroot (if any). |
| 11 | /// Subsequently calls `xcrun --sdk <target_sdk> --show-sdk-version` which fetches version of the SDK. | 30 | /// Subsequently calls `xcrun --sdk <target_sdk> --show-sdk-version` which fetches version of the SDK. |
| 12 | /// The caller needs to deinit the resulting struct. | 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 | const is_simulator_abi = target.abi == .simulator; | 33 | const is_simulator_abi = target.abi == .simulator; |
| 15 | const sdk = switch (target.os.tag) { | 34 | const sdk = switch (target.os.tag) { |
| 16 | .macos => "macosx", | 35 | .macos => "macosx", |
| ... | @@ -20,8 +39,8 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK { | ... | @@ -20,8 +39,8 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK { |
| 20 | else => return null, | 39 | else => return null, |
| 21 | }; | 40 | }; |
| 22 | const path = path: { | 41 | const path = path: { |
| 23 | const argv = &[_][]const u8{ "xcrun", "--sdk", sdk, "--show-sdk-path" }; | 42 | const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-path" }; |
| 24 | const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }); | 43 | const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return null; |
| 25 | defer { | 44 | defer { |
| 26 | allocator.free(result.stderr); | 45 | allocator.free(result.stderr); |
| 27 | allocator.free(result.stdout); | 46 | allocator.free(result.stdout); |
| ... | @@ -31,12 +50,12 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK { | ... | @@ -31,12 +50,12 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK { |
| 31 | // and in the worst case the user can specify the sysroot manually. | 50 | // and in the worst case the user can specify the sysroot manually. |
| 32 | return null; | 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 | break :path path; | 54 | break :path path; |
| 36 | }; | 55 | }; |
| 37 | const version = version: { | 56 | const version = version: { |
| 38 | const argv = &[_][]const u8{ "xcrun", "--sdk", sdk, "--show-sdk-version" }; | 57 | const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-version" }; |
| 39 | const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }); | 58 | const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return null; |
| 40 | defer { | 59 | defer { |
| 41 | allocator.free(result.stderr); | 60 | allocator.free(result.stderr); |
| 42 | allocator.free(result.stdout); | 61 | allocator.free(result.stdout); |