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