configure-android 12 KB

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