[REFERENCE] How to compile an Android kernel

Search This thread

KRafi

Member
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.
Getting the following issue. It's trying to compile an x86 kernel! :/

$ make O=~/Documents/UBports/Kernel -j$(nproc --
all)
make[1]: Entering directory '/home/user/Documents/UBports/Kernel'
GEN ./Makefile
scripts/kconfig/conf --silentoldconfig Kconfig
drivers/input/touchscreen/ft5446/Kconfig:5:warning: ignoring type redefinition of 'TOUCHSCREEN_FT5446' from 't
ristate' to 'boolean'
warning: (USB_CI13XXX_MSM) selects USB_MSM_OTG which has unmet direct dependencies (USB_SUPPORT && (USB || USB
_GADGET) && (ARCH_QCOM || COMPILE_TEST))
*
* Restart config...
*
*
* Linux/x86 4.9.193 Kernel Configuration
*
64-bit kernel (64BIT) [Y/n/?] (NEW)
 

newbit

Senior Member
Nov 16, 2008
150
64
Getting the following issue. It's trying to compile an x86 kernel! :/

$ make O=~/Documents/UBports/Kernel -j$(nproc --
all)
make[1]: Entering directory '/home/user/Documents/UBports/Kernel'
GEN ./Makefile
scripts/kconfig/conf --silentoldconfig Kconfig
drivers/input/touchscreen/ft5446/Kconfig:5:warning: ignoring type redefinition of 'TOUCHSCREEN_FT5446' from 't
ristate' to 'boolean'
warning: (USB_CI13XXX_MSM) selects USB_MSM_OTG which has unmet direct dependencies (USB_SUPPORT && (USB || USB
_GADGET) && (ARCH_QCOM || COMPILE_TEST))
*
* Restart config...
*
*
* Linux/x86 4.9.193 Kernel Configuration
*
64-bit kernel (64BIT) [Y/n/?] (NEW)

Receiving help

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.


What is your question again? Could you please fulfill the above-mentioned requirements to receive help?!
 

KRafi

Member
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.
Hlw. Which kernel config should I enable to include Device Tree Overlay in arm architecture. In arm64, I have `CONFIG_BUILD_ARM^$_DT_OVERLAY`, but i can't find a similar config option for arm.
 

mistersmee

Member
  • Jan 14, 2019
    46
    7
    17
    Kolhapur, India
    Moto G9 Power
    Excellent guide, thanks! I was able to plod along with building the kernel for my Moto G9 Power "cebu", fixing the broken symlinks in the kernel tarball which somehow occurred, when I snagged this error
    Code:
    In file included from /home/andbuild/Downloads/and/kernel/motorola/cebu/kernel/sched/core.c:23:0:
    /home/andbuild/Downloads/and/kernel/motorola/cebu/kernel/sched/walt.h:
    In function 'walt_task_dump': /home/andbuild/Downloads/and/kernel/motorola/cebu/kernel/sched/walt.h:351:2:
    warning: format '%llu' expects argument of type 'long long unsigned int', but argument 3 has type 'pid_t' [-Wformat=]
    error, forbidden warning: walt.h:351
    
    make[3]: *** [/home/andbuild/Downloads/and/kernel/motorola/cebu/scripts/Makefile.build:338: kernel/sched/core.o] Error 1
    
    make[2]: *** [/home/andbuild/Downloads/and/kernel/motorola/cebu/scripts/Makefile.build:643: kernel/sched] Error 2
    
    make[1]: *** [/home/andbuild/Downloads/and/kernel/motorola/cebu/Makefile:1143: kernel] Error 2
    I've been using all of my Google-fu, and yet I can't resolve this, and I'm not a C expert, so I can't hack at the .c,.h files myself, I wouldn't even know where to start.
    Has anyone had something similar happen to them, can anyone help me out here? The kernel source is https://github.com/MotorolaMobilityLLC/kernel-msm/tree/MMI-QZC30.Q4-22-57


    EDIT: I just noticed that the makefile for the kernel is using the GCC wrapper that adds -Werror, erroring out at the slightest error. That might be it, though I have no idea if this particular error is critical or not. Actually, that is definitely it, I'm building on Arch, with the latest gcc 10.x toolchain, so yeah. I didn't disable that. Let me do that and see how that goes.
     
    Last edited:

    mistersmee

    Member
  • Jan 14, 2019
    46
    7
    17
    Kolhapur, India
    Moto G9 Power
    Excellent guide, thanks! I was able to plod along with building the kernel for my Moto G9 Power "cebu", fixing the broken symlinks in the kernel tarball which somehow occurred, when I snagged this error
    Code:
    In file included from /home/andbuild/Downloads/and/kernel/motorola/cebu/kernel/sched/core.c:23:0:
    /home/andbuild/Downloads/and/kernel/motorola/cebu/kernel/sched/walt.h:
    In function 'walt_task_dump': /home/andbuild/Downloads/and/kernel/motorola/cebu/kernel/sched/walt.h:351:2:
    warning: format '%llu' expects argument of type 'long long unsigned int', but argument 3 has type 'pid_t' [-Wformat=]
    error, forbidden warning: walt.h:351
    
    make[3]: *** [/home/andbuild/Downloads/and/kernel/motorola/cebu/scripts/Makefile.build:338: kernel/sched/core.o] Error 1
    
    make[2]: *** [/home/andbuild/Downloads/and/kernel/motorola/cebu/scripts/Makefile.build:643: kernel/sched] Error 2
    
    make[1]: *** [/home/andbuild/Downloads/and/kernel/motorola/cebu/Makefile:1143: kernel] Error 2
    I've been using all of my Google-fu, and yet I can't resolve this, and I'm not a C expert, so I can't hack at the .c,.h files myself, I wouldn't even know where to start.
    Has anyone had something similar happen to them, can anyone help me out here? The kernel source is https://github.com/MotorolaMobilityLLC/kernel-msm/tree/MMI-QZC30.Q4-22-57


    EDIT: I just noticed that the makefile for the kernel is using the GCC wrapper that adds -Werror, erroring out at the slightest error. That might be it, though I have no idea if this particular error is critical or not. Actually, that is definitely it, I'm building on Arch, with the latest gcc 10.x toolchain, so yeah. I didn't disable that. Let me do that and see how that goes.

    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.
     

    newbit

    Senior Member
    Nov 16, 2008
    150
    64
    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: 6
    Last edited:
    • Like
    Reactions: h3xmind

    mistersmee

    Member
  • Jan 14, 2019
    46
    7
    17
    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
    13
    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
    13
    0

    Attachments

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

    Flacko89

    Member
    Jul 12, 2018
    13
    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

    New member
    Aug 1, 2017
    3
    0
    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: 8

    trueToastedCode

    Senior Member
    Mar 14, 2019
    61
    8
    Cologne
    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)
     
    Feb 21, 2020
    7
    0
    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?
     

    Top Liked Posts

    • There are no posts matching your filters.
    • 211
      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.
      19
      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.
      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
      @nathanchance Thank you so much for this guide
      I am getting errors trying to compile U11 kernel forked from ElementalX by @flar2
      https://imgur.com/a/tE5Elfw

      Code:
      make clean
      lib/Makefile:17: *** recipe commences before first target.  Stop.
      Makefile:1213: recipe for target '_clean_lib' failed
      make: *** [_clean_lib] Error 2

      Just skip the make clean for now and go straight to make.

      everything worked fine but after 5min got this error please help
      Code:
      kernel/built-in.o: In function `sched_slice.isra.75':
      jump_label.c:(.text+0x3d8e0): undefined reference to `__multi3'
      jump_label.c:(.text+0x3d8e0): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      kernel/built-in.o: In function `update_curr':
      jump_label.c:(.text+0x3de48): undefined reference to `__multi3'
      jump_label.c:(.text+0x3de48): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      kernel/built-in.o: In function `task_dead_fair':
      jump_label.c:(.text+0x3e490): undefined reference to `__multi3'
      jump_label.c:(.text+0x3e490): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      jump_label.c:(.text+0x3e4ac): undefined reference to `__multi3'
      jump_label.c:(.text+0x3e4ac): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      jump_label.c:(.text+0x3e56c): undefined reference to `__multi3'
      jump_label.c:(.text+0x3e56c): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      kernel/built-in.o:jump_label.c:(.text+0x3e590): more undefined references to `__multi3' follow
      kernel/built-in.o: In function `task_dead_fair':
      jump_label.c:(.text+0x3e590): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      kernel/built-in.o: In function `migrate_task_rq_fair':
      jump_label.c:(.text+0x3e6cc): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      jump_label.c:(.text+0x3e6e8): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      jump_label.c:(.text+0x3e7a8): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      jump_label.c:(.text+0x3e7cc): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__multi3'
      kernel/built-in.o: In function `check_preempt_wakeup':
      jump_label.c:(.text+0x3ea44): additional relocation overflows omitted from the output
      Makefile:986: recipe for target 'vmlinux' failed
      make: *** [vmlinux] Error 1
      please help

      Normally I just ignore posts like this since you clearly didn't follow the "Receiving help" section but I want to make this an educational opportunity. How in the world am I supposed to help you when you give me quite literally zero information?

      Nothing about device, toolchain, kernel source link.

      Please realize I'm not a wizard or magician. I cannot read your mind. My initial guess is your kernel source is broken or your toolchain is. Please come back with more info if you want my help.
      6
      You're on fire with this kernel stuff:D

      Sent from my LEX727 using XDA Labs