[AT&T Samsung Galaxy S II Skyrocket SGH-I727] BMP7777:
KERNEL BUILDING and other EXTRAS

Building your first kernel

:

This is a set of steps to get you building your first kernel. Special thanks to thewadegeek for putting this tutorial together. His thread can be found here: http://xdaforums.com/showthread.php?t=1748297

Step 1.

Build Environment

A. Install Ubuntu 12.04(Not holding your hand here, if you can't do this you shouldn't be messing with kernels)

B. Required packages: git-core, gnupg, flex, bison, gperf, libsdl-dev, libesd0-dev, libwxgtk2.6-dev, build-essential, zip, curl, libncurses5-dev, zlib1g-dev, ia32-libs, lib32z1-dev, lib32ncurses5-dev, gcc-multilib, g++-multilib, and Adb.

C. Open a terminal

D. Type

[COLOR="black"]mkdir android[/COLOR]

E. Type

[COLOR="black"]cd android[/COLOR]

G.Type

[COLOR="black"]mkdir kernel[/COLOR]

Step 2.

Your Source

A. Open your Terminal Prompt

B. Type

[COLOR="black"]cd android/kernel[/COLOR]

C. Type

[COLOR="black"]git clone git://github.com/DooMLoRD/android_prebuilt_toolchains.git toolchains[/COLOR]

D. Now comes the tricky part, you need to have some-type of source for your kernel. Check the following two sites for your device as appropriate. Once you have it download it is extracted/cloned into a folder in your kernel directory.

http://www.htcdev.com/

http://opensource.samsung.com/

.http://developer.sonymobile.com/wpor...ads/opensource

http://www.lg.com/global/support/ope...opensource.jsp

Step 3.

Modifications

This is the part people are curious about, they want to make modifications to the kernel to make it "special". Start all these from the root directory of your kernel source.

Mod 1.

Applying a patch

A. Download the patch you wish to apply, in this case this one should work.

B. Save that file as "kernelPatch" in your kernel directory.

C. Open a Terminal

D. Move into the root directory of the kernel you wish to patch.

E. Type

[COLOR="black"]patch -p1 < ../kernelPatch[/COLOR]

Mod 2.

Adding a Governor Alone

A. Open "drivers/cpufreq/Kconfig"

B. Add the following lines in appropriate spot amongst the other govenor's

[COLOR="Black"]config CPU_FREQ_DEFAULT_GOV_SMARTASS
bool "smartass"
select CPU_FREQ_GOV_SMARTASS
select CPU_FREQ_GOV_PERFORMANCE
help
Use the CPUFreq governor 'smartass' as default.
[/COLOR]

[COLOR="Black"]config CPU_FREQ_GOV_SMARTASS
tristate "'smartass' cpufreq governor"
depends on CPU_FREQ
help
smartass' - a "smart" optimized governor!
If in doubt, say N.[/COLOR]

C. Open "drivers/cpufreq/Makefile"

D. Add the following line in the appropriate spot.

[COLOR="Black"] obj-$(CONFIG_CPU_FREQ_GOV_SMARTASS) += cpufreq_smartass.o[/COLOR]

E. Create a file called "drivers/cpufreq/cpufreq_smartass.c"

F. Put the following code in that file.

http://pastebin.com/f0Bk9kVZ

G. open "include/linux/cpufreq.h"

H. Under the "Cpufreq Default" section add

[COLOR="Black"]#elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_SMARTASS)
extern struct cpufreq_governor cpufreq_gov_smartass;
#define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_smartass)[/COLOR]

Ok there is a governor added, do the exact same steps for any other one's you would like to add.

Step 4.

Getting a Config file

Option A.

Pulling a config file from a kernel.

A. Hook up a device that is using a kernel similar to one you are using as your base.

B. Open a terminal

C. Change to your root kernel directory

D. Type

[COLOR="black"]adb pull /proc/config.gz[/COLOR]

E. Type

[COLOR="black"]gunzip config.gz[/COLOR]

F. Type

[COLOR="black"]mv config arch/arm/configs/<your_config_name>_defconfig[/COLOR]

Option B.

Using the manufacturers config.

Unfortunately as stated above, not all kernels support the "/proc/config.gz" method. You can typically find a manufacturer's configuration file in "arch/arm/configs". I believe the one for my HTC Flyer was called "flyer_hc_defconfig", so look for a layout similar to that one. Also read the README to get a better idea of how to modify it. I would personally make a copy of it called "<your_config_name>_defconfig" and use that as my base.

Step 5.

Building

Time to start the real "build" section of this tutorial.

Part A.

Pre-build Steps

A. Open terminal and change to the root of your kernel directory

B. Type

[COLOR="black"]export ARCH=arm[/COLOR]

C. Type

[COLOR="black"]export CROSS_COMPILE=~/android/kernel/toolchains/arm-eabi-linaro-4.6.2/bin/arm-eabi-[/COLOR]

Part B.

The First Build

A. Type

[COLOR="black"]make <your_config_name>_defconfig[/COLOR]

B. Type

[COLOR="black"]make menuconfig[/COLOR]

and make the required changes to use any modules you added or similar changes.

C. Type

[COLOR="black"]make -j<maximum number of jobs>[/COLOR]

Part C.

Re-Builds

A. Type

[COLOR="black"]make clean[/COLOR]

B. Type

[COLOR="black"]make oldconfig[/COLOR]

C. Type

[COLOR="black"]make -j<maximum number of jobs>[/COLOR]

Part D.

Building Modules

You have two options:

A. Type

[COLOR="black"]make modules[/COLOR]

B. Type

[COLOR="black"]make path/to/your/module.ko[/COLOR]

The above steps explained:

Part A.

(These steps are required every time you close your terminal and re-open it to build again.)

A. Ok shouldn’t need to explain this.

B. This command sets your target architecture.

C. Defines the path to the toolchain we are going to use to compile our kernel. You can change this to point towards whatever toolchain you have downloaded or feel like using, the way it is currently configured it will use the Linaro toolchain that we downloaded above.

Part B.

(These only need to be run the first time you build a kernel.)

A. Load's your configuration file from earlier.

B. Open up a menu to configure your kernel. It will use the config file you loaded in the previous step as a base.

C. Viola start the build. I typically allow 1 job per core, so on my quad core machine I put "make -j4". Just raising that number will not make your build faster, your processor needs to be able to support the number of jobs you are assigning it.

Part C.

(Use the command's when you are building any-time outside of the first)

A. This command gets rid of any old/outdated binaries or modules you compiled before, and let's start fresh. I like to run it every I build unless my changes are really small and localized.

B. A very awesome command, it parses through what has changed and only prompts you about new options.

C. See the explanation for the above "Part C.".

Part D.

(Use these for just building kernel modules.)

A. This will re-build all modules.

B. Will rebuild just the module you need. Very useful when you need to rebuild a WiFi module.

Step 6.

Now what

Ok we have now started our build and we are waiting for it to finish, so there are two possible outcomes:

Outcome A.

Build Succeeds

W00t!! You have a kernel built by your self from source. There are a couple things you need in-order to use this kernel on your device any ".ko" modules and the zImage binary. If you pay attention to the output of your compiler then you will see the location of those objects. However the following commands will make your life a bit easier(Thanks Recognized Developer Hacre):

A. Open a terminal

B. Change to your root kernel directory

C. Type

[COLOR="black"]mkdir ../<your_kernel>_output[/COLOR]

D. Type

[COLOR="black"]cp arch/arm/boot/zImage ../<your_kernel>_output/zImage[/COLOR]

E. Type

[COLOR="black"]find . -name "*.ko" -exec cp {} ../<your_kernel>_output \;[/COLOR]

The above steps explained:

A-C. Self-Explanatory

D. Move our kernel binary into our output folder

E. This handy bit of magic finds all ".ko" modules and also copies them into your output file.

You will also need to assemble a kernel image containing a initramfs for your device, along with the kernel binary and such. That however is beyond the scope of this tutorial. To get started though try searching the following phrases.

[COLOR="Black"]building android kernel image
xda build kernel image
xda unpack boot.img[/COLOR]

Outcome B.

Build Fails

Oh dear. It failed. Well guess what...this is going to happen..

a LOT

. Get used to it, and get used to googling and experimenting with different solutions. The following are some tips that will help you with debugging your issues.

Running a "Clean" build

A. Backup your config file - Type

[COLOR="black"]cp .config ../backupConfig[/COLOR]

B. Re-run the build process using just your defconfig from earlier.

Limiting Output(Thanks Hacre.)

A. Another good tip is to run "make -j1" to get the error, as it will limit the amount of text you need to scroll through.

CCACHE​

Here is a brief tutorial on the installation of Ccache, which can shorten build times drastically :

If you have room on your build system you can speed

your builds up considerably by using ccache. ccache is a compiler

cache. It speeds up re-compilation of C/C++ code by caching

previous compiles and detecting when the same compile is

being done again.

Install it with apt-get:

[COLOR="Black"]sudo apt-get install ccache[/COLOR]

Set environment vars for Android use:

(I put these exports in my .bashrc)

[COLOR="Black"]export USE_CCACHE=1
export NDK_CCACHE=ccache[/COLOR]

Set a cache size of 10G for optimal results:

[COLOR="black"]ccache -M 10G[/COLOR]

Check the status of your cache with the command:

[COLOR="black"]ccache -s [/COLOR]

Returns:

[COLOR="black"]
cache directory /home/jocala/.ccache
cache hit 106673
cache miss 18379
called for link 3758
preprocessor error 5
not a C/C++ file 4425
unsupported compiler option 678
no input file 4565
files in cache 36758
cache size 3.2 Gbytes
max cache size 10.0 Gbytes[/COLOR]

Instructions for submitting to Gerrit are listed here: http://xdaforums.com/showpost.php?p=34095392&postcount=2588

ADVANCED INSTRUCTIONS FOR BUILDING CUSTOMIZATION

Special thanks to m4570d0n for putting this together for everyone

If you wanted to add something like, for example, Titanium Backup, you could go to /vendor/cm/config/common.mk and starting on line 98 where you see:

[COLOR="Black"]PRODUCT_COPY_FILES += \
vendor/cm/proprietary/RomManager.apk:system/app/RomManager.apk \
vendor/cm/proprietary/Term.apk:system/app/Term.apk \
vendor/cm/proprietary/lib/armeabi/libjackpal-androidterm4.so:system/lib/libjackpal-androidterm4.so \
vendor/cm/prebuilt/common/apps/Superuser.apk:system/app/Superuser.apk[/COLOR]

and add a line below that like this:

[COLOR="black"] vendor/cm/prebuilt/common/apps/TitaniumBackup.apk:system/app/TitaniumBackup.apk[/COLOR]

Then just place your TitaniumBackup.apk in the /vendor/cm/prebuilt/common/apps folder. You can modify that folder location to whatever folder you want to use, and you can substitute Titanium Backup with whatever apk you'd like. Take note though that this will install it as a system app in /system/app, while most apps would normally be installed as a user app in /data/apps. I think you could change the location to /data/app in that line, but I'm not positive. Someone please correct me if I'm wrong. I mean, I know you can, but I'm not sure if it would be an issue when you flash a zip without wiping /data and the zip file has files in /data.

You could also create your own github repo, name it something like vendor_onlychevys and add that repo to your local manifest with the path as vendor/onlychevys. Then you could use that as your directory path to the apps you want to add.

The slightly longer, but better way, is that you could also create your own make file in that repo by copying /device/samsung/skyrocket/cm.mk to your repo and rename it (let's say vendor_onlychevys/products/onlychevys_skyrocket.mk) and add a line in that file with something like this:

[COLOR="black"]# Inherit some common onlychevys stuff.
$(call inherit-product, vendor/onlychevys/config/common_full_phone.mk)[/COLOR]

and also editing the product name in that file near the bottom to something like this:

[COLOR="black"]PRODUCT_NAME := onlychevys_skyrocket[/COLOR]

You would need to make 3 more files.

In that same directory make a file called AndroidProducts.mk and in that file add this:

[COLOR="black"] PRODUCT_MAKEFILES := \
$(LOCAL_DIR)/onlychevys_skyrocket.mk[/COLOR]

Next, copy /vendor/cm/config/common_full_phone.mk to your repo at vendor_onlychevys/config/common_full_phone.mk. Then edit that file by adding a line in there with something like this:

[COLOR="black"]PRODUCT_COPY_FILES += \
vendor/onlychevys/prebuilt/common/apps/TitaniumBackup.apk[/COLOR]

And make sure to put your app in that prebuilt/common/apps/ directory in your repo.

Finally, create a new file in gedit (or other text editor of your choice). In that file add this:

[COLOR="black"] add_lunch_combo onlychevys_skyrocket-userdebug[/COLOR]

Name the file vendorsetup.sh and put it in your repo at vendor_onlychevys/vendorsetup.sh

Now, when you want to build you can run:

[COLOR="black"]. build/envsetup.sh
brunch onlychevys_skyrocket-userdebug[/COLOR]

This will be the same as if you used the regular command but will also include whatever apks you want to put in your repo, since you are just copying those files and adding a line to also include your stuff. This way, though you can add apps and/or make changes to your repo and you wont get errors about having local changes to vendor/cm or wherever when you try to run repo sync. You also wont have to keep deleting and re-adding the same files over and over after every sync because they will be in your own repo.

And just for clarity, your repo would look something like this:

[COLOR="black"]vendor_onlychevys
|-- config
| `-- common_full_phone.mk
|-- prebuilt
| `-- common
| | `-- apps
| | `-- TitaniumBackup.apk
|-- products
| |-- AndroidProducts.mk
| `-- onlychevys_skyrocket.mk
`-- vendorsetup.sh[/COLOR]

More advanced instructions to be posted as time allows. Now, get to work!

CHERRYPICKING: If you have progressed beyond the level of copying and pasting, and have begun researching the 'cherry-picking' process, there is a brief set of steps regarding this on post #1292 : http://xdaforums.com/showthread.php?t=1798056&page=130.

This should give those interested a basic idea of the process. More detailed instructions are going to be posted as time allows.

NOTE: the cherry-picks listed in that tutorial are no longer neccesary. They have been merged, and don't require picking. But, for the informational purposes, the steps are useful. Thanks to m4570d0n for laying that out for everyone!​