llvm.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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>`_, and `OpenMandriva
  15. <https://www.openmandriva.org/>`_ use Clang built kernels. `LLVM is a
  16. collection of toolchain components implemented in terms of C++ objects
  17. <https://www.aosabook.org/en/llvm.html>`_. Clang is a front-end to LLVM that
  18. supports C and the GNU C extensions required by the kernel, and is pronounced
  19. "klang," not "see-lang."
  20. Clang
  21. -----
  22. The compiler used can be swapped out via ``CC=`` command line argument to ``make``.
  23. ``CC=`` should be set when selecting a config and during a build. ::
  24. make CC=clang defconfig
  25. make CC=clang
  26. Cross Compiling
  27. ---------------
  28. A single Clang compiler binary will typically contain all supported backends,
  29. which can help simplify cross compiling. ::
  30. make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-gnu-
  31. ``CROSS_COMPILE`` is not used to prefix the Clang compiler binary, instead
  32. ``CROSS_COMPILE`` is used to set a command line flag: ``--target=<triple>``. For
  33. example: ::
  34. clang --target=aarch64-linux-gnu foo.c
  35. LLVM Utilities
  36. --------------
  37. LLVM has substitutes for GNU binutils utilities. They can be enabled individually.
  38. The full list of supported make variables::
  39. make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \
  40. OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \
  41. HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld
  42. To simplify the above command, Kbuild supports the ``LLVM`` variable::
  43. make LLVM=1
  44. If your LLVM tools are not available in your PATH, you can supply their
  45. location using the LLVM variable with a trailing slash::
  46. make LLVM=/path/to/llvm/
  47. which will use ``/path/to/llvm/clang``, ``/path/to/llvm/ld.lld``, etc.
  48. If your LLVM tools have a version suffix and you want to test with that
  49. explicit version rather than the unsuffixed executables like ``LLVM=1``, you
  50. can pass the suffix using the ``LLVM`` variable::
  51. make LLVM=-14
  52. which will use ``clang-14``, ``ld.lld-14``, etc.
  53. ``LLVM=0`` is not the same as omitting ``LLVM`` altogether, it will behave like
  54. ``LLVM=1``. If you only wish to use certain LLVM utilities, use their respective
  55. make variables.
  56. The integrated assembler is enabled by default. You can pass ``LLVM_IAS=0`` to
  57. disable it.
  58. Omitting CROSS_COMPILE
  59. ----------------------
  60. As explained above, ``CROSS_COMPILE`` is used to set ``--target=<triple>``.
  61. If ``CROSS_COMPILE`` is not specified, the ``--target=<triple>`` is inferred
  62. from ``ARCH``.
  63. That means if you use only LLVM tools, ``CROSS_COMPILE`` becomes unnecessary.
  64. For example, to cross-compile the arm64 kernel::
  65. make ARCH=arm64 LLVM=1
  66. If ``LLVM_IAS=0`` is specified, ``CROSS_COMPILE`` is also used to derive
  67. ``--prefix=<path>`` to search for the GNU assembler and linker. ::
  68. make ARCH=arm64 LLVM=1 LLVM_IAS=0 CROSS_COMPILE=aarch64-linux-gnu-
  69. Supported Architectures
  70. -----------------------
  71. LLVM does not target all of the architectures that Linux supports and
  72. just because a target is supported in LLVM does not mean that the kernel
  73. will build or work without any issues. Below is a general summary of
  74. architectures that currently work with ``CC=clang`` or ``LLVM=1``. Level
  75. of support corresponds to "S" values in the MAINTAINERS files. If an
  76. architecture is not present, it either means that LLVM does not target
  77. it or there are known issues. Using the latest stable version of LLVM or
  78. even the development tree will generally yield the best results.
  79. An architecture's ``defconfig`` is generally expected to work well,
  80. certain configurations may have problems that have not been uncovered
  81. yet. Bug reports are always welcome at the issue tracker below!
  82. .. list-table::
  83. :widths: 10 10 10
  84. :header-rows: 1
  85. * - Architecture
  86. - Level of support
  87. - ``make`` command
  88. * - arm
  89. - Supported
  90. - ``LLVM=1``
  91. * - arm64
  92. - Supported
  93. - ``LLVM=1``
  94. * - hexagon
  95. - Maintained
  96. - ``LLVM=1``
  97. * - mips
  98. - Maintained
  99. - ``LLVM=1``
  100. * - powerpc
  101. - Maintained
  102. - ``CC=clang``
  103. * - riscv
  104. - Maintained
  105. - ``LLVM=1``
  106. * - s390
  107. - Maintained
  108. - ``CC=clang``
  109. * - um (User Mode)
  110. - Maintained
  111. - ``LLVM=1``
  112. * - x86
  113. - Supported
  114. - ``LLVM=1``
  115. Getting Help
  116. ------------
  117. - `Website <https://clangbuiltlinux.github.io/>`_
  118. - `Mailing List <https://lore.kernel.org/llvm/>`_: <[email protected]>
  119. - `Old Mailing List Archives <https://groups.google.com/g/clang-built-linux>`_
  120. - `Issue Tracker <https://github.com/ClangBuiltLinux/linux/issues>`_
  121. - IRC: #clangbuiltlinux on irc.libera.chat
  122. - `Telegram <https://t.me/ClangBuiltLinux>`_: @ClangBuiltLinux
  123. - `Wiki <https://github.com/ClangBuiltLinux/linux/wiki>`_
  124. - `Beginner Bugs <https://github.com/ClangBuiltLinux/linux/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22>`_
  125. .. _getting_llvm:
  126. Getting LLVM
  127. -------------
  128. - https://releases.llvm.org/download.html
  129. - https://github.com/llvm/llvm-project
  130. - https://llvm.org/docs/GettingStarted.html
  131. - https://llvm.org/docs/CMake.html
  132. - https://apt.llvm.org/
  133. - https://www.archlinux.org/packages/extra/x86_64/llvm/
  134. - https://github.com/ClangBuiltLinux/tc-build
  135. - https://github.com/ClangBuiltLinux/linux/wiki/Building-Clang-from-source
  136. - https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/