[Redmi K20 / Xiaomi Mi 9T Guides, News, & Discussio] acervenky:
Introduction :

I'm not an expert in kernel development, but would like to share the steps that I followed to build my kernel. If any of other kernel developers out there would like to add on some tips or correct something, please do!

Pre-requisites :

Ubuntu or any other Linux based OS

Stable Internet Connection

Patience

Step 1 : Setup Build Environment

Open the terminal and enter the following :

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

Step 2 : Download Required Files

Download device source :

git clone --depth=1 https://github.com/MiCode/Xiaomi_Kernel_OpenSource.git -b davinci-p-oss davinci-p-oss

Download a compatible GCC toolchain (Using AOSP's GCC for this guide) :

cd davinci-p-oss
git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9 toolchain

Download a compatible CLANG toolchain (Using AOSP's CLANG for this guide) : Download

Move the downloaded file in the davinci-p-oss folder manually and then extract using the following command :

tar vxzf linux-x86-android-9.0.0_r48-clang-4691093.tar.gz

Download the device-tree-compiler attached below and place it in /usr/bin

Step 3 : Make device specific changes

Make these changes in the /kernel/module.c file.

If you are not familiar with github, you can check out many github guides available on XDA, for now you can just download the module.c file attached to this thread and replace the one in /kernel folder with the downloaded one!

Now, browse back to davinci-p-oss directory, and open the davinci_user_defconfig located in \arch\arm64\configs

Add the following lines :

CONFIG_BUILD_ARM64_DT_OVERLAY=y
CONFIG_MODULE_FORCE_LOAD=y

WIFI & Audio won't work if you don't make these changes, apparently Xiaomi decided to skip wlan & audio drivers in pie sources.

Step 4 : Building The Kernel

cd davinci-p-oss
rm -rf out
mkdir out
export ARCH=arm64
export SUBARCH=arm64
export DTC_EXT=dtc

make O=out ARCH=arm64 davinci_user_defconfig

PATH="${PWD}/bin:${PWD}/toolchain/bin:${PATH}" \
make -j$(nproc --all) O=out \
ARCH=arm64 \
CC=clang \
CLANG_TRIPLE=aarch64-linux-gnu- \
CROSS_COMPILE=aarch64-linux-android- | tee kernel.log

Step 5 : How To Get Help If You Encounter Errors

A kernel.log file will be generated in davinci-p-oss folder, find the line which says error.

If you can't figure out the solution, attach the kernel.log in your reply to this thread.

Step 6 : Booting The Kernel

Once you're done with Step 4, browse to /out/arch/arm64/boot & you'll find the Image.gz-dtb file (compiled zImage)

Download the anykernel template for davinci from attachments and add your Image.gz-dtb file to the archive.

Boot in TWRP, backup your stock kernel & flash the anykernel zip.

References :

Information on compiling Android kernels with Clang by nathanchance

How to compile kernel standalone by Xiaomi

Kernel Builder Virtual Machine & device specific changes by mslezak

AnyKernel3 Template by osm0sis

Wahoo Kernel Tools by frap129

Regards,

acervenky