[Ubuntu][How to][4/11/12 Creating a Kernel Build Script

Search This thread

ayysir

Senior Member
Aug 30, 2011
4,552
5,980
Bronx, NY
This thread is meant for kernel devs or people who wants to begin building kernels for their first time.
There is a bit of a learning curve to this though, you need to known Linux functions and have android environment setup

This is a UBUNTU script saved as a shell script for any version of UBUNTU


Start off script with executable script definitions
Code:
#!/bin/bash


This next part is important and ultimately optional however it makes coding the script crazy easier
I call these definitions (likely not what they are technically but, it's my interpretation) to enable shortcoding commands which makes the script look cleaner

These can be named whatever you want btw


Kerneldir - is the directory where your kernel files are kept and this is the main place where you build your kernel

Packagedir - is your work environment, this is where your boot.img will be copied to, extra files, updater-script to be later packaged into a zip folder

INITRAMFS_SOURCE - is where the ramdisk should be placed in folder form [Click Here for how to split ramdisk and zimage from boot.img]

Meta - where the updater script is kept so it can be packaged for my kernel to be able to flashed

ARCH - Must stay as this definition as it is needed for compile to become successful

CROSS_COMPILE - Toolchain command, make sure your kernel supports linaro 4.6 and later. Or default to CM's 4.4.3 toolchain. Must stay as this definition as it is needed for compile to become successful

Code:
export KERNELDIR=/home/ayysir/android/kernel/ASDK
export INITRAMFS_DEST=$KERNELDIR/kernel/usr/initramfs
export PACKAGEDIR=/home/ayysir/android/kernel/asdk_out
export INITRAMFS_SOURCE=/home/ayysir/android/kernel
export Meta=/home/ayysir/android/kernel/Ayysir/META-INF
export Drivers=/home/ayysir/android/kernel/Ayysir/Drivers
export App=/home/ayysir/android/kernel/Ayysir/app
export exfat=/home/ayysir/android/kernel/Ayysir/exfat_modules
export ARCH=arm
export CROSS_COMPILE=/home/ayysir/android/kernel/toolchains/android-toolchain-4.7/bin/arm-eabi-

This part are mostly setup commands to clean out workspace deleting previous compile remnants. This is where definitions we made above comes in handy.

The echo commands are like description commands, used for many situations but for our purpose, it tells the user what is currently happening in the script


Assuming you have knowledge about kernel deving so I don't have to hold your hand in describing things
Below commands vary depending on dev as they have their own defconfig setup for their specific device replace my asdk_defconfig with
this is is for configuring what to include the compile build



UPDATE:
OPTIONAL:


Code:
# Colorize and add text parameters
red=$(tput setaf 1) # red
grn=$(tput setaf 2) # green
cya=$(tput setaf 6) # cyan
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgrn=${txtbld}$(tput setaf 2) # green
bldblu=${txtbld}$(tput setaf 4) # blue
bldcya=${txtbld}$(tput setaf 6) # cyan
txtrst=$(tput sgr0) # Reset

Above Codes Colorize and add text parameters.. it makes it look cleaner and more concise on what is happening in the terminal

The "echo -e" command tells terminal to apply the following text.

"${bldcya}" refers back to colorize table, making it bold (bld) and making text color cyan (cya)

"${txtrst}" closes the statement and resets the color back to terminal default. So the color parameters do not spill over into the next lines

the echo "" adds a space in between lines, just so it looks clean and does not jumble things

so here are some example on how to implement this: [Below]


Code:
echo -e "${bldcya} Making Kernel ${txtrst}"
make asdk_defconfig

echo""

echo -e "${bldcya} Clean Environment ${txtrst}"
make menuconfig

Code:
echo "Remove old Package Files"
rm -rf $PACKAGEDIR/*

echo "Setup Package Directory"
mkdir -p $PACKAGEDIR/system/lib/modules

echo "Setup App Directory"
mkdir -p $PACKAGEDIR/data/app

echo "Remove old zImage"
rm $PACKAGEDIR/zImage

echo "Remove old ramdisk"
rm $INITRAMFS_SOURCE/ramdisk.img.gz

echo "Clean Environment"
cd $KERNELDIR
make clean

echo "Make the kernel"
make asdk_defconfig

echo "Clean Environment"
make menuconfig


Before we get to this important section you need two files put into your /bin folder, mkbootfs and mkbootimg Click Here to download these files.

Then you need to cp these files to bin folder [put the files in home folder]


Code:
sudo cp ~/mkbootfs /bin
sudo cp ~/mkbootimg /bin
cd /bin
sudo chmod +x mkbootfs
sudo chmod +x mkbootimg


Now we are ready for next section


Make -j[num] command tells system how much things can be compiled at once, it's dependent on your system (number of processors] so i cay default to -j4

You also notice the script coding wrapped around the make command, this is what I setup to create compile log for compile session just incase you need to go back to review errors or debug some things

Code:
script -q ~/Compile.log -c "
make -j12 "


Below command searches for modules in zimage file and copies the modules files to workplace modules folder for packaging


Code:
cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/

Like I stated before you should of have already split the boot.img file and extracted ramdisk to a ramdisk folder. Below command uses mkbootfs to compress that folder into a .gz formatted file.


Below part is a bit tricky, because it is dependent on device to device basis.
first the code below moves ramdisk to workfolder
Then it cd's to the package folder and uses mkbootimg command to combine zimage and ramdisk together to make a boot.img
What is tricky is the command mkbootimg uses to determine space for the boot.img to be compiled in

The best way to determine the page size, base, & ramdiskaddr is either use the previous tools that you used to split a already compile boot.img commands and it spits out a file info run down or use DSIXDA ANDROID KITCHEN and using it's advanced options to determine boot.img info. Again I am not going to hold your hand on this one since you should research before trying this build script.

Note: many boot.img's use cmdline option in creation


Code:
echo "Make boot.img"
cp $INITRAMFS_SOURCE/ramdisk.img.gz $PACKAGEDIR
cd $PACKAGEDIR
mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.img.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img

Code:
echo "Packaging Ramdisk"
cd $INITRAMFS_SOURCE/
mkbootfs ./ASDK-ramdisk | gzip > ramdisk.img.gz

Code:
echo "Compiling"
script -q ~/Compile.log -c "
make -j12 "

echo "Copy modules to Package"
cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/
cp $Drivers/* $PACKAGEDIR/system/lib/


echo "Copy zImage to Package"
cp $KERNELDIR/arch/arm/boot/zImage $PACKAGEDIR/zImage

echo "Packaging Ramdisk"
cd $INITRAMFS_SOURCE/
mkbootfs ./ASDK-ramdisk | gzip > ramdisk.img.gz

echo "Make boot.img"
cp $INITRAMFS_SOURCE/ramdisk.img.gz $PACKAGEDIR
cd $PACKAGEDIR
mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.img.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img


This is where you add files you want packaged into your zip folder

*Remember add new definitions every time you add a new directory to the mix and have them cp all the files to workfolder

Code:
export curdate=`date "+%m-%d-%Y"`

This sets definition to zip to auto-generate dates to date your builds so it takes the work out of renaming the zip file [Your welcome lol]

Code:
zip -r ../ASDK-AOSP-4.2-$curdate.zip


set a name to your build and add $curdate.zip to enable auto dating zip



Code:
echo "Import of META-INF"
cp -R $Meta $PACKAGEDIR

echo "Import Voltage Control"
cp $App/* $PACKAGEDIR/data/app

echo "Importing Exfat fix"
cp $exfat/* $PACKAGEDIR/system/lib/modules

export curdate=`date "+%m-%d-%Y"`
        cp ~/Compile.log ~/android/Compile-$curdate.log
        rm ~/Compile.log
cd $PACKAGEDIR
rm ramdisk.img.gz
rm zImage
rm ../ASDK-AOSP-4.2*.zip\
rm -R .fr-7q5stU
zip -r ../ASDK-AOSP-4.2-$curdate.zip .

read -p "Press ENTER to Exit"

UPDATE:

Not to confuse anyone, Below is based off my new build script I use. PLEASE DO NOT COPY WORD FOR WORD, CODE BY CODE JUST USE IT AS REFERENCE. IF YOUR NOT SMART ENOUGH TO GET WHAT I AM DOING THEN DONT TRY BUILDING KERNELS LOL


Code:
if [ -e $KERNELDIR/arch/arm/boot/zImage ]; then
	echo -e "${bldred} Copy modules to Package ${txtrst}"
	cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/
 	
	echo ""

	echo -e "${bldred} Copy zImage to Package ${txtrst}"
	cp $KERNELDIR/arch/arm/boot/zImage $PACKAGEDIR/zImage
 	
	echo ""

	echo -e "${bldred} Ramdisk Readying.. ${txtrst}"
	cp $KERNELDIR/mkbootfs $INITRAMFS_SOURCE
	cd $INITRAMFS_SOURCE/
	./mkbootfs ASDK-krait | gzip > ramdisk.krait.gz
	rm mkbootfs
 	
	echo ""

	echo -e "${bldred} Making Boot.img.. ${txtrst}"
	cp $KERNELDIR/mkbootimg $PACKAGEDIR
	cp $INITRAMFS_SOURCE/ramdisk.krait.gz $PACKAGEDIR
	cd $PACKAGEDIR
	./mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.krait.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img
	rm mkbootimg
 	
	echo ""

	export curdate=`date "+%m-%d-%Y"`
        cp ~/Compile.log ~/android/compile_logs/Success-Krait-$curdate.log
        rm ~/Compile.log
	cd $PACKAGEDIR
	rm ramdisk.krait.gz
	rm zImage
	rm ../../ASDK-AOSP-3.4.x-*.zip
	rm ../ramdisk.krait.gz
	rm ../zImage
	zip -r ../ASDK-AOSP-3.4.x-$curdate-EXP.zip .
	mv ../ASDK-AOSP-3.4.x-$curdate-EXP.zip ~/android/kernel

	echo "ASDK HOT OFF THE PRESS"

else
	echo "KERNEL DID NOT BUILD! no zImage exist"
	export curdate=`date "+%m-%d-%Y"`
	cp ~/Compile.log ~/android/compile_logs/Failed-Krait-$curdate.log
fi;

exit 0

Now before you get mind blown Let me explain the update above:

"if [ -e $KERNELDIR/arch/arm/boot/zImage ]; then" is known as the "if, then" commands. Most used in C and scripting it tells system to look for a certain file or directory first, "if" present "then" it will proceed to following commands after it.

so if the zimage does exist then, the code after the statement will go on and end up making the zip folder.

"else" commands tells terminal, well if that file in the if statement does not exist then use following commands instead. In this case if $KERNELDIR/arch/arm/boot/zImage does not exist then else do this:

Code:
else
	echo "KERNEL DID NOT BUILD! no zImage exist"
	export curdate=`date "+%m-%d-%Y"`
	cp ~/Compile.log ~/android/compile_logs/Failed-Krait-$curdate.log

This else statement makes a fail log to where you keep your build logs. This allows you to see where in the build did you received an error which caused the zimage to not be produced. Also it wont make a zip folder so no useless zips with no boot.img in there

chmod +x your script [make sure you save it as blahblah.sh] and boom your done

CLICK HERE TO SEE MY FULL SCRIPT THAT I USE FOR MY GS3 KERNEL BUILDS [NEEDS REVISING THOUGH]

IF YOU HAVE ANY QUESTIONS LEAVE IT IN THE COMMENTS OR PM ME

IF YOU LIKED MY HOW-TO AND WOULD LIKE TO DONATE TO HELP ME DEV FOR MORE DEVICES [APPRECIATED BUT DONATION NOT REQUIRED] CLICK HERE
 
Last edited:

Cl3Kener

Senior Member
Feb 4, 2013
71
7,001
Cleveland, OH
plus.google.com
Another Example for Fun

Well this thread looked like it needed a comment or at least some positive feedback so here it goes. :)

Thanks so much @ayysir for making this tutorial!!!! :D I've been using scripts to do lots of things for me for about a year now and it all started when I read this thread. (for some reason the though of bash scripts never really crossed my mind before then)

Here is an example of how creative I've got since the beginning of this. My script now includes some colored logos just for fun as well as final build time, uploading to goo.im, and signing of the final zip. (The logos might not show up right on here but they look great in terminal)

Code:
#!/bin/bash
#Cl3Kener's Hammerhead Script

# Colorize and add text parameters (originally grabbed from Ayysir)
red=$(tput setaf 1) # red
grn=$(tput setaf 2) # green
cya=$(tput setaf 6) # cyan
pnk=$(tput bold ; tput setaf 5) # pink
yel=$(tput bold ; tput setaf 3) # yellow
pur=$(tput setaf 5) # purple
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgrn=${txtbld}$(tput setaf 2) # green
bldyel=${txtbld}$(tput bold ; tput setaf 3) # yellow
bldblu=${txtbld}$(tput setaf 4) # blue
bldpur=${txtbld}$(tput setaf 5) # purple
bldpnk=${txtbld}$(tput bold ; tput setaf 5) # pink
bldcya=${txtbld}$(tput setaf 6) # cyan
txtrst=$(tput sgr0) # Reset

#Place you defconfig name here:
DEFCONFIG=uber_hammerhead_defconfig

# Change these exports as needed:
export INITRAMFS_DEST=~/android/kernel/usr/initramfs
export PACKAGEDIR=~/android/kernel/OUT
export KERNEL_SOURCE=~/android/kernel
export Meta=~/android/kernel/Cl3Kener/META-INF
export Etc=~/android/kernel/Cl3Kener/etc
export Scripts=~/android/kernel/Cl3Kener/kernel
export Bin=~/android/kernel/Cl3Kener/bin
export Lib=~/android/kernel/Cl3Kener/lib
export GPU=~/android/kernel/Cl3Kener/kcontrol_gpu_msm
export sign=~/android/kernel/Cl3Kener/signapk_files
export ARCH=arm
export CROSS_COMPILE=~/android/kernel/TOOLCHAINS/arm-eabi-4.7/bin/arm-eabi-

# Place your goo.im password here instead of XXXXXX so you don't have to input password (or you can use ssh key if you have one):
export SSHPASS=XXXXXXX

# Start Time
res1=$(date +%s.%N)

# Logo (just for fun)
echo "${bldpur}                                          ${txtrst}"
echo "${bldpur} ________________________________________ ${txtrst}"
echo "${bldpur}|                                        |${txtrst}"
echo "${bldpur}| _|    _|  _|_|_|    _|_|_|_|  _|_|_|   |${txtrst}"
echo "${bldpur}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
echo "${bldpur}| _|    _|  _|_|_|    _|_|_|    _|_|_|   |${txtrst}"
echo "${bldpur}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
echo "${bldpur}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
echo "${bldpur}|   _|_|    _|_|_|    _|_|_|_|  _|    _| |${txtrst}"
echo "${bldpur}|________________________________________|${txtrst}"
echo "${bldpur}                                          ${txtrst}"

echo "${bldcya}Remove Old Package Files ${txtrst}"
rm -rf $PACKAGEDIR/*

echo " "

echo "${bldgrn}Setup Init.d Directory ${txtrst}"
mkdir -p $PACKAGEDIR/system/etc

echo " "

echo "${bldyel}Setup Scripts Directory ${txtrst}"
mkdir -p $PACKAGEDIR/modules

echo " "

echo "${bldblu}Remove old zImage ${txtrst}"
rm $KERNEL_SOURCE/arch/arm/boot/zImage
rm $KERNEL_SOURCE/arch/arm/boot/zImage-dtb

echo " "

echo -e "${bldred}Removing annoying gedit backup files ${txtrst}"
cd ~/android/kernel
find ./ -name '*~' | xargs rm

echo " "

echo "${bldgrn}Clean Environment ${txtrst}"
cd $KERNEL_SOURCE
make $DEFCONFIG
rm $KERNEL_SOURCE/.version
rm $KERNEL_SOURCE/.config.old
rm $KERNEL_SOURCE/.config
make clean

echo " "

echo "${bldpnk}Kernel Menu ${txtrst}"
make menuconfig

echo " "

echo "${bldcya}Compiling.... ${txtrst}"
script -q ~/Compile.log -c " 
make -j7"

echo " "

if [ -e $KERNEL_SOURCE/arch/arm/boot/zImage-dtb ]; then

	# I used Showp1984 msm_mpdecision hotplug and so I use his KControl Module.
	echo "${bldyel}Making GPU Module ${txtrst}"
	cd $GPU
	make clean
	make -j7

	echo " "

	cd $KERNEL_SOURCE

	echo "${bldgrn}Copy Modules to OUT ${txtrst}"
	cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/modules/

	echo "${bldblu}Import Scripts ${txtrst}"
	cp -R $Scripts $PACKAGEDIR

	echo "${bldcya}Copy bin to OUT ${txtrst}"
	cp -R $Bin $PACKAGEDIR/system/bin

	echo "${bldred}Copy zImage to OUT ${txtrst}"
	cp $KERNEL_SOURCE/arch/arm/boot/zImage-dtb $PACKAGEDIR/kernel/zImage

	echo "${bldgrn}Import of META-INF ${txtrst}"
	cp -R $Meta $PACKAGEDIR

	echo "${bldcya}Import Init.d Tweaks ${txtrst}"
	cp -R $Etc/init.d $PACKAGEDIR/system/etc

	echo "${bldred}Import Dalvik/Bionic Optimized Libs ${txtrst}"
	cp -R $Lib $PACKAGEDIR/system

	export curdate=`date "+%m-%d-%Y"`
        cp ~/Compile.log ~/android/Logs/Completed-Uber-hammerhead-$curdate.log
        rm ~/Compile.log

	cd $PACKAGEDIR
	rm ../UBER-Hammerhead*.zip\
	rm -R .fr-7q5stU
	zip -r ../UBER-Hammerhead-Nightly.zip .

	echo " "

	echo "${bldblu}Signing Zip ${txtrst}"
	cd $KERNEL_SOURCE
	cp $sign/signapk.jar $KERNEL_SOURCE	
	cp $sign/testkey.pk8 $KERNEL_SOURCE	
	cp $sign/testkey.x509.pem $KERNEL_SOURCE	
	java -jar ~/android/kernel/signapk.jar testkey.x509.pem testkey.pk8 UBER-Hammerhead-Nightly.zip UBER-Hammerhead-Linaro-4.8.3-$curdate.zip
	rm UBER-Hammerhead-Nightly.zip
	rm signapk.jar
	rm testkey.pk8
	rm testkey.x509.pem

	echo "${bldgrn}                                          ${txtrst}"
	echo "${bldgrn}                                          ${txtrst}"
	echo "${bldgrn} ________________________________________ ${txtrst}"
	echo "${bldgrn}|                                        |${txtrst}"
	echo "${bldgrn}| _|    _|  _|_|_|    _|_|_|_|  _|_|_|   |${txtrst}"
	echo "${bldgrn}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
	echo "${bldgrn}| _|    _|  _|_|_|    _|_|_|    _|_|_|   |${txtrst}"
	echo "${bldgrn}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
	echo "${bldgrn}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
	echo "${bldgrn}|   _|_|    _|_|_|    _|_|_|_|  _|    _| |${txtrst}"
	echo "${bldgrn}|________________________________________|${txtrst}"
	echo "${bldgrn}                                          ${txtrst}"
	echo "${bldgrn}                                          ${txtrst}"
	echo "${bldgrn}Kernel has completed successfully!!! ${txtrst}"
	echo " "
	echo " "
	echo "${bldgrn}Uploading .... ${txtrst}"

	echo " "

	sshpass -e scp -P 2222  ~/android/kernel/UBER-Hammerhead-Linaro-4.8.3-$curdate.zip Cl3Kener@upload.goo.im:/home/Cl3Kener/public_html/HAMMERHEAD/KERNELS/UBER/EXP-LINARO/UBER-Hammerhead-Linaro-4.8.3-$curdate.zip

	# Show Elapsed Time
	res2=$(date +%s.%N)
	echo "${bldgrn}Total elapsed time of ALL BUILDS AND UPLOADS: ${txtrst}${grn}$(echo "($res2 - $res1) / 60"|bc ) minutes ($(echo "$res2 - $res1"|bc ) seconds) ${txtrst}"

else

	export curdate=`date "+%m-%d-%Y"`
	cp ~/Compile.log ~/android/Logs/Failed-Uber-hammerhead-$curdate.log
        rm ~/Compile.log
	echo "${bldred}                                          ${txtrst}"
	echo "${bldred}                                          ${txtrst}"
	echo "${bldred} ________________________________________ ${txtrst}"
	echo "${bldred}|                                        |${txtrst}"
	echo "${bldred}| _|    _|  _|_|_|    _|_|_|_|  _|_|_|   |${txtrst}"
	echo "${bldred}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
	echo "${bldred}| _|    _|  _|_|_|    _|_|_|    _|_|_|   |${txtrst}"
	echo "${bldred}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
	echo "${bldred}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
	echo "${bldred}|   _|_|    _|_|_|    _|_|_|_|  _|    _| |${txtrst}"
	echo "${bldred}|________________________________________|${txtrst}"
	echo "${bldred}                                          ${txtrst}"
	echo "${bldred}                                          ${txtrst}"
	echo "${bldred}KERNEL IMAGE DID NOT BUILD PROPERLY! Check Compile log! ${txtrst}"

	echo " "

	# Show Elapsed Time
	res2=$(date +%s.%N)
	echo "${bldred}Total elapsed time of ALL BUILDS AND UPLOADS: ${txtrst}${red}$(echo "($res2 - $res1) / 60"|bc ) minutes ($(echo "$res2 - $res1"|bc ) seconds) ${txtrst}"
fi;

echo " "

read -p "Press ENTER to Exit"

Hope this helps some of you get creative!

If you want to see a finished product so you can figure out how everything came together see here: http://goo.im/devs/Cl3Kener/HAMMERHEAD/KERNELS/UBER

Cheers!
Cl3Kener
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 34
    This thread is meant for kernel devs or people who wants to begin building kernels for their first time.
    There is a bit of a learning curve to this though, you need to known Linux functions and have android environment setup

    This is a UBUNTU script saved as a shell script for any version of UBUNTU


    Start off script with executable script definitions
    Code:
    #!/bin/bash


    This next part is important and ultimately optional however it makes coding the script crazy easier
    I call these definitions (likely not what they are technically but, it's my interpretation) to enable shortcoding commands which makes the script look cleaner

    These can be named whatever you want btw


    Kerneldir - is the directory where your kernel files are kept and this is the main place where you build your kernel

    Packagedir - is your work environment, this is where your boot.img will be copied to, extra files, updater-script to be later packaged into a zip folder

    INITRAMFS_SOURCE - is where the ramdisk should be placed in folder form [Click Here for how to split ramdisk and zimage from boot.img]

    Meta - where the updater script is kept so it can be packaged for my kernel to be able to flashed

    ARCH - Must stay as this definition as it is needed for compile to become successful

    CROSS_COMPILE - Toolchain command, make sure your kernel supports linaro 4.6 and later. Or default to CM's 4.4.3 toolchain. Must stay as this definition as it is needed for compile to become successful

    Code:
    export KERNELDIR=/home/ayysir/android/kernel/ASDK
    export INITRAMFS_DEST=$KERNELDIR/kernel/usr/initramfs
    export PACKAGEDIR=/home/ayysir/android/kernel/asdk_out
    export INITRAMFS_SOURCE=/home/ayysir/android/kernel
    export Meta=/home/ayysir/android/kernel/Ayysir/META-INF
    export Drivers=/home/ayysir/android/kernel/Ayysir/Drivers
    export App=/home/ayysir/android/kernel/Ayysir/app
    export exfat=/home/ayysir/android/kernel/Ayysir/exfat_modules
    export ARCH=arm
    export CROSS_COMPILE=/home/ayysir/android/kernel/toolchains/android-toolchain-4.7/bin/arm-eabi-

    This part are mostly setup commands to clean out workspace deleting previous compile remnants. This is where definitions we made above comes in handy.

    The echo commands are like description commands, used for many situations but for our purpose, it tells the user what is currently happening in the script


    Assuming you have knowledge about kernel deving so I don't have to hold your hand in describing things
    Below commands vary depending on dev as they have their own defconfig setup for their specific device replace my asdk_defconfig with
    this is is for configuring what to include the compile build



    UPDATE:
    OPTIONAL:


    Code:
    # Colorize and add text parameters
    red=$(tput setaf 1) # red
    grn=$(tput setaf 2) # green
    cya=$(tput setaf 6) # cyan
    txtbld=$(tput bold) # Bold
    bldred=${txtbld}$(tput setaf 1) # red
    bldgrn=${txtbld}$(tput setaf 2) # green
    bldblu=${txtbld}$(tput setaf 4) # blue
    bldcya=${txtbld}$(tput setaf 6) # cyan
    txtrst=$(tput sgr0) # Reset

    Above Codes Colorize and add text parameters.. it makes it look cleaner and more concise on what is happening in the terminal

    The "echo -e" command tells terminal to apply the following text.

    "${bldcya}" refers back to colorize table, making it bold (bld) and making text color cyan (cya)

    "${txtrst}" closes the statement and resets the color back to terminal default. So the color parameters do not spill over into the next lines

    the echo "" adds a space in between lines, just so it looks clean and does not jumble things

    so here are some example on how to implement this: [Below]


    Code:
    echo -e "${bldcya} Making Kernel ${txtrst}"
    make asdk_defconfig
    
    echo""
    
    echo -e "${bldcya} Clean Environment ${txtrst}"
    make menuconfig

    Code:
    echo "Remove old Package Files"
    rm -rf $PACKAGEDIR/*
    
    echo "Setup Package Directory"
    mkdir -p $PACKAGEDIR/system/lib/modules
    
    echo "Setup App Directory"
    mkdir -p $PACKAGEDIR/data/app
    
    echo "Remove old zImage"
    rm $PACKAGEDIR/zImage
    
    echo "Remove old ramdisk"
    rm $INITRAMFS_SOURCE/ramdisk.img.gz
    
    echo "Clean Environment"
    cd $KERNELDIR
    make clean
    
    echo "Make the kernel"
    make asdk_defconfig
    
    echo "Clean Environment"
    make menuconfig


    Before we get to this important section you need two files put into your /bin folder, mkbootfs and mkbootimg Click Here to download these files.

    Then you need to cp these files to bin folder [put the files in home folder]


    Code:
    sudo cp ~/mkbootfs /bin
    sudo cp ~/mkbootimg /bin
    cd /bin
    sudo chmod +x mkbootfs
    sudo chmod +x mkbootimg


    Now we are ready for next section


    Make -j[num] command tells system how much things can be compiled at once, it's dependent on your system (number of processors] so i cay default to -j4

    You also notice the script coding wrapped around the make command, this is what I setup to create compile log for compile session just incase you need to go back to review errors or debug some things

    Code:
    script -q ~/Compile.log -c "
    make -j12 "


    Below command searches for modules in zimage file and copies the modules files to workplace modules folder for packaging


    Code:
    cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/

    Like I stated before you should of have already split the boot.img file and extracted ramdisk to a ramdisk folder. Below command uses mkbootfs to compress that folder into a .gz formatted file.


    Below part is a bit tricky, because it is dependent on device to device basis.
    first the code below moves ramdisk to workfolder
    Then it cd's to the package folder and uses mkbootimg command to combine zimage and ramdisk together to make a boot.img
    What is tricky is the command mkbootimg uses to determine space for the boot.img to be compiled in

    The best way to determine the page size, base, & ramdiskaddr is either use the previous tools that you used to split a already compile boot.img commands and it spits out a file info run down or use DSIXDA ANDROID KITCHEN and using it's advanced options to determine boot.img info. Again I am not going to hold your hand on this one since you should research before trying this build script.

    Note: many boot.img's use cmdline option in creation


    Code:
    echo "Make boot.img"
    cp $INITRAMFS_SOURCE/ramdisk.img.gz $PACKAGEDIR
    cd $PACKAGEDIR
    mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.img.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img

    Code:
    echo "Packaging Ramdisk"
    cd $INITRAMFS_SOURCE/
    mkbootfs ./ASDK-ramdisk | gzip > ramdisk.img.gz

    Code:
    echo "Compiling"
    script -q ~/Compile.log -c "
    make -j12 "
    
    echo "Copy modules to Package"
    cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/
    cp $Drivers/* $PACKAGEDIR/system/lib/
    
    
    echo "Copy zImage to Package"
    cp $KERNELDIR/arch/arm/boot/zImage $PACKAGEDIR/zImage
    
    echo "Packaging Ramdisk"
    cd $INITRAMFS_SOURCE/
    mkbootfs ./ASDK-ramdisk | gzip > ramdisk.img.gz
    
    echo "Make boot.img"
    cp $INITRAMFS_SOURCE/ramdisk.img.gz $PACKAGEDIR
    cd $PACKAGEDIR
    mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.img.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img


    This is where you add files you want packaged into your zip folder

    *Remember add new definitions every time you add a new directory to the mix and have them cp all the files to workfolder

    Code:
    export curdate=`date "+%m-%d-%Y"`

    This sets definition to zip to auto-generate dates to date your builds so it takes the work out of renaming the zip file [Your welcome lol]

    Code:
    zip -r ../ASDK-AOSP-4.2-$curdate.zip


    set a name to your build and add $curdate.zip to enable auto dating zip



    Code:
    echo "Import of META-INF"
    cp -R $Meta $PACKAGEDIR
    
    echo "Import Voltage Control"
    cp $App/* $PACKAGEDIR/data/app
    
    echo "Importing Exfat fix"
    cp $exfat/* $PACKAGEDIR/system/lib/modules
    
    export curdate=`date "+%m-%d-%Y"`
            cp ~/Compile.log ~/android/Compile-$curdate.log
            rm ~/Compile.log
    cd $PACKAGEDIR
    rm ramdisk.img.gz
    rm zImage
    rm ../ASDK-AOSP-4.2*.zip\
    rm -R .fr-7q5stU
    zip -r ../ASDK-AOSP-4.2-$curdate.zip .
    
    read -p "Press ENTER to Exit"

    UPDATE:

    Not to confuse anyone, Below is based off my new build script I use. PLEASE DO NOT COPY WORD FOR WORD, CODE BY CODE JUST USE IT AS REFERENCE. IF YOUR NOT SMART ENOUGH TO GET WHAT I AM DOING THEN DONT TRY BUILDING KERNELS LOL


    Code:
    if [ -e $KERNELDIR/arch/arm/boot/zImage ]; then
    	echo -e "${bldred} Copy modules to Package ${txtrst}"
    	cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/system/lib/modules/
     	
    	echo ""
    
    	echo -e "${bldred} Copy zImage to Package ${txtrst}"
    	cp $KERNELDIR/arch/arm/boot/zImage $PACKAGEDIR/zImage
     	
    	echo ""
    
    	echo -e "${bldred} Ramdisk Readying.. ${txtrst}"
    	cp $KERNELDIR/mkbootfs $INITRAMFS_SOURCE
    	cd $INITRAMFS_SOURCE/
    	./mkbootfs ASDK-krait | gzip > ramdisk.krait.gz
    	rm mkbootfs
     	
    	echo ""
    
    	echo -e "${bldred} Making Boot.img.. ${txtrst}"
    	cp $KERNELDIR/mkbootimg $PACKAGEDIR
    	cp $INITRAMFS_SOURCE/ramdisk.krait.gz $PACKAGEDIR
    	cd $PACKAGEDIR
    	./mkbootimg --cmdline 'console = null androidboot.hardware=qcom user_debug=31 zcache' --kernel $PACKAGEDIR/zImage --ramdisk $PACKAGEDIR/ramdisk.krait.gz --base 0x80200000 --pagesize 2048 --ramdiskaddr 0x81500000 --output $PACKAGEDIR/boot.img
    	rm mkbootimg
     	
    	echo ""
    
    	export curdate=`date "+%m-%d-%Y"`
            cp ~/Compile.log ~/android/compile_logs/Success-Krait-$curdate.log
            rm ~/Compile.log
    	cd $PACKAGEDIR
    	rm ramdisk.krait.gz
    	rm zImage
    	rm ../../ASDK-AOSP-3.4.x-*.zip
    	rm ../ramdisk.krait.gz
    	rm ../zImage
    	zip -r ../ASDK-AOSP-3.4.x-$curdate-EXP.zip .
    	mv ../ASDK-AOSP-3.4.x-$curdate-EXP.zip ~/android/kernel
    
    	echo "ASDK HOT OFF THE PRESS"
    
    else
    	echo "KERNEL DID NOT BUILD! no zImage exist"
    	export curdate=`date "+%m-%d-%Y"`
    	cp ~/Compile.log ~/android/compile_logs/Failed-Krait-$curdate.log
    fi;
    
    exit 0

    Now before you get mind blown Let me explain the update above:

    "if [ -e $KERNELDIR/arch/arm/boot/zImage ]; then" is known as the "if, then" commands. Most used in C and scripting it tells system to look for a certain file or directory first, "if" present "then" it will proceed to following commands after it.

    so if the zimage does exist then, the code after the statement will go on and end up making the zip folder.

    "else" commands tells terminal, well if that file in the if statement does not exist then use following commands instead. In this case if $KERNELDIR/arch/arm/boot/zImage does not exist then else do this:

    Code:
    else
    	echo "KERNEL DID NOT BUILD! no zImage exist"
    	export curdate=`date "+%m-%d-%Y"`
    	cp ~/Compile.log ~/android/compile_logs/Failed-Krait-$curdate.log

    This else statement makes a fail log to where you keep your build logs. This allows you to see where in the build did you received an error which caused the zimage to not be produced. Also it wont make a zip folder so no useless zips with no boot.img in there

    chmod +x your script [make sure you save it as blahblah.sh] and boom your done

    CLICK HERE TO SEE MY FULL SCRIPT THAT I USE FOR MY GS3 KERNEL BUILDS [NEEDS REVISING THOUGH]

    IF YOU HAVE ANY QUESTIONS LEAVE IT IN THE COMMENTS OR PM ME

    IF YOU LIKED MY HOW-TO AND WOULD LIKE TO DONATE TO HELP ME DEV FOR MORE DEVICES [APPRECIATED BUT DONATION NOT REQUIRED] CLICK HERE
    9
    Another Example for Fun

    Well this thread looked like it needed a comment or at least some positive feedback so here it goes. :)

    Thanks so much @ayysir for making this tutorial!!!! :D I've been using scripts to do lots of things for me for about a year now and it all started when I read this thread. (for some reason the though of bash scripts never really crossed my mind before then)

    Here is an example of how creative I've got since the beginning of this. My script now includes some colored logos just for fun as well as final build time, uploading to goo.im, and signing of the final zip. (The logos might not show up right on here but they look great in terminal)

    Code:
    #!/bin/bash
    #Cl3Kener's Hammerhead Script
    
    # Colorize and add text parameters (originally grabbed from Ayysir)
    red=$(tput setaf 1) # red
    grn=$(tput setaf 2) # green
    cya=$(tput setaf 6) # cyan
    pnk=$(tput bold ; tput setaf 5) # pink
    yel=$(tput bold ; tput setaf 3) # yellow
    pur=$(tput setaf 5) # purple
    txtbld=$(tput bold) # Bold
    bldred=${txtbld}$(tput setaf 1) # red
    bldgrn=${txtbld}$(tput setaf 2) # green
    bldyel=${txtbld}$(tput bold ; tput setaf 3) # yellow
    bldblu=${txtbld}$(tput setaf 4) # blue
    bldpur=${txtbld}$(tput setaf 5) # purple
    bldpnk=${txtbld}$(tput bold ; tput setaf 5) # pink
    bldcya=${txtbld}$(tput setaf 6) # cyan
    txtrst=$(tput sgr0) # Reset
    
    #Place you defconfig name here:
    DEFCONFIG=uber_hammerhead_defconfig
    
    # Change these exports as needed:
    export INITRAMFS_DEST=~/android/kernel/usr/initramfs
    export PACKAGEDIR=~/android/kernel/OUT
    export KERNEL_SOURCE=~/android/kernel
    export Meta=~/android/kernel/Cl3Kener/META-INF
    export Etc=~/android/kernel/Cl3Kener/etc
    export Scripts=~/android/kernel/Cl3Kener/kernel
    export Bin=~/android/kernel/Cl3Kener/bin
    export Lib=~/android/kernel/Cl3Kener/lib
    export GPU=~/android/kernel/Cl3Kener/kcontrol_gpu_msm
    export sign=~/android/kernel/Cl3Kener/signapk_files
    export ARCH=arm
    export CROSS_COMPILE=~/android/kernel/TOOLCHAINS/arm-eabi-4.7/bin/arm-eabi-
    
    # Place your goo.im password here instead of XXXXXX so you don't have to input password (or you can use ssh key if you have one):
    export SSHPASS=XXXXXXX
    
    # Start Time
    res1=$(date +%s.%N)
    
    # Logo (just for fun)
    echo "${bldpur}                                          ${txtrst}"
    echo "${bldpur} ________________________________________ ${txtrst}"
    echo "${bldpur}|                                        |${txtrst}"
    echo "${bldpur}| _|    _|  _|_|_|    _|_|_|_|  _|_|_|   |${txtrst}"
    echo "${bldpur}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    echo "${bldpur}| _|    _|  _|_|_|    _|_|_|    _|_|_|   |${txtrst}"
    echo "${bldpur}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    echo "${bldpur}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    echo "${bldpur}|   _|_|    _|_|_|    _|_|_|_|  _|    _| |${txtrst}"
    echo "${bldpur}|________________________________________|${txtrst}"
    echo "${bldpur}                                          ${txtrst}"
    
    echo "${bldcya}Remove Old Package Files ${txtrst}"
    rm -rf $PACKAGEDIR/*
    
    echo " "
    
    echo "${bldgrn}Setup Init.d Directory ${txtrst}"
    mkdir -p $PACKAGEDIR/system/etc
    
    echo " "
    
    echo "${bldyel}Setup Scripts Directory ${txtrst}"
    mkdir -p $PACKAGEDIR/modules
    
    echo " "
    
    echo "${bldblu}Remove old zImage ${txtrst}"
    rm $KERNEL_SOURCE/arch/arm/boot/zImage
    rm $KERNEL_SOURCE/arch/arm/boot/zImage-dtb
    
    echo " "
    
    echo -e "${bldred}Removing annoying gedit backup files ${txtrst}"
    cd ~/android/kernel
    find ./ -name '*~' | xargs rm
    
    echo " "
    
    echo "${bldgrn}Clean Environment ${txtrst}"
    cd $KERNEL_SOURCE
    make $DEFCONFIG
    rm $KERNEL_SOURCE/.version
    rm $KERNEL_SOURCE/.config.old
    rm $KERNEL_SOURCE/.config
    make clean
    
    echo " "
    
    echo "${bldpnk}Kernel Menu ${txtrst}"
    make menuconfig
    
    echo " "
    
    echo "${bldcya}Compiling.... ${txtrst}"
    script -q ~/Compile.log -c " 
    make -j7"
    
    echo " "
    
    if [ -e $KERNEL_SOURCE/arch/arm/boot/zImage-dtb ]; then
    
    	# I used Showp1984 msm_mpdecision hotplug and so I use his KControl Module.
    	echo "${bldyel}Making GPU Module ${txtrst}"
    	cd $GPU
    	make clean
    	make -j7
    
    	echo " "
    
    	cd $KERNEL_SOURCE
    
    	echo "${bldgrn}Copy Modules to OUT ${txtrst}"
    	cp -a $(find . -name *.ko -print |grep -v initramfs) $PACKAGEDIR/modules/
    
    	echo "${bldblu}Import Scripts ${txtrst}"
    	cp -R $Scripts $PACKAGEDIR
    
    	echo "${bldcya}Copy bin to OUT ${txtrst}"
    	cp -R $Bin $PACKAGEDIR/system/bin
    
    	echo "${bldred}Copy zImage to OUT ${txtrst}"
    	cp $KERNEL_SOURCE/arch/arm/boot/zImage-dtb $PACKAGEDIR/kernel/zImage
    
    	echo "${bldgrn}Import of META-INF ${txtrst}"
    	cp -R $Meta $PACKAGEDIR
    
    	echo "${bldcya}Import Init.d Tweaks ${txtrst}"
    	cp -R $Etc/init.d $PACKAGEDIR/system/etc
    
    	echo "${bldred}Import Dalvik/Bionic Optimized Libs ${txtrst}"
    	cp -R $Lib $PACKAGEDIR/system
    
    	export curdate=`date "+%m-%d-%Y"`
            cp ~/Compile.log ~/android/Logs/Completed-Uber-hammerhead-$curdate.log
            rm ~/Compile.log
    
    	cd $PACKAGEDIR
    	rm ../UBER-Hammerhead*.zip\
    	rm -R .fr-7q5stU
    	zip -r ../UBER-Hammerhead-Nightly.zip .
    
    	echo " "
    
    	echo "${bldblu}Signing Zip ${txtrst}"
    	cd $KERNEL_SOURCE
    	cp $sign/signapk.jar $KERNEL_SOURCE	
    	cp $sign/testkey.pk8 $KERNEL_SOURCE	
    	cp $sign/testkey.x509.pem $KERNEL_SOURCE	
    	java -jar ~/android/kernel/signapk.jar testkey.x509.pem testkey.pk8 UBER-Hammerhead-Nightly.zip UBER-Hammerhead-Linaro-4.8.3-$curdate.zip
    	rm UBER-Hammerhead-Nightly.zip
    	rm signapk.jar
    	rm testkey.pk8
    	rm testkey.x509.pem
    
    	echo "${bldgrn}                                          ${txtrst}"
    	echo "${bldgrn}                                          ${txtrst}"
    	echo "${bldgrn} ________________________________________ ${txtrst}"
    	echo "${bldgrn}|                                        |${txtrst}"
    	echo "${bldgrn}| _|    _|  _|_|_|    _|_|_|_|  _|_|_|   |${txtrst}"
    	echo "${bldgrn}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    	echo "${bldgrn}| _|    _|  _|_|_|    _|_|_|    _|_|_|   |${txtrst}"
    	echo "${bldgrn}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    	echo "${bldgrn}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    	echo "${bldgrn}|   _|_|    _|_|_|    _|_|_|_|  _|    _| |${txtrst}"
    	echo "${bldgrn}|________________________________________|${txtrst}"
    	echo "${bldgrn}                                          ${txtrst}"
    	echo "${bldgrn}                                          ${txtrst}"
    	echo "${bldgrn}Kernel has completed successfully!!! ${txtrst}"
    	echo " "
    	echo " "
    	echo "${bldgrn}Uploading .... ${txtrst}"
    
    	echo " "
    
    	sshpass -e scp -P 2222  ~/android/kernel/UBER-Hammerhead-Linaro-4.8.3-$curdate.zip Cl3Kener@upload.goo.im:/home/Cl3Kener/public_html/HAMMERHEAD/KERNELS/UBER/EXP-LINARO/UBER-Hammerhead-Linaro-4.8.3-$curdate.zip
    
    	# Show Elapsed Time
    	res2=$(date +%s.%N)
    	echo "${bldgrn}Total elapsed time of ALL BUILDS AND UPLOADS: ${txtrst}${grn}$(echo "($res2 - $res1) / 60"|bc ) minutes ($(echo "$res2 - $res1"|bc ) seconds) ${txtrst}"
    
    else
    
    	export curdate=`date "+%m-%d-%Y"`
    	cp ~/Compile.log ~/android/Logs/Failed-Uber-hammerhead-$curdate.log
            rm ~/Compile.log
    	echo "${bldred}                                          ${txtrst}"
    	echo "${bldred}                                          ${txtrst}"
    	echo "${bldred} ________________________________________ ${txtrst}"
    	echo "${bldred}|                                        |${txtrst}"
    	echo "${bldred}| _|    _|  _|_|_|    _|_|_|_|  _|_|_|   |${txtrst}"
    	echo "${bldred}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    	echo "${bldred}| _|    _|  _|_|_|    _|_|_|    _|_|_|   |${txtrst}"
    	echo "${bldred}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    	echo "${bldred}| _|    _|  _|    _|  _|        _|    _| |${txtrst}"
    	echo "${bldred}|   _|_|    _|_|_|    _|_|_|_|  _|    _| |${txtrst}"
    	echo "${bldred}|________________________________________|${txtrst}"
    	echo "${bldred}                                          ${txtrst}"
    	echo "${bldred}                                          ${txtrst}"
    	echo "${bldred}KERNEL IMAGE DID NOT BUILD PROPERLY! Check Compile log! ${txtrst}"
    
    	echo " "
    
    	# Show Elapsed Time
    	res2=$(date +%s.%N)
    	echo "${bldred}Total elapsed time of ALL BUILDS AND UPLOADS: ${txtrst}${red}$(echo "($res2 - $res1) / 60"|bc ) minutes ($(echo "$res2 - $res1"|bc ) seconds) ${txtrst}"
    fi;
    
    echo " "
    
    read -p "Press ENTER to Exit"

    Hope this helps some of you get creative!

    If you want to see a finished product so you can figure out how everything came together see here: http://goo.im/devs/Cl3Kener/HAMMERHEAD/KERNELS/UBER

    Cheers!
    Cl3Kener
    1
    Awesome guide. probbaly not seen much because of its placement but his is helpful!