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(
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
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;
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
883878pub fn resolveSearchDir(