-
Notifications
You must be signed in to change notification settings - Fork 15
Steps for compiling the kernel with Clang
We still defer to binutils for assembling and linking (though we happily accept bug reports from trying LLVM's equivalents).
For cross-compiling, you need to have the cross-target versions of binutils in your $PATH:
$ sudo apt install binutils-aarch64-linux-gnu
Note: The minimal version of Clang required to build the kernel is hard to exactly pin down, due to the large combinations of kernel [LTS] branch, target ISA, kernel configurations. Test your combination, and report issues in our bug tracker. Clang 4 was used to ship a Clang built arm64 kernel on the Google Pixel phone.
$ make CC=clang HOSTCC=clang
$ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make CC=clang HOSTCC=clang
Note that unlike gcc/binutils, clang ships with support for all backends by default (you can configure them off, but this is considered an antipattern). ARCH= specifies the kernel's notion of target ISA. These can be found in the kernel sources under the arch/ directory. CROSS_COMPILE is a prefix for binutils tools. KBUILD will literally run $ $CROSS_COMPILE-as and $ $CROSS_COMPILE-ld, so verify that you've installed the correct cross version of binutils and they're accessible from your $PATH.
This is the relevant part of the Kernel's top level Makefile. These define the internal variables that will be used during the build. The tools are prefixed with CROSS_COMPILE if that's set in the environment.
For example, to substitute GNU objcopy for llvm-objcopy, you might invoke your kernel build as:
$ make OBJCOPY=llvm-objcopy
where llvm-objcopy was already accessible via $PATH. Or when cross compiling:
$ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make OBJCOPY=llvm-objcopy
You can see the relevant variables being overridden in our CI scripts here.
So the list of relevant variables is CC=clang, AS=clang, LD=ld.lld, AR=llvm-ar, NM=llvm-nm, STRIP=llvm-strip, OBJCOPY=llvm-objcopy, OBJDUMP=llvm-objdump, HOSTCC=clang, HOSTLD=ld.lld, and HOSTAR=llvm-ar. (TODO: It's probably worthwhile to simplify this upstream).
The eventual goal is to be able to compile the kernel hermetically w/ only Clang and LLVM tools.