-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·475 lines (356 loc) · 11.3 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/bin/bash
# Configurable globals
readonly MIN_IOS_VERSION="7.0"
# This is coming off a fork, arm support is not fully functional from the master repo
# See https://github.com/NativeScript/libffi
readonly LIBFFI_VERSION="bf64a497b4b1da1aae4f14d61761f420ca48cc03"
readonly GLIB_VERSION="2.47.1"
readonly GETTEXT_VERSION="0.19.6"
readonly ICONV_VERSION="1.14"
readonly ARCHS=(armv7 armv7s arm64 i386 x86_64)
# Calculated globals
readonly ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly DEPS_DIR="${ROOT_DIR}/dependencies"
readonly WORK_DIR="${ROOT_DIR}/work"
readonly LOGFILE="${ROOT_DIR}/build.log"
readonly LIPO="$(xcrun --sdk iphoneos -f lipo)"
readonly IPHONEOS_CC="$(xcrun --sdk iphoneos -f clang)"
readonly IPHONEOS_CXX="$(xcrun --sdk iphoneos -f clang++)"
readonly IPHONEOS_SDK=$(xcrun --sdk iphoneos --show-sdk-path)
readonly IPHONEOS_CFLAGS="-isysroot $IPHONEOS_SDK -miphoneos-version-min=$MIN_IOS_VERSION"
readonly IPHONESIM_CC="$(xcrun --sdk iphonesimulator -f clang)"
readonly IPHONESIM_CXX="$(xcrun --sdk iphonesimulator -f clang++)"
readonly IPHONESIM_SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)
readonly IPHONESIM_CFLAGS="-isysroot $IPHONESIM_SDK -mios-simulator-version-min=$MIN_IOS_VERSION"
log() {
local msg=$1
local now=$(date "+%Y-%m-%d% %H:%M:%S")
echo "[${now}] $msg" >> ${LOGFILE}
}
is_file() {
local file=$1
[[ -f $file ]]
}
is_dir() {
local dir=$1
[[ -d $dir ]]
}
is_empty() {
local var=$1
[[ -z $var ]]
}
is_universal() {
local path=$1
file $path | grep -q universal
}
is_iphone_arch() {
local arch=$1
[[ $arch == arm* ]]
}
fetch() {
local name=$1
local url=$2
local dest=$3
run "curl -s -L -o $dest $url" "Fetching $name"
}
run() {
local cmd=$1
local msg=$2
log "> ${cmd}"
echo -n "${msg}..."
$cmd >> $LOGFILE 2>&1 && echo "done." || {
log "FAILED with exit code $?"
echo "failed."
echo "Build Failed. See ${LOGFILE} for details."
exit 1
}
log "OK."
}
clean_up_prior_build() {
is_file "${LOGFILE}" \
&& run "rm -f ${LOGFILE}" "Removing old logfile"
is_dir "${WORK_DIR}" \
&& run "rm -rf ${WORK_DIR}" "Removing old work tree"
mkdir -p "${WORK_DIR}"
}
# Given an architecture (armv7, i386, etc.) in $1,
# echo out the correct autoconf host triplet
host_for_arch() {
local readonly arch=$1
case "$arch" in
armv*)
echo "arm-apple-darwin"
;;
arm64)
echo "aarch64-apple-darwin"
;;
x86_64)
echo "x86_64-apple-darwin"
;;
i386)
echo "i386-apple-darwin"
;;
*)
local msg="ERROR: Unable to determine architecture triplet for $arch"
log $msg
echo $msg
exit 1
esac
}
# Given an architecture (armv7, i386, etc.) in $1,
# export CFLAGS, CXXFLAGS, etc. appropriate for that arch
set_build_env_for_arch() {
local readonly arch=$1
case "$arch" in
arm*)
export CC=$IPHONEOS_CC
export CXX=$IPHONEOS_CXX
export CFLAGS="$IPHONEOS_CFLAGS"
;;
x86_64 | i386)
export CC=$IPHONESIM_CC
export CXX=$IPHONESIM_CXX
export CFLAGS="$IPHONESIM_CFLAGS"
;;
*)
local msg="ERROR: Unable to set environment variables for $arch"
log $msg
echo $msg
exit 1
esac
export PKG_CONFIG_PATH="${ROOT_DIR}/dependencies/libffi/${arch}/lib/pkgconfig"
export CFLAGS="$CFLAGS -arch ${arch}"
export CFLAGS="$CFLAGS -I${ROOT_DIR}/dependencies/gettext/${arch}/include"
export CFLAGS="$CFLAGS -I${ROOT_DIR}/dependencies/libiconv/${arch}/include"
export CXXFLAGS=$CFLAGS
export CPPFLAGS=$CFLAGS
export LDFLAGS="-L${ROOT_DIR}/dependencies/libffi/fat/lib"
export LDFLAGS="$LDFLAGS -L${ROOT_DIR}/dependencies/gettext/fat/lib"
export LDFLAGS="$LDFLAGS -L${ROOT_DIR}/dependencies/libiconv/fat/lib"
export LDFLAGS="$LDFLAGS -framework Foundation"
export ac_cv_func__NSGetEnviron=no
export glib_cv_stack_grows=no
export glib_cv_uscore=no
export ac_cv_func_posix_getgrgid_r=yes
export ac_cv_func_posix_getpwuid_r=yes
OLD_PATH=$PATH
export PATH="${ROOT_DIR}/dependencies/gettext/i386/bin:$PATH"
}
unset_build_env() {
unset \
PKG_CONFIG_PATH \
CC \
CXX \
CFLAGS \
CXXFLAGS \
CPPFLAGS \
LDFLAGS \
\
ac_cv_func__NSGetEnviron \
glib_cv_stack_grows \
glib_cv_uscore \
ac_cv_func_posix_getgrgid_r \
ac_cv_func_posix_getpwuid_r
export PATH=$OLD_PATH
}
# Given a pattern like dependencies/libffi/ARCH/lib/libffi.a
# fatify will locate the static libs for each arch in $ARCHS
# (replacing the magic string 'ARCH' with the appropriate architecture)
# and emit a fat binary in, e.g., dependencies/libffi/fat/lib/libffi.a
fatify() {
local pattern=$1
local libshortname=$(basename ${pattern})
local destfile=$(echo ${pattern} | sed s/ARCH/fat/)
local destdir=$(dirname ${destfile})
local tempdir="${ROOT_DIR}/.lipo_tmp"
is_dir "${tempdir}" \
&& run "rm -rf ${tempdir}" "Removing old universal prep temp directory"
run "mkdir ${tempdir}" "Creating temp directory for universal binary prep"
local archlist=""
for arch in "${ARCHS[@]}"; do
local path=$(echo $pattern | sed s/ARCH/$arch/)
local libname=${path}
if $(is_universal $path); then
local tmpname="${tempdir}/${arch}_${libshortname}"
local cmd="${LIPO} ${path} -thin ${arch} -output ${tmpname}"
run "${cmd}" "Extracting $arch from ${path}"
libname=${tmpname}
fi
archlist="${archlist} -arch ${arch} ${libname}"
done
run "mkdir -p ${destdir}" "Creating universal binary output directory ${destdir}"
local cmd="${LIPO} -create -output ${destfile} ${archlist}"
run "${cmd}" "Creating universal binary for $(basename ${destfile})"
}
build_iconv() {
local iconvarchive="${WORK_DIR}/iconv.tar.gz"
local iconvdir="${WORK_DIR}/libiconv-${ICONV_VERSION}"
local prefix="${DEPS_DIR}/libiconv"
echo "Beginning build of libiconv"
! is_file $iconvarchive \
&& fetch "libiconv" \
"http://ftp.gnu.org/pub/gnu/libiconv/libiconv-${ICONV_VERSION}.tar.gz" \
${iconvarchive}
for arch in "${ARCHS[@]}"; do
set_build_env_for_arch ${arch}
run "env" "Logging environment"
echo "Building libiconv for $arch"
is_dir $iconvdir \
&& run "rm -rf $iconvdir" " Removing old libiconv build directory"
cd "${WORK_DIR}"
run "tar xzf ${iconvarchive}" " Unpacking libiconv"
cd "${iconvdir}"
is_iphone_arch ${arch} \
&& sed -i '' 's/if defined __APPLE__ \&\& defined __MACH__/if defined __APPLE__ \&\& defined __MACH__ \&\& defined SKIPIT/' srclib/unistd.in.h
local host_type=$(host_for_arch ${arch})
if [ "$arch" == "arm64" ]; then
host_type="arm-apple-darwin"
fi
read -d '' cmd <<EOF
./configure \
--prefix=$prefix/${arch} \
--enable-static \
--disable-shared \
--host=${host_type}
EOF
run "${cmd}" " Configuring libiconv for ${arch}"
run "make -j12" " Building libiconv for ${arch}"
run "mkdir -p ${prefix}/${arch}" " Creating output directory for ${arch}"
run "make install" " Installing libiconv for ${arch} into ${prefix}/${arch}"
done
cd ${ROOT_DIR}
local iconvlibs=(libcharset.a libiconv.a)
for lib in "${iconvlibs[@]}"; do
fatify "dependencies/libiconv/ARCH/lib/${lib}"
done
unset_build_env
unset cmd
}
build_gettext() {
local gtarchive="${WORK_DIR}/gt.tar.gz"
local gtdir="${WORK_DIR}/gettext-${GETTEXT_VERSION}"
local prefix="${DEPS_DIR}/gettext"
echo "Beginning build of gettext"
! is_file $gtarchive \
&& fetch "gettext" \
"http://ftp.gnu.org/pub/gnu/gettext/gettext-${GETTEXT_VERSION}.tar.gz" \
${gtarchive}
for arch in "${ARCHS[@]}"; do
set_build_env_for_arch ${arch}
run "env" "Logging environment"
echo "Building gettext for $arch"
is_dir $gtdir \
&& run "rm -rf $gtdir" " Removing old gettext build directory"
cd "${WORK_DIR}"
run "tar xzf ${gtarchive}" " Unpacking gettext"
cd "${gtdir}"
read -d '' cmd <<EOF
./configure \
--prefix=$prefix/${arch} \
--enable-static \
--disable-shared \
--host=$(host_for_arch ${arch})
EOF
run "${cmd}" " Configuring gettext for ${arch}"
run "make -j12" " Building gettext for ${arch}"
run "mkdir -p ${prefix}/${arch}" " Creating output directory for ${arch}"
run "make install" " Installing gettext for ${arch} into ${prefix}/${arch}"
done
cd ${ROOT_DIR}
local gtlibs=(libasprintf.a libgettextpo.a libintl.a)
for lib in "${gtlibs[@]}"; do
fatify "dependencies/gettext/ARCH/lib/${lib}"
done
unset_build_env
unset cmd
}
build_glib() {
local glibzip="${WORK_DIR}/glib.zip"
local glibdir="${WORK_DIR}/glib-${GLIB_VERSION}"
local prefix="${DEPS_DIR}/glib"
echo "Beginning build of glib"
! is_file $glibzip \
&& fetch "glib" \
"https://github.com/GNOME/glib/archive/${GLIB_VERSION}.zip" \
${glibzip}
for arch in "${ARCHS[@]}"; do
set_build_env_for_arch ${arch}
run "env" "Logging environment"
echo "Building glib for $arch"
is_dir $glibdir \
&& run "rm -rf $glibdir" " Removing old glib build directory"
cd "${WORK_DIR}"
run "unzip ${glibzip}" " Unpacking glib"
cd "${glibdir}"
export NOCONFIGURE=true
run "./autogen.sh" " Bootstrapping autoconf for glib"
unset NOCONFIGURE
read -d '' cmd <<EOF
./configure \
--prefix=$prefix/${arch} \
--enable-static \
--disable-shared \
--host=$(host_for_arch ${arch}) \
--with-libiconv=gnu \
--disable-dtrace
EOF
run "${cmd}" " Configuring glib for ${arch}"
run "make -j12" " Building glib for ${arch}"
run "mkdir -p ${prefix}/${arch}" " Creating output directory for ${arch}"
run "make install" " Installing glib for ${arch} into ${prefix}/${arch}"
done
cd ${ROOT_DIR}
local glibs=(libgio-2.0.a libglib-2.0.a libgmodule-2.0.a libgobject-2.0.a libgthread-2.0.a)
for lib in "${glibs[@]}"; do
fatify "dependencies/glib/ARCH/lib/${lib}"
done
unset_build_env
unset cmd
}
build_libffi() {
local ffizip="${WORK_DIR}/ffi.zip"
local ffidir="${WORK_DIR}/libffi-${LIBFFI_VERSION}"
local prefix="${DEPS_DIR}/libffi"
echo "Beginning build of dependency: libffi"
! is_file $ffizip \
&& fetch "libffi" \
"https://github.com/NativeScript/libffi/archive/${LIBFFI_VERSION}.zip" \
${ffizip}
is_dir $ffidir \
&& run "rm -rf $ffidir" "Removing old libffi build directory"
cd "${WORK_DIR}"
run "unzip ${ffizip}" "Unpacking libffi"
cd "${ffidir}"
run "./autogen.sh" "Bootstrapping autoconf for libffi"
for arch in "${ARCHS[@]}"; do
set_build_env_for_arch ${arch}
echo "Building libffi for $arch"
is_file config.status \
&& run "make distclean" " Cleaning up from last run"
read -d '' cmd <<EOF
./configure \
--prefix=$prefix/${arch} \
--enable-static \
--disable-shared \
--host=$(host_for_arch ${arch})
EOF
run "${cmd}" " Configuring libffi for ${arch}"
run "make -j12" " Building libffi for ${arch}"
run "mkdir -p ${prefix}/${arch}" " Creating output directory for ${arch}"
run "make install" " Installing libffi for ${arch} into ${prefix}/${arch}"
done
cd ${ROOT_DIR}
fatify "dependencies/libffi/ARCH/lib/libffi.a"
unset_build_env
unset cmd
}
main() {
# clean_up_prior_build
log "Beginning build"
build_libffi
build_iconv
build_gettext
build_iconv
build_glib
}
main