[Sprint HTC One (M7)] BMP7777:
KERNEL BUILDING and other EXTRAS

Building your first kernel

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

Building TWRP recovery - http://xdaforums.com/showthread.php?t=1943625​

Building CWM recovery - http://xdaforums.com/showthread.php?t=1866545​

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]

To clear your ccache:

[COLOR="Black"]ccache -C[/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]

Using Git and learning to push commits - WIP

- When you "create" a new repo it has to be named correctly and the rest will follow with a tut on how to make your initial commit. Well once you start compiling and changing things, it can get pretty difficult to figure out how to push your commits. I'm not gonna get into setting up github because there are several tutorials for that. Ok let's say you've changed some things and try to run a "repo sync" and you get ERROR: you must stash or commit your changes or they will be lost during checkout. Hmm what do we do here? Well I track down the folder which has the changed commit and open it in terminal. Here I use something like "git checkout kk44" since this is my branch name. Sometimes if I get an error I will use "git branch kk44" I don't know the difference but I always seem to get the job done. I'll break this down later into just the cmds by themselves. Now the commands are not all the same & you need different ones for rebasing your repo but for now we want to commit our device changes. Below I will list some things that eventually work to allow me to push my commits to my git.

Code:

UPDATE existing branch!

git checkout kk44

"or"

git branch kk44

git init

git update-server-info

git add <any changes here>

git commit -m <Your message for the commit>

.. You need to be on the specific branch

git push origin kk44 <here put in your username & PW for git>

git rm <filename>

to remove a file if the build did NOT work for you!

Sometimes the above don't always work so we go below. This will be a way to "rebase" your repo & in some cases commit your work. I'm not even close to being an avid git user but I'm trying here!

Thanks to Scotthartbti for pointing some of the below out to me.

Quote:

- Wherever changes are made do the following to save it.

git add -A

git commit -s

put a title for it <my change for BS>

then hit CTRL X & Y

git push remote_name branch_name..

The above will get you some place and if it doesn't, try the below. I believe below are commands to "rebase" a repository?

Code:

git pull origin kk44

git push origin kk44

If you do the above steps it will usually lead you to what needs to be committed and you can "git add boardconfigcommon.mk <this is for informational purposes only!

"git add ." is supposed to add all files to be committed but I find it doesn't work most times. You can always go to your github to see what you have named your device tree.

I really just play around with most of these cmds until I get my repo to commit. If you get a message stating "nothing to commit, up to date" but you know you have files to commit, keep working with different cmds until you push your work. Hopefully this guide will get more clear as others help out or as I learn more.

Building with Linaro 4.8 toolchain

Go into Terminal and navigate to the root folder of your source tree, then:

[COLOR="Black"]prebuilts/gcc/linux-x86/arm/[/COLOR]

Run:

[COLOR="black"]wget http://releases.linaro.org/13.06/components/toolchain/gcc-linaro/4.8/gcc-linaro-4.8-2013.06.tar.bz2[/COLOR]

Run:

[COLOR="black"]bunzip2 *.tar.bz2[/COLOR]

Run:

[COLOR="black"]tar -xvf *.tar[/COLOR]

Using your file manager, navigate to the root folder of your source tree again.

Navigate To "/build", in your root folder.

Run:

[COLOR="black"]gksudo gedit envsetup.sh[/COLOR]

change the line in bold, to look exactly like this one:

[COLOR="black"]case $ARCH in
x86) toolchaindir=x86/i686-linux-android-4.6/bin
;;
[B]arm) toolchaindir=arm/gcc-linaro-4.8-2013.06/bin[/B] [COLOR="Red"]<--------[/COLOR]
;;
mips) toolchaindir=mips/mipsel-linux-android-4.6/bin
;;
*)
echo "Can't find toolchain for unknown architecture: $ARCH"
toolchaindir=xxxxxxxxx
;;
esac
[/COLOR]

Exit out and open new. Initialize and build. That's it! Makes you wonder why you never did it before, right?

Not so fast. You may have build issues, you may not. I had a few lengthy sets of errors in gsm calltracker and another .java file that was solved by using Meld and making it look exactly like a successful build's file. More as I learn.......

​