radare2/sys/ndk-gcc
2016-01-16 13:15:25 +01:00

121 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
# android gcc wrapper
if [ -z "${NDK}" ]; then
NDK=~/Downloads/android-ndk-r10e
if [ ! -d "${NDK}" ]; then
echo "Invalid path in NDK environment"
exit 1
fi
fi
if [ -n "${NDK_ARCH}" ]; then
ARCH=${NDK_ARCH}
else
ARCH=mips
ARCH=arm
ARCH=x86
fi
#------------------------------------------#
PROGDIR="$(dirname "$0")"
PROGDIR="$(cd "$PROGDIR" && pwd)"
ANDROID_KITKAT=19
ANDROID_LOLIPOP=21
# shellcheck disable=SC2019
# shellcheck disable=SC2018
OS="$(uname | tr 'A-Z' 'a-z')"
[ "${OS}" = macos ] && OS=darwin
ARCH2=${ARCH}
case "${ARCH}" in
aarch64)
ANDROID_SDK_VERSION=${ANDROID_LOLIPOP}
NDKPFX=${ARCH}-linux-android
ARCH2=arm64
;;
arm)
ANDROID_SDK_VERSION=${ANDROID_KITKAT}
NDKPFX=${ARCH}-linux-androideabi
;;
mips)
ANDROID_SDK_VERSION=${ANDROID_KITKAT}
NDKPFX=mipsel-linux-android
${NDKPFX}-gcc 2>/dev/null
if [ $? -gt 1 ]; then
NDKPFX=mipsel-linux-android
fi
;;
mips64)
ANDROID_SDK_VERSION=${ANDROID_LOLIPOP}
NDKPFX=mips64el-linux-android
${NDKPFX}-gcc 2>/dev/null
if [ $? -gt 1 ]; then
NDKPFX=mips64el-linux-android
fi
;;
x86)
ANDROID_SDK_VERSION=${ANDROID_KITKAT} #x86
NDKPFX=i686-android-linux
${NDKPFX}-gcc 2>/dev/null
if [ $? -gt 1 ]; then
NDKPFX=i686-linux-android
fi
;;
esac
if [ -d "${PROGDIR}/platforms/android-L/arch-${ARCH}" ]; then
ANDROID_SDK_VERSION=L
fi
PROGDIR=${NDK}
PLATFORM=android-${ANDROID_SDK_VERSION}
PLATFORM_ROOT=${PROGDIR}/platforms/${PLATFORM}/arch-${ARCH2}
PLATFORM_PATH=${PLATFORM_ROOT}/usr
# shellcheck disable=SC2010
NDK_BINS=$(ls "${PROGDIR}"/toolchains/*/prebuilt/"${OS}"*/bin |grep prebuilt | grep "/${ARCH}-" |sort |tail -n 1)
# shellcheck disable=SC2010
[ -z "${NDK_BINS}" ] && \
NDK_BINS=$(ls "${PROGDIR}"/toolchains/*/prebuilt/"${OS}"*/bin |grep prebuilt | grep "/${ARCH}" |sort |tail -n 1)
#echo "ls ${PROGDIR}/toolchains/*/prebuilt/linux*/bin |grep prebuilt | grep "/${ARCH}-" |sort |tail -n 1"
#echo NDKBINS=${NDK_BINS}
if [ -z "${NDK_BINS}" ]; then
echo "Cant find NDK_BINS"
exit 1
fi
CFLAGS="${CFLAGS} --sysroot=${PLATFORM_ROOT}"
CFLAGS="${CFLAGS} -I${PLATFORM_PATH}/include"
CFLAGS="${CFLAGS} -fPIC -fPIE"
case "${ARCH}" in
mips64)
LDFLAGS=-L${PLATFORM_PATH}/lib64
;;
*)
LDFLAGS=-L${PLATFORM_PATH}/lib
;;
esac
#LDFLAGS="${LDFLAGS} -nostdlib"
#LDFLAGS="${LDFLAGS} -lc -ldl"
#LDFLAGS="${LDFLAGS} -Wl,-dynamic-linker=/system/bin/linker"
LDFLAGS="${LDFLAGS} --sysroot=${PLATFORM_ROOT}"
#if [ -z "`echo $*" | grep shared`" ]; then
#LDFLAGS="${LDFLAGS} ${PLATFORM_PATH}/lib/crtbegin_dynamic.o"
#fi
CC="${NDKPFX}-gcc"
AR="${NDKPFX}-ar"
EXT_SO=so
export PATH="${NDK_BINS}:$PATH"
export EXT_SO AR CC CFLAGS LDFLAGS PATH
#shellcheck disable=SC2048
#shellcheck disable=SC2086
echo ${CC} ${LDFLAGS} ${CFLAGS} $*
${CC} ${LDFLAGS} ${CFLAGS} $*