[Nexus 6P Android Development] nathanchance:
How to compile the kernel

BUILD GUIDE: https://github.com/nathanchance/Android-Tools/blob/master/Guides/Building_Flash.txt

BUILD SCRIPT: https://github.com/nathanchance/Android-Tools/blob/master/Scripts/build-flash.sh

FORMER GUIDE:

1. Configure your environment

In order to compile this kernel, I'd recommend using Linux (OS X should be acceptable but this guide will be tailored for Linux). If you can't dual bot, use Virtual Box to create a virtual machine. To get the environment configured, either use Google's own guide here or my guide (only section 1) here.

2. Grab the source

Grab the kernel source

mkdir ~/Android
cd ~/Android
git clone https://github.com/Flash-Kernel/angler.git Flash

Explanation:

First command makes a directory named Android in your home folder (mkdir = make directory). You can change the location if you want but keep it consistent throughout the guide.

Second command moves into that directory (change directory)

Third command pulls the source from my Github into a directory named Ninja (git clone <url> <folder_to_clone_in_to> is the format of the command).

Grab the toolchain you want to use to compile the kernel

cd ~/Android
git clone <kernel_url>

Explanation:

In order to compile the kernel, we need to grab a toolchain (bunch of compilation tools). I currently use DF-LINARO4.9 (third to last option in this list) but I have used all of these in the past. Below are the list of possible URLs to clone; copy and paste it after the words git clone above:

AOSP: https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9

Uber 4.9: https://bitbucket.org/DespairFactor/aarch64-linux-android-4.9-kernel.git

Uber 5.4: https://bitbucket.org/DespairFactor/aarch64-linux-android-5.x-kernel.git

Uber 6.1: https://bitbucket.org/DespairFactor/aarch64-linux-android-6.x-kernel.git

Uber 7.0: https://bitbucket.org/DespairFactor/aarch64-linux-android-7.0-kernel.git

Linaro 4.9: https://android-git.linaro.org/git/.../aarch64/aarch64-linux-android-4.9-linaro.git

Linaro 5.4: https://android-git.linaro.org/git/.../aarch64/aarch64-linux-android-5.4-linaro.git

Linaro 6.1: https://android-git.linaro.org/git/.../aarch64/aarch64-linux-android-6.1-linaro.git

DF-Linaro 4.9: https://bitbucket.org/DespairFactor/aarch64-linux-android-4.9-kernel-linaro.git

DF-Linaro 5.4: https://bitbucket.org/DespairFactor/aarch64-linux-android-5.x-kernel-linaro.git

DF-Linaro 6.1: https://bitbucket.org/DespairFactor/aarch64-linux-android-6.x-kernel-linaro.git

3. Setup the build

Make sure everything looks right

After following the above steps, type:

ls

You should see something like this:

aarch64-linux-android-4.9-kernel-linaro Flash

The toolchain directory make be different depending on what you cloned above. If it looks different, delete the Android folder and do step 2 again.

Select the correct kernel branch

cd ~/Android/Flash
git checkout release

Explanation

First command will move you into the Flash source directory we cloned above.

Second command will checkout the branch you want to compile for (in this case, release)

4. Make the kernel

Tell the makefile what toolchain and architecture you are building for

Figure out the full path of your toolchain folder

Assuming you have done this whole process correctly, you can move into your toolchain directory and type:

cd ~/Android/<toolchain_folder>
pwd

Example:

cd ~/Android/aarch64-linux-android-4.9-kernel-linaro
pwd

You should get something like this:

/home/nathan/Android/aarch64-linux-android-4.9-kernel-linaro

Tell the compiler where that toolchain is

Copy and paste that file location above into this command:

export CROSS_COMPILE=<toolchain_dir>/bin/aarch64-linux-android-

Example:

export CROSS_COMPILE=/home/nathan/Android/aarch64-linux-android-4.9-kernel-linaro/bin/aarch64-linux-android-

Set the architecture

Angler is a 64-bit device so we need arm64

export ARCH=arm64
export SUBARCH=arm64

Clean up from a previous compilation and update the source

cd ~/Android/Flash
git reset --hard origin/n
git clean -f -d -x > /dev/null 2>&1
make clean && make mrproper
git pull

Example:

cd ~/Android/Flash
git reset --hard origin/n
git clean -f -d -x > /dev/null 2>&1
make clean && make mrproper
git pull

Explanation:

The first command moves you into the Flash source directory.

The second command will undo any local commits you have done and align your source with the most recent commit from me.

The third command will clean any untracked or compiled files (this is send to /dev/null so you do not see it happen).

The fourth and fifth command (strung together with &&) will remove you

The sixth command will pull any new kernel changes from me into the source.

Set up the defconfig

A defconfig tells the compiler which features you want in the kernel. This is already set up by me. To set up the defconfig, type the following:

cd ~/Android/Ninja
make flash_defconfig

Name the kernel

If you notice in About Phone > Kernel Version, it shows 3.10.103-FLASH-v3.10 (or something similar). You can customize this by opening the Makefile in the source directory and editing the EXTRAVERSION variable (this will be undone next time you compile).

Make the kernel!

make -j#

Example:

make -j4

Explanation:

The # after the "j" is how many simultaneous threads are run. Traditionally, this is the number of cores you have plus one. So a quad-core CPU would be -j5.

5. Zip it up and flash!

Copy the zImage to the AnyKernel directory

cd ~/Android/Flash/arch/arm64/boot
cp -v Image.gz-dtb ~/Android/Flash/anykernel/zImage

Explanation:

We need to copy the newly compiled kernel (which resides in the arch/arm64/boot folder) to the AnyKernel directory, while changing its name to zImage.

cp = copy

-v = verbose (prints the copying)

Image.gz-dtb = file to be copied

~/Android/Flash/anykernel/zImage = destination and name

Zip it up!

cd ~/Android/Flash/anykernel
zip -r9 flash-kernel.zip * -x README flash-kernel.zip

Using the above commands, your newly completed kernel will be located at ~/Android/Flash/anykernel/flash-kernel.zip

If you followed all of the above steps successfully, congratulations, you built the kernel!! Once you have done all five steps, you will just need to follow steps 4 and 5 to update in the future. Below, I have linked a script that will do all of this for you (with some variable edits at the top); however, please do it manually first so you know exactly what you are doing :) enjoy!

https://raw.githubusercontent.com/nathanchance/angler/release/build-flash.sh

Below are screenshots of me doing this process in a virtual machine (Mint 18) with 2 cores, 4GB of RAM, and 15GB of storage on my MacBook with a 2.5 GHz Intel Core i5 and 8GB of RAM (fairly average in the world of PC specs I think). It's totally possible to do this on your own. Additionally, in the next post, I have provided some tips for customizing the kernel even further!