authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-17 19:38:45+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-18 09:28:00+02:00
logdaaec68aec6e78999a0066621dfd382025505918
tree5562e29d344e528e01e82cf4acf65f4da5ee4478
parent1dac5f5214aa9e35ad0fbc2ba561a894bd193ae3

zld: apply @mikdusan's suggestions

Library search paths now should be closer to what ld64 does. Also, cwd now has to be specified explicitly to be considered.

1 files changed, 39 insertions(+), 11 deletions(-)

src/link/MachO.zig+39-11
......@@ -705,6 +705,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
705705 // By this time, we depend on these libs being dynamically linked libraries and not static libraries
706706 // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which
707707 // case we want to avoid prepending "-l".
708 // TODO I think then they should go as an input file instead of via shared_libs.
708709 if (Compilation.classifyFileExt(link_lib) == .shared_library) {
709710 try shared_libs.append(link_lib);
710711 continue;
......@@ -714,18 +715,45 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
714715 }
715716
716717 var search_lib_dirs = std.ArrayList([]const u8).init(arena);
717 try search_lib_dirs.ensureCapacity(self.base.options.lib_dirs.len * 2 + 1);
718718
719 search_lib_dirs.appendAssumeCapacity("."); // We will always start the search in cwd
719 for (self.base.options.lib_dirs) |path| {
720 if (fs.path.isAbsolute(path)) {
721 var candidates = std.ArrayList([]const u8).init(arena);
722 if (self.base.options.syslibroot) |syslibroot| {
723 const full_path = try fs.path.join(arena, &[_][]const u8{ syslibroot, path });
724 try candidates.append(full_path);
725 }
726 try candidates.append(path);
727
728 var found = false;
729 for (candidates.items) |candidate| {
730 // Verify that search path actually exists
731 var tmp = fs.cwd().openDir(candidate, .{}) catch |err| switch (err) {
732 error.FileNotFound => continue,
733 else => |e| return e,
734 };
735 defer tmp.close();
736
737 try search_lib_dirs.append(candidate);
738 found = true;
739 break;
740 }
720741
721 for (self.base.options.lib_dirs) |lib_dir| {
722 search_lib_dirs.appendAssumeCapacity(lib_dir);
723 }
742 if (!found) {
743 log.warn("directory not found for '-L{s}'", .{path});
744 }
745 } else {
746 // Verify that search path actually exists
747 var tmp = fs.cwd().openDir(path, .{}) catch |err| switch (err) {
748 error.FileNotFound => {
749 log.warn("directory not found for '-L{s}'", .{path});
750 continue;
751 },
752 else => |e| return e,
753 };
754 defer tmp.close();
724755
725 if (self.base.options.syslibroot) |syslibroot| {
726 for (self.base.options.lib_dirs) |lib_dir| {
727 const path = try fs.path.join(arena, &[_][]const u8{ syslibroot, lib_dir });
728 search_lib_dirs.appendAssumeCapacity(path);
756 try search_lib_dirs.append(path);
729757 }
730758 }
731759
......@@ -749,7 +777,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
749777
750778 if (!found) {
751779 log.warn("library '-l{s}' not found", .{l_name});
752 log.warn("searched paths:", .{});
780 log.warn("Library search paths:", .{});
753781 for (search_lib_dirs.items) |lib_dir| {
754782 log.warn(" {s}", .{lib_dir});
755783 }
......@@ -763,7 +791,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
763791 try rpath_table.putNoClobber(rpath, {});
764792 }
765793
766 var rpaths = std.ArrayList([]const u8) .init(arena);
794 var rpaths = std.ArrayList([]const u8).init(arena);
767795 try rpaths.ensureCapacity(rpath_table.count());
768796 for (rpath_table.items()) |entry| {
769797 rpaths.appendAssumeCapacity(entry.key);