You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The document has had a few minor tweaks over the years, but the last
major piece of work on it was 2016, after first being introduced in
2013. My aim is to provide a clear and clean recipe for cross-compiling
LLVM that:
* Should be achievable for anyone on common variants of Linux
(_including_ the step of acquiring a working sysroot).
* I think I've kept the coverage of setting up acquiring a Debian
sysroot minimal enough that it can reasonably be included.
`debootstrap` is packaged for most common Linux distributions including
non-Debian derived distributions like Arch Linux and Fedora.
* Describes a setup that we can reasonably support within the community.
* I realise with the ninja symlink canonicalisation issue I haven't
completely avoided hacks, but I look particularly to point 2 under
hacks in the current docs which talks about libraries on the host
being found by CMake and adding `-L` and `-I` to try to hack around
this. We've all been there and made these kind of temporary workarounds
to see if we can get further, but it's very hard to support someone who
has problems with a setup that's improperly leaking between the host and
target like this. The approach I describe with a clean sysroot and
setting appropriate `CMAKE_FIND_ROOT_PATH_MODE_*` settings doesn't have
this issue.
* Cuts down on extraneous / outdated information, especially where it is
better covered elsewhere (e.g. detailed descriptions of CMake options
not directly relevant to cross compilation).
I've run through the instructions for AArch64, RISC-V (64-bit), and
armhf.
These instructions have been tested for targeting 32-bit ARM, AArch64, or
22
+
RISC-V but should be equally applicable to any other target.
24
23
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
24
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:
42
-
43
-
* ``-DCMAKE_SYSTEM_NAME=<target-system>``
44
-
* ``-DCMAKE_INSTALL_PREFIX=<install-dir>``
45
-
* ``-DLLVM_HOST_TRIPLE=arm-linux-gnueabihf``
46
-
* ``-DLLVM_TARGETS_TO_BUILD=ARM``
47
-
48
-
Note: ``CMAKE_CROSSCOMPILING`` is always set automatically when ``CMAKE_SYSTEM_NAME`` is set. Don't put ``-DCMAKE_CROSSCOMPILING=TRUE`` in your options.
49
-
50
-
Also note that ``LLVM_HOST_TRIPLE`` specifies the triple of the system
51
-
that the cross built LLVM is going to run on - the flag is named based
52
-
on the autoconf build/host/target nomenclature. (This flag implicitly sets
53
-
other defaults, such as ``LLVM_DEFAULT_TARGET_TRIPLE``.)
54
-
55
-
If you're compiling with GCC, you can use architecture options for your target,
56
-
and the compiler driver will detect everything that it needs:
and are set so as to avoid files for the host being used in the build.
129
+
* ``LLVM_HOST_TRIPLE``: Specifies the target triple the built LLVM will run
130
+
on, which also implicitly sets other defaults such as
131
+
``LLVM_DEFAULT_TARGET_TRIPLE``.
132
+
* ``CMAKE_SYSROOT``: Gives the path to the sysroot containing libraries and
133
+
headers for the target, as generated earlier in this document.
134
+
* ``CMAKE_INSTALL_PREFIX``: Unnecessary if you don't intend to use the
135
+
``install`` target. But setting it avoids installing binaries compiled for
136
+
the target across system directories on the host system.
137
+
138
+
See `LLVM's build documentation
139
+
<https://llvm.org/docs/CMake.html#frequently-used-cmake-variables>`_ for more
140
+
guidance on CMake variables (e.g. ``LLVM_TARGETS_TO_BUILD`` may be useful if
141
+
your cross-compiled binaries only need to support compiling for one target).
142
+
143
+
Assuming you have ``qemu-user`` installed you can test the produced target
144
+
binaries either by relying on binfmt_misc (as was necessary for debootstrap)
145
+
or invoking ``qemu-{tgt}-static`` directly. For instance, to first check the
146
+
type of executable and then run ``clang --version`` both ways:
170
147
171
148
.. code-block:: bash
172
149
173
-
$ ninja
150
+
$ file -L ./build/aarch64-linux-gnu/bin/clang
151
+
./build/aarch64-linux-gnu/bin/clang: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=516b8b366a790fcd3563bee4aec0cdfcb90bb1c7, not stripped
0 commit comments