Giter Site home page Giter Site logo

android-kernel-clang's Introduction

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.

android-kernel-clang's People

Contributors

nathanchance avatar vasishath avatar yarost12 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.