]> sigrok.org Git - sigrok-util.git/blame - cross-compile/android/sigrok-cross-android
sigrok-cross-android: Add a "prepare" option to setup SDK/NDK/toolchain.
[sigrok-util.git] / cross-compile / android / sigrok-cross-android
CommitLineData
0832e7d6
UH
1#!/bin/bash
2##
3## This file is part of the sigrok-util project.
4##
25b2b9f5 5## Copyright (C) 2013-2014 Uwe Hermann <uwe@hermann-uwe.de>
0832e7d6
UH
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.
11##
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.
16##
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
20##
21
c28fee03
MC
22set -e
23
3bdccc55
UH
24# The path where the NDK, SDK, and toolchains are located.
25ANDROID_PATH=$HOME/android
26
e25d7d5a 27# The path where the Android NDK is located.
3bdccc55 28ANDROID_NDK=$ANDROID_PATH/android-ndk-r10c
11b9792e 29
e25d7d5a 30# The path where the Android SDK is located.
3bdccc55 31ANDROID_SDK=$ANDROID_PATH/android-sdk-linux
11b9792e 32
0832e7d6 33# The path where your Android toolchain directory is located.
3bdccc55
UH
34TOOLCHAIN=$ANDROID_PATH/android-arm-toolchain
35# TOOLCHAIN=$ANDROID_PATH/android-mipsel-toolchain
36# TOOLCHAIN=$ANDROID_PATH/android-i686-toolchain
e2bb3ab1
UH
37
38# Select which Android toolchain to use.
39# Don't forget to also adapt TOOLCHAIN above if you change TOOLCHAIN_TRIPLET!
40TOOLCHAIN_TRIPLET=arm-linux-androideabi
41# TOOLCHAIN_TRIPLET=mipsel-linux-android
42# TOOLCHAIN_TRIPLET=i686-linux-android
0832e7d6
UH
43
44# The path where the cross-compiled packages will be installed.
45PREFIX=$HOME/sr_android
46
47# The path where to download files to and where to build packages.
07947dda 48BUILDDIR=./build
0832e7d6 49
819eb05b
UH
50# Edit this to enable/disable/modify parallel compiles.
51PARALLEL="-j 2"
52
0832e7d6
UH
53# You usually don't need to change anything below this line.
54
55# -----------------------------------------------------------------------------
56
57VER_LIBICONV=1.14
d4069d6e 58VER_GETTEXT=0.19.2
b118df5f 59VER_ZLIB=1.2.8
d4069d6e
UH
60VER_PCRE=8.35
61VER_LIBFFI=3.1
62VER_GLIB=2.41.3
63VER_GLIB_SHORT=2.41
64VER_LIBSIGCXX=2.3.2
65VER_LIBSIGCXX_SHORT=2.3
66VER_LIBZIP=0.11.2
7966c37c 67VER_LIBFTDI1=1.1
d4069d6e 68VER_LIBUSB=1.0.19
5451bfe2 69VER_PYTHON=3.3.3
11b9792e 70VER_BOOST=1.55.0
2256009e 71VER_QT=5.3.1
3bdccc55
UH
72VER_NDK=10c
73VER_SDK=23.0.2
0832e7d6
UH
74
75SF_MIRROR=switch.dl.sourceforge.net
76
ec49447e 77WGET="wget --quiet"
a51ca6bd 78GIT_CLONE="git clone --depth=1"
ec49447e 79
7966c37c
MC
80case "$TOOLCHAIN_TRIPLET" in
81 arm*) TARGET_ARCH=armeabi;;
82 mips*) TARGET_ARCH=mips;;
83 i686*) TARGET_ARCH=x86;;
e25d7d5a 84 *) echo >&2 "Unknown prefix for TOOLCHAIN_TRIPLET."; exit 1;;
7966c37c
MC
85esac
86
0832e7d6 87# -----------------------------------------------------------------------------
3bdccc55
UH
88
89# Get the Android NDK and SDK, install required packages and toolchains.
90if [ "x$1" = "xprepare" ]; then
91 mkdir -p $ANDROID_PATH
92 cd $ANDROID_PATH
93
94 # NDK
95 $WGET http://dl.google.com/android/ndk/android-ndk-r$VER_NDK-linux-x86.bin
96 chmod 700 android-ndk-r$VER_NDK-linux-x86.bin
97 ./android-ndk-r$VER_NDK-linux-x86.bin
98 cd android-ndk-r$VER_NDK
99 ./build/tools/make-standalone-toolchain.sh --platform=android-16 \
100 --toolchain=arm-linux-androideabi-4.9 \
101 --install-dir=$ANDROID_PATH/android-arm-toolchain
102 cd ..
103
104 # SDK
105 $WGET http://dl.google.com/android/android-sdk_r$VER_SDK-linux.tgz
106 tar xfz android-sdk_r$VER_SDK-linux.tgz
107 cd android-sdk-linux
108 ./tools/android update sdk --no-ui --filter platform-tools,build-tools-21.1.0,android-8,android-10,android-11,android-14,android-16
109 cd ..
110
111 exit
112fi
113
114# -----------------------------------------------------------------------------
0832e7d6 115
2e353a08
ML
116# Remove build directory contents (if any) and create a new build dir.
117rm -rf $BUILDDIR
118mkdir $BUILDDIR
119cd $BUILDDIR
120
e25d7d5a
UH
121# -----------------------------------------------------------------------------
122
2e353a08 123# Build host Python before we start messing with the environment.
510f5ccb 124# Don't do parallel builds, this doesn't seem to work well.
ec49447e 125$WGET http://python.org/ftp/python/$VER_PYTHON/Python-$VER_PYTHON.tar.xz
25b2b9f5 126tar xfJ Python-$VER_PYTHON.tar.xz
5451bfe2 127cd Python-$VER_PYTHON
2e353a08 128./configure
510f5ccb 129make python
2e353a08 130mv python hostpython
2e353a08
ML
131cd ..
132
133# -----------------------------------------------------------------------------
134
135# We need to find tools in the toolchain.
136export PATH=$TOOLCHAIN/bin:$PATH
0832e7d6 137
02c87af2 138# Tell pkg-config to only look for our cross-built stuff.
226830e4
MC
139export PKG_CONFIG_LIBDIR="$PREFIX/lib/pkgconfig"
140export -n PKG_CONFIG_PATH
141
0832e7d6 142# Define some helper variables.
e2bb3ab1 143C="--host=$TOOLCHAIN_TRIPLET --prefix=$PREFIX"
7966c37c 144CM="-Wno-dev -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake -DANDROID_ABI=${TARGET_ARCH} -DCMAKE_INSTALL_PREFIX=${PREFIX} -DANDROID_STANDALONE_TOOLCHAIN=${TOOLCHAIN}"
0832e7d6 145
2fddc139
MC
146# Look for STL, needed to build bindings
147NDK_TOOLCHAIN_VERSION=`${TOOLCHAIN_TRIPLET}-gcc --version | head -1 | sed -e 's/.* \([0-9]\+\.[0-9.]\+\)\( \|.*\)$/\1/'`
148STL_BASE="${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${NDK_TOOLCHAIN_VERSION}"
149if [ -d ${STL_BASE}/include ]; then
150 CPPFLAGS_STL="-I${STL_BASE}/include -I${STL_BASE}/libs/${TARGET_ARCH}/include"
151 LDFLAGS_STL="-L${STL_BASE}/libs/${TARGET_ARCH} -Wl,-rpath=${STL_BASE}/libs/${TARGET_ARCH}"
152 BINDINGS_CONFIG="--enable-bindings"
153else
154 echo "Warning: STL unavailable, libsigrok bindings will not be built."
155 CPPFLAGS_STL=
156 LDFLAGS_STL=
157 BINDINGS_CONFIG="--disable-bindings"
158fi
159
e25d7d5a
UH
160# -----------------------------------------------------------------------------
161
0832e7d6 162# Get the latest versions of config.guess/config.sub that know about Android.
a51ca6bd 163$GIT_CLONE git://git.savannah.gnu.org/config.git
0832e7d6 164
e25d7d5a
UH
165# Get a toolchain for cmake that knows about Android.
166$WGET https://android-cmake.googlecode.com/hg/toolchain/android.toolchain.cmake
7966c37c
MC
167patch < ../android.toolchain.cmake.patch android.toolchain.cmake
168
0832e7d6
UH
169# -----------------------------------------------------------------------------
170
f7b8aa73 171# Python (needed for libsigrokdecode)
510f5ccb 172# Don't do parallel Python builds, this doesn't seem to work well.
f7b8aa73
UH
173cd Python-$VER_PYTHON
174make distclean
175autoreconf
176patch -p1 < ../../Python-$VER_PYTHON.patch
177ac_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
9a98e0d1 178sed -i "s/^#zlib/zlib/g" Modules/Setup
510f5ccb 179make PYTHON_FOR_BUILD=./hostpython install
f7b8aa73
UH
180cd ..
181
85b46172 182# libiconv (needed for glib)
ec49447e 183$WGET http://ftp.gnu.org/pub/gnu/libiconv/libiconv-$VER_LIBICONV.tar.gz
85b46172
MC
184tar xfz libiconv-$VER_LIBICONV.tar.gz
185cd libiconv-$VER_LIBICONV
186cp -f ../config/config.guess build-aux
187cp -f ../config/config.sub build-aux
188cp -f ../config/config.guess libcharset/build-aux
189cp -f ../config/config.sub libcharset/build-aux
190./configure $C --enable-shared
191make lib/localcharset.h
192cd libcharset
819eb05b 193make $PARALLEL
85b46172
MC
194make install
195cd ../lib
819eb05b 196make $PARALLEL
85b46172
MC
197make install
198cd ..
199test -d $PREFIX/include || mkdir $PREFIX/include
200cp include/iconv.h.inst $PREFIX/include/iconv.h
201cd ..
0832e7d6 202
87ea3974 203# gettext (needed for glib)
ec49447e 204$WGET http://ftp.gnu.org/pub/gnu/gettext/gettext-$VER_GETTEXT.tar.gz
87ea3974
MC
205tar xfz gettext-$VER_GETTEXT.tar.gz
206cd gettext-$VER_GETTEXT
207./configure $C --enable-shared --disable-libasprintf
208cd gettext-runtime
819eb05b 209make $PARALLEL
87ea3974
MC
210make install
211cd ../..
0832e7d6
UH
212
213# zlib (needed for glib and libzip)
ec49447e 214$WGET http://zlib.net/zlib-$VER_ZLIB.tar.gz
25b2b9f5 215tar xfz zlib-$VER_ZLIB.tar.gz
167cb16e
UH
216cd zlib-$VER_ZLIB
217# Note: zlib's configure doesn't understand --host, we need to pass $CC.
e2bb3ab1 218CC=$TOOLCHAIN_TRIPLET-gcc ./configure --prefix=$PREFIX
819eb05b 219make $PARALLEL
c28fee03 220make install
167cb16e 221cd ..
0832e7d6
UH
222
223# pcre (needed for glib)
ec49447e 224$WGET ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$VER_PCRE.tar.gz
25b2b9f5 225tar xfz pcre-$VER_PCRE.tar.gz
85c9910a 226cd pcre-$VER_PCRE
091ed535 227./configure $C --disable-cpp
819eb05b 228make $PARALLEL
c28fee03 229make install
85c9910a 230cd ..
0832e7d6
UH
231
232# libffi (needed for glib)
ec49447e 233$WGET ftp://sourceware.org/pub/libffi/libffi-$VER_LIBFFI.tar.gz
0832e7d6
UH
234tar xfz libffi-$VER_LIBFFI.tar.gz
235cd libffi-$VER_LIBFFI
c28fee03 236./configure $C
819eb05b 237make $PARALLEL
c28fee03 238make install
0832e7d6
UH
239cd ..
240
e25d7d5a 241# glib (needed for libsigrok)
ec49447e 242$WGET http://ftp.gnome.org/pub/gnome/sources/glib/$VER_GLIB_SHORT/glib-$VER_GLIB.tar.xz
25b2b9f5 243tar xfJ glib-$VER_GLIB.tar.xz
f7b8aa73 244cd glib-$VER_GLIB
f0e760bb 245./autogen.sh
559be0ff 246# Note: Manual LDFLAGS/CPPFLAGS needed for libiconv, rest uses pkg-config.
e2bb3ab1 247LDFLAGS=-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
819eb05b 248make $PARALLEL
f0e760bb
MC
249make install
250cd ..
0832e7d6 251
e25d7d5a 252# libzip (needed for libsigrok)
ec49447e 253$WGET http://www.nih.at/libzip/libzip-$VER_LIBZIP.tar.gz
0832e7d6
UH
254tar xfz libzip-$VER_LIBZIP.tar.gz
255cd libzip-$VER_LIBZIP
256cp -f ../config/config.guess .
257cp -f ../config/config.sub .
c28fee03 258./configure $C
819eb05b 259make $PARALLEL
c28fee03 260make install
0832e7d6
UH
261cd ..
262
e25d7d5a 263# libusb-1.0 (needed for libsigrok)
d4069d6e
UH
264$WGET http://$SF_MIRROR/project/libusb/libusb-1.0/libusb-$VER_LIBUSB/libusb-$VER_LIBUSB.tar.bz2
265tar xfj libusb-$VER_LIBUSB.tar.bz2
266cd libusb-$VER_LIBUSB
c09c85b3 267patch -p1 < ../../libusb-1.0.patch
6907ee7e 268./configure $C --disable-udev
819eb05b 269make $PARALLEL
13b2dfdc
MC
270make install
271cd ..
272
e25d7d5a 273# libftdi1 (needed for libsigrok)
7966c37c
MC
274$WGET http://www.intra2net.com/en/developer/libftdi/download/libftdi1-$VER_LIBFTDI1.tar.bz2
275tar xfj libftdi1-$VER_LIBFTDI1.tar.bz2
276cd libftdi1-$VER_LIBFTDI1
9773a798 277cmake $CM -DFTDIPP=no -DDOCUMENTATION=no -DEXAMPLES=no -DFTDI_EEPROM=no -DPYTHON_BINDINGS=no .
7966c37c
MC
278make $PARALLEL
279make install
280cd ..
281
900a8684 282# libserialport
a51ca6bd 283$GIT_CLONE git://sigrok.org/libserialport
900a8684
ML
284cd libserialport
285./autogen.sh
67b2de8a
UH
286mkdir build
287cd build
288../configure $C
83b801db 289make $PARALLEL V=1
900a8684 290make install
67b2de8a 291cd ../..
900a8684 292
2fddc139
MC
293# Build dependencies for libsigrok bindings if needed
294
295if [ x"$CPPFLAGS_STL" != x ]; then
296
297 # libsigc++ (needed for glibmm)
298 $WGET http://ftp.gnome.org/pub/gnome/sources/libsigc++/$VER_LIBSIGCXX_SHORT/libsigc++-$VER_LIBSIGCXX.tar.xz
25b2b9f5 299 tar xfJ libsigc++-$VER_LIBSIGCXX.tar.xz
2fddc139
MC
300 cd libsigc++-$VER_LIBSIGCXX
301 cp -f ../config/config.guess build
302 cp -f ../config/config.sub build
9773a798 303 CPPFLAGS="$CPPFLAGS_STL" LDFLAGS="$LDFLAGS_STL -lgnustl_shared" ./configure --disable-documentation $C
2fddc139
MC
304 make $PARALLEL
305 make install
306 cd ..
307
308 # glibmm (needed for libsigrok bindings)
309 $WGET http://ftp.gnome.org/pub/gnome/sources/glibmm/$VER_GLIB_SHORT/glibmm-$VER_GLIB.tar.xz
25b2b9f5 310 tar xfJ glibmm-$VER_GLIB.tar.xz
2fddc139 311 cd glibmm-$VER_GLIB
9773a798 312 CPPFLAGS="$CPPFLAGS_STL" LDFLAGS="$LDFLAGS_STL -lgnustl_shared" ./configure --disable-documentation $C
2fddc139
MC
313 make $PARALLEL
314 make install
315 cd ..
316
317fi
318
c062d1dc 319# libsigrok
a51ca6bd 320$GIT_CLONE git://sigrok.org/libsigrok
c062d1dc
MC
321cd libsigrok
322./autogen.sh
67b2de8a
UH
323mkdir build
324cd build
325CPPFLAGS="$CPPFLAGS_STL" LDFLAGS="$LDFLAGS_STL" ../configure $C $BINDINGS_CONFIG --datadir=/sdcard
83b801db 326make $PARALLEL V=1
2fddc139 327make datadir='$(datarootdir)' install
67b2de8a 328cd ../..
02c87af2 329
2fbea156 330# libsigrokdecode
a51ca6bd 331$GIT_CLONE git://sigrok.org/libsigrokdecode
2fbea156
ML
332cd libsigrokdecode
333./autogen.sh
67b2de8a
UH
334mkdir build
335cd build
336../configure $C
83b801db 337make $PARALLEL V=1
2fbea156 338make install
67b2de8a 339cd ../..
2fbea156
ML
340
341# sigrok-cli
a51ca6bd 342$GIT_CLONE git://sigrok.org/sigrok-cli
2fbea156
ML
343cd sigrok-cli
344./autogen.sh
67b2de8a
UH
345mkdir build
346cd build
347../configure $C
83b801db 348make $PARALLEL V=1
2fbea156 349make install
67b2de8a 350cd ../..
819eb05b 351
e25d7d5a 352# Check if we have all requirements to build PulseView:
11b9792e
MC
353
354if [ -z "$ANDROID_NDK" -o ! -d "$ANDROID_NDK" ]; then
e25d7d5a 355 echo "Android NDK not available, not building PulseView."
11b9792e
MC
356 exit 0
357fi
358
e25d7d5a
UH
359if [ -z "$ANDROID_SDK" -o \
360 ! -d "$ANDROID_SDK"/platforms/android-8 -o \
7b888971
MC
361 ! -d "$ANDROID_SDK"/platforms/android-10 -o \
362 ! -d "$ANDROID_SDK"/platforms/android-11 -o \
363 ! -d "$ANDROID_SDK"/platforms/android-14 -o \
364 ! -d "$ANDROID_SDK"/platforms/android-16 ]; then
e25d7d5a 365 echo "Android SDK with platforms 8 10 11 14 16 not available, not building PulseView."
11b9792e
MC
366 exit 0
367fi
368
e25d7d5a
UH
369if [ ! -e "${ANDROID_SDK}/tools/android" ]; then
370 echo "Essential Android build tools not available, not building PulseView."
11b9792e
MC
371 exit 0
372fi
373
11b9792e
MC
374NDK_HOST=`"$ANDROID_NDK/ndk-build" -p 2>/dev/null | awk '$1 == "HOST_TAG" { print $3 }'`
375
e25d7d5a 376# Boost (needed for PulseView)
11b9792e
MC
377$WGET http://$SF_MIRROR/project/boost/boost/$VER_BOOST/boost_${VER_BOOST//./_}.tar.bz2
378tar xfj boost_${VER_BOOST//./_}.tar.bz2
379cd boost_${VER_BOOST//./_}
380patch -p1 < ../../boost.patch
7744cf5c 381./bootstrap.sh --with-toolset=gcc --with-libraries=filesystem,system --without-icu
2fddc139 382echo "using gcc : $NDK_TOOLCHAIN_VERSION : \"${TOOLCHAIN_TRIPLET}-g++\" : <cxxflags>\"-I$PREFIX/include $CPPFLAGS_STL\" <linkflags>\"-L$PREFIX/lib $LDFLAGS_STL -lgnustl_shared\" ;" > user-config.jam
ea9301bf 383./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
11b9792e
MC
384cd ..
385
e25d7d5a 386# Qt (needed for PulseView)
7b888971
MC
387$WGET http://download.qt-project.org/official_releases/qt/${VER_QT%.*}/${VER_QT}/single/qt-everywhere-opensource-src-${VER_QT}.tar.gz
388tar xfz qt-everywhere-opensource-src-${VER_QT}.tar.gz
389cd qt-everywhere-opensource-src-${VER_QT}
11b9792e 390patch -p1 < ../../android-qt.patch
2256009e 391./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
30adf6b9 392make module-qtbase module-qtandroidextras module-qtsvg module-qtimageformats $PARALLEL
7b888971 393make install
11b9792e 394cd ..
5d1a9a53 395
7d3d2c0c 396# sigrok-androidutils
621e2e6e 397$GIT_CLONE git://sigrok.org/sigrok-androidutils
7d3d2c0c
MC
398cd sigrok-androidutils
399./autogen.sh
400./configure $C --with-android-sdk="${ANDROID_SDK}"
bb260ef2 401make $PARALLEL V=1
7d3d2c0c
MC
402make install
403cd ..
404
f6a6d164
UH
405# Strip all shared libs to reduce size.
406find $PREFIX -iname "*.so" -exec ${TOOLCHAIN_TRIPLET}-strip -S {} \;
407
5d1a9a53 408# PulseView
621e2e6e 409$GIT_CLONE git://sigrok.org/pulseview
5d1a9a53 410cd pulseview
0c5c3b89 411cmake $CM -DANDROID_STL_PATH=${ANDROID_NDK}/sources/cxx-stl .
bb260ef2 412make $PARALLEL VERBOSE=1
f6a6d164
UH
413make install
414${TOOLCHAIN_TRIPLET}-strip -S libs/$TARGET_ARCH/libpulseview.so
1238f785 415cd android
e25d7d5a 416"${ANDROID_SDK}/tools/android" update project -p . -t android-14 -n PulseView
aef0361e
MC
417ant debug
418cd ../..
e25d7d5a 419