Error: Build fail when linking OpenBLAS shared library with gfortran on macOS
Hi, I am building OpenBLAS on macOS with gfortran and have an issue on the last step, when trying to link the final libopenblas.dylib shared library.
I am using the following OpenBLAS specific CMake configuration, on an x64 architecture:
-DBUILD_WITHOUT_LAPACK=OFF -DBUILD_WITHOUT_CBLAS=OFF -DNOFORTRAN=OFF -DDYNAMIC_ARCH=ON -DDYNAMIC_OLDER=ON
It is compiling all the 20000+ files correctly, then, there is the last step where I get:
> collect2: fatal error: execvp: Argument list too long
On the official FAQ, it is stated that we should take care of using response files thanks to the undocumented CMake variables: https://github.com/xianyi/OpenBLAS/wiki/Faq#cmakeosx-build-fails-with-argument-list-too-long
By the way, when using Ninja, there is also the CMAKE_NINJA_FORCE_RESPONSE_FILE variable.
I have the same behavior either if I am asking explicitly the response file or letting the default behavior: A response file is generated and used.
However, it seems the issue is more when gfortran itself is calling collect2 to link through an exec() function.
I am seeing a similar issue reported 5 years ago by Brad King where he is explaining that it looks like a gfortran internal issue: https://gitlab.kitware.com/cmake/cmake/-/issues/16206#note_141137
Knowing these fact, I tried several things to have a successful build.
Firstly, regarding the Argument list too long itself, I can confirm the issue by checking if the ARG_MAX is reached
getconf ARG_MAX
> 1048576
wc -c CMakeFiles/openblas.rsp
> 1281606 CMakeFiles/openblas.rsp
Then, some tests to obtain the correct libopenblas.dylib.
Default CMake behavior:
gfortran <args...> @CMakeFiles/openblas.rsp
> collect2: fatal error: execvp: Argument list too long
Trying to forward directly to the linker as a response file
gfortran <args...> -Wl,@CMakeFiles/openblas.rsp
> gfortran: fatal error: no input files; unwilling to write output files
Trying to bypass the no input files error by using a dummy object
echo "" | gfortran -o dummy.o -c -x f95-cpp-input - # Awesome command to generate an empty dummy.o file
gfortran <args...> -Wl,@CMakeFiles/openblas.rsp dummy.o
> collect2: fatal error: execvp: Argument list too long
Generating a temporary archive with all objects and bypassing the no input files error
cat CMakeFiles/openblas.rsp | xargs -n1024 ar -ur libopenblas.a # Iteratively fill libopenblas.a with objects
echo "" | gfortran -o dummy.o -c -x f95-cpp-input -
gfortran <args...> -Wl,-force_load,libopenblas.a dummy.o
> SUCCESS
FYI I also tried to rebuild gfortran myself, but am here using the Homebrew version.
In the end, I am able to get a dynamic library of OpenBLAS through manual hacks but have several questions:
- Is it a known issue to have this kind of errors even when using response files?
- Do you see a simpler solution? Maybe I am missing something as it seems me an overly complicated process for such a standard thing
- I know it looks like more a gfortran (or CMake) oriented issue but it seems that, if confirmed, this issue has always been there. So, could it be something handled in
CMakeLists.txt? I will try on my side to see if it is possible to have a proper handling of this issue but maybe I could then do a PR if it feels it could be useful. As an example, here is a quite similar issue onlibflamewith a patch on the build process (not CMake): https://github.com/flame/libflame/issues/29
I suspect you may be aggravating the issue by doing a full DYNAMIC_ARCH&DYNAMIC_OLDER build rather than one for a shorter list or single cpu model, or maybe the additional files from LAPACK 3.9.1 have pushed it over another edge. (There is a CI job that is supposed to test a cmake build configuration with DYNAMIC_ARCH (but not DYNAMIC_OLDER) on OSX, but I noticed just now that it seems to be missing gfortran lately). The trivial workaround would be to compile with GNU make, but your hack does look quite elegant, so if it can be transformed into cmake syntax I am all for including it. BTW I never saw any followup to that cmake ticket on either the kitware issue tracker or the GCC mailing lists
you could build with list of only intel cpus, hope both hackintosh users will not complain... but well, this limit was there to hit any time.
Interestingly, I cannot reproduce this in Azure CI (macOS 10.15.7, xcode 12.4, cmake 3.21.3, gcc/gfortran 10.3)
Maybe long base path adds up?
Maybe long base path adds up?
Did not think of that, it certainly would explain why it seems to work for "most" people but not all ... but would the individual object file paths be absolute ? (I have not even tried using a response file in the CI job)
My mistake - the CI job was still building a static library...
I have just built OpenBLAS on a PowerPC Mac:
36-137:~ svacchanda$ port installed OpenBLAS The following ports are currently installed: OpenBLAS @0.3.18_0+gcc7+lapack+native (active)
right, this problem will only occur for DYNAMIC_ARCH builds on x86_64 where there are around ten times as many cpu-specific files to link as in a native build. (And a DYNAMIC_ARCH build is not relevant on PowerMac as that cpu list would be all big server types)
@barracuda156 i am afraid you are absolutely wrong in your statement. PowerPC is like G3 G4 processors, OSX tiger , if that rings the bell. What is you question actually?
@brada4 that comment was absolutely uncalled for. Please keep the discussion civil. (From reading the issue title alone one might assume that the build is broken on Mac in general... and it is actually useful to know that there are still people using OpenBLAS on PowerMac.)
@barracuda156 i am afraid you are absolutely wrong in your statement. PowerPC is like G3 G4 processors, OSX tiger , if that rings the bell. What is you question actually?
@brada4 I am sorry, I thought it might be useful for someone to know that it works on some Macs, as it is not clear from basic info.