xref: /aosp_15_r20/external/clang/docs/CrossCompilation.rst (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li===================================================================
2*67e74705SXin LiCross-compilation using Clang
3*67e74705SXin Li===================================================================
4*67e74705SXin Li
5*67e74705SXin LiIntroduction
6*67e74705SXin Li============
7*67e74705SXin Li
8*67e74705SXin LiThis document will guide you in choosing the right Clang options
9*67e74705SXin Lifor cross-compiling your code to a different architecture. It assumes you
10*67e74705SXin Lialready know how to compile the code in question for the host architecture,
11*67e74705SXin Liand that you know how to choose additional include and library paths.
12*67e74705SXin Li
13*67e74705SXin LiHowever, this document is *not* a "how to" and won't help you setting your
14*67e74705SXin Libuild system or Makefiles, nor choosing the right CMake options, etc.
15*67e74705SXin LiAlso, it does not cover all the possible options, nor does it contain
16*67e74705SXin Lispecific examples for specific architectures. For a concrete example, the
17*67e74705SXin Li`instructions for cross-compiling LLVM itself
18*67e74705SXin Li<http://llvm.org/docs/HowToCrossCompileLLVM.html>`_ may be of interest.
19*67e74705SXin Li
20*67e74705SXin LiAfter reading this document, you should be familiar with the main issues
21*67e74705SXin Lirelated to cross-compilation, and what main compiler options Clang provides
22*67e74705SXin Lifor performing cross-compilation.
23*67e74705SXin Li
24*67e74705SXin LiCross compilation issues
25*67e74705SXin Li========================
26*67e74705SXin Li
27*67e74705SXin LiIn GCC world, every host/target combination has its own set of binaries,
28*67e74705SXin Liheaders, libraries, etc. So, it's usually simple to download a package
29*67e74705SXin Liwith all files in, unzip to a directory and point the build system to
30*67e74705SXin Lithat compiler, that will know about its location and find all it needs to
31*67e74705SXin Liwhen compiling your code.
32*67e74705SXin Li
33*67e74705SXin LiOn the other hand, Clang/LLVM is natively a cross-compiler, meaning that
34*67e74705SXin Lione set of programs can compile to all targets by setting the ``-target``
35*67e74705SXin Lioption. That makes it a lot easier for programmers wishing to compile to
36*67e74705SXin Lidifferent platforms and architectures, and for compiler developers that
37*67e74705SXin Lionly have to maintain one build system, and for OS distributions, that
38*67e74705SXin Lineed only one set of main packages.
39*67e74705SXin Li
40*67e74705SXin LiBut, as is true to any cross-compiler, and given the complexity of
41*67e74705SXin Lidifferent architectures, OS's and options, it's not always easy finding
42*67e74705SXin Lithe headers, libraries or binutils to generate target specific code.
43*67e74705SXin LiSo you'll need special options to help Clang understand what target
44*67e74705SXin Liyou're compiling to, where your tools are, etc.
45*67e74705SXin Li
46*67e74705SXin LiAnother problem is that compilers come with standard libraries only (like
47*67e74705SXin Li``compiler-rt``, ``libcxx``, ``libgcc``, ``libm``, etc), so you'll have to
48*67e74705SXin Lifind and make available to the build system, every other library required
49*67e74705SXin Lito build your software, that is specific to your target. It's not enough to
50*67e74705SXin Lihave your host's libraries installed.
51*67e74705SXin Li
52*67e74705SXin LiFinally, not all toolchains are the same, and consequently, not every Clang
53*67e74705SXin Lioption will work magically. Some options, like ``--sysroot`` (which
54*67e74705SXin Lieffectively changes the logical root for headers and libraries), assume
55*67e74705SXin Liall your binaries and libraries are in the same directory, which may not
56*67e74705SXin Litrue when your cross-compiler was installed by the distribution's package
57*67e74705SXin Limanagement. So, for each specific case, you may use more than one
58*67e74705SXin Lioption, and in most cases, you'll end up setting include paths (``-I``) and
59*67e74705SXin Lilibrary paths (``-L``) manually.
60*67e74705SXin Li
61*67e74705SXin LiTo sum up, different toolchains can:
62*67e74705SXin Li * be host/target specific or more flexible
63*67e74705SXin Li * be in a single directory, or spread out across your system
64*67e74705SXin Li * have different sets of libraries and headers by default
65*67e74705SXin Li * need special options, which your build system won't be able to figure
66*67e74705SXin Li   out by itself
67*67e74705SXin Li
68*67e74705SXin LiGeneral Cross-Compilation Options in Clang
69*67e74705SXin Li==========================================
70*67e74705SXin Li
71*67e74705SXin LiTarget Triple
72*67e74705SXin Li-------------
73*67e74705SXin Li
74*67e74705SXin LiThe basic option is to define the target architecture. For that, use
75*67e74705SXin Li``-target <triple>``. If you don't specify the target, CPU names won't
76*67e74705SXin Limatch (since Clang assumes the host triple), and the compilation will
77*67e74705SXin Ligo ahead, creating code for the host platform, which will break later
78*67e74705SXin Lion when assembling or linking.
79*67e74705SXin Li
80*67e74705SXin LiThe triple has the general format ``<arch><sub>-<vendor>-<sys>-<abi>``, where:
81*67e74705SXin Li * ``arch`` = ``x86``, ``arm``, ``thumb``, ``mips``, etc.
82*67e74705SXin Li * ``sub`` = for ex. on ARM: ``v5``, ``v6m``, ``v7a``, ``v7m``, etc.
83*67e74705SXin Li * ``vendor`` = ``pc``, ``apple``, ``nvidia``, ``ibm``, etc.
84*67e74705SXin Li * ``sys`` = ``none``, ``linux``, ``win32``, ``darwin``, ``cuda``, etc.
85*67e74705SXin Li * ``abi`` = ``eabi``, ``gnu``, ``android``, ``macho``, ``elf``, etc.
86*67e74705SXin Li
87*67e74705SXin LiThe sub-architecture options are available for their own architectures,
88*67e74705SXin Liof course, so "x86v7a" doesn't make sense. The vendor needs to be
89*67e74705SXin Lispecified only if there's a relevant change, for instance between PC
90*67e74705SXin Liand Apple. Most of the time it can be omitted (and Unknown)
91*67e74705SXin Liwill be assumed, which sets the defaults for the specified architecture.
92*67e74705SXin LiThe system name is generally the OS (linux, darwin), but could be special
93*67e74705SXin Lilike the bare-metal "none".
94*67e74705SXin Li
95*67e74705SXin LiWhen a parameter is not important, it can be omitted, or you can
96*67e74705SXin Lichoose ``unknown`` and the defaults will be used. If you choose a parameter
97*67e74705SXin Lithat Clang doesn't know, like ``blerg``, it'll ignore and assume
98*67e74705SXin Li``unknown``, which is not always desired, so be careful.
99*67e74705SXin Li
100*67e74705SXin LiFinally, the ABI option is something that will pick default CPU/FPU,
101*67e74705SXin Lidefine the specific behaviour of your code (PCS, extensions),
102*67e74705SXin Liand also choose the correct library calls, etc.
103*67e74705SXin Li
104*67e74705SXin LiCPU, FPU, ABI
105*67e74705SXin Li-------------
106*67e74705SXin Li
107*67e74705SXin LiOnce your target is specified, it's time to pick the hardware you'll
108*67e74705SXin Libe compiling to. For every architecture, a default set of CPU/FPU/ABI
109*67e74705SXin Liwill be chosen, so you'll almost always have to change it via flags.
110*67e74705SXin Li
111*67e74705SXin LiTypical flags include:
112*67e74705SXin Li * ``-mcpu=<cpu-name>``, like x86-64, swift, cortex-a15
113*67e74705SXin Li * ``-mfpu=<fpu-name>``, like SSE3, NEON, controlling the FP unit available
114*67e74705SXin Li * ``-mfloat-abi=<fabi>``, like soft, hard, controlling which registers
115*67e74705SXin Li   to use for floating-point
116*67e74705SXin Li
117*67e74705SXin LiThe default is normally the common denominator, so that Clang doesn't
118*67e74705SXin Ligenerate code that breaks. But that also means you won't get the best
119*67e74705SXin Licode for your specific hardware, which may mean orders of magnitude
120*67e74705SXin Lislower than you expect.
121*67e74705SXin Li
122*67e74705SXin LiFor example, if your target is ``arm-none-eabi``, the default CPU will
123*67e74705SXin Libe ``arm7tdmi`` using soft float, which is extremely slow on modern cores,
124*67e74705SXin Liwhereas if your triple is ``armv7a-none-eabi``, it'll be Cortex-A8 with
125*67e74705SXin LiNEON, but still using soft-float, which is much better, but still not
126*67e74705SXin Ligreat.
127*67e74705SXin Li
128*67e74705SXin LiToolchain Options
129*67e74705SXin Li-----------------
130*67e74705SXin Li
131*67e74705SXin LiThere are three main options to control access to your cross-compiler:
132*67e74705SXin Li``--sysroot``, ``-I``, and ``-L``. The two last ones are well known,
133*67e74705SXin Libut they're particularly important for additional libraries
134*67e74705SXin Liand headers that are specific to your target.
135*67e74705SXin Li
136*67e74705SXin LiThere are two main ways to have a cross-compiler:
137*67e74705SXin Li
138*67e74705SXin Li#. When you have extracted your cross-compiler from a zip file into
139*67e74705SXin Li   a directory, you have to use ``--sysroot=<path>``. The path is the
140*67e74705SXin Li   root directory where you have unpacked your file, and Clang will
141*67e74705SXin Li   look for the directories ``bin``, ``lib``, ``include`` in there.
142*67e74705SXin Li
143*67e74705SXin Li   In this case, your setup should be pretty much done (if no
144*67e74705SXin Li   additional headers or libraries are needed), as Clang will find
145*67e74705SXin Li   all binaries it needs (assembler, linker, etc) in there.
146*67e74705SXin Li
147*67e74705SXin Li#. When you have installed via a package manager (modern Linux
148*67e74705SXin Li   distributions have cross-compiler packages available), make
149*67e74705SXin Li   sure the target triple you set is *also* the prefix of your
150*67e74705SXin Li   cross-compiler toolchain.
151*67e74705SXin Li
152*67e74705SXin Li   In this case, Clang will find the other binaries (assembler,
153*67e74705SXin Li   linker), but not always where the target headers and libraries
154*67e74705SXin Li   are. People add system-specific clues to Clang often, but as
155*67e74705SXin Li   things change, it's more likely that it won't find than the
156*67e74705SXin Li   other way around.
157*67e74705SXin Li
158*67e74705SXin Li   So, here, you'll be a lot safer if you specify the include/library
159*67e74705SXin Li   directories manually (via ``-I`` and ``-L``).
160*67e74705SXin Li
161*67e74705SXin LiTarget-Specific Libraries
162*67e74705SXin Li=========================
163*67e74705SXin Li
164*67e74705SXin LiAll libraries that you compile as part of your build will be
165*67e74705SXin Licross-compiled to your target, and your build system will probably
166*67e74705SXin Lifind them in the right place. But all dependencies that are
167*67e74705SXin Linormally checked against (like ``libxml`` or ``libz`` etc) will match
168*67e74705SXin Liagainst the host platform, not the target.
169*67e74705SXin Li
170*67e74705SXin LiSo, if the build system is not aware that you want to cross-compile
171*67e74705SXin Liyour code, it will get every dependency wrong, and your compilation
172*67e74705SXin Liwill fail during build time, not configure time.
173*67e74705SXin Li
174*67e74705SXin LiAlso, finding the libraries for your target are not as easy
175*67e74705SXin Lias for your host machine. There aren't many cross-libraries available
176*67e74705SXin Lias packages to most OS's, so you'll have to either cross-compile them
177*67e74705SXin Lifrom source, or download the package for your target platform,
178*67e74705SXin Liextract the libraries and headers, put them in specific directories
179*67e74705SXin Liand add ``-I`` and ``-L`` pointing to them.
180*67e74705SXin Li
181*67e74705SXin LiAlso, some libraries have different dependencies on different targets,
182*67e74705SXin Liso configuration tools to find dependencies in the host can get the
183*67e74705SXin Lilist wrong for the target platform. This means that the configuration
184*67e74705SXin Liof your build can get things wrong when setting their own library
185*67e74705SXin Lipaths, and you'll have to augment it via additional flags (configure,
186*67e74705SXin LiMake, CMake, etc).
187*67e74705SXin Li
188*67e74705SXin LiMultilibs
189*67e74705SXin Li---------
190*67e74705SXin Li
191*67e74705SXin LiWhen you want to cross-compile to more than one configuration, for
192*67e74705SXin Liexample hard-float-ARM and soft-float-ARM, you'll have to have multiple
193*67e74705SXin Licopies of your libraries and (possibly) headers.
194*67e74705SXin Li
195*67e74705SXin LiSome Linux distributions have support for Multilib, which handle that
196*67e74705SXin Lifor you in an easier way, but if you're not careful and, for instance,
197*67e74705SXin Liforget to specify ``-ccc-gcc-name armv7l-linux-gnueabihf-gcc`` (which
198*67e74705SXin Liuses hard-float), Clang will pick the ``armv7l-linux-gnueabi-ld``
199*67e74705SXin Li(which uses soft-float) and linker errors will happen.
200*67e74705SXin Li
201*67e74705SXin LiThe same is true if you're compiling for different ABIs, like ``gnueabi``
202*67e74705SXin Liand ``androideabi``, and might even link and run, but produce run-time
203*67e74705SXin Lierrors, which are much harder to track down and fix.
204