forked from shigeyas/build-openssl-iphone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·252 lines (210 loc) · 6.67 KB
/
build.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
#!/bin/bash
#
# Build OpenSSL libraries for iPhone, both device and simulator. Building debug libraries
# is attempted, but currently fails for OpenSSL openssl-1.0.0d. Results of builds are placed
# in directories dist-{d,}, which contain:
# dist universal (i386,arm) libraries for both device and simulator
# dist-d universal (i386,arm) debug libraries for both device and simulator
#
# Currently, since builds are not working cleanly, the arch-specific dist directories are
# hanging around:
# dist-device
# dist-device-d
# dist-simulator
# dist-simulator-d
#
# Common usage:
# git clone <this project> build-openssl-iphone
# wget http://www.openssl.org/source/openssl-1.0.0d.tar.gz
# ./build-openssl-iphone/BUILD-OpenSSL.sh -t openssl-1.0.0d.tar.gz
BUILD_STATUS=0
build_openssl() {
local TARGET_DIR="$1"
local LIBNAME=$2
local DISTDIR="`pwd`/dist-$LIBNAME"
local PLATFORM="$3"
local SDKPATH="$4"
p "Building binary for iPhone $LIBNAME $PLATFORM to $DISTDIR from $TARGET_DIR"
case $LIBNAME in
${LIBNAME_DEVICE}) ARCH="armv6"; ASSEMBLY="no-asm";;
*) ARCH="i386"; ASSEMBLY="";;
esac
cd "${TARGET_DIR}"
p Patching crypto/ui/ui_openssl.c
p From: `fgrep 'intr_signal;' crypto/ui/ui_openssl.c`
perl -pi.bak \
-e 's/static volatile sig_atomic_t intr_signal;/static volatile int intr_signal;/;' \
crypto/ui/ui_openssl.c
p To: `fgrep 'intr_signal;' crypto/ui/ui_openssl.c`
# Compile a version for the device...
PATH="${PLATFORM}"/Developer/usr/bin:$OPATH
export PATH
# build release and debug versions
for dflag in "" -d; do
# # for now *always* build debug versions
# dflag=-d
p "# Building clean on current config first"
x make clean
p "# Putting intermediate builds into directory ${DISTDIR}${dflag}"
x mkdir "${DISTDIR}${dflag}"
p "# configuring: ${dflag} --openssldir=${DISTDIR}${dflag} ${ASSEMBLY}"
x ./config ${dflag} --openssldir="${DISTDIR}${dflag}" ${ASSEMBLY}
p "# hand-editing CC, CFLAG and -arch in Makefile"
perl -pi.bak \
-e "s#CC= cc#CC=${PLATFORM}/Developer/usr/bin/gcc# ;" \
-e "s#CFLAG= #CFLAG=-arch ${ARCH} -isysroot ${SDKPATH} # ;" \
-e "s#-arch i386#-arch ${ARCH}# ;" \
Makefile
case $LIBNAME in
${LIBNAME_SIMULATOR})
perl -pi.bak \
-e 'if (/LIBDEPS=/) { s/\)}";/\)} -L.";/; }' \
Makefile.shared
(cd apps; ln -s "${SDKPATH}"/usr/lib/crt1.10.5.o crt1.10.6.o);
(cd test; ln -s "${SDKPATH}"/usr/lib/crt1.10.5.o crt1.10.6.o);
;;
esac
p "# Building libraries (only)"
# use -j (with # of CPUs) for parallel, fast build
x make -j `/usr/bin/hwprefs cpu_count` build_libs
BUILD_STATUS=$?
[ $BUILD_STATUS != 0 ] && break
p "# installing ${DISTDIR}${dflag}"
x make install
echo -n "Finished ${DISTDIR}${dflag}. Hit ENTER to continue: "
read input
done
cd -
}
extract_tarball() {
local tarball="$1"
local dir="${tarball%%.tar.gz}"
if [ -d "${dir}" ]; then
p Removing previous tarball directory: ${dir}
/bin/rm -rf ${dir}
fi
p Extracting tarball ${tarball}
x tar zxf ${tarball}
}
clean_dir() {
local dir="$1"
if [ $BUILD_STATUS = 0 ]; then
p Cleaning up "${DIR}"
/bin/rm -rf ${DIR}
else
p "Build failed ($BUILD_STATUS)"
p "Output is in $OUT"
exit 1
fi
}
latest_sdk() {
# set SDK to the last SDK in directory $1 (sorted lexically)
local dir="$1"
x pushd "$dir"
local sdk_list=(*)
local sdk_count=${#sdk_list[*]}
local last_sdk_index=$sdk_count
((last_sdk_index--))
LATEST_SDK="${dir}/${sdk_list[$last_sdk_index]}"
x popd
}
setup_vars() {
# clean up old compiler output
rm -f /tmp/build_openssl-*
# where verbose compiler output goes
OUT=`mktemp /tmp/build_openssl.XXXXXX` || exit 1
# use whatever name you want for the libraries
LIBNAME_DEVICE=device
LIBNAME_SIMULATOR=simulator
# don't touch anything below
# if you want to change the "current" Xcode, use "sudo xcode-select switch <path>"
XCODE=`xcode-select -print-path`
PLATFORMS="${XCODE}"/Platforms
MAC_PLATFORM="${PLATFORMS}"/MacOSX.platform
IOS_PLATFORM="${PLATFORMS}"/iPhoneOS.platform
IOS_SIMULATOR_PLATFORM="${PLATFORMS}"/iPhoneSimulator.platform
MAC_SDK_DIR="$MAC_PLATFORM/Developer/SDKs"
IOS_SDK_DIR="$IOS_PLATFORM/Developer/SDKs"
IOS_SIMULATOR_SDK_DIR="$IOS_SIMULATOR_PLATFORM/Developer/SDKs"
OPATH=$PATH
# latest_sdk() sets "LATEST_SDK" to the latest one (why can't functions return values? *sigh)
latest_sdk "$IOS_SDK_DIR"
IOS_SDK="$LATEST_SDK"
latest_sdk "$IOS_SIMULATOR_SDK_DIR"
IOS_SIMULATOR_SDK="$LATEST_SDK"
}
p() {
echo "$*" | tee -a $OUT 2>&1
}
px() {
echo "$*" | tee -a $OUT
$* | tee -a $OUT 2>&1
}
x() {
$* >> $OUT 2>&1
}
# use getopt to get command-line options
args=`getopt d:t: $*`; errcode=$?; set -- $args
if [ $errcode != 0 ]; then
echo <<EOF
Usage: $0 [-d dir] [-t tb.tar.gz]
-d dir -- build already-extracted tarball in dir
-t tb.tar.gz -- use specified tarball
You should specify **either** the tarball or the directory, but not both.
EOF
exit 1
fi
for i; do
case "$i"
in
-d) TARGET_DIR="$2"; echo "TARGET_DIR is $TARGET_DIR"; shift;;
-t) TARGET="$2"; shift;;
--) shift; break;;
esac
done
if [ ${#TARGET} -gt 0 -a ${#TARGET_DIR} -gt 0 ]; then
echo "Only specify *either* the target directory or the tarball"
exit 1
fi
if [ ${#TARGET} = 0 -a ${#TARGET_DIR} = 0 ]; then
echo "Please specify either the target directory or the tarball"
exit 1
fi
if [ ${#TARGET} -a ${#TARGET_DIR} = 0 ]; then
p "Setting TARGET_DIR based on TARGET ($TARGET)"
TARGET_DIR="${TARGET%%.tar.gz}"
fi
setup_vars
p "# Compiler output is in $OUT"
# build device library
[ ${#TARGET} -gt 0 ] && extract_tarball "${TARGET}"
build_openssl "$TARGET_DIR" "${LIBNAME_DEVICE}" "$IOS_PLATFORM" "$IOS_SDK"
if [ ${#TARGET} -gt 0 ]; then
clean_dir "${TARGET%%.tar.gz}"
extract_tarball "${TARGET}"
else
build_clean "$TARGET_DIR" "${LIBNAME_DEVICE}" "$IOS_PLATFORM" "$IOS_SDK"
fi
# build simulator library
build_openssl "$TARGET_DIR" "${LIBNAME_SIMULATOR}" "$IOS_SIMULATOR_PLATFORM" "$IOS_SIMULATOR_SDK"
clean_dir "${TARGET%%.tar.gz}"
# then, combine them into one..
# iterate over debug and non-debug versions
for d in "" "-d"; do
# lipo both release and debug
[ -d dist$d ] && /bin/rm -rf dist$d
mkdir dist$d
# check for directories being there
[ ! -d dist-${LIBNAME_DEVICE}$d -o ! -d dist-${LIBNAME_SIMULATOR}$d ] && continue
p "# Creating combined binary into directory dist$d"
(cd dist-${LIBNAME_DEVICE}$d; tar cf - . ) | (cd dist$d; tar xf -) >> $OUT 2>&1
for i in crypto ssl; do
x lipo \
-create dist-${LIBNAME_DEVICE}$d/lib/lib$i.a dist-${LIBNAME_SIMULATOR}$d/lib/lib$i.a \
-o dist$d/lib/lib$i$d.a
done
done
# /bin/rm -rf dist-${LIBNAME_SIMULATOR} dist-${LIBNAME_DEVICE}
# /bin/rm -rf dist-${LIBNAME_SIMULATOR}-d dist-${LIBNAME_DEVICE}-d
p "Now package is ready in 'dist' directory"
p "Verbose output is in $OUT"