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