flang-compiler / flang

Flang is a Fortran language front-end designed for integration with LLVM.
Other
791 stars 135 forks source link

`-fno-fortran-main` not working on macOS #1221

Open meow464 opened 2 years ago

meow464 commented 2 years ago

I came across this problem while compiling OpenBLAS on macOS. Calling flang with -fno-fortran-main does nothing, by using -v you can see that ld still receives -lflangmain. To trigger an error you can use -all_load.

This command line can be used to reproduce without building OpenBLAS:

flang -v -fno-fortran-main -all_load -dynamiclib -L/usr/local/lib /usr/local/lib/libpgmath.a -o libdummy_pgmath.dylib

The error:

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
 "/usr/bin/ld" -demangle -lto_library /usr/local/stow/flang/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -all_load -platform_version macos 10.17.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o libdummy_pgmath.dylib -L/usr/local/lib /usr/local/lib/libpgmath.a -lflangmain -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)

Any tips as to why or where -fno-fortran-main is being ignored are appreciated.

EDIT: Please let me know if this bug happens on other platforms. -all_load is a very apple flag. EDIT2: -lflangmain is still added to the linker command line but the error can't be triggered because -all_load isn't available.

kiranchandramohan commented 2 years ago

Thanks for reporting this issue. I think there is a bug in the code and currently, it requires the two flags (-fno-fortran-main and -Mnomain) to be present to not have main. Please test this. https://github.com/flang-compiler/classic-flang-llvm-project/blob/f645e319607d5d8c8eabfb0c7dcefa544f6bbcf9/clang/lib/Driver/ToolChains/CommonArgs.cpp#L124

The fix is to change the above code to something like the following,

&& !(Args.hasArg(options::OPT_Mnomain) ||
            Args.hasArg(options::OPT_no_fortran_main)));
meow464 commented 2 years ago

@kiranchandramohan I can build for macOS with those changes :+1: EDIT: I tested it on branch release_100