xref: /minix3/external/bsd/llvm/dist/clang/docs/CrossCompilation.rst (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc===================================================================
2f4a2713aSLionel SambucCross-compilation using Clang
3f4a2713aSLionel Sambuc===================================================================
4f4a2713aSLionel Sambuc
5f4a2713aSLionel SambucIntroduction
6f4a2713aSLionel Sambuc============
7f4a2713aSLionel Sambuc
8f4a2713aSLionel SambucThis document will guide you in choosing the right Clang options
9f4a2713aSLionel Sambucfor cross-compiling your code to a different architecture. It assumes you
10f4a2713aSLionel Sambucalready know how to compile the code in question for the host architecture,
11f4a2713aSLionel Sambucand that you know how to choose additional include and library paths.
12f4a2713aSLionel Sambuc
13f4a2713aSLionel SambucHowever, this document is *not* a "how to" and won't help you setting your
14f4a2713aSLionel Sambucbuild system or Makefiles, nor choosing the right CMake options, etc.
15f4a2713aSLionel SambucAlso, it does not cover all the possible options, nor does it contain
16f4a2713aSLionel Sambucspecific examples for specific architectures. For a concrete example, the
17f4a2713aSLionel Sambuc`instructions for cross-compiling LLVM itself
18f4a2713aSLionel Sambuc<http://llvm.org/docs/HowToCrossCompileLLVM.html>`_ may be of interest.
19f4a2713aSLionel Sambuc
20f4a2713aSLionel SambucAfter reading this document, you should be familiar with the main issues
21f4a2713aSLionel Sambucrelated to cross-compilation, and what main compiler options Clang provides
22f4a2713aSLionel Sambucfor performing cross-compilation.
23f4a2713aSLionel Sambuc
24f4a2713aSLionel SambucCross compilation issues
25f4a2713aSLionel Sambuc========================
26f4a2713aSLionel Sambuc
27f4a2713aSLionel SambucIn GCC world, every host/target combination has its own set of binaries,
28f4a2713aSLionel Sambucheaders, libraries, etc. So, it's usually simple to download a package
29f4a2713aSLionel Sambucwith all files in, unzip to a directory and point the build system to
30f4a2713aSLionel Sambucthat compiler, that will know about its location and find all it needs to
31f4a2713aSLionel Sambucwhen compiling your code.
32f4a2713aSLionel Sambuc
33f4a2713aSLionel SambucOn the other hand, Clang/LLVM is natively a cross-compiler, meaning that
34f4a2713aSLionel Sambucone set of programs can compile to all targets by setting the ``-target``
35f4a2713aSLionel Sambucoption. That makes it a lot easier for programers wishing to compile to
36f4a2713aSLionel Sambucdifferent platforms and architectures, and for compiler developers that
37f4a2713aSLionel Sambuconly have to maintain one build system, and for OS distributions, that
38f4a2713aSLionel Sambucneed only one set of main packages.
39f4a2713aSLionel Sambuc
40f4a2713aSLionel SambucBut, as is true to any cross-compiler, and given the complexity of
41f4a2713aSLionel Sambucdifferent architectures, OS's and options, it's not always easy finding
42f4a2713aSLionel Sambucthe headers, libraries or binutils to generate target specific code.
43f4a2713aSLionel SambucSo you'll need special options to help Clang understand what target
44f4a2713aSLionel Sambucyou're compiling to, where your tools are, etc.
45f4a2713aSLionel Sambuc
46f4a2713aSLionel SambucAnother problem is that compilers come with standard libraries only (like
47f4a2713aSLionel Sambuc``compiler-rt``, ``libcxx``, ``libgcc``, ``libm``, etc), so you'll have to
48f4a2713aSLionel Sambucfind and make available to the build system, every other library required
49f4a2713aSLionel Sambucto build your software, that is specific to your target. It's not enough to
50f4a2713aSLionel Sambuchave your host's libraries installed.
51f4a2713aSLionel Sambuc
52f4a2713aSLionel SambucFinally, not all toolchains are the same, and consequently, not every Clang
53f4a2713aSLionel Sambucoption will work magically. Some options, like ``--sysroot`` (which
54f4a2713aSLionel Sambuceffectively changes the logical root for headers and libraries), assume
55f4a2713aSLionel Sambucall your binaries and libraries are in the same directory, which may not
56f4a2713aSLionel Sambuctrue when your cross-compiler was installed by the distribution's package
57f4a2713aSLionel Sambucmanagement. So, for each specific case, you may use more than one
58f4a2713aSLionel Sambucoption, and in most cases, you'll end up setting include paths (``-I``) and
59f4a2713aSLionel Sambuclibrary paths (``-L``) manually.
60f4a2713aSLionel Sambuc
61f4a2713aSLionel SambucTo sum up, different toolchains can:
62f4a2713aSLionel Sambuc * be host/target specific or more flexible
63f4a2713aSLionel Sambuc * be in a single directory, or spread out across your system
64f4a2713aSLionel Sambuc * have different sets of libraries and headers by default
65f4a2713aSLionel Sambuc * need special options, which your build system won't be able to figure
66f4a2713aSLionel Sambuc   out by itself
67f4a2713aSLionel Sambuc
68f4a2713aSLionel SambucGeneral Cross-Compilation Options in Clang
69f4a2713aSLionel Sambuc==========================================
70f4a2713aSLionel Sambuc
71f4a2713aSLionel SambucTarget Triple
72f4a2713aSLionel Sambuc-------------
73f4a2713aSLionel Sambuc
74f4a2713aSLionel SambucThe basic option is to define the target architecture. For that, use
75f4a2713aSLionel Sambuc``-target <triple>``. If you don't specify the target, CPU names won't
76f4a2713aSLionel Sambucmatch (since Clang assumes the host triple), and the compilation will
77f4a2713aSLionel Sambucgo ahead, creating code for the host platform, which will break later
78f4a2713aSLionel Sambucon when assembling or linking.
79f4a2713aSLionel Sambuc
80f4a2713aSLionel SambucThe triple has the general format ``<arch><sub>-<vendor>-<sys>-<abi>``, where:
81f4a2713aSLionel Sambuc * ``arch`` = ``x86``, ``arm``, ``thumb``, ``mips``, etc.
82f4a2713aSLionel Sambuc * ``sub`` = for ex. on ARM: ``v5``, ``v6m``, ``v7a``, ``v7m``, etc.
83f4a2713aSLionel Sambuc * ``vendor`` = ``pc``, ``apple``, ``nvidia``, ``ibm``, etc.
84f4a2713aSLionel Sambuc * ``sys`` = ``none``, ``linux``, ``win32``, ``darwin``, ``cuda``, etc.
85f4a2713aSLionel Sambuc * ``abi`` = ``eabi``, ``gnu``, ``android``, ``macho``, ``elf``, etc.
86f4a2713aSLionel Sambuc
87f4a2713aSLionel SambucThe sub-architecture options are available for their own architectures,
88f4a2713aSLionel Sambucof course, so "x86v7a" doesn't make sense. The vendor needs to be
89f4a2713aSLionel Sambucspecified only if there's a relevant change, for instance between PC
90f4a2713aSLionel Sambucand Apple. Most of the time it can be omitted (and Unknown)
91f4a2713aSLionel Sambucwill be assumed, which sets the defaults for the specified architecture.
92f4a2713aSLionel SambucThe system name is generally the OS (linux, darwin), but could be special
93f4a2713aSLionel Sambuclike the bare-metal "none".
94f4a2713aSLionel Sambuc
95f4a2713aSLionel SambucWhen a parameter is not important, they can be omitted, or you can
96f4a2713aSLionel Sambucchoose ``unknown`` and the defaults will be used. If you choose a parameter
97f4a2713aSLionel Sambucthat Clang doesn't know, like ``blerg``, it'll ignore and assume
98f4a2713aSLionel Sambuc``unknown``, which is not always desired, so be careful.
99f4a2713aSLionel Sambuc
100f4a2713aSLionel SambucFinally, the ABI option is something that will pick default CPU/FPU,
101f4a2713aSLionel Sambucdefine the specific behaviour of your code (PCS, extensions),
102f4a2713aSLionel Sambucand also choose the correct library calls, etc.
103f4a2713aSLionel Sambuc
104f4a2713aSLionel SambucCPU, FPU, ABI
105f4a2713aSLionel Sambuc-------------
106f4a2713aSLionel Sambuc
107f4a2713aSLionel SambucOnce your target is specified, it's time to pick the hardware you'll
108f4a2713aSLionel Sambucbe compiling to. For every architecture, a default set of CPU/FPU/ABI
109f4a2713aSLionel Sambucwill be chosen, so you'll almost always have to change it via flags.
110f4a2713aSLionel Sambuc
111f4a2713aSLionel SambucTypical flags include:
112f4a2713aSLionel Sambuc * ``-mcpu=<cpu-name>``, like x86-64, swift, cortex-a15
113f4a2713aSLionel Sambuc * ``-fpu=<fpu-name>``, like SSE3, NEON, controlling the FP unit available
114f4a2713aSLionel Sambuc * ``-mfloat-abi=<fabi>``, like soft, hard, controlling which registers
115f4a2713aSLionel Sambuc   to use for floating-point
116f4a2713aSLionel Sambuc
117f4a2713aSLionel SambucThe default is normally the common denominator, so that Clang doesn't
118f4a2713aSLionel Sambucgenerate code that breaks. But that also means you won't get the best
119f4a2713aSLionel Sambuccode for your specific hardware, which may mean orders of magnitude
120f4a2713aSLionel Sambucslower than you expect.
121f4a2713aSLionel Sambuc
122f4a2713aSLionel SambucFor example, if your target is ``arm-none-eabi``, the default CPU will
123f4a2713aSLionel Sambucbe ``arm7tdmi`` using soft float, which is extremely slow on modern cores,
124f4a2713aSLionel Sambucwhereas if your triple is ``armv7a-none-eabi``, it'll be Cortex-A8 with
125f4a2713aSLionel SambucNEON, but still using soft-float, which is much better, but still not
126f4a2713aSLionel Sambucgreat.
127f4a2713aSLionel Sambuc
128f4a2713aSLionel SambucToolchain Options
129f4a2713aSLionel Sambuc-----------------
130f4a2713aSLionel Sambuc
131f4a2713aSLionel SambucThere are three main options to control access to your cross-compiler:
132f4a2713aSLionel Sambuc``--sysroot``, ``-I``, and ``-L``. The two last ones are well known,
133f4a2713aSLionel Sambucbut they're particularly important for additional libraries
134f4a2713aSLionel Sambucand headers that are specific to your target.
135f4a2713aSLionel Sambuc
136f4a2713aSLionel SambucThere are two main ways to have a cross-compiler:
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc#. When you have extracted your cross-compiler from a zip file into
139f4a2713aSLionel Sambuc   a directory, you have to use ``--sysroot=<path>``. The path is the
140f4a2713aSLionel Sambuc   root directory where you have unpacked your file, and Clang will
141f4a2713aSLionel Sambuc   look for the directories ``bin``, ``lib``, ``include`` in there.
142f4a2713aSLionel Sambuc
143f4a2713aSLionel Sambuc   In this case, your setup should be pretty much done (if no
144f4a2713aSLionel Sambuc   additional headers or libraries are needed), as Clang will find
145f4a2713aSLionel Sambuc   all binaries it needs (assembler, linker, etc) in there.
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc#. When you have installed via a package manager (modern Linux
148f4a2713aSLionel Sambuc   distributions have cross-compiler packages available), make
149f4a2713aSLionel Sambuc   sure the target triple you set is *also* the prefix of your
150f4a2713aSLionel Sambuc   cross-compiler toolchain.
151f4a2713aSLionel Sambuc
152f4a2713aSLionel Sambuc   In this case, Clang will find the other binaries (assembler,
153f4a2713aSLionel Sambuc   linker), but not always where the target headers and libraries
154f4a2713aSLionel Sambuc   are. People add system-specific clues to Clang often, but as
155f4a2713aSLionel Sambuc   things change, it's more likely that it won't find than the
156f4a2713aSLionel Sambuc   other way around.
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc   So, here, you'll be a lot safer if you specify the include/library
159f4a2713aSLionel Sambuc   directories manually (via ``-I`` and ``-L``).
160f4a2713aSLionel Sambuc
161f4a2713aSLionel SambucTarget-Specific Libraries
162f4a2713aSLionel Sambuc=========================
163f4a2713aSLionel Sambuc
164f4a2713aSLionel SambucAll libraries that you compile as part of your build will be
165f4a2713aSLionel Sambuccross-compiled to your target, and your build system will probably
166f4a2713aSLionel Sambucfind them in the right place. But all dependencies that are
167f4a2713aSLionel Sambucnormally checked against (like ``libxml`` or ``libz`` etc) will match
168f4a2713aSLionel Sambucagainst the host platform, not the target.
169f4a2713aSLionel Sambuc
170f4a2713aSLionel SambucSo, if the build system is not aware that you want to cross-compile
171f4a2713aSLionel Sambucyour code, it will get every dependency wrong, and your compilation
172f4a2713aSLionel Sambucwill fail during build time, not configure time.
173f4a2713aSLionel Sambuc
174f4a2713aSLionel SambucAlso, finding the libraries for your target are not as easy
175f4a2713aSLionel Sambucas for your host machine. There aren't many cross-libraries available
176f4a2713aSLionel Sambucas packages to most OS's, so you'll have to either cross-compile them
177f4a2713aSLionel Sambucfrom source, or download the package for your target platform,
178f4a2713aSLionel Sambucextract the libraries and headers, put them in specific directories
179f4a2713aSLionel Sambucand add ``-I`` and ``-L`` pointing to them.
180f4a2713aSLionel Sambuc
181f4a2713aSLionel SambucAlso, some libraries have different dependencies on different targets,
182f4a2713aSLionel Sambucso configuration tools to find dependencies in the host can get the
183f4a2713aSLionel Sambuclist wrong for the target platform. This means that the configuration
184f4a2713aSLionel Sambucof your build can get things wrong when setting their own library
185f4a2713aSLionel Sambucpaths, and you'll have to augment it via additional flags (configure,
186f4a2713aSLionel SambucMake, CMake, etc).
187f4a2713aSLionel Sambuc
188f4a2713aSLionel SambucMultilibs
189f4a2713aSLionel Sambuc---------
190f4a2713aSLionel Sambuc
191f4a2713aSLionel SambucWhen you want to cross-compile to more than one configuration, for
192f4a2713aSLionel Sambucexample hard-float-ARM and soft-float-ARM, you'll have to have multiple
193*0a6a1f1dSLionel Sambuccopies of your libraries and (possibly) headers.
194f4a2713aSLionel Sambuc
195f4a2713aSLionel SambucSome Linux distributions have support for Multilib, which handle that
196f4a2713aSLionel Sambucfor you in an easier way, but if you're not careful and, for instance,
197f4a2713aSLionel Sambucforget to specify ``-ccc-gcc-name armv7l-linux-gnueabihf-gcc`` (which
198f4a2713aSLionel Sambucuses hard-float), Clang will pick the ``armv7l-linux-gnueabi-ld``
199f4a2713aSLionel Sambuc(which uses soft-float) and linker errors will happen.
200f4a2713aSLionel Sambuc
201f4a2713aSLionel SambucThe same is true if you're compiling for different ABIs, like ``gnueabi``
202f4a2713aSLionel Sambucand ``androideabi``, and might even link and run, but produce run-time
203f4a2713aSLionel Sambucerrors, which are much harder to track down and fix.
204