|
| 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. |
| 11 | +log() { |
| 12 | + echo "$1" >&2 |
| 13 | +} |
| 14 | + |
| 15 | +fail() { |
| 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 |
| 27 | +ndk_version=26.2.11394342 |
| 28 | + |
| 29 | +ndk=$ANDROID_HOME/ndk/$ndk_version |
| 30 | +if ! [ -e $ndk ]; then |
| 31 | + log "Installing NDK: this may take several minutes" |
| 32 | + yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "ndk;$ndk_version" |
| 33 | +fi |
| 34 | + |
| 35 | +if [ $HOST = "arm-linux-androideabi" ]; then |
| 36 | + clang_triplet=armv7a-linux-androideabi |
| 37 | +else |
| 38 | + clang_triplet=$HOST |
| 39 | +fi |
| 40 | + |
| 41 | +# These variables are based on BuildSystemMaintainers.md above, and |
| 42 | +# $ndk/build/cmake/android.toolchain.cmake. |
| 43 | +toolchain=$(echo $ndk/toolchains/llvm/prebuilt/*) |
| 44 | +export AR="$toolchain/bin/llvm-ar" |
| 45 | +export AS="$toolchain/bin/llvm-as" |
| 46 | +export CC="$toolchain/bin/${clang_triplet}${api_level}-clang" |
| 47 | +export CXX="${CC}++" |
| 48 | +export LD="$toolchain/bin/ld" |
| 49 | +export NM="$toolchain/bin/llvm-nm" |
| 50 | +export RANLIB="$toolchain/bin/llvm-ranlib" |
| 51 | +export READELF="$toolchain/bin/llvm-readelf" |
| 52 | +export STRIP="$toolchain/bin/llvm-strip" |
| 53 | + |
| 54 | +# The quotes make sure the wildcard in the `toolchain` assignment has been expanded. |
| 55 | +for 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 |
| 59 | +done |
| 60 | + |
| 61 | +export CFLAGS="" |
| 62 | +export LDFLAGS="-Wl,--build-id=sha1 -Wl,--no-rosegment" |
| 63 | + |
| 64 | +# Many packages get away with omitting -lm on Linux, but Android is stricter. |
| 65 | +LDFLAGS="$LDFLAGS -lm" |
| 66 | + |
| 67 | +# -mstackrealign is included where necessary in the clang launcher scripts which are |
| 68 | +# pointed to by $CC, so we don't need to include it here. |
| 69 | +if [ $HOST = "arm-linux-androideabi" ]; then |
| 70 | + CFLAGS="$CFLAGS -march=armv7-a -mthumb" |
| 71 | +fi |
| 72 | + |
| 73 | +if [ -n "${PREFIX:-}" ]; then |
| 74 | + abs_prefix=$(realpath $PREFIX) |
| 75 | + CFLAGS="$CFLAGS -I$abs_prefix/include" |
| 76 | + LDFLAGS="$LDFLAGS -L$abs_prefix/lib" |
| 77 | + |
| 78 | + export PKG_CONFIG="pkg-config --define-prefix" |
| 79 | + export PKG_CONFIG_LIBDIR="$abs_prefix/lib/pkgconfig" |
| 80 | +fi |
| 81 | + |
| 82 | +# Use the same variable name as conda-build |
| 83 | +if [ $(uname) = "Darwin" ]; then |
| 84 | + export CPU_COUNT=$(sysctl -n hw.ncpu) |
| 85 | +else |
| 86 | + export CPU_COUNT=$(nproc) |
| 87 | +fi |
0 commit comments