compiler-rt - Cannot cross complie libclang_rt.builtins-x32.a runtime for x32.
When building on x86_64 and attempting to cross complile compiler-rt for use by musl, as this needs this for x32, the following error is returned in the cmake configure:
cmake ../compiler-rt \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_C_FLAGS="-mx32" \
-DCMAKE_CXX_FLAGS="-mx32" \
-DCOMPILER_RT_BUILD_BUILTINS=ON \
-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
-DCMAKE_C_COMPILER_TARGET=x86_64-linux-muslx32 \
-DCMAKE_CXX_COMPILER_TARGET=x86_64-linux-muslx32 \
-DCMAKE_ASM_COMPILER_TARGET=x86_64-linux-muslx32 \
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
CMake Warning:
Ignoring extra path from command line:
" "
-- The C compiler identification is Clang 22.1.2
-- The CXX compiler identification is Clang 22.1.2
-- The ASM compiler identification is Clang with GNU-like command-line
-- Found assembler: /usr/lib/llvm/22/bin/clang
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/lib/llvm/22/bin/clang
-- Check for working C compiler: /usr/lib/llvm/22/bin/clang - broken
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message):
The C compiler
"/usr/lib/llvm/22/bin/clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: '/mnt/vmtmp/llvm/build/CMakeFiles/CMakeScratch/TryCompile-ytQoUb'
Run Build Command(s): /usr/sbin/cmake -E env VERBOSE=1 /usr/sbin/gmake -f Makefile cmTC_6b8d5/fast
/usr/sbin/gmake -f CMakeFiles/cmTC_6b8d5.dir/build.make CMakeFiles/cmTC_6b8d5.dir/build
gmake[1]: Entering directory '/mnt/vmtmp/llvm/build/CMakeFiles/CMakeScratch/TryCompile-ytQoUb'
Building C object CMakeFiles/cmTC_6b8d5.dir/testCCompiler.c.o
/usr/lib/llvm/22/bin/clang --target=x86_64-linux-muslx32 -mx32 -MD -MT CMakeFiles/cmTC_6b8d5.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_6b8d5.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_6b8d5.dir/testCCompiler.c.o -c /mnt/vmtmp/llvm/build/CMakeFiles/CMakeScratch/TryCompile-ytQoUb/testCCompiler.c
Linking C executable cmTC_6b8d5
/usr/sbin/cmake -E cmake_link_script CMakeFiles/cmTC_6b8d5.dir/link.txt --verbose=1
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find Scrt1.o: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find crti.o: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find crtbeginS.o: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find -lgcc: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find -lgcc_s: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find -lc: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find -lgcc: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find -lgcc_s: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find crtendS.o: No such file or directory
/usr/x86_64-pc-linux-gnu/binutils-bin/2.46.0/ld: cannot find crtn.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Also, the same thing happens when trying to use ld.lld, ie -DLLVM_ENABLE_LLD=ON. This is the same problem that you get when trying to build musl without the runtime ( libclang_rt.builtins-x32.a).
There is a work around but that involves building the compiler runtime code that is by musl within musl. But, this is horribly messy and unclean. Should there be a dependancy on the runtime on the C Library? I thought that these circualr dependancies didn't exist in LLVM?
FWIW, you can see an x32 build using llvm in this gist. https://gist.github.com/daunabomba/bf1e732cbcd71545750b2cf1362bb8bd
Also, doing a musl install-headers and passing a sysroot would not help in this case as it is still trying to build executables.
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY may help?
The only thing that came close was using was using -DCOMPILER_RT_BAREMETAL_BUILD=ON and the CFLAGS-ffreestanding -U__linux__ .
I think that this is a bug having the freestanding baremetal code building assert.h even if it is being built for linux.
Probably the BAREMETA_BUILD should not compile the assert.c and should check for -ffreestanding.
Also the runtime file is called x86_64 even if it is built as x32.