1=================================================================== 2How To Cross-Compile Clang/LLVM using Clang/LLVM 3=================================================================== 4 5Introduction 6============ 7 8This document contains information about building LLVM and 9Clang on host machine, targeting another platform. 10 11For more information on how to use Clang as a cross-compiler, 12please check https://clang.llvm.org/docs/CrossCompilation.html. 13 14TODO: Add MIPS and other platforms to this document. 15 16Cross-Compiling from x86_64 to ARM 17================================== 18 19In this use case, we'll be using CMake and Ninja, on a Debian-based Linux 20system, cross-compiling from an x86_64 host (most Intel and AMD chips 21nowadays) to a hard-float ARM target (most ARM targets nowadays). 22 23The packages you'll need are: 24 25 * ``cmake`` 26 * ``ninja-build`` (from backports in Ubuntu) 27 * ``gcc-4.7-arm-linux-gnueabihf`` 28 * ``gcc-4.7-multilib-arm-linux-gnueabihf`` 29 * ``binutils-arm-linux-gnueabihf`` 30 * ``libgcc1-armhf-cross`` 31 * ``libsfgcc1-armhf-cross`` 32 * ``libstdc++6-armhf-cross`` 33 * ``libstdc++6-4.7-dev-armhf-cross`` 34 35Configuring CMake 36----------------- 37 38For more information on how to configure CMake for LLVM/Clang, 39see :doc:`CMake`. 40 41The CMake options you need to add are: 42 43 * ``-DCMAKE_SYSTEM_NAME=<target-system>`` 44 * ``-DCMAKE_INSTALL_PREFIX=<install-dir>`` 45 * ``-DLLVM_TABLEGEN=<path-to-host-bin>/llvm-tblgen`` 46 * ``-DCLANG_TABLEGEN=<path-to-host-bin>/clang-tblgen`` 47 * ``-DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf`` 48 * ``-DLLVM_TARGET_ARCH=ARM`` 49 * ``-DLLVM_TARGETS_TO_BUILD=ARM`` 50 51Note: ``CMAKE_CROSSCOMPILING`` is always set automatically when ``CMAKE_SYSTEM_NAME`` is set. Don't put ``-DCMAKE_CROSSCOMPILING=TRUE`` in your options. 52 53If you're compiling with GCC, you can use architecture options for your target, 54and the compiler driver will detect everything that it needs: 55 56 * ``-DCMAKE_CXX_FLAGS='-march=armv7-a -mcpu=cortex-a9 -mfloat-abi=hard'`` 57 58However, if you're using Clang, the driver might not be up-to-date with your 59specific Linux distribution, version or GCC layout, so you'll need to fudge. 60 61In addition to the ones above, you'll also need: 62 63 * ``--target=arm-linux-gnueabihf`` or whatever is the triple of your cross GCC. 64 * ``'--sysroot=/usr/arm-linux-gnueabihf'``, ``'--sysroot=/opt/gcc/arm-linux-gnueabihf'`` 65 or whatever is the location of your GCC's sysroot (where /lib, /bin etc are). 66 * Appropriate use of ``-I`` and ``-L``, depending on how the cross GCC is installed, 67 and where are the libraries and headers. 68 69The TableGen options are required to compile it with the host compiler, 70so you'll need to compile LLVM (or at least ``llvm-tblgen``) to your host 71platform before you start. The CXX flags define the target, cpu (which in this case 72defaults to ``fpu=VFP3`` with NEON), and forcing the hard-float ABI. If you're 73using Clang as a cross-compiler, you will *also* have to set ``--sysroot`` 74to make sure it picks the correct linker. 75 76When using Clang, it's important that you choose the triple to be *identical* 77to the GCC triple and the sysroot. This will make it easier for Clang to 78find the correct tools and include headers. But that won't mean all headers and 79libraries will be found. You'll still need to use ``-I`` and ``-L`` to locate 80those extra ones, depending on your distribution. 81 82Most of the time, what you want is to have a native compiler to the 83platform itself, but not others. So there's rarely a point in compiling 84all back-ends. For that reason, you should also set the 85``TARGETS_TO_BUILD`` to only build the back-end you're targeting to. 86 87You must set the ``CMAKE_INSTALL_PREFIX``, otherwise a ``ninja install`` 88will copy ARM binaries to your root filesystem, which is not what you 89want. 90 91Hacks 92----- 93 94There are some bugs in current LLVM, which require some fiddling before 95running CMake: 96 97#. If you're using Clang as the cross-compiler, there is a problem in 98 the LLVM ARM back-end that is producing absolute relocations on 99 position-independent code (``R_ARM_THM_MOVW_ABS_NC``), so for now, you 100 should disable PIC: 101 102 .. code-block:: bash 103 104 -DLLVM_ENABLE_PIC=False 105 106 This is not a problem, since Clang/LLVM libraries are statically 107 linked anyway, it shouldn't affect much. 108 109#. The ARM libraries won't be installed in your system. 110 But the CMake prepare step, which checks for 111 dependencies, will check the *host* libraries, not the *target* 112 ones. Below there's a list of some dependencies, but your project could 113 have more, or this document could be outdated. You'll see the errors 114 while linking as an indication of that. 115 116 Debian based distros have a way to add ``multiarch``, which adds 117 a new architecture and allows you to install packages for those 118 systems. See https://wiki.debian.org/Multiarch/HOWTO for more info. 119 120 But not all distros will have that, and possibly not an easy way to 121 install them in any anyway, so you'll have to build/download 122 them separately. 123 124 A quick way of getting the libraries is to download them from 125 a distribution repository, like Debian (http://packages.debian.org/jessie/), 126 and download the missing libraries. Note that the ``libXXX`` 127 will have the shared objects (``.so``) and the ``libXXX-dev`` will 128 give you the headers and the static (``.a``) library. Just in 129 case, download both. 130 131 The ones you need for ARM are: ``libtinfo``, ``zlib1g``, 132 ``libxml2`` and ``liblzma``. In the Debian repository you'll 133 find downloads for all architectures. 134 135 After you download and unpack all ``.deb`` packages, copy all 136 ``.so`` and ``.a`` to a directory, make the appropriate 137 symbolic links (if necessary), and add the relevant ``-L`` 138 and ``-I`` paths to ``-DCMAKE_CXX_FLAGS`` above. 139 140 141Running CMake and Building 142-------------------------- 143 144Finally, if you're using your platform compiler, run: 145 146 .. code-block:: bash 147 148 $ cmake -G Ninja <source-dir> -DCMAKE_BUILD_TYPE=<type> <options above> 149 150If you're using Clang as the cross-compiler, run: 151 152 .. code-block:: bash 153 154 $ CC='clang' CXX='clang++' cmake -G Ninja <source-dir> -DCMAKE_BUILD_TYPE=<type> <options above> 155 156If you have ``clang``/``clang++`` on the path, it should just work, and special 157Ninja files will be created in the build directory. I strongly suggest 158you to run ``cmake`` on a separate build directory, *not* inside the 159source tree. 160 161To build, simply type: 162 163 .. code-block:: bash 164 165 $ ninja 166 167It should automatically find out how many cores you have, what are 168the rules that needs building and will build the whole thing. 169 170You can't run ``ninja check-all`` on this tree because the created 171binaries are targeted to ARM, not x86_64. 172 173Installing and Using 174-------------------- 175 176After the LLVM/Clang has built successfully, you should install it 177via: 178 179 .. code-block:: bash 180 181 $ ninja install 182 183which will create a sysroot on the install-dir. You can then tar 184that directory into a binary with the full triple name (for easy 185identification), like: 186 187 .. code-block:: bash 188 189 $ ln -sf <install-dir> arm-linux-gnueabihf-clang 190 $ tar zchf arm-linux-gnueabihf-clang.tar.gz arm-linux-gnueabihf-clang 191 192If you copy that tarball to your target board, you'll be able to use 193it for running the test-suite, for example. Follow the guidelines at 194https://llvm.org/docs/lnt/quickstart.html, unpack the tarball in the 195test directory, and use options: 196 197 .. code-block:: bash 198 199 $ ./sandbox/bin/python sandbox/bin/lnt runtest nt \ 200 --sandbox sandbox \ 201 --test-suite `pwd`/test-suite \ 202 --cc `pwd`/arm-linux-gnueabihf-clang/bin/clang \ 203 --cxx `pwd`/arm-linux-gnueabihf-clang/bin/clang++ 204 205Remember to add the ``-jN`` options to ``lnt`` to the number of CPUs 206on your board. Also, the path to your clang has to be absolute, so 207you'll need the `pwd` trick above. 208