Created
April 9, 2019 16:49
-
-
Save Wizermil/4af48e37ba7a7c7b889e0782d1fccf72 to your computer and use it in GitHub Desktop.
Script to compile openssl static library for iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
OPENSSL_VERSION="1.1.1b" | |
ARCHS=("i386" "armv7" "armv7s" "arm64" "arm64e" "x86_64") | |
SDK_VERSION="12.2" | |
ROOT_DIR=$(pwd) | |
WORKER=$(getconf _NPROCESSORS_ONLN) | |
BUILD_DIR="build/ios/openssl" | |
LOG_FILE="$ROOT_DIR/$BUILD_DIR/build.log" | |
if [ -d "$BUILD_DIR/download" ]; then | |
rm -rf "$BUILD_DIR/download" | |
fi | |
if [ -d "$BUILD_DIR/source" ]; then | |
rm -rf "$BUILD_DIR/source" | |
fi | |
if [ -f "$LOG_FILE" ]; then | |
rm "$LOG_FILE" | |
touch "$LOG_FILE" | |
fi | |
mkdir -p "$BUILD_DIR/download" | |
mkdir -p "$BUILD_DIR/source" | |
error() { | |
echo -e "$@" 1>&2 | |
} | |
fail() { | |
error "$@" | |
exit 1 | |
} | |
echo "Downloading: openssl" | |
curl -Lo "$BUILD_DIR/download/openssl-$OPENSSL_VERSION.tar.gz" "https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz" >> "$LOG_FILE" 2>&1 || fail "Error download openssl" | |
echo "Downloaded: openssl" | |
echo "Uncompressing: openssl" | |
tar zxf "$BUILD_DIR/download/openssl-$OPENSSL_VERSION.tar.gz" -C "$BUILD_DIR/source" >> "$LOG_FILE" 2>&1 || fail "Error uncompress openssl" | |
echo "Uncompressed: openssl" | |
for ARCH in "${ARCHS[@]}"; do | |
echo " " | |
cd "$ROOT_DIR" || exit 1 | |
if [ -d "$BUILD_DIR/output/$ARCH" ]; then | |
rm -rf "$BUILD_DIR/output/$ARCH" | |
fi | |
mkdir -p "$BUILD_DIR/output/$ARCH" | |
cp -R "$BUILD_DIR/source/openssl-$OPENSSL_VERSION/" "$BUILD_DIR/output/$ARCH" | |
if [ -d "$BUILD_DIR/install/$ARCH" ]; then | |
rm -rf "$BUILD_DIR/install/$ARCH" | |
fi | |
mkdir -p "$BUILD_DIR/install/$ARCH" | |
if [ "$ARCH" == "i386" ] || [ "$ARCH" == "x86_64" ]; then | |
PLATFORM="iPhoneSimulator" | |
MIN_VERSION="-mios-simulator-version-min" | |
else | |
PLATFORM="iPhoneOS" | |
MIN_VERSION="-mios-version-min" | |
fi | |
export CROSS_COMPILE="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/" | |
export CROSS_TOP="/Applications/Xcode.app/Contents/Developer/Platforms/$PLATFORM.platform/Developer/" | |
export CROSS_SDK="${PLATFORM}${SDK_VERSION}.sdk" | |
export BUILD_SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/$PLATFORM.platform/Developer/SDKs/${PLATFORM}${SDK_VERSION}.sdk" | |
export LDFLAGS="-arch $ARCH -pipe -isysroot $BUILD_SDKROOT $MIN_VERSION=10.0 -Os" | |
export CFLAGS="-arch $ARCH -pipe -isysroot $BUILD_SDKROOT -fembed-bitcode $MIN_VERSION=10.0 -Os" | |
export CPPFLAGS="$CFLAGS" | |
export CXXFLAGS="${CFLAGS} -fno-exceptions -fno-rtti" | |
export CC="clang" | |
export CPP="clang -E" | |
export CXX="clang++" | |
cd "$ROOT_DIR/$BUILD_DIR/output/$ARCH" || exit 1 | |
if [ -d "${BUILD_DIR}/download" ]; then | |
rm -rf "${BUILD_DIR}/download" | |
fi | |
if [ "$ARCH" == "x86_64" ]; then | |
HOST="darwin64-x86_64-cc" | |
elif [ "$ARCH" == "i386" ]; then | |
HOST="darwin-i386-cc" | |
elif [ "$ARCH" == "armv7" ] || [ "$ARCH" == "armv7s" ]; then | |
HOST="ios-cross" | |
elif [ "$ARCH" == "arm64" ] || [ "$ARCH" == "arm64e" ]; then | |
HOST="ios64-cross" | |
fi | |
echo "Configuring: openssl ${ARCH}" | |
./Configure "${HOST}" no-ssl2 no-ssl3 no-comp no-hw no-engine no-shared no-tests no-ui no-deprecated zlib --prefix="${ROOT_DIR}/${BUILD_DIR}/install/${ARCH}" --openssldir="${ROOT_DIR}/${BUILD_DIR}/output/${ARCH}" >> "$LOG_FILE" 2>&1 || fail "Error configuration openssl $ARCH" | |
sed -i '' -e "s!^CNF_CFLAGS=.*!CNF_CFLAGS=!g" "Makefile" || fail "Error configuration openssl $ARCH" | |
echo "Configured: openssl $ARCH" | |
echo "Compiling: openssl $ARCH" | |
make -j "$WORKER" >> "$LOG_FILE" 2>&1 || fail "Error compilation openssl $ARCH" | |
echo "Compiled: openssl $ARCH" | |
echo "Installing: openssl $ARCH" | |
make install >> "$LOG_FILE" 2>&1 || fail "Error installation openssl $ARCH" | |
echo "Installed: openssl $ARCH" | |
done | |
cd "$ROOT_DIR/$BUILD_DIR" || exit 1 | |
if [ -f "libcrypto.a" ]; then | |
rm -f "libcrypto.a" | |
fi | |
if [ -f "libssl.a" ]; then | |
rm -f "libssl.a" | |
fi | |
echo " " | |
echo "Creating Flat Libs: crypto + ssl" | |
LIPO_CRYPTO_CMD="lipo -create" | |
LIPO_SSL_CMD="lipo -create" | |
for ARCH in "${ARCHS[@]}"; do | |
if [ -f "$ROOT_DIR/$BUILD_DIR/install/$ARCH/lib/libcrypto.a" ]; then | |
LIPO_CRYPTO_CMD="$LIPO_CRYPTO_CMD $ROOT_DIR/$BUILD_DIR/install/$ARCH/lib/libcrypto.a" | |
fi | |
if [ -f "$ROOT_DIR/$BUILD_DIR/install/$ARCH/lib/libssl.a" ]; then | |
LIPO_SSL_CMD="$LIPO_SSL_CMD $ROOT_DIR/$BUILD_DIR/install/$ARCH/lib/libssl.a" | |
fi | |
done | |
LIPO_CRYPTO_CMD="$LIPO_CRYPTO_CMD -output libcrypto.a" | |
eval "$LIPO_CRYPTO_CMD" >> "$LOG_FILE" 2>&1 || fail "Error creation flat crypto" | |
echo "Created Flat Lib: crypto" | |
LIPO_SSL_CMD="$LIPO_SSL_CMD -output libssl.a" | |
eval "$LIPO_SSL_CMD" >> "$LOG_FILE" 2>&1 || fail "Error creation flat ssl" | |
echo "Created Flat Lib: ssl" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment