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.
- A compatible kernel (4.4, 4.9, or 4.14 LTS work best)
- arm64 or x86_64
- Patience
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.
- Add the Clang commits to your kernel source (more on that below).
- Download/build a compatible Clang toolchain. I recommend AOSP's Clang) at first (direct tarball links below, otherwise
git clone
that link).- clang-4053586 for Oreo branches
- clang-4691093 for Pie branches
- 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.
- 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.
- Add the Clang commits to your kernel source (more on that below).
- Make sure your ROM has this commit
- Add the following to your
BoardConfig.mk
file in your device tree:TARGET_KERNEL_CLANG_COMPILE := true
To test and verify everything is working:
- Build a kernel image:
m kernel
orm bootimage
- Open the
out/target/product/*/obj/KERNEL_OBJ/include/generated/compile.h
file and look at theLINUX_COMPILER
option.
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:
-
msm-3.18-oreo - based on the latest Oreo branch for the Snapdragon 820/821 (kernel.lnx.3.18.r33-rel). Uses
msm-perf_defconfig
. -
msm-3.18-pie - based on the latest Pie branch for the Snapdragon 820/821 (kernel.lnx.3.18.r34-rel). Uses
msm-perf_defconfig
. -
msm-4.4-oreo - based on the latest Oreo branch for the Snapdragon 835 (kernel.lnx.4.4.r27-rel). Uses
msmcortex-perf_defconfig
. -
msm-4.4-pie - based on the latest Pie branch for the Snapdragon 835 (kernel.lnx.4.4.r35-rel). Uses
msmcortex-perf_defconfig
. -
msm-4.9-oreo - based on the latest Oreo branch for the Snapdragon 845 (kernel.lnx.4.9.r7-rel). Uses
sdm845-perf_defconfig
. -
msm-4.9-pie - based on the latest Oreo branch for the Snapdragon 845 (kernel.lnx.4.9.r11-rel). Uses
sdm845-perf_defconfig
.
The general structure of these commits is as follows:
- The core compilation support
- Fixing Qualcomm specific drivers to compile with Clang
- Fixing warnings that come from code in mainline
- Fixing warnings that come from code outside of mainline
You should pick the commits that I have committed (nathanchance).
Additionally, there are fixes for:
- qcacld-2.0 available in my Pixel XL kernel.
- qcacld-3.0 available in my OnePlus 5 kernel.
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.
In each stack of patches, I have included two sets of two commits:
-
Revert "scripts: gcc-wrapper: Use wrapper to check compiler warnings"
andkernel: 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
andscripts: 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:]]*$//')
The preferred method for getting help is either opening an issue on this repository or joining the Linux kernel newbies chat on Telegram.