Skip to content

Steps for compiling the kernel with Clang

Nick Desaulniers edited this page Jan 8, 2021 · 7 revisions

Most of the relevant information is now available in the Linux kernel docs. See Building Linux with Clang/LLVM

Dependencies

We still defer to binutils for some targets (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 currently 10.0.1, but is tracked officially in the Linux kernel docs for Minimal requirements to compile the Kernel. Test your combinations of kernel LTS branch, target ISA, kernel configurations, and report issues in our bug tracker. Clang 4 was used to ship a Clang built arm64 kernel on the Google Pixel phone.

Installing a built kernel

$ sudo make LLVM=1 -j install_modules install

If you see an error:

make: clang: Command not found

this is because super user doesn't share the same $PATH as the current user. A possible fix is to add the path to clang (via which clang) to /etc/sudoers via the sudo visudo command. Example:

$ which clang
/home/nick/llvm-project/llvm/build/bin/clang
--- /etc/sudoers	2020-07-19 17:57:11.170893459 -0700
+++ /etc/sudoers	2020-07-19 17:56:35.255107049 -0700
@@ -8,7 +8,7 @@
 #
 Defaults	env_reset
 Defaults	mail_badpass
-Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
+Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/home/nick/llvm-project/llvm/build/bin"
 
 # Host alias specification

Goals

The eventual goal is to be able to compile the kernel hermetically w/ only Clang and LLVM tools.