authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-02 16:36:58-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-03 09:52:15-07:00
logc012f5d55dc59f80a3cd4fce22443a39fcbb67e9
treea231342c69ad42700a726c70eef653fe739e944d
parente582a3642bb001a7a19ecb72b128f45ab7fc08aa

std.zig.system.darwin.isSdkInstalled: fix implementation

* don't assert that the child process doesn't crash * don't give a false negative on warnings printed to stderr Also fix getSdk from the same file in the same way

1 files changed, 24 insertions(+), 20 deletions(-)

lib/std/zig/system/darwin.zig+24-20
...@@ -8,27 +8,33 @@ pub const macos = @import("darwin/macos.zig");...@@ -8,27 +8,33 @@ pub const macos = @import("darwin/macos.zig");
88
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#L63013/// stderr from xcode-select is ignored.
14/// If error.OutOfMemory occurs in Allocator, this function returns null.
14pub fn isSdkInstalled(allocator: Allocator) bool {15pub 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}
2731
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.
32pub fn getSdk(allocator: Allocator, target: Target) ?Sdk {38pub 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 anyhow55 .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 anyhow69 .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{