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;
66
77pub 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
928/// Detect SDK on Darwin.
1029/// Calls `xcrun --sdk <target_sdk> --show-sdk-path` which fetches the path to the SDK sysroot (if any).
1130/// Subsequently calls `xcrun --sdk <target_sdk> --show-sdk-version` which fetches version of the SDK.
1231/// The caller needs to deinit the resulting struct.
13pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK {
32pub fn getDarwinSDK(allocator: *Allocator, target: Target) ?DarwinSDK {
1433 const is_simulator_abi = target.abi == .simulator;
1534 const sdk = switch (target.os.tag) {
1635 .macos => "macosx",
......@@ -20,8 +39,8 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK {
2039 else => return null,
2140 };
2241 const path = path: {
23 const argv = &[_][]const u8{ "xcrun", "--sdk", sdk, "--show-sdk-path" };
24 const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv });
42 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;
2544 defer {
2645 allocator.free(result.stderr);
2746 allocator.free(result.stdout);
......@@ -31,12 +50,12 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK {
3150 // and in the worst case the user can specify the sysroot manually.
3251 return null;
3352 }
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;
3554 break :path path;
3655 };
3756 const version = version: {
38 const argv = &[_][]const u8{ "xcrun", "--sdk", sdk, "--show-sdk-version" };
39 const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv });
57 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;
4059 defer {
4160 allocator.free(result.stderr);
4261 allocator.free(result.stdout);
src/main.zig+3-1
......@@ -1858,7 +1858,9 @@ fn buildOutputType(
18581858 }
18591859
18601860 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;
18621864 native_darwin_sdk = sdk;
18631865 try clang_argv.ensureUnusedCapacity(2);
18641866 clang_argv.appendAssumeCapacity("-isysroot");