Created
February 14, 2017 04:04
-
-
Save willyliu/607d86372a561cd11041d4a43b62aaf7 to your computer and use it in GitHub Desktop.
Build openssl for android, test with openssl-1.1.0c. Based on https://gist.github.com/cogwirrel/4a733b4138d1b35d4cfe69b4df110d0b.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
USAGE=$'Usage: | |
./android_openssl_builder <options> | |
Required: | |
-s <openssl_directory> The top level openssl directory | |
-n <ndk_directory> The top level ndk directory | |
-o <output_directory> The directory in which you would like the builds | |
Optional: | |
-h Help | |
-p <platform> Android platform (default: android-24) | |
-t <toolchain_install_dir> The directory in which to install the toolchains (default: /tmp/android_openssl_builder/toolhain) | |
-v <toolchain_version> The toolchain version (default: 4.9) | |
' | |
# Architectures and Toolchains | |
TOOLCHAIN_VERSION=4.9 | |
PLATFORM=android-24 | |
TOOLCHAIN_INSTALL_DIR=/tmp/android_openssl_builder/toolchain | |
OPENSSL_OUTDIR=/tmp/android_openssl_builder/ssl | |
LOGDIR=/tmp/android_openssl_builder/logs | |
ARCHS=( | |
x86 | |
arm64-v8a | |
armeabi | |
armeabi-v7a | |
mips | |
mips64 | |
x86_64 | |
) | |
# In same order as ARCHS - each toolchain corresponds to an architecture | |
TOOLS=( | |
x86 | |
aarch64-linux-android | |
arm-linux-androideabi | |
arm-linux-androideabi | |
mipsel-linux-android | |
mips64el-linux-android | |
x86_64 | |
) | |
# In same order as ARCHS - the host is the prefix of the tools in the toolchain bin | |
HOSTS=( | |
i686-linux-android | |
aarch64-linux-android | |
arm-linux-androideabi | |
arm-linux-androideabi | |
mipsel-linux-android | |
mips64el-linux-android | |
x86_64-linux-android | |
) | |
# In same order as ARCHS - folder names of <ndk>/platforms/$PLATFORM/xxxxx | |
SYSROOTS=( | |
arch-x86 | |
arch-arm64 | |
arch-arm | |
arch-arm | |
arch-mips | |
arch-mips64 | |
arch-x86_64 | |
) | |
# In same order as ARCHS - machines for openssl | |
OPENSSL_OS=( | |
linux-generic32 | |
linux-generic64 | |
linux-generic32 | |
linux-generic32 | |
linux-generic32 | |
linux-generic64 | |
linux-generic64 | |
) | |
check_built() | |
{ | |
arch=$1 | |
LOG_PREFIX="=== "$arch": " | |
openssl_artefact=$output_directory/$arch/libssl.so | |
crypto_artefact=$output_directory/$arch/libcrypto.so | |
if [ -f "$openssl_artefact" -a -f "$crypto_artefact" ]; then | |
echo $LOG_PREFIX "openssl: OK" | |
else | |
echo $LOG_PREFIX "openssl: FAIL" | |
echo $LOG_PREFIX "$openssl_artefact" | |
echo $LOG_PREFIX "$crypto_artefact" | |
fi | |
} | |
while getopts o:n:s:y:p:t:v:h opt; do | |
case $opt in | |
n) | |
ndk_directory=$OPTARG | |
;; | |
o) | |
output_directory=$OPTARG | |
;; | |
s) | |
openssl_directory=$OPTARG | |
;; | |
p) | |
PLATFORM=$OPTARG | |
;; | |
t) | |
TOOLCHAIN_INSTALL_DIR=$OPTARG | |
;; | |
v) | |
TOOLCHAIN_VERSION=$OPTARG | |
;; | |
h) | |
echo "$USAGE" | |
exit 0 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
if [ -z "$output_directory" -o -z "$ndk_directory" -o -z "$openssl_directory" ]; then | |
echo "$USAGE" | |
exit 0 | |
fi | |
echo "========================================" | |
echo "======== openssl_builder start ========" | |
echo "========================================" | |
echo "=== Logs will be output to $LOGDIR" | |
mkdir -p $LOGDIR | |
# Build openssl for each Android architecture | |
for (( i=0; i<${#ARCHS[@]}; i++)); do | |
tool=${TOOLS[$i]} | |
arch=${ARCHS[$i]} | |
host=${HOSTS[$i]} | |
sysroot=${SYSROOTS[$i]} | |
# Clear the log file | |
logfile=$LOGDIR/"$arch".log | |
rm -f $logfile | |
touch $logfile | |
install_dir=$TOOLCHAIN_INSTALL_DIR/$tool | |
mkdir -p $install_dir | |
LOG_PREFIX="=== "$arch": " | |
if [ -d "$install_dir/bin" ]; then | |
echo $LOG_PREFIX "Skipping creation of toolchain - exists in $install_dir" | |
else | |
echo $LOG_PREFIX "Building toolchain" | |
$ndk_directory/build/tools/make-standalone-toolchain.sh --platform=$PLATFORM --install-dir=$install_dir --toolchain=$tool-"$TOOLCHAIN_VERSION" --abis=$arch --force >> $logfile | |
fi | |
echo $LOG_PREFIX "Adding toolchain to PATH" | |
export PATH=$install_dir/bin:$PATH | |
############ OPENSSL ############ | |
echo $LOG_PREFIX "Clean openssl output directory" | |
mkdir -p $OPENSSL_OUTDIR/$arch | |
rm -rf $OPENSSL_OUTDIR/$arch/* | |
echo $LOG_PREFIX "Setting up openssl environment" | |
export ANDROID_NDK_ROOT=$ndk_directory | |
export ANDROID_ARCH=$sysroot | |
export ANDROID_EABI=$host-"$TOOLCHAIN_VERSION" | |
export ANDROID_API=$PLATFORM | |
export ANDROID_SYSROOT=$ndk_directory/platforms/$PLATFORM/$sysroot | |
export ANDROID_TOOLCHAIN=$install_dir | |
export ANDROID_DEV=$ANDROID_SYSROOT/usr | |
export SYSTEM=android | |
export ARCH=$arch | |
export CROSS_COMPILE=$host- | |
export CFLAGS="--sysroot=$ANDROID_SYSROOT" | |
export CPPFLAGS="--sysroot=$ANDROID_SYSROOT" | |
export CXXFLAGS="--sysroot=$ANDROID_SYSROOT" | |
echo $LOG_PREFIX "Configuring openssl" | |
pushd $openssl_directory > /dev/null | |
./Configure ${OPENSSL_OS[$i]} shared -no-asm -no-ssl3 -no-comp -no-hw --cross-compile-prefix=$CROSS_COMPILE --openssldir=$OPENSSL_OUTDIR/$arch --prefix=$OPENSSL_OUTDIR/$arch >> $logfile | |
popd > /dev/null | |
echo $LOG_PREFIX "Building openssl" | |
pushd $openssl_directory > /dev/null | |
echo $LOG_PREFIX "make depend" | |
make depend >> $logfile | |
echo $LOG_PREFIX "make" | |
make >> $logfile | |
echo $LOG_PREFIX "Installing openssl libraries to $OPENSSL_OUTDIR/$arch" | |
make install_sw >> $logfile | |
echo $LOG_PREFIX "Cleaning up openssl" | |
make clean >> $logfile | |
popd > /dev/null | |
echo $LOG_PREFIX "Copying build artefacts to output directory" | |
output_dir=$output_directory/$arch | |
mkdir -p $output_dir/include | |
cp -r $OPENSSL_OUTDIR/$arch/include/* $output_dir/include/ | |
cp -a $OPENSSL_OUTDIR/$arch/lib/libssl.so.* $output_dir/libssl.so | |
cp -a $OPENSSL_OUTDIR/$arch/lib/libssl.a $output_dir/ | |
cp -a $OPENSSL_OUTDIR/$arch/lib/libcrypto.so.* $output_dir/libcrypto.so | |
cp -a $OPENSSL_OUTDIR/$arch/lib/libcrypto.a $output_dir/ | |
echo "========================================" | |
check_built $arch | |
echo "========================================" | |
done | |
echo "========================================" | |
echo "========= openssl_builder done =========" | |
echo "========================================" | |
echo "Checking if everything worked..." | |
echo "----------------------------------------" | |
for (( i=0; i<${#ARCHS[@]}; i++)); do | |
arch=${ARCHS[$i]} | |
check_built $arch | |
echo "----------------------------------------" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment