Linker issue using flang --target=xxx
I am trying to build a Fortran test on x86, using --target=aarch64-linux-gnu:
$ cat test.f90
program test
end program
$ flang-new --target=aarch64-linux-gnu --sysroot=<aarch64-sysroot> test.f90 -c
$ flang-new --target=aarch64-linux-gnu --sysroot=<aarch64-sysroot> -Wl,-dynamic-linker=<aarch64-sysroot>/lib/ld-linux-aarch64.so.1 test.o -flang-experimental-exec -fuse-ld=lld
ld.lld: error: llvm-install/lib/libFortran_main.a(Fortran_main.c.o) is incompatible with elf64-littleaarch64
ld.lld: error: llvm-install/lib/libFortranRuntime.a(ISO_Fortran_binding.cpp.o) is incompatible with elf64-littleaarch64
ld.lld: error: llvm-install/lib/libFortranRuntime.a(allocatable.cpp.o) is incompatible with elf64-littleaarch64
ld.lld: error: llvm-install/lib/libFortranRuntime.a(assign.cpp.o) is incompatible with elf64-littleaarch64
[...]
It tries to link AArch64 objects (test.o, <sysroot>/lib/..) and x86 objects (libFortran_main, libFortranRuntime, libFortranDecimal)
Is it a current limitation, or did I miss something?
Thanks
Flang is a project under active development. It is not yet ready for general use. Cross-compiling is currently not supported.
@llvm/issue-subscribers-flang-driver
In case it is helpful, some time ago a few patches landed (please see https://reviews.llvm.org/D130352 and https://reviews.llvm.org/D130351) that made it possible to build the runtime and decimal libraries standalone and we use this to cross-compile the runtime/decimal libraries for aarch64.
We build "core" clang/flang/etc. as it sounds like you do (to run on x86_64 and be able to target aarch64) then separately run cmake again in the runtime and decimal folders (and specifying whatever aarch64 cross-compiling flags through cmake as normal). Then, just make sure to link against the resulting cross-compiled libs when running flang. We've been using this approach to build for aarch64 since the aforementioned patches landed and haven't run into major issues yet that I'm aware of.
As a warning though, I don't think this approach is tested at all and has broken a few times. ~~I think this is actually broken at the moment (I last tested at 5dd7c16c3dcfd3154a53ee59e0e092c1e0092197) so you may see errors along the lines of "native" "IN_LIST" "link_components" "AND" "IN_LIST" "LLVM_TARGETS_TO_BUILD". I think this is stemming from f1d3bc49634cfefe35de3aa631cef9f808b12896 as I don't think we're handling LLVM_TARGET_ARCH/LLVM_NATIVE_ARCH as expected, but I haven't dealt with this yet.~~ Edit: I think this was fixed by 2e3153059c268700d4b399a8cbba28e9c2514e09.
Per this comment, this has been fixed. With the current main, this works as expected.