[GUIDE]A Noob Guide On Building Your Own Custom Kernel (ARM & ARM64 & MTK)

What does the thread need ?


  • Total voters
    41
  • Poll closed .
Search This thread

3lambda

Inactive Recognized Contributor
Dec 6, 2012
1,755
1,460
Clermont-Ferrand
Xiaomi Mi 8
1. INTRODUCTION:​

This is a guide to build your own custom kernel. Although I'm still a "noob" at this,
I've struggled a lot to build one as all the guides which I followed were not very clear.
So I hope this will be clear enough and as noob friendly as possible!

You will learn how to:

- Build a kernel for arm and mediatek devices (arm64 coming soon)
- Add feature
- Basic use of git

Requirements:

- A computer running any distribution of linux 64 bit (Unbuntu, archlinux etc) as the Primary OS or a Virtual Machine (I used BBQ linux x64)
- Space on your HDD (On my Virtual Machine I set 40GB, set yours according to how many kernels you want to build) The minimum space for a kernel source (and its compiled code) is about 2 GB
- Minimal linux knowledge (Terminal, Commands etc)
- Your Brain
- And finally patience

2. SETTING UP YOUR BUILD ENVIRONMENT:​

- Open your terminal and type "sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2"

(The command can be different depending upon your linux distribution)

- Then type "gcc"

If "gcc" is already installed, you should see "gcc : fatal error : no input file"

- Then type "make"

If "make" is already installed, you should see "make: *** no target specified and no makefile found. stop."
(In your native language)

- Then type "git"

If "git" is already installed, you should see bunch of basic git commands

Now you're almost ready to start building your kernel!

Toolchains:

There are several types of toolchains (GCC, Linaro and few custom made ones)
Warning : Not every single device kernel will boot (or even compiles) with older or newer GCC

- For ARM:

We'll be using GCC 4.7 in this tutorial (link : https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/ )

-Open terminal and type: "mkdir kernel"(Type the name you want, I used "kernel")

-Then type "cd kernel" (the name which you used above)

-Then type "git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7"

-Wait till it finishes.


- For ARM 64:

For ARM 64 you need a 64 bit kernel compiler (there's "aarch64" in the name for telling that's a 64 bit compiler)

Exemple : https://releases.linaro.org/archive/13.07/components/toolchain/binaries/gcc-linaro-aarch64-linux-gnu-4.8-2013.07-1_linux.tar.xz


3.DOWNLOADING SOURCE FILES FOR YOUR DEVICE:​

Now you have to find a github that contains your kernel source.
Search on Google or XDA to find a kernel github repo for your device.

A kernel github looks like this: "https://github.com/atxoxx/android_kernel_samsung_msm8974/tree/xenomTW"

On the upper left side you can see branch: completed by a name
These are the different versions of the kernel/project (generally can be for testing, android version etc)

QUICK EXPLANATION OF FILES/FOLDERS:

- /arch/arm/configs : contains the config files for device (where you add option like new governors, features etc)
- /output/arch/arm/boot/ : Where zimage is stored (will explain that later)
- build.sh : Script to make the building much easier (will explain how it works later)
- /arm-cortex-linux-gnueabi-linaro_5.2-2015.11-2 : I put the toolchain in my kernel source making it easier to find (your kernel's toolchain name may be different)

If you don't have your kernel source yet, you need to download it.

Open terminal and make sure that you are in "kernel" folder (the one you previously created)

Then type in terminal : "git clone "URL of the github kernel" -b "name of the branch" "

For Example : "git clone https://github.com/atxoxx/android_kernel_samsung_msm8974 -b xenomTW"

Good! Now you have your kernel source!

4.BUILDING:​

For an easier way you can go to the location using your file explorer to : "/home/"name of your session"/kernel"

You'll see two folders (The Toolchain and The Kernel Source)

Go into your kernel source folder.

- For ARM:

Copy paste this:

#!/bin/bash

export ARCH=arm
export CROSS_COMPILE="path to your toolchain" (it have to end by something like "nameofarch-something-")

mkdir output

make -C $(pwd) O=output "name of defconfig and variant if needed"
make -j4 -C $(pwd) O=output

Explaination:

- #!/bin/bash: Tells the script to run in shell command
- export ARCH=arm: Defining which kernel architecture type it is (For example arm64 etc)
- export CROSS_COMPILE= : Locate where the toolchain is, it has to match the exact path to it and the dash ("-") in the end is really important ! (Almost everyone makes an error at this part!!!)
- mkdir output: Create a directory for storing compiled zimage
- make -C $(pwd) O=output : Defining defconfig for guiding kernel compilation (will explain later)
- make -j4 -C $(pwd) O=output: where the building start, "-j4" is how fast it'll compile, you have to setup this number according to your CPU !
- cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage: This one is for moving image into the second path (thanks @Has.007 for this infromation)

Example :

#!/bin/bash

export ARCH=arm
export CROSS_COMPILE=$(pwd)/arm-cortex-linux-gnueabi-linaro_5.2-2015.11-2/bin/arm-cortex-linux-gnueabi-

mkdir output

make -C $(pwd) O=output msm8974_sec_defconfig VARIANT_DEFCONFIG=msm8974_sec_ks01_skt_defconfig SELINUX_DEFCONFIG=selinux_defconfig
make -j4 -C $(pwd) O=output

cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage

- For ARM 64:

Copy paste this:

#!/bin/bash

export ARCH=arm64
export CROSS_COMPILE="path to your toolchain" (it have to end by something like "nameofarch-something-")

mkdir output

make -C $(pwd) O=output "name of defconfig and variant if needed"
make -j4 -C $(pwd) O=output

Explaination:

- #!/bin/bash: Tells the script to run in shell command
- export ARCH=arm64: Defining which kernel architecture type it is (For example arm64 etc)
- export CROSS_COMPILE= : Locate where the toolchain is, it has to match the exact path to it and the dash ("-") in the end is really important ! (Almost everyone makes an error at this part!!!)
- mkdir output: Create a directory for storing compiled zimage
- make -C $(pwd) O=output : Defining defconfig for guiding kernel compilation (will explain later)
- make -j4 -C $(pwd) O=output: where the building start, "-j4" is how fast it'll compile, you have to setup this number according to your CPU !
- cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage: This one is for moving image into the second path (thanks @Has.007 for this infromation)

Example :

#!/bin/bash

export ARCH=arm64
export CROSS_COMPILE=$(pwd)/gcc-linaro-aarch64-linux-gnu-4.8-2013.07-1_linux/bin/aarch64-linux-gnu-

mkdir output

make -C $(pwd) O=output msm8974_sec_defconfig VARIANT_DEFCONFIG=msm8974_sec_ks01_skt_defconfig SELINUX_DEFCONFIG=selinux_defconfig
make -j4 -C $(pwd) O=output

cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage

- For Mediatek:

Copy paste this:

#!/bin/bash

export CROSS_COMPILE="path to your toolchain" (it have to end by something like "nameofarch-something-")
export ARCH=arm ARCH_MTK_PLATFORM=
make "name of defconfig and variant if needed"
make -j4

Explaination:

- #!/bin/bash: Tells the script to run in shell command
- export CROSS_COMPILE= : Locate where the toolchain is, it has to match the exact path to it and the dash ("-") in the end is really important ! (Almost everyone makes an error at this part!!!)
- export ARCH=arm ARCH_MTK_PLATFORM=: Defining which kernel architecture type it is (For example arm64 etc) "ARCH_MTK_PLATFORM=" is for specifying which mediatek platform it is
- make _defconfig : Defining which defconfig to use (will explain later)
- make -j4: where the building starts, "-j4" is how fast it'll compile, you have to setup this number according to your CPU !

Example :

#!/bin/bash

export CROSS_COMPILE=$(pwd)/arm-eabi-4.8/bin/arm-eabi-
export ARCH=arm ARCH_MTK_PLATFORM=mt6580
make pixi4_4_8g1g_defconfig
make -j4

When these step are done make sure you are in kernel folder in terminal and type ". build.sh" (the dot at the beginning is essential don't forget it!)

If it compiles without any problems:

Wait till it finishes (it'll say something like "zimage is ready")

If you followed arm and arm64:

Then go to "/Output/arch/arm/boot/" to find your zimage.

If you followed mediatek:

Then go to "/arch/arm/boot/" to find your zimage.

Caution : Not all kernel build Zimage, it can build image or other compressed image

If in case you have any errors:

Check and see what it says, generally it'll tell you where the error is.

If the text is going too fast reduce the -j number as explained above.

For reference I compile with an AMD Phenom X4 3.4GHz,Samsung HDD and 8GB of RAM and it takes around 10min to build

It is recommanded to type in the terminal "make clean && mrproper" before compiling again​

5.MAKING THE KERNEL BOOT:​

You have 2 solutions here:

1) You can use @osm0sis anykernel method, which is explainded here: "https://forum.xda-developers.com/showthread.php?t=2670512" (A huge shoutout to him!)

OR

2) You can unpack the boot.img (from the same rom (CM, touchwizz,sense etc) and android version) and swap Zimage in it explained here : "https://forum.xda-developers.com/showthread.php?t=2073775" (thanks again to @osm0sis !)

Before flashing the kernel which you've made, backup your "stock" boot.img and Then flash your kernel and see if it boots!


6.HOW TO ADD FEATURE TO KERNEL WORK:​

Here starts the most interesting part! Now let's see how it works:

Basically you can add: Governors, IO Schedulers, Overclock the CPU & Many Tweaks...
Checkout the github section (Section 7) to see how to add them properly.

Here's an exemple for adding a governor (this one is called Intellimm) : https://github.com/gugu0das/android...mmit/7186ee60c171b06ea3a027e8383be392d3186bb1

The text in the blue box is the commit description (generally tells you about the changelog, general information and who originally made the commit)

The other text boxes tell you about where and which files have been modified/changed.

Everything in green indicates what has been added.

Everything in red indicates what has been deleted.

We can see in the first 2 text boxes that in "arch/arm/configs/" "msm8974_sec_defconfig" and "cm_msm8974_sec_defconfig" have been modified.

Between the lines 140 and 141 of this files this text has been added : "CONFIG_CPU_FREQ_GOV_INTELLIMM=y"
(This line is for enabling Intellimm when you're compiling your kernel)

Same technique applies to the other text boxes (what has been added and deleted and it's location)

Depending on the features you add, more or less files can be modified, added or deleted.

So to sum it up, a Commit let's you see all the changes which have been made and everything else!

7.GUIDE TO GITHUB:​

For this, I'll direct you over to this awsome guide made by @eagleeyetom !

8.GPL (IMPORTANT !!!):​

The Rules as they apply on XDA

As XDA has no legal power to uphold the GPL (and frankly we want to stay as far away from doing so as possible), we can’t force any of our users to abide by the GPL. However it is in XDA’s interests as well as the interests of our developer-base to ensure all GPL-derived materials hosted or linked on XDA comply fully with the GPL.

GPL-derived materials that do not come with the complete sources used to compile the GPL components are considered warez, and will be treated as such under forum rule 6 and 9.
If you use GPL components, but do not make any modifications to them whatsoever, you should provide a link to the original source of your GPL code.
Sources accompanying a release should be complete, and contain all the necessary source code for any modules, scripts or definition files. Complete sources will be defined as those which compile correctly and completely against the platform for which the software is distributed, and which contain any and all modifications made to the released General Public Licenced code. The source code supplied should be the exact version for which the source code is being requested, complete with all modifications.

EXAMPLE: Here’s a bit of code that could be used as a template to post your releases

<Kernel Or Author Name> <Kernel Nr>:
<Source>|<ReadMe>|<Credits>|<Other>

The Very Quick Summary of General Public License (GPL)

The text of the GPL Licence itself will be used to reach any final conclusion regarding any disputes over GPL Licenced materials. The above is a summary of what XDA expects of members using GPL code, and the complete text can be read at the GNU website.



The GPL states that anyone who modifies GPL licenced code is required to make available the sources used to compile it. This is to further improve and encourage collaborative work, as well as to ensure that the best code possible is produced, and to encourage peer-review of all work. This benefits both developers and end users in numerous ways, including:

Allowing anyone to verify the code they are trusting with their data, and its authenticity
Encouraging community collaboration to produce faster fixes and updates, and better code
Helping bring new developments from other devices and fields to your own, letting you benefit from new code that wouldn’t have been available without this sharing.
The GPL imparts great freedom for GPL end users. It ensures innovation is never stifled and no project is dependent upon any single developer.

It is in everyone’s interest for the GPL to be adhered to, as it gives us all better ROMs, better transparency, and a better atmosphere for developers to work together to make great code.

THANKS :

- @ravish_919 : For testing and correcting this guide :p
- @karkasss : As my friend and support ;)
- @gugu0das : For helping me a lot when I tried to build my kernel ;)
- @eagleeyetom : For his awsome github guide :)
- @kirito9 : Huge thanks to him for providing mediatek guide ! ;)
- @F4uzan : Huge thanks to him for giving me a lot of useful information to fill this guide ! ;)
- @sunilpaulmathew : For providing an advanced method to rename your kernel ! :D
- @RendyAK and @DroidThug : For correcting me about "#!/bin/bash" ;)
- All the developers for their hard work !
- XDA and The Community!
 
Last edited:

3lambda

Inactive Recognized Contributor
Dec 6, 2012
1,755
1,460
Clermont-Ferrand
Xiaomi Mi 8
TIPS AND TRICKS​

1. You can use a copy of a defconfig file with different setup :

Usage : Use a "stock" one and use another one with esperimental feature for testing without altering original defconfig

Exemple : copy "stock" defconfig and in copied one add a governor see if it compile and work

How to do : Create a second build.sh with modified defconfig name !

2. Change kernel name and version :

Simple method :
Edit this line "CONFIG_LOCALVERSION="-" after - in your defconfig

Exemple : CONFIG_LOCALVERSION="-XenomTW-3.2.6"

Advanced method :​
1. Add the name after "EXTRAVERSION" (e.g: EXTRAVERSION = -XenomTW-3.2.6) in the 'Makefile' (line 4) which will be found in the root folder..
2. Edit the last two sections ("const char linux_banner[] =" and "const char linux_proc_banner[] =") of 'init/version.c".

e.g. Change this to
const char linux_banner[] =
"Linux version " UTS_RELEASE " (" LINUX_COMPILE_BY "@"
LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION "\n";

const char linux_proc_banner[] =
"%s version %s"
" (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ")"
" (" LINUX_COMPILER ") %s\n";

To something like this :
const char linux_banner[] =
"Linux version " UTS_RELEASE " (XenomTW)"
"(" LINUX_COMPILER ") " UTS_VERSION "\n";

const char linux_proc_banner[] =
"%s version %s"
" (XenomTW)"
" (" LINUX_COMPILER ") %s\n";

More to come...
 
Last edited:

kirito9

Inactive Recognized Contributor
Oct 30, 2013
3,127
1,363
Hey, nice work to see an updated guide. Do you think it will be possible to include Mediatek(MTK) support? I'll provide details through PM how I build my kernel and how MTK differs. I'm still a noob but have compiled enough to merge this guide.

You can probably add a page dedicated to MTK :). Guides aren't my forte :").
 

3lambda

Inactive Recognized Contributor
Dec 6, 2012
1,755
1,460
Clermont-Ferrand
Xiaomi Mi 8
Hey, nice work to see an updated guide. Do you think it will be possible to include Mediatek(MTK) support? I'll provide details through PM how I build my kernel and how MTK differs. I'm still a noob but have compiled enough to merge this guide.

You can probably add a page dedicated to MTK :). Guides aren't my forte :").

Of course could be really cool !
I'll credit you etc :)
Also I'll try later to support arm64 too :)
 

ravish_919

Senior Member
Jul 11, 2010
789
268
Thank you for this helpful guide! Will be waiting for the "Adding Features" section! Quite the work you have done! :good:
 
  • Like
Reactions: 3lambda

The Hard Gamer

Senior Member
Feb 11, 2016
686
931
23
Gotham
Of course could be really cool !
I'll credit you etc :)
Also I'll try later to support arm64 too :)

I got some Unwinding errors related to vmlinux.o and built-in.o
Device_Lenovo A6000
Kernel Sources_ https://github.com/varunhardgamer
repo name- kernel lenovo a6000
used toolchain_ arm-linux-androideabi-4.9 from android NDK
It would be grateful if you can help me
Thanks:good:
 
  • Like
Reactions: JovanSijacki

3lambda

Inactive Recognized Contributor
Dec 6, 2012
1,755
1,460
Clermont-Ferrand
Xiaomi Mi 8
  • Like
Reactions: The Hard Gamer

Has.007

Senior Member
Sep 13, 2015
1,093
4,377
Curepipe
Code:
cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage
This
Will copy the Image file from output folder to the arch/arm/boot folder so the users can find it directly from there.
cp is used to copy something from the command line.
mv is used to rename or move a file.
Anyways nice start & nice guide! :good: ;)
 
  • Like
Reactions: 3lambda

i'mrunningoutofusernames

Senior Member
Mar 22, 2015
222
48
Linaro

I would appreciate it a ton if you could show us how to download and use a different toolchain (especially linaro) to compile the kernel. You did amazing work on this guide!
 

sunilpaulmathew

Recognized Developer
2. Change kernel name and version :

Simply edit this line "CONFIG_LOCALVERSION="-" after - in your defconfig

Exemple : CONFIG_LOCALVERSION="-XenomTW-3.2.6"

Thanks for the awesome guide. I really wish to see a similar guide before few month so that I was able to save a lot of time :p.
Btw, few more tips for adding kernel name from my side...
1. Add the name after "EXTRAVERSION" (e.g: EXTRAVERSION = -XenomTW-3.2.6) in the 'Makefile' (line 4) which will be found in the root folder..
2. Edit the last two sections ("const char linux_banner[] =" and "const char linux_proc_banner[] =") of 'init/version.c".
e.g. Change this to
const char linux_banner[] =
"Linux version " UTS_RELEASE " (" LINUX_COMPILE_BY "@"
LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION "\n";

const char linux_proc_banner[] =
"%s version %s"
" (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ")"
" (" LINUX_COMPILER ") %s\n";
some thing like this
const char linux_banner[] =
"Linux version " UTS_RELEASE " (XenomTW)"
"(" LINUX_COMPILER ") " UTS_VERSION "\n";

const char linux_proc_banner[] =
"%s version %s"
" (XenomTW)"
" (" LINUX_COMPILER ") %s\n";;
 
  • Like
Reactions: kirito9 and 3lambda

Top Liked Posts

  • There are no posts matching your filters.
  • 86
    1. INTRODUCTION:​

    This is a guide to build your own custom kernel. Although I'm still a "noob" at this,
    I've struggled a lot to build one as all the guides which I followed were not very clear.
    So I hope this will be clear enough and as noob friendly as possible!

    You will learn how to:

    - Build a kernel for arm and mediatek devices (arm64 coming soon)
    - Add feature
    - Basic use of git

    Requirements:

    - A computer running any distribution of linux 64 bit (Unbuntu, archlinux etc) as the Primary OS or a Virtual Machine (I used BBQ linux x64)
    - Space on your HDD (On my Virtual Machine I set 40GB, set yours according to how many kernels you want to build) The minimum space for a kernel source (and its compiled code) is about 2 GB
    - Minimal linux knowledge (Terminal, Commands etc)
    - Your Brain
    - And finally patience

    2. SETTING UP YOUR BUILD ENVIRONMENT:​

    - Open your terminal and type "sudo apt-get install -y build-essential kernel-package libncurses5-dev bzip2"

    (The command can be different depending upon your linux distribution)

    - Then type "gcc"

    If "gcc" is already installed, you should see "gcc : fatal error : no input file"

    - Then type "make"

    If "make" is already installed, you should see "make: *** no target specified and no makefile found. stop."
    (In your native language)

    - Then type "git"

    If "git" is already installed, you should see bunch of basic git commands

    Now you're almost ready to start building your kernel!

    Toolchains:

    There are several types of toolchains (GCC, Linaro and few custom made ones)
    Warning : Not every single device kernel will boot (or even compiles) with older or newer GCC

    - For ARM:

    We'll be using GCC 4.7 in this tutorial (link : https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/ )

    -Open terminal and type: "mkdir kernel"(Type the name you want, I used "kernel")

    -Then type "cd kernel" (the name which you used above)

    -Then type "git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7"

    -Wait till it finishes.


    - For ARM 64:

    For ARM 64 you need a 64 bit kernel compiler (there's "aarch64" in the name for telling that's a 64 bit compiler)

    Exemple : https://releases.linaro.org/archive/13.07/components/toolchain/binaries/gcc-linaro-aarch64-linux-gnu-4.8-2013.07-1_linux.tar.xz


    3.DOWNLOADING SOURCE FILES FOR YOUR DEVICE:​

    Now you have to find a github that contains your kernel source.
    Search on Google or XDA to find a kernel github repo for your device.

    A kernel github looks like this: "https://github.com/atxoxx/android_kernel_samsung_msm8974/tree/xenomTW"

    On the upper left side you can see branch: completed by a name
    These are the different versions of the kernel/project (generally can be for testing, android version etc)

    QUICK EXPLANATION OF FILES/FOLDERS:

    - /arch/arm/configs : contains the config files for device (where you add option like new governors, features etc)
    - /output/arch/arm/boot/ : Where zimage is stored (will explain that later)
    - build.sh : Script to make the building much easier (will explain how it works later)
    - /arm-cortex-linux-gnueabi-linaro_5.2-2015.11-2 : I put the toolchain in my kernel source making it easier to find (your kernel's toolchain name may be different)

    If you don't have your kernel source yet, you need to download it.

    Open terminal and make sure that you are in "kernel" folder (the one you previously created)

    Then type in terminal : "git clone "URL of the github kernel" -b "name of the branch" "

    For Example : "git clone https://github.com/atxoxx/android_kernel_samsung_msm8974 -b xenomTW"

    Good! Now you have your kernel source!

    4.BUILDING:​

    For an easier way you can go to the location using your file explorer to : "/home/"name of your session"/kernel"

    You'll see two folders (The Toolchain and The Kernel Source)

    Go into your kernel source folder.

    - For ARM:

    Copy paste this:

    #!/bin/bash

    export ARCH=arm
    export CROSS_COMPILE="path to your toolchain" (it have to end by something like "nameofarch-something-")

    mkdir output

    make -C $(pwd) O=output "name of defconfig and variant if needed"
    make -j4 -C $(pwd) O=output

    Explaination:

    - #!/bin/bash: Tells the script to run in shell command
    - export ARCH=arm: Defining which kernel architecture type it is (For example arm64 etc)
    - export CROSS_COMPILE= : Locate where the toolchain is, it has to match the exact path to it and the dash ("-") in the end is really important ! (Almost everyone makes an error at this part!!!)
    - mkdir output: Create a directory for storing compiled zimage
    - make -C $(pwd) O=output : Defining defconfig for guiding kernel compilation (will explain later)
    - make -j4 -C $(pwd) O=output: where the building start, "-j4" is how fast it'll compile, you have to setup this number according to your CPU !
    - cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage: This one is for moving image into the second path (thanks @Has.007 for this infromation)

    Example :

    #!/bin/bash

    export ARCH=arm
    export CROSS_COMPILE=$(pwd)/arm-cortex-linux-gnueabi-linaro_5.2-2015.11-2/bin/arm-cortex-linux-gnueabi-

    mkdir output

    make -C $(pwd) O=output msm8974_sec_defconfig VARIANT_DEFCONFIG=msm8974_sec_ks01_skt_defconfig SELINUX_DEFCONFIG=selinux_defconfig
    make -j4 -C $(pwd) O=output

    cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage

    - For ARM 64:

    Copy paste this:

    #!/bin/bash

    export ARCH=arm64
    export CROSS_COMPILE="path to your toolchain" (it have to end by something like "nameofarch-something-")

    mkdir output

    make -C $(pwd) O=output "name of defconfig and variant if needed"
    make -j4 -C $(pwd) O=output

    Explaination:

    - #!/bin/bash: Tells the script to run in shell command
    - export ARCH=arm64: Defining which kernel architecture type it is (For example arm64 etc)
    - export CROSS_COMPILE= : Locate where the toolchain is, it has to match the exact path to it and the dash ("-") in the end is really important ! (Almost everyone makes an error at this part!!!)
    - mkdir output: Create a directory for storing compiled zimage
    - make -C $(pwd) O=output : Defining defconfig for guiding kernel compilation (will explain later)
    - make -j4 -C $(pwd) O=output: where the building start, "-j4" is how fast it'll compile, you have to setup this number according to your CPU !
    - cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage: This one is for moving image into the second path (thanks @Has.007 for this infromation)

    Example :

    #!/bin/bash

    export ARCH=arm64
    export CROSS_COMPILE=$(pwd)/gcc-linaro-aarch64-linux-gnu-4.8-2013.07-1_linux/bin/aarch64-linux-gnu-

    mkdir output

    make -C $(pwd) O=output msm8974_sec_defconfig VARIANT_DEFCONFIG=msm8974_sec_ks01_skt_defconfig SELINUX_DEFCONFIG=selinux_defconfig
    make -j4 -C $(pwd) O=output

    cp output/arch/arm/boot/Image $(pwd)/arch/arm/boot/zImage

    - For Mediatek:

    Copy paste this:

    #!/bin/bash

    export CROSS_COMPILE="path to your toolchain" (it have to end by something like "nameofarch-something-")
    export ARCH=arm ARCH_MTK_PLATFORM=
    make "name of defconfig and variant if needed"
    make -j4

    Explaination:

    - #!/bin/bash: Tells the script to run in shell command
    - export CROSS_COMPILE= : Locate where the toolchain is, it has to match the exact path to it and the dash ("-") in the end is really important ! (Almost everyone makes an error at this part!!!)
    - export ARCH=arm ARCH_MTK_PLATFORM=: Defining which kernel architecture type it is (For example arm64 etc) "ARCH_MTK_PLATFORM=" is for specifying which mediatek platform it is
    - make _defconfig : Defining which defconfig to use (will explain later)
    - make -j4: where the building starts, "-j4" is how fast it'll compile, you have to setup this number according to your CPU !

    Example :

    #!/bin/bash

    export CROSS_COMPILE=$(pwd)/arm-eabi-4.8/bin/arm-eabi-
    export ARCH=arm ARCH_MTK_PLATFORM=mt6580
    make pixi4_4_8g1g_defconfig
    make -j4

    When these step are done make sure you are in kernel folder in terminal and type ". build.sh" (the dot at the beginning is essential don't forget it!)

    If it compiles without any problems:

    Wait till it finishes (it'll say something like "zimage is ready")

    If you followed arm and arm64:

    Then go to "/Output/arch/arm/boot/" to find your zimage.

    If you followed mediatek:

    Then go to "/arch/arm/boot/" to find your zimage.

    Caution : Not all kernel build Zimage, it can build image or other compressed image

    If in case you have any errors:

    Check and see what it says, generally it'll tell you where the error is.

    If the text is going too fast reduce the -j number as explained above.

    For reference I compile with an AMD Phenom X4 3.4GHz,Samsung HDD and 8GB of RAM and it takes around 10min to build

    It is recommanded to type in the terminal "make clean && mrproper" before compiling again​

    5.MAKING THE KERNEL BOOT:​

    You have 2 solutions here:

    1) You can use @osm0sis anykernel method, which is explainded here: "https://forum.xda-developers.com/showthread.php?t=2670512" (A huge shoutout to him!)

    OR

    2) You can unpack the boot.img (from the same rom (CM, touchwizz,sense etc) and android version) and swap Zimage in it explained here : "https://forum.xda-developers.com/showthread.php?t=2073775" (thanks again to @osm0sis !)

    Before flashing the kernel which you've made, backup your "stock" boot.img and Then flash your kernel and see if it boots!


    6.HOW TO ADD FEATURE TO KERNEL WORK:​

    Here starts the most interesting part! Now let's see how it works:

    Basically you can add: Governors, IO Schedulers, Overclock the CPU & Many Tweaks...
    Checkout the github section (Section 7) to see how to add them properly.

    Here's an exemple for adding a governor (this one is called Intellimm) : https://github.com/gugu0das/android...mmit/7186ee60c171b06ea3a027e8383be392d3186bb1

    The text in the blue box is the commit description (generally tells you about the changelog, general information and who originally made the commit)

    The other text boxes tell you about where and which files have been modified/changed.

    Everything in green indicates what has been added.

    Everything in red indicates what has been deleted.

    We can see in the first 2 text boxes that in "arch/arm/configs/" "msm8974_sec_defconfig" and "cm_msm8974_sec_defconfig" have been modified.

    Between the lines 140 and 141 of this files this text has been added : "CONFIG_CPU_FREQ_GOV_INTELLIMM=y"
    (This line is for enabling Intellimm when you're compiling your kernel)

    Same technique applies to the other text boxes (what has been added and deleted and it's location)

    Depending on the features you add, more or less files can be modified, added or deleted.

    So to sum it up, a Commit let's you see all the changes which have been made and everything else!

    7.GUIDE TO GITHUB:​

    For this, I'll direct you over to this awsome guide made by @eagleeyetom !

    8.GPL (IMPORTANT !!!):​

    The Rules as they apply on XDA

    As XDA has no legal power to uphold the GPL (and frankly we want to stay as far away from doing so as possible), we can’t force any of our users to abide by the GPL. However it is in XDA’s interests as well as the interests of our developer-base to ensure all GPL-derived materials hosted or linked on XDA comply fully with the GPL.

    GPL-derived materials that do not come with the complete sources used to compile the GPL components are considered warez, and will be treated as such under forum rule 6 and 9.
    If you use GPL components, but do not make any modifications to them whatsoever, you should provide a link to the original source of your GPL code.
    Sources accompanying a release should be complete, and contain all the necessary source code for any modules, scripts or definition files. Complete sources will be defined as those which compile correctly and completely against the platform for which the software is distributed, and which contain any and all modifications made to the released General Public Licenced code. The source code supplied should be the exact version for which the source code is being requested, complete with all modifications.

    EXAMPLE: Here’s a bit of code that could be used as a template to post your releases

    <Kernel Or Author Name> <Kernel Nr>:
    <Source>|<ReadMe>|<Credits>|<Other>

    The Very Quick Summary of General Public License (GPL)

    The text of the GPL Licence itself will be used to reach any final conclusion regarding any disputes over GPL Licenced materials. The above is a summary of what XDA expects of members using GPL code, and the complete text can be read at the GNU website.



    The GPL states that anyone who modifies GPL licenced code is required to make available the sources used to compile it. This is to further improve and encourage collaborative work, as well as to ensure that the best code possible is produced, and to encourage peer-review of all work. This benefits both developers and end users in numerous ways, including:

    Allowing anyone to verify the code they are trusting with their data, and its authenticity
    Encouraging community collaboration to produce faster fixes and updates, and better code
    Helping bring new developments from other devices and fields to your own, letting you benefit from new code that wouldn’t have been available without this sharing.
    The GPL imparts great freedom for GPL end users. It ensures innovation is never stifled and no project is dependent upon any single developer.

    It is in everyone’s interest for the GPL to be adhered to, as it gives us all better ROMs, better transparency, and a better atmosphere for developers to work together to make great code.

    THANKS :

    - @ravish_919 : For testing and correcting this guide :p
    - @karkasss : As my friend and support ;)
    - @gugu0das : For helping me a lot when I tried to build my kernel ;)
    - @eagleeyetom : For his awsome github guide :)
    - @kirito9 : Huge thanks to him for providing mediatek guide ! ;)
    - @F4uzan : Huge thanks to him for giving me a lot of useful information to fill this guide ! ;)
    - @sunilpaulmathew : For providing an advanced method to rename your kernel ! :D
    - @RendyAK and @DroidThug : For correcting me about "#!/bin/bash" ;)
    - All the developers for their hard work !
    - XDA and The Community!
    32
    TIPS AND TRICKS​

    1. You can use a copy of a defconfig file with different setup :

    Usage : Use a "stock" one and use another one with esperimental feature for testing without altering original defconfig

    Exemple : copy "stock" defconfig and in copied one add a governor see if it compile and work

    How to do : Create a second build.sh with modified defconfig name !

    2. Change kernel name and version :

    Simple method :
    Edit this line "CONFIG_LOCALVERSION="-" after - in your defconfig

    Exemple : CONFIG_LOCALVERSION="-XenomTW-3.2.6"

    Advanced method :​
    1. Add the name after "EXTRAVERSION" (e.g: EXTRAVERSION = -XenomTW-3.2.6) in the 'Makefile' (line 4) which will be found in the root folder..
    2. Edit the last two sections ("const char linux_banner[] =" and "const char linux_proc_banner[] =") of 'init/version.c".

    e.g. Change this to
    const char linux_banner[] =
    "Linux version " UTS_RELEASE " (" LINUX_COMPILE_BY "@"
    LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION "\n";

    const char linux_proc_banner[] =
    "%s version %s"
    " (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ")"
    " (" LINUX_COMPILER ") %s\n";

    To something like this :
    const char linux_banner[] =
    "Linux version " UTS_RELEASE " (XenomTW)"
    "(" LINUX_COMPILER ") " UTS_VERSION "\n";

    const char linux_proc_banner[] =
    "%s version %s"
    " (XenomTW)"
    " (" LINUX_COMPILER ") %s\n";

    More to come...
    9
    Thread Updated with mediatek support !
    Keep in mind that's a basic guide on how to compile kernel :)
    It will be better over time, with more details, tips and tricks etc
    Also arm64 support is coming soon

    I'll make video soon too, I need to train my english accent before
    And thanks to all people who contributed or helped this guide ! ;)
    stay tuned
    8
    Ok guys !
    I was really busy these past times, I train myself to have a better english accent for making videos
    Also 64 bits compiling will come soon stay tuned :)
    8
    What people think about videos too ?
    Like a series of guide
    For example one for basic build
    One for explaining how to implement a feature
    One that explain common issue/bug etc ?