From 82a706934452560e5848527207c6cb476e1f1444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Sat, 11 Dec 2021 10:40:57 +0200 Subject: [PATCH 1/2] macho: fail if requested -framework is not found If `-framework` is requested, but not found, the linker will err instead of creating a strange executable. https://github.com/ziglang/zig/issues/10299#issuecomment-990404953 Refs #9542 Refs #10299 Refs #10158 --- src/link/MachO.zig | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index a67d3a4452..840c03701f 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -751,7 +751,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { } } - var framework_not_found = false; for (self.base.options.frameworks) |framework| { for (&[_][]const u8{ ".tbd", ".dylib", "" }) |ext| { if (try resolveFramework(arena, framework_dirs.items, framework, ext)) |full_path| { @@ -760,14 +759,13 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { } } else { log.warn("framework not found for '-framework {s}'", .{framework}); - framework_not_found = true; - } - } - - if (framework_not_found) { - log.warn("Framework search paths:", .{}); - for (framework_dirs.items) |dir| { - log.warn(" {s}", .{dir}); + log.warn("Framework search paths:", .{}); + for (framework_dirs.items) |dir| { + log.warn(" {s}", .{dir}); + } else { + log.warn(" . Consider specifying --sysroot", .{}); + } + return error.FrameworkNotFound; } } From 4ce62087502c2b1eca7d5721f3face5bc4fe538b Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sun, 12 Dec 2021 13:49:24 +0100 Subject: [PATCH 2/2] macho: if lib or framework not found, wait until syms resolved MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This way, we will inform the user that there are unresolved symbols in addition to missing library/framework as requested on the linker line. If all symbols were resolved on the other hand, we still flag up that the library/framework cannot be found. Example behaviour: ``` $ zig cc hello.c -framework MyFoundation --verbose warning(link): framework not found for '-framework MyFoundation' warning(link): Framework search paths: warning(link): /Library/Frameworks warning(link): /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks thread 1079397 panic: attempt to unwrap error: FrameworkNotFound ...stack trace... ``` and ``` ❯ zig cc hello.c -lWAT --verbose warning(link): library not found for '-lWAT' warning(link): Library search paths: warning(link): /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib warning(link): /usr/local/lib thread 1079824 panic: attempt to unwrap error: LibraryNotFound ...stack trace... ``` --- src/link/MachO.zig | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/link/MachO.zig b/src/link/MachO.zig index a7a2a0e030..43d6733132 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -572,6 +572,9 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { try self.populateMissingMetadata(); } + var lib_not_found = false; + var framework_not_found = false; + if (needs_full_relink) { for (self.objects.items) |*object| { object.free(self.base.allocator, self); @@ -688,7 +691,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { } var libs = std.ArrayList([]const u8).init(arena); - var lib_not_found = false; for (search_lib_names.items) |lib_name| { // Assume ld64 default: -search_paths_first // Look in each directory for a dylib (stub first), and then for archive @@ -760,13 +762,14 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { } } else { log.warn("framework not found for '-framework {s}'", .{framework}); - log.warn("Framework search paths:", .{}); - for (framework_dirs.items) |dir| { - log.warn(" {s}", .{dir}); - } else { - log.warn(" . Consider specifying --sysroot", .{}); - } - return error.FrameworkNotFound; + framework_not_found = true; + } + } + + if (framework_not_found) { + log.warn("Framework search paths:", .{}); + for (framework_dirs.items) |dir| { + log.warn(" {s}", .{dir}); } } @@ -926,6 +929,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { if (self.unresolved.count() > 0) { return error.UndefinedSymbolReference; } + if (lib_not_found) { + return error.LibraryNotFound; + } + if (framework_not_found) { + return error.FrameworkNotFound; + } try self.createTentativeDefAtoms(); try self.parseObjectsIntoAtoms();