authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-12-20 11:45:48+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-02 15:29:05+01:00
log91a35e1a922e865ca0e9beb43a2be2c5552fc494
treed12efbc864e83cff8ceaaccdd4181978078acc75
parenta9c75a2b48f202d5c55097877499942ed07cc2e8

Detect native iframework dirs on macOS

This commit adds default search paths for system frameworks on macOS while also adding `-isysroot` for OS versions at least BigSur. Since BigSur (11.0.1), neither headers nor libs exist in standard root locations (`/usr/include`, `/System/Library/Frameworks`). Instead, they are now exclusively part of the installed developer toolchain (either via XCode.app or CLT), and specifying `-isysroot` allows us to keep using universal search paths such as `/System/Library/Frameworks` while only changing the include flag from `-iframework` to `-iframeworkwithsysroot`.

3 files changed, 53 insertions(+), 9 deletions(-)

lib/std/zig/system.zig+27-2
...@@ -22,6 +22,7 @@ pub const getSDKPath = macos.getSDKPath;...@@ -22,6 +22,7 @@ pub const getSDKPath = macos.getSDKPath;
22pub const NativePaths = struct {22pub const NativePaths = struct {
23 include_dirs: ArrayList([:0]u8),23 include_dirs: ArrayList([:0]u8),
24 lib_dirs: ArrayList([:0]u8),24 lib_dirs: ArrayList([:0]u8),
25 framework_dirs: ArrayList([:0]u8),
25 rpaths: ArrayList([:0]u8),26 rpaths: ArrayList([:0]u8),
26 warnings: ArrayList([:0]u8),27 warnings: ArrayList([:0]u8),
2728
...@@ -29,6 +30,7 @@ pub const NativePaths = struct {...@@ -29,6 +30,7 @@ pub const NativePaths = struct {
29 var self: NativePaths = .{30 var self: NativePaths = .{
30 .include_dirs = ArrayList([:0]u8).init(allocator),31 .include_dirs = ArrayList([:0]u8).init(allocator),
31 .lib_dirs = ArrayList([:0]u8).init(allocator),32 .lib_dirs = ArrayList([:0]u8).init(allocator),
33 .framework_dirs = ArrayList([:0]u8).init(allocator),
32 .rpaths = ArrayList([:0]u8).init(allocator),34 .rpaths = ArrayList([:0]u8).init(allocator),
33 .warnings = ArrayList([:0]u8).init(allocator),35 .warnings = ArrayList([:0]u8).init(allocator),
34 };36 };
...@@ -88,6 +90,19 @@ pub const NativePaths = struct {...@@ -88,6 +90,19 @@ pub const NativePaths = struct {
88 return self;90 return self;
89 }91 }
9092
93 if (comptime Target.current.isDarwin()) {
94 try self.addIncludeDir("/usr/include");
95 try self.addIncludeDir("/usr/local/include");
96
97 try self.addLibDir("/usr/lib");
98 try self.addLibDir("/usr/local/lib");
99
100 try self.addFrameworkDir("/Library/Frameworks");
101 try self.addFrameworkDir("/System/Library/Frameworks");
102
103 return self;
104 }
105
91 if (!is_windows) {106 if (!is_windows) {
92 const triple = try Target.current.linuxTriple(allocator);107 const triple = try Target.current.linuxTriple(allocator);
93 const qual = Target.current.cpu.arch.ptrBitWidth();108 const qual = Target.current.cpu.arch.ptrBitWidth();
...@@ -122,6 +137,7 @@ pub const NativePaths = struct {...@@ -122,6 +137,7 @@ pub const NativePaths = struct {
122 pub fn deinit(self: *NativePaths) void {137 pub fn deinit(self: *NativePaths) void {
123 deinitArray(&self.include_dirs);138 deinitArray(&self.include_dirs);
124 deinitArray(&self.lib_dirs);139 deinitArray(&self.lib_dirs);
140 deinitArray(&self.framework_dirs);
125 deinitArray(&self.rpaths);141 deinitArray(&self.rpaths);
126 deinitArray(&self.warnings);142 deinitArray(&self.warnings);
127 self.* = undefined;143 self.* = undefined;
...@@ -158,6 +174,16 @@ pub const NativePaths = struct {...@@ -158,6 +174,16 @@ pub const NativePaths = struct {
158 return self.appendArray(&self.warnings, s);174 return self.appendArray(&self.warnings, s);
159 }175 }
160176
177 pub fn addFrameworkDir(self: *NativePaths, s: []const u8) !void {
178 return self.appendArray(&self.framework_dirs, s);
179 }
180
181 pub fn addFrameworkDirFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
182 const item = try std.fmt.allocPrint0(self.framework_dirs.allocator, fmt, args);
183 errdefer self.framework_dirs.allocator.free(item);
184 try self.framework_dirs.append(item);
185 }
186
161 pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {187 pub fn addWarningFmt(self: *NativePaths, comptime fmt: []const u8, args: anytype) !void {
162 const item = try std.fmt.allocPrint0(self.warnings.allocator, fmt, args);188 const item = try std.fmt.allocPrint0(self.warnings.allocator, fmt, args);
163 errdefer self.warnings.allocator.free(item);189 errdefer self.warnings.allocator.free(item);
...@@ -237,8 +263,7 @@ pub const NativeTargetInfo = struct {...@@ -237,8 +263,7 @@ pub const NativeTargetInfo = struct {
237 // `---` `` ``--> Sub-version (Starting from Windows 10 onwards)263 // `---` `` ``--> Sub-version (Starting from Windows 10 onwards)
238 // \ `--> Service pack (Always zero in the constants defined)264 // \ `--> Service pack (Always zero in the constants defined)
239 // `--> OS version (Major & minor)265 // `--> OS version (Major & minor)
240 const os_ver: u16 =266 const os_ver: u16 = @intCast(u16, version_info.dwMajorVersion & 0xff) << 8 |
241 @intCast(u16, version_info.dwMajorVersion & 0xff) << 8 |
242 @intCast(u16, version_info.dwMinorVersion & 0xff);267 @intCast(u16, version_info.dwMinorVersion & 0xff);
243 const sp_ver: u8 = 0;268 const sp_ver: u8 = 0;
244 const sub_ver: u8 = if (os_ver >= 0x0A00) subver: {269 const sub_ver: u8 = if (os_ver >= 0x0A00) subver: {
src/Compilation.zig+1-6
...@@ -2079,12 +2079,6 @@ pub fn addCCArgs(...@@ -2079,12 +2079,6 @@ pub fn addCCArgs(
2079 try argv.append("-ffunction-sections");2079 try argv.append("-ffunction-sections");
2080 }2080 }
20812081
2082 try argv.ensureCapacity(argv.items.len + comp.bin_file.options.framework_dirs.len * 2);
2083 for (comp.bin_file.options.framework_dirs) |framework_dir| {
2084 argv.appendAssumeCapacity("-iframework");
2085 argv.appendAssumeCapacity(framework_dir);
2086 }
2087
2088 if (comp.bin_file.options.link_libcpp) {2082 if (comp.bin_file.options.link_libcpp) {
2089 const libcxx_include_path = try std.fs.path.join(arena, &[_][]const u8{2083 const libcxx_include_path = try std.fs.path.join(arena, &[_][]const u8{
2090 comp.zig_lib_directory.path.?, "libcxx", "include",2084 comp.zig_lib_directory.path.?, "libcxx", "include",
...@@ -2894,6 +2888,7 @@ fn buildOutputFromZig(...@@ -2894,6 +2888,7 @@ fn buildOutputFromZig(
2894 .directory = null, // Put it in the cache directory.2888 .directory = null, // Put it in the cache directory.
2895 .basename = bin_basename,2889 .basename = bin_basename,
2896 };2890 };
2891
2897 const sub_compilation = try Compilation.create(comp.gpa, .{2892 const sub_compilation = try Compilation.create(comp.gpa, .{
2898 .global_cache_directory = comp.global_cache_directory,2893 .global_cache_directory = comp.global_cache_directory,
2899 .local_cache_directory = comp.global_cache_directory,2894 .local_cache_directory = comp.global_cache_directory,
src/main.zig+25-1
...@@ -1436,11 +1436,35 @@ fn buildOutputType(...@@ -1436,11 +1436,35 @@ fn buildOutputType(
1436 for (paths.warnings.items) |warning| {1436 for (paths.warnings.items) |warning| {
1437 warn("{}", .{warning});1437 warn("{}", .{warning});
1438 }1438 }
1439
1440 const has_sysroot = if (comptime std.Target.current.isDarwin()) outer: {
1441 const at_least_big_sur = target_info.target.os.getVersionRange().semver.min.major >= 11;
1442 if (at_least_big_sur) {
1443 const sdk_path = try std.zig.system.getSDKPath(arena);
1444 try clang_argv.ensureCapacity(clang_argv.items.len + 2);
1445 clang_argv.appendAssumeCapacity("-isysroot");
1446 clang_argv.appendAssumeCapacity(sdk_path);
1447 break :outer true;
1448 }
1449 break :outer false;
1450 } else false;
1451
1439 try clang_argv.ensureCapacity(clang_argv.items.len + paths.include_dirs.items.len * 2);1452 try clang_argv.ensureCapacity(clang_argv.items.len + paths.include_dirs.items.len * 2);
1453 const isystem_flag = if (has_sysroot) "-iwithsysroot" else "-isystem";
1440 for (paths.include_dirs.items) |include_dir| {1454 for (paths.include_dirs.items) |include_dir| {
1441 clang_argv.appendAssumeCapacity("-isystem");1455 clang_argv.appendAssumeCapacity(isystem_flag);
1442 clang_argv.appendAssumeCapacity(include_dir);1456 clang_argv.appendAssumeCapacity(include_dir);
1443 }1457 }
1458
1459 try clang_argv.ensureCapacity(clang_argv.items.len + paths.framework_dirs.items.len * 2);
1460 try framework_dirs.ensureCapacity(framework_dirs.items.len + paths.framework_dirs.items.len);
1461 const iframework_flag = if (has_sysroot) "-iframeworkwithsysroot" else "-iframework";
1462 for (paths.framework_dirs.items) |framework_dir| {
1463 clang_argv.appendAssumeCapacity(iframework_flag);
1464 clang_argv.appendAssumeCapacity(framework_dir);
1465 framework_dirs.appendAssumeCapacity(framework_dir);
1466 }
1467
1444 for (paths.lib_dirs.items) |lib_dir| {1468 for (paths.lib_dirs.items) |lib_dir| {
1445 try lib_dirs.append(lib_dir);1469 try lib_dirs.append(lib_dir);
1446 }1470 }