Skip to content

Instantly share code, notes, and snippets.

@mjtorn
Last active January 19, 2017 07:02
Show Gist options
  • Save mjtorn/6cf8ed8b33fd91b3ffa05e7981b8f769 to your computer and use it in GitHub Desktop.
Save mjtorn/6cf8ed8b33fd91b3ffa05e7981b8f769 to your computer and use it in GitHub Desktop.
Build sfdroid for the Fairphone2
#!/usr/bin/env bash
# vim: ts=4 sw=4 et ai
# Markus Törnqvist <mjt@nysv.org> 2016
# Licensed under the DWTFYW public license
set -eu
FP2_BLOBS=fp2-sibon-16.11.0-blobs.sh
# I run this with ./build-sfdroid-repos.sh FP2 fairphone_os
# which is why this path is ok for me, YMMV
REPO_SCRIPT=${PWD}/repo
test -x $REPO_SCRIPT || exit 67
clean=0
SFDROID_PATH=/usr/libexec/sfdroid
HW_PATH=$SFDROID_PATH/system/lib/hw
BIN_PATH=$SFDROID_PATH/system/bin
LIB_PATH=$SFDROID_PATH/system/lib
FRAMEWORK_PATH=$SFDROID_PATH/system/framework
APP_PATH=$SFDROID_PATH/system/priv-app
export USE_CCACHE=1
COMPILE_RULES=(
"system/netd/server mma"
"frameworks/native/services/surfaceflinger mma"
"frameworks/base mma"
"frameworks/av/media/mediaserver mma"
"packages/apps/Settings mma"
"external/dbus mma"
# "frameworks/opt/net/wifi/service mma"
"./ make wifi-service"
"frameworks/opt/net/ethernet mm"
"hardware/libhardware/modules/sharebuffer mm"
"hardware/libhardware/modules/sfdroid_sensors mm"
)
function parse_args () {
if [ $# -lt 2 ] || [ $# -ge 4 ]; then
echo "Usage: ${0} DEVICE SOURCE_ROOT [--clean]"
exit 1
elif [ $# == 2 ]; then
DEVICE=$1
target=$2
elif [ $# == 3 ]; then
if [ $3 != "--clean" ]; then
echo "The only supported argument is --clean"
exit 1
else
DEVICE=$1
target=$2
clean=1
fi
fi
}
function do_chdir () {
# Has to be its own function because `setup` may be called twice
test -d $target || exit 67
cd $target
}
function repo_sync () {
$REPO_SCRIPT sync
}
function setup () {
INSTALLDIR=${target}/${DEVICE}_sfdroid/
test -d $INSTALLDIR && rm -rfv $INSTALLDIR
set +u # envsetup would start to fail here
if [ $DEVICE == "FP2" ]; then
test -f build/envsetup.sh || exit 67
source build/envsetup.sh
echo "=== Sourced build/envsetup.sh"
test -f $FP2_BLOBS || exit 67
choosecombo 1 $DEVICE 2
else
lunch cm_$DEVICE-eng
fi
}
function setup_data () {
# Set up data for the device, and GATHER_FILES
# because INSTALLDIR would be undefined in it earlier on
REPOS_DIRS=(
"https://github.com/sfdroid/android_hardware_libhardware hardware/libhardware"
"https://github.com/sfdroid/android_frameworks_native frameworks/native"
"https://github.com/sfdroid/android_frameworks_base frameworks/base"
"https://github.com/sfdroid/android_frameworks_av frameworks/av"
"https://github.com/sfdroid/android_packages_apps_Settings packages/apps/Settings"
"https://github.com/sfdroid/android_system_netd system/netd"
"https://github.com/sfdroid/android_frameworks_opt_net_wifi frameworks/opt/net/wifi"
"https://github.com/sfdroid/android_frameworks_opt_net_ethernet frameworks/opt/net/ethernet"
)
# These are actual clones, not alterations to the android sources
EXTERNAL_REPOS_DIRS=(
"https://github.com/sfdroid/sfdroid-init sfdroid-init"
"https://github.com/sfdroid/android_external_dbus dbus"
)
if [ $DEVICE == "FP2" ]; then
branch="fp2-sibon"
else
if [ $DEVICE == "onyx" ]; then
branch="oneplus-onyx"
elif [ $DEVICE == "mako" ]; then
branch="mako_12"
else
echo "Unknown device ${DEVICE}"
exit 1
fi
fi
GATHER_FILES=(
"framework/framework.jar $INSTALLDIR/$FRAMEWORK_PATH"
"framework/ext.jar $INSTALLDIR/$FRAMEWORK_PATH"
"framework/am.jar $INSTALLDIR/$FRAMEWORK_PATH"
"framework/services.jar $INSTALLDIR/$FRAMEWORK_PATH"
"framework/ethernet-service.jar $INSTALLDIR/$FRAMEWORK_PATH"
"framework/wifi-service.jar $INSTALLDIR/$FRAMEWORK_PATH"
"lib/libandroid_runtime.so $INSTALLDIR/$LIB_PATH"
"lib/libandroid_servers.so $INSTALLDIR/$LIB_PATH"
"lib/libdbus.so $INSTALLDIR/$LIB_PATH"
"lib/libhardware.so $INSTALLDIR/$LIB_PATH"
"lib/libsurfaceflinger.so $INSTALLDIR/$LIB_PATH"
"lib/hw/sfdroid_sensors.default.so $INSTALLDIR/$HW_PATH"
"lib/hw/sharebuffer.default.so $INSTALLDIR/$HW_PATH"
"bin/netd $INSTALLDIR/$BIN_PATH"
"bin/mediaserver $INSTALLDIR/$BIN_PATH"
"priv-app/Settings/Settings.apk $INSTALLDIR/$APP_PATH/Settings"
"external/sfdroid-init/init.sfdroid.rc $INSTALLDIR/"
)
}
function maybe_clean () {
if [ $clean == 1 ]; then
echo "=== Cleaning source tree"
_PWD=$PWD
cpu_count=$(grep -c ^processor /proc/cpuinfo)
make clean
if [ $DEVICE == "FP2" ]; then
echo y | sh $FP2_BLOBS
fi
for repo_dir in "${REPOS_DIRS[@]}"; do
dir=$(echo ${repo_dir} | cut -d" " -f2)
cd $dir
git checkout origin/${branch}
cd $_PWD
done
echo $PWD
fi
}
function maybe_make () {
if [ $clean == 1 ]; then
# A new round of setup is required after make clean
setup
echo "=== Compiling: make -j${cpu_count}"
make -j${cpu_count}
fi
}
function get_repos () {
_PWD=$PWD
for repo_dir in "${REPOS_DIRS[@]}"; do
repo=$(echo ${repo_dir} | cut -d" " -f1)
dir=$(echo ${repo_dir} | cut -d" " -f2)
cd $dir
git remote add $DEVICE $repo || echo "Remote exists, no worries"
git fetch $DEVICE
git checkout ${DEVICE}/${branch}
cd $_PWD
done
}
function get_external_repos () {
_PWD=$PWD
test -d external || exit 67
cd external
for repo_dir in "${EXTERNAL_REPOS_DIRS[@]}"; do
repo=$(echo ${repo_dir} | cut -d" " -f1)
dir=$(echo ${repo_dir} | cut -d" " -f2)
test -d $dir || {
git clone $repo $dir
}
cd $dir
git fetch
git checkout origin/${branch}
cd ..
done
cd $_PWD
}
function compile_targets () {
_PWD=$PWD
for compile_rule in "${COMPILE_RULES[@]}"; do
dir=$(echo $compile_rule | cut -d" " -f1)
cmd=$(echo $compile_rule | cut -d" " -f2-)
echo "=== Compiling ${dir} with ${cmd}"
cd $dir
$cmd
cd $_PWD
done
}
function gather_files () {
src_base=out/target/product/$DEVICE/system
echo "=== Gathering files from ${src_base} to ${INSTALLDIR}"
mkdir -p ${INSTALLDIR}
mkdir -p ${INSTALLDIR}/${LIB_PATH}
mkdir -p ${INSTALLDIR}/${HW_PATH}
mkdir -p ${INSTALLDIR}/${BIN_PATH}
mkdir -p ${INSTALLDIR}/${FRAMEWORK_PATH}
mkdir -p $INSTALLDIR/$APP_PATH/Settings
for gather_file in "${GATHER_FILES[@]}"; do
src=$(echo $gather_file | cut -d" " -f1)
dst=$(echo $gather_file | cut -d" " -f2)
test -f ${src_base}/${src} && {
cp -pv ${src_base}/${src} ${dst}
} || {
echo "Not found, falling back"
cp -pv ${src} ${dst}
}
done
}
function create_tarball () {
echo "=== Creating tarball of ${INSTALLDIR}"
tarball=../${DEVICE}_sfdroid.tar.gz
_PWD=$PWD
cd $INSTALLDIR
tar czvf $tarball .
cd $_PWD
echo "Created ${tarball}"
}
function main () {
parse_args "${@}"
do_chdir
repo_sync
setup
setup_data
# maybe_clean
# maybe_make
get_repos
get_external_repos
compile_targets
# These are untested
gather_files
create_tarball
}
main "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment