set -e
+# The path where Android NDK is located.
+ANDROID_NDK=/usr/local/android-ndk-r9d
+
+# The path where Android SDK is located.
+ANDROID_SDK=/usr/local/android
+
# The path where your Android toolchain directory is located.
TOOLCHAIN=$HOME/android/android-arm-toolchain
# TOOLCHAIN=$HOME/android/android-mipsel-toolchain
PREFIX=$HOME/sr_android
# The path where to download files to and where to build packages.
-BUILDDIR=./sr_android_build
+BUILDDIR=./build
# Edit this to enable/disable/modify parallel compiles.
PARALLEL="-j 2"
VER_GLIB=2.38.2
VER_GLIB_SHORT=2.38
VER_LIBZIP=0.11.1
-VER_LIBUSB=0.1.12
-VER_LIBFTDI=0.20
+VER_LIBFTDI1=1.1
VER_LIBUSBX=1.0.17
VER_PYTHON=3.3.3
+VER_BOOST=1.55.0
+VER_QT=n0.31
SF_MIRROR=switch.dl.sourceforge.net
+WGET="wget --quiet"
+GIT_CLONE="git clone --depth=1"
+
+case "$TOOLCHAIN_TRIPLET" in
+ arm*) TARGET_ARCH=armeabi;;
+ mips*) TARGET_ARCH=mips;;
+ i686*) TARGET_ARCH=x86;;
+ *) echo >&2 "Unknown prefix for TOOLCHAIN_TRIPLET"; exit 1;;
+esac
+
# -----------------------------------------------------------------------------
# Remove build directory contents (if any) and create a new build dir.
cd $BUILDDIR
# Build host Python before we start messing with the environment.
-wget http://python.org/ftp/python/$VER_PYTHON/Python-$VER_PYTHON.tar.xz
+$WGET http://python.org/ftp/python/$VER_PYTHON/Python-$VER_PYTHON.tar.xz
tar xJf Python-$VER_PYTHON.tar.xz
cd Python-$VER_PYTHON
./configure
# Define some helper variables.
C="--host=$TOOLCHAIN_TRIPLET --prefix=$PREFIX"
+CM="-Wno-dev -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake -DANDROID_ABI=${TARGET_ARCH} -DCMAKE_INSTALL_PREFIX=${PREFIX} -DANDROID_STANDALONE_TOOLCHAIN=${TOOLCHAIN}"
# Get the latest versions of config.guess/config.sub that know about Android.
-git clone git://git.savannah.gnu.org/config.git
+$GIT_CLONE git://git.savannah.gnu.org/config.git
+
+# Get a toolchain for cmake that knows about Android
+wget https://android-cmake.googlecode.com/hg/toolchain/android.toolchain.cmake
+patch < ../android.toolchain.cmake.patch android.toolchain.cmake
# -----------------------------------------------------------------------------
cd ..
# libiconv (needed for glib)
-wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-$VER_LIBICONV.tar.gz
+$WGET http://ftp.gnu.org/pub/gnu/libiconv/libiconv-$VER_LIBICONV.tar.gz
tar xfz libiconv-$VER_LIBICONV.tar.gz
cd libiconv-$VER_LIBICONV
cp -f ../config/config.guess build-aux
cd ..
# gettext (needed for glib)
-wget http://ftp.gnu.org/pub/gnu/gettext/gettext-$VER_GETTEXT.tar.gz
+$WGET http://ftp.gnu.org/pub/gnu/gettext/gettext-$VER_GETTEXT.tar.gz
tar xfz gettext-$VER_GETTEXT.tar.gz
cd gettext-$VER_GETTEXT
./configure $C --enable-shared --disable-libasprintf
cd ../..
# zlib (needed for glib and libzip)
-wget http://zlib.net/zlib-$VER_ZLIB.tar.gz
+$WGET http://zlib.net/zlib-$VER_ZLIB.tar.gz
tar xfvz zlib-$VER_ZLIB.tar.gz
cd zlib-$VER_ZLIB
# Note: zlib's configure doesn't understand --host, we need to pass $CC.
cd ..
# pcre (needed for glib)
-wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$VER_PCRE.tar.gz
+$WGET ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$VER_PCRE.tar.gz
tar xfvz pcre-$VER_PCRE.tar.gz
cd pcre-$VER_PCRE
./configure $C --disable-cpp
cd ..
# libffi (needed for glib)
-wget ftp://sourceware.org/pub/libffi/libffi-$VER_LIBFFI.tar.gz
+$WGET ftp://sourceware.org/pub/libffi/libffi-$VER_LIBFFI.tar.gz
tar xfz libffi-$VER_LIBFFI.tar.gz
cd libffi-$VER_LIBFFI
./configure $C
cd ..
# glib
-wget http://ftp.gnome.org/pub/gnome/sources/glib/$VER_GLIB_SHORT/glib-$VER_GLIB.tar.xz
+$WGET http://ftp.gnome.org/pub/gnome/sources/glib/$VER_GLIB_SHORT/glib-$VER_GLIB.tar.xz
tar xJf glib-$VER_GLIB.tar.xz
cd glib-$VER_GLIB
./autogen.sh
cd ..
# libzip
-wget http://www.nih.at/libzip/libzip-$VER_LIBZIP.tar.gz
+$WGET http://www.nih.at/libzip/libzip-$VER_LIBZIP.tar.gz
tar xfz libzip-$VER_LIBZIP.tar.gz
cd libzip-$VER_LIBZIP
cp -f ../config/config.guess .
make install
cd ..
-# libusb-0.1 (not used directly, but needed for libftdi)
-wget http://$SF_MIRROR/project/libusb/libusb-0.1%20%28LEGACY%29/$VER_LIBUSB/libusb-$VER_LIBUSB.tar.gz
-tar xfz libusb-$VER_LIBUSB.tar.gz
-cd libusb-$VER_LIBUSB
-cp -f ../config/config.guess .
-cp -f ../config/config.sub .
-./configure $C
-# Explicitly exclude libusbpp.la from lib_LTLIBRARIES, and skip subdirs.
-make lib_LTLIBRARIES=libusb.la SUBDIRS=. $PARALLEL
-make lib_LTLIBRARIES=libusb.la SUBDIRS=. install
-cd ..
-
-# libftdi
-wget http://www.intra2net.com/en/developer/libftdi/download/libftdi-$VER_LIBFTDI.tar.gz
-tar xfz libftdi-$VER_LIBFTDI.tar.gz
-cd libftdi-$VER_LIBFTDI
-cp -f ../config/config.guess .
-cp -f ../config/config.sub .
-# libftdi needs to find libusb-config which is installed in $PREFIX/bin.
-PATH=$PREFIX/bin:$PATH ./configure $C
-make $PARALLEL
-make install
-cd ..
-
# libusb-1.0
-wget http://$SF_MIRROR/project/libusbx/releases/$VER_LIBUSBX/source/libusbx-$VER_LIBUSBX.tar.bz2
+$WGET http://$SF_MIRROR/project/libusbx/releases/$VER_LIBUSBX/source/libusbx-$VER_LIBUSBX.tar.bz2
tar xfj libusbx-$VER_LIBUSBX.tar.bz2
cd libusbx-$VER_LIBUSBX
patch -p1 < ../../libusb-1.0.patch
make install
cd ..
+# libftdi1
+$WGET http://www.intra2net.com/en/developer/libftdi/download/libftdi1-$VER_LIBFTDI1.tar.bz2
+tar xfj libftdi1-$VER_LIBFTDI1.tar.bz2
+cd libftdi1-$VER_LIBFTDI1
+cmake $CM .
+make $PARALLEL
+make install
+cd ..
+
# libserialport
-git clone git://sigrok.org/libserialport
+$GIT_CLONE git://sigrok.org/libserialport
cd libserialport
./autogen.sh
-./configure $C --without-libudev
-make $PARALLEL
+./configure $C
+make $PARALLEL V=1
make install
cd ..
# libsigrok
-git clone git://sigrok.org/libsigrok
+$GIT_CLONE git://sigrok.org/libsigrok
cd libsigrok
./autogen.sh
./configure $C
-make $PARALLEL
+make $PARALLEL V=1
make install
cd ..
# libsigrokdecode
-git clone git://sigrok.org/libsigrokdecode
+$GIT_CLONE git://sigrok.org/libsigrokdecode
cd libsigrokdecode
./autogen.sh
./configure $C
-make $PARALLEL
+make $PARALLEL V=1
make install
cd ..
# sigrok-cli
-git clone git://sigrok.org/sigrok-cli
+$GIT_CLONE git://sigrok.org/sigrok-cli
cd sigrok-cli
./autogen.sh
./configure $C
-make $PARALLEL
+make $PARALLEL V=1
make install
cd ..
+
+# Check if we have kit to build PulseView
+
+if [ -z "$ANDROID_NDK" -o ! -d "$ANDROID_NDK" ]; then
+ echo "Android NDK not available, not building PulseView"
+ exit 0
+fi
+
+if [ -z "$ANDROID_SDK" -o ! -d "$ANDROID_SDK"/platforms/android-4 -o \
+ ! -d "$ANDROID_SDK"/platforms/android-7 -o \
+ ! -d "$ANDROID_SDK"/platforms/android-8 -o \
+ ! -d "$ANDROID_SDK"/platforms/android-14 ]; then
+ echo "Android SDK with platforms 4 7 8 14 not available, not building PulseView"
+ exit 0
+fi
+
+ANDROID_TOOLS=${ANDROID_SDK}/tools
+ANDROID_BUILD_TOOLS=${ANDROID_SDK}/build-tools/19.0.1
+
+if [ ! -e "$ANDROID_TOOLS/android" -o ! -e "$ANDROID_BUILD_TOOLS/dx" ]; then
+ echo "Essential Android build tools not available, not building PulseView"
+ exit 0
+fi
+
+NDK_TOOLCHAIN_VERSION=`${TOOLCHAIN_TRIPLET}-gcc --version | head -1 | sed -e 's/.* \([0-9]\+\.[0-9.]\+\)\( \|.*\)$/\1/'`
+NDK_HOST=`"$ANDROID_NDK/ndk-build" -p 2>/dev/null | awk '$1 == "HOST_TAG" { print $3 }'`
+
+
+# boost (needed for PulseView)
+$WGET http://$SF_MIRROR/project/boost/boost/$VER_BOOST/boost_${VER_BOOST//./_}.tar.bz2
+tar xfj boost_${VER_BOOST//./_}.tar.bz2
+cd boost_${VER_BOOST//./_}
+patch -p1 < ../../boost.patch
+./bootstrap.sh --with-toolset=gcc --with-libraries=filesystem,system,thread --without-icu
+echo "using gcc : $NDK_TOOLCHAIN_VERSION : \"${TOOLCHAIN_TRIPLET}-g++\" : <cxxflags>\"-I$PREFIX/include -I${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${NDK_TOOLCHAIN_VERSION}/include -I${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${NDK_TOOLCHAIN_VERSION}/libs/${TARGET_ARCH}/include\" <linkflags>\"-L$PREFIX/lib -L${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${NDK_TOOLCHAIN_VERSION}/libs/${TARGET_ARCH} -lgnustl_shared\" ;" > user-config.jam
+./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
+cd ..
+
+# qt (needed for PulseView)
+$GIT_CLONE -b $VER_QT git://anongit.kde.org/android-qt.git
+cd android-qt
+patch -p1 < ../../android-qt.patch
+ANDROID_SDK_PLATFORM_TOOLS_PATH=$ANDROID_BUILD_TOOLS ANDROID_SDK_TOOLS_PATH=$ANDROID_TOOLS ./android/androidconfigbuild.sh -n $ANDROID_NDK -a $TARGET_ARCH -l 14 -q 1 -d 1 -i $PREFIX -k 1 -o $NDK_HOST -v $NDK_TOOLCHAIN_VERSION
+cd ..
+
+# PulseView
+$GIT_CLONE git://sigrok.org/pulseview
+cd pulseview
+patch -p1 < ../../pulseview-cpp.patch
+cmake $CM -DQT_QMAKE_EXECUTABLE=${PREFIX}/bin/qmake -DANDROID_STL_PATH=${ANDROID_NDK}/sources/cxx-stl -DENABLE_DECODE=FALSE .
+make $PARALLEL
+cp -r "$PREFIX"/src/android/java .
+cp -r libs java/
+cd java
+patch -p2 < ../../../pulseview-java.patch
+mkdir -p assets
+READELF=$TOOLCHAIN_TRIPLET-readelf ../../../fix-libraries.sh "$PREFIX/lib" libs/"$TARGET_ARCH" libpulseview.so > assets/liblist.txt
+cp ${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${NDK_TOOLCHAIN_VERSION}/libs/${TARGET_ARCH}/libgnustl_shared.so libs/${TARGET_ARCH}/
+"$ANDROID_TOOLS/android" update project -p . -t android-14 -n PulseView
+ant debug
+cd ../..