^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) .. _kbuild_llvm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) ==============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) Building Linux with Clang/LLVM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) ==============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) This document covers how to build the Linux kernel with Clang and LLVM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) utilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) About
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) -----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) The Linux kernel has always traditionally been compiled with GNU toolchains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) such as GCC and binutils. Ongoing work has allowed for `Clang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) <https://clang.llvm.org/>`_ and `LLVM <https://llvm.org/>`_ utilities to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) used as viable substitutes. Distributions such as `Android
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) <https://www.android.com/>`_, `ChromeOS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) <https://www.chromium.org/chromium-os>`_, and `OpenMandriva
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) <https://www.openmandriva.org/>`_ use Clang built kernels. `LLVM is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) collection of toolchain components implemented in terms of C++ objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) <https://www.aosabook.org/en/llvm.html>`_. Clang is a front-end to LLVM that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) supports C and the GNU C extensions required by the kernel, and is pronounced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) "klang," not "see-lang."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) Clang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) -----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) The compiler used can be swapped out via ``CC=`` command line argument to ``make``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ``CC=`` should be set when selecting a config and during a build. ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) make CC=clang defconfig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) make CC=clang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) Cross Compiling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ---------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) A single Clang compiler binary will typically contain all supported backends,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) which can help simplify cross compiling. ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make CC=clang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ``CROSS_COMPILE`` is not used to prefix the Clang compiler binary, instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ``CROSS_COMPILE`` is used to set a command line flag: ``--target=<triple>``. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) example: ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) clang --target=aarch64-linux-gnu foo.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) LLVM Utilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) --------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) LLVM has substitutes for GNU binutils utilities. Kbuild supports ``LLVM=1``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) to enable them. ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) make LLVM=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) They can be enabled individually. The full list of the parameters: ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) Currently, the integrated assembler is disabled by default. You can pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ``LLVM_IAS=1`` to enable it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) Getting Help
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) - `Website <https://clangbuiltlinux.github.io/>`_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) - `Mailing List <https://groups.google.com/forum/#!forum/clang-built-linux>`_: <clang-built-linux@googlegroups.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) - `Issue Tracker <https://github.com/ClangBuiltLinux/linux/issues>`_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) - IRC: #clangbuiltlinux on chat.freenode.net
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) - `Telegram <https://t.me/ClangBuiltLinux>`_: @ClangBuiltLinux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) - `Wiki <https://github.com/ClangBuiltLinux/linux/wiki>`_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) - `Beginner Bugs <https://github.com/ClangBuiltLinux/linux/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22>`_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .. _getting_llvm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) Getting LLVM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) -------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) - https://releases.llvm.org/download.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) - https://github.com/llvm/llvm-project
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) - https://llvm.org/docs/GettingStarted.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) - https://llvm.org/docs/CMake.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) - https://apt.llvm.org/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) - https://www.archlinux.org/packages/extra/x86_64/llvm/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) - https://github.com/ClangBuiltLinux/tc-build
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) - https://github.com/ClangBuiltLinux/linux/wiki/Building-Clang-from-source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) - https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/