authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-02 13:41:32-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-11-02 13:41:32-05:00
log00ceb592ef8d396ec354152c1684321a6c5847e4
treec12a7a6cb5421aaa19753d2b2223ef4d057db60f
parentbe26c3bf4e4c6a7654ea68e6606c1167ef969a09
parent6ecefd590360be0484d766d18a23e19c6c7325e6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6932 from kubkon/fix-6318

macOS: fix linking issues on BigSur

5 files changed, 42 insertions(+), 0 deletions(-)

lib/std/zig/system.zig+2
......@@ -17,6 +17,8 @@ const macos = @import("system/macos.zig");
1717
1818const is_windows = Target.current.os.tag == .windows;
1919
20pub const getSDKPath = macos.getSDKPath;
21
2022pub const NativePaths = struct {
2123 include_dirs: ArrayList([:0]u8),
2224 lib_dirs: ArrayList([:0]u8),
lib/std/zig/system/macos.zig+24
......@@ -4,6 +4,8 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66const std = @import("std");
7const assert = std.debug.assert;
8const mem = std.mem;
79
810pub fn version_from_build(build: []const u8) !std.builtin.Version {
911 // build format:
......@@ -452,3 +454,25 @@ test "version_from_build" {
452454 std.testing.expect(std.mem.eql(u8, sver, pair[1]));
453455 }
454456}
457
458/// Detect SDK path on Darwin.
459/// Calls `xcrun --show-sdk-path` which result can be used to specify
460/// `-syslibroot` param of the linker.
461/// The caller needs to free the resulting path slice.
462pub fn getSDKPath(allocator: *mem.Allocator) ![]u8 {
463 assert(std.Target.current.isDarwin());
464 const argv = &[_][]const u8{ "xcrun", "--show-sdk-path" };
465 const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv });
466 defer {
467 allocator.free(result.stderr);
468 allocator.free(result.stdout);
469 }
470 if (result.stderr.len != 0) {
471 std.log.err("unexpected 'xcrun --show-sdk-path' stderr: {}", .{result.stderr});
472 }
473 if (result.term.Exited != 0) {
474 return error.ProcessTerminated;
475 }
476 const syslibroot = mem.trimRight(u8, result.stdout, "\r\n");
477 return mem.dupe(allocator, u8, syslibroot);
478}
src/Compilation.zig+9
......@@ -472,6 +472,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
472472 break :blk false;
473473 };
474474
475 const syslibroot = if (build_options.have_llvm and comptime std.Target.current.isDarwin()) outer: {
476 const path = if (use_lld and options.is_native_os and options.target.isDarwin()) inner: {
477 const syslibroot_path = try std.zig.system.getSDKPath(arena);
478 break :inner syslibroot_path;
479 } else null;
480 break :outer path;
481 } else null;
482
475483 const link_libc = options.link_libc or target_util.osRequiresLibC(options.target);
476484
477485 const must_dynamic_link = dl: {
......@@ -773,6 +781,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
773781 .frameworks = options.frameworks,
774782 .framework_dirs = options.framework_dirs,
775783 .system_libs = system_libs,
784 .syslibroot = syslibroot,
776785 .lib_dirs = options.lib_dirs,
777786 .rpath_list = options.rpath_list,
778787 .strip = strip,
src/link.zig+2
......@@ -88,6 +88,8 @@ pub const Options = struct {
8888 llvm_cpu_features: ?[*:0]const u8,
8989 /// Extra args passed directly to LLD. Ignored when not linking with LLD.
9090 extra_lld_args: []const []const u8,
91 /// Darwin-only. Set the root path to the system libraries and frameworks.
92 syslibroot: ?[]const u8,
9193
9294 objects: []const []const u8,
9395 framework_dirs: []const []const u8,
src/link/MachO.zig+5
......@@ -628,6 +628,11 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
628628 }
629629 }
630630
631 if (self.base.options.syslibroot) |dir| {
632 try argv.append("-syslibroot");
633 try argv.append(dir);
634 }
635
631636 for (self.base.options.lib_dirs) |lib_dir| {
632637 try argv.append("-L");
633638 try argv.append(lib_dir);