1#!/bin/bash
2
3# Script to compile BOINC for Android.
4# Requires the BOINC source directory and the Android NDK.
5#
6# Date: December 12th, 2011
7# Author: Peter Hanappe (Sony Computer Science Laboratory)
8
9BOINCDIR=`pwd`
10NDK_SYSROOT=""
11NDK_PATH=""
12NDK_ARCH="arm"
13NDK_ROOT=""
14NDK_ABI="armeabi"
15NDK_VERSION="android-8"
16COMPILE_BOINC="yes"
17COMPILE_ZLIB="yes"
18COMPILE_OPENSSL="yes"
19COMPILE_CURL="yes"
20CONFIGURE="yes"
21MAKE_CLEAN="no"
22
23function print_usage()
24{
25    echo "Options:"
26    echo "  --help"
27    echo "  --boinc-dir <dir>       Root directory of BOINC source tree (default: current dir)."
28    echo "  --android-root <dir>    The root directory of the Android NDK."
29    echo "  --android-sysroot <dir> The target sysroot directory of the Android NDK."
30    echo "  --android-bin <dir>     The bin directory of the Android NDK compilers."
31    echo "  --android-arch <dir>    Compile for 'arm' or 'x86' architecture (default: arm)."
32    echo "  --android-version <val> Specify Android version (default: android-8)."
33    echo "  --skip-boinc            Don't compile BOINC."
34    echo "  --skip-zlib             Don't download and compile zlib."
35    echo "  --skip-openssl          Don't download and compile OpenSSL."
36    echo "  --skip-curl             Don't download and compile curl."
37    echo "  --skip-configure        Don't run configure script before compiling."
38    echo "  --make-clean            Run 'make clean' before compiling."
39    echo "Example: ./AndroidBuild.sh \\"
40    echo "           --android-root /opt/google/android-ndk-r6 \\"
41    echo "           --android-bin /opt/google/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin"
42    exit 0
43}
44
45while (($#)); do
46      option=$1
47      shift
48      case "$option" in
49          --help)
50              print_usage
51              shift
52              ;;
53          --boinc-dir)
54              BOINCDIR=$1
55              shift
56              ;;
57          --android-sysroot)
58              NDK_SYSROOT=$1
59              shift
60              ;;
61          --android-root)
62              NDK_ROOT=$1
63              shift
64              ;;
65          --android-bin)
66              NDK_PATH=$1
67              shift
68              ;;
69          --android-arch)
70              NDK_ARCH=$1
71              shift
72              ;;
73          --android-version)
74              NDK_VERSION=$1
75              shift
76              ;;
77          --skip-boinc)
78              COMPILE_BOINC="no"
79              ;;
80          --skip-zlib)
81              COMPILE_ZLIB="no"
82              ;;
83          --skip-openssl)
84              COMPILE_OPENSSL="no"
85              ;;
86          --skip-curl)
87              COMPILE_CURL="no"
88              ;;
89          --skip-configure)
90              CONFIGURE="no"
91              ;;
92          --make-clean)
93              MAKE_CLEAN="yes"
94              ;;
95          *)
96              echo "Unknown option ${option}"
97              print_usage
98              ;;
99      esac
100done
101
102###################################################
103# Test options and paths
104
105if [ ! -e "${BOINCDIR}/client/gui_rpc_server.h" ]; then
106   echo "Please launch this script in the boinc root directory, or use the --boinc-dir <dir> option."
107   print_usage
108fi
109
110if [ "x${NDK_ROOT}" == "x" ]; then
111   echo "Please specify the Android NDK root directory."
112   print_usage
113fi
114
115if [ ${NDK_ARCH} == "arm" ]; then
116    NDK_HOST=arm-linux-androideabi
117elif [ ${NDK_ARCH} == "x86" ]; then
118    NDK_HOST=i686-android-linux
119else
120    echo "Please specify the Android architecture."
121    print_usage
122fi
123
124NDK_CC=${NDK_HOST}-gcc
125
126if [ "x${NDK_SYSROOT}" == "x" ]; then
127    NDK_SYSROOT=${NDK_ROOT}/platforms/${NDK_VERSION}/arch-${NDK_ARCH}
128    if [ ! -d ${NDK_SYSROOT} ]; then
129        echo "Please specify the Android NDK sysroot."
130        echo "Tried, but failed: ${NDK_SYSROOT}"
131        print_usage
132    fi
133fi
134
135if [ "x${NDK_PATH}" == "x" ]; then
136   echo "Please specify the Android binary directory."
137   print_usage
138fi
139
140if [ ! -e "${NDK_SYSROOT}/usr/include/stdio.h" ]; then
141    echo "Could not find ${NDK_SYSROOT}/usr/include/stdio.h header. Please verify the sysroot."
142    print_usage
143fi
144
145if [ ! -e "${NDK_PATH}/${NDK_CC}" ]; then
146    echo "Could not find the ${NDK_CC} compiler. Please verify the binary path."
147    print_usage
148fi
149
150###################################################
151
152echo "================================================"
153echo "BOINCDIR        ${BOINCDIR}"
154echo "NDK_ROOT        ${NDK_ROOT}"
155echo "NDK_SYSROOT     ${NDK_SYSROOT}"
156echo "NDK_PATH        ${NDK_PATH}"
157echo "NDK_ARCH        ${NDK_ARCH}"
158echo "NDK_HOST        ${NDK_HOST}"
159echo "NDK_ABI         ${NDK_ABI}"
160echo "NDK_VERSION     ${NDK_VERSION}"
161echo "NDK_CC          ${NDK_CC}"
162echo "COMPILE_ZLIB    $COMPILE_ZLIB"
163echo "COMPILE_OPENSSL $COMPILE_OPENSSL"
164echo "COMPILE_CURL    $COMPILE_CURL"
165echo "CONFIGURE       $CONFIGURE"
166echo "MAKE_CLEAN      $MAKE_CLEAN"
167echo "================================================"
168
169###################################################
170
171function compile_zlib()
172{
173    echo "================================================"
174    echo "= Starting download and compilation of zlib    ="
175    echo "================================================"
176
177    cd ${BOINCDIR}/android
178
179    if [ ! -e zlib-1.2.5 ]; then
180        wget http://zlib.net/zlib-1.2.5.tar.gz
181        tar xzvf zlib-1.2.5.tar.gz
182    fi
183
184    cd zlib-1.2.5
185
186    if [ $CONFIGURE == "yes" ]; then
187        CC="${NDK_PATH}/${NDK_CC} --sysroot=${NDK_SYSROOT}" \
188            ./configure --prefix=${BOINCDIR}/android/${NDK_ARCH} --static
189    fi
190    if [ $MAKE_CLEAN == "yes" ]; then
191        make clean
192    fi
193    make
194# The 'make install' script fails
195    mkdir -p ${BOINCDIR}/android/${NDK_ARCH}/lib
196    mkdir -p ${BOINCDIR}/android/${NDK_ARCH}/include
197    cp libz.a ${BOINCDIR}/android/${NDK_ARCH}/lib
198    cp zlib.h ${BOINCDIR}/android/${NDK_ARCH}/lib
199    cp zconf.h ${BOINCDIR}/android/${NDK_ARCH}/lib
200}
201
202###################################################
203
204function compile_openssl()
205{
206    echo "================================================"
207    echo "= Starting download and compilation of openssl ="
208    echo "================================================"
209
210    cd ${BOINCDIR}/android
211
212    if [ ! -e openssl-1.0.0e ]; then
213        wget http://www.openssl.org/source/openssl-1.0.0e.tar.gz
214        tar xzvf openssl-1.0.0e.tar.gz
215    fi
216
217    cd openssl-1.0.0e
218
219    if [ $CONFIGURE == "yes" ]; then
220        CC="${NDK_PATH}/${NDK_CC} --sysroot=${NDK_SYSROOT}" \
221            ./config --prefix=${BOINCDIR}/android/${NDK_ARCH} no-shared no-asm
222    fi
223
224    if [ $MAKE_CLEAN == "yes" ]; then
225        make clean
226    fi
227
228    make
229    make install
230}
231
232###################################################
233
234function compile_curl()
235{
236    echo "================================================"
237    echo "= Starting download and compilation of curl ="
238    echo "================================================"
239
240    cd ${BOINCDIR}/android
241
242    if [ ! -e curl-7.23.1 ]; then
243        wget http://curl.haxx.se/download/curl-7.23.1.tar.gz
244        tar xzvf curl-7.23.1.tar.gz
245    fi
246
247    cd curl-7.23.1
248
249# Export binary path.
250    export PATH=${PATH}:${NDK_PATH}
251
252#
253# ./configure --with-sysroot=${NDK_SYSROOT} doesn't seem to work. Pass
254# the correct path using the CFLAGS environment variable.
255#
256# The ANDROID flag is used in curl-7.23.1/include/curl/curl.h
257#
258    if [ $CONFIGURE == "yes" ]; then
259        CFLAGS="--sysroot=${NDK_SYSROOT} -DANDROID" \
260            ./configure --host=${NDK_HOST} \
261            --with-sysroot=${NDK_SYSROOT} \
262            --prefix=${BOINCDIR}/android/${NDK_ARCH} --enable-debug \
263            --disable-shared --enable-static \
264            --enable-http --disable-ftp --disable-ldap --disable-ldaps \
265            --disable-rtsp --disable-proxy --disable-dict --disable-telnet \
266            --disable-tftp --disable-pop3 --disable-imap --disable-smtp \
267            --disable-gopher --disable-ipv6 --disable-sspi --disable-crypto-auth \
268            --disable-tls-srp --disable-ntlm-wb --disable-cookies --disable-manual \
269            --disable-threaded-resolver --without-ca-bundle
270    fi
271
272    if [ $MAKE_CLEAN == "yes" ]; then
273        make clean
274    fi
275
276    make
277    make install
278}
279
280###################################################
281# Problems:
282# - problems in configure.ac with curl: --with-curl doesn't use the correct path
283# - config.sub too old to recognize --host=arm-linux-androideabi, used newer config.sub (2011-03-23)
284# - changed lib/network.cpp:298: !defined(__ANDROID__)
285# - changed lib/synch.cpp: #if defined(__ANDROID__) \ #define create_semaphore(_key) (-1), ...
286# - changed lib/mac_address.cpp:189: #elif (defined(SIOCGIFCONF) || defined(SIOCGLIFCONF)) && !defined(__ANDROID__)
287function compile_boinc()
288{
289    cd ${BOINCDIR}
290
291# Export binary path.
292    export PATH=${PATH}:${NDK_PATH}
293
294    if [ $CONFIGURE == "yes" ]; then
295
296        bash _autosetup
297
298        OPTIONS="--host=${NDK_HOST} \
299            --disable-server \
300            --enable-client \
301            --disable-manager \
302            --enable-libraries \
303            --enable-debug \
304            --with-ssl=${BOINCDIR}/android/arm/lib"
305
306        echo "================================================"
307        echo "Configuring BOINC with: $OPTIONS"
308        echo "================================================"
309
310        FLAGS="--sysroot=${NDK_SYSROOT} \
311            -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/include \
312            -I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/libs/${NDK_ABI}/include"
313
314        CFLAGS="${FLAGS}" \
315        CXXFLAGS="${FLAGS}" \
316        LDFLAGS="-L${BOINCDIR}/android/arm/lib -L${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/libs/${NDK_ABI}" \
317        LIBS="-lstdc++" \
318            ./configure ${OPTIONS}
319    fi
320
321    if [ $MAKE_CLEAN == "yes" ]; then
322        make clean
323    fi
324
325    make
326}
327
328###################################################
329
330if [ ! -e "${BOINCDIR}/android" ]; then
331    echo "================================================"
332    echo "= Creating the android direcory                ="
333    echo "================================================"
334    mkdir ${BOINCDIR}/android
335fi
336if [ $COMPILE_ZLIB == "yes" ]; then
337    compile_zlib
338fi
339if [ $COMPILE_OPENSSL == "yes" ]; then
340    compile_openssl
341fi
342if [ $COMPILE_CURL == "yes" ]; then
343    compile_curl
344fi
345if [ $COMPILE_BOINC == "yes" ]; then
346    compile_boinc
347fi
348
349