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 {...@@ -705,6 +705,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
705 // By this time, we depend on these libs being dynamically linked libraries and not static libraries705 // By this time, we depend on these libs being dynamically linked libraries and not static libraries
706 // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which706 // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which
707 // case we want to avoid prepending "-l".707 // case we want to avoid prepending "-l".
708 // TODO I think then they should go as an input file instead of via shared_libs.
708 if (Compilation.classifyFileExt(link_lib) == .shared_library) {709 if (Compilation.classifyFileExt(link_lib) == .shared_library) {
709 try shared_libs.append(link_lib);710 try shared_libs.append(link_lib);
710 continue;711 continue;
...@@ -714,18 +715,45 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {...@@ -714,18 +715,45 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
714 }715 }
715716
716 var search_lib_dirs = std.ArrayList([]const u8).init(arena);717 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 cwd719 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| {742 if (!found) {
722 search_lib_dirs.appendAssumeCapacity(lib_dir);743 log.warn("directory not found for '-L{s}'", .{path});
723 }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| {756 try search_lib_dirs.append(path);
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);
729 }757 }
730 }758 }
731759
...@@ -749,7 +777,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {...@@ -749,7 +777,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
749777
750 if (!found) {778 if (!found) {
751 log.warn("library '-l{s}' not found", .{l_name});779 log.warn("library '-l{s}' not found", .{l_name});
752 log.warn("searched paths:", .{});780 log.warn("Library search paths:", .{});
753 for (search_lib_dirs.items) |lib_dir| {781 for (search_lib_dirs.items) |lib_dir| {
754 log.warn(" {s}", .{lib_dir});782 log.warn(" {s}", .{lib_dir});
755 }783 }
...@@ -763,7 +791,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {...@@ -763,7 +791,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
763 try rpath_table.putNoClobber(rpath, {});791 try rpath_table.putNoClobber(rpath, {});
764 }792 }
765793
766 var rpaths = std.ArrayList([]const u8) .init(arena);794 var rpaths = std.ArrayList([]const u8).init(arena);
767 try rpaths.ensureCapacity(rpath_table.count());795 try rpaths.ensureCapacity(rpath_table.count());
768 for (rpath_table.items()) |entry| {796 for (rpath_table.items()) |entry| {
769 rpaths.appendAssumeCapacity(entry.key);797 rpaths.appendAssumeCapacity(entry.key);