Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2542,15 +2542,18 @@ 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,
// 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 (sysroot == null and 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)});
};
Expand Down Expand Up @@ -4453,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,
Expand All @@ -4472,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)});
};
Expand Down