Debian Codegraph @

Android/android-env.sh

raw
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 : "${ANDROID_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 stdout is captured.
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=27.3.13750724
28
29 ndk=$ANDROID_HOME/ndk/$ndk_version
30 if ! [ -e "$ndk/package.xml" ]; 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}${ANDROID_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="-D__BIONIC_NO_PAGE_SIZE_MACRO"
62 export 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.
68 LDFLAGS="$LDFLAGS -Wl,--no-undefined"
69
70 # Many packages get away with omitting -lm on Linux, but Android is stricter.
71 LDFLAGS="$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.
75 if [ "$HOST" = "arm-linux-androideabi" ]; then
76 CFLAGS="$CFLAGS -march=armv7-a -mthumb"
77 fi
78
79 if [ -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"
86 fi
87
88 # When compiling C++, some build systems will combine CFLAGS and CXXFLAGS, and some will
89 # use CXXFLAGS alone.
90 export CXXFLAGS="$CFLAGS"
91
92 # Use the same variable name as conda-build
93 if [ "$(uname)" = "Darwin" ]; then
94 CPU_COUNT="$(sysctl -n hw.ncpu)"
95 export CPU_COUNT
96 else
97 CPU_COUNT="$(nproc)"
98 export CPU_COUNT
99 fi