Skip to content

How to compile kernel standalone

秋叶随风ivan edited this page Feb 20, 2019 · 30 revisions

Host: ubuntu 18.04

Phone: Xiaomi Max 3

Toolchain used: aarch64-linux-android-4.9 from google

Setup Build Environment My environment for build aosp and debug, not only for kernel

sudo apt-get install git ccache automake flex lzop bison \
gperf build-essential zip curl zlib1g-dev zlib1g-dev:i386 \
g++-multilib python-networkx libxml2-utils bzip2 libbz2-dev \
libbz2-1.0 libghc-bzlib-dev squashfs-tools pngcrush \
schedtool dpkg-dev liblz4-tool make optipng maven libssl-dev \
pwgen libswitch-perl policycoreutils minicom libxml-sax-base-perl \
libxml-simple-perl bc libc6-dev-i386 lib32ncurses5-dev \
x11proto-core-dev libx11-dev lib32z-dev libgl1-mesa-dev xsltproc unzip

Next , let us compile kernel

For non-dtbo such as Max3

$ git clone --depth=1 https://github.com/MiCode/Xiaomi_Kernel_OpenSource.git -b nitrogen-o-oss nitrogen-o-oss
$ cd nitrogen-o-oss
$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9 toolchain
$ mkdir out
$ export ARCH=arm64
$ export SUBARCH=arm64
$ export CROSS_COMPILE=${PWD}/toolchain/bin/aarch64-linux-android-
make O=out nitrogen_user_defconfig
make -j$(nproc) O=out 2>&1 | tee kernel.log

For include-dtbo, such as Mix 3

if you have aosp, you can compile dtc that out/host/linux-x86/bin/dtc

if you dont have aosp source code, then install it.

sudo apt-get install device-tree-compiler

$ git clone --depth=1 https://github.com/MiCode/Xiaomi_Kernel_OpenSource.git -b perseus-p-oss perseus-p-oss
$ cd perseus-p-oss
$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9 toolchain
$ mkdir out
$ export ARCH=arm64
$ export SUBARCH=arm64
$ export DTC_EXT=dtc
$ export CROSS_COMPILE=${PWD}/toolchain/bin/aarch64-linux-android-

Set CONFIG_BUILD_ARM64_DT_OVERLAY=y

make O=out perseus_user_defconfig
make -j$(nproc) O=out 2>&1 | tee kernel.log

For msm-4.14 like Mi 9

dtc must be from aosp source code(pie-release) We use clang from qcom link:https://developer.qualcomm.com/download/sdllvm/snapdragon-llvm-compiler-android-linux64-609.tar.gz but it is compile ok from google

$ git clone --depth=1 https://github.com/MiCode/Xiaomi_Kernel_OpenSource.git -b cepheus-p-oss cepheus-p-oss
$ cd perseus-p-oss
$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9 toolchain
$ git clone --depth=1 https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86 -b master clang
$ mkdir out
$ export ARCH=arm64
$ export SUBARCH=arm64
$ export DTC_EXT=dtc
$ export CROSS_COMPILE=${PWD}/toolchain/bin/aarch64-linux-android-
Set CONFIG_BUILD_ARM64_DT_OVERLAY=y
make O=out REAL_CC=${PWD}/clang/clang-4691093/bin/clang CLANG_TRIPLE=aarch64-linux-gnu- cepheus_user_defconfig
make -j$(nproc) O=out REAL_CC=${PWD}/clang/clang-4691093/bin/clang CLANG_TRIPLE=aarch64-linux-gnu- 2>&1 | tee kernel.log

After that, you can find many compiled files in the out directory. You can find the kernel in this directory. out/arch/arm64/boot

If you want to flash this,you must unpack boot.img,replace kernel, repack boot.img. Some device need dtbo.img, so you must repack dtbo.img.

Here we focus only on compiling the kernel. Please go to xda or Google search how to repack boot.img and repack dtbo.img

Sometimes, you need compile wlan and audio(sdm845) modules if you find wifi and audio not work after flashing boot.img

maybe some of you face compile error like this error: msm_isp.h: No such file or directory

In android source code (caf), It is already setup environment. like O=out/target/product/{TARGET_PRODUCT}/obj/kernel/, toolchain= ARCH=arm64/arm/x86/mips

it's not a problem with kernel at all! This can be fixed by redirecting output to a out folder as done in steps above

How to Get Help ?

If you follow this guide and still got some errors and you know that this is the problem with kernel than make a issue with link to your kernel.log file ( you'll find it in your kernel directory ), you can use any paste services to upload it like https://pastebin.com

Clone this wiki locally