Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A shell script to build openssl for iOS and Mac. >>>>> It currently builds: Mac (i386, x86_64) >>>>> iOS (armv7, arm64) >>>>> iOS Simulator (i386, x86_64) >>>>> Updated to work with Xcode 7 and produce bitcode enabled binaries >>>>> Minimum deployment target can be easily configured
#!/bin/bash
# This script builds the iOS and Mac openSSL libraries with Bitcode enabled
# Download openssl http://www.openssl.org/source/ and place the tarball next to this script
# Credits:
# https://github.com/st3fan/ios-openssl
# https://github.com/x2on/OpenSSL-for-iPhone/blob/master/build-libssl.sh
# Peter Steinberger, PSPDFKit GmbH, @steipete.
# Doron Adler, GlideTalk, @Norod78
# Updated to work with Xcode 7 and iOS 9
set -e
###################################
# OpenSSL Version
###################################
OPENSSL_VERSION="openssl-1.0.2d"
###################################
###################################
# SDK Version
###################################
IOS_SDK_VERSION=$(xcodebuild -version -sdk iphoneos | grep SDKVersion | cut -f2 -d ':' | tr -d '[[:space:]]')
###################################
################################################
# Minimum iOS deployment target version
################################################
MIN_IOS_VERSION="7.0"
################################################
# Minimum OS X deployment target version
################################################
MIN_OSX_VERSION="10.7"
echo "----------------------------------------"
echo "OpenSSL version: ${OPENSSL_VERSION}"
echo "iOS SDK version: ${IOS_SDK_VERSION}"
echo "iOS deployment target: ${MIN_IOS_VERSION}"
echo "OS X deployment target: ${MIN_OSX_VERSION}"
echo "----------------------------------------"
echo " "
DEVELOPER=`xcode-select -print-path`
buildMac()
{
ARCH=$1
echo "Start Building ${OPENSSL_VERSION} for ${ARCH}"
TARGET="darwin-i386-cc"
if [[ $ARCH == "x86_64" ]]; then
TARGET="darwin64-x86_64-cc"
fi
export CC="${BUILD_TOOLS}/usr/bin/clang -mmacosx-version-min=${MIN_OSX_VERSION}"
pushd . > /dev/null
cd "${OPENSSL_VERSION}"
echo "Configure"
./Configure ${TARGET} --openssldir="/tmp/${OPENSSL_VERSION}-${ARCH}" &> "/tmp/${OPENSSL_VERSION}-${ARCH}.log"
make >> "/tmp/${OPENSSL_VERSION}-${ARCH}.log" 2>&1
echo "make install"
make install >> "/tmp/${OPENSSL_VERSION}-${ARCH}.log" 2>&1
echo "make clean"
make clean >> "/tmp/${OPENSSL_VERSION}-${ARCH}.log" 2>&1
popd > /dev/null
echo "Done Building ${OPENSSL_VERSION} for ${ARCH}"
}
buildIOS()
{
ARCH=$1
echo "Start Building ${OPENSSL_VERSION} for ${PLATFORM} ${IOS_SDK_VERSION} ${ARCH}"
pushd . > /dev/null
cd "${OPENSSL_VERSION}"
if [[ "${ARCH}" == "i386" || "${ARCH}" == "x86_64" ]]; then
PLATFORM="iPhoneSimulator"
else
PLATFORM="iPhoneOS"
sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
fi
export $PLATFORM
export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
export CROSS_SDK="${PLATFORM}${IOS_SDK_VERSION}.sdk"
export BUILD_TOOLS="${DEVELOPER}"
export CC="${BUILD_TOOLS}/usr/bin/gcc -fembed-bitcode -mios-version-min=${MIN_IOS_VERSION} -arch ${ARCH}"
echo "Configure"
if [[ "${ARCH}" == "x86_64" ]]; then
./Configure darwin64-x86_64-cc --openssldir="/tmp/${OPENSSL_VERSION}-iOS-${ARCH}" &> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log"
else
./Configure iphoneos-cross --openssldir="/tmp/${OPENSSL_VERSION}-iOS-${ARCH}" &> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log"
fi
# add -isysroot to CC=
sed -ie "s!^CFLAG=!CFLAG=-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -mios-version-min=${MIN_IOS_VERSION} !" "Makefile"
echo "make"
make >> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log" 2>&1
echo "make install"
make install >> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log" 2>&1
echo "make clean"
make clean >> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log" 2>&1
popd > /dev/null
echo "Done Building ${OPENSSL_VERSION} for ${ARCH}"
}
echo "Cleaning up"
rm -rf include/openssl/* lib/*
rm -rf /tmp/${OPENSSL_VERSION}-*
rm -rf ${OPENSSL_VERSION}
mkdir -p lib/iOS
mkdir -p lib/Mac
mkdir -p include/openssl/
rm -rf "/tmp/${OPENSSL_VERSION}-*"
rm -rf "/tmp/${OPENSSL_VERSION}-*.log"
rm -rf "${OPENSSL_VERSION}"
if [ ! -e ${OPENSSL_VERSION}.tar.gz ]; then
echo "Downloading ${OPENSSL_VERSION}.tar.gz"
curl -O https://www.openssl.org/source/${OPENSSL_VERSION}.tar.gz
else
echo "Using ${OPENSSL_VERSION}.tar.gz"
fi
echo "Unpacking openssl"
tar xfz "${OPENSSL_VERSION}.tar.gz"
buildMac "i386"
buildMac "x86_64"
echo "Copying headers"
cp /tmp/${OPENSSL_VERSION}-i386/include/openssl/* include/openssl/
echo "Building Mac libraries"
lipo \
"/tmp/${OPENSSL_VERSION}-i386/lib/libcrypto.a" \
"/tmp/${OPENSSL_VERSION}-x86_64/lib/libcrypto.a" \
-create -output lib/Mac/libcrypto.a
lipo \
"/tmp/${OPENSSL_VERSION}-i386/lib/libssl.a" \
"/tmp/${OPENSSL_VERSION}-x86_64/lib/libssl.a" \
-create -output lib/Mac/libssl.a
buildIOS "armv7"
buildIOS "arm64"
buildIOS "x86_64"
buildIOS "i386"
echo "Building iOS libraries"
lipo \
"/tmp/${OPENSSL_VERSION}-iOS-armv7/lib/libcrypto.a" \
"/tmp/${OPENSSL_VERSION}-iOS-arm64/lib/libcrypto.a" \
"/tmp/${OPENSSL_VERSION}-iOS-i386/lib/libcrypto.a" \
"/tmp/${OPENSSL_VERSION}-iOS-x86_64/lib/libcrypto.a" \
-create -output lib/iOS/libcrypto.a
lipo \
"/tmp/${OPENSSL_VERSION}-iOS-armv7/lib/libssl.a" \
"/tmp/${OPENSSL_VERSION}-iOS-arm64/lib/libssl.a" \
"/tmp/${OPENSSL_VERSION}-iOS-i386/lib/libssl.a" \
"/tmp/${OPENSSL_VERSION}-iOS-x86_64/lib/libssl.a" \
-create -output lib/iOS/libssl.a
echo "Cleaning up"
rm -rf /tmp/${OPENSSL_VERSION}-*
rm -rf ${OPENSSL_VERSION}
echo "Done"
@Norod

This comment has been minimized.

Copy link
Owner Author

Norod commented Jul 29, 2015

Changes from fork:

  • Updated built OpenSSL version to openssl-1.0.2d
  • The script will now deduce the iOS SDK version by itself
  • Added some additional log traces
  • A bit more Cleanup
@EricDavies

This comment has been minimized.

Copy link

EricDavies commented Sep 2, 2015

Worked for first time. The parent script was failing for me. Thank you!

@Norod

This comment has been minimized.

Copy link
Owner Author

Norod commented Sep 16, 2015

Updated:

  • Build with bitcode enabled (Xcode 7)
@Norod

This comment has been minimized.

Copy link
Owner Author

Norod commented Sep 17, 2015

@EricDavies You are welcome 👍

@Norod

This comment has been minimized.

Copy link
Owner Author

Norod commented Sep 20, 2015

Update:

  • Minimum iOS deployment target version can now be configured
@pvinis

This comment has been minimized.

Copy link

pvinis commented Sep 30, 2015

im on 10.10. how can i build this for osx 10.9? with this script i get this ld: warning: object file (/Users/pvinis/Desktop/test/Vendor/openssl/lib/libcrypto.a(bio_lib.o)) was built for newer OSX version (10.10) than being linked (10.9)

@i36lib

This comment has been minimized.

Copy link

i36lib commented Oct 27, 2015

I made some changes and it work for me, thanks for your script:

  • openssl-1.0.2d ==> openssl-1.0.1g
  • make install ==> make install_sw
@pavithr

This comment has been minimized.

Copy link

pavithr commented Nov 2, 2015

Thanks for your script, it work for MAC, For IOS the script fails while configuring at below line..
I am using Xcode-6.1.1 and OSX 10.9.5.

What does it mean by "iphoneos-cross" in below script , please find attached log .

else
./Configure iphoneos-cross --openssldir="/tmp/${OPENSSL_VERSION}-iOS-${ARCH}" &> "/tmp/${OPENSSL_VERSION}-iOS-${ARCH}.log"
fi

openssl-1.0.0f-iOS-armv7.log::
Usage: Configure [no- ...] [enable- ...] [experimental- ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [no-asm] [no-dso] [no-krb5] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--test-sanity] os/compiler[:flags]

pick os/compiler from:
BC-32 BS2000-OSD BSD-generic32 BSD-generic64 BSD-ia64 BSD-sparc64 BSD-sparcv8
BSD-x86 BSD-x86-elf BSD-x86_64 Cygwin Cygwin-pre1.3 DJGPP MPE/iX-gcc OS2-EMX
OS390-Unix QNX6 QNX6-i386 ReliantUNIX SINIX SINIX-N UWIN VC-CE VC-WIN32
VC-WIN64A VC-WIN64I aix-cc aix-gcc aix3-cc aix64-cc aix64-gcc aux3-gcc
beos-x86-bone beos-x86-r5 bsdi-elf-gcc cc cray-j90 cray-t3e darwin-i386-cc
darwin-ppc-cc darwin64-ppc-cc darwin64-x86_64-cc dgux-R3-gcc dgux-R4-gcc
dgux-R4-x86-gcc dist gcc hpux-cc hpux-gcc hpux-ia64-cc hpux-ia64-gcc
hpux-parisc-cc hpux-parisc-cc-o4 hpux-parisc-gcc hpux-parisc1_0-cc
hpux-parisc2-cc hpux-parisc2-gcc hpux64-ia64-cc hpux64-ia64-gcc
hpux64-parisc2-cc hpux64-parisc2-gcc hurd-x86 irix-cc irix-gcc irix-mips3-cc
irix-mips3-gcc irix64-mips4-cc irix64-mips4-gcc linux-alpha+bwx-ccc
linux-alpha+bwx-gcc linux-alpha-ccc linux-alpha-gcc linux-aout linux-armv4
linux-elf linux-generic32 linux-generic64 linux-ia32-icc linux-ia64
linux-ia64-ecc linux-ia64-icc linux-ppc linux-ppc64 linux-s390x linux-sparcv8
linux-sparcv9 linux-x86_64 linux64-sparcv9 mingw mingw64 ncr-scde netware-clib
netware-clib-bsdsock netware-clib-bsdsock-gcc netware-clib-gcc netware-libc
netware-libc-bsdsock netware-libc-bsdsock-gcc netware-libc-gcc newsos4-gcc
nextstep nextstep3.3 osf1-alpha-cc osf1-alpha-gcc purify qnx4 rhapsody-ppc-cc
sco5-cc sco5-gcc solaris-sparcv7-cc solaris-sparcv7-gcc solaris-sparcv8-cc
solaris-sparcv8-gcc solaris-sparcv9-cc solaris-sparcv9-gcc solaris-x86-cc
solaris-x86-gcc solaris64-sparcv9-cc solaris64-sparcv9-gcc solaris64-x86_64-cc
solaris64-x86_64-gcc sunos-gcc tandem-c89 tru64-alpha-cc uClinux-dist
uClinux-dist64 ultrix-cc ultrix-gcc unixware-2.0 unixware-2.1 unixware-7
unixware-7-gcc vos-gcc vxworks-mipsle vxworks-ppc405 vxworks-ppc750
vxworks-ppc750-debug vxworks-ppc860 debug debug-BSD-x86-elf debug-Cygwin
debug-VC-WIN32 debug-VC-WIN64A debug-VC-WIN64I debug-ben debug-ben-debug
debug-ben-no-opt debug-ben-openbsd debug-ben-openbsd-debug debug-ben-strict
debug-bodo debug-darwin-i386-cc debug-darwin-ppc-cc debug-geoff32
debug-geoff64 debug-levitte-linux-elf debug-levitte-linux-elf-extreme
debug-levitte-linux-noasm debug-levitte-linux-noasm-extreme debug-linux-elf
debug-linux-elf-noefence debug-linux-generic32 debug-linux-generic64
debug-linux-ia32-aes debug-linux-pentium debug-linux-ppro debug-linux-x86_64
debug-rse debug-solaris-sparcv8-cc debug-solaris-sparcv8-gcc
debug-solaris-sparcv9-cc debug-solaris-sparcv9-gcc debug-steve-opt
debug-steve32 debug-steve64 debug-ulf debug-vos-gcc

NOTE: If in doubt, on Unix-ish systems use './config'.
Configuring for iphoneos-cross

@Norod

This comment has been minimized.

Copy link
Owner Author

Norod commented Feb 17, 2016

@pvinis Hey, sorry it appears that gist does not trigger notifications when someone comments. I just merged @armadsen 's fork which sets a minimum deployment target for OS X as well

@Norod

This comment has been minimized.

Copy link
Owner Author

Norod commented Feb 17, 2016

@pavithr Hey, sorry for this extremly late response. Seems like gist does not trigger notifications when someone comments. I think "iphoneos-cross" is define as part of OpenSSL's build setting but I'm not familiar with it 😞

@davidpn

This comment has been minimized.

Copy link

davidpn commented Mar 1, 2016

I'd like to be able to make this script build the dylibs for iOS simulator instead of the static libraries. I've attempted to modify the script to use the shared parameter in ./Configure, however that doesn't seemed to have worked, unless the dylibs are put somewhere I can't find :-)

@davidpn

This comment has been minimized.

Copy link

davidpn commented Mar 7, 2016

For anyone who is interested, I managed to work it out. The result of my work is here: http://delphi.radsoft.com.au/2016/03/building-openssl-dylibs-for-ios-simulator/
There's a bunch of stuff specific to Delphi, however the script could be used for any language that can use the dylibs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.