Skip to content

Instantly share code, notes, and snippets.

@cogwirrel
Created December 20, 2016 01:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cogwirrel/4a733b4138d1b35d4cfe69b4df110d0b to your computer and use it in GitHub Desktop.
Save cogwirrel/4a733b4138d1b35d4cfe69b4df110d0b to your computer and use it in GitHub Desktop.
Build curl and openssl for all android architectures
#!/bin/bash
USAGE=$'Usage:
./libcurl_builder <options>
Required:
-c <curl_directory> The top level curl directory
-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
-y <system> The system you are using (default: darwin-x86_64 - for mac users)
-p <platform> Android platform (default: android-21)
-t <toolchain_install_dir> The directory in which to install the toolchains (default: /tmp/libcurl_builder/toolhain)
-v <toolchain_version> The toolchain version (default: 4.9)
'
# Architectures and Toolchains
TOOLCHAIN_VERSION=4.9
PLATFORM=android-21
TOOLCHAIN_SYSTEM=darwin-x86_64
TOOLCHAIN_INSTALL_DIR=/tmp/libcurl_builder/toolchain
OPENSSL_OUTDIR=/tmp/libcurl_builder/ssl
LOGDIR=/tmp/libcurl_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/libcurl/archs/$arch/lib/libssl.so
if [ -f "$openssl_artefact" ]; then
echo $LOG_PREFIX "openssl: OK"
else
echo $LOG_PREFIX "openssl: FAIL"
fi
curl_artefact=$output_directory/libcurl/archs/$arch/libcurl.so
if [ -f "$curl_artefact" ]; then
echo $LOG_PREFIX "curl: OK"
else
echo $LOG_PREFIX "curl: FAIL"
fi
}
while getopts c:o:n:s:y:p:t:v:h opt; do
case $opt in
c)
curl_directory=$OPTARG
;;
n)
ndk_directory=$OPTARG
;;
o)
output_directory=$OPTARG
;;
s)
openssl_directory=$OPTARG
;;
y)
TOOLCHAIN_SYSTEM=$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 "$curl_directory" -o -z "$output_directory" -o -z "$ndk_directory" -o -z "$openssl_directory" ]; then
echo "$USAGE"
exit 0
fi
echo "========================================"
echo "======== libcurl_builder start ========"
echo "========================================"
echo "=== Logs will be output to $LOGDIR"
mkdir -p $LOGDIR
# Generate configure file
if [ -f "$curl_directory/configure" ]; then
echo "=== Skipping generation of curl configure file"
else
echo "=== Generating curl configure file"
pushd $curl_directory > /dev/null
./buildconf > $LOGDIR/curl_buildconf.log
popd > /dev/null
fi
# Build curl 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 --system=$TOOLCHAIN_SYSTEM >> $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-ssl2 -no-ssl3 -no-comp -no-hw --cross-compile-prefix=$CROSS_COMPILE --openssldir=$OPENSSL_OUTDIR/$arch >> $logfile
popd > /dev/null
# Remove the version from the soname generated by the makefile
echo $LOG_PREFIX "Modify openssl makefile to remove version from soname"
pushd $openssl_directory > /dev/null
sed -i.bak 's/^SHLIB_EXT=\.so\..*/SHLIB_EXT=\.so/' Makefile
sed -i.bak 's/LIBVERSION=[^ ]* /LIBVERSION= /g' Makefile
sed -i.bak 's/install: all install_docs install_sw/install: install_docs install_sw/g' Makefile
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 to $OPENSSL_OUTDIR/$arch"
make install >> $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/libssl/archs/$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/
output_dir=$output_directory/libcrypto/archs/$arch
mkdir -p $output_dir
cp -a $OPENSSL_OUTDIR/$arch/lib/libcrypto.so $output_dir/
############ CURL ############
echo $LOG_PREFIX "Setting flags for configure"
export ANDROID_SYSROOT=$ndk_directory/platforms/$PLATFORM/$sysroot
export CFLAGS="--sysroot=$ANDROID_SYSROOT"
export CPPFLAGS="--sysroot=$ANDROID_SYSROOT"
export CXXFLAGS="--sysroot=$ANDROID_SYSROOT"
echo $LOG_PREFIX "Configuring curl"
pushd $curl_directory > /dev/null
./configure --host=$host --with-sysroot=$ANDROID_SYSROOT --with-ssl=$OPENSSL_OUTDIR/$arch --disable-soname-bump >> $logfile
popd > /dev/null
# Libtool generates the SONAME libcurl.so.x.x.x, but android requires libcurl.so
# So we remove $major (the version) from the soname spec
echo $LOG_PREFIX "Modifying libtool to generate shared library without version in name"
pushd $curl_directory > /dev/null
sed -i.bak 's/^soname_spec=\"\(.*\)\\\$major\"$/soname_spec=\"\1\"/' libtool
sed -i.bak 's/^version_type=linux$/version_type=none/' libtool
popd > /dev/null
echo $LOG_PREFIX "Building curl"
pushd $curl_directory > /dev/null
make >> $logfile
popd > /dev/null
echo $LOG_PREFIX "Copying build artefacts to output directory"
output_dir=$output_directory/libcurl/archs/$arch
mkdir -p $output_dir/include
cp -r $curl_directory/include/* $output_dir/include/
cp -a $curl_directory/lib/.libs/libcurl.so* $output_dir/
echo $LOG_PREFIX "Cleaning curl"
pushd $curl_directory > /dev/null
make clean >> $logfile
popd > /dev/null
echo "========================================"
check_built $arch
echo "========================================"
done
echo "========================================"
echo "========= libcurl_builder done ========="
echo "========================================"
echo "Checking if everything worked..."
echo "----------------------------------------"
for (( i=0; i<${#ARCHS[@]}; i++)); do
arch=${ARCHS[$i]}
check_built $arch
echo "----------------------------------------"
done
@wseemann
Copy link

Thank you for putting this script together! Do you have a paypal or something similar so I can send you some money?

@willyliu
Copy link

willyliu commented Feb 3, 2017

Thanks so much for this script, but there were some problems:

  1. The make-standalone-toolchain.sh line should append --force so that it will actually build the toolchain.
  2. The Configure line for openssl should append --prefix==$OPENSSL_OUTDIR/$arch so that it won't install into /usr/local/ssl

@karolsteve
Copy link

Hello guy. thanks for script. please can you update that ?

@cogwirrel
Copy link
Author

cogwirrel commented Jul 31, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment