Commit e2b6dfa608

Jakub Konka <kubkon@jakubkonka.com>
2021-11-26 18:09:14
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.
1 parent a956958
Changed files (2)
lib
std
zig
system
src
lib/std/zig/system/darwin.zig
@@ -6,11 +6,30 @@ const Version = std.builtin.Version;
 
 pub const macos = @import("darwin/macos.zig");
 
+/// Check if SDK is installed on Darwin without triggering CLT installation popup window.
+/// 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`
+/// and checking if the status is nonzero or the returned string in nonempty.
+/// https://github.com/Homebrew/brew/blob/e119bdc571dcb000305411bc1e26678b132afb98/Library/Homebrew/brew.sh#L630
+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) {
+        // We don't actually care if there were errors as this is best-effort check anyhow.
+        return false;
+    }
+    return result.stdout.len > 0;
+}
+
 /// Detect SDK on Darwin.
 /// 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.
 /// The caller needs to deinit the resulting struct.
-pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK {
+pub fn getDarwinSDK(allocator: *Allocator, target: Target) ?DarwinSDK {
     const is_simulator_abi = target.abi == .simulator;
     const sdk = switch (target.os.tag) {
         .macos => "macosx",
@@ -20,8 +39,8 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK {
         else => return null,
     };
     const path = path: {
-        const argv = &[_][]const u8{ "xcrun", "--sdk", sdk, "--show-sdk-path" };
-        const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv });
+        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);
@@ -31,12 +50,12 @@ pub fn getDarwinSDK(allocator: *Allocator, target: Target) !?DarwinSDK {
             // and in the worst case the user can specify the sysroot manually.
             return null;
         }
-        const path = try allocator.dupe(u8, mem.trimRight(u8, result.stdout, "\r\n"));
+        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{ "xcrun", "--sdk", sdk, "--show-sdk-version" };
-        const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv });
+        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);
src/main.zig
@@ -1858,7 +1858,9 @@ fn buildOutputType(
         }
 
         const has_sysroot = if (comptime builtin.target.isDarwin()) outer: {
-            if (try std.zig.system.darwin.getDarwinSDK(arena, target_info.target)) |sdk| {
+            if (std.zig.system.darwin.isDarwinSDKInstalled(arena)) {
+                const sdk = std.zig.system.darwin.getDarwinSDK(arena, target_info.target) orelse
+                    break :outer false;
                 native_darwin_sdk = sdk;
                 try clang_argv.ensureUnusedCapacity(2);
                 clang_argv.appendAssumeCapacity("-isysroot");