authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-26 18:09:14+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-26 18:09:14+01:00
loge2b6dfa6087f2e63bbc03e0430b73e900c95a193
treed831a33e04f90fb27558bce226985095ef981546
parenta956958ba9186edb612fbecc5205d6b35cd419a6

macos: do not trigger CLT installation popup when using zig cc

On a bare macOS, when there is no CLT/Xcode installed, do not trigger the CLT installation popup when building with zig cc.

2 files changed, 28 insertions(+), 7 deletions(-)

lib/std/zig/system/darwin.zig+25-6
...@@ -6,11 +6,30 @@ const Version = std.builtin.Version;...@@ -6,11 +6,30 @@ const Version = std.builtin.Version;
66
7pub const macos = @import("darwin/macos.zig");7pub const macos = @import("darwin/macos.zig");
88
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
14pub 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.
13pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK {32pub 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);
src/main.zig+3-1
...@@ -1858,7 +1858,9 @@ fn buildOutputType(...@@ -1858,7 +1858,9 @@ fn buildOutputType(
1858 }1858 }
18591859
1860 const has_sysroot = if (comptime builtin.target.isDarwin()) outer: {1860 const has_sysroot = if (comptime builtin.target.isDarwin()) outer: {
1861 if (try std.zig.system.darwin.getDarwinSDK(arena, target_info.target)) |sdk| {1861 if (std.zig.system.darwin.isDarwinSDKInstalled(arena)) {
1862 const sdk = std.zig.system.darwin.getDarwinSDK(arena, target_info.target) orelse
1863 break :outer false;
1862 native_darwin_sdk = sdk;1864 native_darwin_sdk = sdk;
1863 try clang_argv.ensureUnusedCapacity(2);1865 try clang_argv.ensureUnusedCapacity(2);
1864 clang_argv.appendAssumeCapacity("-isysroot");1866 clang_argv.appendAssumeCapacity("-isysroot");