├── .gitignore ├── README.md ├── build-openssl-android.sh └── setenv-android.sh /.gitignore: -------------------------------------------------------------------------------- 1 | openssl-* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # build-openssl-android 2 | A simple bash script for building openssl android. 3 | 4 | ## Setup Android Development on Mac OS X 5 | 6 | * Install Android SDK and NDK 7 | 8 | ``` 9 | brew install android android-ndk ant makedepend 10 | ``` 11 | 12 | * Edit `~/.bashrc` and add: 13 | 14 | ```!bash 15 | # Android SDK 16 | export ANDROID_SDK_ROOT=/usr/local/Cellar/android-sdk/23.0.2 17 | export ANDROID_NDK_ROOT=/usr/local/Cellar/android-ndk/r10b 18 | ``` 19 | 20 | * Run 21 | 22 | ```!bash 23 | source ~/.bashrc 24 | ``` 25 | 26 | ## Usage: `./build-openssl-android.sh ` 27 | * Build openssl-1.0.1j 28 | 29 | ```!bash 30 | ./build-openssl-android.sh 1.0.1j 31 | ``` 32 | 33 | * Build openssl-1.0.2-beta3 34 | 35 | ```!bash 36 | ./build-openssl-android.sh 1.0.2-beta3 37 | ``` 38 | 39 | * Build for Android-L 40 | 41 | ```!bash 42 | # Install Android-L sdk using `android` command first 43 | _ANDROID_API="android-L" ./build-openssl-android.sh 44 | ``` 45 | 46 | ## Reference 47 | * http://wiki.openssl.org/index.php/Android -------------------------------------------------------------------------------- /build-openssl-android.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | OPENSSL_VERSION=${1:-"1.0.1j"} 4 | OPENSSL_TARBALL=openssl-${OPENSSL_VERSION}.tar.gz 5 | OPENSSL_DIR=openssl-${OPENSSL_VERSION} 6 | OPENSSL_BUILD_LOG=openssl-${OPENSSL_VERSION}.log 7 | 8 | # Download setenv_android.sh 9 | if [ ! -e setenv-android.sh ]; then 10 | echo "Downloading setenv_android.sh..." 11 | curl -# -o setenv-android.sh http://wiki.openssl.org/images/7/70/Setenv-android.sh 12 | chmod a+x setenv-android.sh 13 | fi 14 | 15 | # Download openssl source 16 | if [ ! -e ${OPENSSL_TARBALL} ]; then 17 | echo "Downloading openssl-${OPENSSL_VERSION}.tar.gz..." 18 | curl -# -O https://www.openssl.org/source/${OPENSSL_TARBALL} 19 | fi 20 | 21 | # Verify the source file 22 | if [ ! -e ${OPENSSL_TARBALL}.sha1 ]; then 23 | echo -n "Verifying... " 24 | curl -o ${OPENSSL_TARBALL}.sha1 -s https://www.openssl.org/source/${OPENSSL_TARBALL}.sha1 25 | CHECKSUM=`cat ${OPENSSL_TARBALL}.sha1` 26 | ACTUAL=`shasum ${OPENSSL_TARBALL} | awk '{ print \$1 }'` 27 | if [ "x$ACTUAL" == "x$CHECKSUM" ]; then 28 | echo "OK" 29 | else 30 | echo "FAIL" 31 | rm -f ${OPENSSL_TARBALL} 32 | rm -f ${OPENSSL_TARBALL}.sha1 33 | return 1 34 | fi 35 | fi 36 | 37 | # Untar the file 38 | if [ ! -e ${OPENSSL_DIR} ]; then 39 | tar zxf ${OPENSSL_TARBALL} 40 | fi 41 | 42 | # Setup the environment 43 | . ./setenv-android.sh 44 | 45 | # Build 46 | echo "Compiling..." 47 | cd ${OPENSSL_DIR} 48 | perl -pi -e 's/install: all install_docs install_sw/install: install_docs install_sw/g' Makefile.org 49 | ./config shared -no-ssl2 -no-ssl3 -no-comp -no-hw -no-engine --openssldir=/usr/local/ssl/$ANDROID_API > ../${OPENSSL_BUILD_LOG} 50 | make depend >> ../${OPENSSL_BUILD_LOG} 51 | make all >> ../${OPENSSL_BUILD_LOG} 52 | 53 | # Installing 54 | echo "Installing..." 55 | sudo -E make install CC=$ANDROID_TOOLCHAIN/arm-linux-androideabi-gcc RANLIB=$ANDROID_TOOLCHAIN/arm-linux-androideabi-ranlib >> ../${OPENSSL_BUILD_LOG} 56 | -------------------------------------------------------------------------------- /setenv-android.sh: -------------------------------------------------------------------------------- 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 | ##################################################################### 11 | 12 | # Set ANDROID_NDK_ROOT to you NDK location. For example, 13 | # /opt/android-ndk-r8e or /opt/android-ndk-r9. This can be done in a 14 | # login script. If ANDROID_NDK_ROOT is not specified, the script will 15 | # try to pick it up with the value of _ANDROID_NDK_ROOT below. If 16 | # ANDROID_NDK_ROOT is set, then the value is ignored. 17 | # _ANDROID_NDK="android-ndk-r8e" 18 | #_ANDROID_NDK="android-ndk-r9" 19 | # _ANDROID_NDK="android-ndk-r10" 20 | _ANDROID_NDK=${_ANDROID_NDK:-"android-ndk-r10"} 21 | 22 | # Set _ANDROID_EABI to the EABI you want to use. You can find the 23 | # list in $ANDROID_NDK_ROOT/toolchains. This value is always used. 24 | # _ANDROID_EABI="x86-4.6" 25 | # _ANDROID_EABI="arm-linux-androideabi-4.6" 26 | _ANDROID_EABI="arm-linux-androideabi-4.8" 27 | 28 | # Set _ANDROID_ARCH to the architecture you are building for. 29 | # This value is always used. 30 | # _ANDROID_ARCH=arch-x86 31 | _ANDROID_ARCH=arch-arm 32 | 33 | # Set _ANDROID_API to the API you want to use. You should set it 34 | # to one of: android-14, android-9, android-8, android-14, android-5 35 | # android-4, or android-3. You can't set it to the latest (for 36 | # example, API-17) because the NDK does not supply the platform. At 37 | # Android 5.0, there will likely be another platform added (android-22?). 38 | # This value is always used. 39 | # _ANDROID_API="android-14" 40 | _ANDROID_API=${_ANDROID_API:-"android-18"} 41 | # _ANDROID_API="android-19" 42 | 43 | ##################################################################### 44 | 45 | # If the user did not specify the NDK location, try and pick it up. 46 | # We expect something like ANDROID_NDK_ROOT=/opt/android-ndk-r8e 47 | # or ANDROID_NDK_ROOT=/usr/local/android-ndk-r8e. 48 | 49 | if [ -z "$ANDROID_NDK_ROOT" ]; then 50 | 51 | _ANDROID_NDK_ROOT="" 52 | if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "/usr/local/$_ANDROID_NDK" ]; then 53 | _ANDROID_NDK_ROOT="/usr/local/$_ANDROID_NDK" 54 | fi 55 | 56 | if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "/opt/$_ANDROID_NDK" ]; then 57 | _ANDROID_NDK_ROOT="/opt/$_ANDROID_NDK" 58 | fi 59 | 60 | if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "$HOME/$_ANDROID_NDK" ]; then 61 | _ANDROID_NDK_ROOT="$HOME/$_ANDROID_NDK" 62 | fi 63 | 64 | if [ -z "$_ANDROID_NDK_ROOT" ] && [ -d "$PWD/$_ANDROID_NDK" ]; then 65 | _ANDROID_NDK_ROOT="$PWD/$_ANDROID_NDK" 66 | fi 67 | 68 | # If a path was set, then export it 69 | if [ ! -z "$_ANDROID_NDK_ROOT" ] && [ -d "$_ANDROID_NDK_ROOT" ]; then 70 | export ANDROID_NDK_ROOT="$_ANDROID_NDK_ROOT" 71 | fi 72 | fi 73 | 74 | # Error checking 75 | # ANDROID_NDK_ROOT should always be set by the user (even when not running this script) 76 | # http://groups.google.com/group/android-ndk/browse_thread/thread/a998e139aca71d77 77 | if [ -z "$ANDROID_NDK_ROOT" ] || [ ! -d "$ANDROID_NDK_ROOT" ]; then 78 | echo "Error: ANDROID_NDK_ROOT is not a valid path. Please edit this script." 79 | # echo "$ANDROID_NDK_ROOT" 80 | # exit 1 81 | fi 82 | 83 | # Error checking 84 | if [ ! -d "$ANDROID_NDK_ROOT/toolchains" ]; then 85 | echo "Error: ANDROID_NDK_ROOT/toolchains is not a valid path. Please edit this script." 86 | # echo "$ANDROID_NDK_ROOT/toolchains" 87 | # exit 1 88 | fi 89 | 90 | # Error checking 91 | if [ ! -d "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI" ]; then 92 | echo "Error: ANDROID_EABI is not a valid path. Please edit this script." 93 | # echo "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI" 94 | # exit 1 95 | fi 96 | 97 | ##################################################################### 98 | 99 | # Based on ANDROID_NDK_ROOT, try and pick up the required toolchain. We expect something like: 100 | # /opt/android-ndk-r83/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin 101 | # Once we locate the toolchain, we add it to the PATH. Note: this is the 'hard way' of 102 | # doing things according to the NDK documentation for Ice Cream Sandwich. 103 | # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html 104 | 105 | ANDROID_TOOLCHAIN="" 106 | for host in "linux-x86_64" "linux-x86" "darwin-x86_64" "darwin-x86" 107 | do 108 | if [ -d "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI/prebuilt/$host/bin" ]; then 109 | ANDROID_TOOLCHAIN="$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI/prebuilt/$host/bin" 110 | break 111 | fi 112 | done 113 | 114 | # Error checking 115 | if [ -z "$ANDROID_TOOLCHAIN" ] || [ ! -d "$ANDROID_TOOLCHAIN" ]; then 116 | echo "Error: ANDROID_TOOLCHAIN is not valid. Please edit this script." 117 | # echo "$ANDROID_TOOLCHAIN" 118 | # exit 1 119 | fi 120 | 121 | case $_ANDROID_ARCH in 122 | arch-arm) 123 | ANDROID_TOOLS="arm-linux-androideabi-gcc arm-linux-androideabi-ranlib arm-linux-androideabi-ld" 124 | ;; 125 | arch-x86) 126 | ANDROID_TOOLS="i686-linux-android-gcc i686-linux-android-ranlib i686-linux-android-ld" 127 | ;; 128 | *) 129 | echo "ERROR ERROR ERROR" 130 | ;; 131 | esac 132 | 133 | for tool in $ANDROID_TOOLS 134 | do 135 | # Error checking 136 | if [ ! -e "$ANDROID_TOOLCHAIN/$tool" ]; then 137 | echo "Error: Failed to find $tool. Please edit this script." 138 | # echo "$ANDROID_TOOLCHAIN/$tool" 139 | # exit 1 140 | fi 141 | done 142 | 143 | # Only modify/export PATH if ANDROID_TOOLCHAIN good 144 | if [ ! -z "$ANDROID_TOOLCHAIN" ]; then 145 | export ANDROID_TOOLCHAIN="$ANDROID_TOOLCHAIN" 146 | export PATH="$ANDROID_TOOLCHAIN":"$PATH" 147 | fi 148 | 149 | ##################################################################### 150 | 151 | # For the Android SYSROOT. Can be used on the command line with --sysroot 152 | # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html 153 | export ANDROID_SYSROOT="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH" 154 | export SYSROOT="$ANDROID_SYSROOT" 155 | export NDK_SYSROOT="$ANDROID_SYSROOT" 156 | 157 | # Error checking 158 | if [ -z "$ANDROID_SYSROOT" ] || [ ! -d "$ANDROID_SYSROOT" ]; then 159 | echo "Error: ANDROID_SYSROOT is not valid. Please edit this script." 160 | # echo "$ANDROID_SYSROOT" 161 | # exit 1 162 | fi 163 | 164 | ##################################################################### 165 | 166 | # If the user did not specify the FIPS_SIG location, try and pick it up 167 | # If the user specified a bad location, then try and pick it up too. 168 | if [ -z "$FIPS_SIG" ] || [ ! -e "$FIPS_SIG" ]; then 169 | 170 | # Try and locate it 171 | _FIPS_SIG="" 172 | if [ -d "/usr/local/ssl/$_ANDROID_API" ]; then 173 | _FIPS_SIG=`find "/usr/local/ssl/$_ANDROID_API" -name incore` 174 | fi 175 | 176 | if [ ! -e "$_FIPS_SIG" ]; then 177 | _FIPS_SIG=`find $PWD -name incore` 178 | fi 179 | 180 | # If a path was set, then export it 181 | if [ ! -z "$_FIPS_SIG" ] && [ -e "$_FIPS_SIG" ]; then 182 | export FIPS_SIG="$_FIPS_SIG" 183 | fi 184 | fi 185 | 186 | # Error checking. Its OK to ignore this if you are *not* building for FIPS 187 | if [ -z "$FIPS_SIG" ] || [ ! -e "$FIPS_SIG" ]; then 188 | echo "Error: FIPS_SIG does not specify incore module. Please edit this script." 189 | # echo "$FIPS_SIG" 190 | # exit 1 191 | fi 192 | 193 | ##################################################################### 194 | 195 | # Most of these should be OK (MACHINE, SYSTEM, ARCH). RELEASE is ignored. 196 | export MACHINE=armv7 197 | export RELEASE=2.6.37 198 | export SYSTEM=android 199 | export ARCH=arm 200 | export CROSS_COMPILE="arm-linux-androideabi-" 201 | 202 | if [ "$_ANDROID_ARCH" == "arch-x86" ]; then 203 | export MACHINE=i686 204 | export RELEASE=2.6.37 205 | export SYSTEM=android 206 | export ARCH=x86 207 | export CROSS_COMPILE="i686-linux-android-" 208 | fi 209 | 210 | # For the Android toolchain 211 | # https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html 212 | export ANDROID_SYSROOT="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH" 213 | export SYSROOT="$ANDROID_SYSROOT" 214 | export NDK_SYSROOT="$ANDROID_SYSROOT" 215 | export ANDROID_NDK_SYSROOT="$ANDROID_SYSROOT" 216 | export ANDROID_API="$_ANDROID_API" 217 | 218 | # CROSS_COMPILE and ANDROID_DEV are DFW (Don't Fiddle With). Its used by OpenSSL build system. 219 | # export CROSS_COMPILE="arm-linux-androideabi-" 220 | export ANDROID_DEV="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH/usr" 221 | export HOSTCC=gcc 222 | 223 | VERBOSE=1 224 | if [ ! -z "$VERBOSE" ] && [ "$VERBOSE" != "0" ]; then 225 | echo "ANDROID_NDK_ROOT: $ANDROID_NDK_ROOT" 226 | echo "ANDROID_ARCH: $_ANDROID_ARCH" 227 | echo "ANDROID_EABI: $_ANDROID_EABI" 228 | echo "ANDROID_API: $ANDROID_API" 229 | echo "ANDROID_SYSROOT: $ANDROID_SYSROOT" 230 | echo "ANDROID_TOOLCHAIN: $ANDROID_TOOLCHAIN" 231 | echo "FIPS_SIG: $FIPS_SIG" 232 | echo "CROSS_COMPILE: $CROSS_COMPILE" 233 | echo "ANDROID_DEV: $ANDROID_DEV" 234 | fi 235 | --------------------------------------------------------------------------------