llvm.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. .. _kbuild_llvm:
  2. ==============================
  3. Building Linux with Clang/LLVM
  4. ==============================
  5. This document covers how to build the Linux kernel with Clang and LLVM
  6. utilities.
  7. About
  8. -----
  9. The Linux kernel has always traditionally been compiled with GNU toolchains
  10. such as GCC and binutils. Ongoing work has allowed for `Clang
  11. <https://clang.llvm.org/>`_ and `LLVM <https://llvm.org/>`_ utilities to be
  12. used as viable substitutes. Distributions such as `Android
  13. <https://www.android.com/>`_, `ChromeOS
  14. <https://www.chromium.org/chromium-os>`_, `OpenMandriva
  15. <https://www.openmandriva.org/>`_, and `Chimera Linux
  16. <https://chimera-linux.org/>`_ use Clang built kernels. Google's and Meta's
  17. datacenter fleets also run kernels built with Clang.
  18. `LLVM is a collection of toolchain components implemented in terms of C++
  19. objects <https://www.aosabook.org/en/llvm.html>`_. Clang is a front-end to LLVM
  20. that supports C and the GNU C extensions required by the kernel, and is
  21. pronounced "klang," not "see-lang."
  22. Building with LLVM
  23. ------------------
  24. Invoke ``make`` via::
  25. make LLVM=1
  26. to compile for the host target. For cross compiling::
  27. make LLVM=1 ARCH=arm64
  28. The LLVM= argument
  29. ------------------
  30. LLVM has substitutes for GNU binutils utilities. They can be enabled
  31. individually. The full list of supported make variables::
  32. make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \
  33. OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \
  34. HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld
  35. ``LLVM=1`` expands to the above.
  36. If your LLVM tools are not available in your PATH, you can supply their
  37. location using the LLVM variable with a trailing slash::
  38. make LLVM=/path/to/llvm/
  39. which will use ``/path/to/llvm/clang``, ``/path/to/llvm/ld.lld``, etc. The
  40. following may also be used::
  41. PATH=/path/to/llvm:$PATH make LLVM=1
  42. If your LLVM tools have a version suffix and you want to test with that
  43. explicit version rather than the unsuffixed executables like ``LLVM=1``, you
  44. can pass the suffix using the ``LLVM`` variable::
  45. make LLVM=-14
  46. which will use ``clang-14``, ``ld.lld-14``, etc.
  47. To support combinations of out of tree paths with version suffixes, we
  48. recommend::
  49. PATH=/path/to/llvm/:$PATH make LLVM=-14
  50. ``LLVM=0`` is not the same as omitting ``LLVM`` altogether, it will behave like
  51. ``LLVM=1``. If you only wish to use certain LLVM utilities, use their
  52. respective make variables.
  53. The same value used for ``LLVM=`` should be set for each invocation of ``make``
  54. if configuring and building via distinct commands. ``LLVM=`` should also be set
  55. as an environment variable when running scripts that will eventually run
  56. ``make``.
  57. Cross Compiling
  58. ---------------
  59. A single Clang compiler binary (and corresponding LLVM utilities) will
  60. typically contain all supported back ends, which can help simplify cross
  61. compiling especially when ``LLVM=1`` is used. If you use only LLVM tools,
  62. ``CROSS_COMPILE`` or target-triple-prefixes become unnecessary. Example::
  63. make LLVM=1 ARCH=arm64
  64. As an example of mixing LLVM and GNU utilities, for a target like ``ARCH=s390``
  65. which does not yet have ``ld.lld`` or ``llvm-objcopy`` support, you could
  66. invoke ``make`` via::
  67. make LLVM=1 ARCH=s390 LD=s390x-linux-gnu-ld.bfd \
  68. OBJCOPY=s390x-linux-gnu-objcopy
  69. This example will invoke ``s390x-linux-gnu-ld.bfd`` as the linker and
  70. ``s390x-linux-gnu-objcopy``, so ensure those are reachable in your ``$PATH``.
  71. ``CROSS_COMPILE`` is not used to prefix the Clang compiler binary (or
  72. corresponding LLVM utilities) as is the case for GNU utilities when ``LLVM=1``
  73. is not set.
  74. The LLVM_IAS= argument
  75. ----------------------
  76. Clang can assemble assembler code. You can pass ``LLVM_IAS=0`` to disable this
  77. behavior and have Clang invoke the corresponding non-integrated assembler
  78. instead. Example::
  79. make LLVM=1 LLVM_IAS=0
  80. ``CROSS_COMPILE`` is necessary when cross compiling and ``LLVM_IAS=0``
  81. is used in order to set ``--prefix=`` for the compiler to find the
  82. corresponding non-integrated assembler (typically, you don't want to use the
  83. system assembler when targeting another architecture). Example::
  84. make LLVM=1 ARCH=arm LLVM_IAS=0 CROSS_COMPILE=arm-linux-gnueabi-
  85. Ccache
  86. ------
  87. ``ccache`` can be used with ``clang`` to improve subsequent builds, (though
  88. KBUILD_BUILD_TIMESTAMP_ should be set to a deterministic value between builds
  89. in order to avoid 100% cache misses, see Reproducible_builds_ for more info)::
  90. KBUILD_BUILD_TIMESTAMP='' make LLVM=1 CC="ccache clang"
  91. .. _KBUILD_BUILD_TIMESTAMP: kbuild.html#kbuild-build-timestamp
  92. .. _Reproducible_builds: reproducible-builds.html#timestamps
  93. Supported Architectures
  94. -----------------------
  95. LLVM does not target all of the architectures that Linux supports and
  96. just because a target is supported in LLVM does not mean that the kernel
  97. will build or work without any issues. Below is a general summary of
  98. architectures that currently work with ``CC=clang`` or ``LLVM=1``. Level
  99. of support corresponds to "S" values in the MAINTAINERS files. If an
  100. architecture is not present, it either means that LLVM does not target
  101. it or there are known issues. Using the latest stable version of LLVM or
  102. even the development tree will generally yield the best results.
  103. An architecture's ``defconfig`` is generally expected to work well,
  104. certain configurations may have problems that have not been uncovered
  105. yet. Bug reports are always welcome at the issue tracker below!
  106. .. list-table::
  107. :widths: 10 10 10
  108. :header-rows: 1
  109. * - Architecture
  110. - Level of support
  111. - ``make`` command
  112. * - arm
  113. - Supported
  114. - ``LLVM=1``
  115. * - arm64
  116. - Supported
  117. - ``LLVM=1``
  118. * - hexagon
  119. - Maintained
  120. - ``LLVM=1``
  121. * - loongarch
  122. - Maintained
  123. - ``LLVM=1``
  124. * - mips
  125. - Maintained
  126. - ``LLVM=1``
  127. * - powerpc
  128. - Maintained
  129. - ``LLVM=1``
  130. * - riscv
  131. - Supported
  132. - ``LLVM=1``
  133. * - s390
  134. - Maintained
  135. - ``LLVM=1`` (LLVM >= 18.1.0), ``CC=clang`` (LLVM < 18.1.0)
  136. * - sparc (sparc64 only)
  137. - Maintained
  138. - ``CC=clang LLVM_IAS=0`` (LLVM >= 20)
  139. * - um (User Mode)
  140. - Maintained
  141. - ``LLVM=1``
  142. * - x86
  143. - Supported
  144. - ``LLVM=1``
  145. Getting Help
  146. ------------
  147. - `Website <https://clangbuiltlinux.github.io/>`_
  148. - `Mailing List <https://lore.kernel.org/llvm/>`_: <llvm@lists.linux.dev>
  149. - `Old Mailing List Archives <https://groups.google.com/g/clang-built-linux>`_
  150. - `Issue Tracker <https://github.com/ClangBuiltLinux/linux/issues>`_
  151. - IRC: #clangbuiltlinux on irc.libera.chat
  152. - `Telegram <https://t.me/ClangBuiltLinux>`_: @ClangBuiltLinux
  153. - `Wiki <https://github.com/ClangBuiltLinux/linux/wiki>`_
  154. - `Beginner Bugs <https://github.com/ClangBuiltLinux/linux/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22>`_
  155. .. _getting_llvm:
  156. Getting LLVM
  157. -------------
  158. We provide prebuilt stable versions of LLVM on `kernel.org
  159. <https://kernel.org/pub/tools/llvm/>`_. These have been optimized with profile
  160. data for building Linux kernels, which should improve kernel build times
  161. relative to other distributions of LLVM.
  162. Below are links that may be useful for building LLVM from source or procuring
  163. it through a distribution's package manager.
  164. - https://releases.llvm.org/download.html
  165. - https://github.com/llvm/llvm-project
  166. - https://llvm.org/docs/GettingStarted.html
  167. - https://llvm.org/docs/CMake.html
  168. - https://apt.llvm.org/
  169. - https://www.archlinux.org/packages/extra/x86_64/llvm/
  170. - https://github.com/ClangBuiltLinux/tc-build
  171. - https://github.com/ClangBuiltLinux/linux/wiki/Building-Clang-from-source
  172. - https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/