From ae881ed2498e069a0d4741f15c9e329c8b6bc07c Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 20 Jun 2023 11:27:51 +0200 Subject: [PATCH 1/3] macos: try auto-detecting the SDK when building on Apple for Apple --- src/main.zig | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main.zig b/src/main.zig index e7cd48a3dafd..56ca68e1bfe4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2548,9 +2548,18 @@ fn buildOutputType( want_native_include_dirs = true; } - if (sysroot == null and cross_target.isNativeOs() and - (system_libs.count() != 0 or want_native_include_dirs)) - { + const want_native_paths_detection = blk: { + if (sysroot != null) break :blk false; + // If we are building natively on macOS and targeting any Apple platform, + // try auto-detecting the native SDK. + // Note that if the user specified the sysroot explicitly, we will NOT auto-detect + // the SDK. + if ((comptime builtin.target.isDarwin()) and cross_target.isDarwin()) break :blk true; + break :blk cross_target.isNativeOs() and + (system_libs.count() != 0 or want_native_include_dirs); + }; + + if (want_native_paths_detection) { const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| { fatal("unable to detect native system paths: {s}", .{@errorName(err)}); }; From 772ca19ee616dfd2a5453ae8e4b86751252b33bd Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 20 Jun 2023 22:26:24 +0200 Subject: [PATCH 2/3] macos: remove obsolete check for native paths detection when linking frameworks This is already guaranteed as frameworks are Apple-only concept, and when targeting Apple from Apple we always try linking against native SDK anyhow. --- src/main.zig | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main.zig b/src/main.zig index 56ca68e1bfe4..2b2d1ca80285 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2542,12 +2542,6 @@ fn buildOutputType( } } - if (comptime builtin.target.isDarwin()) { - // If we want to link against frameworks, we need system headers. - if (framework_dirs.items.len > 0 or frameworks.count() > 0) - want_native_include_dirs = true; - } - const want_native_paths_detection = blk: { if (sysroot != null) break :blk false; // If we are building natively on macOS and targeting any Apple platform, From 740565d8f52e9237efa82a27c663053fea51a999 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 20 Jun 2023 23:53:20 +0200 Subject: [PATCH 3/3] macos: use native SDK when building build.zig if available --- src/main.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.zig b/src/main.zig index 2b2d1ca80285..6dfe23341263 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4456,6 +4456,20 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi } try main_pkg.add(gpa, "@build", &build_pkg); + // We always try detecting the native SDK when building build.zig on macOS. + var native_darwin_sdk: ?std.zig.system.darwin.DarwinSDK = null; + var lib_dirs = std.ArrayList([]const u8).init(gpa); + defer lib_dirs.deinit(); + if ((comptime builtin.target.isDarwin()) and cross_target.isDarwin()) blk: { + if (std.zig.system.darwin.isDarwinSDKInstalled(arena)) { + const paths = std.zig.system.NativePaths.detect(arena, target_info) catch break :blk; + native_darwin_sdk = std.zig.system.darwin.getDarwinSDK(arena, target_info.target); + for (paths.lib_dirs.items) |lib_dir| { + try lib_dirs.append(lib_dir); + } + } + } + const comp = Compilation.create(gpa, .{ .zig_lib_directory = zig_lib_directory, .local_cache_directory = local_cache_directory, @@ -4475,6 +4489,8 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi .cache_mode = .whole, .reference_trace = reference_trace, .debug_compile_errors = debug_compile_errors, + .native_darwin_sdk = native_darwin_sdk, + .lib_dirs = lib_dirs.items, }) catch |err| { fatal("unable to create compilation: {s}", .{@errorName(err)}); };