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 {...@@ -773,6 +773,8 @@ pub const InitOptions = struct {
773 wasi_exec_model: ?std.builtin.WasiExecModel = null,773 wasi_exec_model: ?std.builtin.WasiExecModel = null,
774 /// (Zig compiler development) Enable dumping linker's state as JSON.774 /// (Zig compiler development) Enable dumping linker's state as JSON.
775 enable_link_snapshots: bool = false,775 enable_link_snapshots: bool = false,
776 /// (Darwin). Path to native macOS SDK if detected.
777 native_macos_sdk_path: ?[]const u8 = null,
776};778};
777779
778fn addPackageTableToCacheHash(780fn addPackageTableToCacheHash(
...@@ -962,18 +964,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -962,18 +964,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
962 break :blk false;964 break :blk false;
963 };965 };
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
972 const sysroot = blk: {967 const sysroot = blk: {
973 if (options.sysroot) |sysroot| {968 if (options.sysroot) |sysroot| {
974 break :blk sysroot;969 break :blk sysroot;
975 } else if (darwin_use_system_sdk) {970 } else if (options.native_macos_sdk_path) |sdk_path| {
976 break :blk try std.zig.system.darwin.getSDKPath(arena, options.target);971 break :blk sdk_path;
977 } else {972 } else {
978 break :blk null;973 break :blk null;
979 }974 }
...@@ -1060,6 +1055,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1060,6 +1055,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1060 link_libc,1055 link_libc,
1061 options.system_lib_names.len != 0 or options.frameworks.len != 0,1056 options.system_lib_names.len != 0 or options.frameworks.len != 0,
1062 options.libc_installation,1057 options.libc_installation,
1058 options.native_macos_sdk_path != null,
1063 );1059 );
10641060
1065 const must_pie = target_util.requiresPIE(options.target);1061 const must_pie = target_util.requiresPIE(options.target);
...@@ -3776,6 +3772,37 @@ const LibCDirs = struct {...@@ -3776,6 +3772,37 @@ const LibCDirs = struct {
3776 libc_installation: ?*const LibCInstallation,3772 libc_installation: ?*const LibCInstallation,
3777};3773};
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
3779fn detectLibCIncludeDirs(3806fn detectLibCIncludeDirs(
3780 arena: *Allocator,3807 arena: *Allocator,
3781 zig_lib_dir: []const u8,3808 zig_lib_dir: []const u8,
...@@ -3784,6 +3811,7 @@ fn detectLibCIncludeDirs(...@@ -3784,6 +3811,7 @@ fn detectLibCIncludeDirs(
3784 link_libc: bool,3811 link_libc: bool,
3785 link_system_libs: bool,3812 link_system_libs: bool,
3786 libc_installation: ?*const LibCInstallation,3813 libc_installation: ?*const LibCInstallation,
3814 has_macos_sdk: bool,
3787) !LibCDirs {3815) !LibCDirs {
3788 if (!link_libc) {3816 if (!link_libc) {
3789 return LibCDirs{3817 return LibCDirs{
...@@ -3800,11 +3828,14 @@ fn detectLibCIncludeDirs(...@@ -3800,11 +3828,14 @@ fn detectLibCIncludeDirs(
3800 // using the system libc installation.3828 // using the system libc installation.
3801 if (link_system_libs and is_native_abi and !target.isMinGW()) {3829 if (link_system_libs and is_native_abi and !target.isMinGW()) {
3802 if (target.isDarwin()) {3830 if (target.isDarwin()) {
3803 // For Darwin/macOS, we are all set with getSDKPath found earlier.3831 return if (has_macos_sdk)
3804 return LibCDirs{3832 // For Darwin/macOS, we are all set with getSDKPath found earlier.
3805 .libc_include_dir_list = &[0][]u8{},3833 LibCDirs{
3806 .libc_installation = null,3834 .libc_include_dir_list = &[0][]u8{},
3807 };3835 .libc_installation = null,
3836 }
3837 else
3838 getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target);
3808 }3839 }
3809 const libc = try arena.create(LibCInstallation);3840 const libc = try arena.create(LibCInstallation);
3810 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });3841 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });
...@@ -3815,36 +3846,7 @@ fn detectLibCIncludeDirs(...@@ -3815,36 +3846,7 @@ fn detectLibCIncludeDirs(
3815 // default if possible.3846 // default if possible.
3816 if (target_util.canBuildLibC(target)) {3847 if (target_util.canBuildLibC(target)) {
3817 switch (target.os.tag) {3848 switch (target.os.tag) {
3818 .macos => {3849 .macos => return getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target),
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 },
3848 else => {3850 else => {
3849 const generic_name = target_util.libCGenericName(target);3851 const generic_name = target_util.libCGenericName(target);
3850 // Some architectures are handled by the same set of headers.3852 // Some architectures are handled by the same set of headers.
src/main.zig+3
...@@ -663,6 +663,7 @@ fn buildOutputType(...@@ -663,6 +663,7 @@ fn buildOutputType(
663 var minor_subsystem_version: ?u32 = null;663 var minor_subsystem_version: ?u32 = null;
664 var wasi_exec_model: ?std.builtin.WasiExecModel = null;664 var wasi_exec_model: ?std.builtin.WasiExecModel = null;
665 var enable_link_snapshots: bool = false;665 var enable_link_snapshots: bool = false;
666 var native_macos_sdk_path: ?[]const u8 = null;
666667
667 var system_libs = std.StringArrayHashMap(Compilation.SystemLib).init(gpa);668 var system_libs = std.StringArrayHashMap(Compilation.SystemLib).init(gpa);
668 defer system_libs.deinit();669 defer system_libs.deinit();
...@@ -1858,6 +1859,7 @@ fn buildOutputType(...@@ -1858,6 +1859,7 @@ fn buildOutputType(
18581859
1859 const has_sysroot = if (comptime builtin.target.isDarwin()) outer: {1860 const has_sysroot = if (comptime builtin.target.isDarwin()) outer: {
1860 if (try std.zig.system.darwin.getSDKPath(arena, target_info.target)) |sdk_path| {1861 if (try std.zig.system.darwin.getSDKPath(arena, target_info.target)) |sdk_path| {
1862 native_macos_sdk_path = sdk_path;
1861 try clang_argv.ensureUnusedCapacity(2);1863 try clang_argv.ensureUnusedCapacity(2);
1862 clang_argv.appendAssumeCapacity("-isysroot");1864 clang_argv.appendAssumeCapacity("-isysroot");
1863 clang_argv.appendAssumeCapacity(sdk_path);1865 clang_argv.appendAssumeCapacity(sdk_path);
...@@ -2340,6 +2342,7 @@ fn buildOutputType(...@@ -2340,6 +2342,7 @@ fn buildOutputType(
2340 .wasi_exec_model = wasi_exec_model,2342 .wasi_exec_model = wasi_exec_model,
2341 .debug_compile_errors = debug_compile_errors,2343 .debug_compile_errors = debug_compile_errors,
2342 .enable_link_snapshots = enable_link_snapshots,2344 .enable_link_snapshots = enable_link_snapshots,
2345 .native_macos_sdk_path = native_macos_sdk_path,
2343 }) catch |err| {2346 }) catch |err| {
2344 fatal("unable to create compilation: {s}", .{@errorName(err)});2347 fatal("unable to create compilation: {s}", .{@errorName(err)});
2345 };2348 };