blob: e8e5fefa39b998b0910bf5ad991a34afc449555d [file] [log] [blame]
Renato Golind24899b2013-09-08 20:44:48 +00001===================================================================
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 http://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
Sean Silva23d2f692013-09-09 19:05:03 +000025 * ``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 Golind24899b2013-09-08 20:44:48 +000034
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:
Sean Silva23d2f692013-09-09 19:05:03 +000042 * ``-DCMAKE_CROSSCOMPILING=True``
43 * ``-DCMAKE_INSTALL_PREFIX=<install-dir>``
44 * ``-DLLVM_TABLEGEN=<path-to-host-bin>/llvm-tblgen``
45 * ``-DCLANG_TABLEGEN=<path-to-host-bin>/clang-tblgen``
46 * ``-DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf``
47 * ``-DLLVM_TARGET_ARCH=ARM``
48 * ``-DLLVM_TARGETS_TO_BUILD=ARM``
Renato Golin66172362016-05-10 12:54:12 +000049
50If you're compiling with GCC, you can use architecture options for your target,
51and the compiler driver will detect everything that it needs:
52 * ``-DCMAKE_CXX_FLAGS='-march=armv7-a -mcpu=cortex-a9 -mfloat-abi=hard'``
53
54However, if you're using Clang, the driver might not be up-to-date with your
55specific Linux distribution, version or GCC layout, so you'll need to fudge.
56
57In addition to the ones above, you'll also need:
58 * ``'-target arm-linux-gnueabihf'`` or whatever is the triple of your cross GCC.
59 * ``'--sysroot=/usr/arm-linux-gnueabihf'``, ``'--sysroot=/opt/gcc/arm-linux-gnueabihf'``
60 or whatever is the location of your GCC's sysroot (where /lib, /bin etc are).
61 * Appropriate use of ``-I`` and ``-L``, depending on how the cross GCC is installed,
62 and where are the libraries and headers.
Renato Golind24899b2013-09-08 20:44:48 +000063
64The TableGen options are required to compile it with the host compiler,
Sean Silva23d2f692013-09-09 19:05:03 +000065so you'll need to compile LLVM (or at least ``llvm-tblgen``) to your host
Renato Golin66172362016-05-10 12:54:12 +000066platform before you start. The CXX flags define the target, cpu (which in this case
Sean Silva23d2f692013-09-09 19:05:03 +000067defaults to ``fpu=VFP3`` with NEON), and forcing the hard-float ABI. If you're
Renato Golin66172362016-05-10 12:54:12 +000068using Clang as a cross-compiler, you will *also* have to set ``--sysroot``
Renato Golind24899b2013-09-08 20:44:48 +000069to make sure it picks the correct linker.
70
Renato Golin66172362016-05-10 12:54:12 +000071When using Clang, it's important that you choose the triple to be *identical*
72to the GCC triple and the sysroot. This will make it easier for Clang to
73find the correct tools and include headers. But that won't mean all headers and
74libraries will be found. You'll still need to use ``-I`` and ``-L`` to locate
75those extra ones, depending on your distribution.
76
Renato Golind24899b2013-09-08 20:44:48 +000077Most of the time, what you want is to have a native compiler to the
Renato Golin66172362016-05-10 12:54:12 +000078platform itself, but not others. So there's rarely a point in compiling
Sean Silva23d2f692013-09-09 19:05:03 +000079all back-ends. For that reason, you should also set the
Renato Golin66172362016-05-10 12:54:12 +000080``TARGETS_TO_BUILD`` to only build the back-end you're targeting to.
Renato Golind24899b2013-09-08 20:44:48 +000081
Sean Silva23d2f692013-09-09 19:05:03 +000082You must set the ``CMAKE_INSTALL_PREFIX``, otherwise a ``ninja install``
Renato Golind24899b2013-09-08 20:44:48 +000083will copy ARM binaries to your root filesystem, which is not what you
84want.
85
86Hacks
87-----
88
89There are some bugs in current LLVM, which require some fiddling before
90running CMake:
91
92#. If you're using Clang as the cross-compiler, there is a problem in
93 the LLVM ARM back-end that is producing absolute relocations on
Sean Silva23d2f692013-09-09 19:05:03 +000094 position-independent code (``R_ARM_THM_MOVW_ABS_NC``), so for now, you
Renato Golind24899b2013-09-08 20:44:48 +000095 should disable PIC:
96
97 .. code-block:: bash
98
99 -DLLVM_ENABLE_PIC=False
100
101 This is not a problem, since Clang/LLVM libraries are statically
102 linked anyway, it shouldn't affect much.
103
Renato Golin66172362016-05-10 12:54:12 +0000104#. The ARM libraries won't be installed in your system.
105 But the CMake prepare step, which checks for
Sean Silva23d2f692013-09-09 19:05:03 +0000106 dependencies, will check the *host* libraries, not the *target*
Renato Golin66172362016-05-10 12:54:12 +0000107 ones. Below there's a list of some dependencies, but your project could
108 have more, or this document could be outdated. You'll see the errors
109 while linking as an indication of that.
110
111 Debian based distros have a way to add ``multiarch``, which adds
112 a new architecture and allows you to install packages for those
113 systems. See https://wiki.debian.org/Multiarch/HOWTO for more info.
114
115 But not all distros will have that, and possibly not an easy way to
116 install them in any anyway, so you'll have to build/download
117 them separately.
Renato Golind24899b2013-09-08 20:44:48 +0000118
119 A quick way of getting the libraries is to download them from
Renato Golin66172362016-05-10 12:54:12 +0000120 a distribution repository, like Debian (http://packages.debian.org/jessie/),
Sean Silva23d2f692013-09-09 19:05:03 +0000121 and download the missing libraries. Note that the ``libXXX``
122 will have the shared objects (``.so``) and the ``libXXX-dev`` will
123 give you the headers and the static (``.a``) library. Just in
Renato Golind24899b2013-09-08 20:44:48 +0000124 case, download both.
125
126 The ones you need for ARM are: ``libtinfo``, ``zlib1g``,
127 ``libxml2`` and ``liblzma``. In the Debian repository you'll
128 find downloads for all architectures.
129
Sean Silva23d2f692013-09-09 19:05:03 +0000130 After you download and unpack all ``.deb`` packages, copy all
Renato Golind24899b2013-09-08 20:44:48 +0000131 ``.so`` and ``.a`` to a directory, make the appropriate
132 symbolic links (if necessary), and add the relevant ``-L``
Sean Silva23d2f692013-09-09 19:05:03 +0000133 and ``-I`` paths to ``-DCMAKE_CXX_FLAGS`` above.
Renato Golind24899b2013-09-08 20:44:48 +0000134
135
136Running CMake and Building
137--------------------------
138
139Finally, if you're using your platform compiler, run:
140
141 .. code-block:: bash
142
143 $ cmake -G Ninja <source-dir> <options above>
144
145If you're using Clang as the cross-compiler, run:
146
147 .. code-block:: bash
148
149 $ CC='clang' CXX='clang++' cmake -G Ninja <source-dir> <options above>
150
Sean Silva23d2f692013-09-09 19:05:03 +0000151If you have ``clang``/``clang++`` on the path, it should just work, and special
Renato Golind24899b2013-09-08 20:44:48 +0000152Ninja files will be created in the build directory. I strongly suggest
Sean Silva23d2f692013-09-09 19:05:03 +0000153you to run ``cmake`` on a separate build directory, *not* inside the
Renato Golind24899b2013-09-08 20:44:48 +0000154source tree.
155
156To build, simply type:
157
158 .. code-block:: bash
159
160 $ ninja
161
162It should automatically find out how many cores you have, what are
163the rules that needs building and will build the whole thing.
164
165You can't run ``ninja check-all`` on this tree because the created
166binaries are targeted to ARM, not x86_64.
167
168Installing and Using
169--------------------
170
171After the LLVM/Clang has built successfully, you should install it
172via:
173
174 .. code-block:: bash
175
176 $ ninja install
177
Sean Silva23d2f692013-09-09 19:05:03 +0000178which will create a sysroot on the install-dir. You can then tar
Renato Golind24899b2013-09-08 20:44:48 +0000179that directory into a binary with the full triple name (for easy
180identification), like:
181
182 .. code-block:: bash
183
184 $ ln -sf <install-dir> arm-linux-gnueabihf-clang
185 $ tar zchf arm-linux-gnueabihf-clang.tar.gz arm-linux-gnueabihf-clang
186
Sean Silva23d2f692013-09-09 19:05:03 +0000187If you copy that tarball to your target board, you'll be able to use
Renato Golind24899b2013-09-08 20:44:48 +0000188it for running the test-suite, for example. Follow the guidelines at
Sean Silva23d2f692013-09-09 19:05:03 +0000189http://llvm.org/docs/lnt/quickstart.html, unpack the tarball in the
Renato Golind24899b2013-09-08 20:44:48 +0000190test directory, and use options:
191
192 .. code-block:: bash
193
194 $ ./sandbox/bin/python sandbox/bin/lnt runtest nt \
195 --sandbox sandbox \
196 --test-suite `pwd`/test-suite \
197 --cc `pwd`/arm-linux-gnueabihf-clang/bin/clang \
198 --cxx `pwd`/arm-linux-gnueabihf-clang/bin/clang++
199
200Remember to add the ``-jN`` options to ``lnt`` to the number of CPUs
201on your board. Also, the path to your clang has to be absolute, so
202you'll need the `pwd` trick above.