forked from moritz-wundke/Boost-for-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-common.sh
660 lines (591 loc) · 15.1 KB
/
build-common.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#!/bin/sh
#
# Copyright (C) 2010 Mystic Tree Games
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: Moritz "Moss" Wundke ([email protected])
#
# <License>
#
# Adapted common build methods from NDK-Common.sh and prebuilt-common.sh
# from the Android NDK
#
# Current script name into PROGNAME
PROGNAME=`basename $0`
PROGDIR=`pwd`
## Logging support
##
VERBOSE=${VERBOSE-yes}
VERBOSE2=${VERBOSE2-no}
TMPLOG=
# Setup a log file where all log() and log2() output will be sent
#
# $1: log file path (optional)
#
setup_log_file ()
{
if [ -n "$1" ] ; then
TMPLOG="$1"
else
mkdir -p $PROGDIR/logs/
TMPLOG=$PROGDIR/logs/myst-log-$$.log
fi
rm -f $TMPLOG && touch $TMPLOG
echo "To follow build in another terminal, please use: tail -F $TMPLOG"
}
dump ()
{
if [ -n "$TMPLOG" ] ; then
echo "$@" >> $TMPLOG
fi
echo "$@"
}
log ()
{
if [ "$VERBOSE" = "yes" ] ; then
echo "$@"
else
if [ "$TMPLOG" ] ; then
echo "$@" >> $TMPLOG
fi
fi
}
log2 ()
{
if [ "$VERBOSE2" = "yes" ] ; then
echo "$@"
else
if [ -n "$TMPLOG" ] ; then
echo "$@" >> $TMPLOG
fi
fi
}
run ()
{
if [ "$VERBOSE" = "yes" ] ; then
echo "##### NEW COMMAND"
echo "$@"
$@ 2>&1
else
if [ -n "$TMPLOG" ] ; then
echo "##### NEW COMMAND" >> $TMPLOG
echo "$@" >> $TMPLOG
$@ 2>&1 | tee -a $TMPLOG
else
$@ > /dev/null 2>&1
fi
fi
}
## Utilities
##
# Generate a random temp directory
random_temp_directory ()
{
mktemp -d /tmp/myst-dir-XXXXXX
}
# return the value of a given named variable
# $1: variable name
#
# example:
# FOO=BAR
# BAR=ZOO
# echo `var_value $FOO`
# will print 'ZOO'
#
var_value ()
{
# find a better way to do that ?
eval echo "$`echo $1`"
}
# convert to uppercase
# assumes tr is installed on the platform ?
#
to_uppercase ()
{
echo $1 | tr "[:lower:]" "[:upper:]"
}
## Normalize OS and CPU
##
HOST_ARCH=`uname -m`
case "$HOST_ARCH" in
i?86) HOST_ARCH=x86
;;
amd64) HOST_ARCH=x86_64
;;
powerpc) HOST_ARCH=ppc
;;
esac
log2 "HOST_ARCH=$HOST_ARCH"
# at this point, the supported values for CPU are:
# x86
# x86_64
# ppc
#
# other values may be possible but haven't been tested
#
HOST_EXE=""
HOST_OS=`uname -s`
case "$HOST_OS" in
Darwin)
HOST_OS=darwin
;;
Linux)
# note that building 32-bit binaries on x86_64 is handled later
HOST_OS=linux
;;
FreeBsd) # note: this is not tested
HOST_OS=freebsd
;;
CYGWIN*|*_NT-*)
HOST_OS=windows
HOST_EXE=.exe
if [ "x$OSTYPE" = xcygwin ] ; then
HOST_OS=cygwin
fi
;;
esac
log2 "HOST_OS=$HOST_OS"
log2 "HOST_EXE=$HOST_EXE"
# at this point, the value of HOST_OS should be one of the following:
# linux
# darwin
# windows (MSys)
# cygwin
#
# Note that cygwin is treated as a special case because it behaves very differently
# for a few things. Other values may be possible but have not been tested
#
# define HOST_TAG as a unique tag used to identify both the host OS and CPU
# supported values are:
#
# linux-x86
# linux-x86_64
# darwin-x86
# darwin-ppc
# windows
#
# other values are possible but were not tested.
#
compute_host_tag ()
{
case "$HOST_OS" in
windows|cygwin)
HOST_TAG="windows"
;;
*) HOST_TAG="${HOST_OS}-${HOST_ARCH}"
esac
log2 "HOST_TAG=$HOST_TAG"
}
compute_host_tag
# Compute the number of host CPU cores an HOST_NUM_CPUS
#
case "$HOST_OS" in
linux)
HOST_NUM_CPUS=`cat /proc/cpuinfo | grep processor | wc -l`
;;
darwin|freebsd)
HOST_NUM_CPUS=`sysctl -n hw.ncpu`
;;
windows|cygwin)
HOST_NUM_CPUS=$NUMBER_OF_PROCESSORS
;;
*) # let's play safe here
HOST_NUM_CPUS=1
esac
log2 "HOST_NUM_CPUS=$HOST_NUM_CPUS"
# If BUILD_NUM_CPUS is not already defined in your environment,
# define it as the double of HOST_NUM_CPUS. This is used to
# run Make commends in parralles, as in 'make -j$BUILD_NUM_CPUS'
#
if [ -z "$BUILD_NUM_CPUS" ] ; then
BUILD_NUM_CPUS=`expr $HOST_NUM_CPUS \* 2`
fi
log2 "BUILD_NUM_CPUS=$BUILD_NUM_CPUS"
# Various probes are going to need to run a small C program
TMPC=/tmp/myst-$$-test.c
TMPO=/tmp/myst-$$-test.o
TMPE=/tmp/myst-$$-test$EXE
TMPL=/tmp/myst-$$-test.log
# cleanup temporary files
clean_temp ()
{
rm -f $TMPC $TMPO $TMPL $TMPE
}
# cleanup temp files then exit with an error
clean_exit ()
{
clean_temp
exit 1
}
pattern_match ()
{
echo "$2" | grep -q -E -e "$1"
}
# Let's check that we have a working md5sum here
check_md5sum ()
{
A_MD5=`echo "A" | md5sum | cut -d' ' -f1`
if [ "$A_MD5" != "bf072e9119077b4e76437a93986787ef" ] ; then
echo "Please install md5sum on this machine"
exit 2
fi
}
# Find if a given shell program is available.
#
# $1: variable name
# $2: program name
#
# Result: set $1 to the full path of the corresponding command
# or to the empty/undefined string if not available
#
find_program ()
{
eval $1=`command -v $2`
}
prepare_download ()
{
find_program CMD_WGET wget
find_program CMD_CURL curl
find_program CMD_SCRP scp
}
# Download a file with either 'curl', 'wget' or 'scp'
#
# $1: source URL (e.g. http://foo.com, ssh://blah, /some/path)
# $2: target file
download_file ()
{
# Is this HTTP, HTTPS or FTP ?
if pattern_match "^(http|https|ftp):.*" "$1"; then
if [ -n "$CMD_WGET" ] ; then
run $CMD_WGET -O $2 $1
elif [ -n "$CMD_CURL" ] ; then
run $CMD_CURL -L -o $2 $1
else
echo "Please install wget or curl on this machine"
exit 1
fi
return
fi
# Is this SSH ?
# Accept both ssh://<path> or <machine>:<path>
#
if pattern_match "^(ssh|[^:]+):.*" "$1"; then
if [ -n "$CMD_SCP" ] ; then
scp_src=`echo $1 | sed -e s%ssh://%%g`
run $CMD_SCP $scp_src $2
else
echo "Please install scp on this machine"
exit 1
fi
return
fi
# Is this a file copy ?
# Accept both file://<path> or /<path>
#
if pattern_match "^(file://|/).*" "$1"; then
cp_src=`echo $1 | sed -e s%^file://%%g`
run cp -f $cp_src $2
return
fi
}
# Return the maximum length of a series of strings
#
# Usage: len=`max_length <string1> <string2> ...`
#
max_length ()
{
echo "$@" | tr ' ' '\n' | awk 'BEGIN {max=0} {len=length($1); if (len > max) max=len} END {print max}'
}
# Translate dashes to underscores
# Usage: str=`dashes_to_underscores <values>`
dashes_to_underscores ()
{
echo $@ | tr '-' '_'
}
# Translate underscores to dashes
# Usage: str=`underscores_to_dashes <values>`
underscores_to_dashes ()
{
echo $@ | tr '_' '-'
}
#-----------------------------------------------------------------------
# OPTION PROCESSING
#-----------------------------------------------------------------------
# We recognize the following option formats:
#
# -f
# --flag
#
# -s<value>
# --setting=<value>
#
# NOTE: We translate '-' into '_' when storing the options in global
# variables
#
OPTIONS=""
OPTION_FLAGS=""
OPTION_SETTINGS=""
# Set a given option attribute
# $1: option name
# $2: option attribute
# $3: attribute value
#
option_set_attr ()
{
eval OPTIONS_$1_$2=\"$3\"
}
# Get a given option attribute
# $1: option name
# $2: option attribute
#
option_get_attr ()
{
echo `var_value OPTIONS_$1_$2`
}
# Determine optional variable value
# $1: final variable name
# $2: option variable name
# $3: small description for the option
fix_option ()
{
if [ -n "$2" ] ; then
eval $1="$2"
log "Using specific $3: $2"
else
log "Using default $3: `var_value $1`"
fi
}
# Register a new option
# $1: option
# $2: name of function that will be called when the option is parsed
# $3: small abstract for the option
# $4: optional. default value
#
register_option ()
{
local optname optvalue opttype optlabel
optlabel=
optname=
optvalue=
opttype=
while [ -n "1" ] ; do
# Check for something like --setting=<value>
echo "$1" | grep -q -E -e '^--[^=]+=<.+>$'
if [ $? = 0 ] ; then
optlabel=`expr -- "$1" : '\(--[^=]*\)=.*'`
optvalue=`expr -- "$1" : '--[^=]*=\(<.*>\)'`
opttype="long_setting"
break
fi
# Check for something like --flag
echo "$1" | grep -q -E -e '^--[^=]+$'
if [ $? = 0 ] ; then
optlabel="$1"
opttype="long_flag"
break
fi
# Check for something like -f<value>
echo "$1" | grep -q -E -e '^-[A-Za-z0-9]<.+>$'
if [ $? = 0 ] ; then
optlabel=`expr -- "$1" : '\(-.\).*'`
optvalue=`expr -- "$1" : '-.\(<.+>\)'`
opttype="short_setting"
break
fi
# Check for something like -f
echo "$1" | grep -q -E -e '^-.$'
if [ $? = 0 ] ; then
optlabel="$1"
opttype="short_flag"
break
fi
echo "ERROR: Invalid option format: $1"
echo " Check register_option call"
exit 1
done
log "new option: type='$opttype' name='$optlabel' value='$optvalue'"
optname=`dashes_to_underscores $optlabel`
OPTIONS="$OPTIONS $optname"
OPTIONS_TEXT="$OPTIONS_TEXT $1"
option_set_attr $optname label "$optlabel"
option_set_attr $optname otype "$opttype"
option_set_attr $optname value "$optvalue"
option_set_attr $optname text "$1"
option_set_attr $optname funcname "$2"
option_set_attr $optname abstract "$3"
option_set_attr $optname default "$4"
}
# Print the help, including a list of registered options for this program
# Note: Assumes PROGRAM_PARAMETERS and PROGRAM_DESCRIPTION exist and
# correspond to the parameters list and the program description
#
print_help ()
{
local opt text abstract default
echo "Usage: $PROGNAME [options] $PROGRAM_PARAMETERS"
echo ""
if [ -n "$PROGRAM_DESCRIPTION" ] ; then
echo "$PROGRAM_DESCRIPTION"
echo ""
fi
echo "Valid options (defaults are in brackets):"
echo ""
maxw=`max_length "$OPTIONS_TEXT"`
AWK_SCRIPT=`echo "{ printf \"%-${maxw}s\", \\$1 }"`
for opt in $OPTIONS; do
text=`option_get_attr $opt text | awk "$AWK_SCRIPT"`
abstract=`option_get_attr $opt abstract`
default=`option_get_attr $opt default`
if [ -n "$default" ] ; then
echo " $text $abstract [$default]"
else
echo " $text $abstract"
fi
done
echo ""
}
option_panic_no_args ()
{
echo "ERROR: Option '$1' does not take arguments. See --help for usage."
exit 1
}
option_panic_missing_arg ()
{
echo "ERROR: Option '$1' requires an argument. See --help for usage."
exit 1
}
extract_parameters ()
{
local opt optname otype value name fin funcname
PARAMETERS=""
while [ -n "$1" ] ; do
# If the parameter does not begin with a dash
# it is not an option.
param=`expr -- "$1" : '^\([^\-].*\)$'`
if [ -n "$param" ] ; then
if [ -z "$PARAMETERS" ] ; then
PARAMETERS="$1"
else
PARAMETERS="$PARAMETERS $1"
fi
shift
continue
fi
while [ -n "1" ] ; do
# Try to match a long setting, i.e. --option=value
opt=`expr -- "$1" : '^\(--[^=]*\)=.*$'`
if [ -n "$opt" ] ; then
otype="long_setting"
value=`expr -- "$1" : '^--[^=]*=\(.*\)$'`
break
fi
# Try to match a long flag, i.e. --option
opt=`expr -- "$1" : '^\(--.*\)$'`
if [ -n "$opt" ] ; then
otype="long_flag"
value=
break
fi
# Try to match a short setting, i.e. -o<value>
opt=`expr -- "$1" : '^\(-[A-Za-z0-9]\)..*$'`
if [ -n "$opt" ] ; then
otype="short_setting"
value=`expr -- "$1" : '^-.\(.*\)$'`
break
fi
# Try to match a short flag, i.e. -o
opt=`expr -- "$1" : '^\(-.\)$'`
if [ -n "$opt" ] ; then
otype="short_flag"
value=
break
fi
echo "ERROR: Unknown option '$1'. Use --help for list of valid values."
exit 1
done
#echo "Found opt='$opt' otype='$otype' value='$value'"
name=`dashes_to_underscores $opt`
found=0
for xopt in $OPTIONS; do
if [ "$name" != "$xopt" ] ; then
continue
fi
# Check that the type is correct here
#
# This also allows us to handle -o <value> as -o<value>
#
xotype=`option_get_attr $name otype`
if [ "$otype" != "$xotype" ] ; then
case "$xotype" in
"short_flag")
option_panic_no_args $opt
;;
"short_setting")
if [ -z "$2" ] ; then
option_panic_missing_arg $opt
fi
value="$2"
shift
;;
"long_flag")
option_panic_no_args $opt
;;
"long_setting")
option_panic_missing_arg $opt
;;
esac
fi
found=1
break
break
done
if [ "$found" = "0" ] ; then
echo "ERROR: Unknown option '$opt'. See --help for usage."
exit 1
fi
# Launch option-specific function, value, if any as argument
eval `option_get_attr $name funcname` \"$value\"
shift
done
# Change log out put if requested
if [ "x$OPTION_OUTPUT" != "x" ] ; then
setup_log_file $OPTION_OUTPUT
else
setup_log_file
fi
}
do_option_help ()
{
print_help
exit 0
}
VERBOSE=no
VERBOSE2=no
do_option_verbose ()
{
if [ $VERBOSE = "yes" ] ; then
VERBOSE2=yes
else
VERBOSE=yes
fi
}
OPTION_OUTPUT=
do_logpath () { OPTION_OUTPUT=$1; }
do_progress_bar()
{
OPTION_PROGRESS="yes"
}
register_option "--help" do_option_help "Print this help."
register_option "--verbose" do_option_verbose "Enable verbose mode."
register_option "--output=<path>" do_logpath "Specify specific log output path (only terminal output by default)"
register_option "--progress" do_progress_bar "Enable extraction progress bar"