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