Skip to content

Commit d1328e1

Browse files
committed
[docs] Improve HowToCrossCompilerBuiltinsOnArm
Some recent experience on llvm-dev pointed out some errors in the document: - Assumption of ninja - Use of --march rather than -march - Problems with host include files when a multiarch setup was used - Insufficient target information passed to assembler - Instructions on using the cmake cache file BaremetalARM.cmake were incomplete There was also insufficient guidance on what to do when various stages failed due to misconfiguration or missing steps. Summary of changes: - Fixed problems above - Added a troubleshooting section with common errors. - Cleared up one "at time of writing" that is no longer a problem. Differential Revision: https://reviews.llvm.org/D55709 llvm-svn: 349477
1 parent 1ec4911 commit d1328e1

File tree

1 file changed

+140
-51
lines changed

1 file changed

+140
-51
lines changed

llvm/docs/HowToCrossCompileBuiltinsOnArm.rst

Lines changed: 140 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,46 @@ may need to adapt the instructions here to fit your own local situation.
1919
Prerequisites
2020
=============
2121

22-
In this use case we'll be using CMake on a Debian-based Linux system,
22+
In this use case we'll be using cmake on a Debian-based Linux system,
2323
cross-compiling from an x86_64 host to a hard-float Armv7-A target. We'll be
2424
using as many of the LLVM tools as we can, but it is possible to use GNU
2525
equivalents.
2626

2727
* ``A build of LLVM/clang for the llvm-tools and llvm-config``
28+
* ``A clang executable with support for the ARM target``
29+
* ``compiler-rt sources``
2830
* ``The qemu-arm user mode emulator``
2931
* ``An arm-linux-gnueabihf sysroot``
3032

33+
In this example we will be using ninja.
34+
3135
See https://compiler-rt.llvm.org/ for more information about the dependencies
3236
on clang and LLVM.
3337

38+
See https://llvm.org/docs/GettingStarted.html for information about obtaining
39+
the source for LLVM and compiler-rt. Note that the getting started guide
40+
places compiler-rt in the projects subdirectory, but this is not essential and
41+
if you are using the BaremetalARM.cmake cache for v6-M, v7-M and v7-EM then
42+
compiler-rt must be placed in the runtimes directory.
43+
3444
``qemu-arm`` should be available as a package for your Linux distribution.
3545

3646
The most complicated of the prequisites to satisfy is the arm-linux-gnueabihf
37-
sysroot. The :doc:`HowToCrossCompileLLVM` has information about how to use the
38-
Linux distributions multiarch support to fulfill the dependencies for building
39-
LLVM. Alternatively, as building and testing just the compiler-rt builtins
40-
requires fewer dependencies than LLVM, it is possible to use the Linaro
41-
arm-linux-gnueabihf gcc installation as our sysroot.
47+
sysroot. In theory it is possible to use the Linux distributions multiarch
48+
support to fulfill the dependencies for building but unfortunately due to
49+
/usr/local/include being added some host includes are selected. The easiest way
50+
to supply a sysroot is to download the arm-linux-gnueabihf toolchain. This can
51+
be found at:
52+
* https://developer.arm.com/open-source/gnu-toolchain/gnu-a/downloads for gcc 8 and above
53+
* https://releases.linaro.org/components/toolchain/binaries/ for gcc 4.9 to 7.3
4254

4355
Building compiler-rt builtins for Arm
4456
=====================================
4557
We will be doing a standalone build of compiler-rt using the following cmake
4658
options.
4759

48-
* ``path/to/llvm/projects/compiler-rt``
60+
* ``path/to/compiler-rt``
61+
* ``-G Ninja``
4962
* ``-DCOMPILER_RT_BUILD_BUILTINS=ON``
5063
* ``-DCOMPILER_RT_BUILD_SANITIZERS=OFF``
5164
* ``-DCOMPILER_RT_BUILD_XRAY=OFF``
@@ -57,22 +70,38 @@ options.
5770
* ``-DCMAKE_RANLIB=/path/to/llvm-ranlib``
5871
* ``-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld"``
5972
* ``-DCMAKE_C_COMPILER_TARGET="arm-linux-gnueabihf"``
73+
* ``-DCMAKE_ASM_COMPILER_TARGET="arm-linux-gnueabihf"``
6074
* ``-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON``
6175
* ``-DLLVM_CONFIG_PATH=/path/to/llvm-config``
6276
* ``-DCMAKE_C_FLAGS="build-c-flags"``
77+
* ``-DCMAKE_ASM_FLAGS="build-c-flags"``
6378

64-
The build-c-flags need to be sufficient to pass the C-make compiler check and
65-
to compile compiler-rt. When using a GCC 7 Linaro arm-linux-gnueabihf
66-
installation the following flags are needed:
79+
The ``build-c-flags`` need to be sufficient to pass the C-make compiler check,
80+
compile compiler-rt, and if you are running the tests, compile and link the
81+
tests. When cross-compiling with clang we will need to pass sufficient
82+
information to generate code for the Arm architecture we are targeting. We will
83+
need to select the Arm target, select the Armv7-A architecture and choose
84+
between using Arm or Thumb.
85+
instructions. For example:
6786

6887
* ``--target=arm-linux-gnueabihf``
69-
* ``--march=armv7a``
88+
* ``-march=armv7a``
89+
* ``-mthumb``
90+
91+
When using a GCC arm-linux-gnueabihf toolchain the following flags are
92+
needed to pick up the includes and libraries:
93+
7094
* ``--gcc-toolchain=/path/to/dir/toolchain``
7195
* ``--sysroot=/path/to/toolchain/arm-linux-gnueabihf/libc``
7296

73-
Depending on how your sysroot is laid out, you may not need ``--gcc-toolchain``.
74-
For example if you have added armhf as an architecture using your Linux
75-
distributions multiarch support then you should be able to use ``--sysroot=/``.
97+
In this example we will be adding all of the command line options to both
98+
``CMAKE_C_FLAGS`` and ``CMAKE_ASM_FLAGS``. There are cmake flags to pass some of
99+
these options individually which can be used to simplify the ``build-c-flags``:
100+
101+
* ``-DCMAKE_C_COMPILER_TARGET="arm-linux-gnueabihf"``
102+
* ``-DCMAKE_ASM_COMPILER_TARGET="arm-linux-gnueabihf"``
103+
* ``-DCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN=/path/to/dir/toolchain``
104+
* ``-DCMAKE_SYSROOT=/path/to/dir/toolchain/arm-linux-gnueabihf/libc``
76105

77106
Once cmake has completed the builtins can be built with ``ninja builtins``
78107

@@ -90,12 +119,72 @@ cmake that we wish to run the tests on ``qemu-arm``.
90119
The ``/path/to/armhf/sysroot`` should be the same as the one passed to
91120
``--sysroot`` in the "build-c-flags".
92121

93-
The "test-c-flags" can be the same as the "build-c-flags", with the addition
94-
of ``"-fuse-ld=lld`` if you wish to use lld to link the tests.
122+
The "test-c-flags" need to include the target, architecture, gcc-toolchain,
123+
sysroot and arm/thumb state. The additional cmake defines such as
124+
``CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN`` do not apply when building the tests. If
125+
you have put all of these in "build-c-flags" then these can be repeated. If you
126+
wish to use lld to link the tests then add ``"-fuse-ld=lld``.
95127

96128
Once cmake has completed the tests can be built and run using
97129
``ninja check-builtins``
98130

131+
Troubleshooting
132+
===============
133+
134+
The cmake try compile stage fails
135+
---------------------------------
136+
At an early stage cmake will attempt to compile and link a simple C program to
137+
test if the toolchain is working.
138+
139+
This stage can often fail at link time if the ``--sysroot`` and
140+
``--gcc-toolchain`` options are not passed to the compiler. Check the
141+
``CMAKE_C_FLAGS`` and ``CMAKE_C_COMPILER_TARGET`` flags.
142+
143+
It can be useful to build a simple example outside of cmake with your toolchain
144+
to make sure it is working. For example: ``clang --target=arm-linux-gnueabi -march=armv7a --gcc-toolchain=/path/to/gcc-toolchain --sysroot=/path/to/gcc-toolchain/arm-linux-gnueabihf/libc helloworld.c``
145+
146+
Clang uses the host header files
147+
--------------------------------
148+
On debian based systems it is possible to install multiarch support for
149+
arm-linux-gnueabi and arm-linux-gnueabihf. In many cases clang can successfully
150+
use this multiarch support when -gcc-toolchain and --sysroot are not supplied.
151+
Unfortunately clang adds ``/usr/local/include`` before
152+
``/usr/include/arm-linux-gnueabihf`` leading to errors when compiling the hosts
153+
header files.
154+
155+
The multiarch support is not sufficient to build the builtins you will need to
156+
use a separate arm-linux-gnueabihf toolchain.
157+
158+
No target passed to clang
159+
-------------------------
160+
If clang is not given a target it will typically use the host target, this will
161+
not understand the Arm assembly language files resulting in error messages such
162+
as ``error: unknown directive .syntax unified``.
163+
164+
You can check the clang invocation in the error message to see if there is no
165+
``--target`` or if it is set incorrectly. The cause is usually
166+
``CMAKE_ASM_FLAGS`` not containing ``--target`` or ``CMAKE_ASM_COMPILER_TARGET`` not being present.
167+
168+
Arm architecture not given
169+
--------------------------
170+
The ``--target=arm-linux-gnueabihf`` will default to arm architecture v4t which
171+
cannot assemble the barrier instructions used in the synch_and_fetch source
172+
files.
173+
174+
The cause is usually a missing ``-march=armv7a`` from the ``CMAKE_ASM_FLAGS``.
175+
176+
Compiler-rt builds but the tests fail to build
177+
----------------------------------------------
178+
The flags used to build the tests are not the same as those used to build the
179+
builtins. The c flags are provided by ``COMPILER_RT_TEST_COMPILE_CFLAGS`` and
180+
the ``CMAKE_C_COMPILER_TARGET``, ``CMAKE_ASM_COMPILER_TARGET``,
181+
``CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN`` and ``CMAKE_SYSROOT`` flags are not
182+
applied.
183+
184+
Make sure that ``COMPILER_RT_TEST_COMPILE_CFLAGS`` contains all the necessary
185+
information.
186+
187+
99188
Modifications for other Targets
100189
===============================
101190

@@ -112,6 +201,8 @@ may need extra c-flags such as ``-mfloat-abi=softfp`` for use of floating-point
112201
instructions, and ``-mfloat-abi=soft -mfpu=none`` for software floating-point
113202
emulation.
114203

204+
You will need to use an arm-linux-gnueabi GNU toolchain for soft-float.
205+
115206
AArch64 Target
116207
--------------
117208
The instructions for Arm can be used for AArch64 by substituting AArch64
@@ -125,39 +216,24 @@ The CMAKE_C_FLAGS and COMPILER_RT_TEST_COMPILER_CFLAGS may also need:
125216

126217
Armv6-m, Armv7-m and Armv7E-M targets
127218
-------------------------------------
128-
If you wish to build, but not test compiler-rt for Armv6-M, Armv7-M or Armv7E-M
129-
then the easiest way is to use the BaremetalARM.cmake recipe in
130-
clang/cmake/caches.
131-
132-
You will need a bare metal sysroot such as that provided by the GNU ARM
133-
Embedded toolchain.
134-
135-
The libraries can be built with the cmake options:
136-
137-
* ``-DBAREMETAL_ARMV6M_SYSROOT=/path/to/bare/metal/sysroot``
138-
* ``-DBAREMETAL_ARMV7M_SYSROOT=/path/to/bare/metal/sysroot``
139-
* ``-DBAREMETAL_ARMV7EM_SYSROOT=/path/to/bare/metal/sysroot``
140-
* ``-C /path/to/llvm/source/tools/clang/cmake/caches/BaremetalARM.cmake``
141-
142-
**Note** that for the recipe to work the compiler-rt source must be checked out
143-
into the directory llvm/runtimes and not llvm/projects.
144-
145219
To build and test the libraries using a similar method to Armv7-A is possible
146220
but more difficult. The main problems are:
147221

148222
* There isn't a ``qemu-arm`` user-mode emulator for bare-metal systems. The ``qemu-system-arm`` can be used but this is significantly more difficult to setup.
149-
* The target to compile compiler-rt have the suffix -none-eabi. This uses the BareMetal driver in clang and by default won't find the libraries needed to pass the cmake compiler check.
223+
* The targets to compile compiler-rt have the suffix -none-eabi. This uses the BareMetal driver in clang and by default won't find the libraries needed to pass the cmake compiler check.
150224

151225
As the Armv6-M, Armv7-M and Armv7E-M builds of compiler-rt only use instructions
152226
that are supported on Armv7-A we can still get most of the value of running the
153227
tests using the same ``qemu-arm`` that we used for Armv7-A by building and
154228
running the test cases for Armv7-A but using the builtins compiled for
155-
Armv6-M, Armv7-M or Armv7E-M. This will not catch instructions that are
156-
supported on Armv7-A but not Armv6-M, Armv7-M and Armv7E-M.
157-
158-
To get the cmake compile test to pass the libraries needed to successfully link
159-
the test application will need to be manually added to ``CMAKE_CFLAGS``.
160-
Alternatively if you are using version 3.6 or above of cmake you can use
229+
Armv6-M, Armv7-M or Armv7E-M. This will test that the builtins can be linked
230+
into a binary and execute the tests correctly but it will not catch if the
231+
builtins use instructions that are supported on Armv7-A but not Armv6-M,
232+
Armv7-M and Armv7E-M.
233+
234+
To get the cmake compile test to pass you will need to pass the libraries
235+
needed to successfully link the cmake test via ``CMAKE_CFLAGS``. It is
236+
strongly recommended that you use version 3.6 or above of cmake so you can use
161237
``CMAKE_TRY_COMPILE_TARGET=STATIC_LIBRARY`` to skip the link step.
162238

163239
* ``-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY``
@@ -169,14 +245,15 @@ Alternatively if you are using version 3.6 or above of cmake you can use
169245
* ``-DCOMPILER_RT_BUILD_PROFILE=OFF``
170246
* ``-DCMAKE_C_COMPILER=${host_install_dir}/bin/clang``
171247
* ``-DCMAKE_C_COMPILER_TARGET="your *-none-eabi target"``
248+
* ``-DCMAKE_ASM_COMPILER_TARGET="your *-none-eabi target"``
172249
* ``-DCMAKE_AR=/path/to/llvm-ar``
173250
* ``-DCMAKE_NM=/path/to/llvm-nm``
174251
* ``-DCMAKE_RANLIB=/path/to/llvm-ranlib``
175252
* ``-DCOMPILER_RT_BAREMETAL_BUILD=ON``
176253
* ``-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON``
177254
* ``-DLLVM_CONFIG_PATH=/path/to/llvm-config``
178255
* ``-DCMAKE_C_FLAGS="build-c-flags"``
179-
* ``-DCMAKE_ASM_FLAGS="${arm_cflags}"``
256+
* ``-DCMAKE_ASM_FLAGS="build-c-flags"``
180257
* ``-DCOMPILER_RT_EMULATOR="qemu-arm -L /path/to/armv7-A/sysroot"``
181258
* ``-DCOMPILER_RT_INCLUDE_TESTS=ON``
182259
* ``-DCOMPILER_RT_TEST_COMPILER="/path/to/clang"``
@@ -186,16 +263,28 @@ The Armv6-M builtins will use the soft-float ABI. When compiling the tests for
186263
Armv7-A we must include ``"-mthumb -mfloat-abi=soft -mfpu=none"`` in the
187264
test-c-flags. We must use an Armv7-A soft-float abi sysroot for ``qemu-arm``.
188265

189-
Unfortunately at time of writing the Armv7-M and Armv7E-M builds of
190-
compiler-rt will always include assembler files including floating point
191-
instructions. This means that building for a cpu without a floating point unit
192-
requires something like removing the arm_Thumb1_VFPv2_SOURCES from the
193-
arm_Thumb1_SOURCES in builtins/CMakeLists.txt. The float-abi of the compiler-rt
194-
library must be matched by the float abi of the Armv7-A sysroot used by
195-
qemu-arm.
196-
197266
Depending on the linker used for the test cases you may encounter BuildAttribute
198267
mismatches between the M-profile objects from compiler-rt and the A-profile
199-
objects from the test. The lld linker does not check the BuildAttributes so it
200-
can be used to link the tests by adding -fuse-ld=lld to the
268+
objects from the test. The lld linker does not check the profile
269+
BuildAttribute so it can be used to link the tests by adding -fuse-ld=lld to the
201270
``COMPILER_RT_TEST_COMPILER_CFLAGS``.
271+
272+
Alternative using a cmake cache
273+
-------------------------------
274+
If you wish to build, but not test compiler-rt for Armv6-M, Armv7-M or Armv7E-M
275+
the easiest way is to use the BaremetalARM.cmake recipe in clang/cmake/caches.
276+
277+
You will need a bare metal sysroot such as that provided by the GNU ARM
278+
Embedded toolchain.
279+
280+
The libraries can be built with the cmake options:
281+
282+
* ``-DBAREMETAL_ARMV6M_SYSROOT=/path/to/bare/metal/toolchain/arm-none-eabi``
283+
* ``-DBAREMETAL_ARMV7M_SYSROOT=/path/to/bare/metal/toolchain/arm-none-eabi``
284+
* ``-DBAREMETAL_ARMV7EM_SYSROOT=/path/to/bare/metal/toolchain/arm-none-eabi``
285+
* ``-C /path/to/llvm/source/tools/clang/cmake/caches/BaremetalARM.cmake``
286+
* ``/path/to/llvm``
287+
288+
**Note** that for the recipe to work the compiler-rt source must be checked out
289+
into the directory llvm/runtimes. You will also need clang and lld checked out.
290+

0 commit comments

Comments
 (0)