Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error building shared libraries for MacOS #3528

Closed
meow464 opened this issue Feb 8, 2022 · 17 comments · Fixed by #3532
Closed

Error building shared libraries for MacOS #3528

meow464 opened this issue Feb 8, 2022 · 17 comments · Fixed by #3532

Comments

@meow464
Copy link
Contributor

meow464 commented Feb 8, 2022

Is shared lib even supported on macOS?

MacOS Monterey (12)
LLVM-10, clang and flang from the classic flang project
CPU: Ryzen 5 (ZEN)

Build script:

#!/bin/bash

RANLIB="/usr/local/bin/llvm-ranlib"

TARGET_SDKROOT="$(xcrun --show-sdk-path)"
CC="/usr/local/bin/clang"
FC="/usr/local/bin/flang"

COMMON_FLAGS="-isysroot $TARGET_SDKROOT"
FFLAGS=$COMMON_FLAGS" -L/usr/local/lib"
LDFLAGS=" -L/usr/local/lib"

HOST_SDKROOT="$(xcrun --show-sdk-path)"
HOSTCC="/usr/local/bin/clang -isysroot $HOST_SDKROOT"

make TARGET=ZEN BINARY=64 HOSTCC="$HOSTCC" CC="$CC" CFLAGS="$COMMON_FLAGS" FC="$FC" FFLAGS="$FFLAGS" RANLIB="$RANLIB" VERBOSE=1 USE_OPENMP=0 NO_LAPACK=0 NOFORTRAN=0 PREFIX=/Users/tuco/Projects/OpenBLAS/macOS/install libs netlib re_lapack shared install

The "interesting" part of the error:

/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -O2 -Mrecursive -Kieee -Wall -fPIC -msse3 -msse4.1 -mavx -mavx2 -mavx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm  -lflang
clang-10: warning: argument unused during compilation: '-Mrecursive' [-Wunused-command-line-argument]
clang-10: warning: argument unused during compilation: '-Kieee' [-Wunused-command-line-argument]
ld: warning: dylib (/usr/local/lib/libflang.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libflangrti.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libpgmath.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libompstub.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: object file (/usr/local/lib/libflangmain.a(flangmain.c.o)) was built for newer macOS version (10.17) than being linked (10.8)
Undefined symbols for architecture x86_64:
  "_MAIN_", referenced from:
      _main in libflangmain.a(flangmain.c.o)
ld: symbol(s) not found for architecture x86_64
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [libopenblas_zenp-r0.3.19.dev.dylib] Error 1
make: *** [shared] Error 2

The full enormous log (VERBOSE=1): https://gist.github.com/meow464/3d55454c05d581a042f35616172b8bb1
(There are a lot of warnings about target overriding, this is a flang problem, the output is for the correct target)

@isuruf
Copy link
Contributor

isuruf commented Feb 8, 2022

Is shared lib even supported on macOS?

Yes, but you are using an extremely new and experimental fortran compiler which is not supported yet.

Probably f_check file needs to drop flangmain as it's only used for executable.

@meow464
Copy link
Contributor Author

meow464 commented Feb 8, 2022

I checked the f_check file but it seems to only add -lflang.

I then tried passing -v to flang and ld
perl ./gensymbol osx x86_64 _ 0 0  0 0 0 0 "" "" 1 0 1 1 1 1 > osx.def
/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -v -O2 -Mrecursive -Kieee -Wall -fPIC -msse3 -msse4.1 -mavx -mavx2 -mavx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm  -lflang
clang version 10.0.1 (https://github.com/flang-compiler/classic-flang-llvm-project.git 4a566ddfa4157ed1a24920ab7673ccc01c46fd99)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /usr/local/bin
clang-10: warning: argument unused during compilation: '-Mrecursive' [-Wunused-command-line-argument]
clang-10: warning: argument unused during compilation: '-Kieee' [-Wunused-command-line-argument]
 "/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name /Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib -all_load -headerpad_max_install_names -platform_version macos 10.8.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ../libopenblas_zenp-r0.3.19.dev.dylib -L/usr/local/lib ../libopenblas_zenp-r0.3.19.dev.a -lflangmain -exported_symbols_list osx.def -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm -lflang -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem
ld: warning: dylib (/usr/local/lib/libflang.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libflangrti.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libpgmath.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libompstub.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: object file (/usr/local/lib/libflangmain.a(flangmain.c.o)) was built for newer macOS version (10.17) than being linked (10.8)
Undefined symbols for architecture x86_64:
  "_MAIN_", referenced from:
      _main in libflangmain.a(flangmain.c.o)
ld: symbol(s) not found for architecture x86_64
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [libopenblas_zenp-r0.3.19.dev.dylib] Error 1
make: *** [shared] Error 2

And indeed flang has the very default looking -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm with an additional -lflang appended at the end, which I assume comes from f_check.

Note that those flags on the ld command can be split into 3 groups:

  1. -lflangmain -exported_symbols_list osx.def
  2. -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm -lflang
  3. -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem

As for what they are:

  1. I don't know what this is;
  2. I assume this is inherited from the flang command line;
  3. What I think would be valid ld parameters.

Passing -no-flang-libs -no-flang-libs to flang doesn't yield much better results:

The new error message
/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -v -no-flang-libs -no-flang-libs -O2 -Mrecursive -Kieee -Wall -fPIC -m
sse3 -msse4.1 -mavx -mavx2 -mavx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.
dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm  -lflang            
clang version 10.0.1 (https://github.com/flang-compiler/classic-flang-llvm-project.git 4a566ddfa4157ed1a24920ab7673ccc01c46fd99)                                         
Target: x86_64-apple-darwin21.3.0                                                                                                                                                                                    
Thread model: posix                                                                                                                                                                                                  
InstalledDir: /usr/local/bin                                                                                                                                                                                         
clang-10: warning: argument unused during compilation: '-Mrecursive' [-Wunused-command-line-argument]                                                                                                                
clang-10: warning: argument unused during compilation: '-Kieee' [-Wunused-command-line-argument]                                                                                                                     
 "/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name /Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib -all_load -
headerpad_max_install_names -platform_version macos 10.8.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ../libopenblas_zenp-r0.3.19.dev.dylib -
L/usr/local/lib ../libopenblas_zenp-r0.3.19.dev.a -exported_symbols_list osx.def -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm -lflang -lSystem                
ld: warning: dylib (/usr/local/lib/libflang.dylib) was built for newer macOS version (10.17) than being linked (10.8)                                                                             
ld: warning: dylib (/usr/local/lib/libflangrti.dylib) was built for newer macOS version (10.17) than being linked (10.8)                                               
ld: warning: dylib (/usr/local/lib/libompstub.dylib) was built for newer macOS version (10.17) than being linked (10.8)                                                 
ld: warning: dylib (/usr/local/lib/libpgmath.dylib) was built for newer macOS version (10.17) than being linked (10.8)                                                        
ld: warning: object file (/usr/local/lib/libflangmain.a(flangmain.c.o)) was built for newer macOS version (10.17) than being linked (10.8)                                                
Undefined symbols for architecture x86_64:                                                                                                                                                                           
  "_MAIN_", referenced from:                                                                                                                                                                                         
      _main in libflangmain.a(flangmain.c.o)                                                                                                                                                                         
ld: symbol(s) not found for architecture x86_64                                                                                                                                                                      
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)                                                                                                                                   
make[1]: *** [libopenblas_zenp-r0.3.19.dev.dylib] Error 1                                                                                                                                                            
make: *** [shared] Error 2                                                                                                                                                                                           

ld now has a small improvement: -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm -lflang -lSystem

Except for the double -lSystem, the double -lflang and the single -lflangmain it looks almost right.

And indeed if I remove -lflangmain and call the linker command from the "export" directory the .dylib is generated just fine:

"/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name /Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib -all_load -headerpad_max_install_names -platform_version macos 10.8.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ../libopenblas_zenp-r0.3.19.dev.dylib -L/usr/local/lib ../libopenblas_zenp-r0.3.19.dev.a -exported_symbols_list osx.def -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm -lflang -lSystem

The problems seem to be that flang retains the default linking options instead of using just -lflang. It's not clear to me why this is happening but I'm very open to ideas.

This message has a lot of markdown. I did my best but please let me know if it's hard to read.

@martin-frbg
Copy link
Collaborator

Looks like either a missing compiler option or a compiler bug if flang tries to link shared libraries the same as executables.
Strange as flang-classic used to work (at least on other platforms). The f_check script has a section near the end where it removes some libraries from the default list the compiler would use, probably flangmain would need to be included there (and possibly added back for linking the test executables...)

@meow464
Copy link
Contributor Author

meow464 commented Feb 8, 2022

The f_check script has a section near the end where it removes some libraries from the default list the compiler would use, probably flangmain would need to be included there (and possibly added back for linking the test executables...)

Given the problems below it seems like this would help but perl is not one of my strengths and I can't find what you are referring to.


These commands are ran from the export folder as before.

The problem persists if I call flang directly with only -lflang, it auto inserts all the other flags including -lflangmain in the linker command:

/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -v -O2 -Mrecursive -Kieee -Wall -fPIC -msse3 -msse4.1 -mavx -mavx2 -mavx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -lflang
clang version 10.0.1 (https://github.com/flang-compiler/classic-flang-llvm-project.git 4a566ddfa4157ed1a24920ab7673ccc01c46fd99)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /usr/local/bin
clang-10: warning: argument unused during compilation: '-Mrecursive' [-Wunused-command-line-argument]
clang-10: warning: argument unused during compilation: '-Kieee' [-Wunused-command-line-argument]
 "/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name /Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib -all_load -headerpad_max_install_names -platform_version macos 10.17.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ../libopenblas_zenp-r0.3.19.dev.dylib -L/usr/local/lib ../libopenblas_zenp-r0.3.19.dev.a -lflangmain -exported_symbols_list osx.def -lflang -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem
Undefined symbols for architecture x86_64:
  "_MAIN_", referenced from:
      _main in libflangmain.a(flangmain.c.o)
ld: symbol(s) not found for architecture x86_64
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

But if I use -no-flang-libs -lflang -lflangrti -lpgmath -lompstub -lm -lpthread it works:

/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -v -O2 -Mrecursive -Kieee -Wall -fPIC -msse3 -msse4.1 -mavx -mavx2 -mavx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -no-flang-libs         
clang version 10.0.1 (https://github.com/flang-compiler/classic-flang-llvm-project.git 4a566ddfa4157ed1a24920ab7673ccc01c46fd99)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /usr/local/bin
clang-10: warning: argument unused during compilation: '-Mrecursive' [-Wunused-command-line-argument]
clang-10: warning: argument unused during compilation: '-Kieee' [-Wunused-command-line-argument]
 "/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name /Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib -all_load -headerpad_max_install_names -platform_version macos 10.17.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ../libopenblas_zenp-r0.3.19.dev.dylib -L/usr/local/lib ../libopenblas_zenp-r0.3.19.dev.a -exported_symbols_list osx.def -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem

But if I add those flags to f_check#L395 the problem persists:

/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -v -O2 -Mrecursive -Kieee -Wall -fPIC -msse3 -msse4.1 -mavx -mavx2 -mavx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm  -no-flang-libs -lflang -lflangrti -lpgmath -lompstub -lm -lpthread
clang version 10.0.1 (https://github.com/flang-compiler/classic-flang-llvm-project.git 4a566ddfa4157ed1a24920ab7673ccc01c46fd99)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /usr/local/bin
clang-10: warning: argument unused during compilation: '-Mrecursive' [-Wunused-command-line-argument]
clang-10: warning: argument unused during compilation: '-Kieee' [-Wunused-command-line-argument]
 "/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name /Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib -all_load -headerpad_max_install_names -platform_version macos 10.8.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ../libopenblas_zenp-r0.3.19.dev.dylib -L/usr/local/lib ../libopenblas_zenp-r0.3.19.dev.a -exported_symbols_list osx.def -lflangmain -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lm -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem
ld: warning: dylib (/usr/local/lib/libflang.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libflangrti.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libpgmath.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: dylib (/usr/local/lib/libompstub.dylib) was built for newer macOS version (10.17) than being linked (10.8)
ld: warning: object file (/usr/local/lib/libflangmain.a(flangmain.c.o)) was built for newer macOS version (10.17) than being linked (10.8)
Undefined symbols for architecture x86_64:
  "_MAIN_", referenced from:
      _main in libflangmain.a(flangmain.c.o)
ld: symbol(s) not found for architecture x86_64
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [libopenblas_zenp-r0.3.19.dev.dylib] Error 1
make: *** [shared] Error 2

@martin-frbg
Copy link
Collaborator

I'll try to provide a patch in a few hours when I have time. Btw did you build flang-classic yourself or is there a package in homebrew or whatever that I could plug into the CI job to test ?

@meow464
Copy link
Contributor Author

meow464 commented Feb 8, 2022

I built it myself. MacOS is a recent addition to flang and I don't think there is a homebrew package yet. The build instructions are simple enough, just replace gcc and g++ with clang and clang++. I recommend adding -DCMAKE_BUILD_TYPE=Release to setup.sh to speed things up.

I uploaded my own build here (the tar file, not the zip). To use it simply extract to /usr/local/stow and run cd /usr/local/stow && stow flang. You need to have stow installed of course.

@meow464
Copy link
Contributor Author

meow464 commented Feb 8, 2022

It seems -fno-fortran-main is not working.... I see no reason why this command line shouldn't work.

/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -v -O2 -Mrecursive -Kieee -Wall -fPIC -msse3 -msse4.1 -mavx -mavx2 -mavx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -lflang -fno-fortran-main
clang version 10.0.1 (https://github.com/flang-compiler/classic-flang-llvm-project.git 4a566ddfa4157ed1a24920ab7673ccc01c46fd99)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /usr/local/bin
clang-10: warning: argument unused during compilation: '-Mrecursive' [-Wunused-command-line-argument]
clang-10: warning: argument unused during compilation: '-Kieee' [-Wunused-command-line-argument]
 "/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name /Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib -all_load -headerpad_max_install_names -platform_version macos 10.17.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ../libopenblas_zenp-r0.3.19.dev.dylib -L/usr/local/lib ../libopenblas_zenp-r0.3.19.dev.a -lflangmain -exported_symbols_list osx.def -lflang -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem
Undefined symbols for architecture x86_64:
  "_MAIN_", referenced from:
      _main in libflangmain.a(flangmain.c.o)
ld: symbol(s) not found for architecture x86_64
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

@meow464
Copy link
Contributor Author

meow464 commented Feb 8, 2022

Just to make clear what works, this command ran from the command line in the exports folder succeeds:

/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -v -fno-fortran-main -O2 -Mrecursive -Kieee -Wall -fPIC -msse3 -msse4.1 -mavx -mavx2 -mavx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -no-flang-libs -lflang -lflangrti -lpgmath -lompstub -lm -lpthread

Applying this diff to f_check works. But ideally -fno-fortran-main would work.

@martin-frbg
Copy link
Collaborator

My hunch for f_check was simply:

--- a/f_check
+++ b/f_check
@@ -363,6 +363,7 @@ if ($link ne "") {
            && ($flags !~ /gfortranbegin/)
            && ($flags !~ /frtbegin/)
            && ($flags !~ /pathfstart/)
+           && ($flags !~ /flangmain/)
            && ($flags !~ /crt[0-9]/)
            && ($flags !~ /gcc/)
            && ($flags !~ /user32/)

but this is untested

@meow464
Copy link
Contributor Author

meow464 commented Feb 9, 2022

Still didn't work. It did remove -lflangmain from the flang command line which is definitely part of the solution, but LD still uses -lflangmain. Removing -all_load does the trick but that will probably break other things.

/usr/local/bin/flang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -L/usr/local/lib -v -O2 -Mrecursive -Kieee -Wall -fPIC -msse3 -msse4.1 -mavx -mavx2 -ma
vx2  -all_load -headerpad_max_install_names -install_name "/Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib" -dynamiclib -o ../libopenblas_zenp-r0.3.19.dev.dylib ../libopenblas_zenp-r0.3.19.dev.a -Wl,-exported_symbols_list,osx.def  -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem  -lflang                                                                                                  clang version 10.0.1 (https://github.com/flang-compiler/classic-flang-llvm-project.git 4a566ddfa4157ed1a24920ab7673ccc01c46fd99)                                                                                     
Target: x86_64-apple-darwin21.3.0                                                                                                                                                                                    
Thread model: posix                                                                                                                                                                                                  
InstalledDir: /usr/local/bin                                                                                                                                                                                         
clang-10: warning: argument unused during compilation: '-Mrecursive' [-Wunused-command-line-argument]                                                                                                                
clang-10: warning: argument unused during compilation: '-Kieee' [-Wunused-command-line-argument]                                                                                                                     
 "/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name /Users/tuco/Projects/OpenBLAS/macOS/OpenBLAS/exports/../libopenblas.0.dylib -all_load -
headerpad_max_install_names -platform_version macos 10.8.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o ../libopenblas_zenp-r0.3.19.dev.dylib -L/usr/local/lib ../libopenblas_zenp-r0.3.19.dev.a -lflangmain -exported_symbols_list osx.def -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem -lflang -lflang -lflangrti -lpgmath -lompstub -lm -lpthread -lSystem                                                                                                                                                                                                            ld: warning: dylib (/usr/local/lib/libflang.dylib) was built for newer macOS version (10.17) than being linked (10.8)                                                                                                
ld: warning: dylib (/usr/local/lib/libflangrti.dylib) was built for newer macOS version (10.17) than being linked (10.8)                                                                                             
ld: warning: dylib (/usr/local/lib/libpgmath.dylib) was built for newer macOS version (10.17) than being linked (10.8)                                                                                               
ld: warning: dylib (/usr/local/lib/libompstub.dylib) was built for newer macOS version (10.17) than being linked (10.8)                                                                                              
ld: warning: object file (/usr/local/lib/libflangmain.a(flangmain.c.o)) was built for newer macOS version (10.17) than being linked (10.8)                                                                         
Undefined symbols for architecture x86_64:                                                                                                                                                                           
  "_MAIN_", referenced from:                                                                                                                                                                                               _main in libflangmain.a(flangmain.c.o)                                                                                                                                                                         ld: symbol(s) not found for architecture x86_64                                                                                                                                                                      
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)                                                                                                                                   
make[1]: *** [libopenblas_zenp-r0.3.19.dev.dylib] Error 1                                                                                                                                                            
make: *** [shared] Error 2 

It seems -fno-fortran-main is being ignored. See: flang-compiler/flang#1221

@meow464
Copy link
Contributor Author

meow464 commented Feb 9, 2022

-force_load ../libopenblas_zenp-r0.3.19.dev.a instead of -all_load seems to work, but it could also break subtly. I don't know enough about OpenBLAS to judge.

@martin-frbg
Copy link
Collaborator

I've copied the suggestion to use both -fno-fortran-main -Mnomain from the flang issue to exports/Makefile.. we'll see how it goes

@meow464
Copy link
Contributor Author

meow464 commented Feb 9, 2022

I added it to f_check#L396 and yes it works. I also have the flangmain excluding code you posted.

I will now test the flang patch posted on the issue.

EDIT: The patch works.

@meow464
Copy link
Contributor Author

meow464 commented Feb 10, 2022

For reference this is the f_check diff

diff --git a/f_check b/f_check
index 4825fb09..46715010 100644
--- a/f_check
+++ b/f_check
@@ -363,6 +363,7 @@ if ($link ne "") {
            && ($flags !~ /gfortranbegin/)
            && ($flags !~ /frtbegin/)
            && ($flags !~ /pathfstart/)
+           && ($flags !~ /flangmain/)
            && ($flags !~ /crt[0-9]/)
            && ($flags !~ /gcc/)
            && ($flags !~ /user32/)
@@ -392,7 +393,7 @@ if ($link ne "") {
 }
 
 if ($vendor eq "FLANG"){
-    $linker_a .= "-lflang"
+    $linker_a .= "-fno-fortran-main -lflang"
 }
 
 open(MAKEFILE, ">> $makefile") || die "Can't append $makefile";

@martin-frbg
Copy link
Collaborator

Thanks. Putting it all in f_check does not work for me - during the build of openblas_utest the option gets passed to clang, which errors out on unknown arguments. So I guess the "no-fortran-main" bit has to stay in exports/Makefile (with an added ifdef that applies it only to flang)

@meow464
Copy link
Contributor Author

meow464 commented Feb 10, 2022

The pull request above works for me.

@martin-frbg
Copy link
Collaborator

Thank you for confirming, and for all your help with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants