📜 ⬆️ ⬇️

Self build cURL for iOS and Android

Good day!
I am developing an application for iOS / Android, which uses the cURL library.
Until recently, we used the libcurl build build:

At the same time, the library versions were different, and the header files were the same.
In order to restore order, it was decided to build the library on their own.


Supported support for the following versions of mobile platforms:

Build under iOS

The cURL download page has a link to the library version for iOS.
Clicking on it we find ourselves on a page where the author describes in detail how you can build cURL for iOS.
Building on iOS comes down to simple steps:
export CC="__llvm-gcc" export CFLAGS="-arch armv7 -arch armv7s -pipe -Os -gdwarf-2 -isysroot __iPhoneOS.sdk" export LDFLAGS="-arch armv7 -arch armv7s -isysroot __iPhoneOS.sdk" curl -O http://curl.haxx.se/download/curl-7.31.0.tar.gz tar -xzf curl-7.31.0.tar.gz cd curl-7.31.0 ./configure --disable-shared --enable-static --disable-dependency-tracking --host=armv7-apple-darwin make 

To support HTTPS on iOS, it is sufficient to configure cURL with the –with-darwinssl key (only for iOS5 and higher).
For earlier versions will have to use OpenSSL.

Build under Android

The cURL documentation suggests two ways to build for Android:

Collect under Android, it was decided the second way.
To do this, you first need to generate a toolchain by running the script make-standalone-toolchain.sh
More information about this can be found in the Android documentation NDK STANDALONE-TOOLCHAIN.html
In my case, the toolchain build looked like this (MAC OS X, Android NDK r8e, android-14, gcc4.7):
 $NDK_ROOT/build/tools/make-standalone-toolchain.sh \ --platform=android-14 \ --install-dir=android-toolchain-gcc4.7 \ --toolchain=arm-linux-androideabi-4.7 \ --system=darwin-x86_64 #   NDK_ROOT       Android NDK 

As a result, the android-toolchain-gcc4.7 directory will be created, containing a copy of sysroot for arm android-14 and toolchain gcc4.7.
This toolchain can be used to build Android projects with the Autotools build system.
This is what the cURL build would look like:
 $NDK_ROOT/build/tools/make-standalone-toolchain.sh --install-dir=toolchain bla-bla-bla export PATH=`pwd`/toolchain/bin:$PATH export CC=arm-linux-androideabi-gcc curl -O http://curl.haxx.se/download/curl-7.31.0.tar.gz tar -xzf curl-7.31.0.tar.gz cd curl-7.31.0 ./configure --disable-shared --enable-static --host=arm-linux-androideabi make 

To support HTTPS, you will have to additionally build from OpenSSL sources.
')
Here is my script for building libcurl for iOS and Android with support for the HTTP, HTTPS protocols (MAC OS X, Xcode 4.6.3, iOS SDK 6.1):
build.sh
 #!/bin/bash mkdir -p include mkdir -p prebuilt/ios/device mkdir -p prebuilt/ios/simulator mkdir -p prebuilt/android/armeabi-v7a # 0. Make "standalone toolchain" for android if [ ! -d android-toolchain-gcc4.7 ] then $NDK_ROOT/build/tools/make-standalone-toolchain.sh --platform=android-14 --install-dir=android-toolchain-gcc4.7 --toolchain=arm-linux-androideabi-4.7 --system=darwin-x86_64 fi # 1. Get sources LIBCURL_SRC=curl-7.31.0 OPENSSL_SRC=openssl-1.0.1e LIBCURL_PAGE=http://curl.haxx.se/download OPENSSL_PAGE=http://www.openssl.org/source LIBCURL_ROOT=curl OPENSSL_ROOT=openssl # param 1: lib name # param 2: download page # param 3: symlink to source dir # exit with status 1 if downloading failed function download_and_unpack() { TARBALL=$1.tar.gz echo $TARBALL if [ ! -f $TARBALL ] then curl -O $2/$TARBALL || exit 1 rm -rf $1 tar -xzf $TARBALL rm $3 ln -s $1 $3 fi } download_and_unpack $LIBCURL_SRC $LIBCURL_PAGE $LIBCURL_ROOT download_and_unpack $OPENSSL_SRC $OPENSSL_PAGE $OPENSSL_ROOT # 2. Build sources CURL_EXTRA="--enable-ipv6 --disable-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" LIBCURL_BINARY=lib/.libs/libcurl.a CPU_COUNT=`sysctl -n hw.logicalcpu_max` export IPHONEOS_DEPLOYMENT_TARGET="5.0" # 2.1 Build cURL for iOS device (armv7, armv7s) pushd $LIBCURL_ROOT export CC="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2" export CFLAGS="-arch armv7 -arch armv7s -pipe -Os -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk" export LDFLAGS="-arch armv7 -arch armv7s -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk" ./configure --disable-shared --enable-static --disable-dependency-tracking --host=armv7-apple-darwin --with-darwinssl $CURL_EXTRA || exit 1 make clean && make -j $CPU_COUNT || exit 1 cp -f $LIBCURL_BINARY ../prebuilt/ios/device popd # 2.2 Build cURL for iOS simulator (i386) pushd $LIBCURL_ROOT export CC="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-gcc-4.2" export CFLAGS="-arch i386 -pipe -Os -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk" export CPPFLAGS="-D__IPHONE_OS_VERSION_MIN_REQUIRED=${IPHONEOS_DEPLOYMENT_TARGET%%.*}0000" export LDFLAGS="-arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk" ./configure --disable-shared --enable-static --disable-dependency-tracking --host=i386-apple-darwin --with-darwinssl $CURL_EXTRA || exit 1 make clean && make -j $CPU_COUNT || exit 1 cp -f $LIBCURL_BINARY ../prebuilt/ios/simulator popd # 2.3.1 Build OpenSSL for Android pushd $OPENSSL_ROOT # See 0 above export PATH=`pwd`/../android-toolchain-gcc4.7/bin:$PATH export CC=arm-linux-androideabi-gcc export CXX=arm-linux-androideabi-g++ export AR=arm-linux-androideabi-ar export RANLIB=arm-linux-androideabi-ranlib ./Configure android-armv7 no-shared || exit 1 make clean && make build_crypto build_ssl -j $CPU_COUNT || exit 1 cp -f libcrypto.a ../prebuilt/android/armeabi-v7a cp -f libssl.a ../prebuilt/android/armeabi-v7a popd # 2.3.2 Build CURL for Android pushd $LIBCURL_ROOT OPENSSL=`pwd`/../openssl export CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16" export CPPFLAGS=-DANDROID export LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8 -L$OPENSSL" ./configure --disable-shared --enable-static --host=arm-linux-androideabi --with-random=/dev/urandom --with-ssl=$OPENSSL --without-ca-bundle --without-ca-path --with-zlib $CURL_EXTRA || exit 1 make clean && make -j $CPU_COUNT || exit 1 cp -f $LIBCURL_BINARY ../prebuilt/android/armeabi-v7a popd # 3 Copy headers cp -f ./curl/include/curl/*.h ./include 


Basic steps:

Compiled libraries are copied to the prebuilt directory, header files in include.
To support earlier versions of Android, you will need to build the appropriate standalone toolchain.

Related Links:
Offline build documentation cURL
Assembly instructions cURL for iOS
Android NDK Standalone toolchain

Source: https://habr.com/ru/post/184960/


All Articles