Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,731,559 Members 51,829 Now Online
XDA Developers Android and Mobile Development Forum

[GUIDE][ICS] Compile Cyanogenmod 9 on Mac OS X Lion

Tip us?
 
ArmanUV
Old
(Last edited by ArmanUV; 12th June 2012 at 01:16 AM.)
#1  
ArmanUV's Avatar
Senior Member - OP
Thanks Meter 222
Posts: 825
Join Date: Jan 2012
Default [GUIDE][ICS] Compile Cyanogenmod 9 on Mac OS X Lion

There are a lot of toturials for building CM9 on ubuntu or CM7 on Mac but I couldn't find a decent toturial for building CM9 on Mac (specially Lion). Development in AOSP/CM land is rapid and guides frequently need updating. I had to spend a little time to figure everything out and I decided to share it here.
This tutorial is for building CM9 (ICS) for Galaxy Nexus GSM (maguro) on Mac OS X Lion 10.7.3 using Xcode 4.3 and homebrew . You can easily make the instructions work for most other cm9 devices, but I wouldn't know anything about that.
DISCLAIMER: I'm not responsible if you blow yourself up, blah blah blah
However, I've tried to make this as noob friendly as possible because, well I'm a noob myself


Instrunctions:

UPADTE (MAY 29TH) : The Xcode 4.3 default compiler (llvm-gcc) used to be incompatible with CM9. Thanks to jocelyn and topprospect, the LLVM compatibility patches from mainline AOSP are now merged into CM9. Therefore, you can now use Xcode 4.3 and its command line tools without installing another compiler. However, since GCC is still the only officially supported compiler, incompatibilites with llvm-gcc could still be introduced with future updates. Therefore, if your build fails, it might be worth it to try installing and compiling with GCC 4.2. See the Troubleshooting section for more info.

Now that we have Xcode 4.3 and Xcode command line tools (CLT) installed, let's continue.
  • Open Terminal and run
    Code:
    java
    if you don't have Java, you will get a prompt asking you to download and install Java. Go ahead and install it.
  • If you don't have adb and fastboot working, download the android-sdk from google (version r18 as of now) and put it in /usr/local/ and rename the folder to "android-sdk".
  • Install the homebrew package manager
    Code:
    /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
  • To make sure that homebrew and android-sdk executables are in $PATH:
    Code:
    touch ~/.bash_profile && echo "PATH=/usr/local/bin:/usr/local/sbin:$PATH:/usr/local/android-sdk/tools:/usr/local/android-sdk/platform-tools" >> ~/.bash_profile
    Relaunch Terminal for the change to take effect.
  • At this point you can run
    Code:
    brew doctor
    to detect any problems there might be (Homebrew may instruct you to use the xcode-select utility to select the xcode installation path). Hopefully, your system is raring to brew
  • Now we have to install a bunch of packages:
    Code:
    brew install git coreutils findutils gnu-sed gnupg pngcrush repo
  • We now need to create a couple of symlinks so that the gnu versions of 'sed' and 'find' are used rather than the osx provided versions :
    Code:
    ln -s /usr/local/bin/gfind /usr/local/bin/find && ln -s /usr/local/bin/gsed /usr/local/bin/sed && ln -s /usr/local/bin/gstat /usr/local/bin/stat
  • It's time to create a case sensitive image which will hold our working directory:
    Code:
    hdiutil create -type SPARSE -fs "Case-sensitive Journaled HFS+" -size 40g -volname "android" -attach ~/Desktop/Android
    Now we have a disk image in ~/Desktop/Android. Mount it if it's not mounted already. (Don't be picky about the size, the image will only take as much as space as its contents).
  • Now we need to create a working directory inside the mounted volume:
    Code:
    cd /Volumes/android && mkdir cm9 && cd cm9
  • We can initialize and download the source now:
    Code:
    repo init -u git://github.com/CyanogenMod/android.git -b ics && repo sync && say 'finished'
  • Now we need to get the required proprietry files for our device. We can get these from the device itself. Connect your phone (make sure USB Debugging is enabled) and run the following (for maguro):
    Code:
    cd /Volumes/android/cm9/device/samsung/maguro/ && ./extract-files.sh
    [If you see errors in the output from extract-files.sh, see the Troubleshooting section below]
    For Google devices,we can also get them directly from google. For maguro, download the 3 files and extract them to /Volumes/android/cm9. Then,
    Code:
    cd /Volumes/android/cm9
    /Volumes/android/cm9/extract-broadcom-maguro.sh
    /Volumes/android/cm9/extract-imgtec-maguro.sh
    /Volumes/android/cm9/extract-samsung-maguro.sh
  • We also need the prebuilts (like ROM manager and Term.apk):
    Code:
    /Volumes/android/cm9/vendor/cm/get-prebuilts
  • You can optionally tell the build to use the ccache tool. CCache acts as a compiler cache that can be used to speed-up rebuilds :
    Code:
    export USE_CCACHE=1 && /Volumes/android/cm9/prebuilt/darwin-x86/ccache/ccache -M 20G
    Default is 1GB. Anything between 20GB-50GB should be fine.
  • Before starting the build, we need to workaround an issue with Lion and compiling the QEMU emulator.
    [This step doesn't seem to be needed anymore. QEMU is automatically ignored on OS X/Darwin]
  • If you build now, you're probably gonna get kernel build errors regarding the missing elf.h header (this error might be device specific). Fortunately, we already have this file downloaded, so we only need to copy it to /usr/local/include:
    Code:
    cp /Volumes/android/cm9/external/elfutils/libelf/elf.h /usr/local/include
  • FINALLY, we are ready to build:
    Code:
    cd /Volumes/android/cm9 && source build/envsetup.sh && brunch
    Pick your device from the list and enter the number. For maguro, you could use "brunch maguro" instead and skip the menu. Depending on your system, this will take 30min-4hours.
  • You should now see a beautiful zip file waiting to be flashed:

Troubleshooting:
  • The extract script for maguro seems to be a little outdated, as it doesn't pull the gps proprietary blob. You can either use the google provided scripts, or add koush's git repository for your device to your local_manifest.xml.
  • As explained above, the CM9 source is currently compatible with llvm-gcc. In the future, if llvm-gcc fails to build correctly, you should try installing and compiling using GCC4.2 (the Xcode 3 compiler). You can install apple-gcc4.2 from homebrew:
    Code:
    brew install https://raw.github.com/Homebrew/homebrew-dupes/master/apple-gcc42.rb
    This version of gcc can happily coexist with Xcode 4.x .
    So now you have GCC 4.2 installed, but it won't be used unless we update the corresposing environment variables:
    Code:
    export CC=/usr/local/bin/gcc-4.2 && export CXX=/usr/local/bin/g++-4.2
    Notice that using the export command is temporary. If you relaunch Terminal, you will need to set these again. However, this is a good thing, because changing these values permanently (by putting them in ~/.bash_profile) can interfere with other builds.
    If you use this method, the build might fail while compiling "external/zlib/x86/adler32.c". It appears that a recent change in zlib has introduced an incompatibility with gcc 4.2. you have to revert the following 2 commits:
    Code:
    cd external/zlib
    git revert dd6786cae3f4493faa6661d5f74db587932f15d7
    git revert 13bf40af68236c961542bdee1d4b7c0176bf15a0
    Alternatively, you can add topprospect's zlib on github (which has those commits reverted) to your local_manifast.xml. Simply run:
    Code:
    nano /Volumes/android/cm9/.repo/local_manifest.xml
    and add the following line
    Code:
    <project name="dferg/android_external_zlib" path="external/zlib" remote="github" />
  • If you get a build error, and your error is not covered here, copy the last 20-30 lines of the build output AND the output from the following command into pastebin and post the link. Hopefully me or someone else will help you.
    Code:
    echo -e "\nENV:\n$(env)\n\nWHICH GCC\n:$(which gcc)\n\nWHICH G++:\n$(which g++)\n\nWHICH CC:\n$(which cc)\n\nWHICH C++:\n$(which c++)\n\nBREW DOCTOR:\n$(brew doctor)\n\nBREW LIST:\n$(brew list)\n\n/USR/BIN:\n$(ls -l /usr/bin | grep gcc)\n\n/USR/LOCAL/BIN:\n$(ls -l /usr/local/bin | grep gcc)\n\n"
Notes/Extras:
  • To quickly setup your environment, add an alias like the following to ~/.bash_profile:
    Code:
    alias cm9env="hdiutil attach PATH-TO-DISK-IMAGE -mountpoint /Volumes/android && cd /Volumes/android/cm9 && source ./build/envsetup.sh && export USE_CCACHE=1"
    alias cm9build="cm9env && make clobber && reposync && brunch maguro"
    Now you can save time by using "cm9env" to get your environment setup or "cm9build" to compile a clean updated build.
  • To clear your output directory for a new build, run "make clobber". You probably don't need this if you've only changed a few lines of code.
  • To cherry pick yet-to-be-merged changes from the gerrit instance:
    Quote:
    1. Pick an open commit from CM Gerrit
    2. Under list of Patch Sets pick the latest and open cherry-pick tab
    3. Check what Git repository the url is pointing e.g. http://review.cyanog...frameworks_base
    4. In your CM9 working tree go to the corresponding directory, which in this case is something like ~/your-working-directory/frameworks/base/
    5. Now simply paste the whole line seen in CM Gerrit cherry-pick tab e.g. "git fetch http://review.cyanog....rameworks_base refs/changes/00/13100/4 && git cherry-pick FETCH_HEAD"
    It should be now included in your next compiled build. When doing repo sync again, cherry picks will be lost.[CREDIT Fihlvein from xda]
    Enjoy your custom built CM9!
Galaxy Nexus GSM
The Following 17 Users Say Thank You to ArmanUV For This Useful Post: [ Click to Expand ]
 
ArmanUV
Old
#2  
ArmanUV's Avatar
Senior Member - OP
Thanks Meter 222
Posts: 825
Join Date: Jan 2012
I updated OP with some updated info about Xcode 4.3.
Galaxy Nexus GSM
 
conantroutman
Old
#3  
conantroutman's Avatar
Moderator Committee / XDA Portal Team - Title Under Construction. Please Pardon Our Dust
Thanks Meter 6989
Posts: 13,946
Join Date: Dec 2009
Nice work man. Very helpful.

"If Diane Young won't change your mind......"

HTC HD2, Samsung Galaxy SII, Note II, Nexus 7 2012 & Motorola Xoom

The Following 2 Users Say Thank You to conantroutman For This Useful Post: [ Click to Expand ]
 
ArmanUV
Old
#4  
ArmanUV's Avatar
Senior Member - OP
Thanks Meter 222
Posts: 825
Join Date: Jan 2012
Quote:
Originally Posted by conantroutman View Post
Nice work man. Very helpful.
thanks! I'll try to keep this topic updated as issues with mac are introduced/resolved.
Galaxy Nexus GSM
 
grad061980
Old
#5  
Senior Member
Thanks Meter 36
Posts: 148
Join Date: Dec 2010
Nice write up. Thanks.

Before I start again from scratch I have a question. Does this guide apply to previous versions of mac os x (mine is 10.6.8)? I used the official android initializing build environment page & cm7 wiki page for the instructions to setup my build environment.

Also, any tips to switch from macports to homebrew?

In the past I've had to cherry pick to get my OS X build environment set up for CM9. The compile from source fails because I started with macports instead of homebrew (bad idea). I tried to switch to homebrew without success. Any tips to switch from macports to homebrew?

For the sake of keeping this page on topic a pm response is ok if that is what you prefer.
 
WonkyYew
Old
#6  
Junior Member
Thanks Meter 8
Posts: 25
Join Date: Dec 2011
Location: Dublin
Hi thanks for this !! Helpful one question what do i change so i can do AOSP instead of cm9??
 
ArmanUV
Old
(Last edited by ArmanUV; 22nd May 2012 at 01:32 AM.)
#7  
ArmanUV's Avatar
Senior Member - OP
Thanks Meter 222
Posts: 825
Join Date: Jan 2012
Quote:
Originally Posted by grad061980 View Post
Nice write up. Thanks.

Before I start again from scratch I have a question. Does this guide apply to previous versions of mac os x (mine is 10.6.8)? I used the official android initializing build environment page & cm7 wiki page for the instructions to setup my build environment.

Also, any tips to switch from macports to homebrew?

In the past I've had to cherry pick to get my OS X build environment set up for CM9. The compile from source fails because I started with macports instead of homebrew (bad idea). I tried to switch to homebrew without success. Any tips to switch from macports to homebrew?

For the sake of keeping this page on topic a pm response is ok if that is what you prefer.
Yes, this guide should work fine on Snow Leopard. It mostly depends on your Xcode version. If you have Xcode 3, you can skip step 1 entirely, since you already have gcc4.2 as part of Xcode.
If you have access to Xcode 4.2 and above, you will need to install gcc4.2 separately, as explained in the guide.

Now regarding Macports, I strongly suggest that you completely uninstall Macports before installing homebrew. Instructions are here: http://guide.macports.org/chunked/in...nstalling.html

Quote:
Originally Posted by WonkyYew View Post
Hi thanks for this !! Helpful one question what do i change so i can do AOSP instead of cm9??
Full instructions are available on android.com : http://source.android.com/source/initializing.html
If you are using this guide, you need to change the repo initialization command to :
Code:
repo init -u https://android.googlesource.com/platform/manifest
and then do repo sync. You can setup ccache as usual. I don't think AOSP has the "brunch command", so you have to use launch and then make.
Run "lunch" and select an option from the menu. You can find more info about the options here: http://source.android.com/source/building.html. For maguro, you should use "full_maguro-userdebug".
To start the build, use
Code:
make -j$(sysctl -n hw.ncpu)
Galaxy Nexus GSM
The Following User Says Thank You to ArmanUV For This Useful Post: [ Click to Expand ]
 
grad061980
Old
#8  
Senior Member
Thanks Meter 36
Posts: 148
Join Date: Dec 2010
@ArmanUV. Sounds good & thanks for the input. I'll give the link to macports uninstall a go.
 
the5
Old
#9  
Senior Member
Thanks Meter 14
Posts: 350
Join Date: Jan 2011
Im a real noob, whats the advantage of compiling from source?
 
WonkyYew
Old
#10  
Junior Member
Thanks Meter 8
Posts: 25
Join Date: Dec 2011
Location: Dublin
Thanks for the help man !

Tags
build, cm9, cyanogenmod, ics, mac
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes