[Sprint LG Optimus G] BMP7777:
KERNEL BUILDING and other EXTRAS

Building your first kernel

(this is non-device-specific; tweaks will be required to produce an LS970 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]

​