[Android] akhilnarang:
Kernel Compilation

As many of you'll already know, kernel of an operating system

Android devices use a modified version of the linux kernel

Firstly, most important thing to note, in that the Linux Kernel is licensed under the GNU GPL (General Public License), which can be found here

Basically, you must always keep your work on the kernel open-source, and display the link to it when publishing your work on XDA Threads, etc

In order to do this, make sure you fork, or push the kernel source you're working on to your GitHub account so that you can push the changes you're making

Make sure you give proper authorship, as explained in post #2

If unable to, give credits to author in commit message, but don't kang please [emoji14]

Please try to have at least a basic knowledge of C, so that you can fix errors

Else, use Google before asking in thread, there's a huge chance someone has already gotten and solved it :v

So now, getting to work

I will give an example using Android One Device (sprout4&&sprout8)

The kernel source be found here(branch android-mediatek-lollipop-mr1 for 5.1,or something, not sure, check), and also here, branch cm-12.1 for 5.1 ROMs

So fork it to your GitHub account, or clone from AOSP and push to your git

I'd recommend the one from CM git, as it usually has small bugs fixed compared to stock

In my device's case, a whole lotta broken stuff is fixed, so I would take from cm

So start off, by installing all dependencies as shown in 1st post

$ git clone https://github.com/akhilnarang/android_kernel_mediatek_sprout -b cm-12.1 sprout-kernel

Replace the link with your own, ssh if you wish, as explained in #2

You must specify -b cm-12.1,as the default is cm-11.0, and you will end up will KitKat source!

The last parameter is optional, it saves it in that directory, in this case sprout-kernel

Now, all kernel source's have a file called a defconfig, or default configuration, that is device-specific

You will find it in arch/*your devices architecture*/configs

Your device may be arm, arm64, x86

Make sure you remember the name of the defconfig, you can rename it too [emoji14]

If you would like to give your kernel a name, simplest way would be to look for the CONFIG_LOCALVERSION in the defconfig

Make it something like

CONFIG_LOCALVERSION="-Your-Kernel-Name"

Anyway, so now you need a toolchain

For starting off, we will use GCC 4.8 from Google

For ARM devices

$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/

For aarch64/arm64

$ git clone https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.8/

For x86 I'm not sure, go have a look here

Now remember where you saved it

Now open the file called "Makefile" in the root of kernel source

Look for a line that has ARCH?=

that dosen't start with a # (lines starting with # are comments)

And make it

ARCH ?= arm

or

ARCH ?= arm64

And look for CROSS_COMPILE ?=

make it

CROSS_COMPILE ?= path/to/downloaded/toolchain/bin/arm-eabi-

for arm devices

CROSS_COMPILE ?= path/to/downloaded/toolchain/bin/aarch64-android-linux-

for arm64 devices

This may differ on Custom toolchains, etc, you can find out about that by viewing the commits of different kernels, and going through various threads

I'm only gonna explain how to compile the kernel, not how to add stuff (at least for now)

If you wish now, you can refer to #2, and commit and push these changes [emoji14]

Now run

$ make cyanogenmod_sprout_defconfig

That's what my defconfig is called, yours maybe called something else [emoji14]

And now finally to start the build

First, check how many cores/threads your CPU has

A quad core Intel, an octa core AMD, will have 8

So if you have one, run

$ make -j8

This will compile the code on all 8 threads parallely

However this makes debugging errors harder

If you have an error, just run

$ make

to reduce output and identify error

Anyway, the process generate a zImage in arch/arm/boot/zImage

or arch/arm64/boot/zImage

This is a binary file

If you check the zip of your favorite custom kernel, you will find one too

In most cases, you can simply replace it with this, update the updater-script, and flash :D

Most of these zips use anykernel format, meaning it utilizes ramdisk from existing kernel, so if you find such a zip for your device, you're good to go

Else unpack your boot.img and add this add the file boot.img-zImage, repack, flash

If you don't understand any of these terms [emoji14], please google about them first, and then if you are unable to find out ask here please :D

Enjoy :D

More stuff will be added soon