From 4180e6b31fd5b599a0bb25380277b35848cc525a Mon Sep 17 00:00:00 2001
From: Jonathan De Wachter <dewachter.jonathan@gmail.com>
Date: Mon, 23 Sep 2013 14:08:16 +0200
Subject: [PATCH] [Android] Added tools (unix bash scripts) to recompile
 Android extlibs

For some reason, one might want to recompile the extlibs but this is a tedious task. That's why these scripts are provided.
Please read readme.txt before using them!
---
 tools/android/clean_all.sh                    |  6 ++
 tools/android/compile_arm-v7a.sh              | 12 ++++
 tools/android/compile_arm.sh                  | 12 ++++
 tools/android/compile_libs.sh                 | 63 ++++++++++++++++++
 tools/android/compile_mips.sh                 | 12 ++++
 tools/android/compile_x86.sh                  | 12 ++++
 tools/android/create_toolchains.sh            | 40 ++++++++++++
 tools/android/download_sources.sh             | 65 +++++++++++++++++++
 tools/android/make_all.sh                     | 38 +++++++++++
 .../patches/remove-so-version-suffix.diff     | 13 ++++
 tools/android/readme.txt                      | 27 ++++++++
 11 files changed, 300 insertions(+)
 create mode 100755 tools/android/clean_all.sh
 create mode 100755 tools/android/compile_arm-v7a.sh
 create mode 100755 tools/android/compile_arm.sh
 create mode 100755 tools/android/compile_libs.sh
 create mode 100755 tools/android/compile_mips.sh
 create mode 100755 tools/android/compile_x86.sh
 create mode 100755 tools/android/create_toolchains.sh
 create mode 100755 tools/android/download_sources.sh
 create mode 100755 tools/android/make_all.sh
 create mode 100644 tools/android/patches/remove-so-version-suffix.diff
 create mode 100644 tools/android/readme.txt

diff --git a/tools/android/clean_all.sh b/tools/android/clean_all.sh
new file mode 100755
index 00000000..c1443876
--- /dev/null
+++ b/tools/android/clean_all.sh
@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+
+rm -rf tmp
+rm -rf build
+rm -rf src
+rm -rf toolchains
diff --git a/tools/android/compile_arm-v7a.sh b/tools/android/compile_arm-v7a.sh
new file mode 100755
index 00000000..ef1c6862
--- /dev/null
+++ b/tools/android/compile_arm-v7a.sh
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+DESTDIR=$PWD/tmp/arm-v7a
+
+PATH=$PWD/toolchains/arm/bin:$PATH
+CC=arm-linux-androideabi-gcc
+CXX=arm-linux-androideabi-g++
+CFLAGS="-I$DESTDIR/usr/include -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
+CPPFLAGS="-I$DESTDIR/usr/include -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
+LDFLAGS="-L$DESTDIR/usr/lib -march=armv7-a -Wl,--fix-cortex-a8 -lstlport_shared"
+
+./compile_libs.sh arm-v7a $PATH $CC $CXX "$CFLAGS" "$CPPFLAGS" "$LDFLAGS"
diff --git a/tools/android/compile_arm.sh b/tools/android/compile_arm.sh
new file mode 100755
index 00000000..ee30109a
--- /dev/null
+++ b/tools/android/compile_arm.sh
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+DESTDIR=$PWD/tmp/arm
+
+PATH=$PWD/toolchains/arm/bin:$PATH
+CC=arm-linux-androideabi-gcc
+CXX=arm-linux-androideabi-g++
+CFLAGS=-I$DESTDIR/usr/include
+CPPFLAGS=-I$DESTDIR/usr/include
+LDFLAGS=-L$DESTDIR/usr/lib
+
+./compile_libs.sh arm $PATH $CC $CXX $CFLAGS $CPPFLAGS $LDFLAGS
diff --git a/tools/android/compile_libs.sh b/tools/android/compile_libs.sh
new file mode 100755
index 00000000..fe932a99
--- /dev/null
+++ b/tools/android/compile_libs.sh
@@ -0,0 +1,63 @@
+#!/usr/bin/env bash
+
+DESTDIR=$PWD/tmp
+LOCALDIR=$PWD
+
+OPATH=$PATH
+
+export PATH=$2
+export CC=$3
+export CXX=$4
+export CFLAGS=$5
+export CPPFLAGS=$6
+export LDFLAGS=$7
+
+if [ "$1" = "arm" ]
+then
+	ARCH=arm-linux
+    ANDROID_ABI=armeabi
+
+elif [ "$1" = "arm-v7a" ]
+then
+	ARCH=arm-linux
+    ANDROID_ABI=armeabi-v7a
+
+elif [ "$1" = "x86" ]
+then
+	ARCH=i686-linux
+    ANDROID_ABI=x86
+
+elif [ "$1" = "mips" ]
+then
+	ARCH=mips-linux
+    ANDROID_ABI=mips
+fi
+
+HOST="--host=$ARCH"
+PREFIX="--prefix=$DESTDIR/$1/usr need_version=no"
+
+# Compile OGG
+cd $LOCALDIR/build/libogg-* && sed -i 's/-version-info/-avoid-version/g' src/Makefile.in src/Makefile.am && ./configure $HOST $PREFIX && make && make install
+rm $DESTDIR/$1/usr/lib/libogg*.so*
+
+# Compile FLAC
+cd  $LOCALDIR/build/flac-* && sed -i 's/-version-info/-avoid-version/g' src/libFLAC/Makefile.in src/libFLAC/Makefile.am && ./configure $HOST $PREFIX && make && make install
+rm $DESTDIR/$1/usr/lib/libFLAC*.so*
+
+# Compile VORBIS
+cd  $LOCALDIR/build/libvorbis-* && sed -i 's/-version-info/-avoid-version/g' lib/Makefile.in lib/Makefile.am && ./configure $HOST $PREFIX  && make && make install
+rm $DESTDIR/$1/usr/lib/libvorbis*.so*
+
+# Compile libsndfile (important: --disable-sqlite)
+cd  $LOCALDIR/build/libsndfile-* && sed -i 's/-version-info/-avoid-version/g' src/Makefile.in src/Makefile.am && ./configure $HOST $PREFIX --disable-sqlite && make && make install
+
+# Compile JPEG
+cd $LOCALDIR/build/jpeg-* && sed -i 's/-version-info/-avoid-version/g' Makefile.in Makefile.am && ./configure $HOST $PREFIX && make && make install
+
+# Compile freetype
+cd  $LOCALDIR/build/freetype-* && sed -i 's/-version-info/-avoid-version/g' builds/unix/unix-cc.in && ./configure $HOST $PREFIX && make && make install
+
+# Compile OpenAL-Soft
+cd  $LOCALDIR/build/openal-soft-android-master && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_CMAKE_TOOLCHAIN -DANDROID_ABI=$ANDROID_ABI -DANDROID_NATIVE_API_LEVEL=android-9 -DANDROID_USE_STLPORT=1 .. && make openal && mv libopenal.so $DESTDIR/$1/usr/lib
+
+export PATH=$OPATH
diff --git a/tools/android/compile_mips.sh b/tools/android/compile_mips.sh
new file mode 100755
index 00000000..96985b98
--- /dev/null
+++ b/tools/android/compile_mips.sh
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+DESTDIR=$PWD/tmp/mips
+
+PATH=$PWD/toolchains/mips/bin:$PATH
+CC=mipsel-linux-android-gcc
+CXX=mipsel-linux-android-g++
+CFLAGS=-I$DESTDIR/usr/include
+CPPFLAGS=-I$DESTDIR/usr/include
+LDFLAGS=-L$DESTDIR/usr/lib
+
+./compile_libs.sh mips $PATH $CC $CXX $CFLAGS $CPPFLAGS $LDFLAGS
diff --git a/tools/android/compile_x86.sh b/tools/android/compile_x86.sh
new file mode 100755
index 00000000..20f9fbbb
--- /dev/null
+++ b/tools/android/compile_x86.sh
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+DESTDIR=$PWD/tmp/x86
+
+PATH=$PWD/toolchains/x86/bin:$PATH
+CC=i686-linux-android-gcc
+CXX=i686-linux-android-g++
+CFLAGS=-I$DESTDIR/usr/include
+CPPFLAGS=-I$DESTDIR/usr/include
+LDFLAGS=-L$DESTDIR/usr/lib
+
+./compile_libs.sh x86 $PATH $CC $CXX $CFLAGS $CPPFLAGS $LDFLAGS
diff --git a/tools/android/create_toolchains.sh b/tools/android/create_toolchains.sh
new file mode 100755
index 00000000..9f1d4ff2
--- /dev/null
+++ b/tools/android/create_toolchains.sh
@@ -0,0 +1,40 @@
+#!/usr/bin/env bash
+
+create_toolchain () {
+
+    export SYSROOT=$NDK/platforms/android-$1/arch-$2/
+
+    MAKE=$NDK/build/tools/make-standalone-toolchain.sh
+
+    PLATFORM=--platform=android-$1
+    DIR=--install-dir=toolchains/$2
+
+	# Abort if already created
+	if [ -d "$PWD/toolchains/$2" ]
+	then
+		return
+	fi
+
+    if [ "$2" = "arm" ]
+    then
+        TOOLCHAIN=--toolchain=arm-linux-androideabi-4.8
+    elif [ "$2" = "x86" ]
+    then
+        TOOLCHAIN=--toolchain=x86-4.8
+    elif [ "$2" = "mips" ]
+    then
+        TOOLCHAIN=--toolchain=mipsel-linux-android-4.8
+    else
+        echo "Abort."
+        exit 1
+    fi
+
+    $MAKE $PLATFORM $TOOLCHAIN $DIR --stl=stlport
+
+    # move linux/soundcard.h to sys/soundcard.h
+    mv $PWD/toolchains/$2/sysroot/usr/include/linux/soundcard.h $PWD/toolchains/$2/sysroot/usr/include/sys
+}
+
+create_toolchain 9 arm
+create_toolchain 9 x86
+create_toolchain 9 mips
diff --git a/tools/android/download_sources.sh b/tools/android/download_sources.sh
new file mode 100755
index 00000000..c0370be7
--- /dev/null
+++ b/tools/android/download_sources.sh
@@ -0,0 +1,65 @@
+#!/usr/bin/env bash
+
+FLAC_VERSION=1.2.1
+VORBIS_VERSION=1.3.3
+OGG_VERSION=1.3.1
+
+FLAC=flac-$FLAC_VERSION
+VORBIS=libvorbis-$VORBIS_VERSION
+OGG=libogg-$OGG_VERSION
+
+SNDFILE_VERSION=1.0.25
+SNDFILE=libsndfile-$SNDFILE_VERSION
+
+JPEG_VERSION=9
+JPEG=jpeg-$JPEG_VERSION
+
+FREETYPE_VERSION=2.4.0
+FREETYPE=freetype-$FREETYPE_VERSION
+
+
+mkdir build
+
+wget -nc -P src http://downloads.xiph.org/releases/flac/$FLAC.tar.gz
+if [ ! -d "$PWD/tmp/$FLAC" ]
+then
+	tar -C build -xf src/$FLAC.tar.gz
+fi
+
+wget -nc -P src http://downloads.xiph.org/releases/vorbis/$VORBIS.tar.gz
+if [ ! -d "$PWD/tmp/$VORBIS" ]
+then
+	tar -C build -xf src/$VORBIS.tar.gz
+fi
+
+wget -nc -P src http://downloads.xiph.org/releases/ogg/$OGG.tar.gz
+if [ ! -d "$PWD/tmp/$OGG" ]
+then
+	tar -C build -xf src/$OGG.tar.gz
+fi
+
+wget -nc -P src http://www.mega-nerd.com/libsndfile/files/$SNDFILE.tar.gz
+if [ ! -d "$PWD/tmp/$SNDFILE" ]
+then
+	tar -C build -xf src/$SNDFILE.tar.gz
+fi
+
+wget -nc -P src http://www.ijg.org/files/jpegsrc.v$JPEG_VERSION.tar.gz
+if [ ! -d "$PWD/tmp/$JPEG" ]
+then
+	tar -C build -xf src/jpegsrc.v$JPEG_VERSION.tar.gz
+fi
+
+wget -nc -P src http://download.savannah.gnu.org/releases/freetype/$FREETYPE.tar.gz
+if [ ! -d "$PWD/tmp/$FREETYPE" ]
+then
+	tar -C build -xf src/$FREETYPE.tar.gz
+fi
+
+wget -nc -P src https://github.com/AerialX/openal-soft-android/archive/master.tar.gz
+if [ ! -d "$PWD/tmp/openal-soft-android-master" ]
+then
+	tar -C build -xf src/master.tar.gz
+fi
+
+patch build/openal-soft-android-master/CMakeLists.txt patches/remove-so-version-suffix.diff
diff --git a/tools/android/make_all.sh b/tools/android/make_all.sh
new file mode 100755
index 00000000..2281aa2d
--- /dev/null
+++ b/tools/android/make_all.sh
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+
+# Abort if no known installation of NDK
+if [ "$NDK" == "" ]
+then
+	echo "Where is the NDK location ?"
+	echo -n "NDK: "; read NDK
+	export NDK=$NDK
+fi
+
+export ANDROID_NDK=$NDK
+
+# Abort if we don't know the Android CMake toolchain location
+if [ "$ANDROID_CMAKE_TOOLCHAIN" == "" ]
+then
+	echo "Where is the Android CMake toolchain ?"
+	echo -n "ANDROID_CMAKE_TOOLCHAIN: "; read ANDROID_CMAKE_TOOLCHAIN
+	export ANDROID_CMAKE_TOOLCHAIN=$ANDROID_CMAKE_TOOLCHAIN
+fi
+
+./clean_all.sh
+
+./create_toolchains.sh
+
+./download_sources.sh
+./compile_arm.sh
+
+rm -r $PWD/build
+./download_sources.sh
+./compile_x86.sh
+
+rm -r $PWD/build
+./download_sources.sh
+./compile_mips.sh
+
+rm -r $PWD/build
+./download_sources.sh
+./compile_arm-v7a.sh
diff --git a/tools/android/patches/remove-so-version-suffix.diff b/tools/android/patches/remove-so-version-suffix.diff
new file mode 100644
index 00000000..257d553d
--- /dev/null
+++ b/tools/android/patches/remove-so-version-suffix.diff
@@ -0,0 +1,13 @@
+--- CMakeLists.txt.orig	2013-09-21 15:23:49.305453804 +0200
++++ CMakeLists.txt	2013-09-21 15:24:08.621454210 +0200
+@@ -717,9 +717,7 @@
+ # Build a library
+ ADD_LIBRARY(${LIBNAME} ${LIBTYPE} ${OPENAL_OBJS} ${ALC_OBJS})
+ SET_TARGET_PROPERTIES(${LIBNAME} PROPERTIES DEFINE_SYMBOL AL_BUILD_LIBRARY
+-                                            COMPILE_FLAGS -DAL_ALEXT_PROTOTYPES
+-                                            VERSION ${LIB_VERSION}.0
+-                                            SOVERSION ${LIB_MAJOR_VERSION})
++                                            COMPILE_FLAGS -DAL_ALEXT_PROTOTYPES)
+ IF(WIN32 AND NOT LIBTYPE STREQUAL "STATIC")
+     SET_TARGET_PROPERTIES(${LIBNAME} PROPERTIES PREFIX "")
+ ENDIF()
diff --git a/tools/android/readme.txt b/tools/android/readme.txt
new file mode 100644
index 00000000..23949d1a
--- /dev/null
+++ b/tools/android/readme.txt
@@ -0,0 +1,27 @@
+Compiling external libraries for Android can be a tedious task, especially for
+those who aren't familiar with the NDK, that's why we provide these scripts.
+
+IMPORTANT: Please, be careful when using these scripts! They are unpolished at
+the moment and you'll have to respect a simple rule: call these scripts from
+where they are. So, in that case, head yourself to tools/android, then call
+./make_all.sh.
+
+Feel free to improve them or send patches.
+
+HOW-TO-USE:
+-----------
+1) Some of these scripts need an environement variable to work ($NDK) as well
+as the Android CMake toolchain you'll find in cmake/toolchains (android.toolchain.cmake)
+export NDK=/path/to/your/ndk
+export ANDROID_CMAKE_TOOLCHAIN=/path/to/android.toolchain.cmake
+
+2) You'll need to make them executable, so:
+chmod +x *.sh
+
+3) Type: ./make_all.sh which should create standalone toolchains, download the
+external libraries and compile them.
+
+These scripts will be improved over time. Meanwhile, you'll have to play with
+them if you want a customized behavior.
+
+Good luck!