aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/tools/prebuilt-common.sh
blob: 4b1620103d1bb569933095f4b61b3d460278795a (plain)
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
# Common functions for all prebuilt-related scripts
# This is included/sourced by other scripts
#

# ensure stable sort order
export LC_ALL=C

# NDK_BUILDTOOLS_PATH should point to the directory containing
# this script. If it is not defined, assume that this is one of
# the scripts in the same directory that sourced this file.
#
if [ -z "$NDK_BUILDTOOLS_PATH" ]; then
    NDK_BUILDTOOLS_PATH=$(dirname $0)
    if [ ! -f "$NDK_BUILDTOOLS_PATH/prebuilt-common.sh" ]; then
        echo "INTERNAL ERROR: Please define NDK_BUILDTOOLS_PATH to point to \$NDK/build/tools"
        exit 1
    fi
fi

# Warn if /bin/sh isn't bash.
if [ -z "$BASH_VERSION" ] ; then
    echo "WARNING: The shell running this script isn't bash.  Although we try to avoid bashism in scripts, things can happen."
fi

NDK_BUILDTOOLS_ABSPATH=$(cd $NDK_BUILDTOOLS_PATH && pwd)

. $NDK_BUILDTOOLS_PATH/ndk-common.sh
. $NDK_BUILDTOOLS_PATH/dev-defaults.sh

# Given an input string of the form <foo>-<bar>-<version>, where
# <version> can be <major>.<minor>, extract <major>
extract_version ()
{
    echo $1 | tr '-' '\n' | tail -1
}

# $1: versioned name (e.g. arm-linux-androideabi-4.8)
# Out: major version (e.g. 4)
#
# Examples:  arm-linux-androideabi-4.4.3 -> 4
#            gmp-0.81 -> 0
#
extract_major_version ()
{
    local RET=$(extract_version $1 | cut -d . -f 1)
    RET=${RET:-0}
    echo $RET
}

# Same as extract_major_version, but for the minor version number
# $1: versioned named
# Out: minor version
#
extract_minor_version ()
{
    local RET=$(extract_version $1 | cut -d . -f 2)
    RET=${RET:-0}
    echo $RET
}

# Compare two version numbers and only succeeds if the first one is
# greater than or equal to the second one.
#
# $1: first version (e.g. 4.9)
# $2: second version (e.g. 4.8)
#
# Example: version_is_at_least 4.9 4.8 --> success
#
version_is_at_least ()
{
    local A_MAJOR A_MINOR B_MAJOR B_MINOR
    A_MAJOR=$(extract_major_version $1)
    B_MAJOR=$(extract_major_version $2)

    if [ $A_MAJOR -lt $B_MAJOR ]; then
        return 1
    elif [ $A_MAJOR -gt $B_MAJOR ]; then
        return 0
    fi

    # We have A_MAJOR == B_MAJOR here

    A_MINOR=$(extract_minor_version $1)
    B_MINOR=$(extract_minor_version $2)

    if [ $A_MINOR -lt $B_MINOR ]; then
        return 0
    else
        return 1
    fi
}

#====================================================
#
#  UTILITY FUNCTIONS
#
#====================================================

# 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 '_' '-'
}

# Translate commas to spaces
# Usage: str=`commas_to_spaces <list>`
commas_to_spaces ()
{
    echo "$@" | tr ',' ' '
}

# Translate spaces to commas
# Usage: list=`spaces_to_commas <string>`
spaces_to_commas ()
{
    echo "$@" | tr ' ' ','
}

# Remove trailing path of a path
# $1: path
remove_trailing_slash () {
    echo ${1%%/}
}

# Reverse a file path directory
# foo -> .
# foo/bar -> ..
# foo/bar/zoo -> ../..
reverse_path ()
{
    local path cur item
    path=${1%%/} # remove trailing slash
    cur="."
    if [ "$path" != "." ] ; then
        for item in $(echo "$path" | tr '/' ' '); do
            cur="../$cur"
        done
    fi
    echo ${cur%%/.}
}

# test_reverse_path ()
# {
#     rr=`reverse_path $1`
#     if [ "$rr" != "$2" ] ; then
#         echo "ERROR: reverse_path '$1' -> '$rr' (expected '$2')"
#     fi
# }
#
# test_reverse_path . .
# test_reverse_path ./ .
# test_reverse_path foo ..
# test_reverse_path foo/ ..
# test_reverse_path foo/bar ../..
# test_reverse_path foo/bar/ ../..
# test_reverse_path foo/bar/zoo ../../..
# test_reverse_path foo/bar/zoo/ ../../..

# Sort a space-separated list and remove duplicates
# $1+: slist
# Output: new slist
sort_uniq ()
{
    local RET
    RET=$(echo "$@" | tr ' ' '\n' | sort -u)
    echo $RET
}

# Return the list of all regular files under a given directory
# $1: Directory path
# Output: list of files, relative to $1
list_files_under ()
{
    if [ -d "$1" ]; then
        (cd $1 && find . -type f | sed -e "s!./!!" | sort -u)
    else
        echo ""
    fi
}

# Returns all words in text that do not match any of the pattern
# $1: pattern
# $2: text
filter_out ()
{
    local PATTERN="$1"
    local TEXT="$2"
    for pat in $PATTERN; do
        pat=$"${pat//\//\\/}"
        TEXT=$(echo $TEXT | sed -e 's/'$pat' //g' -e 's/'$pat'$//g')
    done
    echo $TEXT
}

# Assign a value to a variable
# $1: Variable name
# $2: Value
var_assign ()
{
    eval $1=\"$2\"
}

#====================================================
#
#  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`
}

# Register a new option
# $1: option
# $2: small abstract for the option
# $3: optional. default value
#
register_option_internal ()
{
    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 abstract "$2"
    option_set_attr $optname default "$3"
}

# Register a new option with a function callback.
#
# $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
    register_option_internal "$1" "$3" "$4"
    option_set_attr $optname funcname "$2"
}

# Register a new option with a variable store
#
# $1: option
# $2: name of variable that will be set by this option
# $3: small abstract for the option
#
# NOTE: The current value of $2 is used as the default
#
register_var_option ()
{
    local optname optvalue opttype optlabel
    register_option_internal "$1" "$3" "`var_value $2`"
    option_set_attr $optname varname "$2"
}


MINGW=no
DARWIN=no
do_mingw_option ()
{
    if [ "$DARWIN" = "yes" ]; then
        echo "Can not have both --mingw and --darwin"
        exit 1
    fi
    MINGW=yes;
}
do_darwin_option ()
{
    if [ "$MINGW" = "yes" ]; then
        echo "Can not have both --mingw and --darwin"
        exit 1
    fi
    DARWIN=yes; 
}

register_canadian_option ()
{
    if [ "$HOST_OS" = "linux" ] ; then
        register_option "--mingw" do_mingw_option "Generate windows binaries on Linux."
        register_option "--darwin" do_darwin_option "Generate darwin binaries on Linux."
    fi
}

TRY64=no
do_try64_option () { TRY64=yes; }

register_try64_option ()
{
    register_option "--try-64" do_try64_option "Generate 64-bit only binaries."
}


register_jobs_option ()
{
    NUM_JOBS=$BUILD_NUM_CPUS
    register_var_option "-j<number>" NUM_JOBS "Use <number> parallel build jobs"
}

# 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="yes"
                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="yes"
                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
        # Set variable or launch option-specific function.
        varname=`option_get_attr $name varname`
        if [ -n "$varname" ] ; then
            eval ${varname}=\"$value\"
        else
            eval `option_get_attr $name funcname` \"$value\"
        fi
        shift
    done
}

do_option_help ()
{
    print_help
    exit 0
}

VERBOSE=no
do_option_verbose ()
{
    VERBOSE=yes
}

DRYRUN=no
do_option_dryrun ()
{
    DRYRUN=yes
}

register_option "--help"          do_option_help     "Print this help."
register_option "--verbose"       do_option_verbose  "Enable verbose mode."
register_option "--dryrun"        do_option_dryrun   "Set to dryrun mode."

#====================================================
#
#  TOOLCHAIN AND ABI PROCESSING
#
#====================================================

# 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
}


# If SYSROOT is empty, check that $1/$2 contains a sysroot
# and set the variable to it.
#
# $1: sysroot path
# $2: platform/arch suffix
check_sysroot ()
{
    if [ -z "$SYSROOT" ] ; then
        log "Probing directory for sysroot: $1/$2"
        if [ -d $1/$2 ] ; then
            SYSROOT=$1/$2
        fi
    fi
}

# Determine sysroot
# $1: Option value (or empty)
#
fix_sysroot ()
{
    if [ -n "$1" ] ; then
        eval SYSROOT="$1"
        log "Using specified sysroot: $1"
    else
        SYSROOT_SUFFIX=$PLATFORM/arch-$ARCH
        SYSROOT=
        check_sysroot $NDK_DIR/platforms $SYSROOT_SUFFIX
        check_sysroot $ANDROID_NDK_ROOT/platforms $SYSROOT_SUFFIX
        check_sysroot `dirname $ANDROID_NDK_ROOT`/development/ndk/platforms $SYSROOT_SUFFIX

        if [ -z "$SYSROOT" ] ; then
            echo "ERROR: Could not find NDK sysroot path for $SYSROOT_SUFFIX."
            echo "       Use --sysroot=<path> to specify one."
            exit 1
        fi
    fi

    if [ ! -f $SYSROOT/usr/include/stdlib.h ] ; then
        echo "ERROR: Invalid sysroot path: $SYSROOT"
        echo "       Use --sysroot=<path> to indicate a valid one."
        exit 1
    fi
}

# Check for the availability of a compatibility SDK in Darwin
# this can be used to generate binaries compatible with either Tiger or
# Leopard.
#
# $1: SDK root path
# $2: Optional MacOS X minimum version (e.g. 10.5)
DARWIN_MINVER=10.6
check_darwin_sdk ()
{
    local MACSDK="$1"
    local MINVER=$2

    if [ -z "$MINVER" ] ; then
        # expect SDK root path ended up with either MacOSX##.#.sdk or MacOSX##.#u.sdk
        MINVER=${MACSDK##*MacOSX}
        MINVER=${MINVER%%.sdk*}
        if [ "$MINVER" = "10.4u" ]; then
            MINVER=10.4
        fi
    fi
    if [ -d "$MACSDK" ] ; then
        HOST_CFLAGS=$HOST_CFLAGS" -isysroot $MACSDK -mmacosx-version-min=$MINVER -DMACOSX_DEPLOYMENT_TARGET=$MINVER"
        HOST_LDFLAGS=$HOST_LDFLAGS" -Wl,-syslibroot,$MACSDK -mmacosx-version-min=$MINVER"
        DARWIN_MINVER=$MINVER
        return 0  # success
    fi
    return 1
}

# Probe Darwin SDK in specified diectory $DARWIN_SYSROOT, or
# /Developer/SDKs/MacOSX10.6.sdk
#
probe_darwin_sdk ()
{
    if [ -n "$DARWIN_SYSROOT" ]; then
        if check_darwin_sdk "$DARWIN_SYSROOT"; then
            log "Use darwin sysroot $DARWIN_SYSROOT"
        else
            echo "darwin sysroot $DARWIN_SYSROOT is not valid"
            exit 1
        fi
    elif check_darwin_sdk /Developer/SDKs/MacOSX10.6.sdk 10.6; then
        log "Generating Snow Leopard-compatible binaries!"
    else
        local version=`sw_vers -productVersion`
        log "Generating $version-compatible binaries!"
    fi
}

handle_canadian_build ()
{
    HOST_EXE=
    if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
        case $HOST_TAG in
            linux-*)
                ;;
            *)
                echo "ERROR: Can only enable --mingw or --darwin on Linux platforms !"
                exit 1
                ;;
        esac
        if [ "$MINGW" = "yes" ] ; then
            # NOTE: Use x86_64-pc-mingw32msvc or i586-pc-mingw32msvc because wrappers are generated
            #       using these names
            if [ "$TRY64" = "yes" ]; then
                ABI_CONFIGURE_HOST=x86_64-pc-mingw32msvc
                HOST_TAG=windows-x86_64
            else
                ABI_CONFIGURE_HOST=i586-pc-mingw32msvc
                HOST_TAG=windows
            fi
            HOST_OS=windows
            HOST_EXE=.exe
        else
            if [ "$TRY64" = "yes" ]; then
                ABI_CONFIGURE_HOST=x86_64-apple-darwin
                HOST_TAG=darwin-x86_64
            else
                ABI_CONFIGURE_HOST=i686-apple-darwin
                HOST_TAG=darwin-x86
            fi
            HOST_OS=darwin
        fi
    fi
}

# Find mingw toolchain
#
# Set MINGW_GCC to the found mingw toolchain
#
find_mingw_toolchain ()
{
    if [ "$DEBIAN_NAME" -a "$BINPREFIX" -a "$MINGW_GCC" ]; then
        return
    fi
    # IMPORTANT NOTE: binutils 2.21 requires a cross toolchain named
    # i585-pc-mingw32msvc-gcc, or it will fail its configure step late
    # in the toolchain build. Note that binutils 2.19 can build properly
    # with i585-mingw32mvsc-gcc, which is the name used by the 'mingw32'
    # toolchain install on Debian/Ubuntu.
    #
    # To solve this dilemma, we create a wrapper toolchain named
    # i586-pc-mingw32msvc-gcc that really calls i586-mingw32msvc-gcc,
    # this works with all versions of binutils.
    #
    # We apply the same logic to the 64-bit Windows cross-toolchain
    #
    # Fedora note: On Fedora it's x86_64-w64-mingw32- or i686-w64-mingw32-
    # On older Fedora it's 32-bit only and called i686-pc-mingw32-
    # so we just add more prefixes to the list to check.
    if [ "$HOST_ARCH" = "x86_64" -a "$TRY64" = "yes" ]; then
        BINPREFIX=x86_64-pc-mingw32msvc-
        #BINPREFIXLST="x86_64-w64-mingw32- x86_64-pc-mingw32msvc- amd64-mingw32msvc-"
        MINGW_GCC=x86_64-w64-mingw32-gcc
        DEBIAN_NAME=mingw64
        MINGW_PATH="$ANDROID_NDK_ROOT/../prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/bin"
    else
        # we are trying 32 bit anyway, so forcing it to avoid build issues
        force_32bit_binaries
        BINPREFIX=i586-pc-mingw32msvc-
        #BINPREFIXLST="i686-w64-mingw32- i586-pc-mingw32msvc- i686-pc-mingw32- i586-mingw32msvc-"
        MINGW_GCC=i686-w64-mingw32-gcc
        DEBIAN_NAME=mingw32
        MINGW_PATH="$ANDROID_NDK_ROOT/../prebuilts/gcc/linux-x86/host/i686-w64-mingw32-4.8/bin"
    fi

    export PATH="$MINGW_PATH:$PATH"
    dump "Will use mingw toolchain in: $MINGW_PATH"


    # Scan $BINPREFIXLST list to find installed mingw toolchain. It will be
    # wrapped later with $BINPREFIX.
    #for i in $BINPREFIXLST; do
    #    find_program MINGW_GCC ${i}gcc
    #    if [ -n "$MINGW_GCC" ]; then
    #        dump "Found mingw toolchain: $MINGW_GCC"
    #        break
    #    fi
    #done
}

# Check there is a working cross-toolchain installed.
#
# $1: install directory for mingw/darwin wrapper toolchain
#
prepare_canadian_toolchain ()
{
    if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ]; then
        return
    fi
    CROSS_GCC=
    if [ "$MINGW" = "yes" ]; then
        find_mingw_toolchain
        if [ -z "$MINGW_GCC" ]; then
            echo "ERROR: Could not find in your PATH any of:"
            for i in $BINPREFIXLST; do echo "   ${i}gcc"; done
            echo "Please install the corresponding cross-toolchain and re-run this script"
            echo "TIP: On Debian or Ubuntu, try: sudo apt-get install $DEBIAN_NAME"
            exit 1
        fi
        CROSS_GCC=$MINGW_GCC
    else
        if [ -z "$DARWIN_TOOLCHAIN" ]; then
            echo "Please set DARWIN_TOOLCHAIN to darwin cross-toolchain"
            exit 1
        fi
        if [ ! -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
            echo "darwin cross-toolchain $DARWIN_TOOLCHAIN-gcc doesn't exist"
            exit 1
        fi
        if [ "$HOST_ARCH" = "x86_64" -a "$TRY64" = "yes" ]; then
            BINPREFIX=x86_64-apple-darwin-
            DEBIAN_NAME=darwin64
            HOST_CFLAGS=$HOST_CFLAGS" -m64"
        else
            force_32bit_binaries
            BINPREFIX=i686-apple-darwin-
            DEBIAN_NAME=darwin32
            HOST_CFLAGS=$HOST_CFLAGS" -m32"
        fi
        CROSS_GCC=${DARWIN_TOOLCHAIN}-gcc
        probe_darwin_sdk
    fi

    # Create a wrapper toolchain, and prepend its dir to our PATH
    CROSS_WRAP_DIR="$1"/$DEBIAN_NAME-wrapper
    rm -rf "$CROSS_WRAP_DIR"
    mkdir -p "$CROSS_WRAP_DIR"

    if [ "$DARWIN" = "yes" ] ; then
        cat > "$CROSS_WRAP_DIR/sw_vers" <<EOF
#!/bin/sh
# Tiny utility for the real sw_vers some Makefiles need
case \$1 in
    -productVersion)
        echo $DARWIN_MINVER
        ;;
    *)
        echo "ERROR: Unknown switch \$1"
        exit 1
esac
EOF
    chmod 0755 "$CROSS_WRAP_DIR/sw_vers"
    fi

    DST_PREFIX=${CROSS_GCC%gcc}
    if [ "$NDK_CCACHE" ]; then
        DST_PREFIX="$NDK_CCACHE $DST_PREFIX"
    fi
    $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=$BINPREFIX --dst-prefix="$DST_PREFIX" "$CROSS_WRAP_DIR" \
        --cflags="$HOST_CFLAGS" --cxxflags="$HOST_CFLAGS" --ldflags="$HOST_LDFLAGS"
    # generate wrappers for BUILD toolchain
    # this is required for mingw/darwin build to avoid tools canadian cross configuration issues
    # 32-bit BUILD toolchain
    LEGACY_TOOLCHAIN_DIR="$ANDROID_NDK_ROOT/../prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.11-4.8"
    $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=i386-linux-gnu- \
            --cflags="-m32" --cxxflags="-m32" --ldflags="-m elf_i386" --asflags="--32" \
            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/x86_64-linux-" "$CROSS_WRAP_DIR"
    $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=i386-pc-linux-gnu- \
            --cflags="-m32" --cxxflags="-m32" --ldflags="-m elf_i386" --asflags="--32" \
            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/x86_64-linux-" "$CROSS_WRAP_DIR"
    # 64-bit BUILD toolchain.  libbfd is still built in 32-bit.
    $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=x86_64-linux-gnu- \
            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/x86_64-linux-" "$CROSS_WRAP_DIR"
    $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=x86_64-pc-linux-gnu- \
            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/x86_64-linux-" "$CROSS_WRAP_DIR"
    fail_panic "Could not create $DEBIAN_NAME wrapper toolchain in $CROSS_WRAP_DIR"

    export PATH=$CROSS_WRAP_DIR:$PATH
    dump "Using $DEBIAN_NAME wrapper: $CROSS_WRAP_DIR/${BINPREFIX}gcc"
}

handle_host ()
{
    if [ "$TRY64" != "yes" ]; then
        force_32bit_binaries  # to modify HOST_TAG and others
        HOST_BITS=32
    fi
    handle_canadian_build
}

setup_ccache ()
{
    # Support for ccache compilation
    # We can't use this here when building Windows/darwin binaries on Linux with
    # binutils 2.21, because defining CC/CXX in the environment makes the
    # configure script fail later
    #
    if [ "$NDK_CCACHE" -a "$MINGW" != "yes" -a "$DARWIN" != "yes" ]; then
        NDK_CCACHE_CC=$CC
        NDK_CCACHE_CXX=$CXX
        # Unfortunately, we can just do CC="$NDK_CCACHE $CC" because some
        # configure scripts are not capable of dealing with this properly
        # E.g. the ones used to rebuild the GCC toolchain from scratch.
        # So instead, use a wrapper script
        CC=$NDK_BUILDTOOLS_ABSPATH/ndk-ccache-gcc.sh
        CXX=$NDK_BUILDTOOLS_ABSPATH/ndk-ccache-g++.sh
        export NDK_CCACHE_CC NDK_CCACHE_CXX
        log "Using ccache compilation"
        log "NDK_CCACHE_CC=$NDK_CCACHE_CC"
        log "NDK_CCACHE_CXX=$NDK_CCACHE_CXX"
    fi
}

prepare_common_build ()
{
    if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ]; then
        if [ "$TRY64" = "yes" ]; then
            HOST_BITS=64
        else
            HOST_BITS=32
        fi
        if [ "$MINGW" = "yes" ]; then
            log "Generating $HOST_BITS-bit Windows binaries"
        else
            log "Generating $HOST_BITS-bit Darwin binaries"
        fi
        # Do *not* set CC and CXX when building the Windows/Darwin binaries in canadian build.
        # Otherwise, the GCC configure/build script will mess that Canadian cross
        # build in weird ways. Instead we rely on the toolchain detected or generated
        # previously in prepare_canadian_toolchain.
        unset CC CXX
        return
    fi

    # On Linux, detect our legacy-compatible toolchain when in the Android
    # source tree, and use it to force the generation of glibc-2.7 compatible
    # binaries.
    #
    # We only do this if the CC variable is not defined to a given value
    if [ -z "$CC" ]; then
        LEGACY_TOOLCHAIN_DIR=
        if [ "$HOST_OS" = "linux" ]; then
            LEGACY_TOOLCHAIN_DIR="$ANDROID_NDK_ROOT/../prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.11-4.8/bin"
            LEGACY_TOOLCHAIN_PREFIX="$LEGACY_TOOLCHAIN_DIR/x86_64-linux-"
        elif [ "$HOST_OS" = "darwin" ]; then
            local GCCVER=4.9.3
            local LLVMVER=3.7.0
            local GCCDIR="$ANDROID_NDK_ROOT/../prebuilts/gcc/darwin-x86/host/x86_64-apple-darwin-$GCCVER"
            local LLVMDIR="$ANDROID_NDK_ROOT/../prebuilts/clang/darwin-x86/host/x86_64-apple-darwin-$LLVMVER"

            LEGACY_TOOLCHAIN_DIR="$GCCDIR/bin"
            LEGACY_TOOLCHAIN_PREFIX="$LEGACY_TOOLCHAIN_DIR/"

            # For compilation LLDB's Objective-C++ sources we need use clang++, since g++ have a bug
            # not distinguishing between Objective-C call and definition of C++11 lambda:
            # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57607
            # To workaround this, we're using prebuilt clang++
            # with includes from our g++, to keep binary compatibility of produced code
            CXXINC="$LEGACY_TOOLCHAIN_DIR/../include/c++/$GCCVER"
            CXXBITSINC="$CXXINC/x86_64-apple-darwin"
            if [ "$TRY64" != "yes" ]; then
                CXXBITSINC="$CXXBITSINC/i386"
            fi
            OBJCXX="$LLVMDIR/bin/clang++ -I$CXXBITSINC -I$CXXINC"
            export OBJCXX
        fi
        if [ -d "$LEGACY_TOOLCHAIN_DIR" ] ; then
            log "Forcing generation of $HOST_OS binaries with legacy toolchain"
            CC="${LEGACY_TOOLCHAIN_PREFIX}gcc"
            CXX="${LEGACY_TOOLCHAIN_PREFIX}g++"
        fi
    fi

    CC=${CC:-gcc}
    CXX=${CXX:-g++}
    STRIP=${STRIP:-strip}
    case $HOST_TAG in
        darwin-*)
            probe_darwin_sdk
            ;;
    esac

    # Force generation of 32-bit binaries on 64-bit systems.
    # We used to test the value of $HOST_TAG for *-x86_64, but this is
    # not sufficient on certain systems.
    #
    # For example, Snow Leopard can be booted with a 32-bit kernel, running
    # a 64-bit userland, with a compiler that generates 64-bit binaries by
    # default *even* though "gcc -v" will report --target=i686-apple-darwin10!
    #
    # So know, simply probe for the size of void* by performing a small runtime
    # compilation test.
    #
    cat > $TMPC <<EOF
    /* this test should fail if the compiler generates 64-bit machine code */
    int test_array[1-2*(sizeof(void*) != 4)];
EOF
    log_n "Checking whether the compiler generates 32-bit binaries..."
    log $CC $HOST_CFLAGS -c -o $TMPO $TMPC
    $NDK_CCACHE $CC $HOST_CFLAGS -c -o $TMPO $TMPC >$TMPL 2>&1
    if [ $? != 0 ] ; then
        log "no"
        if [ "$TRY64" != "yes" ]; then
            # NOTE: We need to modify the definitions of CC and CXX directly
            #        here. Just changing the value of CFLAGS / HOST_CFLAGS
            #        will not work well with the GCC toolchain scripts.
            CC="$CC -m32"
            CXX="$CXX -m32"
        fi
    else
        log "yes"
        if [ "$TRY64" = "yes" ]; then
            CC="$CC -m64"
            CXX="$CXX -m64"
        fi
    fi

    if [ "$TRY64" = "yes" ]; then
        HOST_BITS=64
    else
        force_32bit_binaries  # to modify HOST_TAG and others
        HOST_BITS=32
    fi
}

prepare_host_build ()
{
    prepare_common_build

    # Now deal with mingw or darwin
    if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ]; then
        handle_canadian_build
        CC=$ABI_CONFIGURE_HOST-gcc
        CXX=$ABI_CONFIGURE_HOST-g++
        CPP=$ABI_CONFIGURE_HOST-cpp
        LD=$ABI_CONFIGURE_HOST-ld
        AR=$ABI_CONFIGURE_HOST-ar
        AS=$ABI_CONFIGURE_HOST-as
        RANLIB=$ABI_CONFIGURE_HOST-ranlib
        STRIP=$ABI_CONFIGURE_HOST-strip
        export CC CXX CPP LD AR AS RANLIB STRIP
    fi

    setup_ccache
}

prepare_abi_configure_build ()
{
    # detect build tag
    case $HOST_TAG in
        linux-x86)
            ABI_CONFIGURE_BUILD=i386-linux-gnu
            ;;
        linux-x86_64)
            ABI_CONFIGURE_BUILD=x86_64-linux-gnu
            ;;
        darwin-x86)
            ABI_CONFIGURE_BUILD=i686-apple-darwin
            ;;
        darwin-x86_64)
            ABI_CONFIGURE_BUILD=x86_64-apple-darwin
            ;;
        windows)
            ABI_CONFIGURE_BUILD=i686-pc-cygwin
            ;;
        *)
            echo "ERROR: Unsupported HOST_TAG: $HOST_TAG"
            echo "Please update 'prepare_host_flags' in build/tools/prebuilt-common.sh"
            ;;
    esac
}

prepare_target_build ()
{
    prepare_abi_configure_build

    # By default, assume host == build
    ABI_CONFIGURE_HOST="$ABI_CONFIGURE_BUILD"

    prepare_common_build
    HOST_GMP_ABI=$HOST_BITS

    # Now handle the --mingw/--darwin flag
    if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
        handle_canadian_build
        STRIP=$ABI_CONFIGURE_HOST-strip
        if [ "$MINGW" = "yes" ] ; then
            # It turns out that we need to undefine this to be able to
            # perform a canadian-cross build with mingw. Otherwise, the
            # GMP configure scripts will not be called with the right options
            HOST_GMP_ABI=
        fi
    fi

    setup_ccache
}

# $1: Toolchain name
#
parse_toolchain_name ()
{
    TOOLCHAIN=$1
    if [ -z "$TOOLCHAIN" ] ; then
        echo "ERROR: Missing toolchain name!"
        exit 1
    fi

    ABI_CFLAGS_FOR_TARGET=
    ABI_CXXFLAGS_FOR_TARGET=

    # Determine ABI based on toolchain name
    #
    case "$TOOLCHAIN" in
    arm-linux-androideabi-*)
        ARCH="arm"
        ABI="armeabi"
        ABI_CONFIGURE_TARGET="arm-linux-androideabi"
        ABI_CONFIGURE_EXTRA_FLAGS="--with-arch=armv5te"
        ;;
    arm-eabi-*)
        ARCH="arm"
        ABI="armeabi"
        ABI_CONFIGURE_TARGET="arm-eabi"
        ABI_CONFIGURE_EXTRA_FLAGS="--with-arch=armv5te --disable-gold --disable-libgomp"
        ;;
    aarch64-linux-android-*)
        ARCH="arm64"
        ABI="arm64-v8a"
        ABI_CONFIGURE_TARGET="aarch64-linux-android"
        ;;
    x86-*)
        ARCH="x86"
        ABI=$ARCH
        ABI_INSTALL_NAME="x86"
        ABI_CONFIGURE_TARGET="i686-linux-android"
        # Enable C++ exceptions, RTTI and GNU libstdc++ at the same time
        # You can't really build these separately at the moment.
        ABI_CFLAGS_FOR_TARGET="-fPIC"
        ;;
    x86_64-*)
        ARCH="x86_64"
        ABI=$ARCH
        ABI_INSTALL_NAME="x86_64"
        ABI_CONFIGURE_TARGET="x86_64-linux-android"
        # Enable C++ exceptions, RTTI and GNU libstdc++ at the same time
        # You can't really build these separately at the moment.
        ABI_CFLAGS_FOR_TARGET="-fPIC"
        ;;
    mipsel*)
        ARCH="mips"
        ABI=$ARCH
        ABI_INSTALL_NAME="mips"
        ABI_CONFIGURE_TARGET="mipsel-linux-android"
        # Set default to mips32
        ABI_CONFIGURE_EXTRA_FLAGS="--with-arch=mips32"
        # Enable C++ exceptions, RTTI and GNU libstdc++ at the same time
        # You can't really build these separately at the moment.
        # Add -fpic, because MIPS NDK will need to link .a into .so.
        ABI_CFLAGS_FOR_TARGET="-fexceptions -fpic"
        ABI_CXXFLAGS_FOR_TARGET="-frtti -fpic"
        # Add --disable-fixed-point to disable fixed-point support
        ABI_CONFIGURE_EXTRA_FLAGS="$ABI_CONFIGURE_EXTRA_FLAGS --disable-fixed-point"
        ;;
    mips64el*)
        ARCH="mips64"
        ABI=$ARCH
        ABI_INSTALL_NAME="mips64"
        ABI_CONFIGURE_TARGET="mips64el-linux-android"
        # Set default to mips64r6
        ABI_CONFIGURE_EXTRA_FLAGS="--with-arch=mips64r6"
        # Enable C++ exceptions, RTTI and GNU libstdc++ at the same time
        # You can't really build these separately at the moment.
        # Add -fpic, because MIPS NDK will need to link .a into .so.
        ABI_CFLAGS_FOR_TARGET="-fexceptions -fpic"
        ABI_CXXFLAGS_FOR_TARGET="-frtti -fpic"
        # Add --disable-fixed-point to disable fixed-point support
        ABI_CONFIGURE_EXTRA_FLAGS="$ABI_CONFIGURE_EXTRA_FLAGS --disable-fixed-point"
        ;;
    * )
        echo "Invalid toolchain specified. Expected (arm-linux-androideabi-*|arm-eabi-*|x86-*|mipsel*|mips64el*)"
        echo ""
        print_help
        exit 1
        ;;
    esac

    log "Targetting CPU: $ARCH"

    GCC_VERSION=`expr -- "$TOOLCHAIN" : '.*-\([0-9x\.]*\)'`
    log "Using GCC version: $GCC_VERSION"

    # Determine --host value when building gdbserver

    case "$TOOLCHAIN" in
    arm-*)
        GDBSERVER_HOST=arm-eabi-linux
        GDBSERVER_CFLAGS="-fno-short-enums"
        GDBSERVER_LDFLAGS=
        ;;
    aarch64-*)
        GDBSERVER_HOST=aarch64-eabi-linux
        GDBSERVER_CFLAGS="-fno-short-enums -DUAPI_HEADERS"
        GDBSERVER_LDFLAGS=
        ;;
    x86-*)
        GDBSERVER_HOST=i686-linux-android
        GDBSERVER_CFLAGS=
        GDBSERVER_LDFLAGS=
        ;;
    x86_64-*)
        GDBSERVER_HOST=x86_64-linux-android
        GDBSERVER_CFLAGS=-DUAPI_HEADERS
        GDBSERVER_LDFLAGS=
        ;;
    mipsel-*)
        GDBSERVER_HOST=mipsel-linux-android
        GDBSERVER_CFLAGS=
        GDBSERVER_LDFLAGS=
        ;;
    mips64el-*)
        GDBSERVER_HOST=mips64el-linux-android
        GDBSERVER_CFLAGS=-DUAPI_HEADERS
        GDBSERVER_LDFLAGS=
        ;;
    *)
        echo "Unknown TOOLCHAIN=$TOOLCHAIN"
        exit
    esac
}

# Return the host "tag" used to identify prebuilt host binaries.
# NOTE: Handles the case where '$MINGW = true' or '$DARWIN = true'
# For now, valid values are: linux-x86, darwin-x86 and windows
get_prebuilt_host_tag ()
{
    local RET=$HOST_TAG
    if [ "$MINGW" = "yes" ]; then
        if [ "$TRY64" = "no" ]; then
            RET=windows
        else
            RET=windows-x86_64
        fi
    fi
    if [ "$DARWIN" = "yes" ]; then
        RET=darwin-x86_64  # let the following handles 32-bit case
    fi
    case $RET in
        linux-x86_64)
            if [ "$TRY64" = "no" ]; then
                RET=linux-x86
            fi
            ;;
        darwin-x86_64)
            if [ "$TRY64" = "no" ]; then
                RET=darwin-x86
            fi
            ;;
    esac
    echo $RET
}

# Return the executable suffix corresponding to host executables
get_prebuilt_host_exe_ext ()
{
    if [ "$MINGW" = "yes" ]; then
        echo ".exe"
    else
        echo ""
    fi
}

# Get library suffix for given ABI
# $1: ABI
# Return: .so or .bc
get_lib_suffix_for_abi ()
{
    local ABI=$1
    echo ".so"
}

# Convert an ABI name into an Architecture name
# $1: ABI name
# Result: Arch name
convert_abi_to_arch ()
{
    local RET
    local ABI=$1
    case $ABI in
        armeabi|armeabi-v7a|armeabi-v7a-hard)
            RET=arm
            ;;
        x86|mips|x86_64|mips64)
            RET=$ABI
            ;;
        mips32r6)
            RET=mips
            ;;
        arm64-v8a)
            RET=arm64
            ;;
        *)
            >&2 echo "ERROR: Unsupported ABI name: $ABI, use one of: armeabi, armeabi-v7a, x86, mips, armeabi-v7a-hard, arm64-v8a, x86_64 or mips64"
            exit 1
            ;;
    esac
    echo "$RET"
}

# Take architecture name as input, and output the list of corresponding ABIs
# Inverse for convert_abi_to_arch
# $1: ARCH name
# Out: ABI names list (comma-separated)
convert_arch_to_abi ()
{
    local RET
    local ARCH=$1
    case $ARCH in
        arm)
            RET=armeabi,armeabi-v7a,armeabi-v7a-hard
            ;;
        x86|x86_64|mips|mips64)
            RET=$ARCH
            ;;
        arm64)
            RET=arm64-v8a
            ;;
        *)
            >&2 echo "ERROR: Unsupported ARCH name: $ARCH, use one of: arm, x86, mips"
            exit 1
            ;;
    esac
    echo "$RET"
}

# Take a list of architecture names as input, and output the list of corresponding ABIs
# $1: ARCH names list (separated by spaces or commas)
# Out: ABI names list (comma-separated)
convert_archs_to_abis ()
{
    local RET
    for ARCH in $(commas_to_spaces $@); do
       ABI=$(convert_arch_to_abi $ARCH)
       if [ -n "$ABI" ]; then
          if [ -n "$RET" ]; then
             RET=$RET",$ABI"
          else
             RET=$ABI
          fi
       else   # Error message is printed by convert_arch_to_abi
          exit 1
       fi
    done
    echo "$RET"
}

# Return the default toolchain binary path prefix for given architecture and gcc version
# For example: arm 4.8 -> toolchains/arm-linux-androideabi-4.8/prebuilt/<system>/bin/arm-linux-androideabi-
# $1: Architecture name
# $2: GCC version
# $3: optional, system name, defaults to $HOST_TAG
get_toolchain_binprefix_for_arch ()
{
    local NAME PREFIX DIR BINPREFIX
    local SYSTEM=${3:-$(get_prebuilt_host_tag)}
    NAME=$(get_toolchain_name_for_arch $1 $2)
    PREFIX=$(get_default_toolchain_prefix_for_arch $1)
    DIR=$(get_toolchain_install . $NAME $SYSTEM)
    BINPREFIX=${DIR#./}/bin/$PREFIX-
    echo "$BINPREFIX"
}

# Return llvm toolchain binary path prefix for given llvm version
# $1: llvm version
# $2: optional, system name, defaults to $HOST_TAG
get_llvm_toolchain_binprefix ()
{
    local NAME DIR BINPREFIX
    local SYSTEM=${2:-$(get_prebuilt_host_tag)}
    NAME=llvm-$1
    DIR=$(get_toolchain_install . $NAME $SYSTEM)
    BINPREFIX=${DIR#./}/bin/
    echo "$BINPREFIX"
}

# Return default API level for a given arch
# This is the level used to build the toolchains.
#
# $1: Architecture name
get_default_api_level_for_arch ()
{
    # For now, always build the toolchain against API level 9 for 32-bit arch
    # and API level $FIRST_API64_LEVEL for 64-bit arch
    case $1 in
        *64) echo $FIRST_API64_LEVEL ;;
        *) echo 9 ;;
    esac
}

# Return the default platform sysroot corresponding to a given architecture
# This is the sysroot used to build the toolchain and other binaries like
# the STLport libraries.
# $1: Architecture name
get_default_platform_sysroot_for_arch ()
{
    local ARCH=$1
    local LEVEL=$(get_default_api_level_for_arch $ARCH)

    if [ "$ARCH" != "${ARCH%%64*}" ] ; then
        LEVEL=$FIRST_API64_LEVEL
    fi
    echo "platforms/android-$LEVEL/arch-$ARCH"
}

# Return the default platform sysroot corresponding to a given abi
# $1: ABI
get_default_platform_sysroot_for_abi ()
{
    local ARCH=$(convert_abi_to_arch $1)
    $(get_default_platform_sysroot_for_arch $ARCH)
}

# Return the default libs dir corresponding to a given architecture
# $1: Architecture name
# $2: Optional llvm version
get_default_libdir_for_arch ()
{
    case $1 in
      x86_64|mips64) echo "lib64" ;;
      arm64) echo "lib" ;; # return "lib" until aarch64 is built to look for sysroot/usr/lib64
      *) echo "lib" ;;
    esac
}

# Return the default libs dir corresponding to a given abi
# $1: ABI
# $2: Optional llvm version
get_default_libdir_for_abi ()
{
    local ARCH

    case $1 in
      mips32r6) echo "libr6" ;;
      *)
        local ARCH=$(convert_abi_to_arch $1)
        echo "$(get_default_libdir_for_arch $ARCH $2)"
        ;;
    esac
}

# Return the host/build specific path for prebuilt toolchain binaries
# relative to $1.
#
# $1: target root NDK directory
# $2: toolchain name
# $3: optional, host system name
#
get_toolchain_install ()
{
    local NDK="$1"
    shift
    echo "$NDK/$(get_toolchain_install_subdir "$@")"
}

# $1: toolchain name
# $2: optional, host system name
get_toolchain_install_subdir ()
{
    local SYSTEM=${2:-$(get_prebuilt_host_tag)}
    echo "toolchains/$1/prebuilt/$SYSTEM"
}

# Return the relative install prefix for prebuilt host
# executables (relative to the NDK top directory).
# NOTE: This deals with MINGW==yes or DARWIN==yes appropriately
#
# $1: optional, system name
# Out: relative path to prebuilt install prefix
get_prebuilt_install_prefix ()
{
    local TAG=${1:-$(get_prebuilt_host_tag)}
    echo "prebuilt/$TAG"
}

# Return the relative path of an installed prebuilt host
# executable
# NOTE: This deals with MINGW==yes or DARWIN==yes appropriately.
#
# $1: executable name
# $2: optional, host system name
# Out: path to prebuilt host executable, relative
get_prebuilt_host_exec ()
{
    local PREFIX EXE
    PREFIX=$(get_prebuilt_install_prefix $2)
    EXE=$(get_prebuilt_host_exe_ext)
    echo "$PREFIX/bin/$1$EXE"
}

# Return the name of a given host executable
# $1: executable base name
# Out: executable name, with optional suffix (e.g. .exe for windows)
get_host_exec_name ()
{
    local EXE=$(get_prebuilt_host_exe_ext)
    echo "$1$EXE"
}

# Return the directory where host-specific binaries are installed.
# $1: target root NDK directory
get_host_install ()
{
    echo "$1/$(get_prebuilt_install_prefix)"
}

# Set the toolchain target NDK location.
# this sets TOOLCHAIN_PATH and TOOLCHAIN_PREFIX
# $1: target NDK path
# $2: toolchain name
set_toolchain_ndk ()
{
    TOOLCHAIN_PATH=`get_toolchain_install "$1" $2`
    log "Using toolchain path: $TOOLCHAIN_PATH"

    TOOLCHAIN_PREFIX=$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET
    log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
}

# Check that a toolchain is properly installed at a target NDK location
#
# $1: target root NDK directory
# $2: toolchain name
#
check_toolchain_install ()
{
    TOOLCHAIN_PATH=`get_toolchain_install "$1" $2`
    if [ ! -d "$TOOLCHAIN_PATH" ] ; then
        echo "ERROR: Cannot find directory '$TOOLCHAIN_PATH'!"
        echo "       Toolchain '$2' not installed in '$NDK_DIR'!"
        echo "       Ensure that the toolchain has been installed there before."
        exit 1
    fi

    set_toolchain_ndk $1 $2
}

# $1: toolchain source directory
check_toolchain_src_dir ()
{
    local SRC_DIR="$1"
    if [ -z "$SRC_DIR" ]; then
        echo "ERROR: Please provide the path to the toolchain source tree. See --help"
        exit 1
    fi

    if [ ! -d "$SRC_DIR" ]; then
        echo "ERROR: Not a directory: '$SRC_DIR'"
        exit 1
    fi

    if [ ! -f "$SRC_DIR/build/configure" -o ! -d "$SRC_DIR/gcc" ]; then
        echo "ERROR: Either the file $SRC_DIR/build/configure or"
        echo "       the directory $SRC_DIR/gcc does not exist."
        echo "This is not the top of a toolchain tree: $SRC_DIR"
        exit 1
    fi
}

#
# The NDK_TMPDIR variable is used to specify a root temporary directory
# when invoking toolchain build scripts. If it is not defined, we will
# create one here, and export the value to ensure that any scripts we
# call after that use the same one.
#
if [ -z "$NDK_TMPDIR" ]; then
    
    NDK_TMPDIR=$TMPDIR/tmp/build-$$
    mkdir -p $NDK_TMPDIR
    if [ $? != 0 ]; then
        echo "ERROR: Could not create NDK_TMPDIR: $NDK_TMPDIR"
        exit 1
    fi
    export NDK_TMPDIR
fi

# Define HOST_TAG32, as the 32-bit version of HOST_TAG
# We do this by replacing an -x86_64 suffix by -x86
HOST_TAG32=$HOST_TAG
case $HOST_TAG32 in
    *-x86_64)
        HOST_TAG32=${HOST_TAG%%_64}
        ;;
esac

# this function should be called after all options are extracted
CACHE_HOST_TAG=linux-x86
set_cache_host_tag ()
{
    if [ "$MINGW" = "yes" ] ; then
        if [ "$TRY64" = "yes" ]; then
            CACHE_HOST_TAG=windows-x86_64
        else
            CACHE_HOST_TAG=windows
        fi
    elif [ "$DARWIN" = "yes" -o "$HOST_OS" = "darwin" ] ; then
        if [ "$TRY64" = "yes" ]; then
            CACHE_HOST_TAG=darwin-x86_64
        else
            CACHE_HOST_TAG=darwin-x86
        fi
    else
        if [ "$TRY64" = "yes" ]; then
            CACHE_HOST_TAG=linux-x86_64
        fi        
    fi
}

assert_cache_host_tag ()
{
    if [ "$CACHE_HOST_TAG" != "$HOST_TAG" ]; then
        fail_panic "ASSERT in $PROGNAME: $CACHE_HOST_TAG != $HOST_TAG"
    fi
}