[Android General] Saumitra Yadav:
Hey Guys,

With All the awesome development going on in the Android world. There Are Many newbies who want to strat development but they exactly Dont Know How To begin with or from where to strat with. So I will Write a guide on how to compile your own kernel from source :)

=================

What Is A Kernel???

=================

The operating system of a device is the part of the device responsible for basic use and administration. This includes the kernel and device drivers, boot loader, command shell or other user interface, and basic file and system utilities. Whereas the user interface is the outermost portition of the operating system, kernel is the innermost. It is the core internals, the software that provides basic services for all other parts of the system, manages hardware and distributes system resources.

============================

Prerequisites To Compile A kernel

============================

1.A Unix Based Os(Ubuntu Recommended Both 32-bit and 64-bit are good to compile a kernel)

2.A few packages, to let the system know that we are compiling a kernel :p

3. The Source Code of kernel for your Device :p

Ok, So Now Lets Begin :)

==================

Packages Required:

==================

Ok So Now Open The Terminal And Copy Paste This Code:

sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 openjdk-6-jdk tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386 git

Okay SO Now If You Are using Ubuntu12.04 , you need to create links of certain files

sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so

Okay now we are done with installing packages :)

SO Now we need java

Open a terminal and Run This Code:

Then Run This:

Then Type This:

So Now You Have all the needed packages to compile a kernel :)

Next you need the bootimagetools,source code of kernel and Androidtoolchainsto compile a kernel :)

Bootimagetools:https://github.com/SaumitraYadav/Boot-Image-tools

AndroidToolchains:https://github.com/SaumitraYadav/Android_Toolchains

Okay once you have Downloaded everything ... now Lets Start Compiling A Kernel :)

Ok So Fireup A Terminal :

mkdir kernel

This Will Create a Directory named kernel in your Home Folder

Now extract your kernel Source in this directory

Then Run This:

cd kernel

Now You Have To Edit the Make File:

gedit Makefile

The Make file will open.........look out for this and change CROSS_COMPILE= "your toolchain path"

EXAMPLE:

CROSS_COMPILE?= /home/saumitra/Downloads/Toolchains/arm-eabi-4.4.3/bin/arm-eabi-

this will open up Makefile in gedit, now set the path where youn have downloaded and extracted the toolchains :)

Now, make the kernel config

make pico_defconfig

Note: I have a Htc Explorer and my device codename is pico. So i run pico_defconfig

Now Other device users have to look for codename or config names in arch/arm/configs

This Will Make a working kernel config according to the source :)

Now, if You Wanna Make Some Changes In Your Source Do so , and run this command to start compiling:

make -jX

The X can be replaced by the maximum number of jobs your computer can handle simultaneously.. The higher this number, the faster the compiling will get.. But do not enter a number too high, your computer may explode due to overheating.

Okay if all goes well your kernel will be compiled in 15mins :)

The resulting kernel will be in Kernel/arch/arm/boot/zImage

Now We also need modules for wifi and BT to work. So,

Okay So Now The Moules will be in kernel/modules :)

So Now We will pack the kernel to be flashed on the target Device :)

-Okay So Now Get A Working Boot.img Of THe Rom on which Wanna Test Your Kernel

-Download and extract The Bootimagetools From The Link Given Above :)

-place the boot.img in source_img folder inside the boot.img-tools

-enter the bootimagetools Directory

-unpack(This Will extract the boot.img)

-Extracting boot.img-ramdisk.gz(This Will Extract The ramdisk From the boot.img)

-Packing a new ramdisk(This Will Pack A new ramdisk which will be used in your kernel)

Now As we want to create a boot.img from the zImage you made so copy the zImage from arch/arm/boot

Then goto the folder unpack in bootimagetools directory and delete boot.img-zImage

now paste the zImage You copied from arch/arm/boot and paste it in unpack folder and rename it to boot.img-zImage

-Create a new boot.img:

Done :)

Now Take The boot.img for target_img folder and flash it via fastboot and dont forget to push modules else wifi will not work :)

=============================

Patching The kernel[The Easy Way]

=============================

This will Show You How to Patch your kernel to the latest version.

All The Patches are Available on kernel.org.

So If you apply the patches manually, then your source code will be messed up and you will face compiling errors+ it's time consuming to allow every patch as yes :P

So I am going to use a program called

interdiff

to

generate a patch representing the differences between two patches and then

apply the result.

Download:

http://cyberelk.net/tim/data/patchutils/stable/patchutils-0.3.3.tar.xz

SO Now Setting up the program to work :

1. Download the given file and extract it somewhere ,you swill get a folder named patchutils-0.3.3

2. First Install these Two Packages:

sudo apt-get install gcc
sudo apt-get install g++

3. Now cd to the folder patchutils-0.3.3

4. Now Type this in terminal

./configure

5.Now type this

make

6. Then this

sudo make install

Now You have setup the program 'interdiff' for applying the patches

To Apply The patches:

interdiff -z patch-xy patch-yx | patch -p1

patch-xy: The current kernel version ( for example= patch-3.0.97)
patch-yx: The version to be patched ( for example=patch-3.0.98)

Note: Both the patches should be in your kernel source directory

Credits:

[MENTION=4918174]Red Devil[/MENTION] :D (For Teaching me how to compile a kernel)