Note:- This is multipart guide and it is incomplete. I will not be responsible for what happens to your device or computer.
Getting the kernel source
It is very important to find the right kernel for your ROM. Usually they differ by kernel version , module configuration or driver updates. You can find different kernels with tons of mods and optimizations in XDA Forums. Lot of them come with link to github repository. If you are looking kernel for stock ROMS you will have to go to device manufacturer developer portal and find the right kernel for your device. For Samsung devices you can head to opensource.samsung.com. I should tell you it is a very very slow site, have some patience. One more thing regarding stock kernels usually it will be bundled with bugs and if you are very serious stock kernels you will have to check other kernels and pick the patch and apply to stock kernel. please read README file bundled with our download. it will have information that will save your time and hair later. For Cyanogen kernels you get it directly from cyanogen github the generic format for kernel is like https://github.com/CyanogenMod/android_kernel_vendorer_device-borad for example for my galaxy note 2 which is based on smdk4412 board the repo is https://github.com/CyanogenMod/android_kernel_samsung_smdk4412 .
When you say you want to build kernel for android what you will be doing is creating a android boot image which can be flashed on to a specific partition which will loaded by bootloader when you switch on the device. Android boot image is nothing but a linux kernel and initial ramdisk ( aka initrd ). Linux kernel will initialize the basic OS features and device drivers. then ramdisk take over control. In android initrd will mount the system and other partitions, loads android specific driver modules if any, setup android system services ( Not application services) then comes Zygote. All these stuff will be in a set of init*.rc script inside initrd.
So you will compile linux kernel with a build system and pack it with initrd to get boot image. some kernel will have the initrd along with it and some dont. If you dont have it the trick is to extract from an existing boot image and then pack it with yours. Once you have kernel you need to build it to make sure everything is working fine. I have seen some kernels ( usually custom kernels ) comes with build script. These scripts will make your life very easy only thing you have to do is edit some variables to set up paths pointing to cross compiler and initrd path etc. then run the script you will have the kernel image ready in minutes. Some of the build scripts are so cool they even produce recovery flashable zip. In case you dont have build script with your kernel follow the steps to build kernel.
Building the kernel
First thing in manual compiling is identifying the config file for your device. config file contains information which is used at compile time to turn on/off/modularize different kernel features. Read the README or any other notice files with the source to get the name of the config file. Still no luck in finding the right config check out foler $KERNEL_SOURCE/arch/arm/configs folder if you find a file name with a close resemblance to you device name/ code name, that is it. For example in my case for galaxy note 2 (n7100) cyanogenmode kernel source config file name is cyanogenmod_n7100_defconfig and for stock rom it is t0_04_defconfig. Before starts building you need to create a configuration. for that execute these command from the root of your kernel source.
make -C $KERNEL_DIR ARCH=arm CROSS_COMPILE="$CC" $K_CONFIG
- $CC is the cross compiler path for instance /cyg/system/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-
- $KERNEL_DIR is the kernel source root full path
- $K_CONFIG is the name of the config file ( just the name not the full path )
This will configure kernel source with configuration specified in the config file. you can see a new file .config in the root path. To start the building execute these commands from root of the kernel source.
make -C $KERNEL_DIR ARCH=arm CROSS_COMPILE="$CC"
if everything is fine you will have your zImage ready in minutes.
Next part will cover how to custom configure kernel and general procedure of packaging with initrd.