authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-08-03 10:02:41+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-03 09:52:15-07:00
logf96d773d989bdca0a7eb08965a84ae2f63ea7f4c
tree6bc948ae95d474672eb6aac26fd6d531f7f12f62
parent72fb58f1078fa2409d4b9275467a99c6533eeb04

macho: append syslibroot to search dirs when resolving libSystem


1 files changed, 40 insertions(+), 45 deletions(-)

src/link/MachO.zig+40-45
...@@ -829,55 +829,50 @@ pub fn resolveLibSystem(...@@ -829,55 +829,50 @@ pub fn resolveLibSystem(
829 out_libs: anytype,829 out_libs: anytype,
830) !void {830) !void {
831 // If we were given the sysroot, try to look there first for libSystem.B.{dylib, tbd}.831 // If we were given the sysroot, try to look there first for libSystem.B.{dylib, tbd}.
832 var libsystem_available = false;832 if (syslibroot) |root| {
833 if (syslibroot != null) blk: {833 const full_dir_path = try std.fs.path.join(arena, &.{ root, "usr", "lib" });
834 // Try stub file first. If we hit it, then we're done as the stub file834 if (try resolveLibSystemInDirs(arena, &.{full_dir_path}, out_libs)) return;
835 // re-exports every single symbol definition.835 }
836 for (search_dirs) |dir| {836
837 if (try resolveLib(arena, dir, "System", ".tbd")) |full_path| {837 // Next, try input search dirs if we are linking on a custom host such as Nix.
838 try out_libs.put(full_path, .{838 if (try resolveLibSystemInDirs(arena, search_dirs, out_libs)) return;
839 .needed = true,839
840 .weak = false,840 // As a fallback, try linking against Zig shipped stub.
841 .path = full_path,841 const libsystem_name = try std.fmt.allocPrint(arena, "libSystem.{d}.tbd", .{
842 });842 target.os.version_range.semver.min.major,
843 libsystem_available = true;843 });
844 break :blk;844 const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
845 }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
854fn 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;
846 }861 }
847 // If we didn't hit the stub file, try .dylib next. However, libSystem.dylib862 }
848 // doesn't export libc.dylib which we'll need to resolve subsequently also.863 // If we didn't hit the stub file, try .dylib next. However, libSystem.dylib
849 for (search_dirs) |dir| {864 // doesn't export libc.dylib which we'll need to resolve subsequently also.
850 if (try resolveLib(arena, dir, "System", ".dylib")) |libsystem_path| {865 for (dirs) |dir| {
851 if (try resolveLib(arena, dir, "c", ".dylib")) |libc_path| {866 if (try resolveLib(arena, dir, "System", ".dylib")) |libsystem_path| {
852 try out_libs.put(libsystem_path, .{867 if (try resolveLib(arena, dir, "c", ".dylib")) |libc_path| {
853 .needed = true,868 try out_libs.put(libsystem_path, .{ .needed = true, .weak = false, .path = libsystem_path });
854 .weak = false,869 try out_libs.put(libc_path, .{ .needed = true, .weak = false, .path = libc_path });
855 .path = libsystem_path,870 return true;
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 }
865 }871 }
866 }872 }
867 }873 }
868 if (!libsystem_available) {874
869 const libsystem_name = try std.fmt.allocPrint(arena, "libSystem.{d}.tbd", .{875 return false;
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 }
881}876}
882877
883pub fn resolveSearchDir(878pub fn resolveSearchDir(