Skip to content

Instantly share code, notes, and snippets.

@tanersener
Last active March 3, 2024 15:55
Show Gist options
  • Save tanersener/e4084e2e2428f7fdcf49c39cd5715ef2 to your computer and use it in GitHub Desktop.
Save tanersener/e4084e2e2428f7fdcf49c39cd5715ef2 to your computer and use it in GitHub Desktop.
Cross-compile script for Android. Creates binaries for arm-v7a, arm64-v8a, x86 and x86-64 platforms.
#!/bin/bash
set_arch_specific_variables() {
case ${ARCH} in
arm-v7a)
TOOLCHAIN="arm"
TARGET_HOST="arm-linux-androideabi"
TARGET_ARCH="arm"
ABI="armeabi-v7a"
;;
arm64-v8a)
TOOLCHAIN="aarch64"
TARGET_HOST="aarch64-linux-android"
TARGET_ARCH="arm"
ABI="arm64-v8a"
;;
x86)
TOOLCHAIN="i686"
TARGET_HOST="i686-linux-android"
TARGET_ARCH="x86"
ABI="x86"
;;
x86-64)
TOOLCHAIN="x86_64"
TARGET_HOST="x86_64-linux-android"
TARGET_ARCH="x86_64"
ABI="x86_64"
;;
esac
}
if [[ -z ${ANDROID_NDK_ROOT} ]]; then
echo -e "(*) ANDROID_NDK_ROOT not defined\n"
exit 1
fi
# PREDEFINED VARIABLES
export API=21
export BASEDIR=$(pwd)
for ARCH in "arm-v7a" "arm64-v8a" "x86" "x86-64"
do
# PREPARING FLAGS
LIB_NAME="xvc"
set_arch_specific_variables
TOOLCHAIN_PATH="${ANDROID_NDK_ROOT}/toolchains/xvc-"${TOOLCHAIN}
if [ ! -d ${TOOLCHAIN_PATH} ]; then
case ${ARCH} in
arm-v7a)
${ANDROID_NDK_ROOT}/build/tools/make_standalone_toolchain.py --arch "arm" --api ${API} --stl libc++ --install-dir ${TOOLCHAIN_PATH} || exit 1
;;
arm64-v8a)
${ANDROID_NDK_ROOT}/build/tools/make_standalone_toolchain.py --arch "arm64" --api ${API} --stl libc++ --install-dir ${TOOLCHAIN_PATH} || exit 1
;;
x86)
${ANDROID_NDK_ROOT}/build/tools/make_standalone_toolchain.py --arch "x86" --api ${API} --stl libc++ --install-dir ${TOOLCHAIN_PATH} || exit 1
;;
x86-64)
${ANDROID_NDK_ROOT}/build/tools/make_standalone_toolchain.py --arch "x86_64" --api ${API} --stl libc++ --install-dir ${TOOLCHAIN_PATH} || exit 1
;;
esac
fi
export PATH=$PATH:${TOOLCHAIN_PATH}/bin
AR=${TARGET_HOST}-ar
AS=${TARGET_HOST}-as
CC=${TARGET_HOST}-clang
CXX=${TARGET_HOST}-clang++
LD=${TARGET_HOST}-ld
RANLIB=${TARGET_HOST}-ranlib
STRIP=${TARGET_HOST}-strip
CFLAGS="-fstrict-aliasing -fPIC -DANDROID -D__ANDROID_API__=${API} -I${TOOLCHAIN_PATH}/sysroot/usr/include -I${TOOLCHAIN_PATH}/sysroot/usr/local/include"
CXXFLAGS="-std=c++11 -fno-exceptions"
LDFLAGS="-lc -lm -ldl -llog -lc++ -landroid -L${TOOLCHAIN_PATH}/sysroot/usr/lib -L${TOOLCHAIN_PATH}/lib -L${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/libs/${ABI}"
cd ${BASEDIR} || exit 1
if [ -d "build" ]; then
rm -rf build
fi
mkdir build || exit 1
cd build || exit 1
mkdir -p ${BASEDIR}/android/${TOOLCHAIN}/${LIB_NAME} || exit 1
cmake -Wno-dev \
-DCMAKE_VERBOSE_MAKEFILE=0 \
-DCMAKE_C_FLAGS="${CFLAGS}" \
-DCMAKE_CXX_FLAGS="${CXXFLAGS}" \
-DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS}" \
-DCMAKE_SYSROOT="${TOOLCHAIN_PATH}/sysroot" \
-DCMAKE_FIND_ROOT_PATH="${TOOLCHAIN_PATH}/sysroot" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="${BASEDIR}/android/${TOOLCHAIN}/${LIB_NAME}" \
-DCMAKE_SYSTEM_NAME=Generic \
-DCMAKE_C_COMPILER="${TOOLCHAIN_PATH}/bin/$CC" \
-DCMAKE_CXX_COMPILER="${TOOLCHAIN_PATH}/bin/$CXX" \
-DCMAKE_LINKER="${TOOLCHAIN_PATH}/bin/$LD" \
-DCMAKE_AR="${TOOLCHAIN_PATH}/bin/$AR" \
-DCMAKE_AS="${TOOLCHAIN_PATH}/bin/$AS" \
-DXVC_TARGET_ARCH=${ABI} .. || exit 1
make || exit 1
make install || exit 1
done
@tanersener
Copy link
Author

I created this script long time ago. NDK was allowing creating separate toolchains using make_standalone_toolchain.py script back then. That feature is deprecated now. Therefore, TOOLCHAIN_PATH must be updated to have the llvm toolchain path.

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