loadArchive on macOS is confusingly being called on non-archives
When attempting to compile a certain Haskell module in my project, I'm getting an error from the runtime linker saying that clang++
is not an archive. It turns out the issue is related to #16063. In my case I see this in the debug output (+RTS -Dl
):
findSystemLibrary: dll_name = `libc++.dylib'
... --print-file-name 'libc++.dylib'
... --print-file-name 'liblibc++.dylib'
... --print-file-name 'c++.lib'
... --print-file-name 'libc++.lib'
... --print-file-name 'libc++.dll.a'
... --print-file-name 'c++.dll.a'
... --print-file-name 'libc++.a'
... --print-file-name 'c++.a'
... --print-file-name 'libc++'
... --print-file-name 'c++'
This is due to the project relying on lc++
, while on my system c++
is a symlink to... clang++
. So I believe it asks the compiler to find c++
, then tries to open it and reports the error about clang++
not being an archive.
This causes me to wonder, what is the logic behind searching for libraries with no "lib" prefix, and no suffix? This was added in 3d17f1f1, but it doesn't explain in the commit message why these "bare" names were added to the list.
I compared with 8.4.4, which does not have this problem, and indeed it's this new "no prefix, no suffix" clause that's biting me:
8.4.4:
... --print-file-name 'c++.dll.a'
... --print-file-name 'libc++.a'
... --print-file-name 'c++.a'
8.6.4:
... --print-file-name 'c++.dll.a'
... --print-file-name 'libc++.a'
... --print-file-name 'c++.a'
... --print-file-name 'libc++'
... --print-file-name 'c++'
The following patch, which returns to 8.4.4 behavior, resolves the issue for me:
--- a/compiler/ghci/Linker.hs
+++ b/compiler/ghci/Linker.hs
@@ -1487,7 +1487,6 @@ locateLib hsc_env is_hs lib_dirs gcc_dirs lib
dyn_obj_file = lib <.> "dyn_o"
arch_files = [ "lib" ++ lib ++ lib_tag <.> "a"
, lib <.> "a" -- native code has no lib_tag
- , "lib" ++ lib, lib
]
lib_tag = if is_hs && loading_profiled_hs_libs then "_p" else ""
Pinging @bgamari, who worked with me to track this down on the #haskell IRC channel.