]> sigrok.org Git - sigrok-util.git/blob - cross-compile/android/sigrok-cross-android
sigrok-cross-android: Bump various lib versions.
[sigrok-util.git] / cross-compile / android / sigrok-cross-android
1 #!/bin/bash
2 ##
3 ## This file is part of the sigrok-util project.
4 ##
5 ## Copyright (C) 2013-2015 Uwe Hermann <uwe@hermann-uwe.de>
6 ## Copyright (C) 2013-2015 Marcus Comstedt <marcus@mc.pp.se>
7 ##
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; either version 2 of the License, or
11 ## (at your option) any later version.
12 ##
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ## GNU General Public License for more details.
17 ##
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21 ##
22
23 set -e
24
25 # The path where the NDK, SDK, and toolchains are located.
26 ANDROID_PATH=$HOME/android
27
28 # The path where the Android NDK is located.
29 ANDROID_NDK=$ANDROID_PATH/android-ndk-r10e
30
31 # The path where the Android SDK is located.
32 ANDROID_SDK=$ANDROID_PATH/android-sdk-linux
33
34 # The path where your Android toolchain directory is located.
35 TOOLCHAIN=$ANDROID_PATH/android-arm-toolchain
36 # TOOLCHAIN=$ANDROID_PATH/android-mipsel-toolchain
37 # TOOLCHAIN=$ANDROID_PATH/android-i686-toolchain
38 # TOOLCHAIN=$ANDROID_NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86
39 # TOOLCHAIN=$ANDROID_NDK/toolchains/x86-4.9/prebuilt/linux-x86
40 # TOOLCHAIN=$ANDROID_NDK/toolchains/mipsel-linux-android-4.9/prebuilt/linux-x86
41
42 # Select which Android toolchain to use.
43 # Don't forget to also adapt TOOLCHAIN above if you change TOOLCHAIN_TRIPLET!
44 TOOLCHAIN_TRIPLET=arm-linux-androideabi
45 # TOOLCHAIN_TRIPLET=mipsel-linux-android
46 # TOOLCHAIN_TRIPLET=i686-linux-android
47
48 # The path where the cross-compiled packages will be installed.
49 PREFIX=$HOME/sr_android
50
51 # The path where to download files to and where to build packages.
52 BUILDDIR=./build
53
54 # Edit this to control verbose build output.
55 # V="V=1 VERBOSE=1"
56
57 # Edit this to enable/disable/modify parallel compiles.
58 PARALLEL="-j 2"
59
60 # You usually don't need to change anything below this line.
61
62 # -----------------------------------------------------------------------------
63
64 VER_LIBICONV=1.14
65 VER_GETTEXT=0.19.5.1
66 VER_ZLIB=1.2.8
67 VER_PCRE=8.37
68 VER_LIBFFI=3.2.1
69 VER_GLIB=2.45.4
70 VER_GLIB_SHORT=2.45
71 VER_GLIBMM=2.45.41
72 VER_GLIBMM_SHORT=2.45
73 VER_LIBSIGCXX=2.5.1
74 VER_LIBSIGCXX_SHORT=2.5
75 VER_LIBZIP=0.11.2
76 VER_LIBFTDI1=1.2
77 VER_LIBUSB=1.0.19
78 VER_PYTHON=3.3.3
79 VER_BOOST=1.58.0
80 VER_QT=5.4.1
81 VER_NDK=10e
82 VER_SDK=24.3.3
83 VER_AND_BT=22.0.1 # Android build tools version
84 VER_AND_API=16    # Target Android API version
85
86 SF_MIRROR=download.sourceforge.net
87
88 WGET="wget -c --quiet"
89 GIT_CLONE="git clone --depth=1"
90
91 case "$TOOLCHAIN_TRIPLET" in
92   arm*) TARGET_ARCH=armeabi;;
93   mips*) TARGET_ARCH=mips;;
94   i686*) TARGET_ARCH=x86;;
95   *) echo >&2 "Unknown prefix for TOOLCHAIN_TRIPLET."; exit 1;;
96 esac
97
98 # -----------------------------------------------------------------------------
99
100 # Get the Android NDK and SDK, install required packages and toolchains.
101 if [ "x$1" = "xprepare" ]; then
102   mkdir -p $ANDROID_PATH
103   cd $ANDROID_PATH
104
105   # NDK
106   $WGET http://dl.google.com/android/ndk/android-ndk-r$VER_NDK-linux-x86.bin
107   chmod 700 android-ndk-r$VER_NDK-linux-x86.bin
108   ./android-ndk-r$VER_NDK-linux-x86.bin -y
109   cd android-ndk-r$VER_NDK
110   ./build/tools/make-standalone-toolchain.sh --platform=android-$VER_AND_API \
111     --toolchain=arm-linux-androideabi-4.9 \
112     --install-dir=$ANDROID_PATH/android-arm-toolchain
113   ./build/tools/make-standalone-toolchain.sh --platform=android-$VER_AND_API \
114     --toolchain=x86-4.9 \
115     --install-dir=$ANDROID_PATH/android-i686-toolchain
116   ./build/tools/make-standalone-toolchain.sh --platform=android-$VER_AND_API \
117     --toolchain=mipsel-linux-android-4.9 \
118     --install-dir=$ANDROID_PATH/android-mipsel-toolchain
119   cd ..
120
121   # SDK
122   $WGET http://dl.google.com/android/android-sdk_r$VER_SDK-linux.tgz
123   tar xfz android-sdk_r$VER_SDK-linux.tgz
124   cd android-sdk-linux
125   echo y | ./tools/android update sdk --no-ui --all --filter platform-tools,build-tools-$VER_AND_BT,android-$VER_AND_API
126   cd ..
127
128   exit
129 fi
130
131 # -----------------------------------------------------------------------------
132
133 # Remove build directory contents (if any) and create a new build dir.
134 rm -rf $BUILDDIR
135 mkdir $BUILDDIR
136 cd $BUILDDIR
137
138 # -----------------------------------------------------------------------------
139
140 # Build host Python before we start messing with the environment.
141 # Don't do parallel builds, this doesn't seem to work well.
142 $WGET http://python.org/ftp/python/$VER_PYTHON/Python-$VER_PYTHON.tar.xz
143 tar xfJ Python-$VER_PYTHON.tar.xz
144 cd Python-$VER_PYTHON
145 ./configure
146 make python $V
147 mv python hostpython
148 cd ..
149
150 # -----------------------------------------------------------------------------
151
152 # We need to find tools in the toolchain.
153 export PATH=$TOOLCHAIN/bin:$PATH
154
155 # Tell pkg-config to only look for our cross-built stuff.
156 export PKG_CONFIG_LIBDIR="$PREFIX/lib/pkgconfig"
157 export -n PKG_CONFIG_PATH
158
159 # Check toolchain
160 if test -d "$TOOLCHAIN"/sysroot; then
161   echo "Toolchain is standalone"
162   STANDALONE_TOOLCHAIN=$TOOLCHAIN
163   SYSROOT=
164 else
165   STANDALONE_TOOLCHAIN=
166   NATIVE_API_LEVEL=$VER_AND_API
167   SYSROOT_DIR=${ANDROID_NDK}/platforms/android-${NATIVE_API_LEVEL}/arch-${TARGET_ARCH%eabi*}
168   if [ ! -d "$SYSROOT_DIR" ]; then
169     echo >&2 "Can't build with NDK toolchain; sysroot $SYSROOT_DIR is missing"
170     exit 1
171   fi
172   SYSROOT="--sysroot $SYSROOT_DIR"
173   CC="$TOOLCHAIN_TRIPLET-gcc $SYSROOT"
174   CXX="$TOOLCHAIN_TRIPLET-g++ $SYSROOT"
175   export CC CXX
176 fi
177 NDK_TOOLCHAIN_VERSION=`${TOOLCHAIN_TRIPLET}-gcc --version | head -1 | sed -e 's/.* \([0-9]\+\.[0-9.]\+\)\( \|.*\)$/\1/'`
178
179 # Define some helper variables.
180 C="--host=$TOOLCHAIN_TRIPLET --prefix=$PREFIX"
181 CM="-Wno-dev -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake -DANDROID_ABI=${TARGET_ARCH} -DCMAKE_INSTALL_PREFIX=${PREFIX}"
182 if [ -n "$STANDALONE_TOOLCHAIN" ]; then
183   CM="$CM -DANDROID_STANDALONE_TOOLCHAIN=${STANDALONE_TOOLCHAIN}"
184   if [ -d "${ANDROID_NDK}/sources/cxx-stl" ]; then
185     CM="$CM -DANDROID_STL_PATH=${ANDROID_NDK}/sources/cxx-stl"
186   fi
187 else
188   CM="$CM -DANDROID_NDK=${ANDROID_NDK}"
189   CM="$CM -DANDROID_NATIVE_API_LEVEL=${NATIVE_API_LEVEL}"
190   CM="$CM -DANDROID_TOOLCHAIN_NAME=${TOOLCHAIN_TRIPLET}-${NDK_TOOLCHAIN_VERSION}"
191 fi
192
193 # Look for STL, needed to build bindings
194 STL_BASE="${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${NDK_TOOLCHAIN_VERSION}"
195 if [ -d ${STL_BASE}/include ]; then
196   CPPFLAGS_STL="-I${STL_BASE}/include -I${STL_BASE}/libs/${TARGET_ARCH}/include"
197   LDFLAGS_STL="-L${STL_BASE}/libs/${TARGET_ARCH} -Wl,-rpath=${STL_BASE}/libs/${TARGET_ARCH}"
198   BINDINGS_CONFIG="--enable-bindings"
199 else
200   echo "Warning: STL unavailable, libsigrok bindings will not be built."
201   CPPFLAGS_STL=
202   LDFLAGS_STL=
203   BINDINGS_CONFIG="--disable-bindings"
204 fi
205
206 # -----------------------------------------------------------------------------
207
208 # Get the latest versions of config.guess/config.sub that know about Android.
209 $GIT_CLONE git://git.savannah.gnu.org/config.git
210
211 # Get a toolchain for cmake that knows about Android.
212 $WGET https://android-cmake.googlecode.com/hg/toolchain/android.toolchain.cmake
213 patch < ../android.toolchain.cmake.patch android.toolchain.cmake
214
215 # -----------------------------------------------------------------------------
216
217 # Python (needed for libsigrokdecode)
218 # Don't do parallel Python builds, this doesn't seem to work well.
219 cd Python-$VER_PYTHON
220 make distclean $V
221 autoreconf
222 patch -p1 < ../../Python-$VER_PYTHON.patch
223 ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no ac_cv_func_pipe2=no ac_cv_func_fdatasync=no ac_cv_func_killpg=no ac_cv_func_waitid=no ac_cv_func_sigaltstack=no ./configure $C --build=x86_64-linux-gnu --disable-ipv6
224 sed -i "s/^#zlib/zlib/g" Modules/Setup
225 make PYTHON_FOR_BUILD='_PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) ./hostpython' install $V
226 cd ..
227
228 # libiconv (needed for glib)
229 $WGET http://ftp.gnu.org/pub/gnu/libiconv/libiconv-$VER_LIBICONV.tar.gz
230 tar xfz libiconv-$VER_LIBICONV.tar.gz
231 cd libiconv-$VER_LIBICONV
232 cp -f ../config/config.guess build-aux
233 cp -f ../config/config.sub build-aux
234 cp -f ../config/config.guess libcharset/build-aux
235 cp -f ../config/config.sub libcharset/build-aux
236 ./configure $C --enable-shared
237 make lib/localcharset.h $V
238 cd libcharset
239 make $PARALLEL $V
240 make install $V
241 cd ../lib
242 make $PARALLEL $V
243 make install $V
244 cd ..
245 test -d $PREFIX/include || mkdir $PREFIX/include
246 cp include/iconv.h.inst $PREFIX/include/iconv.h
247 cd ..
248
249 # gettext (needed for glib)
250 $WGET http://ftp.gnu.org/pub/gnu/gettext/gettext-$VER_GETTEXT.tar.gz
251 tar xfz gettext-$VER_GETTEXT.tar.gz
252 cd gettext-$VER_GETTEXT
253 ./configure $C --enable-shared --disable-libasprintf
254 cd gettext-runtime
255 make $PARALLEL $V
256 make install $V
257 cd ../..
258
259 # zlib (needed for glib and libzip)
260 $WGET http://zlib.net/zlib-$VER_ZLIB.tar.gz
261 tar xfz zlib-$VER_ZLIB.tar.gz
262 cd zlib-$VER_ZLIB
263 # Note: zlib's configure doesn't understand --host, we need to pass $CC.
264 CC="$TOOLCHAIN_TRIPLET-gcc $SYSROOT" ./configure --prefix=$PREFIX
265 make $PARALLEL $V
266 make install $V
267 cd ..
268
269 # pcre (needed for glib)
270 $WGET ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$VER_PCRE.tar.gz
271 tar xfz pcre-$VER_PCRE.tar.gz
272 cd pcre-$VER_PCRE
273 ./configure $C --disable-cpp
274 make $PARALLEL $V
275 make install $V
276 cd ..
277
278 # libffi (needed for glib)
279 $WGET ftp://sourceware.org/pub/libffi/libffi-$VER_LIBFFI.tar.gz
280 tar xfz libffi-$VER_LIBFFI.tar.gz
281 cd libffi-$VER_LIBFFI
282 ./configure $C
283 make $PARALLEL $V
284 make install $V
285 cd ..
286
287 # glib (needed for libsigrok)
288 $WGET http://ftp.gnome.org/pub/gnome/sources/glib/$VER_GLIB_SHORT/glib-$VER_GLIB.tar.xz
289 tar xfJ glib-$VER_GLIB.tar.xz
290 cd glib-$VER_GLIB
291 NOCONFIGURE=yes ./autogen.sh
292 # Note: Manual LDFLAGS/CPPFLAGS needed for libiconv, rest uses pkg-config.
293 LDFLAGS=-L$PREFIX/lib CPPFLAGS=-I$PREFIX/include glib_cv_stack_grows=no glib_cv_uscore=no ac_cv_func_posix_getpwuid_r=no ac_cv_func_posix_getgrgid_r=no ./configure --disable-compile-warnings $C
294 make $PARALLEL $V
295 make install $V
296 cd ..
297
298 # libzip (needed for libsigrok)
299 $WGET http://www.nih.at/libzip/libzip-$VER_LIBZIP.tar.gz
300 tar xfz libzip-$VER_LIBZIP.tar.gz
301 cd libzip-$VER_LIBZIP
302 cp -f ../config/config.guess .
303 cp -f ../config/config.sub .
304 ./configure $C
305 make $PARALLEL $V
306 make install $V
307 cd ..
308
309 # libusb-1.0 (needed for libsigrok)
310 $WGET http://$SF_MIRROR/project/libusb/libusb-1.0/libusb-$VER_LIBUSB/libusb-$VER_LIBUSB.tar.bz2
311 tar xfj libusb-$VER_LIBUSB.tar.bz2
312 cd libusb-$VER_LIBUSB
313 patch -p1 < ../../libusb-1.0.patch
314 ./configure $C --disable-udev
315 make $PARALLEL $V
316 make install $V
317 cd ..
318
319 # libftdi1 (needed for libsigrok)
320 $WGET http://www.intra2net.com/en/developer/libftdi/download/libftdi1-$VER_LIBFTDI1.tar.bz2
321 tar xfj libftdi1-$VER_LIBFTDI1.tar.bz2
322 cd libftdi1-$VER_LIBFTDI1
323 cmake $CM -DFTDIPP=no -DDOCUMENTATION=no -DEXAMPLES=no -DFTDI_EEPROM=no -DPYTHON_BINDINGS=no -DBUILD_TESTS=n .
324 make $PARALLEL $V
325 make install $V
326 cd ..
327
328 # libserialport
329 $GIT_CLONE git://sigrok.org/libserialport
330 cd libserialport
331 ./autogen.sh
332 mkdir build
333 cd build
334 ../configure $C
335 make $PARALLEL $V
336 make install $V
337 cd ../..
338
339 # Build dependencies for libsigrok bindings if needed
340
341 if [ x"$CPPFLAGS_STL" != x ]; then
342
343   # libsigc++ (needed for glibmm)
344   $WGET http://ftp.gnome.org/pub/gnome/sources/libsigc++/$VER_LIBSIGCXX_SHORT/libsigc++-$VER_LIBSIGCXX.tar.xz
345   tar xfJ libsigc++-$VER_LIBSIGCXX.tar.xz
346   cd libsigc++-$VER_LIBSIGCXX
347   cp -f ../config/config.guess build
348   cp -f ../config/config.sub build
349   CPPFLAGS="$CPPFLAGS_STL" LDFLAGS="$LDFLAGS_STL -lgnustl_shared" ./configure --disable-documentation $C
350   make $PARALLEL $V
351   make install $V
352   cd ..
353
354   # glibmm (needed for libsigrok bindings)
355   $WGET http://ftp.gnome.org/pub/gnome/sources/glibmm/$VER_GLIBMM_SHORT/glibmm-$VER_GLIBMM.tar.xz
356   tar xfJ glibmm-$VER_GLIBMM.tar.xz
357   cd glibmm-$VER_GLIBMM
358   CPPFLAGS="$CPPFLAGS_STL" LDFLAGS="$LDFLAGS_STL -lgnustl_shared" ./configure --disable-documentation $C
359   make $PARALLEL $V
360   make install $V
361   cd ..
362
363 fi
364
365 # libsigrok
366 $GIT_CLONE git://sigrok.org/libsigrok
367 cd libsigrok
368 ./autogen.sh
369 mkdir build
370 cd build
371 CPPFLAGS="$CPPFLAGS_STL" LDFLAGS="$LDFLAGS_STL" ../configure $C $BINDINGS_CONFIG --datadir=/sdcard
372 make $PARALLEL $V
373 make datadir='$(datarootdir)' install $V
374 cd ../..
375
376 # libsigrokdecode
377 $GIT_CLONE git://sigrok.org/libsigrokdecode
378 cd libsigrokdecode
379 ./autogen.sh
380 mkdir build
381 cd build
382 ../configure $C
383 make $PARALLEL $V
384 make install $V
385 cd ../..
386
387 # sigrok-cli
388 $GIT_CLONE git://sigrok.org/sigrok-cli
389 cd sigrok-cli
390 ./autogen.sh
391 mkdir build
392 cd build
393 ../configure $C
394 make $PARALLEL $V
395 make install $V
396 cd ../..
397
398 # Check if we have all requirements to build PulseView:
399
400 if [ -z "$ANDROID_NDK" -o ! -d "$ANDROID_NDK" ]; then
401   echo "Android NDK not available, not building PulseView."
402   exit 0
403 fi
404
405 if [ -z "$ANDROID_SDK" -o ! -d "$ANDROID_SDK"/platforms/android-$VER_AND_API ]; then
406   echo "Android SDK with platform $VER_AND_API not available, not building PulseView."
407   exit 0
408 fi
409
410 if [ ! -e "${ANDROID_SDK}/tools/android" ]; then
411   echo "Essential Android build tools not available, not building PulseView."
412   exit 0
413 fi
414
415 NDK_HOST=`"$ANDROID_NDK/ndk-build" -p 2>/dev/null | awk '$1 == "HOST_TAG" { print $3 }'`
416
417 # Boost (needed for PulseView)
418 $WGET http://$SF_MIRROR/project/boost/boost/$VER_BOOST/boost_${VER_BOOST//./_}.tar.bz2
419 tar xfj boost_${VER_BOOST//./_}.tar.bz2
420 cd boost_${VER_BOOST//./_}
421 CC= ./bootstrap.sh --with-toolset=gcc --with-libraries=filesystem,system,thread,test --without-icu
422 echo "using gcc : $NDK_TOOLCHAIN_VERSION : \"${TOOLCHAIN_TRIPLET}-g++\" : <cxxflags>\"$SYSROOT -I$PREFIX/include $CPPFLAGS_STL\" <linkflags>\"$SYSROOT -L$PREFIX/lib $LDFLAGS_STL -lgnustl_shared\" ;" > user-config.jam
423 ./b2 -q -d+2 --ignore-site-config --user-config=user-config.jam toolset=gcc variant=release link=shared threading=multi runtime-link=shared target-os=android --prefix=$PREFIX --layout=system install $PARALLEL
424 cd ..
425
426 # Qt (needed for PulseView)
427 $WGET http://download.qt-project.org/official_releases/qt/${VER_QT%.*}/${VER_QT}/single/qt-everywhere-opensource-src-${VER_QT}.tar.gz
428 tar xfz qt-everywhere-opensource-src-${VER_QT}.tar.gz
429 cd qt-everywhere-opensource-src-${VER_QT}
430 patch -p1 < ../../android-qt.patch
431 CC= CXX= ./configure \
432         --prefix="$PREFIX" \
433         -developer-build \
434         -xplatform android-g++ \
435         -nomake tests \
436         -nomake examples \
437         -android-sdk "$ANDROID_SDK" \
438         -android-ndk "$ANDROID_NDK" \
439         -android-ndk-host "$NDK_HOST" \
440         -android-arch "$TARGET_ARCH" \
441         -android-toolchain-version "$NDK_TOOLCHAIN_VERSION" \
442         -skip qttranslations \
443         -skip qtwebkit \
444         -skip qtserialport \
445         -skip qtwebkit-examples \
446         -skip qtlocation \
447         -skip qtconnectivity \
448         -opensource -confirm-license -silent
449 export ANDROID_API_VERSION=android-$VER_AND_API
450 export ANDROID_BUILD_TOOLS_REVISION=$VER_AND_BT
451 make module-qtbase module-qtandroidextras module-qtsvg module-qtimageformats $PARALLEL $V
452 make install $V
453 cd ..
454
455 # sigrok-androidutils
456 $GIT_CLONE git://sigrok.org/sigrok-androidutils
457 cd sigrok-androidutils
458 ./autogen.sh
459 ./configure $C --with-android-sdk="${ANDROID_SDK}" --with-android-platform=android-$VER_AND_API
460 make $PARALLEL $V
461 make install $V
462 cd ..
463
464 # Strip all shared libs to reduce size.
465 find $PREFIX -iname "*.so" -exec ${TOOLCHAIN_TRIPLET}-strip -S {} \;
466
467 # PulseView
468 $GIT_CLONE git://sigrok.org/pulseview
469 cd pulseview
470 cmake $CM -DENABLE_TESTS=y .
471 make $PARALLEL $V
472 make install $V
473 ${TOOLCHAIN_TRIPLET}-strip -S libs/$TARGET_ARCH/libpulseview.so
474 cd android
475 "${ANDROID_SDK}/tools/android" update project -p . -t android-$VER_AND_API -n PulseView
476 ant debug
477 cd ../..