const std = @import("std");
const mem = std.mem;
const Allocator = mem.Allocator;
const Target = std.Target;
const Version = std.builtin.Version;
pub const macos = @import("darwin/macos.zig");
pub fn isDarwinSDKInstalled(allocator: Allocator) bool {
const argv = &[_][]const u8{ "/usr/bin/xcode-select", "--print-path" };
const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch return false;
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
}
if (result.stderr.len != 0 or result.term.Exited != 0) {
return false;
}
return result.stdout.len > 0;
}
pub fn getDarwinSDK(allocator: Allocator, target: Target) ?DarwinSDK {
const is_simulator_abi = target.abi == .simulator;
const sdk = switch (target.os.tag) {
.macos => "macosx",
.ios => if (is_simulator_abi) "iphonesimulator" else "iphoneos",
.watchos => if (is_simulator_abi) "watchsimulator" else "watchos",
.tvos => if (is_simulator_abi) "appletvsimulator" else "appletvos",
else => return null,
};
const path = 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;
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
}
if (result.stderr.len != 0 or result.term.Exited != 0) {
return null;
}
const path = allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n")) catch return null;
break :path path;
};
const version = 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;
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
}
if (result.stderr.len != 0 or result.term.Exited != 0) {
return null;
}
const raw_version = mem.trimRight(u8, result.stdout, "\r\n");
const version = Version.parse(raw_version) catch Version{
.major = 0,
.minor = 0,
};
break :version version;
};
return DarwinSDK{
.path = path,
.version = version,
};
}
pub const DarwinSDK = struct {
path: []const u8,
version: Version,
pub fn deinit(self: DarwinSDK, allocator: Allocator) void {
allocator.free(self.path);
}
};
test {
_ = macos;
}