File: builder.inc.sh

package info (click to toggle)
keyman 18.0.246-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,316 kB
  • sloc: python: 52,784; cpp: 21,289; sh: 7,633; ansic: 4,823; xml: 3,617; perl: 959; makefile: 139; javascript: 138
file content (2118 lines) | stat: -rwxr-xr-x 63,560 bytes parent folder | download | duplicates (2)
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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
#!/usr/bin/env bash

# Note: these two lines can be uncommented for debugging and profiling build
# scripts:
#
#   set -x
#   PS4='+ $EPOCHREALTIME $0 $LINENO   '
#

#
# This script contains utilities for builder_script calls
#
# * builder_ functions and variables are defined here.
# * REPO_ROOT defines the top level of this repository
# * THIS_SCRIPT_PATH defines the full path of the running script
# * THIS_SCRIPT_NAME defines the basename of the running script
# * THIS_SCRIPT_IDENTIFIER defines the repo-relative path of the running script
# * _builder_ functions and variables are internal use only for builder.inc.sh, and
#   subject to change at any time. Do not use them in other scripts.
# * Note: the running script is the top-level script that includes either
#   builder.inc.sh directly, or, just in the Keyman repo, via build-utils.sh.
#

# Exit on command failure and when using unset variables:
set -eu

#
# Prevents 'clear' on exit of mingw64 bash shell
#
SHLVL=0

# _builder_init is called internally at the bottom of this file after we have
# all function declarations in place.
function _builder_init() {
  _builder_findRepoRoot
  _builder_setBuildScriptIdentifiers

  if [[ -n "$TERM" ]] && [[ "$TERM" != "dumb" ]] && [[ "$TERM" != "unknown" ]] && [ -t 1 ]; then
    builder_use_color true
  else
    builder_use_color false
  fi

  # Set shared Meson package cache (works with Meson >= 1.3)
  if [[ -z ${MESON_PACKAGE_CACHE_DIR:-} ]]; then
    export MESON_PACKAGE_CACHE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/keyman/builder"
    mkdir -p "${MESON_PACKAGE_CACHE_DIR}"
  fi
}

function _builder_findRepoRoot() {
    # We don't need readlink here because our standard script prolog does a
    # readlink -f already so we will have already escaped from any symlinks
    REPO_ROOT="${BASH_SOURCE[0]%/*/*}"
    readonly REPO_ROOT
}

# Used to build script-related build variables useful for referencing the calling script
# and for prefixing `builder_finish_action` outputs in order to more clearly identify the calling
# script.
#
# Assumes that `THIS_SCRIPT` has been set, typically like this:
#
# ```bash
#   ## START STANDARD BUILD SCRIPT INCLUDE
#   # adjust relative paths as necessary
#   THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")"
#   . "${THIS_SCRIPT%/*}/resources/builder.inc.sh"
#   ## END STANDARD BUILD SCRIPT INCLUDE
# ```
#
function _builder_setBuildScriptIdentifiers() {
  if [ ! -z ${THIS_SCRIPT+x} ]; then
    THIS_SCRIPT_PATH="${THIS_SCRIPT%/*}"
    readonly THIS_SCRIPT_PATH
    THIS_SCRIPT_NAME="${THIS_SCRIPT##*/}"
    readonly THIS_SCRIPT_NAME
    # Leaves only the part of the path based upon REPO_ROOT.
    THIS_SCRIPT_IDENTIFIER=${THIS_SCRIPT_PATH#"$REPO_ROOT/"}
    readonly THIS_SCRIPT_IDENTIFIER
  else
    builder_die "THIS_SCRIPT not defined; builder.inc.sh has not been sourced with standard script include."
  fi
}

################################################################################
# Standard build script functions for managing command line, actions and targets
################################################################################

# The following allows coloring of warning and error lines, but only works if there's a
# terminal attached, so not on the build machine.

# Overrides default colorization of logging; can be used in command-line with
# --color or --no-color, or overridden as necessary on a per-script basis.
#
# Parameters
#  1: use_color       true or false
builder_use_color() {
  if $1; then
    # Using esc codes instead of tput for performance
    COLOR_RED='\x1b[31m'                # $(tput setaf 1)
    COLOR_GREEN='\x1b[32m'              # $(tput setaf 2)
    COLOR_YELLOW='\x1b[33m'             # $(tput setaf 3)
    COLOR_BLUE='\x1b[34m'               # $(tput setaf 4)
    COLOR_PURPLE='\x1b[35m'             # $(tput setaf 5)
    COLOR_TEAL='\x1b[36m'               # $(tput setaf 6)
    COLOR_WHITE='\x1b[38;5;252m'        # $(tput setaf 252)
    COLOR_BRIGHT_WHITE='\x1b[38;5;255m' # $(tput setaf 255)
    COLOR_GREY='\x1b[90m'               # $(tput setaf 8)
    COLOR_RESET='\x1b(B\x1b[m'          # $(tput sgr0)
    # e.g. VSCode https://code.visualstudio.com/updates/v1_69#_setmark-sequence-support
    BUILDER_BOLD='\x1b[1m'              # $(tput bold)
    HEADING_SETMARK='\x1b]1337;SetMark\x07'
    # Used by `builder_display_usage` when marking special terms (actions, targets, options)
    # in the plain-text description area.
    BUILDER_TERM_START="$COLOR_BLUE"
    BUILDER_TERM_END="$COLOR_RESET"
  else
    COLOR_RED=
    COLOR_GREEN=
    COLOR_YELLOW=
    COLOR_BLUE=
    COLOR_PURPLE=
    COLOR_TEAL=
    COLOR_WHITE=
    COLOR_BRIGHT_WHITE=
    COLOR_GREY=
    COLOR_RESET=
    BUILDER_BOLD=
    HEADING_SETMARK=
    BUILDER_TERM_START="<"
    BUILDER_TERM_END=">"
  fi
}

#
# Wraps the input string in `builder_display_usage` with $BUILDER_TERM_START and
# $BUILDER_TERM_END
#
function builder_term() {
  echo -e "${BUILDER_TERM_START}$*${BUILDER_TERM_END}"
}

function builder_die() {
  echo
  if [[ $# -eq 0 ]]; then
    builder_echo error "Unspecified error, aborting script"
  else
    builder_echo error "$*"
  fi
  echo
  exit 1
}

function builder_warn() {
  builder_echo warning "$*"
}

function builder_heading() {
  builder_echo heading "$*"
}


####################################################################################
#
# builder_ functions for standard build script parameter and process management
#
####################################################################################


builder_echo() {
  local color=white message= mark=
  if [[ $# -gt 1 && $1 =~ ^(white|grey|green|success|blue|heading|yellow|warning|red|error|purple|brightwhite|teal|debug|setmark)$ ]]; then
    color="$1"
    shift
  fi
  message="$*"

  if [[ ! -z ${COLOR_RED+x} ]]; then
    case $color in
      white) color="$COLOR_WHITE" ;;
      grey) color="$COLOR_GREY" ;;
      green|success) color="$COLOR_GREEN" ;;
      blue|heading) color="$COLOR_BLUE" ;;
      yellow|warning) color="$COLOR_YELLOW" ;;
      red|error) color="$COLOR_RED" ;;
      purple) color="$COLOR_PURPLE" ;;
      brightwhite) color="$COLOR_BRIGHTWHITE" ;;
      teal|debug) color="$COLOR_TEAL" ;;
      setmark) mark="$HEADING_SETMARK" color="$COLOR_PURPLE" ;;
    esac

    if builder_is_dep_build; then
      echo -e "$mark$COLOR_GREY[$THIS_SCRIPT_IDENTIFIER]$COLOR_RESET $color$message$COLOR_RESET"
    else
      echo -e "$mark$BUILDER_BOLD$COLOR_BRIGHT_WHITE[$THIS_SCRIPT_IDENTIFIER]$COLOR_RESET $color$message$COLOR_RESET"
    fi
  else
    # Cope with the case of pre-init message and just emit plain text
    echo -e "$message"
  fi
}

builder_echo_debug() {
  builder_echo debug "[DEBUG] $*"
}

#
# builder_ names are reserved.
# _builder_ names are internal use and subject to change
#

#
# builder_extra_params: string containing all parameters after '--'
#
builder_extra_params=()

# returns 0 if first parameter is in the array passed as second parameter
#
# Usage:
#   if _builder_item_in_array "item" "${array[@]}"; then ...; fi
# Parameters:
#   1: item       item to search for in array
#   2: array      bash array, e.g. array=(one two three)
_builder_item_in_array() {
  local e match="$1"
  shift
  [[ -z "$match" ]] && return 1
  for e; do [[ "$e" == $match ]] && return 0; done
  return 1
}

#
# Returns `0` if first parameter is in the array passed as second parameter,
# where the array may contain globs.
#
# ### Parameters
#
# * 1: `item`       item to search for in array
# * 2: `array`      bash array, e.g. `array=(one two three)`
#
# ### Example
#
# ```bash
#   array=(foo bar it*)
#   if _builder_item_in_glob_array "item" "${array[@]}"; then ...; fi
# ```
#
_builder_item_in_glob_array() {
  local e match="$1"
  shift
  [[ -z "$match" ]] && return 1
  for e; do [[ "$match" == $e ]] && return 0; done
  return 1
}

#
# Expands a shorthand item into a full match from an array of possibilities;
# reports an error if there are ambiguous options. Note that this function
# returns the number of matches, so 0 = no match, 1 = a precise match.
#
# ### Parameters
#
# * 1: `item`       item to search for in array, e.g. "t"
# * 2: `array`      bash array, e.g. `array=(one two three)`
#
# ### Description
#
# Does a substring search by regex for
#
# ### Example
#
# ```bash
#   actions=(clean configure build test)
#
#   action=`_builder_expand_shorthand $1 "${actions[@]}"` &&
#     builder_die "Unrecognized parameter $1" ||
#     case $? in
#       1) echo "Parameter $1 matches {$action}"
#       ;;
#       *) builder_die "Parameter $1 has $? matches, could mean any of {$action}"
#     esac
# ```
#
_builder_expand_shorthand() {
  local item=$1
  shift
  local count=0
  local result=
  local string=
  for e; do
    if [[ $e == $item ]]; then
      # Exact match trumps substring matches
      echo $item
      return 1
    fi
    if [[ $e == "$item"* ]]; then
      count=$((count+1))
      if [[ $count == 2 ]]; then
        string="$result, $e"
        result=$item
      elif [[ $count -gt 2 ]]; then
        string="$string, $e"
      else
        result=$e
      fi
    fi
  done

  if [[ $count -lt 2 ]]; then
    echo $result
  else
    echo $string
  fi
  return $count
}


_builder_item_is_target() {
  local item="$1"
  [[ $item =~ ^: ]] && return 1
  return 0
}

function _builder_warn_if_incomplete() {
  if [ -n "${_builder_current_action}" ]; then
    builder_echo warning "$_builder_current_action never reported success or failure"
    # exit 1  # If we wanted this scenario to result in a forced build-script fail.
  fi

  # Since we've already warned about this once, we'll clear the variable to prevent repetitions.
  _builder_current_action=
}

# Used by a `trap` statement later to facilitate auto-reporting failures on error detection
# without obscuring failure exit/error codes.
_builder_failure_trap() {
  local trappedExitCode=$?
  local action target

  _builder_cleanup_deps

  # Since 'exit' is also trapped, we can also handle end-of-script incomplete actions.
  if [[ $trappedExitCode == 0 ]]; then
    # While there weren't errors, were there any actions that never reported success or failure?
    _builder_warn_if_incomplete
    return
  fi

  # If we've reached this point, we're here because an error occurred.

  # Iterate across currently-active actions and report their failures.
  if [ -n "${_builder_current_action}" ]; then
    action="${_builder_current_action}"
    if [[ $action =~ : ]]; then
      IFS=: read -r action target <<< "$action"
      target=:$target
    else
      target=:project
    fi

    builder_finish_action failure $action$target
  fi

  # Make 100% sure that the exit code chains fully.
  # Without this, nested scripts have failed to chain errors from npm calls past the script
  # that directly executed the failed npm command.
  exit $trappedExitCode
}

#
# Removes temporary `_builder_deps_built` file when top-level build script
# finishes.
#
_builder_cleanup_deps() {
  if ! builder_is_dep_build && ! builder_is_child_build && [[ ! -z ${_builder_deps_built+x} ]]; then
    if $_builder_debug_internal; then
      builder_echo_debug "Dependencies that were built:"
      cat "$_builder_deps_built"
    fi
    rm -f "$_builder_deps_built"
    _builder_deps_built=
  fi
}

#------------------------------------------------------------------------------------------
# Child scripts
#------------------------------------------------------------------------------------------

_builder_execute_child() {
  local action=$1
  local target=$2

  local script="$THIS_SCRIPT_PATH/${_builder_target_paths[$target]}/build.sh"

  if $_builder_debug_internal; then
    builder_echo heading "## $action$target starting..."
  fi

  # Build array of specified inheritable options
  local child_options=()
  local opt
  for opt in "${_builder_options_inheritable[@]}"; do
    if builder_has_option $opt; then
      child_options+=($opt)
    fi
  done

  # If the current build is a dependency build, pass the dependency state on to
  # the child build, so that we don't unnecessarily rebuild children (#11394)
  local dep_flag= dep_module=
  if builder_is_dep_build; then
    dep_flag=--builder-dep-parent
    dep_module="$builder_dep_parent"
  fi

  "$script" \
    --builder-child \
    $_builder_build_deps \
    $dep_flag "$dep_module" \
    $action \
    ${child_options[@]} \
    $builder_verbose \
    $builder_debug \
    $_builder_offline \
  && (
    if $_builder_debug_internal; then
      builder_echo success "## $action$target completed successfully"
    fi
  ) || (
    result=$?
    builder_echo error "## $action$target failed with exit code $result"
    exit $result
  ) || exit $? # Required due to above subshell masking exit
}

_builder_run_child_action() {
  local action="$1" target

  if [[ $action =~ : ]]; then
    IFS=: read -r action target <<< "$action"
    target=:$target
  else
    target=':*'
  fi

  if builder_has_action $action$target; then
    if [[ $target == ':*' ]]; then
      # run all children in order specified in builder_describe
      for target in "${_builder_targets[@]}"; do
        # We have to re-test the action because the user may not
        # have specified all targets in their invocation
        if builder_has_action $action$target; then
          if [[ ! -z ${_builder_target_paths[$target]+x} ]] &&
            [[ -f "$THIS_SCRIPT_PATH/${_builder_target_paths[$target]}/build.sh" ]]; then
            _builder_execute_child $action $target
          fi
        fi
      done
    else
      # If specified explicitly, we assume existence of a child build script.
      _builder_execute_child $action $target
    fi
  fi
}

#
# Executes the specified actions on all child targets, or on the specified
# targets. A child target is any target which has a sub-folder of the same name
# as the target. However, the actions will only be run if they have been
# specified by the user on the command-line.
#
# ### Usage
#
# ```bash
# builder_run_child_actions action1 [...]
# ```
#
# ### Parameters
#
#   1...: action[:target]   name of action:target to run
#
# ### Example
#
# ```bash
# builder_run_child_actions configure build test install
# ```
#
builder_run_child_actions() {
  while [[ $# -gt 0 ]]; do
    local action="$1"
    _builder_run_child_action "$action"
    shift
  done
}

#------------------------------------------------------------------------------------------
# Various API endpoints
#------------------------------------------------------------------------------------------

#
# Builds the standardized `action:target` string for the specified action-target
# pairing and also returns 0 if the user has asked to perform it on the command
# line.  Otherwise, returns 0 and sets an empty string in place of the matched
# pair.
#
# The string will be set as `_builder_matched_action`, which is for
# builder.inc.sh internal use, used by `builder_start_action`.
#
# ### Usage
#
# ```bash
#   if build_has_action action[:target]; then ...; fi
# ````
#
# ### Parameters
#
#   1: action[:target]    name of action:target
#
# ### Example
#
# ```bash
#   if builder_has_action build:app; then ...
# ```
#
builder_has_action() {
  local action="$1" target

  if [[ $action =~ : ]]; then
    IFS=: read -r action target <<< "$action"
    target=:$target
  else
    target=':*'
  fi

  if _builder_item_in_array "$action$target" "${_builder_chosen_action_targets[@]}"; then
    # To avoid WET re-processing of the $action$target string set
    _builder_matched_action="$action$target"
    if [[ $target == ':*' ]]; then
      _builder_matched_action_name="$action"
    else
      _builder_matched_action_name="$action$target"
    fi
    return 0
  else
    _builder_matched_action=
    return 1
  fi
}

#
# Wraps builder_start_action and builder_finish action for single-command
# actions. Can be used together with a local function for multi-command actions.
# Do be aware that this pseudo-closure style cannot be mixed with operators such
# as `<`, `>`, `&&`, `;`, `()` and so on.
#
# ### Usage
#
# ```bash
#   builder_run_action action[:target] command [command-params...]
# ```
#
# ### Parameters
#
# * 1: `action[:target]`   name of action, and optionally also target, if target
#                          excluded starts for all defined targets
# * 2: command             command to run if action is started
# * 3...: command-params   parameters for command
#
# ### Example
#
# ```bash
#   function do_build() {
#     mkdir -p build/cjs-src
#     npm run build
#   }
#
#   builder_run_action clean        rm -rf ./build/ ./tsconfig.tsbuildinfo
#   builder_run_action configure    verify_npm_setup
#   builder_run_action build        do_build
# ```
#
function builder_run_action() {
  local action=$1
  shift
  if builder_start_action $action; then
    "$@"
    builder_finish_action success $action
  fi
  return 0
}

#
# Returns `0` if the user has asked to perform action on target on the command
# line, and then starts the action. Should be paired with
# `builder_finish_action`.
#
# ### Usage
#
# ```bash
#   if builder_start_action action[:target]; then ...; fi
# ```
#
# ### Parameters
#
# * 1: `action[:target]`   name of action, and optionally also target, if
#                          target excluded starts for all defined targets
#
# ### Example
#
# ```bash
#   if builder_start_action build:app; then ...
# ```
#
builder_start_action() {
  if builder_has_action $1; then
    # In a dependency quick build (the default), determine whether we actually
    # need to run this step. Uses data passed to builder_describe_outputs to
    # verify whether a target output is present.
    if builder_is_dep_build &&
        ! builder_is_full_dep_build &&
        _builder_dep_output_exists $_builder_matched_action; then
      builder_echo "skipping $_builder_matched_action_name, up-to-date"
      return 1
    fi

    builder_echo blue "## $_builder_matched_action_name starting..."
    if [ -n "${_builder_current_action}" ]; then
      _builder_warn_if_incomplete
    fi
    _builder_current_action="$_builder_matched_action"

    # Build dependencies as required
    _builder_do_build_deps "$_builder_matched_action"
    return 0
  else
    return 1
  fi
}

#
# Returns 0 if the user has --option on the command line
#
# Usage:
#   if build_has_option option; then ...; fi
# Parameters:
#   1: option    name of option, i.e. --option
# Example:
#   if build_has_option --debug; then
#
builder_has_option() {
  local option="$1"

  if _builder_item_in_array "$option" "${_builder_chosen_options[@]}"; then
    return 0
  fi
  return 1
}

#
# Trims leading and following whitespace from the input parameters
#
# ### Usage
#
# ```bash
#   my_string="$(builder_trim "$my_string")"
# ```
#
# ### Parameters
#
# * `my_string`    An input string
#
builder_trim() {
  local var="$*"
  # remove leading whitespace characters
  var="${var#"${var%%[![:space:]]*}"}"
  # remove trailing whitespace characters
  var="${var%"${var##*[![:space:]]}"}"
  printf '%s' "$var"
}

#
# Expands an in-repo-relative path to a repo-relative path. A path starting with
# `/` is expected to be relative to repo root, not filesystem root. Otherwise,
# it's relative to current script path, not current working directory. The
# returned path will not have a prefix `/`, and will be relative to
# `$REPO_ROOT`. Assumes realpath is installed (brew coreutils on macOS).
#
_builder_expand_relative_path() {
  local path="$1"
  if [[ "$path" =~ ^/ ]]; then
    echo "${path:1}"
  else
    realpath --canonicalize-missing --relative-to="$REPO_ROOT" "$THIS_SCRIPT_PATH/$path"
  fi
}

#
# Expands an `[action][:target]` string, replacing missing values with `*`,
# for example:
#
# * `build` --> `build:*`
# * `build:app` --> `build:app`
# * `:app` --> `*:app`
#
# Supports multiple action:targets in the string
#
_builder_expand_action_target() {
  local input="$1" target= action=
  if [[ "$input" =~ : ]]; then
    IFS=":" read -r action target <<< "$input"
  else
    action="$input"
  fi

  if [[ -z "$action" ]]; then
    action='*'
  fi
  if [[ -z "$target" ]]; then
    target='*'
  fi

  echo "$action:$target"
}

_builder_expand_action_targets() {
  local input=($1) e output=()
  for e in "${input[@]}"; do
    e="$(_builder_expand_action_target "$e")"
    output+=("$e")
  done
  if [[ ${#output[@]} == 0 ]]; then
    echo "*:*"
  else
    echo "${output[@]}"
  fi
}

_builder_child_base=
#
# Describes the path from the build script's working directory to the common subfolder
# containing child scripts / projects without defined custom paths.
#
# This function must be called to set the child base path before builder_describe is
# called in order to work correctly.  Furthermore, note that this setting will be
# ignored by targets with custom paths.
#
# ### Usage
#
# ```bash
#    builder_set_child_base path
# ```
#
# ### Parameters
#
#  * `path`  The relative path from the directory containing the calling script to
#            the base folder to use for child-project detection and resolution
#
builder_set_child_base() {
  _builder_child_base="$1/"
}

#
# Describes a build script, defines available parameters and their meanings. Use
# together with `builder_parse` to process input parameters.
#
# ### Usage
#
# ```bash
#    builder_describe description param_desc...
# ```
#
# ### Parameters
#
#  * `description`  A short description of what the script does.
#  * `param_desc`   Space separated name and description of parameter, e.g.
#                   `"build   Builds the target"`
#                   This parameter may be repeated to describe all parameters.
#
# There are four types of parameters that may be specified:
#
# * **Option:** `"--option[,-o][+][=var]   [One line description]"`
#
#   All options must have a longhand form with two prefix hyphens,
#   e.g. `--option`. The `,-o` shorthand form is optional. When testing if
#   the option is set with `builder_has_option`, always use the longhand
#   form.
#
#   If `=var` is specified, then the next parameter will be a variable stored in
#   `$var` for that option. e.g. `--option=opt` means `$opt` will have the value
#   `"foo"` when the script is called for `--option foo`.
#
#   If `+` is specified, then the option will be passed to child scripts. All
#   child scripts _must_ accept this option, or they will fail. It is acceptable
#   for the child script to declare the option but ignore it. However, the option
#   will _not_ be passed to dependencies.
#
# * **Action**: `"action   [One line description]"`
#
#   Actions must be a single word, lower case. To specify an action as the
#   default, append a `+` to the action name, e.g. `"test+   Test the project"`.
#   If there is no default specified, then it will be `build`.
#
# * **Target:** `":target[=path]   [One line description]"`
#
#   A target always starts with colon, e.g. `:project`. If a folder exists with
#   the same name as a target, then that automatically denotes the target as a
#   "child project". This can simplify parent-child style scripts, using the
#   [`builder_run_child_actions`] function.
#
#   A child project with an alternate folder can also be specified by appending
#   `=path` to the target definition, for example `:app=src/app`. Where
#   possible, avoid differences in names of child projects and folders.
#
# * **Dependency:** `"@/path/to/dependency[:target] [action][:target] ..."``
#
#   A dependency always starts with `@`. The path to the dependency will be
#   relative to the build script folder, or to the root of the repository, if
#   the path starts with `/`, not to the root of the file system. It is an error
#   to specify a dependency outside the repo root.
#
#   Relative paths will be expanded to full paths, again, relative to the root
#   of the repository.
#
#   A dependency definition can include a target for that dependency, for
#   example, `"@/core:arch"`. This would build only the ':arch' target for the
#   core module.
#
#   Dependencies may be limited to specific `action:target` pairs on the current
#   script. If not specified, dependencies will be built for all actions on all
#   targets. Either `action` or `:target` may be omitted, and multiple actions
#   and targets may be specified, space separated.
#
builder_describe() {
  _builder_record_function_call builder_describe

  _builder_description="$1"
  _builder_actions=()
  _builder_targets=()
  _builder_options=()
  _builder_deps=()                    # array of all dependencies for this script
  _builder_options_inheritable=()     # array of all options that should be passed to child scripts
  _builder_default_action=build
  declare -A -g _builder_targets_excluded_by_platform=()
  declare -A -g _builder_params
  declare -A -g _builder_options_short
  declare -A -g _builder_options_var
  declare -A -g _builder_dep_path             # array of output files for action:target pairs
  declare -A -g _builder_dep_related_actions  # array of action:targets associated with a given dependency
  declare -A -g _builder_internal_dep         # array of internal action:targets dependency relationships
  declare -A -g _builder_target_paths         # array of target child project paths
  declare -A -g _builder_dep_targets          # array of :targets given for a specific dependency (comma separated if more than one)
  shift
  local sub=()
  # describe each target, action, and option possibility
  while [[ $# -gt 0 ]]; do
    local key="$1"
    local value="$key"
    local description=
    if [[ $key =~ [[:space:]] ]]; then
      IFS=" " read -r -a sub <<< "$key"
      value="${sub[0]}"
      description="$(builder_trim "${sub[@]:1}")"
    fi

    local original_value="$value"

    if [[ $value =~ ^: ]]; then
      # Parameter is a target
      local target_path=
      if [[ $value =~ = ]]; then
        # The target has a custom child project path
        IFS="=" read -r -a sub <<< "$value"
        target_path="${sub[@]:1}"
        value="${sub[0]}"
        original_value="$value"
        if [[ ! -d "$THIS_SCRIPT_PATH/$target_path" ]]; then
          builder_die "Target path '$target_path' for $value does not exist."
        fi
      else
        # If the target name matches a folder name, implicitly
        # make it available as a child project
        if [[ -d "$THIS_SCRIPT_PATH/$_builder_child_base${value:1}" ]]; then
          target_path="$_builder_child_base${value:1}"
        fi
      fi
      _builder_targets+=($value)
      if [[ ! -z "$target_path" ]]; then
        _builder_target_paths[$value]="$target_path"
      fi
    elif [[ $value =~ ^@ ]]; then
      # Parameter is a dependency
      local dependency="${value:1}"
      local dependency_target= # all targets
      if [[ $dependency =~ : ]]; then
        IFS=":" read -r dependency dependency_target <<< "$dependency"
        dependency_target=":$dependency_target"
      fi

      dependency="`_builder_expand_relative_path "$dependency"`"
      _builder_deps+=($dependency)
      _builder_dep_related_actions[$dependency]="$(_builder_expand_action_targets "$description")"
      _builder_dep_targets[$dependency]="$dependency_target"
      # We don't want to add deps to params, so shift+continue
      shift
      continue
    elif [[ $value =~ ^-- ]]; then
      # Parameter is an option
      # Look for a shorthand version of the option
      local option_var=
      if [[ $value =~ = ]]; then
        IFS="=" read -r -a sub <<< "$value"
        option_var="${sub[@]:1}"
        value="${sub[0]}"
      fi

      local is_inheritable=false

      if [[ $value =~ \+$ ]]; then
        # final + indicates that option is inheritable
        is_inheritable=true
        value="${value:0:-1}"
      fi

      if [[ $value =~ , ]]; then
        IFS="," read -r -a sub <<< "$value"
        local option_long="${sub[0]}"
        local option_short="${sub[@]:1}"
        _builder_options+=($option_long)
        if $is_inheritable; then
          _builder_options_inheritable+=($option_long)
        fi
        _builder_options_short[$option_short]="$option_long"
        if [[ ! -z "$option_var" ]]; then
          _builder_options_var[$option_long]="$option_var"
        fi
        value="$option_long, $option_short"
      else
        _builder_options+=($value)
        if $is_inheritable; then
          _builder_options_inheritable+=($value)
        fi
        if [[ ! -z "$option_var" ]]; then
          _builder_options_var[$value]="$option_var"
        fi
      fi

      if [[ ! -z $option_var ]]; then
        value="$value $option_var"
      fi

    else
      # Parameter is an action
      if [[ $value =~ \+$ ]]; then
        # If the action name has a '+' suffix then it is the default action
        value=${value//+}
        _builder_default_action=$value
      fi
      _builder_actions+=($value)
    fi

    if [[ -z "${description}" ]]; then
      description=$(_builder_get_default_description "$original_value")
    fi
    _builder_params[${original_value}]="$description"

    shift
  done

  # We'll always add a :project if no target is specified
  if (( ! ${#_builder_targets[@]} )); then
    _builder_targets+=(:project)
    _builder_params[\:project]=$(_builder_get_default_description ":project")
  fi
}

#
# Defines an output file or folder expected to be present after successful
# completion of an action for a target. Used to skip actions for dependency
# builds. If `:target` is not provided, assumes `:project`.
#
# Relative paths are relative to script folder; absolute paths are relative
# to repository root, not filesystem root.
#
# ### Usage
#
# ```bash
#   builder_describe_outputs action:target filename [...]
# ```
#
# ### Parameters
#
# * 1: `action[:target]`   action and/or target associated with file
# * 2: `filename`          name of file or folder to check
# * 3+: ... repeat previous arguments for additional outputs
#
# ### Example
#
# ```bash
#   builder_describe_outputs \
#     configure   /node_modules \
#     build       build/index.js
# ```
#
function builder_describe_outputs() {
  _builder_record_function_call builder_describe_outputs

  while [[ $# -gt 0 ]]; do
    local key="$1" path="$2" action= target=
    path="`_builder_expand_relative_path "$path"`"

    if [[ $key =~ : ]]; then
      IFS=":" read -r action target <<< "$key"
      target=":$target"
    else
      # Add dependency expected output file for all targets, as well as a
      # wildcard target match
      action="$key"
      for target in "${_builder_targets[@]}"; do
        _builder_dep_path[$action$target]="$path"
      done
      target=':*'
    fi
    _builder_dep_path[$action$target]="$path"
    shift 2
  done

  _builder_define_default_internal_dependencies

  # We only want to define internal dependencies after both builder_parse and builder_describe_outputs have been called
  if _builder_has_function_been_called builder_parse; then
    _builder_add_chosen_action_target_dependencies
  fi
}

_builder_get_default_description() {
  local description=
  local value="$1"
  # default descriptions for common build actions, targets, and options
  case "$value" in
    clean)     description="remove build/ folder and build artifacts" ;;
    configure) description="install dependencies, e.g. npm" ;;
    build)     description="build target(s)" ;;
    test)      description="run automated tests" ;;
    :project)  description="this project" ;;
    :app)      description="main app" ;;
    :engine)   description="engine module" ;;
    :module)   description="this module" ;;
    :tools)    description="build tools for this project" ;;
    --debug)   description="debug build" ;;
  esac
  echo "$description"
}

_builder_parameter_error() {
  local program="$1"
  local type="$2"
  local param="$3"
  builder_echo red "$program: invalid $type: $param"
  echo
  builder_display_usage
  exit 64
}

#
# Pre-initializes the color setting based on the options specified to a
# a build.sh script. This is called automatically during init.
#
# Parameters
#   1: "$@"         all command-line arguments
#
_builder_check_color() {
  # Process command-line arguments
  while [[ $# -gt 0 ]] ; do
    local key="$1"

    case "$key" in
      --color)
        builder_use_color true
        ;;
      --no-color)
        builder_use_color false
        ;;
    esac
    shift # past the processed argument
  done
}

#
# For every build action:target in _builder_chosen_action_targets, add
# its full internal dependency tree
#
_builder_add_chosen_action_target_dependencies() {
  local action_target i=0 new_actions=()

  # Iterate through every action specified on command line; we use this loop
  # style so that any new actions added here will also be iteratively checked
  while (( $i < ${#_builder_chosen_action_targets[@]} )); do
    action_target=${_builder_chosen_action_targets[$i]}

    # If we have an internal dependency for the chosen action:target pair
    if [[ ! -z ${_builder_internal_dep[$action_target]+x} ]]; then
      local dep_outputs=(${_builder_internal_dep[$action_target]}) dep_output
      for dep_output in "${dep_outputs[@]}"; do
        # If there is a defined output for this dependency
        if [[ ! -z ${_builder_dep_path[$dep_output]+x} ]]; then
          # If the output for the dependency is missing, or we have --force-deps
          if [[ ! -e "$REPO_ROOT/${_builder_dep_path[$dep_output]}" ]] || builder_is_full_dep_build; then
            # Add the dependency to the chosen action:target list
            if ! _builder_item_in_array "$dep_output" "${_builder_chosen_action_targets[@]}"; then
              _builder_chosen_action_targets+=($dep_output)
              new_actions+=($dep_output)
            fi
          fi
        fi
      done
    fi
    i=$((i + 1))
  done

  if [[ ${#new_actions[@]} -gt 0 ]]; then
    if builder_is_full_dep_build; then
      builder_echo "Automatically running all dependency actions due to --force-deps:"
    else
      builder_echo "Automatically running following required actions with missing outputs:"
    fi
    for e in "${new_actions[@]}"; do
      builder_echo "* $e"
    done
  fi
}

#
# If we have described outputs, then we will setup our
# default internal dependency chain:
#
#  configure <- build <- (test,install,publish)
#
_builder_define_default_internal_dependencies() {
  for target in "${_builder_targets[@]}"; do
    _builder_define_default_internal_deps_for_target "$target"
  done

  _builder_define_default_internal_deps_for_target ':*'
}

_builder_define_default_internal_deps_for_target() {
  local target=$1
  _builder_define_default_internal_dep "$target" configure build
  _builder_define_default_internal_dep "$target" build test
  _builder_define_default_internal_dep "$target" build install
}

_builder_define_default_internal_dep() {
  local target=$1 dep=$2 action=$3
  if _builder_item_in_array $dep "${_builder_actions[@]}" &&
        _builder_item_in_array $action "${_builder_actions[@]}"; then
    [[ -z ${_builder_internal_dep[$action$target]+x} ]] &&
      _builder_internal_dep[$action$target]=$dep$target ||
      _builder_internal_dep[$action$target]="${_builder_internal_dep[$action$target]} $dep$target"
  fi
}

#
# Define a local dependency between one action:target and
# another.
#
# Usage:
#   builder_describe_internal_dependency action:target depaction:deptarget ...
# Parameters:
#   1: action:target         The action and target that has a dependency
#   2: depaction:deptarget   The dependency action and target
# Example:
#   builder_describe_internal_dependency \
#     build:mac build:mac-x86_64 \
#     build:mac build:mac-arm64
#
# Note: actions and targets must be fully specified, and this _must_
# be called before either builder_describe_outputs or builder_parse in
# order for dependencies to be resolved.
builder_describe_internal_dependency() {
  while [[ $# -gt 0 ]]; do
    local action_target=$1 dep_action_target=$2
    [[ -z ${_builder_internal_dep[$action_target]+x} ]] &&
      _builder_internal_dep[$action_target]=$dep_action_target ||
      _builder_internal_dep[$action_target]="${_builder_internal_dep[$action_target]} $dep_action_target"
    shift 2
  done
}

# Initializes a build.sh script, parses command line. Will abort the script if
# invalid parameters are passed in. Use together with builder_describe which
# sets up the possible command line parameters
#
# Usage:
#   builder_parse "$@"
# Parameters
#   1: $@         command-line arguments
builder_parse() {
  _builder_record_function_call builder_parse

  local exp=()
  builder_extra_params=()

  while [[ $# -gt 0 ]] ; do
    local action= target=
    local key="$1"
    if [[ $key == "--" ]]; then
      shift
      builder_extra_params=("$@")
      break
    fi

    if [[ $key =~ ^- ]]; then

      # Expand short -o to --option in options lookup
      if [[ ! -z ${_builder_options_short[$key]+x} ]]; then
        key=${_builder_options_short[$key]}
      fi

      exp+=($key)
      if [[ ! -z ${_builder_options_var[$key]+x} ]]; then
        shift
        if [[ $# -eq 0 ]]; then
          _builder_parameter_error "$0" parameter "$key"
        fi
        exp+=("$1")
      fi
    else
      # Expand comma separated values
      if [[ $key =~ : ]]; then
        IFS=: read -r action target <<< "$key"
      else
        action="$key"
        target=
      fi

      local actions targets
      IFS=, read -r -a actions <<< "$action"
      IFS=, read -r -a targets <<< "$target"

      if [[ "${#actions[@]}" -eq 0 ]]; then
        # No actions, so must be at least one :target
        for target in "${targets[@]}"; do
          exp+=(:$target)
        done
      else
        for action in "${actions[@]}"; do
          if [[ "${#targets[@]}" -eq 0 ]]; then
            # No :targets so just expand actions
            exp+=($action)
          else
            # Actions:targets, expand them all
            for target in "${targets[@]}"; do
              exp+=($action:$target)
            done
          fi
        done
      fi
    fi

    shift
  done

  _builder_parse_expanded_parameters "${exp[@]}"
}

_builder_parse_expanded_parameters() {
  _builder_build_deps=--deps
  builder_verbose=
  builder_debug=
  local _params=($@)
  _builder_chosen_action_targets=()
  _builder_chosen_options=()
  _builder_current_action=
  _builder_is_child=1
  _builder_offline=

  local n=0

  # Process command-line arguments
  while [[ $# -gt 0 ]] ; do
    local key="$1"
    local action=
    local target=
    local e has_action has_target has_option longhand_option

    if [[ $key =~ : ]]; then
      IFS=: read -r action target <<< "$key"
      target=:$target
    else
      action="$key"
      target=
    fi

    # Expand shorthand parameters

    new_action=$(_builder_expand_shorthand $action "${_builder_actions[@]}") ||
      case $? in
        1)
          action=$new_action
          ;;
        *)
          builder_warn "Parameter $action has $? matches, could mean any of {$new_action}"
          exit 1
          ;;
      esac

    new_target=$(_builder_expand_shorthand $target "${_builder_targets[@]}") ||
      case $? in
        1)
          target=$new_target
          ;;
        *)
          builder_warn "Parameter $target has $? matches, could mean any of {$new_target}"
          exit 1
          ;;
      esac

    _builder_item_in_array "$action" "${_builder_actions[@]}" && has_action=1 || has_action=0
    _builder_item_in_array "$target" "${_builder_targets[@]}" && has_target=1 || has_target=0

    if (( has_action )) || (( has_target )); then
      # Document parameter expansion for end use
      _params[$n]=$action$target
    fi
    n=$((n + 1))

    _builder_item_in_array "$key" "${_builder_options[@]}" && has_option=1 || has_option=0

    if (( has_action )) && (( has_target )); then
      # apply the selected action and selected target
      _builder_chosen_action_targets+=("$action$target")
    elif (( has_action )); then
      # apply the selected action to all targets
      if [[ ! -z $target ]]; then
        # A target was specified but is not valid
        if builder_is_target_excluded_by_platform "$target"; then
          builder_echo red "$0: Target $target is unavailable because it requires ${_builder_targets_excluded_by_platform[$target]}"
          echo
          builder_display_usage
          exit 64
        else
          _builder_parameter_error "$0" target "$target"
        fi
      fi

      for e in "${_builder_targets[@]}"; do
        _builder_chosen_action_targets+=("$action$e")
      done
    elif (( has_target )); then
      # apply the default action to the selected target

      if [[ ! -z $action ]]; then
        # An action was specified but is not valid
        _builder_parameter_error "$0" action "$action"
      fi

      _builder_chosen_action_targets+=("$_builder_default_action$target")
    elif (( has_option )); then
      _builder_chosen_options+=("$key")
      if [[ ! -z ${_builder_options_var[$key]+x} ]]; then
        shift
        n=$((n + 1))
        # Set the variable associated with this option to the next parameter value
        # A little bit of hoop jumping here to avoid issues with cygwin paths being
        # corrupted too early in the game
        local varname=${_builder_options_var[$key]}
        declare -g $varname="$1"
      fi

    else
      case "$key" in
        --help|-h)
          builder_display_usage
          exit 0
          ;;
        --color)
          builder_use_color true
          ;;
        --no-color)
          builder_use_color false
          ;;
        --verbose|-v)
          _builder_chosen_options+=(--verbose)
          builder_verbose=--verbose
          ;;
        --debug|-d)
          _builder_chosen_options+=(--debug)
          builder_debug=--debug
          ;;
        --deps|--no-deps|--force-deps)
          _builder_build_deps=$key
          ;;
        --builder-dep-parent)
          # internal use parameter for dependency builds - identifier of parent script
          shift
          builder_dep_parent="$1"
          builder_echo setmark "dependency build, started by $builder_dep_parent"
          builder_echo grey "$(basename "$0") parameters: <${_params[@]}>"
          ;;
        --builder-child)
          _builder_is_child=0
          builder_echo setmark "child build, parameters: <${_params[@]}>"
          ;;
        --builder-report-dependencies)
          # internal reporting function, ignores all other parameters
          _builder_report_dependencies
          ;;
        --builder-completion-describe)
          _builder_completion_describe
          exit 0
          ;;
        --offline)
          _builder_offline=--offline
          ;;
        *)
          # script does not recognize anything of action or target form at this point.
          if [[ $key =~ ^: ]]; then
            # This is an unsupported target
            if builder_is_target_excluded_by_platform "$key"; then
              builder_echo red "$0: Target $key is unavailable because it requires ${_builder_targets_excluded_by_platform[$key]}"
              echo
              builder_display_usage
              exit 64
            else
              _builder_parameter_error "$0" target "$key"
            fi
          elif builder_is_child_build; then
            # For child builds, don't fail the build when pass inheritable
            # parameters (#11408)
            builder_echo_debug "Parameter '$key' is not supported, ignoring"
          else
            _builder_parameter_error "$0" parameter "$key"
          fi
      esac
    fi
    shift # past the processed argument
  done

  if (( ! ${#_builder_chosen_action_targets[@]} )); then
    for e in "${_builder_targets[@]}"; do
      _builder_chosen_action_targets+=("$_builder_default_action$e")
    done
  fi

  # We only want to define internal dependencies after both builder_parse and builder_describe_outputs have been called
  if _builder_has_function_been_called builder_describe_outputs; then
    _builder_add_chosen_action_target_dependencies
  fi

  if $_builder_debug_internal; then
    builder_echo_debug "Selected actions and targets:"
    for e in "${_builder_chosen_action_targets[@]}"; do
      builder_echo_debug "* $e"
    done
    builder_echo_debug
    builder_echo_debug "Selected options:"
    for e in "${_builder_chosen_options[@]}"; do
      builder_echo_debug "* $e"
    done
  fi

  if builder_is_dep_build; then
    if [[ -z ${_builder_deps_built+x} ]]; then
      builder_die "FATAL ERROR: Expected '_builder_deps_built' variable to be set"
    fi
  elif builder_is_child_build; then
    if [[ -z ${_builder_deps_built+x} ]]; then
      builder_die "FATAL ERROR: Expected '_builder_deps_built' variable to be set"
    fi
  else
    # This is a top-level invocation, so we want to track which dependencies
    # have been built, so they don't get built multiple times.
    builder_echo setmark "$(basename "$0") parameters: <${_params[@]}>"
    if [[ ${#builder_extra_params[@]} -gt 0 ]]; then
      builder_echo grey "$(basename "$0") extra parameters: <${builder_extra_params[@]}>"
    fi
    export _builder_deps_built=`mktemp`
  fi

  if builder_is_debug_build; then
    BUILDER_CONFIGURATION=debug
  else
    BUILDER_CONFIGURATION=release
  fi

  # Now that we've successfully parsed options adhering to the _builder spec, we may activate our
  # action_failure and action_hanging traps.  (We don't want them active on scripts not yet using
  # said script.)
  #
  # Note:  if an error occurs within a script's function in a `set -e` script, it becomes an exit
  # instead for the function's caller.  So, we need both `err` and `exit` here.
  # See https://medium.com/@dirk.avery/the-bash-trap-trap-ce6083f36700.
  if [[ -z "$(trap -p DEBUG)" ]]; then
    # not running in bashdb
    trap _builder_failure_trap err exit
  fi
}

_builder_pad() {
  local count=$1
  local text1=$2
  local text2=$3
  local fmt="%-${count}s%s\n"
  printf $fmt "$text1" "$text2"
}

_builder_completion_describe() {
  printf '%s ' "${_builder_actions[@]}"
  echo -n "; "
  printf '%s ' "${_builder_targets[@]}"
  echo -n "; "
  # Remove all '+' suffixes from options; they're a config on the option, not part
  # of the actual option text itself.
  local _builder_opts=()
  for e in "${!_builder_params[@]}"; do
    if [[ $e =~ ^-- ]]; then
      _builder_opts+=(${e%+*})
    fi
  done

  # Add default options
  _builder_opts+=( --verbose --debug --color --no-color --offline --help )
  if builder_has_dependencies; then
    _builder_opts+=( --deps --no-deps --force-deps )
  fi
  printf '%s ' "${_builder_opts[@]}"
}

builder_display_usage() {
  local e program description

  # Minimum padding is 12 characters, increase this if necessary
  # if you add other, longer, global options (like --verbose, --debug)
  local width=12

  for e in "${!_builder_params[@]}"; do
    if (( ${#e} > $width )); then
      width=${#e}
    fi
  done

  width=$((width + 6))

  program="$(basename "$0")"
  if [[ ! -z ${_builder_description+x} ]]; then
    echo "Summary:"
    echo "  $_builder_description"
    echo
  fi
  echo "Script Identifier:"
  echo "  $THIS_SCRIPT_IDENTIFIER"
  echo

  echo "Usage:"
  echo "  $program [options...] [action][:target]..."
  echo
  echo "Actions: "

  for e in "${_builder_actions[@]}"; do
    if [[ ! -z "${_builder_params[$e]+x}" ]]; then
      description="${_builder_params[$e]}"
    else
      description=$(_builder_get_default_description "$e")
    fi
    _builder_pad $width "  $e" "$description"
  done

  echo
  echo "Targets: "

  for e in "${_builder_targets[@]}"; do
    if [[ ! -z "${_builder_params[$e]+x}" ]]; then
      description="${_builder_params[$e]}"
    else
      description=$(_builder_get_default_description "$e")
    fi
    _builder_pad $width "  $e" "$description"
  done

  if [[ ${#_builder_targets_excluded_by_platform[@]} -gt 0 ]]; then
    echo
    echo "Unavailable Targets: "
    for e in "${!_builder_targets_excluded_by_platform[@]}"; do
      if [[ ! -z "${_builder_params[$e]+x}" ]]; then
        description="${_builder_params[$e]}"
      else
        description=$(_builder_get_default_description "$e")
      fi
      _builder_pad $width "  $e" "$description (requires ${_builder_targets_excluded_by_platform[$e]})"
    done
  fi

  echo
  echo "Options: "

  for e in "${!_builder_params[@]}"; do
    if [[ $e =~ ^-- ]]; then
      _builder_pad $width "  $e" "${_builder_params[$e]}"
    fi
  done

  _builder_pad $width "  --verbose, -v"  "Verbose logging"
  _builder_pad $width "  --debug, -d"    "Debug build"
  _builder_pad $width "  --color"        "Force colorized output"
  _builder_pad $width "  --no-color"     "Never use colorized output"
  _builder_pad $width "  --offline"      "Try to build while being offline"
  if builder_has_dependencies; then
    _builder_pad $width "  --deps"         "Build dependencies if required (default)"
    _builder_pad $width "  --no-deps"      "Skip build of dependencies"
    _builder_pad $width "  --force-deps"   "Reconfigure and rebuild all dependencies"
  fi
  _builder_pad $width "  --help, -h"     "Show this help"

  echo
  echo "Dependencies: "

  if builder_has_dependencies; then
    for d in "${_builder_deps[@]}"; do
      echo "  $d"
    done
  else
    echo "  This module has no dependencies"
  fi

  # Defined in `builder_use_color`; this assumes that said func has been called.
  local c1=$BUILDER_TERM_START
  local c0=$BUILDER_TERM_END
  echo
  echo -e "* Specify ${c1}action:target${c0} to run a specific ${c1}action${c0} against a specific ${c1}:target${c0}."
  echo -e "* If ${c1}action${c0} is specified without a ${c1}target${c0} suffix, it will be applied to all ${c1}:target${c0}s."
  echo -e "* If ${c1}:target${c0} is specified without an ${c1}action${c0} prefix, ${c1}$_builder_default_action:target${c0} will be inferred."
  echo -e "* If no ${c1}action${c0}, ${c1}:target${c0}, or ${c1}action:target${c0} entries are specified, ${c1}$_builder_default_action${c0} will run on all ${c1}:target${c0}s."
  echo
}

builder_finish_action() {
  local result="$1"
  local action="$2" target action_name

  if [[ $action =~ : ]]; then
    IFS=: read -r action target <<< "$action"
    target=":$target"
  else
    target=':*'
  fi

  if [[ "$target" == ":*" ]]; then
    action_name="$action"
  else
    action_name="$action$target"
  fi

  local matched_action="$action$target"

  if [[ "$matched_action" == "${_builder_current_action}" ]]; then
    if [[ $result == success ]]; then
      # Sanity check:  if there is a described output for this action, does the corresponding
      # file or directory exist now?
      if _builder_dep_output_defined $matched_action && ! _builder_dep_output_exists "$matched_action"; then
        builder_echo warning "Expected output: '${_builder_dep_path[$matched_action]}'."
        builder_echo warning "## $action_name completed successfully, but output does not exist"
      else
        builder_echo success "## $action_name completed successfully"
      fi
    elif [[ $result == failure ]]; then
      builder_echo error "## $action_name failed"
    else
      builder_echo error "## $action_name failed with message: $result"
    fi

    # Remove $action$target from the array; it is no longer a current action
    _builder_current_action=
  else
    builder_echo warning "reporting result of $action_name but the action was never started!"
  fi
}

#------------------------------------------------------------------------------------------
# Dependencies
#------------------------------------------------------------------------------------------

_builder_dep_output_defined() {
  if [[ ! -z ${_builder_dep_path[$1]+x} ]]; then
    return 0
  else
    return 1
  fi
}

_builder_dep_output_exists() {
  if _builder_dep_output_defined $1 && [[ -e "$REPO_ROOT/${_builder_dep_path[$1]}" ]]; then
    return 0
  else
    return 1
  fi
}

#
# Returns `0` if the dependency should be built for the given action:target
#
_builder_should_build_dep() {
  local action_target="$1"
  local dep="$2"
  local related_actions=(${_builder_dep_related_actions[$dep]})

  if [[ $action_target =~ ^clean ]]; then
    # don't attempt to build dependencies for a 'clean' action
    return 1
  fi

  if ! _builder_item_in_glob_array "$action_target" "${related_actions[@]}"; then
    return 1
  fi
  return 0
}

#
# Removes a dependency from the list of available dependencies
#
# Parameters:
#   $1    path to dependency
#
builder_remove_dep() {
  local dependency="$1" i
  dependency="`_builder_expand_relative_path "$dependency"`"

  for i in "${!_builder_deps[@]}"; do
    if [[ ${_builder_deps[i]} = $dependency ]]; then
      unset '_builder_deps[i]'
    fi
  done

  # rebuild the array to remove the empty item
  _builder_deps=( "${_builder_deps[@]}" )
}

#
# Configure and build all dependencies
# Later, may restrict by either action or target
#
_builder_do_build_deps() {
  local action_target="$1"

  if [[ $_builder_build_deps == --no-deps ]]; then
    # we've been asked to skip dependencies
    return 0
  fi

  for dep in "${_builder_deps[@]}"; do
    # Don't attempt to build dependencies that don't match the current
    # action:target (wildcards supported for matches here)
    if ! _builder_should_build_dep "$action_target" "$dep"; then
      builder_echo "Skipping dependency $dep for $_builder_matched_action_name"
      continue
    fi

    dep_target=
    if [[ ! -z ${_builder_dep_targets[$dep]+x} ]]; then
      # TODO: in the future split _builder_dep_targets into comma-separated
      #       array for multiple targets for a dep?
      dep_target=${_builder_dep_targets[$dep]}
    fi

    # Only configure and build the dependency once per invocation
    if builder_has_module_been_built "$dep$dep_target"; then
      continue
    fi

    builder_set_module_has_been_built "$dep$dep_target"
    "$REPO_ROOT/$dep/build.sh" "configure$dep_target" "build$dep_target" \
      $builder_verbose \
      $builder_debug \
      $_builder_offline \
      $_builder_build_deps \
      --builder-dep-parent "$THIS_SCRIPT_IDENTIFIER" && (
      if $_builder_debug_internal; then
        builder_echo success "## Dependency $dep$dep_target for $_builder_matched_action_name successfully"
      fi
    ) || (
      result=$?
      builder_echo error "## Dependency failed with exit code $result"
      exit $result
    ) || exit $? # Required due to above subshell masking exit
  done
}

#
# returns `0` if we are in a dependency doing a build.
#
builder_is_dep_build() {
  if [[ ! -z ${builder_dep_parent+x} ]]; then
    return 0
  fi
  return 1
}

#
# returns `0` if we are in a child script doing a build
#
builder_is_child_build() {
  return $_builder_is_child
}

#
# return `1` for a regular build, `0` to try to build while being offline.
# The offline build might fail if not all dependencies are cached.
#
builder_try_offline() {
  if [[ "${_builder_offline}" == "--offline" ]]; then
    return 0
  else
    return 1
  fi
}

#
# returns `0` if we should attempt to do quick builds in a dependency build, for
# example skipping `tsc -b` where a parent may also do it; corresponds to the
# `--deps` parameter (which is the default).
#
builder_is_quick_dep_build() {
  if [[ $_builder_build_deps == --deps ]]; then
    return 0
  fi
  return 1
}

#
# returns `0` if we should do a full configure and build in a dependency build;
# corresponds to the `--force-deps`` parameter.
#
builder_is_full_dep_build() {
  if [[ $_builder_build_deps == --force-deps ]]; then
    return 0
  fi
  return 1
}

#
# returns `0` if the current build script has at least one dependency.
#
builder_has_dependencies() {
  if [[ ${#_builder_deps[@]} -eq 0 ]]; then
    return 1
  fi
  return 0
}

#
# Tests if a dependency module has been built already in the current script
# invocation; if not running in a builder context, always returns `1` (i.e.
# "false").
#
# ### Usage
#
# ```bash
# builder_has_module_been_built dependency-name
# ```
#
# ### Parameters
#
# * 1: `dependency-name`   the `$SCRIPT_IDENTIFIER` of the dependency
#                          (repo-relative path without leading `/`); or for
#                          external dependencies, a path-like starting with
#                          `/external/`.
#
# ### Examples
#
# ```bash
#   if builder_has_module_been_built common/web/keyman-version; then ...
#   if builder_has_module_been_built /external/npm-ci; then ...
# ```
#
builder_has_module_been_built() {
  local module="$1"


  if [[ -z ${_builder_deps_built+x} ]]; then
    # not in a builder context, so we assume a build is needed
    return 1
  fi

  if [[ -f $_builder_deps_built ]] && grep -qx "$module" $_builder_deps_built; then
    # dependency history file contains the dependency module
    return 0
  fi
  return 1
}

#
# Updates the dependency module build state for the current script invocation;
# if not running in a builder context, a no-op.
#
# ### Usage
#
# ```bash
#   builder_set_module_has_been_built dependency-name
# ```
#
# ### Parameters
#
# * 1: `dependency-name`   the `$SCRIPT_IDENTIFIER` of the dependency
#                          (repo-relative path without leading `/`); or for
#                          external dependencies, a path-like starting with
#                          `/external/`.
#
# ### Examples
#
# ```bash
#   builder_set_module_has_been_built common/web/keyman-version
#   builder_set_module_has_been_built /external/npm-ci
# ```
#
builder_set_module_has_been_built() {
  local module="$1"

  if [[ ! -z ${_builder_deps_built+x} ]]; then
    echo "$module" >> $_builder_deps_built
  fi
}

#
# Reports on all described dependencies, then exits
# used by builder-controls.sh
#
_builder_report_dependencies() {
  echo "${_builder_deps[@]}"
  exit 0
}

#------------------------------------------------------------------------------------------
# Utility functions
#------------------------------------------------------------------------------------------

#
# returns `0` if we should be verbose in output
#
builder_verbose() {
  if [[ $builder_verbose == --verbose ]]; then
    return 0
  fi
  return 1
}

#
# returns `0` if we are doing a debug build
#
builder_is_debug_build() {
  if [[ $builder_debug == --debug ]]; then
    return 0
  fi
  return 1
}

#
# Track whether functions have already been called;
# later we may use this to prevent multiple calls to, e.g.
# builder_describe
#

_builder_function_calls=()

_builder_record_function_call() {
  local func=$1
  if _builder_has_function_been_called $1; then
    # builder_die "ERROR: $func cannot be called more than once."
    return 0
  fi
  _builder_function_calls+=($1)
}

_builder_has_function_been_called() {
  local func=$1
  if _builder_item_in_array $1 "${_builder_function_calls[@]}"; then
    return 0
  fi
  return 1
}

################################################################################
# Platform restrictions
################################################################################

# Describes the platforms for which a given target will be available, and
# filters the list of targets accordingly, removing targets that cannot be built
# on the current platform. Removed targets will be listed in a separate section
# in the help documentation.
#
# Parameters: target platform[s] ...     Target and its list of corresponding
#                                        platform(s)
#
# Multiple targets can be listed. Each target must be followed by a comma
# separated list of platforms. The currently supported platforms are:
#
# Operating System platforms:
#   win        Windows
#   mac        macOS
#   linux      Linux
#
# Development tooling platforms:
#   delphi     Delphi is installed (on Windows only)
#   android    Android Studio is installed
#
builder_describe_platform() {

  local builder_platforms=(linux mac win)
  local builder_tools=(android-studio delphi)

  # --- Detect platform ---

  # Default value, since it's the most general case/configuration to detect.
  local builder_platform=linux

  # This is copied from build-utils.sh to avoid creating a dependency on it
  if [[ $OSTYPE == darwin* ]]; then
    builder_platform=mac
  elif [[ $OSTYPE == msys ]]; then
    builder_platform=win
  elif [[ $OSTYPE == cygwin ]]; then
    builder_platform=win
  fi

  # --- Detect tools ---

  local builder_installed_tools=()

  # Detect delphi compiler (see also delphi_environment.inc.sh)
  if [[ $builder_platform == win ]]; then
    local ProgramFilesx86="$(cygpath -w -F 42)"
    if [[ -x "$(cygpath -u "$ProgramFilesx86\\Embarcadero\\Studio\\20.0\\bin\\dcc32.exe")" ]]; then
      builder_installed_tools+=(delphi)
    fi
  fi

  # Detect Android Studio based on ANDROID_HOME environment variable
  if [[ ! -z ${ANDROID_HOME+x} ]]; then
    builder_installed_tools+=(android-studio)
  fi

  # --- Overrides ---

  # For testing, we can override the current platform
  if [[ ! -z "${BUILDER_PLATFORM_OVERRIDE+x}" ]]; then
    builder_warn "BUILDER_PLATFORM_OVERRIDE variable found. Overriding detected platform '$builder_platform' with '$BUILDER_PLATFORM_OVERRIDE'"
    builder_platform="$BUILDER_PLATFORM_OVERRIDE"
  fi

  # For testing, we can override the current tools
  if [[ ! -z "${BUILDER_TOOLS_OVERRIDE+x}" ]]; then
    builder_warn "BUILDER_TOOLS_OVERRIDE variable found. Overriding detected tools (${builder_installed_tools[@]}) with (${BUILDER_TOOLS_OVERRIDE[@]})"
    builder_installed_tools=("${BUILDER_TOOLS_OVERRIDE[@]}")
  fi

  # --- Exclude targets depending on missing tools and platforms
  while [[ $# -gt 1 ]]; do
    local target="$1"
    shift
    local platforms platform
    IFS="," read -r -a platforms <<< "$1"
    shift

    for platform in "${platforms[@]}"; do
      if _builder_item_in_array "$platform" "${builder_platforms[@]}"; then
        # Check operating system
        if [[ "$platform" != "$builder_platform" ]]; then
          _builder_targets_excluded_by_platform[$target]=$platform
          _builder_targets=( ${_builder_targets[@]/$target} )
        fi
      elif _builder_item_in_array "$platform" "${builder_tools[@]}"; then
        # Check installed tools
        if ! _builder_item_in_array "$platform" "${builder_installed_tools[@]}"; then
          _builder_targets_excluded_by_platform[$target]=$platform
          _builder_targets=( ${_builder_targets[@]/$target} )
        fi
      else
        builder_warn "Available platforms: ${builder_platforms[@]}"
        builder_warn "Available tools: ${builder_tools[@]}"
        builder_die "Invalid platform or tool specified for builder_describe_platforms: $platform"
      fi
    done
  done
}

# Returns 0 if the specified target is excluded by platform requirements
#
# Parameters: target     Target to verify
#
builder_is_target_excluded_by_platform() {
  local target="$1"
  if [[ ! -z "${_builder_targets_excluded_by_platform[$target]+x}" ]]; then
    return 0
  fi
  return 1
}

################################################################################
# Final initialization
################################################################################

#
# Initialize builder once all functions are declared
#
_builder_init
_builder_check_color "$@"

# _builder_debug_internal flag can be used to emit verbose logs for builder itself,
# e.g.:
#   _builder_debug_internal=true ./build.sh
#
if [ -z ${_builder_debug_internal+x} ]; then
  _builder_debug_internal=false
fi

if $_builder_debug_internal; then
  builder_echo_debug "Command line: $0 $@"
fi