Skip to content

Instantly share code, notes, and snippets.

@jhandley
Created January 15, 2016 13:25
Show Gist options
  • Save jhandley/a5e9ab2fe09e7addde6f to your computer and use it in GitHub Desktop.
Save jhandley/a5e9ab2fe09e7addde6f to your computer and use it in GitHub Desktop.
#!/bin/bash
# Download and build libcurl, openssl and zlib for Android using Crystax NDK r7
# Must be run on 32 bit Linux as the Crystax r7 NDK doesn't support 64 bit hosts
# Tested on Ubuntu 14.04
# Make the working directory
mkdir curl-android-build
cd curl-android-build
ROOT_DIR=`pwd -P`
echo Building curl for Android in $ROOT_DIR
OUTPUT_DIR=$ROOT_DIR/output
mkdir $OUTPUT_DIR
# Download Crystax NDK
wget https://www.crystax.net/download/android-ndk-r7-crystax-5.beta3-linux-x86.tar.bz2
tar -xvf android-ndk-r7-crystax-5.beta3-linux-x86.tar.bz2
# NDK environment variables
export NDK_ROOT=$ROOT_DIR/android-ndk-r7-crystax-5.beta3/
export PATH=$PATH:$NDK_ROOT
# Create standalone toolchain for cross-compiling
$NDK_ROOT/build/tools/make-standalone-toolchain.sh --arch=arm --platform=android-14 --install-dir=ndk-standalone-toolchain
TOOLCHAIN=$ROOT_DIR/ndk-standalone-toolchain
# Setup cross-compile environment
export PATH=$PATH:$TOOLCHAIN/bin
export SYSROOT=$TOOLCHAIN/sysroot
export ARCH=armv7
export CC=arm-linux-androideabi-gcc
export CXX=arm-linux-androideabi-g++
export AR=arm-linux-androideabi-ar
export AS=arm-linux-androideabi-as
export LD=arm-linux-androideabi-ld
export RANLIB=arm-linux-androideabi-ranlib
export NM=arm-linux-androideabi-nm
export STRIP=arm-linux-androideabi-strip
export CHOST=arm-linux-androideabi
OUTPUT_DIR=$ROOT_DIR/libcurl-android
mkdir $OUTPUT_DIR
# Download and build zlib
mkdir -p $OUTPUT_DIR/zlib/lib/armeabi-v7a
mkdir $OUTPUT_DIR/zlib/include
ZLIB_DIR=$ROOT_DIR/zlib-1.2.8
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -xvzf zlib-1.2.8.tar.gz
cd $ZLIB_DIR
./configure --static
make
# Copy zlib lib and includes to output directory
cp libz.a $OUTPUT_DIR/zlib/lib/armeabi-v7a/
cp zconf.h $OUTPUT_DIR/zlib/include/
cp zlib.h $OUTPUT_DIR/zlib/include/
cd ..
# Download and build openssl
wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2d.tar.gz
tar -xvf openssl-1.0.2d.tar.gz
cd openssl-1.0.2d/
export CPPFLAGS="-mthumb -mfloat-abi=softfp -mfpu=vfp -march=${ARCH} -DANDROID"
./Configure android-armv7 no-asm no-shared --static --with-zlib-include=${ZLIB_DIR}/include --with-zlib-lib=${ZLIB_DIR}/lib
make build_crypto build_ssl
# Copy openssl lib and includes to output directory
mkdir -p $OUTPUT_DIR/openssl/lib/armeabi-v7a
mkdir $OUTPUT_DIR/openssl/include
cp libssl.a $OUTPUT_DIR/openssl/lib/armeabi-v7a
cp libcrypto.a $OUTPUT_DIR/openssl/lib/armeabi-v7a
cp -LR include/openssl $OUTPUT_DIR/openssl/include
cd ..
OPENSSL_DIR=$ROOT_DIR/openssl-1.0.2d
# Download and build libcurl
wget http://curl.haxx.se/download/curl-7.45.0.tar.gz
tar -xvf curl-7.45.0.tar.gz
cd curl-7.45.0
export CFLAGS="-v --sysroot=$SYSROOT -mandroid -march=$ARCH -mfloat-abi=softfp -mfpu=vfp -mthumb"
export CPPFLAGS="$CFLAGS -DANDROID -DCURL_STATICLIB -mthumb -mfloat-abi=softfp -mfpu=vfp -march=$ARCH -I${OPENSSL_DIR}/include/ -I${TOOLCHAIN}/include"
export LDFLAGS="-march=$ARCH -Wl,--fix-cortex-a8 -L${OPENSSL_DIR}"
./configure --host=arm-linux-androideabi --disable-shared --enable-static --disable-dependency-tracking --with-zlib=${ZLIB_DIR} --with-ssl=${OPENSSL_DIR} --without-ca-bundle --without-ca-path --enable-ipv6 --enable-http --enable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-proxy --disable-dict --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smtp --disable-gopher --disable-sspi --disable-manual --target=arm-linux-androideabi --prefix=/opt/curlssl
make
# Copy libcurl and includes to output directory
mkdir -p $OUTPUT_DIR/curl/lib/armeabi-v7a
mkdir $OUTPUT_DIR/curl/include
cp lib/.libs/libcurl.a $OUTPUT_DIR/curl/lib/armeabi-v7a
cp -LR include/curl $OUTPUT_DIR/curl/include
cd ..
# Tar up output
OUTPUT_FILE=libcurl-android.tar.gz
tar -czf $OUTPUT_FILE -C $OUTPUT_DIR .
echo Build result saved to $ROOT_DIR/$OUTPUT_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment