authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-26 12:44:49+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-26 12:44:49+01:00
log02d8ca71f98800df08754ce3d2d2e39541178f64
tree98f4fbc4b83e6b1c00b6f77fcce5aa339583c8fb
parenta2c546fea30de2caf1efb454d327e70270c07338

macos: always use Zig shipped libc headers when no native SDK

If Zig didn't detect native SDK, always use shipped libc headers when targeting macOS.

2 files changed, 49 insertions(+), 44 deletions(-)

src/Compilation.zig+46-44
......@@ -773,6 +773,8 @@ pub const InitOptions = struct {
773773 wasi_exec_model: ?std.builtin.WasiExecModel = null,
774774 /// (Zig compiler development) Enable dumping linker's state as JSON.
775775 enable_link_snapshots: bool = false,
776 /// (Darwin). Path to native macOS SDK if detected.
777 native_macos_sdk_path: ?[]const u8 = null,
776778};
777779
778780fn addPackageTableToCacheHash(
......@@ -962,18 +964,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
962964 break :blk false;
963965 };
964966
965 const darwin_use_system_sdk = blk: {
966 if (comptime !builtin.target.isDarwin()) break :blk false;
967 if (!options.is_native_os) break :blk false;
968 if (builtin.os.tag != .macos or !options.target.isDarwin()) break :blk false;
969 break :blk options.frameworks.len > 0 or options.framework_dirs.len > 0;
970 };
971
972967 const sysroot = blk: {
973968 if (options.sysroot) |sysroot| {
974969 break :blk sysroot;
975 } else if (darwin_use_system_sdk) {
976 break :blk try std.zig.system.darwin.getSDKPath(arena, options.target);
970 } else if (options.native_macos_sdk_path) |sdk_path| {
971 break :blk sdk_path;
977972 } else {
978973 break :blk null;
979974 }
......@@ -1060,6 +1055,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
10601055 link_libc,
10611056 options.system_lib_names.len != 0 or options.frameworks.len != 0,
10621057 options.libc_installation,
1058 options.native_macos_sdk_path != null,
10631059 );
10641060
10651061 const must_pie = target_util.requiresPIE(options.target);
......@@ -3776,6 +3772,37 @@ const LibCDirs = struct {
37763772 libc_installation: ?*const LibCInstallation,
37773773};
37783774
3775fn getZigShippedLibCIncludeDirsDarwin(arena: *Allocator, zig_lib_dir: []const u8, target: Target) !LibCDirs {
3776 const arch_name = @tagName(target.cpu.arch);
3777 const os_name = try std.fmt.allocPrint(arena, "{s}.{d}", .{
3778 @tagName(target.os.tag),
3779 target.os.version_range.semver.min.major,
3780 });
3781 const s = std.fs.path.sep_str;
3782 const list = try arena.alloc([]const u8, 3);
3783
3784 list[0] = try std.fmt.allocPrint(
3785 arena,
3786 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-gnu",
3787 .{ zig_lib_dir, arch_name, os_name },
3788 );
3789 list[1] = try std.fmt.allocPrint(
3790 arena,
3791 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any",
3792 .{ zig_lib_dir, os_name },
3793 );
3794 list[2] = try std.fmt.allocPrint(
3795 arena,
3796 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-macos-any",
3797 .{zig_lib_dir},
3798 );
3799
3800 return LibCDirs{
3801 .libc_include_dir_list = list,
3802 .libc_installation = null,
3803 };
3804}
3805
37793806fn detectLibCIncludeDirs(
37803807 arena: *Allocator,
37813808 zig_lib_dir: []const u8,
......@@ -3784,6 +3811,7 @@ fn detectLibCIncludeDirs(
37843811 link_libc: bool,
37853812 link_system_libs: bool,
37863813 libc_installation: ?*const LibCInstallation,
3814 has_macos_sdk: bool,
37873815) !LibCDirs {
37883816 if (!link_libc) {
37893817 return LibCDirs{
......@@ -3800,11 +3828,14 @@ fn detectLibCIncludeDirs(
38003828 // using the system libc installation.
38013829 if (link_system_libs and is_native_abi and !target.isMinGW()) {
38023830 if (target.isDarwin()) {
3803 // For Darwin/macOS, we are all set with getSDKPath found earlier.
3804 return LibCDirs{
3805 .libc_include_dir_list = &[0][]u8{},
3806 .libc_installation = null,
3807 };
3831 return if (has_macos_sdk)
3832 // For Darwin/macOS, we are all set with getSDKPath found earlier.
3833 LibCDirs{
3834 .libc_include_dir_list = &[0][]u8{},
3835 .libc_installation = null,
3836 }
3837 else
3838 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target);
38083839 }
38093840 const libc = try arena.create(LibCInstallation);
38103841 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });
......@@ -3815,36 +3846,7 @@ fn detectLibCIncludeDirs(
38153846 // default if possible.
38163847 if (target_util.canBuildLibC(target)) {
38173848 switch (target.os.tag) {
3818 .macos => {
3819 const arch_name = @tagName(target.cpu.arch);
3820 const os_name = try std.fmt.allocPrint(arena, "{s}.{d}", .{
3821 @tagName(target.os.tag),
3822 target.os.version_range.semver.min.major,
3823 });
3824 const s = std.fs.path.sep_str;
3825 const list = try arena.alloc([]const u8, 3);
3826
3827 list[0] = try std.fmt.allocPrint(
3828 arena,
3829 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-gnu",
3830 .{ zig_lib_dir, arch_name, os_name },
3831 );
3832 list[1] = try std.fmt.allocPrint(
3833 arena,
3834 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any",
3835 .{ zig_lib_dir, os_name },
3836 );
3837 list[2] = try std.fmt.allocPrint(
3838 arena,
3839 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-macos-any",
3840 .{zig_lib_dir},
3841 );
3842
3843 return LibCDirs{
3844 .libc_include_dir_list = list,
3845 .libc_installation = null,
3846 };
3847 },
3849 .macos => return getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target),
38483850 else => {
38493851 const generic_name = target_util.libCGenericName(target);
38503852 // Some architectures are handled by the same set of headers.
src/main.zig+3
......@@ -663,6 +663,7 @@ fn buildOutputType(
663663 var minor_subsystem_version: ?u32 = null;
664664 var wasi_exec_model: ?std.builtin.WasiExecModel = null;
665665 var enable_link_snapshots: bool = false;
666 var native_macos_sdk_path: ?[]const u8 = null;
666667
667668 var system_libs = std.StringArrayHashMap(Compilation.SystemLib).init(gpa);
668669 defer system_libs.deinit();
......@@ -1858,6 +1859,7 @@ fn buildOutputType(
18581859
18591860 const has_sysroot = if (comptime builtin.target.isDarwin()) outer: {
18601861 if (try std.zig.system.darwin.getSDKPath(arena, target_info.target)) |sdk_path| {
1862 native_macos_sdk_path = sdk_path;
18611863 try clang_argv.ensureUnusedCapacity(2);
18621864 clang_argv.appendAssumeCapacity("-isysroot");
18631865 clang_argv.appendAssumeCapacity(sdk_path);
......@@ -2340,6 +2342,7 @@ fn buildOutputType(
23402342 .wasi_exec_model = wasi_exec_model,
23412343 .debug_compile_errors = debug_compile_errors,
23422344 .enable_link_snapshots = enable_link_snapshots,
2345 .native_macos_sdk_path = native_macos_sdk_path,
23432346 }) catch |err| {
23442347 fatal("unable to create compilation: {s}", .{@errorName(err)});
23452348 };