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