Skip to content

Instantly share code, notes, and snippets.

@mobileben
Last active May 13, 2019 03:20
Show Gist options
  • Save mobileben/dbe72732d9446a1a055f9d7fb453464c to your computer and use it in GitHub Desktop.
Save mobileben/dbe72732d9446a1a055f9d7fb453464c to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Filename: build-openssl-ios.sh
# Author: Benjamin Lee
# Date: March 11, 2017
# Copyright: (c) Copyright 2017 2n Productions
# License: MIT
#
# Copyright (c) 2017 2n Productions
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# target=macos, ios
# branch =master
# NOTE: This is the current location these items in the Xcode toolchain. These have moved in the past. So be aware you may at some point need to update these
PLATFORMPATH="`xcode-select --print-path`/Platforms"
TOOLSPATH="`xcode-select --print-path`/Toolchains/XcodeDefault.xctoolchain/usr/bin/"
BRANCH=OpenSSL_1_0_2-stable
# By default, we are building for iOS
TARGET=ios
OSNAME=iPhoneOS
IOSARCHS=("armv7s" "armv7" "arm64" "arm64e" "i386" "x86_64")
#IOSARCHS=("x86_64")
MACOSARCHS=("i386" "x86_64")
LIBSSL=libssl
LIBCRYPTO=libcrypto
DEPLOYMENT_SDKVERSION="9.3"
updateOpenSSL() {
if [ -d openssl ]; then
pushd openssl
git remote update
git checkout origin/$BRANCH
if [ $? -ne 0 ]; then
echo "Branch $BRANCH does not exist"
exit 1
fi
git pull origin $BRANCH
popd
else
git clone https://github.com/openssl/openssl.git
pushd openssl
git checkout origin/$BRANCH
if [ $? -ne 0 ]; then
echo "Branch $BRANCH does not exist"
exit 1
fi
popd
fi
}
findLatestSDKVersion() {
sdks=`ls $PLATFORMPATH/$1.platform/Developer/SDKs`
arr=()
for sdk in $sdks
do
arr[${#arr[@]}]=$sdk
done
# Last item will be the current SDK, since it is alpha ordered
count=${#arr[@]}
if [ $count -gt 0 ]; then
sdk=${arr[$count-1]:${#1}}
num=`expr ${#sdk}-4`
SDKVERSION=${sdk:0:$num}
else
SDKVERSION=$DEPLOYMENT_SDKVERSION
fi
}
buildSlice() {
pwd=`pwd`
target=$1
hosttarget=$1
platform=$2
targetdir=$3
# Based on Configuration/10-main.conf, there are now 3 possible configure hosts for ios: iphone-cross, ios-cross, and ios64-cross
#
# Excerpt from 10-main.conf:
# It takes three prior-set environment variables to make it work:
#
# CROSS_COMPILE=/where/toolchain/is/usr/bin/ [note ending slash]
# CROSS_TOP=/where/SDKs/are
# CROSS_SDK=iPhoneOSx.y.sdk
#
# Exact paths vary with Xcode releases, but for couple of last ones
# they would look like this:
#
# CROSS_COMPILE=`xcode-select --print-path`/Toolchains/XcodeDefault.xctoolchain/usr/bin/
# CROSS_TOP=`xcode-select --print-path`/Platforms/iPhoneOS.platform/Developer
# CROSS_SDK=iPhoneOS.sdk
#
# Note that I have found that assigning CROSS_COMPILE does not work properly
#
if [[ $platform == "MacOSX" ]]; then
findLatestSDKVersion $platform
ostype="macosx"
OSFLAGS="-mmacosx-version-min=$SDKVERSION"
if [[ $hosttarget == "i386" ]]; then
CONFIGURE_HOST="darwin-i386-cc"
else
CONFIGURE_HOST="darwin64-x86_64-cc"
fi
export CROSS_TOP="${PLATFORMPATH}/$platform.platform/Developer"
export CROSS_SDK="$platform$SDKVERSION.sdk"
else
if [ $hosttarget == "i386" ] || [ $hosttarget == "x86_64" ]; then
platform=iPhoneSimulator
ostype="iphonesimulator"
else
ostype="iphoneos"
fi
export CC="$(xcrun -sdk ${ostype} -find clang)"
export CPP="$CC -E"
export AR=$(xcrun -sdk ${ostype} -find ar)
export RANLIB=$(xcrun -sdk ${ostype} -find ranlib)
findLatestSDKVersion $platform
OSFLAGS="-miphoneos-version-min=$DEPLOYMENT_SDKVERSION"
if [[ $target == "armv7"* ]]; then
OSFLAGS+=" -Wno-ignored-optimization-argument"
fi
if [[ $hosttarget == "arm64" || $hosttarget == "arm64e" ]]; then
CONFIGURE_HOST="ios64-cross"
CONFIGURE_HOST="iphoneos-cross"
elif [[ $hosttarget == "armv7" || $hosttarget == "armv7s" ]]; then
CONFIGURE_HOST="ios-cross"
CONFIGURE_HOST="iphoneos-cross"
elif [[ $hosttarget == "i386" ]]; then
CONFIGURE_HOST="darwin-i386-cc"
elif [[ $hosttarget == "x86_64" ]]; then
CONFIGURE_HOST="darwin64-x86_64-cc"
else
CONFIGURE_HOST="iphoneos-cross"
fi
export CROSS_TOP="${PLATFORMPATH}/$platform.platform/Developer"
export CROSS_SDK="$platform$SDKVERSION.sdk"
fi
PREFIXDIRNAME=$PREFIX/$target
OPENSSLDIRNAME=$OPENSSLDIR/$target
mkdir -p $PREFIXDIRNAME
mkdir -p $OPENSSLDIRNAME
./configure $CONFIGURE_HOST --prefix="$PREFIXDIRNAME" --openssldir="$OPENSSLDIRNAME"
if [ $CONFIGURE_HOST == "iphoneos-cross" ]; then
# sed -ie "s!^CFLAGS=!CFLAGS=-arch ${target} ${OSFLAGS} !" "Makefile"
sed -ie "s!^CFLAG=!CFLAG=-arch ${target} ${OSFLAGS} !" "Makefile"
elif [ $platform == "MacOSX" ]; then
# sed -ie "s!^CFLAGS=!CFLAGS=${OSFLAGS} !" "Makefile"
sed -ie "s!^CFLAG=!CFLAG=${OSFLAGS} !" "Makefile"
elif [ $platform == "iPhoneSimulator" ]; then
# sed -ie "s!^CFLAGS=!CFLAGS=${OSFLAGS} !" "Makefile"
sed -ie "s!^CFLAG=!CFLAG= -isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} ${OSFLAGS} !" "Makefile"
fi
find . -name *.o | xargs rm
make clean
make depend
make > $OPENSSLDIRNAME/make.out 2>&1
make install >> $OPENSSLDIRNAME/make.out 2>&1
}
while [[ $# -gt 1 ]]; do
key="$1"
case $key in
-b|--branch)
BRANCH="$2"
shift
;;
-t|--target)
TARGET="$2"
shift
;;
--prefix)
PREFIX="$2"
shift
;;
--openssldir)
OPENSSLDIR="$2"
shift
;;
*)
echo "Unsupported argument"
;;
esac
shift
done
if [ $TARGET != "macos" ] && [ $TARGET != "ios" ]; then
echo "Target $TARGET is not supported"
exit 1
fi
pwd=`pwd`
if [ -z $PREFIX ]; then
PREFIX=$pwd/build/$TARGET
elif ! [[ $PREFIX == \/* ]] || [[ $PREFIX == \~\/* ]]; then
PREFIX=$pwd/$PREFIX
fi
mkdir -p $PREFIX
if [ -z $OPENSSLDIR ]; then
OPENSSLDIR=$pwd/build/$TARGET
elif ! [[ $PREFIX == \/* ]] || [[ $PREFIX == \~\/* ]]; then
OPENSSLDIR=$pwd/$OPENSSLDIR
fi
mkdir -p $OPENSSLDIR
updateOpenSSL
if [ $TARGET == "macos" ]; then
OSNAME=MacOSX
ARCHS=( "${MACOSARCHS[@]}" )
else
OSNAME=iPhoneOS
ARCHS=( "${IOSARCHS[@]}" )
fi
pushd openssl
pwd=`pwd`
OPENSSLSLICES=()
CRYPTOSLICES=()
for arch in "${ARCHS[@]}"
do
buildSlice $arch $OSNAME $TARGET
OPENSSLSLICES+=("$OPENSSLDIR/$arch/lib/$LIBSSL.a")
CRYPTOSLICES+=("$OPENSSLDIR/$arch/lib/$LIBCRYPTO.a")
done
lipo -create ${OPENSSLSLICES[@]} -output $OPENSSLDIR/$LIBSSL.a
lipo -create ${CRYPTOSLICES[@]} -output $OPENSSLDIR/$LIBCRYPTO.a
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment