[TUTORIAL]Setting up and compiling CM9/CM10 from source

Search This thread

mbroeders

Senior Member
May 6, 2010
959
221
Amsterdam
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:
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:
Code:
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:
Code:
mkdir CM9
cd CM9
and then initialize the main CM repo (For CM10, just replace ics with jellybean):
Code:
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/):
Code:
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:
Code:
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:
Code:
./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:
Code:
sh extract-files.sh


BUILDING CM9/CM10
The building part is very easy. It just requires two simple commands:
Code:
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:
Code:
make clobber
This will completely remove your output directory!
To update the source, before each build just run:
Code:
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:

Code:
$ 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:
Code:
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:
Code:
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:
Code:
export JAVA_HOME=/usr/bin
Of course replace /usr/bin with your path. Then save and close, and in a terminal run:
Code:
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):
Code:
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:
ifneq (true,$(DISABLE_DEXPREOPT))
ifeq ($(user_variant),user)
Ifeq ($(HOST_OS),linux)
WITH_DEXPREOPT := true
Endif
endif
endif


# ifneq (true,$(DISABLE_DEXPREOPT))
# ifeq ($(user_variant),user)
Ifeq ($(HOST_OS),linux)
WITH_DEXPREOPT := true
Endif
# endif
# endif

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 :) )
 
Last edited:

mbroeders

Senior Member
May 6, 2010
959
221
Amsterdam
So what does that "brunch" command actually do?

Is it just a script that does the make otapackage commands and stuff?

Indeed! Brunch is actually a combination of 'lunch' and 'make'. Using the 'lunch-part' you choose your build (in our case the maguro or toro). Next, the 'make-part' actually gets things going. The nice thing using brunch is that it automatically detects the maximum number of threads it can use so that it will run at maximum speed.
 
  • Like
Reactions: inffy

SupWiz17

Senior Member
Jan 14, 2012
590
67
Chicago, IL
Finally set my computer up to compile CM9 last night for the first time and after a few tries I finally got it going. The only problem is that I am now getting an error very close to this one.

http://xdaforums.com/showpost.php?p=25452343&postcount=3093

That poster says that it has been happening for a few days now. Is this a known issue with compiling CM9 or is this just user error? I have tried twice compiling now and have had no luck. Is it working for anyone else?

---------- Post added at 02:05 PM ---------- Previous post was at 01:19 PM ----------

Here is the exact error that Im getting. I tried it again today just to see if it anything had changed.

make[1]: *** [sub-make] Error 2
make[1]: Leaving directory `/home/mark/CM9/kernel/samsung/tuna'
make: *** [TARGET_KERNEL_BINARIES] Error 2
make: *** Waiting for unfinished jobs....
 

housry23

Senior Member
Sep 23, 2009
2,939
746
Springfield, OH
Thanks for this guide. I just built my first CM9 kang. Getting ready to flash it. Now to find a guide on what I can and can't change and recompile or do I just remove stuff and use 7-zip to zip it back up?
 

mbroeders

Senior Member
May 6, 2010
959
221
Amsterdam
Thanks for this guide. I just built my first CM9 kang. Getting ready to flash it. Now to find a guide on what I can and can't change and recompile or do I just remove stuff and use 7-zip to zip it back up?

Glad to hear you succeeded building your first kang. I don't really understand what you want to do next? If you want to remove stuff from the zip, you can. I for instance always remove stk.apk. But you can also modify the build files so that only things you want will be built. You can play around with the source code and things like that. Anyway, most of the answers you will find using google. I also very much like the development board on Rootzwiki; people are really helpful and friendly there. So if you have any specific questions, i recommend that forum as well!
 
  • Like
Reactions: housry23

housry23

Senior Member
Sep 23, 2009
2,939
746
Springfield, OH
Glad to hear you succeeded building your first kang. I don't really understand what you want to do next? If you want to remove stuff from the zip, you can. I for instance always remove stk.apk. But you can also modify the build files so that only things you want will be built. You can play around with the source code and things like that. Anyway, most of the answers you will find using google. I also very much like the development board on Rootzwiki; people are really helpful and friendly there. So if you have any specific questions, i recommend that forum as well!

Okay thanks. I was asking just what you answered. I want to be able to remove and/or add stuff to the zip for starters. I found the answer through Google, but I do appreciate you taking the time to answer. I'll definitely be visiting the Rootzwiki dev board. Thanks for the suggestion.
 
  • Like
Reactions: mbroeders

SupWiz17

Senior Member
Jan 14, 2012
590
67
Chicago, IL
I have successfully compiled cm9 from source but have never tried to cherry pick or Kang anything yet. Could you quickly explain how you cherry pick with cm9?

Sent from my Galaxy Nexus using Tapatalk 2
 

Dreamboxuser

Senior Member
Jan 24, 2012
380
83

mbroeders

Senior Member
May 6, 2010
959
221
Amsterdam
That's a very useful link, thanks! In addition, if you want to cherry pick commits that haven't been merged yet -specific CM commits, such as navbar customization-, have a look here: http://review.cyanogenmod.com/#/q/branch:ics,n,z
Now let's say you see something interesting that you want to add. Then look at that commit and you'll see a 'download' command, such as "git fetch http://review.cyanogenmod.com/CyanogenMod/android_frameworks_base refs/changes/06/13306/15 && git checkout FETCH_HEAD". Just run that command and if everything works, you have succesfully cherry picked a commit. -of course, because these are not yet merged, there is the chance that no everything will work as it should-
 

Dreamboxuser

Senior Member
Jan 24, 2012
380
83
There are also some GUI programs to manage git. I'm just about to try gitgui by all accounts it is very good.

Sent from my GT-S5360 using Tapatalk 2
 

UncleDan

Senior Member
Feb 6, 2012
307
102
49
Ferrara
www.uncledan.it
Maybe it is a noobish question, but does this line:
Code:
curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > /bin/repo

miss a ~?

Code:
curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
 
  • Like
Reactions: mbroeders

mbroeders

Senior Member
May 6, 2010
959
221
Amsterdam
Maybe it is a noobish question, but does this line:
Code:
curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > /bin/repo

miss a ~?

Code:
curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo

Not noobish at all! You're absolutely right. It's not necessary when you're already in your home folder, but to be sure I've changed it in the guide. Thanks for letting me know! :)
 

llohcyoing

Senior Member
Mar 20, 2011
221
22
TORINO
im sorry for noobish...at this step

1) Create an empty file in ~/CM9/.repo

in home i have dir CM9 but its empty...i havent a folder call .repo

java its installed and adb work perfectly


this is my terminal output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 20774 100 20774 0 0 35679 0 --:--:-- --:--:-- --:--:-- 56604
llo@llo-System-Product-Name:~$ chmod a+x ~/bin/repo
llo@llo-System-Product-Name:~$ cd CM9
llo@llo-System-Product-Name:~/CM9$ repo init -u git://github.com/CyanogenMod/android.git -b ics

Your Name [loris]: loris
Your Email : my email

Your identity is: loris
is this correct [y/N]? y

repo initialized in /home/llo/CM9
llo@llo-System-Product-Name:~/CM9$

and nothing...


SOLVED
 
Last edited:

CdTDroiD

Inactive Recognized Developer
May 1, 2011
3,527
3,532
Auckland
Hey mate any idea why my camera never works after a build... I know I'm missing something but I'm not sure what. I have all the proprietary files for my maguro etc but I just can't get camera to work... everytime

Sent from my Galaxy Nexus using Tapatalk 2
 

inffy

Senior Member
Jun 26, 2005
3,442
2,390
Helsinki, Finland
Hey mate any idea why my camera never works after a build... I know I'm missing something but I'm not sure what. I have all the proprietary files for my maguro etc but I just can't get camera to work... everytime

Sent from my Galaxy Nexus using Tapatalk 2

And you flash gapps everytime?

Sent from my GT-I9300 using Tapatalk 2
 

Top Liked Posts

  • There are no posts matching your filters.
  • 25
    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:
    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:
    Code:
    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:
    Code:
    mkdir CM9
    cd CM9
    and then initialize the main CM repo (For CM10, just replace ics with jellybean):
    Code:
    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/):
    Code:
    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:
    Code:
    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:
    Code:
    ./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:
    Code:
    sh extract-files.sh


    BUILDING CM9/CM10
    The building part is very easy. It just requires two simple commands:
    Code:
    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:
    Code:
    make clobber
    This will completely remove your output directory!
    To update the source, before each build just run:
    Code:
    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:

    Code:
    $ 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:
    Code:
    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:
    Code:
    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:
    Code:
    export JAVA_HOME=/usr/bin
    Of course replace /usr/bin with your path. Then save and close, and in a terminal run:
    Code:
    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):
    Code:
    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:
    ifneq (true,$(DISABLE_DEXPREOPT))
    ifeq ($(user_variant),user)
    Ifeq ($(HOST_OS),linux)
    WITH_DEXPREOPT := true
    Endif
    endif
    endif


    # ifneq (true,$(DISABLE_DEXPREOPT))
    # ifeq ($(user_variant),user)
    Ifeq ($(HOST_OS),linux)
    WITH_DEXPREOPT := true
    Endif
    # endif
    # endif

    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 :) )
    3
    This is an excellent guide. A quick question. Under what directory do I run the command for cherrypicking? When I run it I get an error "fatal: Not a git repository (or any of the parent directories): .git" Also, do you have to re-cherrypick after every repo sync? I've never cherrypicked before so I'm not sure.

    example: link

    project: $BUILD_TOP/frameworks/base

    cd frameworks/base
    git branch whatever_branch_name_you_want github/jellybean
    git checkout whatever_branch_name_you_want
    git fetch http://review.cyanogenmod.com/CyanogenMod/android_frameworks_base refs/changes/91/23491/2 && git cherry-pick FETCH_HEAD

    // this will switch to project dir (android is made of lot's of them), create a new (local) branch which follows remote branch 'jellybean', switch to
    // that branch (using 'git checkout'), and cherry-pick (commits) change Idf086f35 to your local branch (since you already switched to it). Now,
    // you could just return to $BUILD_TOP and issue 'source build/envsetup.sh && brunch cm_maguro-full', to have the rom built WITH the cherry-
    // pick you just pulled.
    // Don't cherry-pick on a remote branch, you'll be left in detached head mode. Always in a branch that you created. Of course, you can have many branches on the
    // same project, but the last one you checked out will be the one that will get built.

    Also, do you have to re-cherrypick after every repo sync? I've never cherrypicked before so I'm not sure.

    no, if when you create a new branch, you set it to track remote branch; if you don't set it, everytime you repo sync you'll see that the projects you had branches on, will switch to the local "master" branch, identified by "*(no_branch)", if i recall it right (i'm pulling this out of my head right now).
    also. THERE'S a LOT of tutorials on how to git, git isn't a tool made for aosp alone.
    2
    That's a very useful link, thanks! In addition, if you want to cherry pick commits that haven't been merged yet -specific CM commits, such as navbar customization-, have a look here: http://review.cyanogenmod.com/#/q/branch:ics,n,z
    Now let's say you see something interesting that you want to add. Then look at that commit and you'll see a 'download' command, such as "git fetch http://review.cyanogenmod.com/CyanogenMod/android_frameworks_base refs/changes/06/13306/15 && git checkout FETCH_HEAD". Just run that command and if everything works, you have succesfully cherry picked a commit. -of course, because these are not yet merged, there is the chance that no everything will work as it should-
    1
    Wow great work. These tutorials keep getting better making it so easy to compile your own rom!
    1
    So what does that "brunch" command actually do?

    Is it just a script that does the make otapackage commands and stuff?

    Indeed! Brunch is actually a combination of 'lunch' and 'make'. Using the 'lunch-part' you choose your build (in our case the maguro or toro). Next, the 'make-part' actually gets things going. The nice thing using brunch is that it automatically detects the maximum number of threads it can use so that it will run at maximum speed.