iykex / android-kernel-clang

Information on compiling Android kernels with Clang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 4.4.56 kernel compiled with Clang 4.0 and upgraded to Android 8.1 with a 4.4.88 kernel compiled with Clang 5.0. According to Google's Clang README, it seems very likely they will ship Android 9.0 with a kernel compiled with Clang 6.0.

Google recently started compiling all Chromebook 4.4 kernels with Clang in R67 (commit, LKML).

Further information on the motive behind compiling with Clang:

TL;DR: Helps find bugs, easier for Google since all of AOSP is compiled with Clang, 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-4053586) at first).
  3. Download/build a compatible GCC toolchain (this is used for assembling and linking - I recommend AOSP's GCC at first).
  4. Compile the kernel (for arm64, x86_64 is similar - example using AOSP's toolchains):
make O=out ARCH=arm64 <defconfig>

make -j$(nproc --all) O=out \
                      ARCH=arm64 \
                      CC="<path to clang folder>/bin/clang" \
                      CLANG_TRIPLE=aarch64-linux-gnu- \
                      CROSS_COMPILE="<path to gcc folder>/bin/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 is available in two places:

The other 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).

  • msm-3.18 - based on the latest Oreo branch for the Snapdragon 820/821 (kernel.lnx.3.18.r33-rel)

    • Compiles with GCC 4.9.4, Clang 5.0 (clang-4053586), Clang 6.0 (clang-4691093), and Clang 7.0 (clang-r328903) without any warnings using arch/arm64/configs/msm-perf_defconfig
  • msm-4.4 - based on the latest Oreo branch for the Snapdragon 835 (kernel.lnx.4.4.r27-rel)

    • Compiles with GCC 4.9.4, Clang 5.0 (clang-4053586), Clang 6.0 (clang-4691093), and Clang 7.0 (clang-r328903) without any warnings using arch/arm64/configs/msmcortex-perf_defconfig

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

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.

About

Information on compiling Android kernels with Clang