How To Cross-Compile Clang/LLVM using Clang/LLVM

Introduction

This document contains information about building LLVM andClang on host machine, targeting another platform.

For more information on how to use Clang as a cross-compiler,please check http://clang.llvm.org/docs/CrossCompilation.html.

TODO: Add MIPS and other platforms to this document.

Cross-Compiling from x86_64 to ARM

In this use case, we’ll be using CMake and Ninja, on a Debian-based Linuxsystem, cross-compiling from an x86_64 host (most Intel and AMD chipsnowadays) to a hard-float ARM target (most ARM targets nowadays).

The packages you’ll need are:

  • cmake
  • ninja-build (from backports in Ubuntu)
  • gcc-4.7-arm-linux-gnueabihf
  • gcc-4.7-multilib-arm-linux-gnueabihf
  • binutils-arm-linux-gnueabihf
  • libgcc1-armhf-cross
  • libsfgcc1-armhf-cross
  • libstdc++6-armhf-cross
  • libstdc++6-4.7-dev-armhf-cross

Configuring CMake

For more information on how to configure CMake for LLVM/Clang,see Building LLVM with CMake.

The CMake options you need to add are:

  • -DCMAKE_CROSSCOMPILING=True
  • -DCMAKE_INSTALL_PREFIX=<install-dir>
  • -DLLVM_TABLEGEN=<path-to-host-bin>/llvm-tblgen
  • -DCLANG_TABLEGEN=<path-to-host-bin>/clang-tblgen
  • -DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf
  • -DLLVM_TARGET_ARCH=ARM
  • -DLLVM_TARGETS_TO_BUILD=ARM

If you’re compiling with GCC, you can use architecture options for your target,and the compiler driver will detect everything that it needs:

  • -DCMAKE_CXX_FLAGS='-march=armv7-a -mcpu=cortex-a9 -mfloat-abi=hard'

However, if you’re using Clang, the driver might not be up-to-date with yourspecific Linux distribution, version or GCC layout, so you’ll need to fudge.

In addition to the ones above, you’ll also need:

  • '-target arm-linux-gnueabihf' or whatever is the triple of your cross GCC.
  • '—sysroot=/usr/arm-linux-gnueabihf', '—sysroot=/opt/gcc/arm-linux-gnueabihf'or whatever is the location of your GCC’s sysroot (where /lib, /bin etc are).
  • Appropriate use of -I and -L, depending on how the cross GCC is installed,and where are the libraries and headers.

The TableGen options are required to compile it with the host compiler,so you’ll need to compile LLVM (or at least llvm-tblgen) to your hostplatform before you start. The CXX flags define the target, cpu (which in this casedefaults to fpu=VFP3 with NEON), and forcing the hard-float ABI. If you’reusing Clang as a cross-compiler, you will also have to set —sysrootto make sure it picks the correct linker.

When using Clang, it’s important that you choose the triple to be _identical_to the GCC triple and the sysroot. This will make it easier for Clang tofind the correct tools and include headers. But that won’t mean all headers andlibraries will be found. You’ll still need to use -I and -L to locatethose extra ones, depending on your distribution.

Most of the time, what you want is to have a native compiler to theplatform itself, but not others. So there’s rarely a point in compilingall back-ends. For that reason, you should also set theTARGETS_TO_BUILD to only build the back-end you’re targeting to.

You must set the CMAKE_INSTALL_PREFIX, otherwise a ninja installwill copy ARM binaries to your root filesystem, which is not what youwant.

Hacks

There are some bugs in current LLVM, which require some fiddling beforerunning CMake:

  • If you’re using Clang as the cross-compiler, there is a problem inthe LLVM ARM back-end that is producing absolute relocations onposition-independent code (R_ARM_THM_MOVW_ABS_NC), so for now, youshould disable PIC:
  1. -DLLVM_ENABLE_PIC=False

This is not a problem, since Clang/LLVM libraries are staticallylinked anyway, it shouldn’t affect much.

  • The ARM libraries won’t be installed in your system.But the CMake prepare step, which checks fordependencies, will check the host libraries, not the _target_ones. Below there’s a list of some dependencies, but your project couldhave more, or this document could be outdated. You’ll see the errorswhile linking as an indication of that.

Debian based distros have a way to add multiarch, which addsa new architecture and allows you to install packages for thosesystems. See https://wiki.debian.org/Multiarch/HOWTO for more info.

But not all distros will have that, and possibly not an easy way toinstall them in any anyway, so you’ll have to build/downloadthem separately.

A quick way of getting the libraries is to download them froma distribution repository, like Debian (http://packages.debian.org/jessie/),and download the missing libraries. Note that the libXXXwill have the shared objects (.so) and the libXXX-dev willgive you the headers and the static (.a) library. Just incase, download both.

The ones you need for ARM are: libtinfo, zlib1g,libxml2 and liblzma. In the Debian repository you’llfind downloads for all architectures.

After you download and unpack all .deb packages, copy all.so and .a to a directory, make the appropriatesymbolic links (if necessary), and add the relevant -Land -I paths to -DCMAKE_CXX_FLAGS above.

Running CMake and Building

Finally, if you’re using your platform compiler, run:

  1. $ cmake -G Ninja <source-dir> <options above>

If you’re using Clang as the cross-compiler, run:

  1. $ CC='clang' CXX='clang++' cmake -G Ninja <source-dir> <options above>

If you have clang/clang++ on the path, it should just work, and specialNinja files will be created in the build directory. I strongly suggestyou to run cmake on a separate build directory, not inside thesource tree.

To build, simply type:

  1. $ ninja

It should automatically find out how many cores you have, what arethe rules that needs building and will build the whole thing.

You can’t run ninja check-all on this tree because the createdbinaries are targeted to ARM, not x86_64.

Installing and Using

After the LLVM/Clang has built successfully, you should install itvia:

  1. $ ninja install

which will create a sysroot on the install-dir. You can then tarthat directory into a binary with the full triple name (for easyidentification), like:

  1. $ ln -sf <install-dir> arm-linux-gnueabihf-clang$ tar zchf arm-linux-gnueabihf-clang.tar.gz arm-linux-gnueabihf-clang

If you copy that tarball to your target board, you’ll be able to useit for running the test-suite, for example. Follow the guidelines athttp://llvm.org/docs/lnt/quickstart.html, unpack the tarball in thetest directory, and use options:

  1. $ ./sandbox/bin/python sandbox/bin/lnt runtest nt \ sandbox sandbox \ test-suite pwd/test-suite \ cc pwd/arm-linux-gnueabihf-clang/bin/clang \ cxx pwd/arm-linux-gnueabihf-clang/bin/clang++

Remember to add the -jN options to lnt to the number of CPUson your board. Also, the path to your clang has to be absolute, soyou’ll need the pwd trick above.