| 1 | #!/bin/sh |
| 2 | # |
| 3 | |
| 4 | F="configure-android" |
| 5 | IS_USING_LLVM="0" |
| 6 | |
| 7 | if test "$*" = "--help" -o "$*" = "-h"; then |
| 8 | echo "$F [--use-ndk-cflags] [OPTIONS]" |
| 9 | echo "" |
| 10 | echo "where:" |
| 11 | echo " --use-ndk-cflags Optional parameter to use the same compilation flags" |
| 12 | echo " as the one used by ndk-build. Android NDK r9 or later" |
| 13 | echo " is required when using this option." |
| 14 | echo " OPTIONS Other options that will be passed directly to" |
| 15 | echo " ./aconfigure script. Run ./aconfigure --help" |
| 16 | echo " for more info." |
| 17 | echo "" |
| 18 | echo "Environment variables:" |
| 19 | echo " ANDROID_NDK_ROOT Specify the directory of Android NDK to use." |
| 20 | echo " APP_PLATFORM Optionally specify the platform level used, e.g." |
| 21 | echo " android-9. By default, configure will use the :" |
| 22 | echo " - maximum platform level detected if ndk < r21" |
| 23 | echo " - minimum platform level detected if ndk >= r21" |
| 24 | echo " (will use 23 if minimum platform level < 23)" |
| 25 | echo " TARGET_ABI Optionally specify a single target architecture," |
| 26 | echo " e.g. armeabi-v7a, arm64-v8a, mips, x86. By default, " |
| 27 | echo " the target architecture is arm64-v8a." |
| 28 | echo " IGNORE_CFLAGS Optionally specify compilation flags to be ignored." |
| 29 | echo " Each grepped flag that satisfies the criteria will" |
| 30 | echo " be ignored. Default:" |
| 31 | echo " IGNORE_CFLAGS=\"\-M\|\-f*stack\|\-f*alias\|\-\<g\>\"" |
| 32 | echo " Only used when --use-ndk-cflags is specified." |
| 33 | echo "" |
| 34 | exit 0 |
| 35 | fi |
| 36 | |
| 37 | if test "x${ANDROID_NDK_ROOT}" = "x" || test ! -e ${ANDROID_NDK_ROOT}; then |
| 38 | echo "$F error: ANDROID_NDK_ROOT env var is not specified or invalid" |
| 39 | exit 0 |
| 40 | fi |
| 41 | ENABLE_WEAK_SYMBOLS="-D__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__ -Werror=unguarded-availability" |
| 42 | |
| 43 | # The BRE version somehow does not work well on MacOSX |
| 44 | #NDK_VER=`sed -n -e 's/.*Pkg.Revision *= *\([0-9]\+\).*/\1/p' ${ANDROID_NDK_ROOT}/source.properties` |
| 45 | NDK_VER=`sed -n -E 's/^Pkg.Revision *= *([0-9]+).*/\1/p' ${ANDROID_NDK_ROOT}/source.properties` |
| 46 | |
| 47 | if test "x$APP_PLATFORM" = "x"; then |
| 48 | if test -d ${ANDROID_NDK_ROOT}/platforms; then |
| 49 | APP_PLATFORM=`ls ${ANDROID_NDK_ROOT}/platforms/ | sed 's/android-//' | sort -gr | head -1` |
| 50 | elif test -f ${ANDROID_NDK_ROOT}/meta/platforms.json; then |
| 51 | if ! [ -x "$(command -v jq)" ]; then |
| 52 | APP_PLATFORM=`cat ${ANDROID_NDK_ROOT}/meta/platforms.json | grep -o '"min":[[:space:]]*[[:digit:]]*' | head -1 | grep -o '[[:digit:]]*' ` |
| 53 | else |
| 54 | APP_PLATFORM=`cat ${ANDROID_NDK_ROOT}/meta/platforms.json | jq -r ".min"` |
| 55 | fi |
| 56 | fi |
| 57 | if test "x$APP_PLATFORM" != "x"; then |
| 58 | if [ "$APP_PLATFORM" -lt "23" ]; then |
| 59 | APP_PLATFORM="23" |
| 60 | fi |
| 61 | APP_PLATFORM="android-${APP_PLATFORM}" |
| 62 | else |
| 63 | APP_PLATFORM="latest" |
| 64 | fi |
| 65 | echo "$F: APP_PLATFORM not specified, using ${APP_PLATFORM}" |
| 66 | fi |
| 67 | |
| 68 | if test "x$TARGET_ABI" = "x"; then |
| 69 | TARGET_ABI="arm64-v8a" |
| 70 | echo "$F: TARGET_ABI not specified, using ${TARGET_ABI}" |
| 71 | fi |
| 72 | |
| 73 | if test "$TARGET_ABI" = "x86_64" || test "$TARGET_ABI" = "mips64"; then |
| 74 | USR_LIB="/usr/lib64" |
| 75 | else |
| 76 | USR_LIB="/usr/lib" |
| 77 | fi |
| 78 | |
| 79 | |
| 80 | if test "$1" = "--use-ndk-cflags" || [ "${NDK_VER}" -ge "17" ]; then |
| 81 | if test "$1" = "--use-ndk-cflags"; then |
| 82 | shift # don't pass this param to main configure script |
| 83 | fi |
| 84 | |
| 85 | ADD_CFLAGS="0" |
| 86 | ADD_CXXFLAGS="0" |
| 87 | ADD_LDFLAGS="0" |
| 88 | ADD_NDK_TOOLCHAIN="0" |
| 89 | ADD_NDK_TARGET="0" |
| 90 | if test "x${IGNORE_CFLAGS}" = "x"; then |
| 91 | IGNORE_CFLAGS="\-M\|\-f*stack\|\-f*alias\|\-\<g\>\|\-DNDEBUG\|\-O" |
| 92 | fi |
| 93 | if test "x${IGNORE_CPPFLAGS}" = "x"; then |
| 94 | IGNORE_CPPFLAGS="\-M\|\-f*stack\|\-f*alias\|\-\<g\>\|\-DNDEBUG\|\-O\|\-std\=" |
| 95 | fi |
| 96 | |
| 97 | if test -f ${ANDROID_NDK_ROOT}/build/ndk-build; then |
| 98 | NDK_BUILD=${ANDROID_NDK_ROOT}/build/ndk-build |
| 99 | else |
| 100 | NDK_BUILD=${ANDROID_NDK_ROOT}/ndk-build |
| 101 | fi |
| 102 | |
| 103 | NDK_OUT=`${NDK_BUILD} -n -C pjsip-apps/src/samples/android_sample APP_PLATFORM=${APP_PLATFORM} APP_ABI=${TARGET_ABI}` |
| 104 | if test ! "${NDK_OUT}"; then |
| 105 | echo "$F error: failed to run ndk-build, check ANDROID_NDK_ROOT env var" |
| 106 | exit 1 |
| 107 | fi |
| 108 | |
| 109 | #echo "=====" |
| 110 | #echo "NDK_OUT : ${NDK_OUT}" |
| 111 | #echo "=====" |
| 112 | |
| 113 | for i in $NDK_OUT; do |
| 114 | |
| 115 | # Parse NDK CXXFLAGS |
| 116 | if test "${ADD_CXXFLAGS}" = "1"; then |
| 117 | if test "$i" = "-o"; then |
| 118 | ADD_CXXFLAGS="0" |
| 119 | continue |
| 120 | fi |
| 121 | if test "$i" = "-c"; then |
| 122 | continue |
| 123 | fi |
| 124 | if test "x`echo $i|grep 'dummy'`" != "x"; then |
| 125 | continue |
| 126 | fi |
| 127 | if test "x`echo $i|grep ${IGNORE_CPPFLAGS}`" != "x"; then |
| 128 | continue |
| 129 | fi |
| 130 | # Check if flag is already in CFLAGS |
| 131 | ISDUP="0" |
| 132 | for j in $NDK_CFLAGS; do |
| 133 | if test "$i" = "$j"; then |
| 134 | ISDUP="1" |
| 135 | break |
| 136 | fi |
| 137 | done |
| 138 | if test "${ISDUP}" = "1"; then |
| 139 | continue |
| 140 | fi |
| 141 | NDK_CXXFLAGS="${NDK_CXXFLAGS} $i" |
| 142 | continue |
| 143 | fi |
| 144 | |
| 145 | # Parse NDK CFLAGS |
| 146 | if test "${ADD_CFLAGS}" = "1"; then |
| 147 | if test "$i" = "-c"; then |
| 148 | ADD_CFLAGS="0" |
| 149 | continue |
| 150 | fi |
| 151 | if test "x`echo $i|grep 'dummy'`" != "x"; then |
| 152 | continue |
| 153 | fi |
| 154 | if test "x`echo $i|grep ${IGNORE_CFLAGS}`" = "x"; then |
| 155 | if test "${ADD_NDK_TOOLCHAIN}" = "0" -a "x`echo $i|grep '\-gcc-toolchain'`" != "x"; then |
| 156 | ADD_NDK_TOOLCHAIN="1" |
| 157 | elif test "${ADD_NDK_TARGET}" = "0" -a "x`echo $i|grep '\-target'`" != "x"; then |
| 158 | ADD_NDK_TARGET="1" |
| 159 | elif test "${ADD_NDK_TOOLCHAIN}" = "1"; then |
| 160 | NDK_TOOLCHAIN="$i" |
| 161 | ADD_NDK_TOOLCHAIN="2" |
| 162 | elif test "${ADD_NDK_TARGET}" = "1"; then |
| 163 | NDK_TARGET="$i" |
| 164 | ADD_NDK_TARGET="2" |
| 165 | fi |
| 166 | NDK_CFLAGS="${NDK_CFLAGS} $i" |
| 167 | fi |
| 168 | continue |
| 169 | fi |
| 170 | |
| 171 | # Parse NDK LDFLAGS |
| 172 | if test "${ADD_LDFLAGS}" = "1"; then |
| 173 | if test "$i" = "-o"; then |
| 174 | ADD_LDFLAGS="0" |
| 175 | continue |
| 176 | fi |
| 177 | if test "x`echo $i|grep 'dummy'`" != "x"; then |
| 178 | continue |
| 179 | fi |
| 180 | if test "x`echo $i|grep '.so'`" != "x"; then |
| 181 | continue |
| 182 | fi |
| 183 | NDK_LDFLAGS="${NDK_LDFLAGS} $i" |
| 184 | continue |
| 185 | fi |
| 186 | |
| 187 | # Find gcc or clang |
| 188 | if test "x${NDK_CC}" = "x"; then |
| 189 | if test "x`echo $i | grep 'gcc'`" != "x" -o "x`echo $i | grep 'clang'`" != "x"; then |
| 190 | NDK_CC=$i |
| 191 | ADD_CFLAGS="1" |
| 192 | if test "x`echo ${NDK_CC} | grep 'clang'`" != "x"; then |
| 193 | IS_USING_LLVM="1" |
| 194 | #echo "---using llvm" |
| 195 | fi |
| 196 | continue |
| 197 | fi |
| 198 | fi |
| 199 | |
| 200 | # Find g++ or clang++ |
| 201 | if test "x${NDK_CXX}" = "x"; then |
| 202 | if test "x`echo $i | grep 'g++'`" != "x"; then |
| 203 | NDK_CXX=$i |
| 204 | ADD_CXXFLAGS="1" |
| 205 | continue |
| 206 | fi |
| 207 | fi |
| 208 | |
| 209 | # Find linking/LDFLAGS |
| 210 | if test "x${NDK_LDFLAGS}" = "x"; then |
| 211 | if test "x`echo $i | grep '\-\<shared\>'`" != "x"; then |
| 212 | ADD_LDFLAGS="1" |
| 213 | continue |
| 214 | fi |
| 215 | fi |
| 216 | |
| 217 | # Find ar tool |
| 218 | if test "x${NDK_AR}" = "x" -a "x${NDK_CC}" != "x" -a "x`echo $i|grep '\-\<ar\>'`" != "x"; then |
| 219 | # In some NDKs, e.g: r17c, gcc/clang and ar have different path |
| 220 | #if test "$(dirname \"${NDK_CC}\")" = "$(dirname \"${i}\")"; then |
| 221 | NDK_AR=$i |
| 222 | #echo "--- found AR=${NDK_AR}" |
| 223 | continue |
| 224 | #fi |
| 225 | fi |
| 226 | |
| 227 | # Find ranlib tool |
| 228 | if test "x${NDK_RANLIB}" = "x" -a "x${NDK_CC}" != "x" -a "x`echo $i|grep '\-\<ranlib\>'`" != "x"; then |
| 229 | #if test "$(dirname \"${NDK_CC}\")" = "$(dirname \"${i}\")"; then |
| 230 | NDK_RANLIB=$i |
| 231 | #echo "--- found RANLIB=${NDK_RANLIB}" |
| 232 | #fi |
| 233 | fi |
| 234 | |
| 235 | # Get STD C++ lib path |
| 236 | if test "x${STD_CPP_LIB}" = "x"; then |
| 237 | if test "x`echo $i | grep 'libc++_shared.so'`" != "x"; then |
| 238 | MY_CONF_TMP=`echo ${i} | sed -e 's/\\\/\\//g' | sed -e 's/\\"//g'` |
| 239 | if test -e $MY_CONF_TMP; then |
| 240 | STD_CPP_LIB=$MY_CONF_TMP |
| 241 | #else |
| 242 | #echo "--- not good path for libc++_shared.so: ${MY_CONF_TMP}" |
| 243 | fi |
| 244 | fi |
| 245 | fi |
| 246 | done |
| 247 | |
| 248 | # Get target host from NDK toolchain dir name |
| 249 | TARGET_HOST=`echo ${NDK_CC} | sed -e 's/.*\/toolchains\/\([^\/]*\).*/\1/'` |
| 250 | |
| 251 | # Remove version number suffix (otherwise config.sub will return error, perhaps it just doesn't like the format) |
| 252 | TARGET_HOST=`echo ${TARGET_HOST} | sed -e 's/\-[0-9\.]*$//'` |
| 253 | |
| 254 | # Get target from '-target' param when TARGET_HOST is 'llvm' |
| 255 | if test "$TARGET_HOST" = "llvm"; then |
| 256 | TARGET_HOST=`echo ${NDK_CFLAGS} | sed -e 's/.*-target \([^- ]*\).*/\1/'` |
| 257 | fi |
| 258 | |
| 259 | # Make sure target host string has 'linux-android' in it |
| 260 | if test "x`echo ${TARGET_HOST} | grep 'linux-android'`" = "x"; then |
| 261 | #TARGET_HOST=`echo ${TARGET_HOST} | sed -e 's/\(.*\)\-\([0-9\.]*\)/\1-linux-android-\2/'` |
| 262 | TARGET_HOST="${TARGET_HOST}-linux-android" |
| 263 | fi |
| 264 | |
| 265 | # Set the binutils |
| 266 | if test "x${NDK_TOOLCHAIN}" = "x"; then |
| 267 | if test "x${NDK_AR}" = "x"; then |
| 268 | NDK_CC_DIR=$(dirname "${NDK_CC}") |
| 269 | NDK_AR=`find ${NDK_CC_DIR} -name "*ar" | grep -m 1 -v "gcc"` |
| 270 | fi |
| 271 | export LDFLAGS="${LDFLAGS}" |
| 272 | else |
| 273 | # find ar and ranlib |
| 274 | if test "x${NDK_AR}" = "x"; then |
| 275 | NDK_AR=`find ${NDK_TOOLCHAIN}/bin/ -name "*-ar" | grep -m 1 -v "gcc"` |
| 276 | fi |
| 277 | if test "x${NDK_RANLIB}" = "x"; then |
| 278 | NDK_RANLIB=`find ${NDK_TOOLCHAIN}/bin/ -name "*-ranlib" | grep -m 1 -v "gcc"` |
| 279 | fi |
| 280 | export LDFLAGS="${LDFLAGS} -target ${NDK_TARGET} -gcc-toolchain ${NDK_TOOLCHAIN}" |
| 281 | fi |
| 282 | |
| 283 | if test "x${NDK_RANLIB}" = "x"; then |
| 284 | NDK_RANLIB="${NDK_AR} s" |
| 285 | fi |
| 286 | |
| 287 | TC_AR=${NDK_AR} |
| 288 | TC_RANLIB=${NDK_RANLIB} |
| 289 | if test "x${TC_AR}" != "x" -a "x${TC_RANLIB}" != "x"; then |
| 290 | export AR="${TC_AR}" |
| 291 | export RANLIB="${TC_RANLIB}" |
| 292 | fi |
| 293 | |
| 294 | export TARGET_ABI="${TARGET_ABI}" |
| 295 | export CC="${NDK_CC}" |
| 296 | export CXX="${NDK_CXX}" |
| 297 | |
| 298 | export CPPFLAGS="${CPPFLAGS}" |
| 299 | export CFLAGS="${NDK_CFLAGS} ${CFLAGS} ${CPPFLAGS} ${ENABLE_WEAK_SYMBOLS}" |
| 300 | export CXXFLAGS="${NDK_CXXFLAGS} ${CPPFLAGS}" |
| 301 | export LDFLAGS="${NDK_LDFLAGS} ${LDFLAGS}" |
| 302 | export LIBS="${LIBS}" |
| 303 | |
| 304 | else |
| 305 | |
| 306 | if test "$TARGET_ABI" != "armeabi"; then |
| 307 | echo "$F error: For targets other than 'armeabi', specify --use-ndk-cflags" |
| 308 | exit 1 |
| 309 | fi |
| 310 | |
| 311 | TARGET_HOST="arm-linux-androideabi" |
| 312 | |
| 313 | ANDROID_TC_VER=`ls -d ${ANDROID_NDK_ROOT}/toolchains/${TARGET_HOST}-* | sed 's/clang/0/' | sort -gr | head -1` |
| 314 | ANDROID_TC=`ls -d ${ANDROID_TC_VER}/prebuilt/* | grep -v gdbserver | head -1` |
| 315 | if test ! -d ${ANDROID_TC}; then |
| 316 | echo "$F error: unable to find directory ${ANDROID_TC} in Android NDK" |
| 317 | exit 1 |
| 318 | fi |
| 319 | |
| 320 | export ANDROID_SYSROOT="${ANDROID_NDK_ROOT}/platforms/${APP_PLATFORM}/arch-arm" |
| 321 | if test ! -d ${ANDROID_SYSROOT}; then |
| 322 | echo "$F error: unable to find sysroot dir ${ANDROID_SYSROOT} in Android NDK" |
| 323 | exit 1 |
| 324 | fi |
| 325 | |
| 326 | export TARGET_ABI="${TARGET_ABI}" |
| 327 | export CC="${ANDROID_TC}/bin/${TARGET_HOST}-gcc" |
| 328 | export CXX="${ANDROID_TC}/bin/${TARGET_HOST}-g++" |
| 329 | export AR="${ANDROID_TC}/bin/${TARGET_HOST}-ar" |
| 330 | export RANLIB="${ANDROID_TC}/bin/${TARGET_HOST}-ranlib" |
| 331 | |
| 332 | export LDFLAGS="${LDFLAGS} --sysroot=${ANDROID_SYSROOT}" |
| 333 | export LIBS="${LIBS} -lc -lgcc" |
| 334 | export CFLAGS="${CFLAGS} --sysroot=${ANDROID_SYSROOT}" |
| 335 | export CPPFLAGS="${CFLAGS} -fexceptions -frtti" |
| 336 | export CXXFLAGS="${CXXFLAGS} -shared --sysroot=${ANDROID_SYSROOT} -fexceptions -frtti" |
| 337 | |
| 338 | fi |
| 339 | |
| 340 | if test "x${CC}" = "x" || test ! -e ${CC}; then |
| 341 | echo "$F error: compiler not found, please check environment settings (TARGET_ABI, etc)" |
| 342 | exit 1 |
| 343 | fi |
| 344 | |
| 345 | # C++ STL |
| 346 | # Note: STL for pjsua2 sample app is specified in pjsip-apps/src/swig/java/android/jni/Application.mk |
| 347 | |
| 348 | if test "${IS_USING_LLVM}" = "1"; then |
| 349 | # llvm |
| 350 | STDCPP_TC="${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++" |
| 351 | #STDCPP_CFLAGS="-I${STDCPP_TC}/include" |
| 352 | #STDCPP_LIBS="-lc++_static -lc++abi" |
| 353 | #STDCPP_LIBS="${STDCPP_TC}/libs/${TARGET_ABI}/libc++_shared.so" |
| 354 | STDCPP_LIBS="${STD_CPP_LIB}" |
| 355 | #STDCPP_LDFLAGS="-L${STDCPP_TC}/libs/${TARGET_ABI}/" |
| 356 | else |
| 357 | # gnustl |
| 358 | STDCPP_TC_VER=`ls -d ${ANDROID_NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/[0-9]* | sort -gr | head -1` |
| 359 | STDCPP_CFLAGS="-I${STDCPP_TC_VER}/include -I${STDCPP_TC_VER}/libs/${TARGET_ABI}/include" |
| 360 | STDCPP_LIBS="-lgnustl_static" |
| 361 | STDCPP_LDFLAGS="-L${STDCPP_TC_VER}/libs/${TARGET_ABI}/" |
| 362 | fi |
| 363 | |
| 364 | if test "x$AR_FLAGS" = "x"; then |
| 365 | AR_FLAGS="rvc" |
| 366 | fi |
| 367 | |
| 368 | # stlport |
| 369 | #STDCPP_CFLAGS="-I${ANDROID_NDK_ROOT}/sources/cxx-stl/stlport/stlport" |
| 370 | #STDCPP_LIBS="-lstlport_static -ldl" |
| 371 | #STDCPP_LDFLAGS="-L${ANDROID_NDK_ROOT}/sources/cxx-stl/stlport/libs/${TARGET_ABI}" |
| 372 | |
| 373 | export CFLAGS="${CFLAGS}" |
| 374 | export LIBS="${LIBS} ${STDCPP_LIBS}" |
| 375 | export LDFLAGS="${LDFLAGS} ${STDCPP_LDFLAGS}" |
| 376 | export CPPFLAGS="${CPPFLAGS}" |
| 377 | export CXXFLAGS="${CXXFLAGS} ${STDCPP_CFLAGS}" |
| 378 | export STD_CPP_LIB="${STD_CPP_LIB}" |
| 379 | export AR_FLAGS="${AR_FLAGS}" |
| 380 | |
| 381 | # Print settings |
| 382 | if test "1" = "1"; then |
| 383 | echo "$F: calling ./configure with env vars:" |
| 384 | echo " NDK_TOOLCHAIN = ${NDK_TOOLCHAIN}" |
| 385 | echo " CC = ${CC}" |
| 386 | echo " CXX = ${CXX}" |
| 387 | echo " CFLAGS = ${CFLAGS}" |
| 388 | echo " CXXFLAGS = ${CXXFLAGS}" |
| 389 | echo " STD_CPP_LIB = ${STD_CPP_LIB}" |
| 390 | echo " LDFLAGS = ${LDFLAGS}" |
| 391 | echo " LIBS = ${LIBS}" |
| 392 | echo " AR = ${AR}" |
| 393 | echo " AR_FLAGS = ${AR_FLAGS}" |
| 394 | echo " RANLIB = ${RANLIB}" |
| 395 | echo " TARGET_HOST = ${TARGET_HOST}" |
| 396 | echo " TARGET_ABI = ${TARGET_ABI}" |
| 397 | fi |
| 398 | |
| 399 | ./configure --host=${TARGET_HOST} $* |