Improve support for building for Android devices

The Android NDK is rather finicky, so handle it specially. The most important
part here is using --sysroot for CFLAGS and LDFLAGS.
This commit is contained in:
Joshua J. Drake 2015-06-17 12:49:51 -05:00
parent 584d3f4a18
commit cbd0392a6d
2 changed files with 44 additions and 4 deletions

View File

@ -137,8 +137,11 @@ Capstone requires no prerequisite packages, so it is easy to compile & install.
(5) Cross-compile for Android
To cross-compile for Android (smartphone/tablet), Android NDK is required.
NOTE: Only ARM and ARM64 are currently supported.
$ ./make.sh cross-android
$ NDK=/android/android-ndk-r10e ./make.sh cross-android arm
or
$ NDK=/android/android-ndk-r10e ./make.sh cross-android arm64
Resulted files libcapstone.so, libcapstone.a & tests/test* can then
be used on Android devices.

43
make.sh
View File

@ -22,12 +22,50 @@ function build_iOS {
CC="$IOS_CC" CFLAGS="$IOS_CFLAGS" LDFLAGS="$IOS_LDFLAGS" LIBARCHS="$IOS_ARCHS" ${MAKE}
}
# build Android lib for only one supported architecture
function build_android {
if [ -z "$NDK" ]; then
echo "ERROR! Please set \$NDK to point at your Android NDK directory."
exit 1
fi
HOSTOS=$(uname -s | tr 'LD' 'ld')
HOSTARCH=$(uname -m)
TARGARCH="$1"
shift
case "$TARGARCH" in
arm)
[ -n "$APILEVEL" ] || APILEVEL="android-14" # default to ICS
[ -n "$GCCVER" ] || GCCVER="4.8"
CROSS=arm-linux-androideabi-
;;
arm64)
[ -n "$APILEVEL" ] || APILEVEL="android-21" # first with arm64
[ -n "$GCCVER" ] || GCCVER="4.9"
CROSS=aarch64-linux-android-
;;
*)
echo "ERROR! Building for Android on $1 is not currently supported."
exit 1
;;
esac
TOOLCHAIN="$NDK/toolchains/$CROSS$GCCVER/prebuilt/$HOSTOS-$HOSTARCH"
PLATFORM="$NDK/platforms/$APILEVEL/arch-$TARGARCH"
${MAKE} clean
CROSS="$TOOLCHAIN/bin/$CROSS" CFLAGS="--sysroot=$PLATFORM" LDFLAGS="--sysroot=$PLATFORM" ${MAKE} $*
}
function build {
if [ $(uname -s) = Darwin ]; then
export LIBARCHS="i386 x86_64"
fi
${MAKE} clean
if [ ${CC}x != x ]; then
${MAKE} CC=$CC $*
@ -123,8 +161,7 @@ case "$TARGET" in
"cross-win64" ) CROSS=x86_64-w64-mingw32- build $*;;
"cygwin-mingw32" ) CROSS=i686-pc-mingw32- build $*;;
"cygwin-mingw64" ) CROSS=x86_64-w64-mingw32- build $*;;
"cross-android" ) CROSS=arm-linux-androideabi- build $*;;
"cross-android64" ) CROSS=aarch64-linux-gnu- build $*;;
"cross-android" ) build_android $*;;
"clang" ) CC=clang build $*;;
"gcc" ) CC=gcc build $*;;
"ios" ) build_iOS $*;;