Blame scripts/OpenSSL-DownloadAndBuild.command

Packit 1fb8d4
#!/bin/bash
Packit 1fb8d4
# 
Packit 1fb8d4
# Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
# 
Packit 1fb8d4
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
Packit 1fb8d4
# If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit 1fb8d4
#
Packit 1fb8d4
# This script will download and build openssl for iOS and simulator - see ARCHS for architectures built
Packit 1fb8d4
Packit 1fb8d4
## Settings
Packit 1fb8d4
# openssl version to use
Packit Service 5a9772
OPENSSLVERSION="1.0.2q"
Packit Service 5a9772
SHA256SUM="5744cfcbcec2b1b48629f7354203bc1e5e9b5466998bbccc5b5fcde3b18eb684"
Packit 1fb8d4
# SDK version to use - if not set latest version found is used
Packit 1fb8d4
SDK_VERSION=""
Packit 1fb8d4
Packit 1fb8d4
# Minimum SDK version the application supports
Packit Service 5a9772
MIN_SDK_VERSION="10.0"
Packit 1fb8d4
Packit 1fb8d4
## Defaults
Packit 1fb8d4
INSTALLDIR="external"
Packit 1fb8d4
Packit 1fb8d4
# Architectures to build
Packit 1fb8d4
ARCHS="i386 x86_64 armv7 armv7s arm64"
Packit 1fb8d4
Packit 1fb8d4
# Use default SDK version if not set
Packit 1fb8d4
if [ -z ${SDK_VERSION} ]; then
Packit 1fb8d4
    SDK_VERSION=`xcrun -sdk iphoneos --show-sdk-version`
Packit 1fb8d4
fi
Packit 1fb8d4
Packit 1fb8d4
CORES=`sysctl hw.ncpu | awk '{print $2}'`
Packit 1fb8d4
MAKEOPTS="-j $CORES"
Packit 1fb8d4
# disable parallell builds since openssl build
Packit 1fb8d4
# fails sometimes
Packit 1fb8d4
MAKEOPTS=""
Packit 1fb8d4
Packit 1fb8d4
DEVELOPER=`xcode-select -print-path`
Packit 1fb8d4
if [ ! -d "$DEVELOPER" ]; then
Packit 1fb8d4
  echo "xcode path is not set correctly $DEVELOPER does not exist (most likely because of xcode > 4.3)"
Packit 1fb8d4
  echo "run"
Packit 1fb8d4
  echo "sudo xcode-select -switch <xcode path>"
Packit 1fb8d4
  echo "for default installation:"
Packit 1fb8d4
  echo "sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer"
Packit 1fb8d4
  exit 1
Packit 1fb8d4
fi
Packit 1fb8d4
Packit 1fb8d4
function run {
Packit 1fb8d4
    "$@"
Packit 1fb8d4
    local status=$?
Packit 1fb8d4
    if [ $status -ne 0 ]; then
Packit 1fb8d4
        echo "error with $@" >&2
Packit 1fb8d4
        exit $status
Packit 1fb8d4
    fi
Packit 1fb8d4
    return $status
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
# Functions
Packit 1fb8d4
function buildArch(){
Packit 1fb8d4
    ARCH=$1
Packit 1fb8d4
    if [[ "${ARCH}" == "i386" || "${ARCH}" == "x86_64" ]];
Packit 1fb8d4
    then
Packit 1fb8d4
        PLATFORM="iPhoneSimulator"
Packit 1fb8d4
    else
Packit 1fb8d4
        run sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
Packit 1fb8d4
        PLATFORM="iPhoneOS"
Packit 1fb8d4
    fi
Packit 1fb8d4
Packit 1fb8d4
    run export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
Packit 1fb8d4
    run export CROSS_SDK="${PLATFORM}${SDK_VERSION}.sdk"
Packit 1fb8d4
    run export BUILD_TOOLS="${DEVELOPER}"
Packit 1fb8d4
    run export CC="${BUILD_TOOLS}/usr/bin/gcc -arch ${ARCH}"
Packit 1fb8d4
    if [ ! -z $MIN_SDK_VERSION ]; then
Packit 1fb8d4
        run export CC="$CC -miphoneos-version-min=${MIN_SDK_VERSION}"
Packit 1fb8d4
    fi
Packit 1fb8d4
    echo "Building openssl-${OPENSSLVERSION} for ${PLATFORM} ${SDK_VERSION} ${ARCH} (min SDK set: ${MIN_SDK_VERSION:-"none"})"
Packit 1fb8d4
Packit 1fb8d4
    LOGFILE="BuildLog.darwin-${ARCH}.txt"
Packit 1fb8d4
    echo -n " Please wait ..."
Packit 1fb8d4
    if [[ "$OPENSSLVERSION" =~ 1.0.0. ]]; then
Packit 1fb8d4
        run ./Configure BSD-generic32 > "${LOGFILE}" 2>&1
Packit 1fb8d4
    elif [ "${ARCH}" == "x86_64" ]; then
Packit 1fb8d4
        run ./Configure darwin64-x86_64-cc > "${LOGFILE}" 2>&1
Packit 1fb8d4
    elif [ "${ARCH}" == "i386" ]; then
Packit 1fb8d4
        run ./Configure iphoneos-cross no-asm > "${LOGFILE}" 2>&1
Packit 1fb8d4
    else
Packit 1fb8d4
        run ./Configure iphoneos-cross  > "${LOGFILE}" 2>&1
Packit 1fb8d4
    fi
Packit 1fb8d4
Packit 1fb8d4
    run make ${MAKEOPTS} >> ${LOGFILE} 2>&1
Packit 1fb8d4
    echo " Done. Build log saved in ${LOGFILE}"
Packit 1fb8d4
    run cp libcrypto.a ../../lib/libcrypto_${ARCH}.a
Packit 1fb8d4
    run cp libssl.a ../../lib/libssl_${ARCH}.a
Packit 1fb8d4
    run make clean >/dev/null 2>&1
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
# main
Packit 1fb8d4
if [ $# -gt 0 ];then
Packit 1fb8d4
    INSTALLDIR=$1
Packit 1fb8d4
    if [ ! -d $INSTALLDIR ];then
Packit 1fb8d4
        echo "Install directory \"$INSTALLDIR\" does not exist"
Packit 1fb8d4
        exit 1
Packit 1fb8d4
    fi
Packit 1fb8d4
fi
Packit 1fb8d4
Packit 1fb8d4
cd $INSTALLDIR
Packit 1fb8d4
if [ ! -d openssl ];then
Packit 1fb8d4
    run mkdir openssl
Packit 1fb8d4
fi
Packit 1fb8d4
run cd openssl
Packit 1fb8d4
CS=`shasum -a 256 "openssl-$OPENSSLVERSION.tar.gz" | cut -d ' ' -f1`
Packit 1fb8d4
if [ ! "$CS" = "$SHA256SUM" ]; then
Packit 1fb8d4
    echo "Downloading OpenSSL Version $OPENSSLVERSION ..."
Packit 1fb8d4
    run rm -f "openssl-$OPENSSLVERSION.tar.gz"
Packit 1fb8d4
    run curl -o "openssl-$OPENSSLVERSION.tar.gz" https://www.openssl.org/source/openssl-$OPENSSLVERSION.tar.gz
Packit 1fb8d4
Packit 1fb8d4
    CS=`shasum -a 256 "openssl-$OPENSSLVERSION.tar.gz" | cut -d ' ' -f1`
Packit 1fb8d4
    if [ ! "$CS" = "$SHA256SUM" ]; then
Packit 1fb8d4
    echo "Download failed or invalid checksum. Have a nice day."
Packit 1fb8d4
    exit 1
Packit 1fb8d4
    fi
Packit 1fb8d4
fi
Packit 1fb8d4
Packit 1fb8d4
# remove old build dir
Packit 1fb8d4
run rm -rf openssltmp
Packit 1fb8d4
run mkdir openssltmp
Packit 1fb8d4
run cd openssltmp
Packit 1fb8d4
Packit 1fb8d4
echo "Unpacking OpenSSL ..."
Packit 1fb8d4
run tar xfz "../openssl-$OPENSSLVERSION.tar.gz"
Packit 1fb8d4
if [ ! $? = 0 ]; then
Packit 1fb8d4
    echo "Unpacking failed."
Packit 1fb8d4
    exit 1
Packit 1fb8d4
fi
Packit 1fb8d4
echo
Packit 1fb8d4
Packit 1fb8d4
run cd "openssl-$OPENSSLVERSION"
Packit 1fb8d4
Packit 1fb8d4
case `pwd` in
Packit 1fb8d4
     *\ * )
Packit 1fb8d4
           echo "The build path (`pwd`) contains whitepsaces - fix this."
Packit 1fb8d4
           exit 1
Packit 1fb8d4
          ;;
Packit 1fb8d4
esac
Packit 1fb8d4
Packit 1fb8d4
# Cleanup old build artifacts
Packit 1fb8d4
run rm -rf ../../include
Packit 1fb8d4
run mkdir -p ../../include
Packit 1fb8d4
Packit 1fb8d4
run rm -rf ../../lib
Packit 1fb8d4
run mkdir -p ../../lib
Packit 1fb8d4
Packit 1fb8d4
for i in ${ARCHS}; do
Packit 1fb8d4
    buildArch $i
Packit 1fb8d4
done
Packit 1fb8d4
Packit 1fb8d4
echo "Copying header files ..."
Packit 1fb8d4
run cp -r include/ ../../include/
Packit 1fb8d4
echo
Packit 1fb8d4
Packit 1fb8d4
echo "Combining to unversal binary"
Packit 1fb8d4
run lipo -create ../../lib/libcrypto_*.a -o ../../lib/libcrypto.a
Packit 1fb8d4
run lipo -create ../../lib/libssl_*.a -o ../../lib/libssl.a
Packit 1fb8d4
Packit 1fb8d4
echo "Finished. Please verify the contens of the openssl folder in \"$INSTALLDIR\""