HowToCrossCompileLLVM.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ===================================================================
  2. How To Cross-Compile Clang/LLVM using Clang/LLVM
  3. ===================================================================
  4. Introduction
  5. ============
  6. This document contains information about building LLVM and
  7. Clang on host machine, targeting another platform.
  8. For more information on how to use Clang as a cross-compiler,
  9. please check http://clang.llvm.org/docs/CrossCompilation.html.
  10. TODO: Add MIPS and other platforms to this document.
  11. Cross-Compiling from x86_64 to ARM
  12. ==================================
  13. In this use case, we'll be using CMake and Ninja, on a Debian-based Linux
  14. system, cross-compiling from an x86_64 host (most Intel and AMD chips
  15. nowadays) to a hard-float ARM target (most ARM targets nowadays).
  16. The packages you'll need are:
  17. * ``cmake``
  18. * ``ninja-build`` (from backports in Ubuntu)
  19. * ``gcc-4.7-arm-linux-gnueabihf``
  20. * ``gcc-4.7-multilib-arm-linux-gnueabihf``
  21. * ``binutils-arm-linux-gnueabihf``
  22. * ``libgcc1-armhf-cross``
  23. * ``libsfgcc1-armhf-cross``
  24. * ``libstdc++6-armhf-cross``
  25. * ``libstdc++6-4.7-dev-armhf-cross``
  26. Configuring CMake
  27. -----------------
  28. For more information on how to configure CMake for LLVM/Clang,
  29. see :doc:`CMake`.
  30. The CMake options you need to add are:
  31. * ``-DCMAKE_CROSSCOMPILING=True``
  32. * ``-DCMAKE_INSTALL_PREFIX=<install-dir>``
  33. * ``-DLLVM_TABLEGEN=<path-to-host-bin>/llvm-tblgen``
  34. * ``-DCLANG_TABLEGEN=<path-to-host-bin>/clang-tblgen``
  35. * ``-DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf``
  36. * ``-DLLVM_TARGET_ARCH=ARM``
  37. * ``-DLLVM_TARGETS_TO_BUILD=ARM``
  38. If you're compiling with GCC, you can use architecture options for your target,
  39. and the compiler driver will detect everything that it needs:
  40. * ``-DCMAKE_CXX_FLAGS='-march=armv7-a -mcpu=cortex-a9 -mfloat-abi=hard'``
  41. However, if you're using Clang, the driver might not be up-to-date with your
  42. specific Linux distribution, version or GCC layout, so you'll need to fudge.
  43. In addition to the ones above, you'll also need:
  44. * ``'-target arm-linux-gnueabihf'`` or whatever is the triple of your cross GCC.
  45. * ``'--sysroot=/usr/arm-linux-gnueabihf'``, ``'--sysroot=/opt/gcc/arm-linux-gnueabihf'``
  46. or whatever is the location of your GCC's sysroot (where /lib, /bin etc are).
  47. * Appropriate use of ``-I`` and ``-L``, depending on how the cross GCC is installed,
  48. and where are the libraries and headers.
  49. The TableGen options are required to compile it with the host compiler,
  50. so you'll need to compile LLVM (or at least ``llvm-tblgen``) to your host
  51. platform before you start. The CXX flags define the target, cpu (which in this case
  52. defaults to ``fpu=VFP3`` with NEON), and forcing the hard-float ABI. If you're
  53. using Clang as a cross-compiler, you will *also* have to set ``--sysroot``
  54. to make sure it picks the correct linker.
  55. When using Clang, it's important that you choose the triple to be *identical*
  56. to the GCC triple and the sysroot. This will make it easier for Clang to
  57. find the correct tools and include headers. But that won't mean all headers and
  58. libraries will be found. You'll still need to use ``-I`` and ``-L`` to locate
  59. those extra ones, depending on your distribution.
  60. Most of the time, what you want is to have a native compiler to the
  61. platform itself, but not others. So there's rarely a point in compiling
  62. all back-ends. For that reason, you should also set the
  63. ``TARGETS_TO_BUILD`` to only build the back-end you're targeting to.
  64. You must set the ``CMAKE_INSTALL_PREFIX``, otherwise a ``ninja install``
  65. will copy ARM binaries to your root filesystem, which is not what you
  66. want.
  67. Hacks
  68. -----
  69. There are some bugs in current LLVM, which require some fiddling before
  70. running CMake:
  71. #. If you're using Clang as the cross-compiler, there is a problem in
  72. the LLVM ARM back-end that is producing absolute relocations on
  73. position-independent code (``R_ARM_THM_MOVW_ABS_NC``), so for now, you
  74. should disable PIC:
  75. .. code-block:: bash
  76. -DLLVM_ENABLE_PIC=False
  77. This is not a problem, since Clang/LLVM libraries are statically
  78. linked anyway, it shouldn't affect much.
  79. #. The ARM libraries won't be installed in your system.
  80. But the CMake prepare step, which checks for
  81. dependencies, will check the *host* libraries, not the *target*
  82. ones. Below there's a list of some dependencies, but your project could
  83. have more, or this document could be outdated. You'll see the errors
  84. while linking as an indication of that.
  85. Debian based distros have a way to add ``multiarch``, which adds
  86. a new architecture and allows you to install packages for those
  87. systems. See https://wiki.debian.org/Multiarch/HOWTO for more info.
  88. But not all distros will have that, and possibly not an easy way to
  89. install them in any anyway, so you'll have to build/download
  90. them separately.
  91. A quick way of getting the libraries is to download them from
  92. a distribution repository, like Debian (http://packages.debian.org/jessie/),
  93. and download the missing libraries. Note that the ``libXXX``
  94. will have the shared objects (``.so``) and the ``libXXX-dev`` will
  95. give you the headers and the static (``.a``) library. Just in
  96. case, download both.
  97. The ones you need for ARM are: ``libtinfo``, ``zlib1g``,
  98. ``libxml2`` and ``liblzma``. In the Debian repository you'll
  99. find downloads for all architectures.
  100. After you download and unpack all ``.deb`` packages, copy all
  101. ``.so`` and ``.a`` to a directory, make the appropriate
  102. symbolic links (if necessary), and add the relevant ``-L``
  103. and ``-I`` paths to ``-DCMAKE_CXX_FLAGS`` above.
  104. Running CMake and Building
  105. --------------------------
  106. Finally, if you're using your platform compiler, run:
  107. .. code-block:: bash
  108. $ cmake -G Ninja <source-dir> <options above>
  109. If you're using Clang as the cross-compiler, run:
  110. .. code-block:: bash
  111. $ CC='clang' CXX='clang++' cmake -G Ninja <source-dir> <options above>
  112. If you have ``clang``/``clang++`` on the path, it should just work, and special
  113. Ninja files will be created in the build directory. I strongly suggest
  114. you to run ``cmake`` on a separate build directory, *not* inside the
  115. source tree.
  116. To build, simply type:
  117. .. code-block:: bash
  118. $ ninja
  119. It should automatically find out how many cores you have, what are
  120. the rules that needs building and will build the whole thing.
  121. You can't run ``ninja check-all`` on this tree because the created
  122. binaries are targeted to ARM, not x86_64.
  123. Installing and Using
  124. --------------------
  125. After the LLVM/Clang has built successfully, you should install it
  126. via:
  127. .. code-block:: bash
  128. $ ninja install
  129. which will create a sysroot on the install-dir. You can then tar
  130. that directory into a binary with the full triple name (for easy
  131. identification), like:
  132. .. code-block:: bash
  133. $ ln -sf <install-dir> arm-linux-gnueabihf-clang
  134. $ tar zchf arm-linux-gnueabihf-clang.tar.gz arm-linux-gnueabihf-clang
  135. If you copy that tarball to your target board, you'll be able to use
  136. it for running the test-suite, for example. Follow the guidelines at
  137. http://llvm.org/docs/lnt/quickstart.html, unpack the tarball in the
  138. test directory, and use options:
  139. .. code-block:: bash
  140. $ ./sandbox/bin/python sandbox/bin/lnt runtest nt \
  141. --sandbox sandbox \
  142. --test-suite `pwd`/test-suite \
  143. --cc `pwd`/arm-linux-gnueabihf-clang/bin/clang \
  144. --cxx `pwd`/arm-linux-gnueabihf-clang/bin/clang++
  145. Remember to add the ``-jN`` options to ``lnt`` to the number of CPUs
  146. on your board. Also, the path to your clang has to be absolute, so
  147. you'll need the `pwd` trick above.