Skip to content

Commit f96d773

Browse files
kubkonandrewrk
authored andcommitted
macho: append syslibroot to search dirs when resolving libSystem
1 parent 72fb58f commit f96d773

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

src/link/MachO.zig

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -829,55 +829,50 @@ pub fn resolveLibSystem(
829829
out_libs: anytype,
830830
) !void {
831831
// If we were given the sysroot, try to look there first for libSystem.B.{dylib, tbd}.
832-
var libsystem_available = false;
833-
if (syslibroot != null) blk: {
834-
// Try stub file first. If we hit it, then we're done as the stub file
835-
// re-exports every single symbol definition.
836-
for (search_dirs) |dir| {
837-
if (try resolveLib(arena, dir, "System", ".tbd")) |full_path| {
838-
try out_libs.put(full_path, .{
839-
.needed = true,
840-
.weak = false,
841-
.path = full_path,
842-
});
843-
libsystem_available = true;
844-
break :blk;
845-
}
832+
if (syslibroot) |root| {
833+
const full_dir_path = try std.fs.path.join(arena, &.{ root, "usr", "lib" });
834+
if (try resolveLibSystemInDirs(arena, &.{full_dir_path}, out_libs)) return;
835+
}
836+
837+
// Next, try input search dirs if we are linking on a custom host such as Nix.
838+
if (try resolveLibSystemInDirs(arena, search_dirs, out_libs)) return;
839+
840+
// As a fallback, try linking against Zig shipped stub.
841+
const libsystem_name = try std.fmt.allocPrint(arena, "libSystem.{d}.tbd", .{
842+
target.os.version_range.semver.min.major,
843+
});
844+
const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
845+
"libc", "darwin", libsystem_name,
846+
});
847+
try out_libs.put(full_path, .{
848+
.needed = true,
849+
.weak = false,
850+
.path = full_path,
851+
});
852+
}
853+
854+
fn resolveLibSystemInDirs(arena: Allocator, dirs: []const []const u8, out_libs: anytype) !bool {
855+
// Try stub file first. If we hit it, then we're done as the stub file
856+
// re-exports every single symbol definition.
857+
for (dirs) |dir| {
858+
if (try resolveLib(arena, dir, "System", ".tbd")) |full_path| {
859+
try out_libs.put(full_path, .{ .needed = true, .weak = false, .path = full_path });
860+
return true;
846861
}
847-
// If we didn't hit the stub file, try .dylib next. However, libSystem.dylib
848-
// doesn't export libc.dylib which we'll need to resolve subsequently also.
849-
for (search_dirs) |dir| {
850-
if (try resolveLib(arena, dir, "System", ".dylib")) |libsystem_path| {
851-
if (try resolveLib(arena, dir, "c", ".dylib")) |libc_path| {
852-
try out_libs.put(libsystem_path, .{
853-
.needed = true,
854-
.weak = false,
855-
.path = libsystem_path,
856-
});
857-
try out_libs.put(libc_path, .{
858-
.needed = true,
859-
.weak = false,
860-
.path = libc_path,
861-
});
862-
libsystem_available = true;
863-
break :blk;
864-
}
862+
}
863+
// If we didn't hit the stub file, try .dylib next. However, libSystem.dylib
864+
// doesn't export libc.dylib which we'll need to resolve subsequently also.
865+
for (dirs) |dir| {
866+
if (try resolveLib(arena, dir, "System", ".dylib")) |libsystem_path| {
867+
if (try resolveLib(arena, dir, "c", ".dylib")) |libc_path| {
868+
try out_libs.put(libsystem_path, .{ .needed = true, .weak = false, .path = libsystem_path });
869+
try out_libs.put(libc_path, .{ .needed = true, .weak = false, .path = libc_path });
870+
return true;
865871
}
866872
}
867873
}
868-
if (!libsystem_available) {
869-
const libsystem_name = try std.fmt.allocPrint(arena, "libSystem.{d}.tbd", .{
870-
target.os.version_range.semver.min.major,
871-
});
872-
const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
873-
"libc", "darwin", libsystem_name,
874-
});
875-
try out_libs.put(full_path, .{
876-
.needed = true,
877-
.weak = false,
878-
.path = full_path,
879-
});
880-
}
874+
875+
return false;
881876
}
882877

883878
pub fn resolveSearchDir(

0 commit comments

Comments
 (0)