blob: 2e936aaed73e462da0f4038b2366c5cda1515ed5 [file] [log] [blame]
Alexandre Lision43b9aeb2014-07-15 14:21:19 -04001#! /bin/sh
2
3# Read the Android Wiki http://wiki.videolan.org/AndroidCompile
4# Setup all that stuff correctly.
Tristan Matthews8d02ef62014-07-22 16:03:17 -04005# Get the latest Android SDK Platform or modify numbers in configure.sh and sflphone-android/default.properties.
Alexandre Lision43b9aeb2014-07-15 14:21:19 -04006
7set -e
8
9BUILD=
10FETCH=
11case "$1" in
12 --fetch)
13 FETCH=1
14 shift
15 ;;
16 --build)
17 BUILD=1
18 shift
19 ;;
20 *)
21 FETCH=1
22 BUILD=1
23 ;;
24esac
25
26if [ -z "$ANDROID_NDK" -o -z "$ANDROID_SDK" ]; then
27 echo "You must define ANDROID_NDK, ANDROID_SDK and ANDROID_ABI before starting."
28 echo "They must point to your NDK and SDK directories.\n"
29 exit 1
30fi
31
32if [ -z "$ANDROID_ABI" ]; then
33 echo "Please set ANDROID_ABI to your architecture: armeabi-v7a, armeabi, x86 or mips."
34 exit 1
35fi
36
37# try to detect NDK version
38REL=$(grep -o '^r[0-9]*.*' $ANDROID_NDK/RELEASE.TXT 2>/dev/null|cut -b2-)
39case "$REL" in
Tristan Matthewsd6033b42014-10-09 17:00:07 -040040 10*)
41 if [ "${HAVE_64}" = 1 ];then
42 GCCVER=4.9
43 ANDROID_API=android-L
44 else
45 GCCVER=4.8
46 ANDROID_API=android-9
47 fi
48 CXXSTL="/"${GCCVER}
49 ;;
50 9*)
51 if [ "${HAVE_64}" = 1 ];then
52 echo "You need the NDKv10 or later for 64 bits build"
53 exit 1
54 fi
Alexandre Lision43b9aeb2014-07-15 14:21:19 -040055 GCCVER=4.8
Tristan Matthewsd6033b42014-10-09 17:00:07 -040056 ANDROID_API=android-9
Alexandre Lision43b9aeb2014-07-15 14:21:19 -040057 CXXSTL="/"${GCCVER}
58 ;;
59 7|8|*)
60 echo "You need the NDKv9 or later"
61 exit 1
62 ;;
63esac
64
65export GCCVER
66export CXXSTL
Tristan Matthewsd6033b42014-10-09 17:00:07 -040067export ANDROID_API
Alexandre Lision43b9aeb2014-07-15 14:21:19 -040068
69# Set up ABI variables
70if [ ${ANDROID_ABI} = "x86" ] ; then
71 TARGET_TUPLE="i686-linux-android"
72 PATH_HOST="x86"
73 HAVE_X86=1
74 PLATFORM_SHORT_ARCH="x86"
75elif [ ${ANDROID_ABI} = "mips" ] ; then
76 TARGET_TUPLE="mipsel-linux-android"
77 PATH_HOST=$TARGET_TUPLE
78 HAVE_MIPS=1
79 PLATFORM_SHORT_ARCH="mips"
80else
81 TARGET_TUPLE="arm-linux-androideabi"
82 PATH_HOST=$TARGET_TUPLE
83 HAVE_ARM=1
84 PLATFORM_SHORT_ARCH="arm"
85fi
86
87# XXX : important!
88[ "$HAVE_ARM" = 1 ] && cat << EOF
89For an ARMv6 device without FPU:
90$ export NO_FPU=1
91For an ARMv5 device:
92$ export NO_ARMV6=1
93
94If you plan to use a release build, run 'compile.sh release'
95EOF
96
97export TARGET_TUPLE
98export PATH_HOST
99export HAVE_ARM
100export HAVE_X86
101export HAVE_MIPS
102export PLATFORM_SHORT_ARCH
103
104# Add the NDK toolchain to the PATH, needed both for contribs and for building
105# stub libraries
106NDK_TOOLCHAIN_PATH=`echo ${ANDROID_NDK}/toolchains/${PATH_HOST}-${GCCVER}/prebuilt/\`uname|tr A-Z a-z\`-*/bin`
107export PATH=${NDK_TOOLCHAIN_PATH}:${PATH}
108
109ANDROID_PATH="`pwd`"
110
Tristan Matthewsa2665ff2014-08-12 18:27:23 -0400111# Fetch sflphone daemon source
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400112if [ ! -z "$FETCH" ]
113then
Tristan Matthews8d02ef62014-07-22 16:03:17 -0400114 # 1/ libsflphone
Tristan Matthewsfe249522014-10-09 19:22:41 -0400115 TESTED_HASH=3426b44e576e4766bd0bd274346d2bfbda562e07
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400116 if [ ! -d "sflphone" ]; then
Tristan Matthewsa2665ff2014-08-12 18:27:23 -0400117 echo "sflphone daemon source not found, cloning"
118 git clone https://gerrit-sflphone.savoirfairelinux.com/sflphone
119 cd sflphone
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400120 echo android/ >> .git/info/exclude
121 echo contrib/android/ >> .git/info/exclude
Alexandre Lision40734322014-09-03 14:26:59 -0400122 git checkout master
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400123 else
Tristan Matthewsa2665ff2014-08-12 18:27:23 -0400124 echo "sflphone daemon source found"
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400125 cd sflphone
Alexandre Lision65be7702014-09-29 18:06:33 -0400126 git fetch
127 git checkout ${TESTED_HASH}
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400128# if ! git cat-file -e ${TESTED_HASH}; then
129# cat << EOF
130#***
131#*** Error: Your sflphone checkout does not contain the latest tested commit ***
132#***
133#
134#Please update your source with something like:
135#
136#cd sflphone
137#git reset --hard origin
138#git pull origin master
139#git checkout -B android ${TESTED_HASH}
140#
141#*** : This will delete any changes you made to the current branch ***
142#
143#EOF
144# exit 1
145# fi
146 fi
147else
148 cd sflphone
149fi
150
151if [ -z "$BUILD" ]
152then
153 echo "Not building anything, please run $0 --build"
154 exit 0
155fi
156
157# Setup CFLAGS
158if [ ${ANDROID_ABI} = "armeabi-v7a" ] ; then
159 EXTRA_CFLAGS="-mfpu=vfpv3-d16 -mcpu=cortex-a8"
160 EXTRA_CFLAGS="${EXTRA_CFLAGS} -mthumb -mfloat-abi=softfp"
161elif [ ${ANDROID_ABI} = "armeabi" ] ; then
162 if [ -n "${NO_ARMV6}" ]; then
163 EXTRA_CFLAGS="-march=armv5te -mtune=arm9tdmi -msoft-float"
164 else
165 if [ -n "${NO_FPU}" ]; then
166 EXTRA_CFLAGS="-march=armv6j -mtune=arm1136j-s -msoft-float"
167 else
168 EXTRA_CFLAGS="-mfpu=vfp -mcpu=arm1136jf-s -mfloat-abi=softfp"
169 fi
170 fi
171elif [ ${ANDROID_ABI} = "x86" ] ; then
172 EXTRA_CFLAGS="-march=pentium"
173elif [ ${ANDROID_ABI} = "mips" ] ; then
174 EXTRA_CFLAGS="-march=mips32 -mtune=mips32r2 -mhard-float"
175 # All MIPS Linux kernels since 2.4.4 will trap any unimplemented FPU
176 # instruction and emulate it, so we select -mhard-float.
177 # See http://www.linux-mips.org/wiki/Floating_point#The_Linux_kernel_and_floating_point
178else
179 echo "Unknown ABI. Die, die, die!"
180 exit 2
181fi
182
183EXTRA_CFLAGS="${EXTRA_CFLAGS} -O2"
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400184EXTRA_CFLAGS="${EXTRA_CFLAGS} -I${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++${CXXSTL}/include"
185EXTRA_CFLAGS="${EXTRA_CFLAGS} -I${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++${CXXSTL}/libs/${ANDROID_ABI}/include"
186
Tristan Matthews676fcee2014-10-03 15:57:09 -0400187# Setup LDFLAGS
188EXTRA_LDFLAGS="-l${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++${CXXSTL}/libs/${ANDROID_ABI}/libgnustl_static.a"
189
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400190# Make in //
191UNAMES=$(uname -s)
192MAKEFLAGS=
193if which nproc >/dev/null
194then
195MAKEFLAGS=-j`nproc`
196elif [ "$UNAMES" == "Darwin" ] && which sysctl >/dev/null
197then
198MAKEFLAGS=-j`sysctl -n machdep.cpu.thread_count`
199fi
200
Tristan Matthewsd6d5d402014-10-09 16:41:06 -0400201# Build buildsystem tools
202export PATH=`pwd`/daemon/extras/tools/build/bin:$PATH
203echo "Building tools"
204cd daemon/extras/tools
205./bootstrap
206make $MAKEFLAGS
207cd ../../..
208
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400209############
210# Contribs #
211############
212echo "Building the contribs"
Alexandre Lision40734322014-09-03 14:26:59 -0400213mkdir -p daemon/contrib/contrib-android-${TARGET_TUPLE}
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400214
215gen_pc_file() {
216 echo "Generating $1 pkg-config file"
217 echo "Name: $1
218Description: $1
219Version: $2
220Libs: -l$1
Alexandre Lision40734322014-09-03 14:26:59 -0400221Cflags:" > daemon/contrib/${TARGET_TUPLE}/lib/pkgconfig/`echo $1|tr 'A-Z' 'a-z'`.pc
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400222}
223
Alexandre Lision40734322014-09-03 14:26:59 -0400224mkdir -p daemon/contrib/${TARGET_TUPLE}/lib/pkgconfig
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400225
Alexandre Lision40734322014-09-03 14:26:59 -0400226cd daemon/contrib/contrib-android-${TARGET_TUPLE}
Alexandre Lisionec1f3ee2014-08-04 19:12:42 -0400227../bootstrap --host=${TARGET_TUPLE}
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400228
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400229# Some libraries have arm assembly which won't build in thumb mode
230# We append -marm to the CFLAGS of these libs to disable thumb mode
231[ ${ANDROID_ABI} = "armeabi-v7a" ] && echo "NOTHUMB := -marm" >> config.mak
232
233# Release or not?
234if [ $# -ne 0 ] && [ "$1" = "release" ]; then
235 OPTS=""
236 EXTRA_CFLAGS="${EXTRA_CFLAGS} -DNDEBUG "
237 RELEASE=1
238else
239 OPTS="--enable-debug"
Tristan Matthews8d02ef62014-07-22 16:03:17 -0400240 EXTRA_CFLAGS="${EXTRA_CFLAGS} -DNDEBUG "
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400241 RELEASE=0
242fi
243
244echo "EXTRA_CFLAGS= -g ${EXTRA_CFLAGS}" >> config.mak
Tristan Matthewsfc848072014-10-09 03:43:54 -0400245echo "EXTRA_CXXFLAGS= -g ${EXTRA_CXXFLAGS}" >> config.mak
Tristan Matthews676fcee2014-10-03 15:57:09 -0400246echo "EXTRA_LDFLAGS= ${EXTRA_LDFLAGS}" >> config.mak
Tristan Matthews8d02ef62014-07-22 16:03:17 -0400247export SFLPHONE_EXTRA_CFLAGS="${EXTRA_CFLAGS}"
Tristan Matthewsfc848072014-10-09 03:43:54 -0400248export SFLPHONE_EXTRA_CXXFLAGS="${EXTRA_CXXFLAGS}"
Tristan Matthews676fcee2014-10-03 15:57:09 -0400249export SFLPHONE_EXTRA_LDFLAGS="${EXTRA_LDFLAGS}"
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400250
Alexandre Lision68d8f2e2014-07-30 17:42:40 -0400251make install
Alexandre Lisionec1f3ee2014-08-04 19:12:42 -0400252echo ${PWD}
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400253# We already have zlib available
254[ -e .zlib ] || (mkdir -p zlib; touch .zlib)
255which autopoint >/dev/null || make $MAKEFLAGS .gettext
256export PATH="$PATH:$PWD/../$TARGET_TUPLE/bin"
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400257
Tristan Matthewseaaa5ba2014-10-09 04:23:06 -0400258export SFLPHONE_BUILD_DIR=sflphone/daemon/build-android-${TARGET_TUPLE}
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400259############
Tristan Matthews8d02ef62014-07-22 16:03:17 -0400260# Make SFLPHONE #
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400261############
262cd ../.. && mkdir -p build-android-${TARGET_TUPLE} && cd build-android-${TARGET_TUPLE}
263
264if [ $# -eq 1 ] && [ "$1" = "jni" ]; then
265 CLEAN="jniclean"
Tristan Matthews33873ca2014-10-09 13:35:39 -0400266 TARGET="sflphone-android/obj/local/${ANDROID_ABI}/libsflphone.so"
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400267else
268 CLEAN="distclean"
269 if [ ! -f config.h ]; then
270 echo "Bootstraping"
Tristan Matthewsfc848072014-10-09 03:43:54 -0400271 cd ../
272 ./autogen.sh
273 cd ../../
Alexandre Lision60057782014-10-03 14:06:01 -0400274 cd sflphone-android
275 ./make-swig.sh
Tristan Matthewsfc848072014-10-09 03:43:54 -0400276 cd ../sflphone/daemon/build-android-${TARGET_TUPLE}
277 echo "Configuring"
278 echo `pwd`
279 ${ANDROID_PATH}/configure.sh ${OPTS}
Tristan Matthewscc806e12014-08-01 16:25:03 -0400280 echo "Building"
281 make $MAKEFLAGS
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400282 fi
283 TARGET=
284fi
285
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400286####################################
287# Ring android UI and specific code
288####################################
289echo "Building Ring for Android"
Alexandre Lision40734322014-09-03 14:26:59 -0400290cd ../../../
Tristan Matthewsfc848072014-10-09 03:43:54 -0400291
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400292make $CLEAN
293make -j1 TARGET_TUPLE=$TARGET_TUPLE PLATFORM_SHORT_ARCH=$PLATFORM_SHORT_ARCH CXXSTL=$CXXSTL RELEASE=$RELEASE $TARGET
294
295#
296# Exporting a environment script with all the necessary variables
297#
298echo "Generating environment script."
299cat <<EOF
300This is a script that will export many of the variables used in this
301script. It will allow you to compile parts of the build without having
302to rebuild the entire build (e.g. recompile only the Java part).
303
304To use it, include the script into your shell, like this:
305 source env.sh
306
307Now, you can use this command to build the Java portion:
308 make -e
309
310The file will be automatically regenerated by compile.sh, so if you change
311your NDK/SDK locations or any build configurations, just re-run this
312script (sh compile.sh) and it will automatically update the file.
313
314EOF
315
316echo "# This file was automatically generated by compile.sh" > env.sh
317echo "# Re-run 'sh compile.sh' to update this file." >> env.sh
318
319# The essentials
320cat <<EssentialsA >> env.sh
321export ANDROID_ABI=$ANDROID_ABI
322export ANDROID_SDK=$ANDROID_SDK
323export ANDROID_NDK=$ANDROID_NDK
324export GCCVER=$GCCVER
325export CXXSTL=$CXXSTL
Tristan Matthewsfc848072014-10-09 03:43:54 -0400326export SFLPHONE_BUILD_DIR=$SFLPHONE_BUILD_DIR
Alexandre Lision43b9aeb2014-07-15 14:21:19 -0400327export TARGET_TUPLE=$TARGET_TUPLE
328export PATH_HOST=$PATH_HOST
329export PLATFORM_SHORT_ARCH=$PLATFORM_SHORT_ARCH
330EssentialsA
331
332# PATH
333echo "export PATH=$NDK_TOOLCHAIN_PATH:\${ANDROID_SDK}/platform-tools:\${PATH}" >> env.sh
334
335# CPU flags
336if [ -n "${HAVE_ARM}" ]; then
337 echo "export HAVE_ARM=1" >> env.sh
338elif [ -n "${HAVE_X86}" ]; then
339 echo "export HAVE_X86=1" >> env.sh
340elif [ -n "${HAVE_MIPS}" ]; then
341 echo "export HAVE_MIPS=1" >> env.sh
342fi
343
344if [ -n "${NO_ARMV6}" ]; then
345 echo "export NO_ARMV6=1" >> env.sh
346fi
347if [ -n "${NO_FPU}" ]; then
348 echo "export NO_FPU=1" >> env.sh
349fi