setenv-android.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/bin/bash
  2. # Cross-compile environment for Android on ARMv7 and x86
  3. #
  4. # Contents licensed under the terms of the OpenSSL license
  5. # http://www.openssl.org/source/license.html
  6. #
  7. # See http://wiki.openssl.org/index.php/FIPS_Library_and_Android
  8. # and http://wiki.openssl.org/index.php/Android
  9. #####################################################################
  10. # Set ANDROID_NDK_ROOT to you NDK location. For example,
  11. # /opt/android-ndk-r8e or /opt/android-ndk-r9. This can be done in a
  12. # login script. If ANDROID_NDK_ROOT is not specified, the script will
  13. # try to pick it up with the value of _ANDROID_NDK_ROOT below. If
  14. # ANDROID_NDK_ROOT is set, then the value is ignored.
  15. # _ANDROID_NDK="android-ndk-r8e"
  16. _ANDROID_NDK="android-ndk-r10e"
  17. # _ANDROID_NDK="android-ndk-r10"
  18. # Set _ANDROID_EABI to the EABI you want to use. You can find the
  19. # list in $ANDROID_NDK_ROOT/toolchains. This value is always used.
  20. # _ANDROID_EABI="x86-4.6"
  21. # _ANDROID_EABI="arm-linux-androideabi-4.6"
  22. _ANDROID_EABI="arm-linux-androideabi-4.8"
  23. # Set _ANDROID_ARCH to the architecture you are building for.
  24. # This value is always used.
  25. # _ANDROID_ARCH=arch-x86
  26. _ANDROID_ARCH=arch-arm
  27. # Set _ANDROID_API to the API you want to use. You should set it
  28. # to one of: android-14, android-9, android-8, android-14, android-5
  29. # android-4, or android-3. You can't set it to the latest (for
  30. # example, API-17) because the NDK does not supply the platform. At
  31. # Android 5.0, there will likely be another platform added (android-22?).
  32. # This value is always used.
  33. # _ANDROID_API="android-14"
  34. _ANDROID_API="android-14"
  35. # _ANDROID_API="android-19"
  36. #####################################################################
  37. # If the user did not specify the NDK location, try and pick it up.
  38. # We expect something like ANDROID_NDK_ROOT=/opt/android-ndk-r8e
  39. # or ANDROID_NDK_ROOT=/usr/local/android-ndk-r8e.
  40. if [ -z "$ANDROID_NDK_ROOT" ]; then
  41. _ANDROID_NDK_ROOT=""
  42. if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "/usr/local/$_ANDROID_NDK" ]; then
  43. _ANDROID_NDK_ROOT="/usr/local/$_ANDROID_NDK"
  44. fi
  45. if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "/opt/$_ANDROID_NDK" ]; then
  46. _ANDROID_NDK_ROOT="/opt/$_ANDROID_NDK"
  47. fi
  48. if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "$HOME/$_ANDROID_NDK" ]; then
  49. _ANDROID_NDK_ROOT="$HOME/$_ANDROID_NDK"
  50. fi
  51. if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "$PWD/$_ANDROID_NDK" ]; then
  52. _ANDROID_NDK_ROOT="$PWD/$_ANDROID_NDK"
  53. fi
  54. # If a path was set, then export it
  55. if [ ! -z "$_ANDROID_NDK_ROOT" ] && [ -d "$_ANDROID_NDK_ROOT" ]; then
  56. export ANDROID_NDK_ROOT="$_ANDROID_NDK_ROOT"
  57. fi
  58. fi
  59. # Error checking
  60. # ANDROID_NDK_ROOT should always be set by the user (even when not running this script)
  61. # http://groups.google.com/group/android-ndk/browse_thread/thread/a998e139aca71d77
  62. if [ -z "$ANDROID_NDK_ROOT" ] || [ ! -d "$ANDROID_NDK_ROOT" ]; then
  63. echo "Error: ANDROID_NDK_ROOT is not a valid path. Please edit this script."
  64. # echo "$ANDROID_NDK_ROOT"
  65. # exit 1
  66. fi
  67. # Error checking
  68. if [ ! -d "$ANDROID_NDK_ROOT/toolchains" ]; then
  69. echo "Error: ANDROID_NDK_ROOT/toolchains is not a valid path. Please edit this script."
  70. # echo "$ANDROID_NDK_ROOT/toolchains"
  71. # exit 1
  72. fi
  73. # Error checking
  74. if [ ! -d "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI" ]; then
  75. echo "Error: ANDROID_EABI is not a valid path. Please edit this script."
  76. # echo "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI"
  77. # exit 1
  78. fi
  79. #####################################################################
  80. # Based on ANDROID_NDK_ROOT, try and pick up the required toolchain. We expect something like:
  81. # /opt/android-ndk-r83/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin
  82. # Once we locate the toolchain, we add it to the PATH. Note: this is the 'hard way' of
  83. # doing things according to the NDK documentation for Ice Cream Sandwich.
  84. # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html
  85. ANDROID_TOOLCHAIN=""
  86. for host in "linux-x86_64" "linux-x86" "darwin-x86_64" "darwin-x86"
  87. do
  88. if [ -d "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI/prebuilt/$host/bin" ]; then
  89. ANDROID_TOOLCHAIN="$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI/prebuilt/$host/bin"
  90. break
  91. fi
  92. done
  93. # Error checking
  94. if [ -z "$ANDROID_TOOLCHAIN" ] || [ ! -d "$ANDROID_TOOLCHAIN" ]; then
  95. echo "Error: ANDROID_TOOLCHAIN is not valid. Please edit this script."
  96. # echo "$ANDROID_TOOLCHAIN"
  97. # exit 1
  98. fi
  99. case $_ANDROID_ARCH in
  100. arch-arm)
  101. ANDROID_TOOLS="arm-linux-androideabi-gcc arm-linux-androideabi-ranlib arm-linux-androideabi-ld"
  102. ;;
  103. arch-x86)
  104. ANDROID_TOOLS="i686-linux-android-gcc i686-linux-android-ranlib i686-linux-android-ld"
  105. ;;
  106. *)
  107. echo "ERROR ERROR ERROR"
  108. ;;
  109. esac
  110. for tool in $ANDROID_TOOLS
  111. do
  112. # Error checking
  113. if [ ! -e "$ANDROID_TOOLCHAIN/$tool" ]; then
  114. echo "Error: Failed to find $tool. Please edit this script."
  115. # echo "$ANDROID_TOOLCHAIN/$tool"
  116. # exit 1
  117. fi
  118. done
  119. # Only modify/export PATH if ANDROID_TOOLCHAIN good
  120. if [ ! -z "$ANDROID_TOOLCHAIN" ]; then
  121. export ANDROID_TOOLCHAIN="$ANDROID_TOOLCHAIN"
  122. export PATH="$ANDROID_TOOLCHAIN":"$PATH"
  123. fi
  124. #####################################################################
  125. # For the Android SYSROOT. Can be used on the command line with --sysroot
  126. # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html
  127. export ANDROID_SYSROOT="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH"
  128. export SYSROOT="$ANDROID_SYSROOT"
  129. export NDK_SYSROOT="$ANDROID_SYSROOT"
  130. # Error checking
  131. if [ -z "$ANDROID_SYSROOT" ] || [ ! -d "$ANDROID_SYSROOT" ]; then
  132. echo "Error: ANDROID_SYSROOT is not valid. Please edit this script."
  133. # echo "$ANDROID_SYSROOT"
  134. # exit 1
  135. fi
  136. #####################################################################
  137. # If the user did not specify the FIPS_SIG location, try and pick it up
  138. # If the user specified a bad location, then try and pick it up too.
  139. if [ -z "$FIPS_SIG" ] || [ ! -e "$FIPS_SIG" ]; then
  140. # Try and locate it
  141. _FIPS_SIG=""
  142. if [ -d "/usr/local/ssl/$_ANDROID_API" ]; then
  143. _FIPS_SIG=`find "/usr/local/ssl/$_ANDROID_API" -name incore`
  144. fi
  145. if [ ! -e "$_FIPS_SIG" ]; then
  146. _FIPS_SIG=`find $PWD -name incore`
  147. fi
  148. # If a path was set, then export it
  149. if [ ! -z "$_FIPS_SIG" ] && [ -e "$_FIPS_SIG" ]; then
  150. export FIPS_SIG="$_FIPS_SIG"
  151. fi
  152. fi
  153. # Error checking. Its OK to ignore this if you are *not* building for FIPS
  154. if [ -z "$FIPS_SIG" ] || [ ! -e "$FIPS_SIG" ]; then
  155. echo "Error: FIPS_SIG does not specify incore module. Please edit this script."
  156. # echo "$FIPS_SIG"
  157. # exit 1
  158. fi
  159. #####################################################################
  160. # Most of these should be OK (MACHINE, SYSTEM, ARCH). RELEASE is ignored.
  161. export MACHINE=armv7
  162. export RELEASE=2.6.37
  163. export SYSTEM=android
  164. export ARCH=arm
  165. export CROSS_COMPILE="arm-linux-androideabi-"
  166. if [ "$_ANDROID_ARCH" == "arch-x86" ]; then
  167. export MACHINE=i686
  168. export RELEASE=2.6.37
  169. export SYSTEM=android
  170. export ARCH=x86
  171. export CROSS_COMPILE="i686-linux-android-"
  172. fi
  173. # For the Android toolchain
  174. # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html
  175. export ANDROID_SYSROOT="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH"
  176. export SYSROOT="$ANDROID_SYSROOT"
  177. export NDK_SYSROOT="$ANDROID_SYSROOT"
  178. export ANDROID_NDK_SYSROOT="$ANDROID_SYSROOT"
  179. export ANDROID_API="$_ANDROID_API"
  180. # CROSS_COMPILE and ANDROID_DEV are DFW (Don't Fiddle With). Its used by OpenSSL build system.
  181. # export CROSS_COMPILE="arm-linux-androideabi-"
  182. export ANDROID_DEV="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH/usr"
  183. export HOSTCC=gcc
  184. VERBOSE=1
  185. if [ ! -z "$VERBOSE" ] && [ "$VERBOSE" != "0" ]; then
  186. echo "ANDROID_NDK_ROOT: $ANDROID_NDK_ROOT"
  187. echo "ANDROID_ARCH: $_ANDROID_ARCH"
  188. echo "ANDROID_EABI: $_ANDROID_EABI"
  189. echo "ANDROID_API: $ANDROID_API"
  190. echo "ANDROID_SYSROOT: $ANDROID_SYSROOT"
  191. echo "ANDROID_TOOLCHAIN: $ANDROID_TOOLCHAIN"
  192. echo "FIPS_SIG: $FIPS_SIG"
  193. echo "CROSS_COMPILE: $CROSS_COMPILE"
  194. echo "ANDROID_DEV: $ANDROID_DEV"
  195. fi