✏ Porting CyanogenMod to FP2

This is a Wiki topic, feel free to contribute using the green pencil symbol in the top right corner.


Continuing the discussion from Fairphone 2 and CyanogenMod, please ask your question on this topic.

If you need help or want to contribute, feel free to join the dedicated matrix.org room #wearefairphone:matrix.org or the wearefairphone IRC channel on Freenode.net (the two are bridged so you only need to join one of them).

This topic explains how to build CyanogenMod for Fairphone2. It has been tested on Ubuntu 16.04 64 bits and Debian Jessie 64 bits

:warning: Currently, it is NOT functional

Status

:white_check_mark: recovery (mka recoveryimage): functional
:negative_squared_cross_mark: boot (mka bootimage): not functional
:negative_squared_cross_mark: system (mka systemimage): bootloop, no ADB
:grey_question: userdata: not tested
:white_check_mark: flashable zip (mka bacon or brunch fp2): flash system.img and boot.img correctly. Can be flashed on top of both fp2 and FP2 (ro.product.device)

Before flashing your own build of CyanogenMod, be sure to backup all your important data as your phone will be wiped.

Note:
After every commit to the GitHub repo a new build is automatically started in jenkins. The resulting files will appear here. If you wan’t develop/test in your own safe environment, follow the instructions below.

A) Setting up a build environment

Installing required packages (Ubuntu 16.04 64 bits)

sudo apt-get install bison build-essential ccache curl \
flex gcc-multilib git gnupg gperf lib32ncurses5-dev \
lib32readline-gplv2-dev lib32readline6-dev lib32z-dev \
lib32z1-dev libesd0-dev liblz4-tool libncurses5-dev \
libsdl1.2-dev libwxgtk3.0-dev libxml2 libxml2-utils \
lzop maven pngcrush schedtool squashfs-tools \
xsltproc zip zlib1g-dev

Installing the JDK

CyanogenMod 12.1 requires Java 7.
Since OpenJDK 1.7 was removed from the official Ubuntu repositories, you have to use Matthias Klose’s PPA (recognised developer of Ubuntu and Debian collaborator):

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-7-jdk

If you have other Java version, update the default Java version to 7 by running:

sudo update-alternatives --config java
sudo update-alternatives --config javac

Installing Android tools

fastboot and adb will be useful tools for flashing and debugging CM, respectively.

sudo apt-get install android-sdk

Alternatively, you can get the latest version by installing the Android SDK (Get just the command line tools section) and adding them to your PATH.

B) Configure the build environment

Configuring USB Access

I am not sure it is needed in Ubuntu 16.04
As the root user, run (don’t forget to replace by your user name):

wget -S -O - http://source.android.com/source/51-android.rules | sed "s/<username>/$USER/" | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules; sudo udevadm control --reload-rules

Alternatively, you can install android-tools-adb and android-tools-fastboot from Ubuntu software repositories (may conflict with your up-to-date Android’s SDK binaries, if installed):

sudo apt install android-tools-adb android-tools-fastboot

Configure Git

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

Create main directories

mkdir -p ~/bin
mkdir -p ~/android/system

Install the repo command

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

Put the ~/bin directory in your path of execution

Check in your ~/.profile that you have (add it if it is missing):

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Alternatively, you can install repo package from Ubuntu software repositories (may not be up-to-date):

sudo apt install repo

C) Getting source files

Initialize the CyanogenMod source repository

cd ~/android/system/
repo init -u https://github.com/LineageOS/android.git -b cm-12.1

Fetch Fairphone 2 specific files from @Roboe’s Github

You need to create a local_manifests folder inside .repo

mkdir ~/android/system/.repo/local_manifests

Inside, create a file named fp2.xml containing:

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <project name="WeAreFairphone/android_device_fairphone_fp2" path="device/fairphone/fp2" remote="github" revision="cm-12.1" />
  <project name="WeAreFairphone/android_kernel_fairphone_fp2" path="kernel/fairphone/fp2" remote="github" revision="cm-12.1" />

  <!-- Dependencies -->
  <project name="LineageOS/android_device_qcom_common" path="device/qcom/common" remote="github" revision="cm-12.1" />
  <project name="LineageOS/android_hardware_qcom_fm" path="hardware/qcom/fm" remote="github" revision="cm-12.1" />
</manifest>

Download the source code

Prepare to wait a long time while the source code downloads, it is about 30GB.
Set -j to the number of core of your computer (or threads for multi-threaded cpu)

repo sync -j 16

D) Optional optimization

Turn on caching to speed up build

You can speed up subsequent builds by adding to your ~/.bashrc file:

export USE_CCACHE=1

Then, specify the amount of disk space (25GB to 50GB) to dedicate to ccache by typing this from the top of your Android tree:

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

This need only to be run once. This space will be permanently occupied on your drive, so take this into consideration.

E) Building CyanogenMod

Load the environment and build the recovery

source build/envsetup.sh
lunch cm_fp2-userdebug
mka recoveryimage

Extract proprietary files (blobs)

Connect your phone, with USB debugging activated (Settings → Developer options. If you don’t have it, go to Settings → About phone and clic several times on Build number).
Then:

cd device/fairphone/fp2
./extract-files.sh
cd ../../..
Alternative getting the bin blobs using the OTA rom

In the source root dir, download the rom, extract it and point the script to the location.

$ wget http://storage.googleapis.com/fairphone-updates/fp2-sibon-16.07.1-ota-userdebug.zip
$ unzip fp2-sibon-16.07.1-ota-userdebug.zip -d  FP2-OTA
$ cd device/fairphone/fp2 
$ ./extract-files.sh ../../../FP2-OTA

Start the complete build

Here we are… This is going to take time!

croot
mka

Optional: run mka bacon or brunch fp2 to generate a flashable zip in addition to images.

At the end of the process, you should have several files in out/target/product/fp2, including:

boot.img
recovery.img
system.img
userdata.img

F) Other informations

Cleaning a build

If you got some errors and you want to start the build from scratch, run:

make clobber

Building on macOS

I tried to do it on Mac OS X El Capitan, it is not working at all.
You need to downgrade Xcode prior to 4.4, downgrade Java and make and still after this, it is not building correctly. But if someone is willing to try, here is what I did:

Creating a case-sensitive disk image

You need to create a case-sensitive filesystem within your existing Mac OS environment using a disk image.

hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 100g ~/android.dmg
Alias for easy mounting

To easily mount this image, you can create aliases in your ~/.bashrc

# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }
# unmount the android file image
function umountAndroid() { hdiutil detach /Volumes/android; }
Reverting from make 3.82 using MacPorts

Edit /opt/local/etc/macports/sources.conf and above the rsync line (at the end) add:

file:///Users/Shared/dports

Create this directory:

mkdir /Users/Shared/dports

In this directory, run:

svn co --revision 50980 http://svn.macports.org/repository/macports/trunk/dports/devel/gmake/ devel/gmake/

Create a port index for your new local repository:

portindex /Users/Shared/dports

Finally, install the old version of gmake:

sudo port install gmake @3.81
Setting a file descriptor limit

On Mac OS, the default limit on the number of simultaneous file descriptors open is too low and a highly parallel build process may exceed this limit. To increase it, add in your ~/.bashrc:

# set the number of open files to be 1024
ulimit -S -n 1024
Installing the SDK

As Xcode doesn’t contains 10.6 SDK anymore, you need to downgrade it to cpoy required files.

Download Xcode 4.1 (you need an Apple Developer account)
Open it but don’t install it. Then:

cp /Volumes/Xcode.../InstallXcodeLion.pkg ~/Desktop
cd ~/Desktop
mkdir tmp
cd tmp
# Extract needed informations
xar -xf ../InstallXcodeLion.pkg
cd InstallXcodeLion.pkg/
# Extract data
cat Payload | gunzip -dc |cpio -i
cp Install\ Xcode.app/Contents/Resources/Packages/MacOSX10.6.pkg ~/Desktop/
cd ~/Desktop
mkdir tmppp
cd tmppp
xar -xf ../MacOSX10.6.pkg
# Extract again
cat Payload | gunzip -dc |cpio -i
sudo mkdir /Developer
# Copy it where it was in previous versions of OS X
sudo cp -R SDKs /Developer/
Installing the JDK

Download Java 7 (scroll a bit to find links)
Then, put it as the default Java used:

export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
export PATH=$JAVA_HOME/bin:$PATH
java -version

Usefull links:

This wiki was started by @anon40444829 here.

28 Likes