1# This script must be sourced with the following variables already set: 2: ${ANDROID_HOME:?} # Path to Android SDK 3: ${HOST:?} # GNU target triplet 4 5# You may also override the following: 6: ${api_level:=21} # Minimum Android API level the build will run on 7: ${PREFIX:-} # Path in which to find required libraries 8 9 10# Print all messages on stderr so they're visible when running within build-wheel. 11log() { 12 echo "$1" >&2 13} 14 15fail() { 16 log "$1" 17 exit 1 18} 19 20# When moving to a new version of the NDK, carefully review the following: 21# 22# * https://developer.android.com/ndk/downloads/revision_history 23# 24# * https://android.googlesource.com/platform/ndk/+/ndk-rXX-release/docs/BuildSystemMaintainers.md 25# where XX is the NDK version. Do a diff against the version you're upgrading from, e.g.: 26# https://android.googlesource.com/platform/ndk/+/ndk-r25-release..ndk-r26-release/docs/BuildSystemMaintainers.md 27ndk_version=27.1.12297006 28 29ndk=$ANDROID_HOME/ndk/$ndk_version 30if ! [ -e $ndk ]; then 31 log "Installing NDK - this may take several minutes" 32 yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "ndk;$ndk_version" 33fi 34 35if [ $HOST = "arm-linux-androideabi" ]; then 36 clang_triplet=armv7a-linux-androideabi 37else 38 clang_triplet=$HOST 39fi 40 41# These variables are based on BuildSystemMaintainers.md above, and 42# $ndk/build/cmake/android.toolchain.cmake. 43toolchain=$(echo $ndk/toolchains/llvm/prebuilt/*) 44export AR="$toolchain/bin/llvm-ar" 45export AS="$toolchain/bin/llvm-as" 46export CC="$toolchain/bin/${clang_triplet}${api_level}-clang" 47export CXX="${CC}++" 48export LD="$toolchain/bin/ld" 49export NM="$toolchain/bin/llvm-nm" 50export RANLIB="$toolchain/bin/llvm-ranlib" 51export READELF="$toolchain/bin/llvm-readelf" 52export STRIP="$toolchain/bin/llvm-strip" 53 54# The quotes make sure the wildcard in the `toolchain` assignment has been expanded. 55for path in "$AR" "$AS" "$CC" "$CXX" "$LD" "$NM" "$RANLIB" "$READELF" "$STRIP"; do 56 if ! [ -e "$path" ]; then 57 fail "$path does not exist" 58 fi 59done 60 61export CFLAGS="-D__BIONIC_NO_PAGE_SIZE_MACRO" 62export LDFLAGS="-Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,-z,max-page-size=16384" 63 64# Unlike Linux, Android does not implicitly use a dlopened library to resolve 65# relocations in subsequently-loaded libraries, even if RTLD_GLOBAL is used 66# (https://github.com/android/ndk/issues/1244). So any library that fails to 67# build with this flag, would also fail to load at runtime. 68LDFLAGS="$LDFLAGS -Wl,--no-undefined" 69 70# Many packages get away with omitting -lm on Linux, but Android is stricter. 71LDFLAGS="$LDFLAGS -lm" 72 73# -mstackrealign is included where necessary in the clang launcher scripts which are 74# pointed to by $CC, so we don't need to include it here. 75if [ $HOST = "arm-linux-androideabi" ]; then 76 CFLAGS="$CFLAGS -march=armv7-a -mthumb" 77fi 78 79if [ -n "${PREFIX:-}" ]; then 80 abs_prefix=$(realpath $PREFIX) 81 CFLAGS="$CFLAGS -I$abs_prefix/include" 82 LDFLAGS="$LDFLAGS -L$abs_prefix/lib" 83 84 export PKG_CONFIG="pkg-config --define-prefix" 85 export PKG_CONFIG_LIBDIR="$abs_prefix/lib/pkgconfig" 86fi 87 88# When compiling C++, some build systems will combine CFLAGS and CXXFLAGS, and some will 89# use CXXFLAGS alone. 90export CXXFLAGS=$CFLAGS 91 92# Use the same variable name as conda-build 93if [ $(uname) = "Darwin" ]; then 94 export CPU_COUNT=$(sysctl -n hw.ncpu) 95else 96 export CPU_COUNT=$(nproc) 97fi 98