Building libclang_rt.* results in error with -DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON

Building 32-bit libclang_rt..so libraries result in error when cmake is invoked with -DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON. The error is due to a link line for 32-bit libclang_rt..so using libclang_rt.builtins-x86_64.a instead of libclang_rt.builtins-i386.a. I am making an in-tree build of LLVM, clang, libc++ and compiler-rt (version 7.0.0). The compiler used for building is clang 5.0.2.

FAILED: lib64/clang/7.0.0/lib/linux/libclang_rt.ubsan_standalone-i386.so

….

/usr/bin/ld: i386:x86-64 architecture of input file `/usr/lib64/clang/5.0.2/lib/linux/libclang_rt.builtins-x86_64.a(udivdi3.c.o)’ is incompatible with i386 output

/usr/bin/ld: i386:x86-64 architecture of input file `/usr/lib64/clang/5.0.2/lib/linux/libclang_rt.builtins-x86_64.a(udivmoddi4.c.o)’ is incompatible with i386 output

/usr/bin/ld: i386:x86-64 architecture of input file `/usr/lib64/clang/5.0.2/lib/linux/libclang_rt.builtins-x86_64.a(umoddi3.c.o)’ is incompatible with i386 output

clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

I traced the problem to the following in projects/compiler-rt/cmake/config-ix.cmake (which was not there in version 5.0 of compiler-rt).

check_library_exists(c fopen “” COMPILER_RT_HAS_LIBC)

if (COMPILER_RT_USE_BUILTINS_LIBRARY)

include(HandleCompilerRT)

find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY)

else()

if (ANDROID)

check_library_exists(gcc __gcc_personality_v0 “” COMPILER_RT_HAS_GCC_LIB)

else()

check_library_exists(gcc_s __gcc_personality_v0 “” COMPILER_RT_HAS_GCC_S_LIB)

endif()

endif()

find_compiler_rt_library set COMPILER_RT_BUILTINS_LIBRARY to …/libclang_rt.builtins-x86_64.a regardless of the architecture. This library then gets added to the sanitizer libs link line. There is an attempt to specify the architecture in find_compiler_rt_library by passing –target=CMAKE_CXX_COMPILER_TARGET but I am not sure where that is supposed to be set. I cannot set it from my own cmake command line because I am building a 64-bit clang with both 64-bit and 32-bit target libraries. Building it this way worked without any issues in LLVM 5.0. But with 7.0, the intent seems to be use COMPILER_RT_USE_BUILTINS_LIBRARY but it does not set COMPILER_RT_BUILTINS_LIBRARY based on architecture of the output file (i.e. use libclang_rt.builtins-x86_64.a for creating libclang_rt.ubsan.standalone-x86_64.so and libclang_rt.builtins-i386.a for creating libclang_rt.ubsan.standalone-i386.so).

Any suggestions how to fix this cleanly? Or is there anything I should set besides or in lieu of COMPILER_RT_USE_BUILTINS_LIBRARY on cmake command line. Note that I want to make a self-contained clang/llvm build with no dependencies on libgcc. This was something I was able to do with 5.0.0 with SANITIZER_USE_COMPILER_RT but that seems to not work anymore.

This seems to be related to:

https://bugs.llvm.org/show_bug.cgi?id=38025

/Riyaz