Skip to content

Instantly share code, notes, and snippets.

@letiemble
Last active April 23, 2023 11:33
Show Gist options
  • Save letiemble/6710405 to your computer and use it in GitHub Desktop.
Save letiemble/6710405 to your computer and use it in GitHub Desktop.
A bash script to generate "all-in-one" OpenSSL static libraries for OS X and iOS. The script produces fat static libraries (i386, x86_64 for OS X) and (i386, x86_64, armv7, armv7s, arm64 for iOS) suitable for integration in both OS X and iOS project.
#!/bin/bash
###############################################################################
## ##
## Build and package OpenSSL static libraries for OSX/iOS ##
## ##
## This script is in the public domain. ##
## Creator : Laurent Etiemble ##
## ##
###############################################################################
## --------------------
## Parameters
## --------------------
VERSION=1.0.2e
OSX_SDK=10.11
MIN_OSX=10.6
IOS_SDK=9.2
# These values are used to avoid version detection
FAKE_NIBBLE=0x102031af
FAKE_TEXT="OpenSSL 0.9.8y 5 Feb 2013"
## --------------------
## Variables
## --------------------
DEVELOPER_DIR=`xcode-select -print-path`
if [ ! -d $DEVELOPER_DIR ]; then
echo "Please set up Xcode correctly. '$DEVELOPER_DIR' is not a valid developer tools folder."
exit 1
fi
if [ ! -d "$DEVELOPER_DIR/Platforms/MacOSX.platform/Developer/SDKs/MacOSX$OSX_SDK.sdk" ]; then
echo "The OS X SDK $OSX_SDK was not found."
exit 1
fi
if [ ! -d "$DEVELOPER_DIR/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS$IOS_SDK.sdk" ]; then
echo "The iOS SDK $IOS_SDK was not found."
exit 1
fi
BASE_DIR=`pwd`
BUILD_DIR="$BASE_DIR/build"
DIST_DIR="$BASE_DIR/dist"
FILES_DIR="$BASE_DIR/files"
OPENSSL_NAME="openssl-$VERSION"
OPENSSL_FILE="$OPENSSL_NAME.tar.gz"
OPENSSL_URL="http://www.openssl.org/source/$OPENSSL_FILE"
OPENSSL_PATH="$FILES_DIR/$OPENSSL_FILE"
## --------------------
## Main
## --------------------
_unarchive() {
# Expand source tree if needed
if [ ! -d "$SRC_DIR" ]; then
echo "Unarchive sources for $PLATFORM-$ARCH..."
(cd "$BUILD_DIR"; tar -zxf "$OPENSSL_PATH"; mv "$OPENSSL_NAME" "$SRC_DIR";)
fi
}
_configure() {
# Configure
if [ "x$DONT_CONFIGURE" == "x" ]; then
echo "Configuring $PLATFORM-$ARCH..."
(cd "$SRC_DIR"; CROSS_TOP="$CROSS_TOP" CROSS_SDK="$CROSS_SDK" CC="$CC" ./Configure --prefix="$DST_DIR" -no-apps "$COMPILER" > "$LOG_FILE" 2>&1)
fi
}
_build() {
# Build
if [ "x$DONT_BUILD" == "x" ]; then
echo "Building $PLATFORM-$ARCH..."
(cd "$SRC_DIR"; CROSS_TOP="$CROSS_TOP" CROSS_SDK="$CROSS_SDK" CC="$CC" make >> "$LOG_FILE" 2>&1)
fi
}
build_osx() {
ARCHS="i386 x86_64"
for ARCH in $ARCHS; do
PLATFORM="MacOSX"
COMPILER="darwin-i386-cc"
SRC_DIR="$BUILD_DIR/$PLATFORM-$ARCH"
DST_DIR="$DIST_DIR/$PLATFORM-$ARCH"
LOG_FILE="$BASE_DIR/$PLATFORM$OSX_SDK-$ARCH.log"
# Select the compiler
if [ "$ARCH" == "i386" ]; then
COMPILER="darwin-i386-cc"
else
COMPILER="darwin64-x86_64-cc"
fi
CROSS_TOP="$DEVELOPER_DIR/Platforms/$PLATFORM.platform/Developer"
CROSS_SDK="$PLATFORM$OSX_SDK.sdk"
CC="$DEVELOPER_DIR/usr/bin/gcc -arch $ARCH"
_unarchive
_configure
# Patch Makefile
sed -ie "s/^CFLAG= -/CFLAG= -mmacosx-version-min=$MIN_OSX -/" "$SRC_DIR/Makefile"
# Patch versions
sed -ie "s/^# define OPENSSL_VERSION_NUMBER.*$/# define OPENSSL_VERSION_NUMBER $FAKE_NIBBLE/" "$SRC_DIR/crypto/opensslv.h"
sed -ie "s/^# define OPENSSL_VERSION_TEXT.*$/# define OPENSSL_VERSION_TEXT \"$FAKE_TEXT\"/" "$SRC_DIR/crypto/opensslv.h"
_build
done
}
build_ios() {
ARCHS="i386 x86_64 armv7 armv7s arm64"
for ARCH in $ARCHS; do
PLATFORM="iPhoneOS"
COMPILER="iphoneos-cross"
SRC_DIR="$BUILD_DIR/$PLATFORM-$ARCH"
DST_DIR="$DIST_DIR/$PLATFORM-$ARCH"
LOG_FILE="$BASE_DIR/$PLATFORM$IOS_SDK-$ARCH.log"
# Select the compiler
if [ "$ARCH" == "i386" ]; then
PLATFORM="iPhoneSimulator"
MIN_IOS="4.2"
elif [ "$ARCH" == "x86_64" ]; then
PLATFORM="iPhoneSimulator"
MIN_IOS="7.0"
elif [ "$ARCH" == "arm64" ]; then
MIN_IOS="7.0"
else
MIN_IOS="6.0"
fi
CROSS_TOP="$DEVELOPER_DIR/Platforms/$PLATFORM.platform/Developer"
CROSS_SDK="$PLATFORM$IOS_SDK.sdk"
CC="clang -arch $ARCH -fembed-bitcode"
_unarchive
_configure
# Patch Makefile
if [ "$ARCH" == "x86_64" ]; then
sed -ie "s/^CFLAG= -/CFLAG= -miphoneos-version-min=$MIN_IOS -DOPENSSL_NO_ASM -/" "$SRC_DIR/Makefile"
else
sed -ie "s/^CFLAG= -/CFLAG= -miphoneos-version-min=$MIN_IOS -/" "$SRC_DIR/Makefile"
fi
# Patch versions
sed -ie "s/^# define OPENSSL_VERSION_NUMBER.*$/# define OPENSSL_VERSION_NUMBER $FAKE_NIBBLE/" "$SRC_DIR/crypto/opensslv.h"
sed -ie "s/^# define OPENSSL_VERSION_TEXT.*$/# define OPENSSL_VERSION_TEXT \"$FAKE_TEXT\"/" "$SRC_DIR/crypto/opensslv.h"
_build
done
}
distribute_osx() {
PLATFORM="MacOSX"
NAME="$OPENSSL_NAME-$PLATFORM"
DIR="$DIST_DIR/$NAME"
FILES="libcrypto.a libssl.a"
mkdir -p "$DIR/include"
mkdir -p "$DIR/lib"
echo "$VERSION" > "$DIR/VERSION"
cp "$BUILD_DIR/MacOSX-i386/LICENSE" "$DIR"
cp -LR "$BUILD_DIR/MacOSX-i386/include/" "$DIR/include"
# Alter rsa.h to make Swift happy
sed -i .bak 's/const BIGNUM \*I/const BIGNUM *i/g' "$DIR/include/openssl/rsa.h"
for f in $FILES; do
lipo -create \
"$BUILD_DIR/MacOSX-i386/$f" \
"$BUILD_DIR/MacOSX-x86_64/$f" \
-output "$DIR/lib/$f"
done
(cd "$DIST_DIR"; tar -cvf "../$NAME.tar.gz" "$NAME")
}
distribute_ios() {
PLATFORM="iOS"
NAME="$OPENSSL_NAME-$PLATFORM"
DIR="$DIST_DIR/$NAME"
FILES="libcrypto.a libssl.a"
mkdir -p "$DIR/include"
mkdir -p "$DIR/lib"
echo "$VERSION" > "$DIR/VERSION"
cp "$BUILD_DIR/iPhoneOS-i386/LICENSE" "$DIR"
cp -LR "$BUILD_DIR/iPhoneOS-i386/include/" "$DIR/include"
# Alter rsa.h to make Swift happy
sed -i .bak 's/const BIGNUM \*I/const BIGNUM *i/g' "$DIR/include/openssl/rsa.h"
for f in $FILES; do
lipo -create \
"$BUILD_DIR/iPhoneOS-i386/$f" \
"$BUILD_DIR/iPhoneOS-x86_64/$f" \
"$BUILD_DIR/iPhoneOS-arm64/$f" \
"$BUILD_DIR/iPhoneOS-armv7/$f" \
"$BUILD_DIR/iPhoneOS-armv7s/$f" \
-output "$DIR/lib/$f"
done
(cd "$DIST_DIR"; tar -cvf "../$NAME.tar.gz" "$NAME")
}
# Create folders
mkdir -p "$BUILD_DIR"
mkdir -p "$DIST_DIR"
mkdir -p "$FILES_DIR"
# Retrieve OpenSSL tarbal if needed
if [ ! -e "$OPENSSL_PATH" ]; then
curl "$OPENSSL_URL" -o "$OPENSSL_PATH"
fi
build_osx
build_ios
distribute_osx
distribute_ios
@octopus-prime
Copy link

Great! Thanks :-)

@MKGitHub
Copy link

DOES NOT WORK!

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 316 100 316 0 0 4230 0 --:--:-- --:--:-- --:--:-- 4270
Unarchive sources for MacOSX-i386...
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
mv: rename openssl-1.0.2e to /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386: No such file or directory
Configuring MacOSX-i386...
./openssl-maker.sh: line 69: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386/Makefile: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386/crypto/opensslv.h: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386/crypto/opensslv.h: No such file or directory
Building MacOSX-i386...
./openssl-maker.sh: line 77: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386: No such file or directory
Unarchive sources for MacOSX-x86_64...
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
mv: rename openssl-1.0.2e to /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-x86_64: No such file or directory
Configuring MacOSX-x86_64...
./openssl-maker.sh: line 69: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-x86_64: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-x86_64/Makefile: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-x86_64/crypto/opensslv.h: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-x86_64/crypto/opensslv.h: No such file or directory
Building MacOSX-x86_64...
./openssl-maker.sh: line 77: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-x86_64: No such file or directory
Unarchive sources for iPhoneSimulator-i386...
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
mv: rename openssl-1.0.2e to /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386: No such file or directory
Configuring iPhoneSimulator-i386...
./openssl-maker.sh: line 69: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386/Makefile: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386/crypto/opensslv.h: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386/crypto/opensslv.h: No such file or directory
Building iPhoneSimulator-i386...
./openssl-maker.sh: line 77: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386: No such file or directory
Unarchive sources for iPhoneSimulator-x86_64...
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
mv: rename openssl-1.0.2e to /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-x86_64: No such file or directory
Configuring iPhoneSimulator-x86_64...
./openssl-maker.sh: line 69: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-x86_64: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-x86_64/Makefile: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-x86_64/crypto/opensslv.h: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-x86_64/crypto/opensslv.h: No such file or directory
Building iPhoneSimulator-x86_64...
./openssl-maker.sh: line 77: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-x86_64: No such file or directory
Unarchive sources for iPhoneOS-armv7...
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
mv: rename openssl-1.0.2e to /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7: No such file or directory
Configuring iPhoneOS-armv7...
./openssl-maker.sh: line 69: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7/Makefile: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7/crypto/opensslv.h: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7/crypto/opensslv.h: No such file or directory
Building iPhoneOS-armv7...
./openssl-maker.sh: line 77: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7: No such file or directory
Unarchive sources for iPhoneOS-armv7s...
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
mv: rename openssl-1.0.2e to /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7s: No such file or directory
Configuring iPhoneOS-armv7s...
./openssl-maker.sh: line 69: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7s: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7s/Makefile: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7s/crypto/opensslv.h: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7s/crypto/opensslv.h: No such file or directory
Building iPhoneOS-armv7s...
./openssl-maker.sh: line 77: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-armv7s: No such file or directory
Unarchive sources for iPhoneOS-arm64...
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
mv: rename openssl-1.0.2e to /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-arm64: No such file or directory
Configuring iPhoneOS-arm64...
./openssl-maker.sh: line 69: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-arm64: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-arm64/Makefile: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-arm64/crypto/opensslv.h: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-arm64/crypto/opensslv.h: No such file or directory
Building iPhoneOS-arm64...
./openssl-maker.sh: line 77: cd: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-arm64: No such file or directory
cp: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386/LICENSE: No such file or directory
cp: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386/include/: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/dist/openssl-1.0.2e-MacOSX/include/openssl/rsa.h: No such file or directory
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't open input file: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386/libcrypto.a (No such file or directory)
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't open input file: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/MacOSX-i386/libssl.a (No such file or directory)
a openssl-1.0.2e-MacOSX
a openssl-1.0.2e-MacOSX/include
a openssl-1.0.2e-MacOSX/lib
a openssl-1.0.2e-MacOSX/VERSION
cp: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386/LICENSE: No such file or directory
cp: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386/include/: No such file or directory
sed: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/dist/openssl-1.0.2e-iOS/include/openssl/rsa.h: No such file or directory
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't open input file: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386/libcrypto.a (No such file or directory)
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't open input file: /Users/mk/Desktop/6710405-fec1a10c0caa67de151e23ec2335658be796d1e6/build/iPhoneOS-i386/libssl.a (No such file or directory)
a openssl-1.0.2e-iOS
a openssl-1.0.2e-iOS/include
a openssl-1.0.2e-iOS/lib
a openssl-1.0.2e-iOS/VERSION

@eSKon
Copy link

eSKon commented Jun 19, 2016

Excuse me, could you explain why you use fake version? Is it necessary? If we use this build with qt, for example, qt will never know real version and create wrong configuration. Can we comment version replacement code?
UPD: Ok, i commented and all was built fine. I change url only from http to https.
Anyway, i would want know why you used fake version.
Thanks.

@MikeoftheClan
Copy link

@ MKGitHub
I had the same issue as yours. It seems that it's the curl command in the script which does not work correctly since we get a very small tar.gz file. From there, nothing can't work of course. As I'm not an expert in all this, I simply downloaded the tar.gz directly from openssl repository (https://www.openssl.org/source/old/1.0.2/), copied the downloaded archive inside a files folder, and then you can run the script easily.
Also, I'm using a recent XCode, 7.3.1, for which IOS_SDK must be 9.3 instead of 9.2.
Hope it helps.

@letiemble: do you know why the curl command fails? I remember that it used to work in previous versions. Many thanks!

@austnryder
Copy link

@MikeoftheClan @MKGitHub @letiemble

Change line 50:
OPENSSL_URL="http://www.openssl.org/source/$OPENSSL_FILE"
to:
OPENSSL_URL="https://www.openssl.org/source/$OPENSSL_FILE"

The curl command will work after the change, and the rest will work too.

@yvsbb
Copy link

yvsbb commented Sep 26, 2017

Thank you so much. This worked with the LTS 1.0.2l.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment