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