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