[Sony Xperia P, U, Sola, Go] Garcia98:
How to build CyanogenMod 11 yourself

Here I'm going to explain you how can you build CyanogenMod 11 for your device in a few simple steps :)

Prerequisites

A Linux system with at least 4GB of RAM and 30GB of free disk space, Ubuntu 14.04 LTS is a recommended distribution. It is also possible to build Android in a virtual machine on unsupported systems such as Windows. If you are running Linux in a virtual machine, you need at least 16GB of RAM/swap in order to build the Android tree. Building under Mac OS is also possible but that process is not described in this guide.

A 64-bit environment.

A brain.

Setting up a Linux enviroment

Installing Java

First of all you need to install the Java Developer Kit, for CyanogenMod 11 we need Java 6 from Oracle.

So you need to get rid of any other Java version installed on your machine, also you need to add the repository from you will download Java, after that, you can install Java:

sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-java6-installer

Installing required packages

We need some build tools in order to compile Android, such as GNU Make, Python, or Git:

sudo apt-get install git gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so

Building CyanogenMod 11

To build CyanogenMod 11, firstly it is needed to download the sources, they weighs around 10GB. After the sources have been downloaded you can start building CyanogenMod 11. In CompileROM script you need to specify the device which you are building for; if you have a Xperia U use the option -ru, if you have a Xperia P use the option -rp, if you have a Xperia Go use the option -rg and if you have a Xperia Sola use the option -rs:

git clone https://github.com/XperiaSTE/android_scripts.git -b cm-11.0 cm-11.0
cd cm-11.0
repo init -u git://github.com/CyanogenMod/android.git -b cm-11.0
./Update.sh
export LD_LIBRARY_PATH=out/host/linux-x86/lib
./CompileROM.sh [ -ru / -rp / -rg / -rs ]
./CompileRamdisk.sh [ -ru / -rp / -rg / -rs ]
rm out/target/product/*/*.zip
./CompileROM.sh -ru

If everything went fine you will find flashable zip in out/target/product/<codename of your device>/cm-11-kumquat-blablabla.zip

For following builds commands change a bit, because you have to clean precompiled stuff before building again:

make clean && make clobber
./Update.sh
export LD_LIBRARY_PATH=out/host/linux-x86/lib
./CompileROM.sh [ -ru / -rp / -rg / -rs ]
./CompileRamdisk.sh [ -ru / -rp / -rg / -rs ]
rm out/target/product/*/*.zip
./CompileROM.sh -ru

Enjoy! :D

Tips

Time that takes building process depends on the power of your computer, but here you have some tips to speed up the process.

Enable ccache

You can optionally tell the build to use the ccache compilation tool. Ccache acts as a compiler cache that can be used to speed-up rebuilds. This works very well if you do "make clean" often, or if you frequently switch between different build products.

Put the following in your .bashrc or equivalent:

export USE_CCACHE=1

The suggested cache size is 50GB. You will need to run the following command once you have downloaded the source code:

prebuilts/misc/linux-x86/ccache/ccache -M 50G

Use a separate output directory

By default, the output of each build is stored in the out/ subdirectory of the matching source tree.

On some machines with multiple storage devices, builds are faster when storing the source files and the output on separate volumes, so if you have a SSD you definitely want that the output directory is in it. For additional performance, the output can be stored on a filesystem optimized for speed instead of crash robustness, since all files can be re-generated in case of filesystem corruption.

To set this up, export the OUT_DIR_COMMON_BASE variable to point to the location where your output directories will be stored:

export OUT_DIR_COMMON_BASE=<path-to-your-out-directory>

Buy more RAM

The RAM is usually the easiest component to change in a PC, and it is not very expensive, and having more RAM makes a difference in build time. So I recommend you to use so much RAM as your motherboard supports.

Questions?​

Every command is written without su access and commands may vary in differents Linux distributions.

Sources: Android Open Source Project