Cross-compiling only compiler-rt libraries

Greetings everyone,

I am currently cross-compiling some of my programs to aarch64 with Clang. I added my own sanitizer and library to the compiler-rt and want to use it. However, since my machine is x86, the libraries are compiled for x86 use. I was able to cross-compile to get the aarch64 libraries. However, this also cross-compiles clang and now I can’t use it on my x86 machine. Is there a way to cross-compile only compiler-rt and not Clang? I can’t seem to find a way to do this.

In short, I want to have aarch64 compiler-rt libraries and an x86 clang, since I will be compiling my programs on an x86 machine. Hope I was able to explain this well enough. Please let me know if I wasn’t clear or if you have any questions. Thank you very much!

Best regards,
Mohannad Ismail


You can do that by running cmake directly against the compiler-rt directory instead of the llvm dir.

Hope this helps!

Hello Tobias,

Thank you for your reply. That is very helpful and I currently was able to get that done. However, I was looking for a much cleaner solution in which I can compile clang to be x86 and compiler-rt to be aarch64 both at the same time in one compilation. Do you have any idea if that is possible?

Best regards,
Mohannad Ismail

Hi Mohannad,

That is possible using a two-stage build [0]. Basically in a two-stage build, CMake builds clang once and then uses the just-built compiler to build the target-specific libraries. You can also set it up such that it uses the just-built compiler to build clang again in order to benefit from the latest optimizations. In llvm-project/clang/cmake/caches you can find a couple of CMake cache files that set this up. You should check out Fuchsia.cmake, Fuchsia-stage2.cmake and BaremetalARM.cmake. I found these to be the easiest ones to follow when setting up such a build.

If you only want to build clang once and then build compiler-rt, you shouldn’t need to look at Fuchsia.cmake. AFAIU it is just to build clang, which is then used by Fuchsia-stage2.cmake to build clang and the libraries.

Hope that helps!



[0] LLVM documentation on two-stage builds: