-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.sh
executable file
·324 lines (269 loc) · 8.74 KB
/
compile.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/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.1j
OSX_SDK=10.9
MIN_OSX=10.6
IOS_SDK=8.1
FWNAME="openssl"
# 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 iPhoneOS SDK $IOS_SDK was not found."
exit 1
fi
if [ ! -d "$DEVELOPER_DIR/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator$IOS_SDK.sdk" ]; then
echo "The iPhoneSimulator 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 -j >> "$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"
if [ -d "$DST_DIR" ]; then
continue
fi
# 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_iphoneos() {
ARCHS="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"
if [ -d "$DST_DIR" ]; then
continue
fi
# Select the compiler
if [ "$ARCH" == "arm64" ]; then
MIN_IOS="7.0"
else
MIN_IOS="4.0"
fi
CROSS_TOP="$DEVELOPER_DIR/Platforms/$PLATFORM.platform/Developer"
CROSS_SDK="$PLATFORM$IOS_SDK.sdk"
CC="clang -arch $ARCH"
_unarchive
_configure
# Patch Makefile
sed -ie "s/^CFLAG= -/CFLAG= -miphoneos-version-min=$MIN_IOS -/" "$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_iphonesimulator() {
ARCHS="i386 x86_64"
for ARCH in $ARCHS; do
PLATFORM="iPhoneSimulator"
COMPILER="iphoneos-cross"
SRC_DIR="$BUILD_DIR/$PLATFORM-$ARCH"
DST_DIR="$DIST_DIR/$PLATFORM-$ARCH"
LOG_FILE="$BASE_DIR/$PLATFORM$IOS_SDK-$ARCH.log"
if [ -d "$DST_DIR" ]; then
continue
fi
# Select the compiler
if [ "$ARCH" == "x86_64" ]; then
MIN_IOS="7.0"
else
MIN_IOS="4.0"
fi
CROSS_TOP="$DEVELOPER_DIR/Platforms/$PLATFORM.platform/Developer"
CROSS_SDK="$PLATFORM$IOS_SDK.sdk"
CC="clang -arch $ARCH"
_unarchive
_configure
# Patch Makefile
sed -ie "s/^CFLAG= -/CFLAG= -miphoneos-version-min=$MIN_IOS -/" "$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
}
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
}
distribute_iphoneos() {
PLATFORM="iPhoneOS"
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-armv7/LICENSE" "$DIR"
cp -LR "$BUILD_DIR/iPhoneOS-armv7/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-arm64/$f" \
"$BUILD_DIR/iPhoneOS-armv7/$f" \
"$BUILD_DIR/iPhoneOS-armv7s/$f" \
-output "$DIR/lib/$f"
done
}
distribute_iphonesimulator() {
PLATFORM="iPhoneSimulator"
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/iPhoneSimulator-i386/LICENSE" "$DIR"
cp -LR "$BUILD_DIR/iPhoneSimulator-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/iPhoneSimulator-i386/$f" \
"$BUILD_DIR/iPhoneSimulator-x86_64/$f" \
-output "$DIR/lib/$f"
done
}
have_not_framework() {
[[ -e "$FWNAME.framework" ]] && return 1 || return 0
}
make_framework() {
FWLIBS=""
PLATFORM=MacOSX
NAME="$OPENSSL_NAME-$PLATFORM"
FILES="libssl.a libcrypto.a"
mkdir -p $FWNAME.framework/Headers
find build -type d -maxdepth 1 -mindepth 1 \
| while read SRC_DIR; do
echo $(basename $SRC_DIR) \
| rev \
| cut -f1 -d- \
| rev
done \
| sort \
| uniq \
| while read ARCH; do
find build -type d -maxdepth 1 -mindepth 1 -name "*-$ARCH" \
| head -n1
done \
| while read ARCH_DIR; do
for f in $FILES; do
FWLIBS="$(find $ARCH_DIR -name "$f" -type f) $FWLIBS"
done
libtool -no_warning_for_no_symbols -static -o $FWNAME.framework/$FWNAME $FWLIBS
done
find $DIST_DIR/$NAME/include -type f | while read HEADER_FILE; do
cp $HEADER_FILE $FWNAME.framework/Headers/$(basename $HEADER_FILE)
done
}
# 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
if $(have_not_framework); then
build_osx
build_iphoneos
build_iphonesimulator
distribute_osx
distribute_iphoneos
distribute_iphonesimulator
make_framework
fi