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