llvm.org GIT mirror llvm / 8216947
Cross-compilation doc git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190282 91177308-0d34-0410-b5e6-96231b3b80d8 Renato Golin 6 years ago
2 changed file(s) with 182 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 ===================================================================
1 How To Cross-Compile Clang/LLVM using Clang/LLVM
2 ===================================================================
3
4 Introduction
5 ============
6
7 This document contains information about building LLVM and
8 Clang on host machine, targeting another platform.
9
10 For more information on how to use Clang as a cross-compiler,
11 please check http://clang.llvm.org/docs/CrossCompilation.html.
12
13 TODO: Add MIPS and other platforms to this document.
14
15 Cross-Compiling from x86_64 to ARM
16 ==================================
17
18 In this use case, we'll be using CMake and Ninja, on a Debian-based Linux
19 system, cross-compiling from an x86_64 host (most Intel and AMD chips
20 nowadays) to a hard-float ARM target (most ARM targets nowadays).
21
22 The packages you'll need are:
23
24 * cmake
25 * ninja-build (from backports in Ubuntu)
26 * gcc-4.7-arm-linux-gnueabihf
27 * gcc-4.7-multilib-arm-linux-gnueabihf
28 * binutils-arm-linux-gnueabihf
29 * libgcc1-armhf-cross
30 * libsfgcc1-armhf-cross
31 * libstdc++6-armhf-cross
32 * libstdc++6-4.7-dev-armhf-cross
33
34 Configuring CMake
35 -----------------
36
37 For more information on how to configure CMake for LLVM/Clang,
38 see :doc:`CMake`.
39
40 The CMake options you need to add are:
41 * -DCMAKE_CROSSCOMPILING=True
42 * -DCMAKE_INSTALL_PREFIX=
43 * -DLLVM_TABLEGEN=/llvm-tblgen
44 * -DCLANG_TABLEGEN=/clang-tblgen
45 * -DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf
46 * -DLLVM_TARGET_ARCH=ARM
47 * -DLLVM_TARGETS_TO_BUILD=ARM
48 * -DCMAKE_CXX_FLAGS='-target armv7a-linux-gnueabihf -mcpu=cortex-a9
49 -I/usr/arm-linux-gnueabihf/include/c++/4.7.2/arm-linux-gnueabihf/
50 -I/usr/arm-linux-gnueabihf/include/ -mfloat-abi=hard
51 -ccc-gcc-name arm-linux-gnueabihf-gcc'
52
53 The TableGen options are required to compile it with the host compiler,
54 so you'll need to compile LLVM (or at least `llvm-tblgen`) to your host
55 platform before you start. The CXX flags define the target, cpu (which
56 defaults to fpu=VFP3 with NEON), and forcing the hard-float ABI. If you're
57 using Clang as a cross-compiler, you will *also* have to set ``-ccc-gcc-name``,
58 to make sure it picks the correct linker.
59
60 Most of the time, what you want is to have a native compiler to the
61 platform itself, but not others. It might not even be feasible to
62 produce x86 binaries from ARM targets, so there's no point in compiling
63 all back-ends. For that reason, you should also set the "TARGETS_TO_BUILD"
64 to only build the ARM back-end.
65
66 You must set the CMAKE_INSTALL_PREFIX, otherwise a ``ninja install``
67 will copy ARM binaries to your root filesystem, which is not what you
68 want.
69
70 Hacks
71 -----
72
73 There are some bugs in current LLVM, which require some fiddling before
74 running CMake:
75
76 #. If you're using Clang as the cross-compiler, there is a problem in
77 the LLVM ARM back-end that is producing absolute relocations on
78 position-independent code (R_ARM_THM_MOVW_ABS_NC), so for now, you
79 should disable PIC:
80
81 .. code-block:: bash
82
83 -DLLVM_ENABLE_PIC=False
84
85 This is not a problem, since Clang/LLVM libraries are statically
86 linked anyway, it shouldn't affect much.
87
88 #. The ARM libraries won't be installed in your system, and possibly
89 not easily installable anyway, so you'll have to build/download
90 them separately. But the CMake prepare step, which check for
91 dependencies, will check the `host` libraries, not the `target`
92 ones.
93
94 A quick way of getting the libraries is to download them from
95 a distribution repository, like Debian (http://packages.debian.org/wheezy/),
96 and download the missing libraries. Note that the `libXXX`
97 will have the shared objects (.so) and the `libXXX-dev` will
98 give you the headers and the static (.a) library. Just in
99 case, download both.
100
101 The ones you need for ARM are: ``libtinfo``, ``zlib1g``,
102 ``libxml2`` and ``liblzma``. In the Debian repository you'll
103 find downloads for all architectures.
104
105 After you download and unpack all `.deb` packages, copy all
106 ``.so`` and ``.a`` to a directory, make the appropriate
107 symbolic links (if necessary), and add the relevant ``-L``
108 and ``-I`` paths to -DCMAKE_CXX_FLAGS above.
109
110
111 Running CMake and Building
112 --------------------------
113
114 Finally, if you're using your platform compiler, run:
115
116 .. code-block:: bash
117
118 $ cmake -G Ninja
119
120 If you're using Clang as the cross-compiler, run:
121
122 .. code-block:: bash
123
124 $ CC='clang' CXX='clang++' cmake -G Ninja
125
126 If you have clang/clang++ on the path, it should just work, and special
127 Ninja files will be created in the build directory. I strongly suggest
128 you to run cmake on a separate build directory, *not* inside the
129 source tree.
130
131 To build, simply type:
132
133 .. code-block:: bash
134
135 $ ninja
136
137 It should automatically find out how many cores you have, what are
138 the rules that needs building and will build the whole thing.
139
140 You can't run ``ninja check-all`` on this tree because the created
141 binaries are targeted to ARM, not x86_64.
142
143 Installing and Using
144 --------------------
145
146 After the LLVM/Clang has built successfully, you should install it
147 via:
148
149 .. code-block:: bash
150
151 $ ninja install
152
153 which will create a sysroot on the install-dir. You can then TarGz
154 that directory into a binary with the full triple name (for easy
155 identification), like:
156
157 .. code-block:: bash
158
159 $ ln -sf arm-linux-gnueabihf-clang
160 $ tar zchf arm-linux-gnueabihf-clang.tar.gz arm-linux-gnueabihf-clang
161
162 If you copy that TarBall to your target board, you'll be able to use
163 it for running the test-suite, for example. Follow the guidelines at
164 http://llvm.org/docs/lnt/quickstart.html, unpack the TarBall in the
165 test directory, and use options:
166
167 .. code-block:: bash
168
169 $ ./sandbox/bin/python sandbox/bin/lnt runtest nt \
170 --sandbox sandbox \
171 --test-suite `pwd`/test-suite \
172 --cc `pwd`/arm-linux-gnueabihf-clang/bin/clang \
173 --cxx `pwd`/arm-linux-gnueabihf-clang/bin/clang++
174
175 Remember to add the ``-jN`` options to ``lnt`` to the number of CPUs
176 on your board. Also, the path to your clang has to be absolute, so
177 you'll need the `pwd` trick above.
6565
6666 CMake
6767 HowToBuildOnARM
68 HowToCrossCompileLLVM
6869 CommandGuide/index
6970 GettingStarted
7071 GettingStartedVS
9394
9495 :doc:`HowToBuildOnARM`
9596 Notes on building and testing LLVM/Clang on ARM.
97
98 :doc:`HowToCrossCompileLLVM`
99 Notes on cross-building and testing LLVM/Clang.
96100
97101 :doc:`GettingStartedVS`
98102 An addendum to the main Getting Started guide for those using Visual Studio