| ... | @@ -8,27 +8,33 @@ pub const macos = @import("darwin/macos.zig"); | ... | @@ -8,27 +8,33 @@ pub const macos = @import("darwin/macos.zig"); |
| 8 | | 8 | |
| 9 | /// Check if SDK is installed on Darwin without triggering CLT installation popup window. | 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. | 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` | 11 | /// Therefore, we resort to invoking `xcode-select --print-path` and checking |
| 12 | /// and checking if the status is nonzero or the returned string in nonempty. | 12 | /// if the status is nonzero. |
| 13 | /// https://github.com/Homebrew/brew/blob/e119bdc571dcb000305411bc1e26678b132afb98/Library/Homebrew/brew.sh#L630 | 13 | /// stderr from xcode-select is ignored. |
| | 14 | /// If error.OutOfMemory occurs in Allocator, this function returns null. |
| 14 | pub fn isSdkInstalled(allocator: Allocator) bool { | 15 | pub fn isSdkInstalled(allocator: Allocator) bool { |
| 15 | const argv = &[_][]const u8{ "/usr/bin/xcode-select", "--print-path" }; | 16 | const result = std.process.Child.exec(.{ |
| 16 | const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return false; | 17 | .allocator = allocator, |
| | 18 | .argv = &.{ "/usr/bin/xcode-select", "--print-path" }, |
| | 19 | }) catch return false; |
| | 20 | |
| 17 | defer { | 21 | defer { |
| 18 | allocator.free(result.stderr); | 22 | allocator.free(result.stderr); |
| 19 | allocator.free(result.stdout); | 23 | allocator.free(result.stdout); |
| 20 | } | 24 | } |
| 21 | if (result.stderr.len != 0 or result.term.Exited != 0) { | 25 | |
| 22 | // We don't actually care if there were errors as this is best-effort check anyhow. | 26 | return switch (result.term) { |
| 23 | return false; | 27 | .Exited => |code| if (code == 0) result.stdout.len > 0 else false, |
| 24 | } | 28 | else => false, |
| 25 | return result.stdout.len > 0; | 29 | }; |
| 26 | } | 30 | } |
| 27 | | 31 | |
| 28 | /// Detect SDK on Darwin. | 32 | /// Detect SDK on Darwin. |
| 29 | /// Calls `xcrun --sdk <target_sdk> --show-sdk-path` which fetches the path to the SDK sysroot (if any). | 33 | /// Calls `xcrun --sdk <target_sdk> --show-sdk-path` which fetches the path to the SDK sysroot (if any). |
| 30 | /// Subsequently calls `xcrun --sdk <target_sdk> --show-sdk-version` which fetches version of the SDK. | 34 | /// Subsequently calls `xcrun --sdk <target_sdk> --show-sdk-version` which fetches version of the SDK. |
| 31 | /// The caller needs to deinit the resulting struct. | 35 | /// The caller needs to deinit the resulting struct. |
| | 36 | /// stderr from xcrun is ignored. |
| | 37 | /// If error.OutOfMemory occurs in Allocator, this function returns null. |
| 32 | pub fn getSdk(allocator: Allocator, target: Target) ?Sdk { | 38 | pub fn getSdk(allocator: Allocator, target: Target) ?Sdk { |
| 33 | const is_simulator_abi = target.abi == .simulator; | 39 | const is_simulator_abi = target.abi == .simulator; |
| 34 | const sdk = switch (target.os.tag) { | 40 | const sdk = switch (target.os.tag) { |
| ... | @@ -40,30 +46,28 @@ pub fn getSdk(allocator: Allocator, target: Target) ?Sdk { | ... | @@ -40,30 +46,28 @@ pub fn getSdk(allocator: Allocator, target: Target) ?Sdk { |
| 40 | }; | 46 | }; |
| 41 | const path = path: { | 47 | const path = path: { |
| 42 | const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-path" }; | 48 | 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; | 49 | const result = std.process.Child.exec(.{ .allocator = allocator, .argv = argv }) catch return null; |
| 44 | defer { | 50 | defer { |
| 45 | allocator.free(result.stderr); | 51 | allocator.free(result.stderr); |
| 46 | allocator.free(result.stdout); | 52 | allocator.free(result.stdout); |
| 47 | } | 53 | } |
| 48 | if (result.stderr.len != 0 or result.term.Exited != 0) { | 54 | switch (result.term) { |
| 49 | // We don't actually care if there were errors as this is best-effort check anyhow | 55 | .Exited => |code| if (code != 0) return null, |
| 50 | // and in the worst case the user can specify the sysroot manually. | 56 | else => return null, |
| 51 | return null; | | |
| 52 | } | 57 | } |
| 53 | const path = allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n")) catch return null; | 58 | const path = allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n")) catch return null; |
| 54 | break :path path; | 59 | break :path path; |
| 55 | }; | 60 | }; |
| 56 | const version = version: { | 61 | const version = version: { |
| 57 | const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-version" }; | 62 | 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; | 63 | const result = std.process.Child.exec(.{ .allocator = allocator, .argv = argv }) catch return null; |
| 59 | defer { | 64 | defer { |
| 60 | allocator.free(result.stderr); | 65 | allocator.free(result.stderr); |
| 61 | allocator.free(result.stdout); | 66 | allocator.free(result.stdout); |
| 62 | } | 67 | } |
| 63 | if (result.stderr.len != 0 or result.term.Exited != 0) { | 68 | switch (result.term) { |
| 64 | // We don't actually care if there were errors as this is best-effort check anyhow | 69 | .Exited => |code| if (code != 0) return null, |
| 65 | // and in the worst case the user can specify the sysroot manually. | 70 | else => return null, |
| 66 | return null; | | |
| 67 | } | 71 | } |
| 68 | const raw_version = mem.trimRight(u8, result.stdout, "\r\n"); | 72 | const raw_version = mem.trimRight(u8, result.stdout, "\r\n"); |
| 69 | const version = parseSdkVersion(raw_version) orelse Version{ | 73 | const version = parseSdkVersion(raw_version) orelse Version{ |