[Samsung Galaxy Nexus] mbroeders:
Since I’ve seen many questions on how to build cyanogenmod 9 (CM9) from source for the Galaxy Nexus, but there isn’t a proper guide, I will attempt to write a small how-to. There already is a very good guide how to build ICS from source, but there are a few extra things you’ll have to do for CM9. I hope it will be useful, and if not, well, at least I’ve tried :)

LATEST UPDATE: August 20th - also added CM10

SETTING UP THE BUILD ENVIRONMENT

I highly recommend Ubuntu 12.04 64 bit for development or Linux Mint 13. It is possible to build on different linux distro’s, but I cannot cover all exceptions. (If you don’t have linux installed or are afraid to set up a dual boot, it is possible to build in a virtual environment –e.g. virtualbox-. Building in a virtual environment however, can be very slow. Also, 64 bit is recommended.)

Make sure java is installed! At the end of this post, I have written a small guide how to install java.

Set up adb and create proper udev rules

I will not write these steps down, but rather point you to some very nice and easy guides. It would be best to do this first, however, it is not completely necessary if you just want to build a fully functioning rom.

1) set up adb (follow this excellent guide)

2) set up udev rules which allow you to start adb without having to use sudo (follow this terrific tutorial)

Installing all necessary packages and set up repo (source)

Open a terminal and copy the following code:

sudo apt-get install git-core gnupg flex bison gperf build-essential \
zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \
libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev:i386 \
g++-multilib mingw32 openjdk-6-jdk tofrodos python-markdown \
libxml2-utils xsltproc zlib1g-dev:i386

WARNING: run the following commands as user (NOT as root) unless stated otherwise (e.g., when it explicitly shows ‘sudo’ before a command)!!!

Next, you’ll have to install repo to download the source. First we’re going to create a bin folder (1) in our home directory and include it in our path (2). Also, download the repo script (3) and make it executable (4). All from the command line:

mkdir ~/bin
PATH=~/bin:$PATH
curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
chmod a+x ~/bin/repo

Okay, we’re done with the first part. So far it’s been similar to building pure AOSP.For CM, there will be some additional things you’ll have to do.

DOWNLOADING THE CM SOURCE

Create a directory (CM9 -or CM10-) for your working files:

mkdir CM9
cd CM9

and then initialize the main CM repo (For CM10, just replace ics with jellybean):

repo init -u git://github.com/CyanogenMod/android.git -b ics

Good, now you’re ready to download the source. This can take a couple of hours!! Run the following command from the terminal (Run the following commands in the terminal from the root of the directory that contains the source, e.g., ~/CM9/):

repo sync

Okay, the majority of the files needed to build CM are now on your computer. However, device specific files are needed. To get them, issue the following command in your terminal:

source build/envsetup.sh
lunch

After the lunch command, choose your device. If you have a GSM version, choose cm_maguro, if you have the CDMA version, choose cm_toro. Additional files needed for your device are being downloaded right now.

Before you can actually build the rom, you’ll need to run two more commands to get some proprietary files.

1) Open a terminal and go to CM9/vendor/cm/. Run the following command:

./get-prebuilts

This will download term.apk and rommanager.apk. You will need these files otherwise you’ll get an error while building.

2) Now we need to grab some files from your phone. Make sure you have a working build cyanogenmod version (just install a nightly) on your phone. Make sure adb is setup properly (see beginning of this post)!

Connect your phone to the pc. Open a terminal and go to CM9/device/samsung/(Maguro OR Toro)/. Run the following command:

sh extract-files.sh

BUILDING CM9/CM10

The building part is very easy. It just requires two simple commands:

source build/envsetup.sh
brunch

After the brunch command, choose your device. Again, if you have a GSM version, choose cm_maguro, if you have the CDMA version, choose cm_toro. Depending on your computer, hopefully you’ll have a fully functioning CM9 or CM10 in 30minutes-2hours (or even longer) . You can find the rom in: /out/target/product/(Maguro OR Toro)/

Next time you build, first clean your working directory. Enter the following command in the terminal:

make clobber

This will completely remove your output directory!

To update the source, before each build just run:

repo sync

ADDITIONAL INFO

Install Java

Installing java is very easy in Ubuntu 12.04. Java 6 is recommended. To install it in Ubuntu 12.04 or Linux Mint 13, download the most recent Java 6 SDK from HERE. To install, open a terminal and run the following commands:

$ chmod +x jdk-6u34-linux-x64.bin
$ sudo ./jdk-6u34-linux-x64.bin
$ sudo mv jdk1.6.0_34 /usr/lib/jvm/

$sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_34/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_34/bin/javac 1
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_34/bin/javaws 1

$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
$ sudo update-alternatives --config javaws

NOTE: after each 'update-alternative'-command, choose the correct (new) java version!

To check if you have the correct java version, type in a terminal:

java –version

I also added JAVA_HOME to my path; I don’t know if it is still necessary, but it doesn’t hurt either. First, check where java is located. In a terminal type:

which java

In my case the output shows /usr/bin/java, but it could be located somewhere else. Write down the path minus '/java'. Then open /home/USERNAME/.bashrc and add the following line to the bottom of the file:

export JAVA_HOME=/usr/bin

Of course replace /usr/bin with your path. Then save and close, and in a terminal run:

source ~/.bashrc

Odexed version

Some people like their rom to be odexed. There are multiple ways to achieve this (special thanks to Planet X for helping me with this):

1)Instead of ‘brunch’ do the following (if you are building for toro, replace maguro with toro):

source /build/envsetup.sh
lunch cm_maguro-user
make –j4

(Note:

taken from source.android.com: GNU make can handle parallel tasks with a -jN argument, and it's common to use a number of tasks N that's between 1 and 2 times the number of hardware threads on the computer being used for the build. E.g. on a dual-E5520 machine (2 CPUs, 4 cores per CPU, 2 threads per core), the fastest builds are made with commands between make -j16 and make -j32.)

2)If you want to build an odexed version every time and just want to use the brunch command, do the following:

-Open build/core/main.mk

-Comment out (place a # at the beginning of the line) lines 240, 241, 245, and 246. Thus, replace:

Now you can use the brunch command to build an odexed version with insecure boot image.

Hopefully this guide will benefit some people, if not, it kept me busy for a while. Enjoy building!!

(btw, I'm not a native english speaker, so excuse me if I made errors in grammar :) )