Skip to content

Latest commit

 

History

History
118 lines (105 loc) · 5.1 KB

Android-Kernel-Guide.md

File metadata and controls

118 lines (105 loc) · 5.1 KB

Guide to Compile Android Kernel~

This one's specifically for Ubuntu based distros and Gitpod workspace.
Assuming you know a bit of git and bash basics, I'll cover everything else in detail~
For TLDR scroll to Command Summary

Note: Guide commands can be followed blindly from any directory. I'll be referring to 64bit ARM SOCs. Also --depth=1 flag has been used each time to save storage space while git clone.

Step 1:

Setup your build environment, dependencies and tools:

$ git clone --depth=1 https://github.com/akhilnarang/scripts.git -b master vps
$ cd vps/scripts
$ bash setup/android_build_env.sh

Environment has been setup, let's move onto Kernel part~

Step 2:

Clone your kernel repo:

git clone --depth=1 <git code url> -b <branch> mykernel

For eg (this repo is for Xiaomi's MSM8937 board based devices, "a12/master" named branch has been specified):

git clone --depth=1 https://github.com/mi-msm8937/android_kernel_xiaomi_msm8937.git -b a12/master mykernel

Kernel repo has been cloned into ./mykernel directory, let's move onto another interesting part~

Step 3:

Now we need to select a toolchain to compile our kernel...

What's a toolchain you ask?
My rough & simplified explanation:

A toolchain is a set of build tools organized in a chain that compile your project using supplied compilers, linkers, debuggers and libraries recursively and sequentially in an automated fashion.

A few popular toolchains and their flavours:

  • Clang
    • AOSP Clang
    • Proton Clang
    • Snapdragon Clang
  • GCC
    • Eva GCC
    • GNU's own GCC
  • Uber Toolchain

There might be more but these are all that I know of currently :P

For now I'll explain with Proton Clang which is @kdrag0n's own flavour of Clang:
Clang Github Homepage

Proton Clang:

Let's clone the toolchain:

git clone --depth=1 https://github.com/kdrag0n/proton-clang.git -b master mykernel/toolchain

We cloned the toolchain in ./mykernel/toolchain directory, let's get started with actual compilation~

First we create an out directory (the directory where all of the compiled stuff goes):

cd mykernel && mkdir out

Now we tell the Makefile the architecture of our device:

export ARCH=arm64 && export SUBARCH=arm64

We specified architecture as 64bit ARM.

Now we specify the defconfig that we want to compile for (make sure to choose the one for your device properly):

make O=out ARCH=arm64 <defconfig file name>

For eg in the MSM8937 kernel repo if I want to compile for "mi8937" device which has it's defconfig named "mi8937_defconfig" then:

make O=out ARCH=arm64 mi8937_defconfig

All defconfig files are located in arch/arm64/configs directory (ofcourse the arm64 here means our architecture so look for your designaed paths accordingly, it's arm for 32bit btw :P).

Now the environment needs the path to your compiler to run it during compilation:

PATH="${PWD}/toolchain/bin:$PATH"

Note: For any compiler you need to point the path to it's /bin directory. If clang -v command shows Proton Clang then the path has been properly set.

To begin the compilation:

make -j$(nproc --all) O=out ARCH=arm64 CC=clang LLVM=1 CROSS_COMPILE=aarch64-linux-gnu- CROSS_COMPILE_ARM32=arm-linux-gnueabi-

Here -j argument specifies how many CPU cores the compiler can use. While -j$(nproc --all) allows the compiler to use all available cores.

Note: LLVM=1 is a shorthand for enabling all LLVM tools instead of GCC binutils. You can instead manually enter commands to use individual tools as per your need if your kernel doesn't support any or the shorthand itself, directly into the make command.

LLVM tool commands (You can use as many as you want, just replace LLVM=1):

  • AR=llvm-ar
  • NM=llvm-nm
  • OBJCOPY=llvm-objcopy
  • OBJDUMP=llvm-objdump
  • STRIP=llvm-strip

Note: In case compiler throws error regarding defconfig or says /out directory dirty, you can either rm -rf out && mkdir out or make clean && make mrproper and redo from specifying architecture to makefile step.

Step 4:

Congratulations 🎉
Your kernel has been compiled into ./out/arch/arm64/boot and will probably be in Image.gz-dtb format depending on your tree.
You can zip it using AnyKernel3 template, edit updater-script, flash, test and release it~
AnyKernel3 GitHub Homepage

Command Summary:

Here's everything you need w/o interruptions:

$ git clone --depth=1 https://github.com/akhilnarang/scripts.git -b master vps
$ cd vps/scripts
$ bash setup/android_build_env.sh
$ git clone --depth=1 <git code url> -b <branch> mykernel
  • For Proton Clang:
$ git clone --depth=1 https://github.com/kdrag0n/proton-clang.git -b master mykernel/toolchain
$ cd mykernel && mkdir out
$ export ARCH=arm64 && SUBARCH=arm64
$ make O=out ARCH=arm64 <defconfig file name>
$ PATH="${PWD}/toolchain/bin:$PATH"
$ make -j$(nproc --all) O=out ARCH=arm64 CC=clang LLVM=1 CROSS_COMPILE=aarch64-linux-gnu- CROSS_COMPILE_ARM32=arm-linux-gnueabi-

Thank You~