Information on compiling Android kernels with Clang
Branch: master
Clone or download
nathanchance README: Update make command to compile with Clang as Google does
For kernels like the Pixel 3, which require that scheme.

Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
Latest commit 2ae41f2 Feb 2, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md README: Update make command to compile with Clang as Google does Feb 2, 2019

README.md

Compiling an Android kernel with Clang

Background

Google compiles the Pixel 2 kernel with Clang. They shipped the device on Android 8.0 with a kernel compiled with Clang 4.0 (build.config commit and prebuilt kernel commit) and upgraded to Android 8.1 with a kernel compiled with Clang 5.0 (build.config commit and prebuilt kernel commit).

Google uses Clang's link-time optimization and control flow integrity in the Pixel 3 kernel, hardening it against return oriented programming attacks (LTO commit, CFI commit).

Google started compiling all Chromebook 4.4 kernels with Clang in R67 (commit, LKML) and going forward, Clang is the default compiler for all future versions of the kernel (commit).

Further information including videos of talks on the motive behind compiling with Clang can be found in the ClangBuiltLinux wiki.

TL;DR: Helps find bugs, easier for Google since all of AOSP is compiled with Clang, compiler specific features such as link-time optimization, and better static analysis for higher code quality.

Requirements

  • A compatible kernel (4.4, 4.9, or 4.14 LTS work best)
  • arm64 or x86_64
  • Patience

How to compile the kernel with Clang (standalone)

NOTE: I am not going to write this for beginnings. I assume if you are smart enough to pick some commits, you are smart enough to know how to run git clone and know the paths of your system.

  1. Add the Clang commits to your kernel source (more on that below).
  2. Download/build a compatible Clang toolchain. I recommend AOSP's Clang) at first (direct tarball links below, otherwise git clone that link).
  3. Download/build a compatible GCC toolchain. GCC itself is not used but binutils are used for linking/assembling right now. When using AOSP Clang, you should use AOSP's GCC to avoid weird incompatibility issues.
  4. Compile the kernel (for arm64, x86_64 is similar - example using AOSP's toolchains):
make O=out ARCH=arm64 <defconfig>

PATH="<path to clang folder>/bin:<path to gcc folder>/bin:${PATH}" \
make -j$(nproc --all) O=out \
                      ARCH=arm64 \
                      CC=clang \
                      CLANG_TRIPLE=aarch64-linux-gnu- \
                      CROSS_COMPILE=aarch64-linux-android-

After compiling, you can verify the toolchain used by opening out/include/generated/compile.h and looking at the LINUX_COMPILER option.

How to compile the kernel with Clang (inline with a custom ROM)

  1. Add the Clang commits to your kernel source (more on that below).
  2. Make sure your ROM has this commit
  3. Add the following to your BoardConfig.mk file in your device tree: TARGET_KERNEL_CLANG_COMPILE := true

To test and verify everything is working:

  1. Build a kernel image: m kernel or m bootimage
  2. Open the out/target/product/*/obj/KERNEL_OBJ/include/generated/compile.h file and look at the LINUX_COMPILER option.

Getting the Clang patchset

The core Clang patchset comes from mainline and it is backported to 4.4 and 4.9 in two places:

The branches in this repository will be dedicated to taking this patchset and enhancing it by fixing/hiding all of the warnings from Clang (from mainline, the Pixel 2, and my own knowledge).

All branches will build with -Werror and the following toolchains:

Oreo branches will build with -Werror and the follow toolchains:

Branch information:

The general structure of these commits is as follows:

  1. The core compilation support
  2. Fixing Qualcomm specific drivers to compile with Clang
  3. Fixing warnings that come from code in mainline
  4. Fixing warnings that come from code outside of mainline

You should pick the commits that I have committed (nathanchance).

Additionally, there are fixes for:

Every time there is a branch update upstream, the branch will be rebased, there is no stable history here! Ideally, I will not need to add any commits but I will do my best to keep everything in the same order.

NOTE: 3.18 Clang is not supported officially by an OEM. I've merely added it here as I decided to support it with my Pixel XL kernel.

Additional commits

In each stack of patches, I have included two sets of two commits:

  • Revert "scripts: gcc-wrapper: Use wrapper to check compiler warnings" and kernel: Add CC_WERROR config to turn warnings into errors: The first commit removes CAF's crappy gcc-wrapper.py script, removing an unnecessary Python dependency, and the second commit adds the ability to compile with -Werror through a Kconfig option, CONFIG_CC_WERROR. I highly recommend you enable this after fixing any warnings that arise from OEM/CAF code!

  • UPSTREAM: scripts/mkcompile_h: Remove trailing spaces from compiler version and scripts: Support a custom compiler name: On Oreo, Google tweaks the regex for parsing the kernel version to support no trailing space, which I think looks better, so the first commit removes the trailing space in GCC. The second commit allows you to easily remove the long URLs in the latest Google Clang versions by adding this bit of code to your compilation script (or run it in your shell before compiling):

    export KBUILD_COMPILER_STRING=$(<path_to_clang_folder/bin/clang --version | head -n 1 | perl -pe 's/\(http.*?\)//gs' | sed -e 's/  */ /g' -e 's/[[:space:]]*$//')

Getting help

The preferred method for getting help is either opening an issue on this repository or joining the Linux kernel newbies chat on Telegram.