#!/bin/sh
set -e

ROOTDIR=$(cd $(dirname $0) && pwd -P)

if [ $# -lt 1 ]; then
    echo "Usage: $0 ARCH" 1>&2
    echo "" 1>&2
    echo "  ARCH: armeabi, armeabi-v7a, arm64, mips, mips64, x86, x86_64" 1>&2
    echo "" 1>&2
    echo "Example usage:" 1>&2
    echo " $0 arm64" 1>&2
    exit 1
fi

ARCH=$1
shift 

echo ""
echo "Cross compiling for $ARCH"

# Place where make-toolchain creates a standalone toolchain
export ANDROID_TOOLCHAIN=${ROOTDIR}/toolchain/${ARCH}

# Directory under the toolchain where we have headers and libs
export SYSROOT=${ANDROID_TOOLCHAIN}/sysroot

#
# Map the selected architecture to useful variables:
#
# TOOLCHAIN_NAME: name of the toolchain to use.
# DESTDIR_NAME: name of the directory where to install headers and libs.
# CFLAGS: flags to be passed to C compiler.
# CXXFLAGS: flags to be passed to C++ compiler.
# LIB_SUFFIX: unset unless toolchain has lib64 libs directory.
# CONFIG_EXTRA: optional extra flags to be passed to configure script.
#
if [ "$ARCH" = "arm64" ]; then
    # Note: arm64 is also known as aarch64 and as armv8
    TOOLCHAIN_NAME=aarch64-linux-android
    DESTDIR_NAME=arm64-v8a
elif [ "$ARCH" = "armeabi" ]; then
    TOOLCHAIN_NAME=arm-linux-androideabi
    DESTDIR_NAME=$ARCH
    #
    # <Bandwagon>These flags have been copied from the flags used by
    # ndk-build when building measurement-kit as a C++ library</>.
    #
    # According to Wikipedia [1], armv5te means v5 of the architecture,
    # with thumbs and DSP extensions. Xscale is a processor built by third
    # parties compatible with armv5te that doesn't have floating point,
    # hence the -msoft-float flag. The -mthumb instructions forces emitting
    # instructions using the thumb instruction set where common instructions
    # are encoded using 16 bits rather than 32 bits.
    #
    # [1] https://en.wikipedia.org/wiki/ARM_architecture#Cores
    #
    # Note: `-Os` was originally part of the flags but this led to
    # non working libevent binaries as documented in #1051. Also
    # `-fno-integrated-as` is needed to avoid another segfault.
    #
    CFLAGS="-target armv5te-none-linux-androideabi -march=armv5te -mtune=xscale -msoft-float -mthumb -fno-integrated-as"
    CXXFLAGS=$CFLAGS
elif [ "$ARCH" = "armeabi-v7a" ]; then
    TOOLCHAIN_NAME=arm-linux-androideabi
    DESTDIR_NAME=$ARCH
    #
    # <Bandwagon>See above.</>
    #
    CFLAGS="-target armv7-none-linux-androideabi -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb"
    CXXFLAGS="$CFLAGS"
elif [ "$ARCH" = "mips" ]; then
    # Note: mips is actually mipsel (i.e. little endian)
    TOOLCHAIN_NAME=mipsel-linux-android
    DESTDIR_NAME=mips
elif [ "$ARCH" = "mips64" ]; then
    # Note: this is mips 64bit little endian
    TOOLCHAIN_NAME=mips64el-linux-android
    DESTDIR_NAME=mips64
    LIB_SUFFIX=64
    CONFIG_EXTRA="--disable-asm"  # See issue 653
elif [ "$ARCH" = "x86" ]; then
    TOOLCHAIN_NAME=i686-linux-android
    DESTDIR_NAME=x86
elif [ "$ARCH" = "x86_64" ]; then
    TOOLCHAIN_NAME=x86_64-linux-android
    DESTDIR_NAME=x86_64
    LIB_SUFFIX=64
else
    echo "$0: invalid $ARCH" 1>&2
    exit 1
fi

# Override makefile's defaults with toolchain's tools. We use clang.
TOOL_PREFIX=${ANDROID_TOOLCHAIN}/bin/${TOOLCHAIN_NAME}
export CPP=${TOOL_PREFIX}-cpp
export AR=${TOOL_PREFIX}-ar
export AS=${TOOL_PREFIX}-as
export NM=${TOOL_PREFIX}-nm
export CC=${TOOL_PREFIX}-clang
export CXX=${TOOL_PREFIX}-clang++
export LD=${TOOL_PREFIX}-ld
export RANLIB=${TOOL_PREFIX}-ranlib
export STRIP=${TOOL_PREFIX}-strip

# Teach the preprocessor to find toolchain includes
export CPPFLAGS="${CPPFLAGS} --sysroot=${SYSROOT} -I${SYSROOT}/usr/include -I${ANDROID_TOOLCHAIN}/include"

# Pass specific flags to the C and/or CXX compiler
export CFLAGS="${CFLAGS}"
export CXXFLAGS="${CXXFLAGS}"

# Teach the linker to find toolchain libraries
export LDFLAGS="${LDFLAGS} -L${SYSROOT}/usr/lib${LIB_SUFFIX} -L${ANDROID_TOOLCHAIN}/lib"

# Extra libraries to link with. Should not be needed.
#export LIBS="-lm"

#
# Flags for ./build/dependency (cross-)build system
#

# Tell configure to use the toolchain and build only static libs
export pkg_configure_flags="--host=${TOOLCHAIN_NAME} --disable-shared $CONFIG_EXTRA"

# Where to install headers and libraries
export pkg_prefix="${ROOTDIR}"

# Tell ./build/dependency that we are cross compiling
export pkg_cross="jni"

# Tell ./build/dependency the name of the cross architecture
export pkg_cross_arch="${DESTDIR_NAME}"

if [ $# -gt 0 ]; then
    $@
else
    env
fi
