android_build_helper.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2014, Joe Eli McIlvain
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. #
  9. # 1. Redistributions of source code must retain the above copyright notice,
  10. # this list of conditions and the following disclaimer.
  11. #
  12. # 2. Redistributions in binary form must reproduce the above copyright notice,
  13. # this list of conditions and the following disclaimer in the documentation
  14. # and/or other materials provided with the distribution.
  15. #
  16. # 3. Neither the name of the copyright holder nor the names of its
  17. # contributors may be used to endorse or promote products derived from
  18. # this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  24. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  30. # THE POSSIBILITY OF SUCH DAMAGE.
  31. #
  32. ###
  33. #
  34. # https://github.com/jemc/android_build_helper
  35. # android_build_helper.sh
  36. #
  37. # The following is a helper script for setting up android builds for
  38. # "native" libraries maintained with an autotools build system.
  39. # It merely helps to create the proper cross-compile environment.
  40. # It makes no attempt to wrap the library or make it accessible to Java code;
  41. # the intention is to make the bare library available to other "native" code.
  42. #
  43. # To get the latest version of this script, please download from:
  44. # https://github.com/jemc/android_build_helper
  45. #
  46. # You are free to modify this script, but if you add improvements,
  47. # please consider submitting a pull request to the aforementioned upstream
  48. # repository for the benefit of other users.
  49. #
  50. # Get directory of current script (if not already set)
  51. # This directory is also the basis for the build directories the get created.
  52. if [ -z "$ANDROID_BUILD_DIR" ]; then
  53. ANDROID_BUILD_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  54. fi
  55. # Set up a variable to hold the global failure reasons, separated by newlines
  56. # (Empty string indicates no failure)
  57. ANDROID_BUILD_FAIL=()
  58. function android_build_check_fail {
  59. if [ ! ${#ANDROID_BUILD_FAIL[@]} -eq 0 ]; then
  60. echo "Android (${TOOLCHAIN_ARCH}) build failed for the following reasons:"
  61. for reason in "${ANDROID_BUILD_FAIL[@]}"; do
  62. local formatted_reason=" ${reason}"
  63. echo "${formatted_reason}"
  64. done
  65. exit 1
  66. fi
  67. }
  68. function android_build_set_env {
  69. BUILD_ARCH=$1
  70. export TOOLCHAIN_PATH="${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_PLATFORM}/bin"
  71. # Set variables for each architecture
  72. if [ $BUILD_ARCH == "arm" ]; then
  73. export TOOLCHAIN_HOST="arm-linux-androideabi"
  74. export TOOLCHAIN_COMP="armv7a-linux-androideabi${MIN_SDK_VERSION}"
  75. export TOOLCHAIN_ABI="armeabi-v7a"
  76. export TOOLCHAIN_ARCH="arm"
  77. elif [ $BUILD_ARCH == "x86" ]; then
  78. export TOOLCHAIN_HOST="i686-linux-android"
  79. export TOOLCHAIN_COMP="i686-linux-android${MIN_SDK_VERSION}"
  80. export TOOLCHAIN_ABI="x86"
  81. export TOOLCHAIN_ARCH="x86"
  82. elif [ $BUILD_ARCH == "arm64" ]; then
  83. export TOOLCHAIN_HOST="aarch64-linux-android"
  84. export TOOLCHAIN_COMP="aarch64-linux-android${MIN_SDK_VERSION}"
  85. export TOOLCHAIN_ABI="arm64-v8a"
  86. export TOOLCHAIN_ARCH="arm64"
  87. elif [ $BUILD_ARCH == "x86_64" ]; then
  88. export TOOLCHAIN_HOST="x86_64-linux-android"
  89. export TOOLCHAIN_COMP="x86_64-linux-android${MIN_SDK_VERSION}"
  90. export TOOLCHAIN_ABI="x86_64"
  91. export TOOLCHAIN_ARCH="x86_64"
  92. fi
  93. export ANDROID_BUILD_SYSROOT="${ANDROID_NDK_ROOT}/platforms/android-${MIN_SDK_VERSION}/arch-${TOOLCHAIN_ARCH}"
  94. export ANDROID_BUILD_PREFIX="${ANDROID_BUILD_DIR}/prefix/${TOOLCHAIN_ARCH}"
  95. }
  96. function android_build_env {
  97. ##
  98. # Check that necessary environment variables are set
  99. if [ -z "$ANDROID_NDK_ROOT" ]; then
  100. ANDROID_BUILD_FAIL+=("Please set the ANDROID_NDK_ROOT environment variable")
  101. ANDROID_BUILD_FAIL+=(" (eg. \"/home/user/android/android-ndk-r20\")")
  102. fi
  103. if [ -z "$TOOLCHAIN_PATH" ]; then
  104. ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_PATH environment variable")
  105. ANDROID_BUILD_FAIL+=(" (eg. \"/home/user/android/android-ndk-r20/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin\")")
  106. fi
  107. if [ -z "$TOOLCHAIN_HOST" ]; then
  108. ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_HOST environment variable")
  109. ANDROID_BUILD_FAIL+=(" (eg. \"arm-linux-androideabi\")")
  110. fi
  111. if [ -z "$TOOLCHAIN_COMP" ]; then
  112. ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_COMP environment variable")
  113. ANDROID_BUILD_FAIL+=(" (eg. \"armv7a-linux-androideabi\")")
  114. fi
  115. if [ -z "$TOOLCHAIN_ABI" ]; then
  116. ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_ABI environment variable")
  117. ANDROID_BUILD_FAIL+=(" (eg. \"armeabi-v7a\")")
  118. fi
  119. if [ -z "$TOOLCHAIN_ARCH" ]; then
  120. ANDROID_BUILD_FAIL+=("Please set the TOOLCHAIN_ARCH environment variable")
  121. ANDROID_BUILD_FAIL+=(" (eg. \"arm\")")
  122. fi
  123. android_build_check_fail
  124. ##
  125. # Check that directories given by environment variables exist
  126. if [ ! -d "$ANDROID_NDK_ROOT" ]; then
  127. ANDROID_BUILD_FAIL+=("The ANDROID_NDK_ROOT directory does not exist")
  128. ANDROID_BUILD_FAIL+=(" ${ANDROID_NDK_ROOT}")
  129. fi
  130. if [ ! -d "$TOOLCHAIN_PATH" ]; then
  131. ANDROID_BUILD_FAIL+=("The TOOLCHAIN_PATH directory does not exist")
  132. ANDROID_BUILD_FAIL+=(" ${TOOLCHAIN_PATH}")
  133. fi
  134. ##
  135. # Set up some local variables and check them
  136. if [ ! -d "$ANDROID_BUILD_SYSROOT" ]; then
  137. ANDROID_BUILD_FAIL+=("The ANDROID_BUILD_SYSROOT directory does not exist")
  138. ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_SYSROOT}")
  139. fi
  140. mkdir -p "$ANDROID_BUILD_PREFIX" || {
  141. ANDROID_BUILD_FAIL+=("Failed to make ANDROID_BUILD_PREFIX directory")
  142. ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_PREFIX}")
  143. }
  144. android_build_check_fail
  145. }
  146. function _android_build_opts_process_binaries {
  147. local TOOLCHAIN="${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_PLATFORM}"
  148. local CC="${TOOLCHAIN_PATH}/${TOOLCHAIN_COMP}-clang"
  149. local CXX="${TOOLCHAIN_PATH}/${TOOLCHAIN_COMP}-clang++"
  150. local LD="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ld"
  151. local AS="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-as"
  152. local AR="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ar"
  153. local RANLIB="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-ranlib"
  154. local STRIP="${TOOLCHAIN_PATH}/${TOOLCHAIN_HOST}-strip"
  155. if [ ! -x "${CC}" ]; then
  156. ANDROID_BUILD_FAIL+=("The CC binary does not exist or is not executable")
  157. ANDROID_BUILD_FAIL+=(" ${CC}")
  158. fi
  159. if [ ! -x "${CXX}" ]; then
  160. ANDROID_BUILD_FAIL+=("The CXX binary does not exist or is not executable")
  161. ANDROID_BUILD_FAIL+=(" ${CXX}")
  162. fi
  163. if [ ! -x "${LD}" ]; then
  164. ANDROID_BUILD_FAIL+=("The LD binary does not exist or is not executable")
  165. ANDROID_BUILD_FAIL+=(" ${LD}")
  166. fi
  167. if [ ! -x "${AS}" ]; then
  168. ANDROID_BUILD_FAIL+=("The AS binary does not exist or is not executable")
  169. ANDROID_BUILD_FAIL+=(" ${AS}")
  170. fi
  171. if [ ! -x "${AR}" ]; then
  172. ANDROID_BUILD_FAIL+=("The AR binary does not exist or is not executable")
  173. ANDROID_BUILD_FAIL+=(" ${AR}")
  174. fi
  175. if [ ! -x "${RANLIB}" ]; then
  176. ANDROID_BUILD_FAIL+=("The RANLIB binary does not exist or is not executable")
  177. ANDROID_BUILD_FAIL+=(" ${RANLIB}")
  178. fi
  179. if [ ! -x "${STRIP}" ]; then
  180. ANDROID_BUILD_FAIL+=("The STRIP binary does not exist or is not executable")
  181. ANDROID_BUILD_FAIL+=(" ${STRIP}")
  182. fi
  183. ANDROID_BUILD_OPTS+=("TOOLCHAIN=${TOOLCHAIN}")
  184. ANDROID_BUILD_OPTS+=("CC=${CC}")
  185. ANDROID_BUILD_OPTS+=("CXX=${CXX}")
  186. ANDROID_BUILD_OPTS+=("LD=${LD}")
  187. ANDROID_BUILD_OPTS+=("AS=${AS}")
  188. ANDROID_BUILD_OPTS+=("AR=${AR}")
  189. ANDROID_BUILD_OPTS+=("RANLIB=${RANLIB}")
  190. ANDROID_BUILD_OPTS+=("STRIP=${STRIP}")
  191. android_build_check_fail
  192. }
  193. # Set the ANDROID_BUILD_OPTS variable to a bash array of configure options
  194. function android_build_opts {
  195. ANDROID_BUILD_OPTS=()
  196. _android_build_opts_process_binaries
  197. local LIBS="-lc -lgcc -ldl -lm -llog -lc++_shared"
  198. local LDFLAGS="-L${ANDROID_BUILD_PREFIX}/lib"
  199. LDFLAGS+=" -L${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/libs/${TOOLCHAIN_ABI}"
  200. CFLAGS+=" -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE"
  201. CPPFLAGS+=" -I${ANDROID_BUILD_PREFIX}/include"
  202. ANDROID_BUILD_OPTS+=("CFLAGS=${CFLAGS} ${ANDROID_BUILD_EXTRA_CFLAGS}")
  203. ANDROID_BUILD_OPTS+=("CPPFLAGS=${CPPFLAGS} ${ANDROID_BUILD_EXTRA_CPPFLAGS}")
  204. ANDROID_BUILD_OPTS+=("CXXFLAGS=${CXXFLAGS} ${ANDROID_BUILD_EXTRA_CXXFLAGS}")
  205. ANDROID_BUILD_OPTS+=("LDFLAGS=${LDFLAGS} ${ANDROID_BUILD_EXTRA_LDFLAGS}")
  206. ANDROID_BUILD_OPTS+=("LIBS=${LIBS} ${ANDROID_BUILD_EXTRA_LIBS}")
  207. ANDROID_BUILD_OPTS+=("PKG_CONFIG_LIBDIR=${ANDROID_NDK_ROOT}/prebuilt/${HOST_PLATFORM}/lib/pkgconfig")
  208. ANDROID_BUILD_OPTS+=("PKG_CONFIG_PATH=${ANDROID_BUILD_PREFIX}/lib/pkgconfig")
  209. ANDROID_BUILD_OPTS+=("PKG_CONFIG_SYSROOT_DIR=${ANDROID_BUILD_SYSROOT}")
  210. ANDROID_BUILD_OPTS+=("PKG_CONFIG_DIR=")
  211. ANDROID_BUILD_OPTS+=("--with-sysroot=${ANDROID_BUILD_SYSROOT}")
  212. ANDROID_BUILD_OPTS+=("--host=${TOOLCHAIN_HOST}")
  213. ANDROID_BUILD_OPTS+=("--prefix=${ANDROID_BUILD_PREFIX}")
  214. android_build_check_fail
  215. }
  216. # Parse readelf output to verify the correct linking of libraries.
  217. # The first argument should be the soname of the newly built library.
  218. # The rest of the arguments should be the sonames of dependencies.
  219. # All sonames should be unversioned for android (no trailing numbers).
  220. function android_build_verify_so {
  221. local soname="$1"
  222. shift # Get rid of first argument - the rest represent dependencies
  223. local sofile="${ANDROID_BUILD_PREFIX}/lib/${soname}"
  224. if [ ! -f "${sofile}" ]; then
  225. ANDROID_BUILD_FAIL+=("Found no library named ${soname}")
  226. ANDROID_BUILD_FAIL+=(" ${sofile}")
  227. fi
  228. android_build_check_fail
  229. if command -v readelf >/dev/null 2>&1 ; then
  230. local readelf_bin="readelf"
  231. elif command -v greadelf >/dev/null 2>&1 ; then
  232. local readelf_bin="greadelf"
  233. else
  234. ANDROID_BUILD_FAIL+=("Could not find [g]readelf")
  235. fi
  236. android_build_check_fail
  237. local elfoutput=$(LC_ALL=C $readelf_bin -d ${sofile})
  238. local soname_regexp='soname: \[([[:alnum:]\.]+)\]'
  239. if [[ $elfoutput =~ $soname_regexp ]]; then
  240. local parsed_soname="${BASH_REMATCH[1]}"
  241. if [ "${parsed_soname}" != "${soname}" ]; then
  242. ANDROID_BUILD_FAIL+=("Actual soname of library ${soname} is incorrect (or versioned):")
  243. ANDROID_BUILD_FAIL+=(" ${parsed_soname}")
  244. fi
  245. else
  246. ANDROID_BUILD_FAIL+=("Failed to meaningfully parse readelf output for library ${soname}:")
  247. ANDROID_BUILD_FAIL+=(" ${elfoutput}")
  248. fi
  249. for dep_soname do
  250. if [[ $elfoutput != *"library: [${dep_soname}]"* ]]; then
  251. ANDROID_BUILD_FAIL+=("Library ${soname} was expected to be linked to library with soname:")
  252. ANDROID_BUILD_FAIL+=(" ${dep_soname}")
  253. fi
  254. done
  255. android_build_check_fail
  256. }