mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
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
This commit is contained in:
parent
e582a3642b
commit
c012f5d55d
1 changed files with 24 additions and 20 deletions
|
|
@ -8,27 +8,33 @@ pub const macos = @import("darwin/macos.zig");
|
||||||
|
|
||||||
/// Check if SDK is installed on Darwin without triggering CLT installation popup window.
|
/// Check if SDK is installed on Darwin without triggering CLT installation popup window.
|
||||||
/// Note: simply invoking `xcrun` will inevitably trigger the CLT installation popup.
|
/// Note: simply invoking `xcrun` will inevitably trigger the CLT installation popup.
|
||||||
/// Therefore, we resort to the same tool used by Homebrew, namely, invoking `xcode-select --print-path`
|
/// Therefore, we resort to invoking `xcode-select --print-path` and checking
|
||||||
/// and checking if the status is nonzero or the returned string in nonempty.
|
/// if the status is nonzero.
|
||||||
/// https://github.com/Homebrew/brew/blob/e119bdc571dcb000305411bc1e26678b132afb98/Library/Homebrew/brew.sh#L630
|
/// stderr from xcode-select is ignored.
|
||||||
|
/// If error.OutOfMemory occurs in Allocator, this function returns null.
|
||||||
pub fn isSdkInstalled(allocator: Allocator) bool {
|
pub fn isSdkInstalled(allocator: Allocator) bool {
|
||||||
const argv = &[_][]const u8{ "/usr/bin/xcode-select", "--print-path" };
|
const result = std.process.Child.exec(.{
|
||||||
const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return false;
|
.allocator = allocator,
|
||||||
|
.argv = &.{ "/usr/bin/xcode-select", "--print-path" },
|
||||||
|
}) catch return false;
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
allocator.free(result.stderr);
|
allocator.free(result.stderr);
|
||||||
allocator.free(result.stdout);
|
allocator.free(result.stdout);
|
||||||
}
|
}
|
||||||
if (result.stderr.len != 0 or result.term.Exited != 0) {
|
|
||||||
// We don't actually care if there were errors as this is best-effort check anyhow.
|
return switch (result.term) {
|
||||||
return false;
|
.Exited => |code| if (code == 0) result.stdout.len > 0 else false,
|
||||||
}
|
else => false,
|
||||||
return result.stdout.len > 0;
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Detect SDK on Darwin.
|
/// Detect SDK on Darwin.
|
||||||
/// Calls `xcrun --sdk <target_sdk> --show-sdk-path` which fetches the path to the SDK sysroot (if any).
|
/// Calls `xcrun --sdk <target_sdk> --show-sdk-path` which fetches the path to the SDK sysroot (if any).
|
||||||
/// Subsequently calls `xcrun --sdk <target_sdk> --show-sdk-version` which fetches version of the SDK.
|
/// Subsequently calls `xcrun --sdk <target_sdk> --show-sdk-version` which fetches version of the SDK.
|
||||||
/// The caller needs to deinit the resulting struct.
|
/// The caller needs to deinit the resulting struct.
|
||||||
|
/// stderr from xcrun is ignored.
|
||||||
|
/// If error.OutOfMemory occurs in Allocator, this function returns null.
|
||||||
pub fn getSdk(allocator: Allocator, target: Target) ?Sdk {
|
pub fn getSdk(allocator: Allocator, target: Target) ?Sdk {
|
||||||
const is_simulator_abi = target.abi == .simulator;
|
const is_simulator_abi = target.abi == .simulator;
|
||||||
const sdk = switch (target.os.tag) {
|
const sdk = switch (target.os.tag) {
|
||||||
|
|
@ -40,30 +46,28 @@ pub fn getSdk(allocator: Allocator, target: Target) ?Sdk {
|
||||||
};
|
};
|
||||||
const path = path: {
|
const path = path: {
|
||||||
const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-path" };
|
const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-path" };
|
||||||
const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return null;
|
const result = std.process.Child.exec(.{ .allocator = allocator, .argv = argv }) catch return null;
|
||||||
defer {
|
defer {
|
||||||
allocator.free(result.stderr);
|
allocator.free(result.stderr);
|
||||||
allocator.free(result.stdout);
|
allocator.free(result.stdout);
|
||||||
}
|
}
|
||||||
if (result.stderr.len != 0 or result.term.Exited != 0) {
|
switch (result.term) {
|
||||||
// We don't actually care if there were errors as this is best-effort check anyhow
|
.Exited => |code| if (code != 0) return null,
|
||||||
// and in the worst case the user can specify the sysroot manually.
|
else => return null,
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
const path = allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n")) catch return null;
|
const path = allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n")) catch return null;
|
||||||
break :path path;
|
break :path path;
|
||||||
};
|
};
|
||||||
const version = version: {
|
const version = version: {
|
||||||
const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-version" };
|
const argv = &[_][]const u8{ "/usr/bin/xcrun", "--sdk", sdk, "--show-sdk-version" };
|
||||||
const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return null;
|
const result = std.process.Child.exec(.{ .allocator = allocator, .argv = argv }) catch return null;
|
||||||
defer {
|
defer {
|
||||||
allocator.free(result.stderr);
|
allocator.free(result.stderr);
|
||||||
allocator.free(result.stdout);
|
allocator.free(result.stdout);
|
||||||
}
|
}
|
||||||
if (result.stderr.len != 0 or result.term.Exited != 0) {
|
switch (result.term) {
|
||||||
// We don't actually care if there were errors as this is best-effort check anyhow
|
.Exited => |code| if (code != 0) return null,
|
||||||
// and in the worst case the user can specify the sysroot manually.
|
else => return null,
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
const raw_version = mem.trimRight(u8, result.stdout, "\r\n");
|
const raw_version = mem.trimRight(u8, result.stdout, "\r\n");
|
||||||
const version = parseSdkVersion(raw_version) orelse Version{
|
const version = parseSdkVersion(raw_version) orelse Version{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue