Skip to content

Instantly share code, notes, and snippets.

@klynch
Created December 31, 2013 04:42
Show Gist options
  • Save klynch/8192710 to your computer and use it in GitHub Desktop.
Save klynch/8192710 to your computer and use it in GitHub Desktop.
This script runs a configure script with the Android NDK toolchain You may need to adjust the COMPILE_TARGET and ANDROID_API variables depending on your requirements.
#!/bin/sh
# This script runs a configure script with the Android NDK toolchain
# You may need to adjust the COMPILE_TARGET and ANDROID_API variables
# depending on your requirements.
#
# Call this script in a directory with a valid configure script.
# Example: PREFIX=${PWD}/bin android-configure.sh
# Set the ANDROID_NDK variable to the root
# Example: export ANDROID_NDK=${HOME}/.local/android-ndk-r9b
if [ -z ${ANDROID_NDK} ]; then
echo "Please set ANDROID_NDK environment variable to the root directory of the Android NDK"
exit 1
fi
# This is just an empty directory where I want the built objects to be installed
if [ -z ${PREFIX} ]; then
echo "Please set PREFIX environment variable to the output directory"
exit 1
fi
export COMPILE_TARGET=arm-linux-androideabi
export ANDROID_API=16
export ANDROID_PREFIX=${ANDROID_NDK}/toolchains/${COMPILE_TARGET}-4.8/prebuilt/linux-x86_64
export SYSROOT=${ANDROID_NDK}/platforms/android-${ANDROID_API}/arch-arm
export TOOLCHAIN_PATH=${ANDROID_PREFIX}/bin
# These are the toolchain utilities
export CPP=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-cpp
export AR=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-ar
export AS=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-as
export NM=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-nm
export CC=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-gcc
export CXX=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-g++
export LD=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-ld
export RANLIB=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-ranlib
export STRIP=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-strip
export OBJDUMP=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-objdump
export OBJCOPY=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-objcopy
export ADDR2LINE=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-addr2line
export READELF=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-readelf
export SIZE=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-size
export STRINGS=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-strings
export ELFEDIT=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-elfedit
export GCOV=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-gcov
export GDB=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-gdb
export GPROF=${TOOLCHAIN_PATH}/${COMPILE_TARGET}-gprof
# Don't mix up .pc files from your host and build target
export PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig
export CFLAGS="${CFLAGS} --sysroot=${SYSROOT} -I${SYSROOT}/usr/include -I${ANDROID_PREFIX}/include"
export CPPFLAGS="${CFLAGS}"
export LDFLAGS="${LDFLAGS} -L${SYSROOT}/usr/lib -L${ANDROID_PREFIX}/lib"
./configure --host=${COMPILE_TARGET} --prefix=${PREFIX} "$@"
@electrolobzik
Copy link

@klynch thank you for the script! It helped me a lot. But I found one bug: for x86 compile target toolchain path should be different. For example gcc has name i686-linux-android-gcc. So I changed your script to have separate option for toolchain path prefix:

#!/bin/sh

# This script runs a configure script with the Android NDK toolchain
# You may need to adjust the COMPILE_TARGET and ANDROID_API variables
# depending on your requirements.
#
# Call this script in a directory with a valid configure script.
# Example: PREFIX=${PWD}/bin android-configure.sh 

# Set the ANDROID_NDK variable to the root
# Example: export ANDROID_NDK=${HOME}/.local/android-ndk-r9b

if [ -z ${ANDROID_NDK} ]; then
  echo "Please set ANDROID_NDK environment variable to the root directory of the Android NDK"
  exit 1
fi

# This is just an empty directory where I want the built objects to be installed
if [ -z ${PREFIX} ]; then
  echo "Please set PREFIX environment variable to the output directory"
  exit 1
fi

export COMPILE_TARGET=x86
export TOOLCHAIN_PREFIX=i686-linux-android
export ANDROID_API=14


export ANDROID_PREFIX=${ANDROID_NDK}/toolchains/${COMPILE_TARGET}-4.9/prebuilt/darwin-x86_64
export SYSROOT=${ANDROID_NDK}/platforms/android-${ANDROID_API}/arch-x86


export TOOLCHAIN_PATH=${ANDROID_PREFIX}/bin

# These are the toolchain utilities
export CPP=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-cpp
export AR=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-ar
export AS=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-as
export NM=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-nm
export CC=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-gcc
export CXX=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-g++
export LD=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-ld
export RANLIB=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-ranlib
export STRIP=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-strip
export OBJDUMP=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-objdump
export OBJCOPY=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-objcopy
export ADDR2LINE=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-addr2line
export READELF=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-readelf
export SIZE=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-size
export STRINGS=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-strings
export ELFEDIT=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-elfedit
export GCOV=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-gcov
export GDB=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-gdb
export GPROF=${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-gprof

# Don't mix up .pc files from your host and build target
export PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig

export CFLAGS="${CFLAGS} --sysroot=${SYSROOT} -I${SYSROOT}/usr/include -I${ANDROID_PREFIX}/include"
export CPPFLAGS="${CFLAGS}"
export LDFLAGS="${LDFLAGS} -L${SYSROOT}/usr/lib -L${ANDROID_PREFIX}/lib"

./configure --host=${COMPILE_TARGET} --prefix=${PREFIX} "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment