[Flang] position of `-L$LLVM_DIR/lib`
The Flang driver puts -L$LLVM_DIR/lib -lflang_rt.runtime after OS library directory -L options when linking.
$ flang -### test.f90 |& tail -1 | tr ' ' '\n' | grep '^"-[Ll]'
"-L/home/foo/llvm/bin/../lib/aarch64-unknown-linux-gnu"
"-L/usr/lib/gcc/aarch64-linux-gnu/13"
"-L/lib/aarch64-linux-gnu"
"-L/usr/lib/aarch64-linux-gnu"
"-L/lib"
"-L/usr/lib"
"-L/home/foo/llvm/lib"
"-lflang_rt.runtime"
"-lm"
"-lgcc"
"-lgcc_s"
"-lc"
"-lgcc"
"-lgcc_s"
If you install Flang (and Flang-RT) manually on an environment where the OS-provided Flang package is already installed, this has a problem.
When user-installed Flang is invoked, libflang_rt.runtime in the user-installed directory should be linked. However, OS-provided libflang_rt.runtime in /lib (or /lib/TRIPLE or /lib64, depending on OS) is linked instead.
https://github.com/llvm/llvm-project/issues/100403 and https://github.com/open-mpi/ompi/issues/13116 suffer from this problem.
Should we prioritize -L$LLVM_DIR/lib over -L/lib?
@llvm/issue-subscribers-flang-driver
Author: KAWASHIMA Takahiro (kawashima-fj)
$ flang -### test.f90 |& tail -1 | tr ' ' '\n' | grep '^"-[Ll]'
"-L/home/foo/llvm/bin/../lib/aarch64-unknown-linux-gnu"
"-L/usr/lib/gcc/aarch64-linux-gnu/13"
"-L/lib/aarch64-linux-gnu"
"-L/usr/lib/aarch64-linux-gnu"
"-L/lib"
"-L/usr/lib"
"-L/home/foo/llvm/lib"
"-lflang_rt.runtime"
"-lm"
"-lgcc"
"-lgcc_s"
"-lc"
"-lgcc"
"-lgcc_s"
If you install Flang (and Flang-RT) manually on an environment where the OS-provided Flang package is already installed, this has a problem.
When user-installed Flang is invoked, libflang_rt.runtime in the user-installed directory should be linked. However, OS-provided libflang_rt.runtime in /lib (or /lib/TRIPLE or /lib64, depending on OS) is linked instead.
https://github.com/llvm/llvm-project/issues/100403 and https://github.com/open-mpi/ompi/issues/13116 suffer from this problem.
Should we prioritize -L$LLVM_DIR/lib over -L/lib?
I think that you're probably right and we should put the LLVM lib dir first, but I'm not a distribution expert so there may be a good reason for things to be this way. @mgorny usually has useful comments from the distribution side of things, so might have some insight.
I think forcing an additional lib path is a problem in itself. Ideally, we should pass path to libflang_rt.a directly, much like the way clang links compiler-rt.
As a side note, should libflang_rt.runtime be in (for example) $PREFIX/lib/aarch64-unknown-linux-gnu instead of $PREFIX/lib?
Let's not invent more locations, please. It should really be in clang resource directory, and flang-rt's CMake is already partway there.
This is not a new location I just made up.
$PREFIX/lib contains libraries used by the compiler (e.g. libLLVMFrontendAtomic.so) whereas $PREFIX/lib/aarch64-unknown-linux-gnu contains the runtime (e.g. libc++.so) that will be used by the generated binaries.
My comment applies to 20.1.0-rc3 and I did not check if this has changed in the main branch.
Yes, strictly speaking that is a location that Debian made up, and that is not FHS-compliant.
Like I said, I did not just make it up, thanks for incorrecting me.
I guess you got the point though.
@kawashima-fj this is fixed by 1264d7a53a4de3094672be2a248db57b213f33ac. Could you please retest and close?
Thanks. I've confirmed this is fixed now.
On my environment (AArch64, Ubuntu 24.04, Flang/LLVM b68565b8c70f):
$ flang -### test.f90 |& tail -1 | tr ' ' '\n' | grep '^"-[Ll]'
"-L/home/foo/llvm/bin/../lib/aarch64-unknown-linux-gnu"
"-L/home/foo/llvm/lib/clang/21/lib/aarch64-unknown-linux-gnu"
"-L/usr/lib/gcc/aarch64-linux-gnu/13"
"-L/lib/aarch64-linux-gnu"
"-L/usr/lib/aarch64-linux-gnu"
"-L/lib"
"-L/usr/lib"
"-L/home/foo/llvm/lib"
"-lflang_rt.runtime"
"-lm"
"-lgcc"
"-lgcc_s"
"-lc"
"-lgcc"
"-lgcc_s"
libflang_rt.runtime.a exists in /home/foo/llvm/lib/clang/21/lib/aarch64-unknown-linux-gnu and its -L is located before -L/lib.