Building Linux with Clang/LLVM¶
This document covers how to build the Linux kernel with Clang and LLVMutilities.
About¶
The Linux kernel has always traditionally been compiled with GNU toolchainssuch as GCC and binutils. Ongoing work has allowed forClang andLLVM utilities to beused as viable substitutes. Distributions such asAndroid,ChromeOS,OpenMandriva, andChimera Linux use Clang built kernels. Google’s and Meta’sdatacenter fleets also run kernels built with Clang.
LLVM is a collection of toolchain components implemented in terms of C++objects. Clang is a front-end to LLVMthat supports C and the GNU C extensions required by the kernel, and ispronounced “klang,” not “see-lang.”
Building with LLVM¶
Invokemake via:
make LLVM=1
to compile for the host target. For cross compiling:
make LLVM=1 ARCH=arm64
The LLVM= argument¶
LLVM has substitutes for GNU binutils utilities. They can be enabledindividually. The full list of supported make variables:
make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \ OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \ HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld
LLVM=1 expands to the above.
If your LLVM tools are not available in your PATH, you can supply theirlocation using the LLVM variable with a trailing slash:
make LLVM=/path/to/llvm/
which will use/path/to/llvm/clang,/path/to/llvm/ld.lld, etc. Thefollowing may also be used:
PATH=/path/to/llvm:$PATH make LLVM=1
If your LLVM tools have a version suffix and you want to test with thatexplicit version rather than the unsuffixed executables likeLLVM=1, youcan pass the suffix using theLLVM variable:
make LLVM=-14
which will useclang-14,ld.lld-14, etc.
To support combinations of out of tree paths with version suffixes, werecommend:
PATH=/path/to/llvm/:$PATH make LLVM=-14
LLVM=0 is not the same as omittingLLVM altogether, it will behave likeLLVM=1. If you only wish to use certain LLVM utilities, use theirrespective make variables.
The same value used forLLVM= should be set for each invocation ofmakeif configuring and building via distinct commands.LLVM= should also be setas an environment variable when running scripts that will eventually runmake.
Cross Compiling¶
A single Clang compiler binary (and corresponding LLVM utilities) willtypically contain all supported back ends, which can help simplify crosscompiling especially whenLLVM=1 is used. If you use only LLVM tools,CROSS_COMPILE or target-triple-prefixes become unnecessary. Example:
make LLVM=1 ARCH=arm64
As an example of mixing LLVM and GNU utilities, for a target likeARCH=s390which does not yet haveld.lld orllvm-objcopy support, you couldinvokemake via:
make LLVM=1 ARCH=s390 LD=s390x-linux-gnu-ld.bfd \ OBJCOPY=s390x-linux-gnu-objcopy
This example will invokes390x-linux-gnu-ld.bfd as the linker ands390x-linux-gnu-objcopy, so ensure those are reachable in your$PATH.
CROSS_COMPILE is not used to prefix the Clang compiler binary (orcorresponding LLVM utilities) as is the case for GNU utilities whenLLVM=1is not set.
The LLVM_IAS= argument¶
Clang can assemble assembler code. You can passLLVM_IAS=0 to disable thisbehavior and have Clang invoke the corresponding non-integrated assemblerinstead. Example:
make LLVM=1 LLVM_IAS=0
CROSS_COMPILE is necessary when cross compiling andLLVM_IAS=0is used in order to set--prefix= for the compiler to find thecorresponding non-integrated assembler (typically, you don’t want to use thesystem assembler when targeting another architecture). Example:
make LLVM=1 ARCH=arm LLVM_IAS=0 CROSS_COMPILE=arm-linux-gnueabi-
Ccache¶
ccache can be used withclang to improve subsequent builds, (thoughKBUILD_BUILD_TIMESTAMP should be set to a deterministic value between buildsin order to avoid 100% cache misses, seeReproducible_builds for more info):
KBUILD_BUILD_TIMESTAMP='' make LLVM=1 CC="ccache clang"
Supported Architectures¶
LLVM does not target all of the architectures that Linux supports andjust because a target is supported in LLVM does not mean that the kernelwill build or work without any issues. Below is a general summary ofarchitectures that currently work withCC=clang orLLVM=1. Levelof support corresponds to “S” values in the MAINTAINERS files. If anarchitecture is not present, it either means that LLVM does not targetit or there are known issues. Using the latest stable version of LLVM oreven the development tree will generally yield the best results.An architecture’sdefconfig is generally expected to work well,certain configurations may have problems that have not been uncoveredyet. Bug reports are always welcome at the issue tracker below!
Architecture | Level of support |
|
|---|---|---|
arm | Supported |
|
arm64 | Supported |
|
hexagon | Maintained |
|
loongarch | Maintained |
|
mips | Maintained |
|
powerpc | Maintained |
|
riscv | Supported |
|
s390 | Maintained |
|
sparc (sparc64 only) | Maintained |
|
um (User Mode) | Maintained |
|
x86 | Supported |
|
Getting Help¶
IRC: #clangbuiltlinux on irc.libera.chat
Telegram: @ClangBuiltLinux
Getting LLVM¶
We provide prebuilt stable versions of LLVM onkernel.org. These have been optimized with profiledata for building Linux kernels, which should improve kernel build timesrelative to other distributions of LLVM.
Below are links that may be useful for building LLVM from source or procuringit through a distribution’s package manager.