[REFERENCE] How to compile an Android kernel

Search This thread

newbit

Senior Member
Nov 16, 2008
262
96
This problem is fixed, it was the GCC wrapper, the build progresses for a bit, but a new, unrelated problem crops up.
Code:
/bin/sh: line 1: -Wp,-MD,arch/arm64/kernel/vdso/.vdso.lds.d: No such file or directory

  VDSOA  arch/arm64/kernel/vdso/note.o

make[2]: *** [/home/smee/Downloads/twrp/kernel/motorola/cebu/scripts/Makefile.build:450: arch/arm64/kernel/vdso/vdso.lds] Error 127

make[2]: *** Waiting for unfinished jobs....

make[1]: *** [arch/arm64/Makefile:188: vdso_prepare] Error 2

Once again, Google-fu fails me. I don't quite know what to do here. Any help appreciated.
I do get different issues trying to compile it.
Is this the official way how Motorola is publishing kernel sources?

Could you please provide a config file from your phone?
adb pull /proc/config.gz
What defconfig do you use?

Could you also please provide your stock kernel file?
Or a link where to download the firmware image.
What are your exact kernel download/clone/config/export steps?

I tried the following with the build.config.gki.aarch64 as config base:
Code:
### Download Sources, Toolchain, Buildtools etc. ###
mkdir mistersmee && cd mistersmee
git clone -b MMI-QZC30.Q4-22-57 --single-branch https://github.com/MotorolaMobilityLLC/kernel-msm.git
VER=r353983c
mkdir clang-$VER && cd clang-$VER
BRANCH=android10-release
wget https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+archive/refs/heads/$BRANCH/clang-$VER.tar.gz
tar -xzf clang-$VER.tar.gz && cd ..
git clone https://android.googlesource.com/kernel/prebuilts/build-tools
git clone \
    -b $BRANCH \
    --single-branch https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9

### Exports: (must be done in kernel-source directory)
VER=r353983c
cd kernel-msm
export ARCH=arm64
export CLANG_TRIPLE=aarch64-linux-gnu-
export CROSS_COMPILE=aarch64-linux-androidkernel-
export LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN=$(pwd)/../aarch64-linux-android-4.9/bin
export CLANG_PREBUILT_BIN=$(pwd)/../clang-$VER/bin
export BUILDTOOLS_PREBUILT_BIN=$(pwd)/../build-tools/linux-x86/bin
DEVEXPS="CC=clang EXTRA_CMDS='' STOP_SHIP_TRACEPRINTK=1 ABI_DEFINITION=abi_gki_aarch64.xml"
export KBUILD_BUILD_USER=build-user
export KBUILD_BUILD_HOST=build-host
export PATH=$LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN:$CLANG_PREBUILT_BIN:$BUILDTOOLS_PREBUILT_BIN:$PATH

./scripts/kconfig/merge_config.sh -m -r \
    ./arch/arm64/configs/gki_defconfig \
    ./arch/arm64/configs/vendor/ext_config/cebu-default.config
make $DEVEXPS savedefconfig
mv defconfig arch/arm64/configs/cebu_gki_defconfig && rm .config

make $DEVEXPS mrproper
make $DEVEXPS distclean

make $DEVEXPS cebu_gki_defconfig
make $DEVEXPS xconfig
time make $DEVEXPS -j$(nproc)

And the build error:
Code:
  CC      fs/notify/fsnotify.o
mm/huge_memory.c:2389:46: error: too few arguments to function call, expected 3, have 2
        unmap_success = try_to_unmap(page, ttu_flags);
                        ~~~~~~~~~~~~                ^
./include/linux/rmap.h:211:1: note: 'try_to_unmap' declared here
bool try_to_unmap(struct page *page, enum ttu_flags flags,
^
1 error generated.
scripts/Makefile.build:338: recipe for target 'mm/huge_memory.o' failed
make[1]: *** [mm/huge_memory.o] Error 1
Makefile:1143: recipe for target 'mm' failed
make: *** [mm] Error 2
  CC      fs/overlayfs/namei.o

[Update 17.04.2021]
I've managed to get the kernel build within the AOSP Build Env. It might be bit much (ap. 28GB), but it did the trick.

Code:
### Download Sources, Toolchain, Buildtools etc. ###
mkdir misterAOSP && cd misterAOSP
repo init --depth=1 -u https://android.googlesource.com/kernel/manifest -b common-android-4.19-stable
repo sync -f --force-sync --no-clone-bundle --no-tags -j$(nproc)

### replace common Kernel Dir with MMI-QZC30.Q4-22-57 aka android-10-release-qzc
mv common common_ori
git clone -b android-10-release-qzc --single-branch https://github.com/MotorolaMobilityLLC/kernel-msm.git common

### Downloaded the Stock Firmware and extracted the defconfig from the boot.img with (Android Image Kitchen from @osm0sis), AIK-linux -> boot.img-zImage
Motorola_Moto_G9_Power_XT2091-3_RETUK_CEBU_RETAIL_QZC30.Q4-22-57_10_by_(motostockrom.com).zip
./common/scripts/extract-ikconfig boot.img-zImage > common/arch/arm64/configs/motog9Pwr_defconfig
Code:
### add CLang Version r353983c to prebuilts-master
VER=r353983c
clangDIR=prebuilts-master/clang/host/linux-x86/clang-$VER
mkdir -p $clangDIR && cd $clangDIR
BRANCH=android10-release
wget https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+archive/refs/heads/$BRANCH/clang-$VER.tar.gz
tar -xzf clang-$VER.tar.gz && cd -

### add GCC to prebuilts
GCCDIR=prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9
mkdir -p $GCCDIR
git clone \
    -b $BRANCH \
    --single-branch https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9 $GCCDIR

### create build.config.motog9Pwr in common
Code:
ARCH=arm64
BRANCH=android-4.19
KERNEL_DIR=common
DEFCONFIG=motog9Pwr_defconfig

CC=clang
EXTRA_CMDS=''
STOP_SHIP_TRACEPRINTK=1
ABI_DEFINITION=abi_gki_aarch64.xml

CLANG_TRIPLE=aarch64-linux-gnu-
CROSS_COMPILE=aarch64-linux-androidkernel-
LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN=prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin
CLANG_PREBUILT_BIN=prebuilts-master/clang/host/linux-x86/clang-r353983c/bin

FILES="
arch/arm64/boot/Image.gz
vmlinux
System.map
"

Code:
### build the kernel
BUILD_CONFIG=common/build.config.motog9Pwr build/build.sh -j$(nproc)

The Kernel file gets placed into out/android-4.19/dist

The new kernel size compared to the stock kernel differ quiet much. There is perhaps
something more needed to get the kernel booting.

Code:
31508992 Apr 17 17:06 boot.img-zImage
11487928 Apr 17 15:35 boot.img-zImage.gz
25762304 Apr 17 17:06 Image
9993603 Apr 17 16:38 Image.gz

I also noticed, that inside the defconfig from the firmware, it says:
# Linux/arm64 4.19.95 Kernel Configuration
# Compiler: clang version 8.0.16 for Android NDK

But inside your kernel source from github, it refers to clang-r353983c, which is clang version 9.0.3.
The Kernel Version 4.19.95 is identical.

Cheers NewBit
 

Attachments

  • build.config.motog9Pwr.txt
    480 bytes · Views: 30
Last edited:
  • Like
Reactions: h3xmind

mistersmee

Senior Member
Jan 14, 2019
71
24
20
Kolhapur, India
Moto G9 Power
I do get different issues trying to compile it.
Is this the official way how Motorola is publishing kernel sources?

Could you please provide a config file from your phone?
adb pull /proc/config.gz
What defconfig do you use?

Could you also please provide your stock kernel file?
Or a link where to download the firmware image.
What are your exact kernel download/clone/config/export steps?

I tried the following with the build.config.gki.aarch64 as config base:
Code:
### Download Sources, Toolchain, Buildtools etc. ###
mkdir mistersmee && cd mistersmee
git clone -b MMI-QZC30.Q4-22-57 --single-branch https://github.com/MotorolaMobilityLLC/kernel-msm.git
VER=r353983c
mkdir clang-$VER && cd clang-$VER
BRANCH=android10-release
wget https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+archive/refs/heads/$BRANCH/clang-$VER.tar.gz
tar -xzf clang-$VER.tar.gz && cd ..
git clone https://android.googlesource.com/kernel/prebuilts/build-tools
git clone \
    -b $BRANCH \
    --single-branch https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9

### Exports: (must be done in kernel-source directory)
VER=r353983c
cd kernel-msm
export ARCH=arm64
export CLANG_TRIPLE=aarch64-linux-gnu-
export CROSS_COMPILE=aarch64-linux-androidkernel-
export LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN=$(pwd)/../aarch64-linux-android-4.9/bin
export CLANG_PREBUILT_BIN=$(pwd)/../clang-$VER/bin
export BUILDTOOLS_PREBUILT_BIN=$(pwd)/../build-tools/linux-x86/bin
DEVEXPS="CC=clang EXTRA_CMDS='' STOP_SHIP_TRACEPRINTK=1 ABI_DEFINITION=abi_gki_aarch64.xml"
export KBUILD_BUILD_USER=build-user
export KBUILD_BUILD_HOST=build-host
export PATH=$LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN:$CLANG_PREBUILT_BIN:$BUILDTOOLS_PREBUILT_BIN:$PATH

./scripts/kconfig/merge_config.sh -m -r \
    ./arch/arm64/configs/gki_defconfig \
    ./arch/arm64/configs/vendor/ext_config/cebu-default.config
make $DEVEXPS savedefconfig
mv defconfig arch/arm64/configs/cebu_gki_defconfig && rm .config

make $DEVEXPS mrproper
make $DEVEXPS distclean

make $DEVEXPS cebu_gki_defconfig
make $DEVEXPS xconfig
time make $DEVEXPS -j$(nproc)

And the build error:
Code:
  CC      fs/notify/fsnotify.o
mm/huge_memory.c:2389:46: error: too few arguments to function call, expected 3, have 2
        unmap_success = try_to_unmap(page, ttu_flags);
                        ~~~~~~~~~~~~                ^
./include/linux/rmap.h:211:1: note: 'try_to_unmap' declared here
bool try_to_unmap(struct page *page, enum ttu_flags flags,
^
1 error generated.
scripts/Makefile.build:338: recipe for target 'mm/huge_memory.o' failed
make[1]: *** [mm/huge_memory.o] Error 1
Makefile:1143: recipe for target 'mm' failed
make: *** [mm] Error 2
  CC      fs/overlayfs/namei.o

[Update 17.04.2021]
I've managed to get the kernel build within the AOSP Build Env. It might be bit much (ap. 28GB), but it did the trick.

Code:
### Download Sources, Toolchain, Buildtools etc. ###
mkdir misterAOSP && cd misterAOSP
repo init --depth=1 -u https://android.googlesource.com/kernel/manifest -b common-android-4.19-stable
repo sync -f --force-sync --no-clone-bundle --no-tags -j$(nproc)

### replace common Kernel Dir with MMI-QZC30.Q4-22-57 aka android-10-release-qzc
mv common common_ori
git clone -b android-10-release-qzc --single-branch https://github.com/MotorolaMobilityLLC/kernel-msm.git common

### Downloaded the Stock Firmware and extracted the defconfig from the boot.img with (Android Image Kitchen from @osm0sis), AIK-linux -> boot.img-zImage
Motorola_Moto_G9_Power_XT2091-3_RETUK_CEBU_RETAIL_QZC30.Q4-22-57_10_by_(motostockrom.com).zip
./common/scripts/extract-ikconfig boot.img-zImage > common/arch/arm64/configs/motog9Pwr_defconfig
Code:
### add CLang Version r353983c to prebuilts-master
VER=r353983c
clangDIR=prebuilts-master/clang/host/linux-x86/clang-$VER
mkdir -p $clangDIR && cd $clangDIR
BRANCH=android10-release
wget https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+archive/refs/heads/$BRANCH/clang-$VER.tar.gz
tar -xzf clang-$VER.tar.gz && cd -

### add GCC to prebuilts
GCCDIR=prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9
mkdir -p $GCCDIR
git clone \
    -b $BRANCH \
    --single-branch https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9 $GCCDIR

### create build.config.motog9Pwr in common
Code:
ARCH=arm64
BRANCH=android-4.19
KERNEL_DIR=common
DEFCONFIG=motog9Pwr_defconfig

CC=clang
EXTRA_CMDS=''
STOP_SHIP_TRACEPRINTK=1
ABI_DEFINITION=abi_gki_aarch64.xml

CLANG_TRIPLE=aarch64-linux-gnu-
CROSS_COMPILE=aarch64-linux-androidkernel-
LINUX_GCC_CROSS_COMPILE_PREBUILTS_BIN=prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin
CLANG_PREBUILT_BIN=prebuilts-master/clang/host/linux-x86/clang-r353983c/bin

FILES="
arch/arm64/boot/Image.gz
vmlinux
System.map
"

Code:
### build the kernel
BUILD_CONFIG=common/build.config.motog9Pwr build/build.sh -j$(nproc)

The Kernel file gets placed into out/android-4.19/dist

The new kernel size compared to the stock kernel differ quiet much. There is perhaps
something more needed to get the kernel booting.

Code:
31508992 Apr 17 17:06 boot.img-zImage
11487928 Apr 17 15:35 boot.img-zImage.gz
25762304 Apr 17 17:06 Image
9993603 Apr 17 16:38 Image.gz

I also noticed, that inside the defconfig from the firmware, it says:
# Linux/arm64 4.19.95 Kernel Configuration
# Compiler: clang version 8.0.16 for Android NDK

But inside your kernel source from github, it refers to clang-r353983c, which is clang version 9.0.3.
The Kernel Version 4.19.95 is identical.

Cheers NewBit

I'm not just trying to build the kernel, but rather TWRP for this phone, so let me see how to get this done within the TWRP build environment. Thanks, though. Knowing that it actually builds, and whatever the errors are, are due to something I'm doing incorrectly, is half the battle.

Hmm, I don't think that when I was building, that it was using clang to build, it probably was using gcc. That might honestly be it. Let's see how I crack along now.

The reason for this rigamarole, is that the touch doesn't work, for some reason while using the prebuilt kernel, and somewhere on the internet in some GitHub issue, someone mentioned that using the kernel sources could fix that, and so here I am.
 
Last edited:

Flacko89

Member
Jul 12, 2018
14
0
Hello everyone can anyone help me out with building a kernel for p2xl? Or link me to one that is done? Been at this for days I'm a a noob at this keep getting error when compiling
 

Flacko89

Member
Jul 12, 2018
14
0

Attachments

  • PXL_20210421_193735907.jpg
    PXL_20210421_193735907.jpg
    6 MB · Views: 81

Flacko89

Member
Jul 12, 2018
14
0
I was getting another message which is how I ended up at this wonderful forum whick was arch/arm64/makefile:48: *** cross_compile_arm32 not defined or empty, the compat vdso will not be built. stop.
 

rishav518

Member
Aug 1, 2017
5
2
There are three defconfig files on /arch/arch/configs directory without my device name(wt88047 or msm8916), which to choose?
defconfig.png
 

rishav518

Member
Aug 1, 2017
5
2
Hi, i got error after last make command to compile, i have attached terminal log, can someone take a look at it.
 

Attachments

  • kernel error log.txt
    4.2 KB · Views: 27

trueToastedCode

Senior Member
Mar 14, 2019
98
20
Cologne
OnePlus 7
Hey, I wanted to compile the stock OnePlus 7 kernel for which i made a repo (https://github.com/trueToastedCode/android_kernel_oneplus7_sm8150/tree/11.0.0.2/msm-4.14).

The problem is that i don't know of the resulting flashable zip is in theorie good (eg. my flash script). May someone just have a look over it: https://pastebin.com/xmFQaJJ1

*Full Kernel: https://drive.google.com/file/d/1JrDBNrzksmLaT7S5mkYcW86L65CsnDnS/view?usp=sharing *It uses AnyKernel3 (commit 37782872f69a68557f53129d3ff1eeca95a07f30)
 

Sanskar Bhusal

Senior Member
Feb 21, 2020
56
7
Do we need to learn programmimg to understand this tutorial? Firstly this looks a nice noob friendly guide and slowly as I continue reading throughly things get complicated. How did you learn these stuffs? Do college teaches these stuffs?
 

op9lover

New member
Jun 26, 2021
1
1
Hello,I have a problem for build the oneplus9 pro kernel.I use OnePlusOSS/android_kernel_oneplus_sm8350. The build Step is
1.export CROSS_COMPILE=$(pwd)/bin/aarch64-linux-android-
2.export ARCH=arm64 && export SUBARCH=arm64
3.make O=out <defconfig_name>
4.make O=out -j2

There is a lot of errors, such as
"
../scripts/mkcompile_h: 57: ../scripts/mkcompile_h: Bad substitution
../init/Makefile:35: recipe for target 'include/generated/compile.h' failed
make[2]: *** [include/generated/compile.h] Error 2
"

and the "../scripts/mkcompile_h: 57: ../scripts/mkcompile_h" is
"
#delete /work
CURRENT_RELEASE="${RELEASE#\/work}"
#replace / to \\
WINDOWS_RELEASE="${CURRENT_RELEASE//\//\\\\}"
"

Could you tell me how to solve this problem?Thank you for your help!
 
  • Like
Reactions: davidsparker

acuicultor

Recognized Contributor / Recognized Developer
Jan 3, 2013
3,349
12,419
46
Palma de Mallorca
Google Pixel 7 Pro
Hello,I have a problem for build the oneplus9 pro kernel.I use OnePlusOSS/android_kernel_oneplus_sm8350. The build Step is
1.export CROSS_COMPILE=$(pwd)/bin/aarch64-linux-android-
2.export ARCH=arm64 && export SUBARCH=arm64
3.make O=out <defconfig_name>
4.make O=out -j2

There is a lot of errors, such as
"
../scripts/mkcompile_h: 57: ../scripts/mkcompile_h: Bad substitution
../init/Makefile:35: recipe for target 'include/generated/compile.h' failed
make[2]: *** [include/generated/compile.h] Error 2
"

and the "../scripts/mkcompile_h: 57: ../scripts/mkcompile_h" is
"
#delete /work
CURRENT_RELEASE="${RELEASE#\/work}"
#replace / to \\
WINDOWS_RELEASE="${CURRENT_RELEASE//\//\\\\}"
"

Could you tell me how to solve this problem?Thank you for your help!
Op9 kernel must be compiled with clang not with gcc.
 

daveomg

Member
May 10, 2015
20
7
OnePlus 5T
Hello.
Can anybody recommend me some books related in any way to compiling android kernel? I have been using Linux for a long time, and decided to compile a kernel for myself to learn something new(the same way I started learning programming languages), but I encounter a lot of errors during the compilation process... so I have decided to find and read some books about it to have a theory background. Thank you in advance :)
 

kevin71246

Senior Member
Nov 13, 2008
226
179
Chicago
Hello.
Can anybody recommend me some books related in any way to compiling android kernel? I have been using Linux for a long time, and decided to compile a kernel for myself to learn something new(the same way I started learning programming languages), but I encounter a lot of errors during the compilation process... so I have decided to find and read some books about it to have a theory background. Thank you in advance :)

I know the OP (& guide) was posted several years ago, but it looks like this thread is still alive and well - and I'm guessing methods may have changed since.

I want to make a minimum change to the AOSP kernel config of a Google Pixel 3a/Sargo, and build/recompile it - but I'm not that stupid so I'm starting with trying to recompile it with NO CHANGES! I would think this would be as simple as it gets since its Google AOSP vanilla with no changes but no. I followed their guide and it doesnt work with a very fundamental error I cant figure out.

Error while building: (~7min in)
Code:
Creating initramfs
build/build.sh: line 967: mkbootfs: command not found

Followed Google's guide here: https://source.android.com/setup/build/building-kernels
Code:
mkdir android-kernel && cd android-kernel
repo init -u https://android.googlesource.com/kernel/manifest -b android-msm-bonito-4.9-android11-qpr2
repo sync
build/build.sh

I found the mkbootfs binary in $HOME/android/aosp-kernel/build/build-tools/path/linux-x86/mkbootfs, as well as many other locations actually, and all of them, when executed, say "command not found"! Is that expected?! Ie. I "cd to $HOME/android/aosp-kernel/build/build-tools/path/linux-x86 & execute mkbootfs (where I can see the file & verified it has execute permissions), and still issues the same error.
I tried adding "$HOME/android/aosp-kernel/build/build-tools/path/linux-x86" to my PATH but same results. For some reason I can't find anything helpful on the net.
PLEASE HELP!
 

kevin71246

Senior Member
Nov 13, 2008
226
179
Chicago
I figured this out: https://android.stackexchange.com/q...ile-trying-to-build-aosp-kernel/239251#239251

Everything builds fine now, able then put to put the generated Image.lz4 in my AOSP source folder, compile, & get a boot.img. I flash it and Android boots fine...except the touchscreen doesnt work! I made ZERO changes to the kernel; Im just trying to build it and prove it works before I change anything. What am I doing wrong?! I created this: https://android.stackexchange.com/q...ild-for-pixel3a-sargo-touchscreen-not-working
 
Last edited:
  • Like
Reactions: flash713

HesThatGuy

Senior Member
Feb 28, 2016
195
67
I'm trying to compile a kernel for the OnePlus 8
Would someone be able to help me find a solution to this error?

No rule to make target 'arch/arm64/boot/dts/qcom/apq8016-sbc.dtb'

I'm using Ubuntu 16.04 LTS through a VM
 

HesThatGuy

Senior Member
Feb 28, 2016
195
67
I figured this out: https://android.stackexchange.com/q...ile-trying-to-build-aosp-kernel/239251#239251

Everything builds fine now, able then put to put the generated Image.lz4 in my AOSP source folder, compile, & get a boot.img. I flash it and Android boots fine...except the touchscreen doesnt work! I made ZERO changes to the kernel; Im just trying to build it and prove it works before I change anything. What am I doing wrong?! I created this: https://android.stackexchange.com/q...ild-for-pixel3a-sargo-touchscreen-not-working
You'll need to change the values of the configs with "touchscreen" in their name from 'm' to 'y'

you might also have to do that for your wifi and fast charging as it's like that with the Pixel 2

I hope this helps you

 

kevin71246

Senior Member
Nov 13, 2008
226
179
Chicago

AmolAmrit

Senior Member
Dec 18, 2013
2,261
4,118
Patna
Galaxy Y Duos
Moto G
Guys, exporting KBUILD_BUILD_HOST and KBUILD_BUILD_HOST does not seem to work on Android 11 sources. It says nobody@android in about phone section. Is someone facing the same issue? Clang compile is enabled.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    How can I compile a 5.x kernel using the 3.18 source? My device doesn’t have newer source.
    sorry I haven't tried, I'm also newbie. I will try first and may tell you if success
  • 240
    Introduction

    Hello everyone, I will be going over how to compile a kernel from beginning to end!

    Prerequisites:
    • A Linux environment (preferably 64-bit)
    • Knowledge of how to navigate the command line
    • Common sense
    • A learning spirit, there will be no spoonfeeding here

    What this guide will cover:
    1. Downloading the source
    2. Downloading a cross compiler
    3. Building the kernel
    4. Flashing the kernel

    What this guide will NOT cover:
    • Setting up a build environment (plenty of existing Linux installation guides)
    • Adding features to the kernel (plenty of git cherry-picking guides)

    I know this has been done before but on a cursory search, I have not seen a guide that was recently updated at all.


    1. Downloading the source


    If you have a custom kernel you want to build, move along after cloning the kernel using the git clone command below.

    If you are compiling your stock kernel, it is ultimately up to you to know where to get your kernel source from but here are some common places:

    Google: https://android.googlesource.com/kernel/msm/ (pick your architecture and look at the branches)
    LG: http://opensource.lge.com/index
    Samsung: http://opensource.samsung.com/reception.do
    HTC: https://www.htcdev.com/devcenter/downloads
    OnePlus: https://github.com/OnePlusOSS
    Motorola: https://github.com/MotorolaMobilityLLC
    Sony: https://github.com/sonyxperiadev/kernel

    To download the kernel, you can either use git clone or download the tarball and extract it:
    Code:
    git clone -b <branch_to_checkout> <url> <desired_folder_name>
    
    OR
    
    tar -xvf <filename>

    For example, if I wanted to grab the latest Nexus 6P from Google above:
    Code:
    git clone -b android-msm-angler-3.10-nougat-mr2 https://android.googlesource.com/kernel/msm/ angler
    This will clone the kernel/msm repo into an angler folder and checkout the android-msm-angler-3.10-nougat-mr2 automatically.

    I can try and help you locate your source if necessary.



    2. Downloading a cross compiler


    Since most Android devices are ARM based, we need a compiler that is targeting ARM devices. A host (or native) compiler will not work unless you are compiling on another ARM device.

    You can either compile one yourself if you know how (crosstool-NG is a great tool for this) or download a prebuilt one. Luckily Google provides a high quality toolchain for this, in both an arm (32-bit) and arm64 (64-bit). It's up to you to know the architecture of your device. Typically speaking, most devices in the past two-three years are 64-bit.

    Another popular toolchain is UberTC, which can be found here: https://bitbucket.org/matthewdalex/. Most kernels will need patches for anything higher than 4.9 and while I don't mind assisting with finding them, you should compile with Google's toolchain first.

    Once you have decided, clone the toolchain:
    Code:
    git clone <url>


    3. Compile the kernel


    1. Point the Makefile to your compiler (run this from within the toolchain folder!!)
    Code:
    export CROSS_COMPILE=$(pwd)/bin/<toolchain_prefix>-
    Example:
    Code:
    export CROSS_COMPILE=$(pwd)/bin/aarch64-linux-android-

    NOTE #1: For kernels that can be compiled with Clang (like the Pixel 2), see this guide. I will support it here if there are any questions.

    NOTE #2: Pixel and Pixel 2 users, you will need to follow these steps as well if compiling for Android Pie.

    2. Tell the Makefile the architecture of the device
    Code:
    export ARCH=<arch> && export SUBARCH=<arch>
    Example:
    Code:
    export ARCH=arm64 && export SUBARCH=arm64

    3. Locate your proper defconfig
    Navigate to the arch/<arch>/configs folder within the kernel source (e.g. arch/arm64/configs) and locate your device's or custom kernel developer's proper config file. For example, it will often be in the form of <codename>_defconfig or <kernel_name>_defconfig. Generic Qualcomm configs may be used as well (msm-perf_defconfig, msmcortex-perf_defconfig). When in doubt, ask here if you are confused. A defconfig tells the compiler what options to add to the kernel.

    4. Build the kernel

    Code:
    make clean
    make mrproper
    make <defconfig_name>
    make -j$(nproc --all)

    If those commands succeed, you will have an Image, Image-dtb, Image.gz, or Image.gz-dtb file at the end.

    If it failed, as was pointed out to me by @flar2 while making a complete idiot of myself, you may need to specify an output directory while making new CAF based kernels, like so:
    Code:
    mkdir -p out
    make O=out clean
    make O=out mrproper
    make O=out <defconfig_name>
    make O=out -j$(nproc --all)

    If after that something is still broken, you may need to fix some headers or other issues. If it is a custom kernel, bring it up with your developer.
    If it's an OEM, it's up to you to try and fix it, which we can assist with.



    4. Flash the kernel


    Assuming you were able to compile the kernel successfully, you now need to flash it! I will be covering two different ways to flash a compiled kernel: unpacking and repacking the boot image by hand using Android Image Kitchen or AnyKernel2, both by the brilliant @osm0sis. If there are any per-device nuances, please let me know and I'll add them here! Additionally, this section can vary drastically by device, you may need to consult developers of your device for assistance if necessary.

    Android Image Kitchen

    1. Pull your device's boot image from the latest image available for your device (whether it be a ROM or stock)
    2. Download the latest Android Image Kitchen from this thread.
    3. Run the following with the boot image:
      Code:
      unpackimg.sh <image_name>.img
    4. Locate the zImage file and replace it with your kernel image (rename it to what came out of the boot image)
    5. Run the following to repack:
      Code:
      repackimg.sh
    6. Flash the new boot image with fastboot or TWRP!

    AnyKernel2

    1. Download the latest AnyKernel2 zip: https://github.com/osm0sis/AnyKernel2/archive/master.zip
    2. Apply this patch to clean out all of the demo files:
      Code:
      wget https://github.com/nathanchance/AnyKernel2/commit/addb6ea860aab14f0ef684f6956d17418f95f29a.diff
      patch -p1 < addb6ea860aab14f0ef684f6956d17418f95f29a.diff
      rm addb6ea860aab14f0ef684f6956d17418f95f29a.diff
    3. Place your kernel image in the root of the file.
    4. Open the anykernel.sh file and modify the following values:
      • kernel.string: your kernel name
      • device.name#: List all of your device's codenames (from the /system/build.prop: ro.product.device, ro.build.product)
      • block: Your boot image's path in your fstab. The fstab can be opened from the root of your device and it will look something like this:
        https://android.googlesource.com/device/huawei/angler/+/master/fstab.angler
        The first column is the value you want to set block to.
    5. After that, zip up the kernel and flash it!
      Code:
      zip -r9 kernel.zip * -x README.md kernel.zip


    Tips and tricks


    1. Remove GCC wrapper

    A lot of kernels from CAF include a Python script that will essentially turn on -Werror, causing your build to error at the most benign stuff. This is necessary with higher GCC versions as there are a lot more warnings.

    Here is the diff of what you need to change in the Makefile:
    Code:
    diff --git a/Makefile b/Makefile
    index 1aaa760f255f..bfccd5594630 100644
    --- a/Makefile
    +++ b/Makefile
    @@ -326,7 +326,7 @@ include $(srctree)/scripts/Kbuild.include
    
     AS		= $(CROSS_COMPILE)as
     LD		= $(CROSS_COMPILE)ld
    -REAL_CC		= $(CROSS_COMPILE)gcc
    +CC		= $(CROSS_COMPILE)gcc
     CPP		= $(CC) -E
     AR		= $(CROSS_COMPILE)ar
     NM		= $(CROSS_COMPILE)nm
    @@ -340,10 +340,6 @@ DEPMOD		= /sbin/depmod
     PERL		= perl
     CHECK		= sparse
    
    -# Use the wrapper for the compiler.  This wrapper scans for new
    -# warnings and causes the build to stop upon encountering them.
    -CC		= $(srctree)/scripts/gcc-wrapper.py $(REAL_CC)
    -
     CHECKFLAGS     := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
     		  -Wbitwise -Wno-return-void $(CF)
     CFLAGS_MODULE   =

    2. Using a higher level GCC toolchain

    Using a higher GCC toolchain (5.x, 6.x, 7.x or even 8.x) will require you to nuke the GCC wrapper script as above and use a unified GCC header file (pick the following if you have an include/linux/compiler-gcc#.h file):

    3.4/3.10: https://git.kernel.org/pub/scm/linu...h?id=a4a4f1cd733fe5b345db4e8cc19bb8868d562a8a
    3.18: https://git.kernel.org/pub/scm/linu...h?id=677fa15cd6d5b0843e7b9c58409f67d656b1ec2f

    You may get a lot of warnings but they are not entirely necessary to fix.


    3. Adding upstream Linux to kernel source

    Once you have gotten familiar with git and the compilation process, you should consider upstreaming your kernel. This will allow you to stay on top of CVE and bug fixes by staying up to date with the latest work of the Linux kernel developers.



    Receiving help

    I am happy to answer anything that I touched on in this guide. I may point you to another thread if it's better suited but I don't mind off topic (within reason) within the thread. I also want this to be a collaborative effort; other developers, if you have something to add, correct, or improve upon, please let me know!

    I am particular in how people ask for help. I do NOT respond to posts asking for a hand out ("How do I fix this?", "Please fix this!", etc.). I only respond to posts with clear logs and steps that you have tried. Basically, show me that you have read this guide and have a specific issue. I am not here to hold your hand through this, this is a developers' forum.
    20
    Compile Android Pie kernels for the Pixel (XL) and Pixel 2 (XL)

    Google added a new compat vDSO on Android Pie for 32-bit userspace, which requires a 32-bit toolchain (commit). As a result, if CROSS_COMPILE_ARM32 is not set, your build will error.

    After setting your main CROSS_COMPILE in step 1, clone this toolchain (or another compatible arm toolchain) next to the aarch64 one (if in the toolchain folder after running 'export CROSS_COMPILE', type 'cd ..'). Move into the 32-bit toolchain folder using cd then run
    Code:
    export CROSS_COMPILE_ARM32=$(pwd)/bin/<toolchain_prefix>
    Example:
    Code:
    export CROSS_COMPILE_ARM32=$(pwd)/bin/arm-linux-androideabi-

    Then continue on with the rest of the steps.
    9
    Introduction

    I am happy to answer anything that I touched on in this guide. I may point you to another thread if it's better suited but I don't mind off topic (within reason) within the thread. I also want this to be a collaborative effort; other developers, if you have something to add, correct, or improve upon, please let me know!

    I am particular in how people ask for help. I do NOT respond to posts asking for a hand out ("How do I fix this?", "Please fix this!", etc.). I only respond to posts with clear logs and steps that you have tried. Basically, show me that you have read this guide and have a specific issue. I am not here to hold your hand through this, this is a developers' forum.

    On a scale of 1-10 how much Off-Topic is allowed ? :highfive::laugh::silly::eek:





    Nice guide :good: :highfive: ..



    8
    @nathanchance thx for great work
    But how to build kernel with gcc 9.2.0
    could you please add manual for that

    Unfortunately, my time nowadays is limited between my job, school, and more important open source projects.

    However, compiling with GCC 9.2.0 is the same as compiling with GCC 4.9.4, you point the CROSS_COMPILE variable at the GCC 9.2.0 toolchain (or use PATH and update the CROSS_COMPILE prefix to match) and go from there. There are probably going to be new warnings that you will need to solve and there are potentially some other commits that you will need to pick to get a boot off.

    If anyone else wants to pitch in and write something up, I'd be happy to add it to the OP.
    7
    You're on fire with this kernel stuff:D

    Sent from my LEX727 using XDA Labs