|
root / base / usr / src / tools / scripts / hh-build
hh-build Plain Text 2697 lines 78.0 KB
   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
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
#!/bin/ksh -p
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2008, 2010, Richard Lowe
# Copyright 2022 Tintri by DDN, Inc. All rights reserved.
# Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
# Copyright (c) 2017 by Delphix. All rights reserved.
# Copyright 2020 Joyent, Inc.
# Copyright 2019 Peter Trible.
# Copyright 2025 Oxide Computer Company
# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
# Copyright 2025 Bill Sommerfeld <sommerfeld@hamachi.org>
# Copyright 2026 Zygaena Project - Hammerhead Build System
#
# Originally "nightly" from the illumos-gate integration folks.
# Renamed to "hh-build" for the Hammerhead/Zygaena project.
#
# Hammerhead modifications:
# - Renamed from "nightly" to "hh-build"
# - Default to GNU Make (gmake) instead of dmake
# - amd64-only build support
# - Git commit hash in log directory names
# - Build metadata written to build-info file
#
# BRINGOVER_WS may be specified in the env file.
# The default is the old behavior of CLONE_WS
#
# -i on the command line, means fast options, so when it's on the
# command line (only), check builds are skipped no matter what
# the setting of their individual flags are in NIGHTLY_OPTIONS.
#
# OPTHOME  may be set in the environment to override /opt
#

#
# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
# under certain circumstances, which can really screw things up; unset it.
#
unset CDPATH

# Get the absolute path of the build script that the user invoked.  This
# may be a relative path, and we need to do this before changing directory.
nightly_path=`whence $0`

#
# Keep track of where we found the build script so we can invoke the
# matching which_scm script.  If that doesn't work, don't go guessing,
# just rely on the $PATH settings, which will generally give us either
# /opt/onbld or the user's workspace.
#
WHICH_SCM=$(dirname $nightly_path)/which_scm
if [[ ! -x $WHICH_SCM ]]; then
	WHICH_SCM=which_scm
fi

function fatal_error
{
	print -u2 "hh-build: $*"
	exit 1
}

#
# Hammerhead: Status output function for build progress visibility
# Writes to both /dev/tty (terminal) and logfile for visibility
#
function build_status
{
	typeset msg="$1"
	typeset timestamp=$(date '+%H:%M:%S')
	# Write to terminal if available (skip in non-interactive sessions)
	(print "---[ $timestamp | $msg ]---" > /dev/tty) 2>/dev/null
	# Also write to logfile
	print "---[ $timestamp | $msg ]---" >> "$LOGFILE" 2>/dev/null || true
}

#
# Function to do a DEBUG and non-DEBUG build. Needed because we might
# need to do another for the source build, and since we only deliver DEBUG or
# non-DEBUG packages.
#
# usage: normal_build
#
function normal_build {

	typeset orig_p_FLAG="$p_FLAG"
	typeset crypto_signer="$CODESIGN_USER"

	suffix=""

	# non-DEBUG build begins

	if [ "$F_FLAG" = "n" ]; then
		set_non_debug_build_flags
		CODESIGN_USER="$crypto_signer" \
		    build "non-DEBUG" "$suffix-nd" "-nd" "$MULTI_PROTO"
	else
		echo "\n==== No non-DEBUG $open_only build ====\n" >> "$LOGFILE"
	fi

	# non-DEBUG build ends

	# DEBUG build begins

	if [ "$D_FLAG" = "y" ]; then
		set_debug_build_flags
		CODESIGN_USER="$crypto_signer" \
		    build "DEBUG" "$suffix" "" "$MULTI_PROTO"
	else
		echo "\n==== No DEBUG $open_only build ====\n" >> "$LOGFILE"
	fi

	# DEBUG build ends

	p_FLAG="$orig_p_FLAG"
}

#
# Build pre-build toolchains before the normal build.
# Order: C toolchain → base tools → OCaml toolchain.
# Each toolchain script uses stamp-file caching to skip rebuilds
# when source is unchanged.
#
# usage: build_toolchain
#
function build_toolchain {

	echo "\n==== Building toolchains ====\n" | \
	    tee -a $mail_msg_file >> $LOGFILE

	typeset gcc_stage=$SRC/tools/.gcc-toolchain
	typeset tools_stage=$SRC/tools/.tools-stage

	# C toolchain (GCC 14.2, binutils, GMP, MPFR, MPC, flex, bison)
	if [[ -x "$SRC/tools/build-gcc.sh" ]]; then
		echo "==== C toolchain ====\n" >> $LOGFILE
		typeset tc_log=$TMPDIR/build-gcc.log
		SRC="$SRC" \
		    CONTRIB="$SRC/contrib" \
		    $SRC/tools/build-gcc.sh \
		    --jobs ${DMAKE_MAX_JOBS:-4} > $tc_log 2>&1
		typeset tc_rc=$?
		cat $tc_log >> $LOGFILE
		cat $tc_log >> $mail_msg_file
		if (( tc_rc != 0 )); then
			build_ok=n
			echo "FATAL: C toolchain build failed" | \
			    tee -a $mail_msg_file >> $LOGFILE
			return
		fi
	fi

	# Note: We do NOT override GNUC_ROOT here.  The env file sets it
	# to /usr (system GCC).  The staged GCC is merged into the proto
	# area by merge_toolchain after the build — it is not used as the
	# build compiler.  Using the staging GCC would fail if the staging
	# is incomplete (missing CRT files, libgcc, etc.).

	# Base tools (gmake, perl, python, autotools, git, curl, etc.)
	if [[ -x "$SRC/tools/build-tools.sh" ]]; then
		echo "==== Base tools ====\n" >> $LOGFILE
		typeset tc_log=$TMPDIR/build-tools.log
		SRC="$SRC" \
		    CONTRIB="$SRC/contrib" \
		    GNUC_ROOT="${GNUC_ROOT:-/usr}" \
		    $SRC/tools/build-tools.sh \
		    --jobs ${DMAKE_MAX_JOBS:-4} > $tc_log 2>&1
		typeset tc_rc=$?
		cat $tc_log >> $LOGFILE
		cat $tc_log >> $mail_msg_file
		if (( tc_rc != 0 )); then
			build_ok=n
			echo "FATAL: Base tools build failed" | \
			    tee -a $mail_msg_file >> $LOGFILE
			return
		fi
	fi

	# Add tools staging area to PATH for subsequent steps
	if [[ -d "$tools_stage/usr/bin" ]]; then
		export PATH="$tools_stage/usr/bin:$PATH"
		echo "Using staged tools: $tools_stage/usr/bin" >> $LOGFILE
	fi

	# Make perl modules built by build-tools.sh (e.g. XML::Parser) visible
	# to normal_build's perl. The build's /usr/bin/perl has @INC baked to
	# /usr/lib/perl5/... but the freshly-built modules live in the staging
	# area until merge_toolchain runs (after normal_build). lib/libbsm's
	# auditxml needs XML::Parser to generate adt_xlate.c during the lib
	# build; without this, the first hh-build on a fresh self-host install
	# (where vendor_perl is empty) fails libbsm — the bug masked on hh-mvp
	# by an accumulated CPAN-installed XML::Parser.
	typeset perl_site="$tools_stage/usr/lib/perl5/site_perl/5.40.1"
	if [[ -d "$perl_site" ]]; then
		export PERL5LIB="$perl_site/amd64-hammerhead-thread-multi:$perl_site"
		echo "PERL5LIB=$PERL5LIB" >> $LOGFILE
	fi

	# Dev toolchain: OCaml, reefc, coral (via bootstrap-hh.sh)
	# Uses stamp-file caching: only rebuilds when source tarballs change.
	# bootstrap-hh.sh is in the repo root (one level above CODEMGR_WS/base/)
	typeset repo_root="${CODEMGR_WS%/base}"
	typeset bootstrap_hh="$repo_root/tools/bootstrap-hh.sh"
	typeset bootstrap_stamp="$repo_root/tools/bootstrap/.bootstrap-hh.stamp"
	typeset bootstrap_sources="$repo_root/tools/bootstrap/sources"
	typeset bootstrap_root="$repo_root/tools/bootstrap/root-hh"

	if [[ -x "$bootstrap_hh" && -d "$bootstrap_sources" ]]; then
		# Check stamp: rebuild if any source tarball or vendor config
		# is newer than the stamp. Uses ksh93's -nt (newer-than) test
		# directly on file mtimes — illumos doesn't ship stat(1), so
		# the prior stat -c/-f chain silently fell back to echo 0 and
		# the comparison was a no-op (it always said "up to date").
		typeset bootstrap_needed=false
		typeset vendored_conf="$repo_root/tools/vendored.conf"
		typeset bootstrap_conf="$repo_root/tools/bootstrap.conf"
		# The staged reefc must exist in root-hh for the later zyginit
		# build. If root-hh was cleaned or a prior bootstrap-hh was
		# interrupted (do_build rm -rf's root-hh up front), a stale stamp
		# alone would wrongly skip the rebuild and leave reefc missing.
		typeset staged_reefc="$repo_root/tools/bootstrap/root-hh/usr/bin/reefc"
		if [[ ! -f "$bootstrap_stamp" || ! -f "$staged_reefc" ]]; then
			bootstrap_needed=true
		else
			# Any tarball newer than stamp -> rebuild
			for tarball in "$bootstrap_sources"/*; do
				[[ -f "$tarball" ]] || continue
				if [[ "$tarball" -nt "$bootstrap_stamp" ]]; then
					bootstrap_needed=true
					break
				fi
			done
			# Vendor config edits (version bumps via update-* scripts)
			# also invalidate the stamp even if tarball mtimes are old.
			if [[ "$bootstrap_needed" == "false" ]]; then
				for cf in "$vendored_conf" "$bootstrap_conf"; do
					[[ -f "$cf" ]] || continue
					if [[ "$cf" -nt "$bootstrap_stamp" ]]; then
						bootstrap_needed=true
						break
					fi
				done
			fi
		fi

		if [[ "$bootstrap_needed" == "true" ]]; then
			echo "==== Dev toolchain (bootstrap-hh.sh) ====\n" >> $LOGFILE
			build_status "Building: dev toolchain (OCaml, reefc, coral)"
			typeset tc_log=$TMPDIR/bootstrap-hh.log

			# bootstrap-hh.sh must run as non-root (prevents /dev/null corruption)
			# and needs zsh (uses 'local' keyword)
			$bootstrap_hh extract > $tc_log 2>&1
			typeset tc_rc=$?
			if (( tc_rc == 0 )); then
				$bootstrap_hh build >> $tc_log 2>&1
				tc_rc=$?
			fi

			cat $tc_log >> $LOGFILE
			cat $tc_log >> $mail_msg_file

			if (( tc_rc != 0 )); then
				build_ok=n
				echo "FATAL: Dev toolchain build failed" | \
				    tee -a $mail_msg_file >> $LOGFILE
			else
				date > "$bootstrap_stamp"
				echo "Dev toolchain complete (stamp: $bootstrap_stamp)" >> $LOGFILE
			fi
		else
			echo "==== Dev toolchain up to date ====\n" >> $LOGFILE
		fi
	fi

	# Extract source-vendored components (zyginit, ...) declared in
	# tools/vendored.conf. Each component's tarball lives in
	# tools/bootstrap/sources/ and gets extracted under $SRC/<name>/.
	# The extracted source is never committed; cmd/init/, cmd/zygctl/,
	# cmd/sysv-wrapper/ Makefiles reference it as build input.
	typeset vendored_conf="$repo_root/tools/vendored.conf"
	typeset vendored_sources="$repo_root/tools/bootstrap/sources"
	if [[ -f "$vendored_conf" ]]; then
		source "$vendored_conf"

		# zyginit. Extract under $SRC/zyginit/source/ so the committed
		# Makefile + enabled.d/ policy at $SRC/zyginit/ survive.
		if [[ -n "${ZYGINIT_TARBALL:-}" ]]; then
			typeset zyginit_tarball="$vendored_sources/$ZYGINIT_TARBALL"
			typeset zyginit_pkg="$SRC/zyginit"
			typeset zyginit_src="$zyginit_pkg/source"

			if [[ ! -f "$zyginit_tarball" ]]; then
				build_ok=n
				echo "FATAL: vendored zyginit tarball not found: $zyginit_tarball" | \
				    tee -a $mail_msg_file >> $LOGFILE
				return
			fi
			if [[ ! -d "$zyginit_pkg" ]]; then
				build_ok=n
				echo "FATAL: $zyginit_pkg missing — Hammerhead's zyginit Makefile is not committed" | \
				    tee -a $mail_msg_file >> $LOGFILE
				return
			fi

			typeset actual_sha
			if command -v sha256sum >/dev/null 2>&1; then
				actual_sha=$(sha256sum "$zyginit_tarball" | awk '{print $1}')
			elif command -v digest >/dev/null 2>&1; then
				actual_sha=$(digest -a sha256 "$zyginit_tarball")
			else
				actual_sha=$(openssl dgst -sha256 "$zyginit_tarball" | awk '{print $NF}')
			fi
			if [[ "$actual_sha" != "$ZYGINIT_SHA256" ]]; then
				build_ok=n
				echo "FATAL: vendored zyginit tarball sha256 mismatch" | \
				    tee -a $mail_msg_file >> $LOGFILE
				echo "  expected: $ZYGINIT_SHA256" >> $LOGFILE
				echo "  actual:   $actual_sha"      >> $LOGFILE
				return
			fi

			echo "==== Extract zyginit $ZYGINIT_VERSION ====\n" >> $LOGFILE
			build_status "Extracting: zyginit $ZYGINIT_VERSION"
			rm -rf "$zyginit_src" "$zyginit_pkg/$ZYGINIT_EXTRACT_DIR" 2>/dev/null
			(cd "$zyginit_pkg" && xz -dc "$zyginit_tarball" | tar xf -) >> $LOGFILE 2>&1
			typeset extract_rc=$?
			if (( extract_rc != 0 )); then
				build_ok=n
				echo "FATAL: zyginit extract failed" | \
				    tee -a $mail_msg_file >> $LOGFILE
				return
			fi
			mv "$zyginit_pkg/$ZYGINIT_EXTRACT_DIR" "$zyginit_src"
			echo "zyginit $ZYGINIT_VERSION → $zyginit_src" >> $LOGFILE
		fi
	fi
}

#
# Merge pre-built toolchain staging areas into the proto area.
# Called after normal_build so that OS-built artifacts are in place
# before the toolchain overlay is applied.
#
# usage: merge_toolchain
#
function merge_toolchain {

	typeset gcc_stage=$SRC/tools/.gcc-toolchain
	typeset tools_stage=$SRC/tools/.tools-stage
	typeset bootstrap_root=${CODEMGR_WS%/base}/tools/bootstrap/root-hh

	echo "\n==== Merging toolchain into proto area ====\n" >> $LOGFILE

	# Rename illumos ld to sunld before merging GNU ld.
	# After merge, /usr/bin/ld = GNU ld (from toolchain),
	# /usr/bin/sunld = illumos ld (for kernel/rtld builds).
	if [[ -f "$ROOT/usr/bin/ld" && ! -f "$ROOT/usr/bin/sunld" ]]; then
		echo "Renaming illumos ld -> sunld" >> $LOGFILE
		cp "$ROOT/usr/bin/ld" "$ROOT/usr/bin/sunld"
	fi

	# Merge GCC toolchain staging area
	if [[ -d "$gcc_stage/usr" ]]; then
		echo "Merging GCC toolchain from $gcc_stage" >> $LOGFILE
		rsync -a \
		    "$gcc_stage/" "$ROOT/" >> $LOGFILE 2>&1
	fi

	# /usr/bin/gnu-ld -> ld: boot/Makefile.inc falls back to this name when
	# the OI-style `gld` isn't present (Hammerhead convention). Without
	# the symlink the boot/efi build fails with "/usr/bin/gnu-ld: No such
	# file or directory" trying to link loader.sym.
	if [[ -f "$ROOT/usr/bin/ld" && ! -e "$ROOT/usr/bin/gnu-ld" ]]; then
		ln -sf ld "$ROOT/usr/bin/gnu-ld"
		echo "Created /usr/bin/gnu-ld -> ld symlink" >> $LOGFILE
	fi

	# Merge base tools staging area.
	# Exclude files that hammerhead builds with illumos-specific formats:
	# - libz: bootstrap copy lacks SUNW_* version symbols
	# - terminfo: bootstrap ncurses compiles 0x011e format, but illumos
	#   libcurses requires 0x011a (SVR4) format from illumos tic
	if [[ -d "$tools_stage/usr" ]]; then
		echo "Merging base tools from $tools_stage" >> $LOGFILE
		rsync -a \
		    --exclude='usr/lib/libz.so*' \
		    --exclude='usr/lib/libz.a' \
		    --exclude='usr/share/lib/terminfo/' \
		    "$tools_stage/" "$ROOT/" >> $LOGFILE 2>&1
	fi

	# Merge dev toolchain (OCaml, reefc, coral) from bootstrap-hh.sh
	if [[ -d "$bootstrap_root/usr" ]]; then
		echo "Merging dev toolchain from $bootstrap_root" >> $LOGFILE
		rsync -a "$bootstrap_root/" "$ROOT/" >> $LOGFILE 2>&1
	fi

	# Merge onbld build tools into base proto so fresh Hammerhead
	# installs are self-host-build-ready without a manual seed step.
	# The in-tree proto is populated as a build-time bootstrap (the OS
	# build reads ONBLD_TOOLS=$TOOLS_PROTO/opt/onbld); merging it into
	# $ROOT/opt/onbld ships the same tree on the installed system.
	typeset onbld_stage=$SRC/tools/proto/root_$MACH-nd
	if [[ -d "$onbld_stage/opt/onbld" ]]; then
		echo "Merging onbld build tools from $onbld_stage" >> $LOGFILE
		mkdir -p "$ROOT/opt"
		rsync -a "$onbld_stage/opt/onbld" "$ROOT/opt/" >> $LOGFILE 2>&1
	fi

	# Fix shebangs — strip staging area paths from interpreter lines.
	# Tools built with DESTDIR get shebangs like
	# #!/path/to/stage/usr/bin/perl; these need to become
	# #!/usr/bin/perl for the deployed system.
	echo "Fixing shebangs for deployment" >> $LOGFILE
	for f in "$ROOT"/usr/local/bin/* "$ROOT"/usr/bin/*; do
		[[ -f "$f" ]] || continue
		typeset shebang
		shebang=$(head -1 "$f" 2>/dev/null) || continue
		if [[ "$shebang" == *"$gcc_stage"* ]]; then
			sed -i "1s|$gcc_stage||" "$f"
		elif [[ "$shebang" == *"$tools_stage"* ]]; then
			sed -i "1s|$tools_stage||" "$f"
		elif [[ "$shebang" == *"$bootstrap_root"* ]]; then
			sed -i "1s|$bootstrap_root||" "$f"
		fi
	done

	# Fix GCC internal ld symlink: must point to gnu-ld-wrapper
	# (GCC's collect2 follows this symlink to invoke the linker)
	typeset gcc_ld="$ROOT/usr/x86_64-pc-solaris2.11/bin/ld"
	if [[ -f "$gcc_ld" || -L "$gcc_ld" ]]; then
		ln -sf /usr/local/bin/gnu-ld-wrapper "$gcc_ld"
		echo "GCC internal ld -> gnu-ld-wrapper" >> $LOGFILE
	fi

	# Install gnu-ld-wrapper + illumos-mapfile-convert to proto area.
	# Source is in the repo tools/ directory; destination is both
	# /usr/local/bin/ (where GCC's collect2 chain finds it) and
	# /usr/bin/ (convenience).  Both scripts must be co-located — the
	# wrapper resolves $SCRIPT_DIR at runtime to find its converter.
	typeset repo_tools=${CODEMGR_WS%/base}/tools
	typeset wrapper=$repo_tools/gnu-ld-wrapper
	typeset converter=$repo_tools/illumos-mapfile-convert
	if [[ -f "$wrapper" ]]; then
		mkdir -p "$ROOT/usr/local/bin" "$ROOT/usr/bin"
		cp "$wrapper" "$ROOT/usr/local/bin/gnu-ld-wrapper"
		cp "$wrapper" "$ROOT/usr/bin/gnu-ld-wrapper"
		chmod 755 "$ROOT/usr/local/bin/gnu-ld-wrapper" \
		          "$ROOT/usr/bin/gnu-ld-wrapper"
		echo "gnu-ld-wrapper installed to proto" >> $LOGFILE
	fi
	if [[ -f "$converter" ]]; then
		mkdir -p "$ROOT/usr/local/bin" "$ROOT/usr/bin"
		cp "$converter" "$ROOT/usr/local/bin/illumos-mapfile-convert"
		cp "$converter" "$ROOT/usr/bin/illumos-mapfile-convert"
		chmod 755 "$ROOT/usr/local/bin/illumos-mapfile-convert" \
		          "$ROOT/usr/bin/illumos-mapfile-convert"
		echo "illumos-mapfile-convert installed to proto" >> $LOGFILE
	fi

	# Fix Perl Config.pm — strip staging root from embedded paths
	while IFS= read -r f; do
		if grep -q "$tools_stage" "$f" 2>/dev/null; then
			sed -i "s|$tools_stage||g" "$f"
			echo "  Fixed Perl config: ${f#$ROOT}" >> $LOGFILE
		fi
	done < <(find "$ROOT/usr/lib/perl5" -type f \
	             \( -name 'Config.pm' -o -name 'Config_heavy.pl' \
	                -o -name '.packlist' \) 2>/dev/null)

	echo "Toolchain merge complete" >> $LOGFILE
}

#
# usage: run_hook HOOKNAME ARGS...
#
# If variable "$HOOKNAME" is defined, insert a section header into
# our logs and then run the command with ARGS
#
function run_hook {
	HOOKNAME=$1
	eval HOOKCMD=\$$HOOKNAME
	shift

	if [ -n "$HOOKCMD" ]; then
		(
			echo "\n==== Running $HOOKNAME command: $HOOKCMD ====\n"
			( $HOOKCMD "$@" 2>&1 )
			if [ "$?" -ne 0 ]; then
				# Let exit status propagate up
				touch $TMPDIR/abort
			fi
		) | tee -a $mail_msg_file >> $LOGFILE

		if [ -f $TMPDIR/abort ]; then
			build_ok=n
			echo "\nAborting at request of $HOOKNAME" |
				tee -a $mail_msg_file >> $LOGFILE
			exit 1
		fi
	fi
}

# Return library search directive as function of given root.
function myldlibs {
	echo "-L$1/lib -L$1/usr/lib"
}

# Return header search directive as function of given root.
function myheaders {
	echo "-I$1/usr/include"
}

#
# Function to do the build, including package generation.
# usage: build LABEL SUFFIX ND MULTIPROTO
# - LABEL is used to tag build output.
# - SUFFIX is used to distinguish files (e.g., DEBUG vs non-DEBUG,
#   open-only vs full tree).
# - ND is "-nd" (non-DEBUG builds) or "" (DEBUG builds).
# - If MULTIPROTO is "yes", it means to name the proto area according to
#   SUFFIX.  Otherwise ("no"), (re)use the standard proto area.
#
function build {
	LABEL=$1
	SUFFIX=$2
	ND=$3
	MULTIPROTO=$4
	INSTALLOG=install${SUFFIX}-${MACH}
	NOISE=noise${SUFFIX}-${MACH}
	PKGARCHIVE=${PKGARCHIVE_ORIG}${SUFFIX}

	ORIGROOT=$ROOT
	[ $MULTIPROTO = no ] || export ROOT=$ROOT$SUFFIX

	export ENVLDLIBS1=`myldlibs $ROOT`
	export ENVCPPFLAGS1=`myheaders $ROOT`
	export TIER="$BUILD_TIER"

	this_build_ok=y
	#
	#	Build OS-Networking source
	#
	build_status "Building: OS-Net source ($LABEL, tier=$BUILD_TIER)"
	echo "\n==== Building OS-Net source at `date` ($LABEL) ====\n" \
		>> $LOGFILE

	rm -f $SRC/${INSTALLOG}.out
	cd $SRC
	# Hammerhead: serial build for reliability. Parallel (-j N) causes
	# pervasive race conditions (ctfmerge, header install, locale data).
	/usr/bin/time $MAKE -e install 2>&1 | \
	    tee -a $SRC/${INSTALLOG}.out >> $LOGFILE
	build_status "Completed: OS-Net source ($LABEL)"

	echo "\n==== Build errors ($LABEL) ====\n" >> $mail_msg_file
	egrep ":" $SRC/${INSTALLOG}.out |
	    egrep -e "(^${MAKE}"$':|[\t ]error[:\t ]|ld: guidance:)' | \
	    egrep -v ": (Entering|Leaving) directory " | \
	    egrep -v "Ignoring unknown host" | \
	    egrep -v "cc .* -o error " | \
	    egrep -v "warning" | tee $TMPDIR/build_errs${SUFFIX} \
	    >> $mail_msg_file
	    sed -n $'/^Undefined[\t ]*first referenced$/,/^ld: fatal:/p' \
	    < $SRC/${INSTALLOG}.out >> $mail_msg_file
	if [[ -s $TMPDIR/build_errs${SUFFIX} ]]; then
		build_ok=n
		this_build_ok=n
	fi
	grep "bootblock image is .* bytes too big" $SRC/${INSTALLOG}.out \
		>> $mail_msg_file
	if [ "$?" = "0" ]; then
		build_ok=n
		this_build_ok=n
	fi

	echo "\n==== Build warnings ($LABEL) ====\n" >>$mail_msg_file
	egrep -i 'warn(ing)?:' $SRC/${INSTALLOG}.out \
		| egrep -v '^tic:' \
		| egrep -v "symbol (\`|')timezone' has differing types:" \
		| egrep -v "parameter <PSTAMP> set to" \
		| egrep -v "Ignoring unknown host" \
		| egrep -v "redefining segment flags attribute for" \
		| tee $TMPDIR/build_warnings${SUFFIX} >> $mail_msg_file
	if [[ -s $TMPDIR/build_warnings${SUFFIX} ]]; then
		# Warnings are informational only — actual build failures
		# are caught by error checks (collect2, failed dirs, CTF).
		# GNU Make and GCC 14 produce many harmless warnings that
		# should not fail the build.
		:
	fi

	echo "\n==== Ended OS-Net source build at `date` ($LABEL) ====\n" \
		>> $LOGFILE

	echo "\n==== Elapsed build time ($LABEL) ====\n" >>$mail_msg_file
	tail -3  $SRC/${INSTALLOG}.out >>$mail_msg_file

	if [ "$i_FLAG" = "n" ]; then
		rm -f $SRC/${NOISE}.ref
		if [ -f $SRC/${NOISE}.out ]; then
			mv $SRC/${NOISE}.out $SRC/${NOISE}.ref
		fi
		grep : $SRC/${INSTALLOG}.out \
			| egrep -v '^/' \
			| egrep -v '^(Start|Finish|real|user|sys|./bld_awk)' \
			| egrep -v '^tic:' \
			| egrep -v '^mcs' \
			| egrep -v '^LD_LIBRARY_PATH=' \
			| egrep -v 'ar: creating' \
			| egrep -v 'ar: writing' \
			| egrep -v 'conflicts:' \
			| egrep -v ':saved created' \
			| egrep -v '^stty.*c:' \
			| egrep -v '^mfgname.c:' \
			| egrep -v '^uname-i.c:' \
			| egrep -v '^volumes.c:' \
			| egrep -v '^lint library construction:' \
			| egrep -v 'tsort: INFORM:' \
			| egrep -v 'stripalign:' \
			| egrep -v 'chars, width' \
			| egrep -v "symbol (\`|')timezone' has differing types:" \
			| egrep -v 'PSTAMP' \
			| egrep -v '^Manifying' \
			| egrep -v 'Ignoring unknown host' \
			| egrep -v 'Processing method:' \
			| egrep -v '^Writing' \
			| egrep -v 'spellin1:' \
			| egrep -v '^adding:' \
			| egrep -v "^echo 'msgid" \
			| egrep -v '^echo ' \
			| egrep -v '\.c:$' \
			| egrep -v '^Adding file:' \
			| egrep -v 'CLASSPATH=' \
			| egrep -v '\/var\/mail\/:saved' \
			| egrep -v -- '-DUTS_VERSION=' \
			| egrep -v '^Running Mkbootstrap' \
			| egrep -v '^Applet length read:' \
			| egrep -v 'bytes written:' \
			| egrep -v '^File:SolarisAuthApplet.bin' \
			| egrep -v -i 'jibversion' \
			| egrep -v '^Output size:' \
			| egrep -v '^Solo size statistics:' \
			| egrep -v '^Using ROM API Version' \
			| egrep -v '^Zero Signature length:' \
			| egrep -v '^Note \(probably harmless\):' \
			| egrep -v '::' \
			| egrep -v '^\+' \
			| egrep -v 'svccfg-native -s svc:/' \
			| egrep -v ": (Entering|Leaving) directory " \
			| sort | uniq >$SRC/${NOISE}.out
		if [ ! -f $SRC/${NOISE}.ref ]; then
			cp $SRC/${NOISE}.out $SRC/${NOISE}.ref
		fi
		echo "\n==== Build noise differences ($LABEL) ====\n" \
			>>$mail_msg_file
		diff $SRC/${NOISE}.ref $SRC/${NOISE}.out >>$mail_msg_file
	fi

	#
	#	Re-sign selected binaries using signing server
	#	(gatekeeper builds only)
	#
	if [ -n "$CODESIGN_USER" -a "$this_build_ok" = "y" ]; then
		echo "\n==== Signing proto area at `date` ====\n" >> $LOGFILE
		signing_file="${TMPDIR}/signing"
		rm -f ${signing_file}
		export CODESIGN_USER
		signproto $SRC/tools/codesign/creds 2>&1 | \
			tee -a ${signing_file} >> $LOGFILE
		echo "\n==== Finished signing proto area at `date` ====\n" \
		    >> $LOGFILE
		echo "\n==== Crypto module signing errors ($LABEL) ====\n" \
		    >> $mail_msg_file
		egrep 'WARNING|ERROR' ${signing_file} >> $mail_msg_file
		if (( $? == 0 )) ; then
			build_ok=n
			this_build_ok=n
		fi
	fi

	#
	#	Building Packages
	#
	if [ "$p_FLAG" = "y" -a "$this_build_ok" = "y" ]; then
		if [ -d $SRC/pkg ]; then
			echo "\n==== Creating $LABEL packages at `date` ====\n" \
				>> $LOGFILE
			echo "Clearing out $PKGARCHIVE ..." >> $LOGFILE
			rm -rf $PKGARCHIVE >> "$LOGFILE" 2>&1
			mkdir -p $PKGARCHIVE >> "$LOGFILE" 2>&1

			rm -f $SRC/pkg/${INSTALLOG}.out
			cd $SRC/pkg
			/usr/bin/time $MAKE -e install 2>&1 | \
			    tee -a $SRC/pkg/${INSTALLOG}.out >> $LOGFILE

			echo "\n==== package build errors ($LABEL) ====\n" \
				>> $mail_msg_file

			egrep "${MAKE}|ERROR|WARNING" $SRC/pkg/${INSTALLOG}.out | \
				grep ':' | \
				grep -v PSTAMP | \
				egrep -v ": (Entering|Leaving) directory " | \
				egrep -v "Ignoring unknown host" | \
				tee $TMPDIR/package >> $mail_msg_file
			if [[ -s $TMPDIR/package ]]; then
				build_extras_ok=n
				this_build_ok=n
			fi
		else
			#
			# Handle it gracefully if -p was set but there so
			# no pkg directory.
			#
			echo "\n==== No $LABEL packages to build ====\n" \
				>> $LOGFILE
		fi
	else
		echo "\n==== Not creating $LABEL packages ====\n" >> $LOGFILE
	fi

	ROOT=$ORIGROOT
}

#
# Bootstrap build tools which are pre-requisites for the rest of the build.
#
function bootstrap_tools {
	echo "\n==== Bootstrapping tools at `date` ====\n" >> $LOGFILE

	typeset INSTALLOG=install-bootstrap-${MACH}

	rm -f ${TOOLS}/$INSTALLOG.out
	cd ${TOOLS}
	# Note: dmake used -K statefile for state tracking; gmake doesn't need this
	/usr/bin/time $MAKE -e TARGET=install bootstrap \
	    2>&1 | tee -a ${TOOLS}/$INSTALLOG.out >> $LOGFILE

	echo "\n==== Bootstrap build errors ====\n" >> $mail_msg_file

	egrep ":" ${TOOLS}/$INSTALLOG.out |
	    egrep -e "(${MAKE}"$':|[\t ]error[:\t ])' | \
	    egrep -v ": (Entering|Leaving) directory " | \
	    egrep -v warning | tee $TMPDIR/bootstrap_errors >> $mail_msg_file

	[[ -s $TMPDIR/bootstrap_errors ]] && return 1
	return 0
}

#
# Build and install the tools.
#
# usage: build_tools DESTROOT
#
# returns zero status if the build was successful.
#
function build_tools {
	DESTROOT=$1

	INSTALLOG=install-${MACH}

	build_status "Building: tools (usr/src/tools)"
	echo "\n==== Building tools at `date` ====\n" \
		>> $LOGFILE

	rm -f ${TOOLS}/${INSTALLOG}.out
	cd ${TOOLS}
	/usr/bin/time $MAKE TOOLS_PROTO=${DESTROOT} -e install 2>&1 | \
	    tee -a ${TOOLS}/${INSTALLOG}.out >> $LOGFILE

	echo "\n==== Tools build errors ====\n" >> $mail_msg_file

	egrep ":" ${TOOLS}/${INSTALLOG}.out |
		egrep -e "(${MAKE}"$':|[\t ]error[:\t ])' | \
		egrep -v ": (Entering|Leaving) directory " | \
		egrep -v "Ignoring unknown host" | \
		egrep -v warning | tee $TMPDIR/tools_errors >> $mail_msg_file

	if [[ -s $TMPDIR/tools_errors ]]; then
		return 1
	fi
	return 0
}

function staffer {
	if [ $ISUSER -ne 0 ]; then
		"$@"
	else
		arg="\"$1\""
		shift
		for i
		do
			arg="$arg \"$i\""
		done
		eval su $STAFFER -c \'$arg\'
	fi
}

#
# Verify that the closed bins are present
#
function check_closed_bins {
	if [[ -n "$ON_CLOSED_BINS" && ! -d "$ON_CLOSED_BINS" ]]; then
		echo "ON_CLOSED_BINS must point to the closed binaries tree."
		build_ok=n
		exit 1
	fi
}

#
# Verify /dev/fd is mounted as fdfs. dtrace -C (USDT probe compilation,
# used by libc's plockstat.o build) reads its preprocessed input via
# /dev/fd/3. If fdfs isn't mounted, cc1 fails with
#     fatal error: /dev/fd/3: No such file or directory
# libc install errors out, gmake -k then silently skips every target
# that lists libc as a prereq — the entire first/second .WAIT batches
# in lib/Makefile (libressl, libm, libmd, libmp, libnsl, libz,
# libnvpair, libsocket, libxml2, libnss). Proto ends up missing those
# libraries with no fatal error; downstream link failures cascade for
# the rest of the build.
#
# vfstab lists fd with mount-at-boot=no per illumos convention.
# zyginit's filesystem service mounts it explicitly post-commit
# c4eb1a4c1d (2026-05-22); older deployments lack that line. Fail fast
# instead of producing a polluted proto.
#
function check_devfd {
	if ! /sbin/mount | grep -q '^/dev/fd '; then
		echo "ERROR: /dev/fd is not mounted." >&2
		echo "    dtrace -C (used by libc's plockstat.o build)" \
		    "requires fdfs at /dev/fd." >&2
		echo "    To mount: mount -F fd fd /dev/fd" >&2
		echo "    For persistence, ensure" \
		    "/etc/zyginit/services/filesystem/start.sh contains" \
		    "the mount-fd line (see commit c4eb1a4c1d)." >&2
		build_ok=n
		exit 1
	fi
}

#
# wrapper over wsdiff.
# usage: do_wsdiff LABEL OLDPROTO NEWPROTO
#
function do_wsdiff {
	label=$1
	oldproto=$2
	newproto=$3

	wsdiff="wsdiff"
	[ "$t_FLAG" = y ] && wsdiff="wsdiff -t"

	echo "\n==== Getting object changes since last build at `date`" \
	    "($label) ====\n" | tee -a $LOGFILE >> $mail_msg_file
	$wsdiff -s -r ${TMPDIR}/wsdiff.results $oldproto $newproto 2>&1 | \
		    tee -a $LOGFILE >> $mail_msg_file
	echo "\n==== Object changes determined at `date` ($label) ====\n" | \
	    tee -a $LOGFILE >> $mail_msg_file
}

#
# Functions for setting build flags (DEBUG/non-DEBUG).  Keep them
# together.
#

function set_non_debug_build_flags {
	export RELEASE_BUILD ; RELEASE_BUILD=
	unset EXTRA_OPTIONS
	unset EXTRA_CFLAGS
	if [ -n "$RELEASE_CONSOLE_COLOR" ]; then
		export DEFAULT_CONSOLE_COLOR="$RELEASE_CONSOLE_COLOR"
	fi
}

function set_debug_build_flags {
	unset RELEASE_BUILD
	unset EXTRA_OPTIONS
	unset EXTRA_CFLAGS

	if [ -n "$DEBUG_CONSOLE_COLOR" ]; then
		export DEFAULT_CONSOLE_COLOR="$DEBUG_CONSOLE_COLOR"
	fi
}


MACH=`uname -p`

if [ "$OPTHOME" = "" ]; then
	OPTHOME=/opt
	export OPTHOME
fi

USAGE='Usage: hh-build [-in] [-B WS] [-b BR] [-d WS] [-V VERS] [-T TIER] <env_file>

Where:
	-i	Fast incremental options (no clobber, check)
	-n      Do not do a bringover
	-B WS	bringover from WS (directory or URL)
	-b BR   bringover branch BR
	-d WS	do build in WS (directory)
	-V VERS set HAMMERHEAD_VERSION (default: YYYYMMDD-NN)

	<env_file>  file in Bourne shell syntax that sets and exports
	variables that configure the operation of this script and many of
	the scripts this one calls. If <env_file> does not exist,
	it will be looked for in $OPTHOME/onbld/env.

non-DEBUG is the default build type. Build options must be set in the
NIGHTLY_OPTIONS variable in the <env_file> as follows:

	-A	check for ABI differences in .so files
	-C	check for cstyle/hdrchk errors
	-D	do a build with DEBUG on
	-F	do _not_ do a non-DEBUG build
	-G	gate keeper default group of options (-au)
	-I	integration engineer default group of options (-ampu)
	-L	do not run pkglint
	-M	(no-op, kept for backward compatibility — pmodes removed)
	-N	(no-op, kept for backward compatibility — protocmp removed)
	-R	default group of options for building a release (-mp)
	-U	update proto area in the parent
	-V VERS set HAMMERHEAD_VERSION (default: YYYYMMDD-NN)
	-f	find unreferenced files
	-i	do an incremental build (no "make clobber")
	-m	send mail to $MAILTO at end of build
	-n      do not do a bringover
	-p	create packages
	-r	check ELF runtime attributes in the proto area
	-t	build and use the tools in $SRC/tools (default setting)
	-u	update proto_list_$MACH and friends in the parent workspace;
		when used with -f, also build an unrefmaster.out in the parent
	-w	report on differences between previous and current proto areas
'
#
#	A log file will be generated under the name $LOGFILE
#	for partially completed build and log.`date '+%F'`
#	in the same directory for fully completed builds.
#

# default values for low-level FLAGS; G I R are group FLAGS
A_FLAG=n
C_FLAG=n
D_FLAG=n
F_FLAG=n
f_FLAG=n
i_FLAG=n; i_CMD_LINE_FLAG=n
L_FLAG=n
M_FLAG=n
m_FLAG=n
N_FLAG=n
n_FLAG=n
p_FLAG=n
r_FLAG=n
t_FLAG=n
U_FLAG=n
u_FLAG=n
V_FLAG=n
w_FLAG=n
W_FLAG=n
#
build_ok=y
build_extras_ok=y

#
# examine arguments
#

OPTIND=1
BUILD_TIER="all"
while getopts +b:B:d:intV:WT: FLAG
do
	case $FLAG in
	  b )	BRINGOVER_BRANCH="$OPTARG"
		;;
	  B )	BRINGOVER_WS="$OPTARG"
		;;
	  d )	CODEMGR_WS="$OPTARG"
		;;
	  i )	i_FLAG=y; i_CMD_LINE_FLAG=y
		;;
	  n )	n_FLAG=y
		;;
	  t )	t_FLAG=y
		;;
	 +t )	t_FLAG=n
		;;
	  T )	BUILD_TIER="$OPTARG"
		case "$BUILD_TIER" in
		    kernel|base|all) ;;
		    *) fatal_error "Invalid tier '$BUILD_TIER'. Use: kernel, base, or all" ;;
		esac
		;;
	  V )	V_FLAG=y
		V_ARG="$OPTARG"
		;;
	  W )   W_FLAG=y
		;;
	 \? )	echo "$USAGE"
		exit 1
		;;
	esac
done

# correct argument count after options
shift `expr $OPTIND - 1`

# test that the path to the environment-setting file was given
if [ $# -ne 1 ]; then
	echo "$USAGE"
	exit 1
fi

# check if user is running hh-build as root
# ISUSER is set non-zero if an ordinary user runs hh-build, or is zero
# when root invokes hh-build.
id | grep '^uid=0(' >/dev/null 2>&1
ISUSER=$?;	export ISUSER

# Hammerhead: run privileged commands directly when already root.
# sudo may not be installed on minimal Hammerhead systems.
if [ "$ISUSER" = "0" ]; then
	SUDO=
else
	SUDO=sudo
fi

#
# force locale to C
LANG=C;		export LANG
LC_ALL=C;	export LC_ALL
LC_COLLATE=C;	export LC_COLLATE
LC_CTYPE=C;	export LC_CTYPE
LC_MESSAGES=C;	export LC_MESSAGES
LC_MONETARY=C;	export LC_MONETARY
LC_NUMERIC=C;	export LC_NUMERIC
LC_TIME=C;	export LC_TIME

# clear environment variables we know to be bad for the build
unset LD_OPTIONS
unset LD_AUDIT		LD_AUDIT_32		LD_AUDIT_64
unset LD_BIND_NOW	LD_BIND_NOW_32		LD_BIND_NOW_64
unset LD_BREADTH	LD_BREADTH_32		LD_BREADTH_64
unset LD_CONFIG		LD_CONFIG_32		LD_CONFIG_64
unset LD_DEBUG		LD_DEBUG_32		LD_DEBUG_64
unset LD_DEMANGLE	LD_DEMANGLE_32		LD_DEMANGLE_64
unset LD_FLAGS		LD_FLAGS_32		LD_FLAGS_64
unset LD_LIBRARY_PATH	LD_LIBRARY_PATH_32	LD_LIBRARY_PATH_64
unset LD_LOADFLTR	LD_LOADFLTR_32		LD_LOADFLTR_64
unset LD_NOAUDIT	LD_NOAUDIT_32		LD_NOAUDIT_64
unset LD_NOAUXFLTR	LD_NOAUXFLTR_32		LD_NOAUXFLTR_64
unset LD_NOCONFIG	LD_NOCONFIG_32		LD_NOCONFIG_64
unset LD_NODIRCONFIG	LD_NODIRCONFIG_32	LD_NODIRCONFIG_64
unset LD_NODIRECT	LD_NODIRECT_32		LD_NODIRECT_64
unset LD_NOLAZYLOAD	LD_NOLAZYLOAD_32	LD_NOLAZYLOAD_64
unset LD_NOOBJALTER	LD_NOOBJALTER_32	LD_NOOBJALTER_64
unset LD_NOVERSION	LD_NOVERSION_32		LD_NOVERSION_64
unset LD_ORIGIN		LD_ORIGIN_32		LD_ORIGIN_64
unset LD_PRELOAD	LD_PRELOAD_32		LD_PRELOAD_64
unset LD_PROFILE	LD_PROFILE_32		LD_PROFILE_64

unset CONFIG
unset GROUP
unset OWNER
unset REMOTE
unset ENV
unset ARCH
unset CLASSPATH
unset NAME

#
# To get ONBLD_TOOLS from the environment, it must come from the env file.
# If it comes interactively, it is generally TOOLS_PROTO, which will be
# clobbered before the compiler version checks, which will therefore fail.
#
unset ONBLD_TOOLS

#
#	Setup environmental variables
#
# Hammerhead: renamed from nightly.conf; check both names for compat
if [ -f /etc/hh-build.conf ]; then
	. /etc/hh-build.conf
elif [ -f /etc/nightly.conf ]; then
	. /etc/nightly.conf
fi

if [ -f $1 ]; then
	if [[ $1 == */* ]]; then
		. $1
	else
		. ./$1
	fi
else
	if [ -f $OPTHOME/onbld/env/$1 ]; then
		. $OPTHOME/onbld/env/$1
	else
		echo "Cannot find env file as either $1 or $OPTHOME/onbld/env/$1"
		exit 1
	fi
fi

# Check if we have sufficient data to continue...
[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
if  [[ "${NIGHTLY_OPTIONS}" == ~(F)n ]] ; then
	# Check if the gate data are valid if we don't do a "bringover" below
	[[ -d "${CODEMGR_WS}" ]] || \
		fatal_error "Error: ${CODEMGR_WS} is not a directory."
	[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || \
		fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."
fi

#
# place ourselves in a new task, respecting BUILD_PROJECT if set.
#
if [ -z "$BUILD_PROJECT" ]; then
	newtask -c $$
else
	newtask -c $$ -p $BUILD_PROJECT
fi

ps -o taskid= -p $$ | read build_taskid
ps -o project= -p $$ | read build_project

#
# See if NIGHTLY_OPTIONS is set
#
if [ "$NIGHTLY_OPTIONS" = "" ]; then
	print -u2 "NIGHTLY_OPTIONS must be set in environment file"
	echo "$USAGE"
	exit 1
fi

#
# If BRINGOVER_WS was not specified, let it default to CLONE_WS
#
if [ "$BRINGOVER_WS" = "" ]; then
	BRINGOVER_WS=$CLONE_WS
fi

if [ "$BRINGOVER_REMOTE" = "" ]; then
	BRINGOVER_REMOTE=nightly_bringover_ws
fi

if [ "$BRINGOVER_SCM" = "" ]; then
	BRINGOVER_SCM=git
fi

check_closed_bins
check_devfd

#
# Note: changes to the option letters here should also be applied to the
#	bldenv script.  `d' is listed for backward compatibility.
#
NIGHTLY_OPTIONS=-${NIGHTLY_OPTIONS#-}
OPTIND=1
while getopts +ABCDdFfGIiLMmNnpRrtUuwW FLAG $NIGHTLY_OPTIONS
do
	case $FLAG in
	  A )	A_FLAG=y
		;;
	  B )	D_FLAG=y
		;; # old version of D
	  C )	C_FLAG=y
		;;
	  D )	D_FLAG=y
		;;
	  F )	F_FLAG=y
		;;
	  f )	f_FLAG=y
		;;
	  G )   u_FLAG=y
		;;
	  I )	m_FLAG=y
		p_FLAG=y
		u_FLAG=y
		;;
	  i )	i_FLAG=y
		;;
	  L )	L_FLAG=y
		;;
	  M )	M_FLAG=y
		;;
	  m )	m_FLAG=y
		;;
	  N )	N_FLAG=y
		;;
	  n )	n_FLAG=y
		;;
	  p )	p_FLAG=y
		;;
	  R )	m_FLAG=y
		p_FLAG=y
		;;
	  r )	r_FLAG=y
		;;
	  t )	t_FLAG=y
		;;
	 +t )	t_FLAG=n
		;;
	  U )   if [ -z "${PARENT_ROOT}" ]; then
			echo "PARENT_ROOT must be set if the U flag is" \
			    "present in NIGHTLY_OPTIONS."
			exit 1
		fi
		NIGHTLY_PARENT_ROOT=$PARENT_ROOT
		if [ -n "${PARENT_TOOLS_ROOT}" ]; then
			NIGHTLY_PARENT_TOOLS_ROOT=$PARENT_TOOLS_ROOT
		fi
		U_FLAG=y
		;;
	  u )	u_FLAG=y
		;;
	  w )	w_FLAG=y
		;;
	  W )   W_FLAG=y
		;;
	 \? )	echo "$USAGE"
		exit 1
		;;
	esac
done

# Skip pkglint if packages aren't being built
[ $p_FLAG = n ] && L_FLAG=y

if [ $ISUSER -ne 0 ]; then
	# Set default value for STAFFER, if needed.
	if [ -z "$STAFFER" -o "$STAFFER" = "nobody" ]; then
		STAFFER=`/usr/xpg4/bin/id -un`
		export STAFFER
	fi
fi

if [ -z "$MAILTO" -o "$MAILTO" = "nobody" ]; then
	MAILTO=$STAFFER
	export MAILTO
fi

# Hammerhead: /bin and /sbin are real directories with boot-essential commands
PATH="$OPTHOME/onbld/bin:$OPTHOME/onbld/bin/${MACH}"
PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin"
PATH="$PATH:/usr/local/bin:."
export PATH

# roots of source trees, both relative to $SRC and absolute.
relsrcdirs="."
abssrcdirs="$SRC"

# Hammerhead: PROTOCMPTERSE removed with the IPS-era manifest checks.
# protocmp / protolist / protocmp.terse were Sun-era IPS-manifest tools;
# Coral packaging supersedes them. The IPS-manifest block in normal_build
# (formerly here, plus invocations later) has been removed.
POUND_SIGN="#"
basews="$(basename "$CODEMGR_WS")"
# have we set RELEASE_DATE in our env file?
if [ -z "$RELEASE_DATE" ]; then
	RELEASE_DATE=$(LC_ALL=C date +"%B %Y")
fi
now=$(LC_ALL=C date +%Y-%b-%d)
DEV_CM_TAIL="development build: $LOGNAME $now [$basews]"

#
# We export POUND_SIGN, RELEASE_DATE and DEV_CM_TAIL to speed up the build
# process by avoiding repeated shell invocations to evaluate Makefile.master
# definitions.
#
export POUND_SIGN RELEASE_DATE DEV_CM_TAIL

# Hammerhead: Use GNU Make by default instead of dmake
maketype="parallel"
if [[ -z "$MAKE" ]]; then
	MAKE=gmake
fi
# Verify MAKE is a valid command (handles both paths and command names)
if ! command -v "$MAKE" >/dev/null 2>&1; then
	echo "\$MAKE ($MAKE) is not a valid command"
	exit 1
fi
export PATH
export MAKE

if [ "${SUNWSPRO}" != "" ]; then
	PATH="${SUNWSPRO}/bin:$PATH"
	export PATH
fi

# Hammerhead: GMAKE_MAX_JOBS controls -j parallelism for GNU Make builds
hostname=$(uname -n)
if [[ $GMAKE_MAX_JOBS != +([0-9]) || $GMAKE_MAX_JOBS == 0 ]]
then
	maxjobs=
	if [[ -f $HOME/.make.machines ]]
	then
		egrep -i $'^[\t ]*'"${hostname}"$'[\t .]' \
			$HOME/.make.machines | read host jobs
		maxjobs=${jobs##*=}
	fi

	if [[ $maxjobs != +([0-9]) || $maxjobs == 0 ]]
	then
		# default
		maxjobs=4
	fi

	export GMAKE_MAX_JOBS=$maxjobs
fi

if [ -z "${ROOT}" ]; then
	echo "ROOT must be set."
	exit 1
fi

# Safety: ROOT must be a dedicated build output directory to prevent
# installing to the live system.  Accept "proto" (OI convention) or
# "hammerhead-build" (self-hosted convention) in the path.
case "${ROOT}" in
*proto*|*hammerhead-build*)
	;;
*)
	echo "FATAL: ROOT='${ROOT}' is not a recognized build output directory."
	echo "This would install build artifacts to the live system."
	echo "ROOT must contain 'proto' or 'hammerhead-build' in its path."
	exit 1
	;;
esac

# illumos /tmp is swap-backed tmpfs (typically a few GB, sized by RAM).
# A full hh-build can write multi-GB of intermediate .s/.o files via GCC's
# TMPDIR, which overflows tmpfs on hosts with <16 GB RAM. Default the
# build's TMPDIR to /var/tmp (ZFS-backed) and honor a caller-set TMPDIR.
TMPDIR="${TMPDIR:-/var/tmp}/hh-build.tmpdir.$$"
export TMPDIR
rm -rf ${TMPDIR}
mkdir -p $TMPDIR || exit 1
chmod 777 $TMPDIR

#
# Work around folks who have historically used GCC_ROOT and convert it to
# GNUC_ROOT. We leave GCC_ROOT in the environment for now (though this could
# mess up the case where multiple different gcc versions are being used to
# shadow).
#
if [[ -n "${GCC_ROOT}" ]]; then
	export GNUC_ROOT=${GCC_ROOT}
fi

#
# Tools should only be built non-DEBUG.  Keep track of the tools proto
# area path relative to $TOOLS, because the latter changes in an
# export build.
#
# TOOLS_PROTO is included below for builds other than usr/src/tools
# that look for this location.  For usr/src/tools, this will be
# overridden on the $MAKE command line in build_tools().
#
TOOLS=${SRC}/tools
TOOLS_PROTO_REL=proto/root_${MACH}-nd
TOOLS_PROTO=${TOOLS}/${TOOLS_PROTO_REL};	export TOOLS_PROTO

unset   CFLAGS LD_LIBRARY_PATH LDFLAGS

#
# Echo the SCM type of the parent workspace, this can't just be which_scm
# as that does not know how to identify various network repositories.
#
function parent_wstype {
	typeset scm_type junk

	CODEMGR_WS="$BRINGOVER_WS" "$WHICH_SCM" 2>/dev/null \
	    | read scm_type junk
	if [[ -z "$scm_type" || "$scm_type" == unknown ]]; then
		# Probe BRINGOVER_WS to determine its type
		case "$BRINGOVER_WS" in
		git://* | http://*.git | https://*.git)
			scm_type="git"
			;;
		ssh://* | http://* | https://* )
			scm_type="${BRINGOVER_SCM}"
			;;
		*)	scm_type="none"
			;;
		esac
	fi

	# fold both unsupported and unrecognized results into "none"
	case "$scm_type" in
	mercurial|git)
		;;
	*)	scm_type=none
		;;
	esac

	echo $scm_type
}

# Echo the SCM types of $CODEMGR_WS and $BRINGOVER_WS
function child_wstype {
	typeset scm_type junk

	# Probe CODEMGR_WS to determine its type
	if [[ -d $CODEMGR_WS ]]; then
		$WHICH_SCM | read scm_type junk || exit 1
	fi

	case "$scm_type" in
	none|git|mercurial)
		;;
	*)	scm_type=none
		;;
	esac

	echo $scm_type
}

# create directories that are automatically removed if the build script
# fails to start correctly
function newdir {
	dir=$1
	toadd=
	while [ ! -d $dir ]; do
		toadd="$dir $toadd"
		dir=`dirname $dir`
	done
	torm=
	newlist=
	for dir in $toadd; do
		if staffer mkdir $dir; then
			newlist="$ISUSER $dir $newlist"
			torm="$dir $torm"
		else
			[ -z "$torm" ] || staffer rmdir $torm
			return 1
		fi
	done
	newdirlist="$newlist $newdirlist"
	return 0
}
newdirlist=

# Initialize the git repo before creating the log subdir; "git clone" insists
# that a preexisting directory be empty.
# Use --reference-if-able to share most of the parent's .git tree.
type init_git > /dev/null 2>&1 || function init_git {
	if [ -d "${BRINGOVER_WS}" ]; then
		REF_WS="--reference-if-able $(git -C ${BRINGOVER_WS} rev-parse --path-format=absolute --git-common-dir)"
	fi
	staffer git clone \
		--no-checkout \
		${CLONE_OPTIONS} \
		--origin ${BRINGOVER_REMOTE} \
		${REF_WS} \
		${BRINGOVER_WS} ${CODEMGR_WS}
}

# All mercurial initialization is done in bringover_mercurial
type init_mercurial > /dev/null 2>&1 || function init_mercurial {
	newdir $CODEMGR_WS
}

type init_none > /dev/null 2>&1 || function init_none {
	newdir $CODEMGR_WS
}

function create_build_ws {
	PARENT_SCM_TYPE=$(parent_wstype)

	eval "init_${PARENT_SCM_TYPE}" 2>&1
}

[ -d $CODEMGR_WS ] || create_build_ws || exit 1

# since this script assumes the build is from full source, it nullifies
# variables likely to have been set by a "ws" script; nullification
# confines the search space for headers and libraries to the proto area
# built from this immediate source.
ENVLDLIBS1=
ENVLDLIBS2=
ENVLDLIBS3=
ENVCPPFLAGS1=
ENVCPPFLAGS2=
ENVCPPFLAGS3=
ENVCPPFLAGS4=
PARENT_ROOT=

export ENVLDLIBS3 ENVCPPFLAGS1 ENVCPPFLAGS2 ENVCPPFLAGS3 ENVCPPFLAGS4 \
	ENVLDLIBS1 ENVLDLIBS2 PARENT_ROOT

PKGARCHIVE_ORIG=$PKGARCHIVE

#
# Juggle the logs and optionally send mail on completion.
#

function logshuffle {
	# Hammerhead: Include git commit hash in log directory name for
	# cross-machine identification. Format: log.<commit>.<date>
	typeset _commit
	_commit=$(cd $CODEMGR_WS && git rev-parse --short HEAD 2>/dev/null)
	if [ -n "$_commit" ]; then
		LLOG="$ATLOG/log.${_commit}.`date '+%F.%H:%M'`"
	else
		LLOG="$ATLOG/log.`date '+%F.%H:%M'`"
	fi
	if [ -f $LLOG -o -d $LLOG ]; then
		LLOG=$LLOG.$$
	fi

	rm -f "$ATLOG/latest" 2>/dev/null
	mkdir -p $LLOG
	export LLOG

	# Write build metadata for easy identification
	cat > $LLOG/build-info <<-BUILDINFO
	hammerhead_version=$HAMMERHEAD_VERSION
	commit=$(cd $CODEMGR_WS && git rev-parse HEAD 2>/dev/null)
	commit_short=$_commit
	branch=$(cd $CODEMGR_WS && git rev-parse --abbrev-ref HEAD 2>/dev/null)
	date=$(date '+%F %T %Z')
	host=$(hostname)
	user=$LOGNAME
	workspace=$CODEMGR_WS
	build_ok=$build_ok
	BUILDINFO

	if [ "$build_ok" = "y" ]; then
		if [ -f $ATLOG/proto_list_${MACH} ]; then
			mv $ATLOG/proto_list_${MACH} $LLOG
		fi

		if [ -f $ATLOG/proto_list_tools_${MACH} ]; then
			mv $ATLOG/proto_list_tools_${MACH} $LLOG
	        fi

		if [ -f $TMPDIR/wsdiff.results ]; then
			mv $TMPDIR/wsdiff.results $LLOG
		fi

		if [ -f $TMPDIR/wsdiff-nd.results ]; then
			mv $TMPDIR/wsdiff-nd.results $LLOG
		fi
	fi

	#
	# Now that we're about to send mail, it's time to check the noise
	# file.  In the event that an error occurs beyond this point, it will
	# be recorded in the build.log file, but nowhere else.  This would
	# include only errors that cause the copying of the noise log to fail
	# or the mail itself not to be sent.
	#

	exec >>$LOGFILE 2>&1
	if [ -s $build_noise_file ]; then
		echo "\n==== Build noise ====\n" |
		    tee -a $LOGFILE >>$mail_msg_file
		cat $build_noise_file >>$LOGFILE
		cat $build_noise_file >>$mail_msg_file
		echo | tee -a $LOGFILE >>$mail_msg_file
	fi
	rm -f $build_noise_file

	case "$build_ok" in
		y)
			state=Completed
			;;
		i)
			state=Interrupted
			;;
		*)
			state=Failed
			;;
	esac

	if [[ $state != "Interrupted" && $build_extras_ok != "y" ]]; then
		state=Failed
	fi

	NIGHTLY_STATUS=$state
	export NIGHTLY_STATUS

	run_hook POST_NIGHTLY $state
	run_hook SYS_POST_NIGHTLY $state

	#
	# mailx(1) sets From: based on the -r flag
	# if it is given.
	#
	mailx_r=
	if [[ -n "${MAILFROM}" ]]; then
		mailx_r="-r ${MAILFROM}"
	fi

	cat $build_time_file $build_environ_file $mail_msg_file \
	    > ${LLOG}/mail_msg
	if [ "$m_FLAG" = "y" ]; then
		mailx ${mailx_r} -s \
		    "Hammerhead ${MACH} Build of ${basews} ${state}." \
		    "${MAILTO}" < "${LLOG}/mail_msg"
	fi

	if [ "$u_FLAG" = "y" -a "$build_ok" = "y" ]; then
		staffer cp ${LLOG}/mail_msg $PARENT_WS/usr/src/mail_msg-${MACH}
		staffer cp $LOGFILE $PARENT_WS/usr/src/nightly-${MACH}.log
	fi

	mv $LOGFILE $LLOG

	ln -s "$LLOG" "$ATLOG/latest"

	# Generate build report (proto inventory, error analysis, deltas)
	typeset _report_script="$SRC/tools/scripts/hh-build-report"
	if [ -x "$_report_script" ]; then
		build_status "Generating build report"
		$_report_script \
		    --log-dir "$LLOG" \
		    --proto "$ROOT" \
		    --workspace "$CODEMGR_WS" \
		    --history "$ATLOG/build-history.json" \
		    2>/dev/null || true
	fi
}

#
#	Remove the locks and temporary files on any exit
#
function cleanup {
	logshuffle

	[ -z "$lockfile" ] || staffer rm -f $lockfile
	[ -z "$atloglockfile" ] || rm -f $atloglockfile
	[ -z "$ulockfile" ] || staffer rm -f $ulockfile
	[ -z "$Ulockfile" ] || rm -f $Ulockfile

	set -- $newdirlist
	while [ $# -gt 0 ]; do
		ISUSER=$1 staffer rmdir $2
		shift; shift
	done
	rm -rf $TMPDIR
}

function cleanup_signal {
	build_ok=i
	# this will trigger cleanup(), above.
	exit 1
}

trap cleanup 0
trap cleanup_signal 1 2 3 15

#
# Generic lock file processing -- make sure that the lock file doesn't
# exist.  If it does, it should name the build host and PID.  If it
# doesn't, then make sure we can create it.  Clean up locks that are
# known to be stale (assumes host name is unique among build systems
# for the workspace).
#
function create_lock {
	lockf=$1
	lockvar=$2

	ldir=`dirname $lockf`
	[ -d $ldir ] || newdir $ldir || exit 1
	eval $lockvar=$lockf

	while ! staffer ln -s $hostname.$STAFFER.$$ $lockf 2> /dev/null; do
		ls -l $lockf | nawk '{print $NF}' | IFS=. read host user pid
		if [ "$host" != "$hostname" ]; then
			echo "$MACH build of $basews apparently" \
			    "already started by $user on $host as $pid."
			exit 1
		elif kill -s 0 $pid 2>/dev/null; then
			echo "$MACH build of $basews already started" \
			    "by $user as $pid."
			exit 1
		else
			# stale lock; clear it out and try again
			rm -f $lockf
		fi
	done
}

#
# Return the list of interesting proto areas, depending on the current
# options.
#
function allprotos {
	typeset roots="$ROOT"

	if [[ "$F_FLAG" == n && "$MULTI_PROTO" == yes ]]; then
		roots="$roots $ROOT-nd"
	fi

	echo $roots
}

# Ensure no other instance of this script is running on this host.
# LOCKNAME can be set in <env_file>, and is by default, but is not
# required due to the use of $ATLOG below.
if [ -n "$LOCKNAME" ]; then
	create_lock /tmp/$LOCKNAME "lockfile"
fi
#
# Create from one, two, or three other locks:
#	$ATLOG/build.lock
#		- protects against multiple builds in same workspace
#	$PARENT_WS/usr/src/build.$MACH.lock
#		- protects against multiple 'u' copy-backs
#	$NIGHTLY_PARENT_ROOT/build.lock
#		- protects against multiple 'U' copy-backs
#
# Overriding ISUSER to 1 causes the lock to be created as root if the
# script is run as root.  The default is to create it as $STAFFER.
ISUSER=1 create_lock $ATLOG/build.lock "atloglockfile"
if [ "$u_FLAG" = "y" ]; then
	create_lock $PARENT_WS/usr/src/build.$MACH.lock "ulockfile"
fi
if [ "$U_FLAG" = "y" ]; then
	# NIGHTLY_PARENT_ROOT is written as root if script invoked as root.
	ISUSER=1 create_lock $NIGHTLY_PARENT_ROOT/build.lock "Ulockfile"
fi

# Locks have been taken, so we're doing a build and we're committed to
# the directories we may have created so far.
newdirlist=

#
# Create mail_msg_file
#
mail_msg_file="${TMPDIR}/mail_msg"
touch $mail_msg_file
build_time_file="${TMPDIR}/build_time"
build_environ_file="${TMPDIR}/build_environ"
touch $build_environ_file
#
#	Move old LOGFILE aside
#	ATLOG directory already made by 'create_lock' above
#
if [ -f $LOGFILE ]; then
	mv -f $LOGFILE ${LOGFILE}-
fi
#
#	Build OsNet source
#
START_DATE=`date`
SECONDS=0
typeset _build_commit
_build_commit=$(cd $CODEMGR_WS && git rev-parse --short HEAD 2>/dev/null)
build_status "Hammerhead build started (${_build_commit:-unknown})"
build_status "Workspace: $CODEMGR_WS"
build_status "Log file: $LOGFILE"
echo "\n==== Hammerhead $maketype build started: $START_DATE ====" \
    | tee -a $LOGFILE > $build_time_file
echo "Build commit: ${_build_commit:-unknown}" | \
    tee -a $LOGFILE >> $build_time_file

echo "\nBuild project:  $build_project\nBuild taskid:   $build_taskid" | \
    tee -a $mail_msg_file >> $LOGFILE

# make sure we log only to the build file
build_noise_file="${TMPDIR}/build_noise"
exec </dev/null >$build_noise_file 2>&1

run_hook SYS_PRE_NIGHTLY
run_hook PRE_NIGHTLY

echo "\n==== list of environment variables ====\n" >> $LOGFILE
env >> $LOGFILE

echo "\n==== Build argument issues ====\n" | tee -a $mail_msg_file >> $LOGFILE

# Hammerhead: removed the -N (skip protocmp) warning block. protocmp
# is gone, the flag is a no-op now.

if [ "$f_FLAG" = "y" ]; then
	if [ "$i_FLAG" = "y" ]; then
		echo "WARNING: the -f flag cannot be used during incremental" \
		    "builds; ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
		f_FLAG=n
	fi
	if [ "${p_FLAG}" != "y" ]; then
		echo "WARNING: the -f flag requires -p;" \
		    "ignoring -f\n" | tee -a $mail_msg_file >> $LOGFILE
		f_FLAG=n
	fi
fi

if [[ "$t_FLAG" != "y" ]]; then
	echo "WARNING: building using out-of-gate tools (via +t flag) " \
	    "is not supported\n" | tee -a $mail_msg_file >> $LOGFILE
fi

if [ "$w_FLAG" = "y" -a ! -d $ROOT ]; then
	echo "WARNING: -w specified, but $ROOT does not exist;" \
	    "ignoring -w\n" | tee -a $mail_msg_file >> $LOGFILE
	w_FLAG=n
fi

case $MULTI_PROTO in
yes|no)	;;
*)
	echo "WARNING: MULTI_PROTO is \"$MULTI_PROTO\"; " \
	    "should be \"yes\" or \"no\"." | tee -a $mail_msg_file >> $LOGFILE
	echo "Setting MULTI_PROTO to \"no\".\n" | \
	    tee -a $mail_msg_file >> $LOGFILE
	export MULTI_PROTO=no
	;;
esac

# Save the current proto area if we're comparing against the last build
if [ "$w_FLAG" = "y" -a -d "$ROOT" ]; then
    if [ -d "$ROOT.prev" ]; then
	rm -rf $ROOT.prev
    fi
    mv $ROOT $ROOT.prev
fi

# Same for non-DEBUG proto area
if [ "$w_FLAG" = "y" -a "$MULTI_PROTO" = yes -a -d "$ROOT-nd" ]; then
	if [ -d "$ROOT-nd.prev" ]; then
		rm -rf $ROOT-nd.prev
	fi
	mv $ROOT-nd $ROOT-nd.prev
fi

SCM_TYPE=$(child_wstype)

#
#	Decide whether to clobber
#
if [ "$i_FLAG" = "n" -a -d "$SRC" ]; then
	build_status "Starting: make clobber"
	echo "\n==== Make clobber at `date` ====\n" >> $LOGFILE

	cd $SRC
	# remove old clobber file
	rm -f $SRC/clobber.out
	rm -f $SRC/clobber-${MACH}.out

	# Remove all .make.state* files, just in case we are restarting
	# the build after having interrupted a previous 'make clobber'.
	find . \( -name SCCS -o -name .hg -o -name .svn -o -name .git \
		-o -name 'interfaces.*' \) -prune \
		-o -name '.make.*' -print | xargs rm -f

	$MAKE -ek clobber 2>&1 | tee -a $SRC/clobber-${MACH}.out >> $LOGFILE
	echo "\n==== Make clobber ERRORS ====\n" >> $mail_msg_file
	grep "$MAKE:" $SRC/clobber-${MACH}.out |
		egrep -v ": (Entering|Leaving) directory " | \
		egrep -v "Ignoring unknown host" | \
		tee $TMPDIR/clobber_errs >> $mail_msg_file

	if [[ -s $TMPDIR/clobber_errs ]]; then
		build_extras_ok=n
	fi

	if [[ "$t_FLAG" == "y" ]]; then
		build_status "Starting: make tools clobber"
		echo "\n==== Make tools clobber at `date` ====\n" >> $LOGFILE
		cd ${TOOLS}
		rm -f ${TOOLS}/clobber-${MACH}.out
		$MAKE TOOLS_PROTO=$TOOLS_PROTO -ek clobber 2>&1 | \
			tee -a ${TOOLS}/clobber-${MACH}.out >> $LOGFILE
		echo "\n==== Make tools clobber ERRORS ====\n" \
			>> $mail_msg_file
		grep "$MAKE:" ${TOOLS}/clobber-${MACH}.out \
			| egrep -v ": (Entering|Leaving) directory " \
			>> $mail_msg_file
		if (( $? == 0 )); then
			build_extras_ok=n
		fi
		rm -rf ${TOOLS_PROTO}
		mkdir -p ${TOOLS_PROTO}
	fi

	typeset roots=$(allprotos)
	echo "\n\nClearing $roots" >> "$LOGFILE"
	# Proto area may be root-owned from post-build ownership fixup;
	# use sudo to ensure we can remove it cleanly.
	$SUDO rm -rf $roots

	# Get back to a clean workspace as much as possible to catch
	# problems that only occur on fresh workspaces.
	# Remove all .make.state* files, libraries, and .o's that may
	# have been omitted from clobber.  A couple of libraries are
	# under source code control, so leave them alone.
	# We should probably blow away temporary directories too.
	cd $SRC
	find $relsrcdirs \( -name SCCS -o -name .hg -o -name .svn \
	    -o -name .git -o -name 'interfaces.*' \) -prune -o \
	    \( -name '.make.*' -o -name 'lib*.a' -o -name 'lib*.so*' -o \
	       -name '*.o' \) -print | \
	    grep -v 'tools/ctf/dwarf/.*/libdwarf' | xargs rm -f
else
	echo "\n==== No clobber at `date` ====\n" >> $LOGFILE
fi

type bringover_git > /dev/null 2>&1 || function bringover_git {
	typeset -x PATH=$PATH

	if [ "$BRINGOVER_BRANCH" = "" ]; then
		if [ -d "$BRINGOVER_WS" ]; then
			BRINGOVER_BRANCH=$(cd "${BRINGOVER_WS}"; git rev-parse --abbrev-ref HEAD)
			if [ "$BRINGOVER_BRANCH" = "HEAD" ]; then
				printf "%s: can't determine BRINGOVER_BRANCH from repo in 'detached HEAD' state\n" "$BRINGOVER_WS"
				touch $TMPDIR/bringover_failed
				return
			fi
		else
			printf "%s: can't determine BRINGOVER_BRANCH\n" "$BRINGOVER_WS"
			touch $TMPDIR/bringover_failed
			return
		fi
	fi
	(cd ${CODEMGR_WS} &&
		staffer git remote set-url ${BRINGOVER_REMOTE} ${BRINGOVER_WS} &&
		staffer git fetch ${BRINGOVER_REMOTE} ${BRINGOVER_BRANCH} &&
		staffer git switch --force-create ${BRINGOVER_BRANCH} \
			${BRINGOVER_REMOTE}/${BRINGOVER_BRANCH}) \
		    >$TMPDIR/bringover.out 2>&1
	if (( $? != 0 )); then
		printf "%s: update failed as follows:\n\n" "$CODEMGR_WS"
		cat $TMPDIR/bringover.out
		touch $TMPDIR/bringover_failed
		return
	fi

	printf "%s: local branch '%s' updated to commit %s\n"	\
		"$CODEMGR_WS" "$BRINGOVER_BRANCH"		\
		$(cd ${CODEMGR_WS}; git rev-parse HEAD)

	staffer git status --no-short --branch --untracked-files=no
}

type bringover_mercurial > /dev/null 2>&1 || function bringover_mercurial {
	typeset -x PATH=$PATH

	# If the repository doesn't exist yet, then we want to populate it.
	if [[ ! -d $CODEMGR_WS/.hg ]]; then
		staffer hg init $CODEMGR_WS
		staffer echo "[paths]" > $CODEMGR_WS/.hg/hgrc
		staffer echo "default=$BRINGOVER_WS" >> $CODEMGR_WS/.hg/hgrc
		touch $TMPDIR/new_repository
	fi

	typeset -x HGMERGE="/bin/false"

	#
	# If the user has changes, regardless of whether those changes are
	# committed, and regardless of whether those changes conflict, then
	# we'll attempt to merge them either implicitly (uncommitted) or
	# explicitly (committed).
	#
	# These are the messages we'll use to help clarify mercurial output
	# in those cases.
	#
	typeset mergefailmsg="\
***\n\
*** hh-build was unable to automatically merge your changes.  You should\n\
*** redo the full merge manually, following the steps outlined by mercurial\n\
*** above, then restart hh-build.\n\
***\n"
	typeset mergepassmsg="\
***\n\
*** hh-build successfully merged your changes.  This means that your working\n\
*** directory has been updated, but those changes are not yet committed.\n\
*** After hh-build completes, you should validate the results of the merge,\n\
*** then use hg commit manually.\n\
***\n"

	#
	# For each repository in turn:
	#
	# 1. Do the pull.  If this fails, dump the output and bail out.
	#
	# 2. If the pull resulted in an extra head, do an explicit merge.
	#    If this fails, dump the output and bail out.
	#
	# Because we can't rely on Mercurial to exit with a failure code
	# when a merge fails (Mercurial issue #186), we must grep the
	# output of pull/merge to check for attempted and/or failed merges.
	#
	# 3. If a merge failed, set the message and fail the bringover.
	#
	# 4. Otherwise, if a merge succeeded, set the message
	#
	# 5. Dump the output, and any message from step 3 or 4.
	#

	typeset HG_SOURCE=$BRINGOVER_WS
	if [ ! -f $TMPDIR/new_repository ]; then
		HG_SOURCE=$TMPDIR/open_bundle.hg
		staffer hg --cwd $CODEMGR_WS incoming --bundle $HG_SOURCE \
		    -v $BRINGOVER_WS > $TMPDIR/incoming_open.out

		#
		# If there are no incoming changesets, then incoming will
		# fail, and there will be no bundle file.  Reset the source,
		# to allow the remaining logic to complete with no false
		# negatives.  (Unlike incoming, pull will return success
		# for the no-change case.)
		#
		if (( $? != 0 )); then
			HG_SOURCE=$BRINGOVER_WS
		fi
	fi

	staffer hg --cwd $CODEMGR_WS pull -u $HG_SOURCE \
	    > $TMPDIR/pull_open.out 2>&1
	if (( $? != 0 )); then
		printf "%s: pull failed as follows:\n\n" "$CODEMGR_WS"
		cat $TMPDIR/pull_open.out
		if grep "^merging.*failed" $TMPDIR/pull_open.out > /dev/null 2>&1; then
			printf "$mergefailmsg"
		fi
		touch $TMPDIR/bringover_failed
		return
	fi

	if grep "not updating" $TMPDIR/pull_open.out > /dev/null 2>&1; then
		staffer hg --cwd $CODEMGR_WS merge \
		    >> $TMPDIR/pull_open.out 2>&1
		if (( $? != 0 )); then
			printf "%s: merge failed as follows:\n\n" \
			    "$CODEMGR_WS"
			cat $TMPDIR/pull_open.out
			if grep "^merging.*failed" $TMPDIR/pull_open.out \
			    > /dev/null 2>&1; then
				printf "$mergefailmsg"
			fi
			touch $TMPDIR/bringover_failed
			return
		fi
	fi

	printf "updated %s with the following results:\n" "$CODEMGR_WS"
	cat $TMPDIR/pull_open.out
	if grep "^merging" $TMPDIR/pull_open.out >/dev/null 2>&1; then
		printf "$mergepassmsg"
	fi
	printf "\n"

	#
	# Per-changeset output is neither useful nor manageable for a
	# newly-created repository.
	#
	if [ -f $TMPDIR/new_repository ]; then
		return
	fi

	printf "\nadded the following changesets to open repository:\n"
	cat $TMPDIR/incoming_open.out
}

type bringover_none > /dev/null 2>&1 || function bringover_none {
	echo "Couldn't figure out what kind of SCM to use for $BRINGOVER_WS."
	touch $TMPDIR/bringover_failed
}

#
#	Decide whether to bringover to the codemgr workspace
#
if [ "$n_FLAG" = "n" ]; then
	PARENT_SCM_TYPE=$(parent_wstype)

	if [[ $SCM_TYPE != none && $SCM_TYPE != $PARENT_SCM_TYPE ]]; then
		echo "cannot bringover from $PARENT_SCM_TYPE to $SCM_TYPE, " \
		    "quitting at `date`." | tee -a $mail_msg_file >> $LOGFILE
		exit 1
	fi

	run_hook PRE_BRINGOVER

	echo "\n==== bringover to $CODEMGR_WS at `date` ====\n" >> $LOGFILE
	echo "\n==== BRINGOVER LOG ====\n" >> $mail_msg_file

	eval "bringover_${PARENT_SCM_TYPE}" 2>&1 |
		tee -a $mail_msg_file >> $LOGFILE

	if [ -f $TMPDIR/bringover_failed ]; then
		rm -f $TMPDIR/bringover_failed
		build_ok=n
		echo "trouble with bringover, quitting at `date`." |
			tee -a $mail_msg_file >> $LOGFILE
		exit 1
	fi

	#
	# It's possible that we used the bringover above to create
	# $CODEMGR_WS.  If so, then SCM_TYPE was previously "none,"
	# but should now be the same as $BRINGOVER_WS.
	#
	[[ $SCM_TYPE == none ]] && SCM_TYPE=$PARENT_SCM_TYPE

	run_hook POST_BRINGOVER

	check_closed_bins

else
	echo "\n==== No bringover to $CODEMGR_WS ====\n" >> $LOGFILE
fi

# Safeguards
[[ -v CODEMGR_WS ]] || fatal_error "Error: Variable CODEMGR_WS not set."
[[ -d "${CODEMGR_WS}" ]] || fatal_error "Error: ${CODEMGR_WS} is not a directory."
[[ -f "${CODEMGR_WS}/usr/src/Makefile" ]] || fatal_error "Error: ${CODEMGR_WS}/usr/src/Makefile not found."

#
# Set HAMMERHEAD_VERSION (format: YYYYMMDD-NN where NN is daily build number).
# Does NOT export as VERSION to avoid polluting sub-builds (e.g. zsh autotools).
#
if [[ "$V_FLAG" == "y" ]]; then
	export HAMMERHEAD_VERSION=$V_ARG
elif [[ -z "$HAMMERHEAD_VERSION" ]]; then
	_today=$(date '+%Y%m%d')
	_history="$ATLOG/build-history.json"
	if [[ -f "$_history" ]]; then
		_count=$(grep -c "\"${_today}" "$_history" 2>/dev/null || echo 0)
	else
		_count=0
	fi
	_seq=$(printf '%02d' $(( _count + 1 )))
	export HAMMERHEAD_VERSION="${_today}-${_seq}"
fi
unset VERSION

[[ -n "$HAMMERHEAD_VERSION" ]] || fatal_error "Error: HAMMERHEAD_VERSION not set"

echo "\n==== Build version ====\n" | tee -a $mail_msg_file >> $LOGFILE
echo $HAMMERHEAD_VERSION | tee -a $mail_msg_file >> $LOGFILE

if [[ "$t_FLAG" == "y" ]]; then
	set_non_debug_build_flags
	# Switch ONBLD_TOOLS early if -t is specified so that
	# we could use bootstrapped cw for compiler checks.
	ONBLD_TOOLS=${TOOLS_PROTO}/opt/onbld
	export ONBLD_TOOLS

	bootstrap_tools || fatal_error "Error: could not bootstrap tools"
fi

echo "\n==== Build environment ====\n" | tee -a $build_environ_file >> $LOGFILE

# System
whence uname | tee -a $build_environ_file >> $LOGFILE
uname -a 2>&1 | tee -a $build_environ_file >> $LOGFILE
echo | tee -a $build_environ_file >> $LOGFILE

# make
whence $MAKE | tee -a $build_environ_file >> $LOGFILE
$MAKE -v | tee -a $build_environ_file >> $LOGFILE
echo "number of concurrent jobs = $GMAKE_MAX_JOBS" |
    tee -a $build_environ_file >> $LOGFILE

#
# Report the compiler versions.
#

if [[ ! -f $SRC/Makefile ]]; then
	build_ok=n
	echo "\nUnable to find \"Makefile\" in $SRC." | \
	    tee -a $build_environ_file >> $LOGFILE
	exit 1
fi

( cd $SRC
  for target in cc-version java-version openssl-version; do
	echo
	#
	# Put statefile somewhere we know we can write to rather than trip
	# over a read-only $srcroot.
	#
	export SRC
	# Note: dmake used -K statefile; gmake doesn't need this
	if $MAKE -e $target 2>/dev/null; then
		continue
	fi
	touch $TMPDIR/nocompiler
  done
  echo
) | egrep -v ": (Entering|Leaving) directory " \
    | tee -a $build_environ_file >> $LOGFILE

if [ -f $TMPDIR/nocompiler ]; then
	rm -f $TMPDIR/nocompiler
	build_ok=n
	echo "Aborting due to missing compiler." |
		tee -a $build_environ_file >> $LOGFILE
	exit 1
fi

# Check that we're running a capable link-editor
whence ld | tee -a $build_environ_file >> $LOGFILE
LDVER=`ld -V 2>&1`
echo $LDVER | tee -a $build_environ_file >> $LOGFILE
# Hammerhead: skip version check for GNU ld (illumos ld format is "-1.NNNN")
if echo "$LDVER" | grep -q 'GNU'; then
	echo "GNU ld detected (Hammerhead)" | \
	    tee -a $build_environ_file >> $LOGFILE
else
	LDVER=`echo $LDVER | sed -e "s/.*-1\.\([0-9]*\).*/\1/"`
	if [ `expr $LDVER \< 422` -eq 1 ]; then
		echo "The link-editor needs to be at version 422 or higher to build" | \
		    tee -a $build_environ_file >> $LOGFILE
		echo "the latest stuff.  Hope your build works." | \
		    tee -a $build_environ_file >> $LOGFILE
	fi
fi

#
# Build and use the workspace's tools if requested
#
if [[ "$t_FLAG" == "y" ]]; then
	set_non_debug_build_flags

	build_tools ${TOOLS_PROTO}
	if (( $? != 0 )); then
		build_ok=n
		exit 1
	fi

	STABS=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/stabs
	export STABS
	CTFSTABS=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfstabs
	export CTFSTABS
	GENOFFSETS=${TOOLS_PROTO}/opt/onbld/bin/genoffsets
	export GENOFFSETS

	CTFCONVERT=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfconvert
	export CTFCONVERT
	CTFMERGE=${TOOLS_PROTO}/opt/onbld/bin/${MACH}/ctfmerge
	export CTFMERGE

	PATH="${TOOLS_PROTO}/opt/onbld/bin/${MACH}:${PATH}"
	PATH="${TOOLS_PROTO}/opt/onbld/bin:${PATH}"
	export PATH

	echo "\n==== New environment settings. ====\n" >> $LOGFILE
	echo "STABS=${STABS}" >> $LOGFILE
	echo "CTFSTABS=${CTFSTABS}" >> $LOGFILE
	echo "CTFCONVERT=${CTFCONVERT}" >> $LOGFILE
	echo "CTFMERGE=${CTFMERGE}" >> $LOGFILE
	echo "PATH=${PATH}" >> $LOGFILE
	echo "ONBLD_TOOLS=${ONBLD_TOOLS}" >> $LOGFILE
fi

# timestamp the start of the normal build; the findunref tool uses it.
touch $SRC/.build.tstamp

# Build pre-build toolchains (OCaml, etc.) before the normal build.
# These use stamp-file caching and are skipped if already up to date.
build_toolchain

# If any of the toolchain steps (build-gcc, build-tools, bootstrap-hh, or
# the zyginit extract) failed, normal_build will produce a proto that's
# missing pieces it depends on. Refuse to continue rather than burn an
# hour of OS-Net work on a doomed run.
if [[ "$build_ok" != "y" ]]; then
	echo "FATAL: build_toolchain failed (build_ok=$build_ok); skipping normal_build" | \
	    tee -a $mail_msg_file >> $LOGFILE
	exit 1
fi

normal_build

# Merge pre-built toolchain into proto area
merge_toolchain

ORIG_SRC=$SRC
BINARCHIVE=${CODEMGR_WS}/bin-${MACH}.cpio.Z

abspkg=
for d in $abssrcdirs; do
	if [ -d "$d/pkg" ]; then
		abspkg="$abspkg $d"
	fi
done

if [ "$L_FLAG" != "y" -a "$build_ok" = y ]; then
	echo "\n==== Linting packages ====\n" | \
	    tee -a $LOGFILE >> $mail_msg_file

	if [ -n "$abspkg" ]; then
		for d in "$abspkg"; do
			( cd $d/pkg ; $MAKE -e pkglint ) | \
			    tee -a $LOGFILE | \
			    egrep -v ": (Entering|Leaving) directory " | \
			    egrep -v 'Lint engine setup|Starting lint run'
		done 2>&1 | tee $TMPDIR/pkglint_noise >> $mail_msg_file
		if [[ -s $TMPDIR/pkglint_noise ]]; then
			build_extras_ok=n
		fi
	fi
fi

#
# There are several checks that need to look at the proto area, but
# they only need to look at one, and they don't care whether it's
# DEBUG or non-DEBUG.
#
if [[ "$MULTI_PROTO" == yes && "$D_FLAG" == n ]]; then
	checkroot=$ROOT-nd
else
	checkroot=$ROOT
fi

# Hammerhead: protolist + protocmp + protocmp.terse blocks removed.
# These were Sun-era IPS-manifest validation tools that no longer exist
# (sources deleted from usr/src/tools/, pkg/ removed during IPS phase-out).
# Coral packaging does the equivalent verification. The -N flag (skip
# protocmp) is now a no-op accepted for backward compatibility.

if [[ "$u_FLAG" == "y" && "$build_ok" == "y" && \
    "$build_extras_ok" == "y" ]]; then
	staffer cp $ATLOG/proto_list_${MACH} \
		$PARENT_WS/usr/src/proto_list_${MACH}
fi

# Update parent proto area if necessary. This is done now
# so that the proto area has either DEBUG or non-DEBUG kernels.
# Note that this clears out the lock file, so we can dispense with
# the variable now.
if [ "$U_FLAG" = "y" -a "$build_ok" = "y" ]; then
	echo "\n==== Copying proto area to $NIGHTLY_PARENT_ROOT ====\n" | \
	    tee -a $LOGFILE >> $mail_msg_file
	rm -rf $NIGHTLY_PARENT_ROOT/*
	unset Ulockfile
	mkdir -p $NIGHTLY_PARENT_ROOT
	if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
		( cd $ROOT; tar cf - . |
		    ( cd $NIGHTLY_PARENT_ROOT;  umask 0; tar xpf - ) ) 2>&1 |
		    tee -a $mail_msg_file >> $LOGFILE
	fi
	if [[ "$MULTI_PROTO" == yes && "$F_FLAG" == n ]]; then
		rm -rf $NIGHTLY_PARENT_ROOT-nd/*
		mkdir -p $NIGHTLY_PARENT_ROOT-nd
		cd $ROOT-nd
		( tar cf - . |
		    ( cd $NIGHTLY_PARENT_ROOT-nd; umask 0; tar xpf - ) ) 2>&1 |
		    tee -a $mail_msg_file >> $LOGFILE
	fi
	if [ -n "${NIGHTLY_PARENT_TOOLS_ROOT}" ]; then
		echo "\n==== Copying tools proto area to $NIGHTLY_PARENT_TOOLS_ROOT ====\n" | \
		    tee -a $LOGFILE >> $mail_msg_file
		rm -rf $NIGHTLY_PARENT_TOOLS_ROOT/*
		mkdir -p $NIGHTLY_PARENT_TOOLS_ROOT
		if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
			( cd $TOOLS_PROTO; tar cf - . |
			    ( cd $NIGHTLY_PARENT_TOOLS_ROOT;
			    umask 0; tar xpf - ) ) 2>&1 |
			    tee -a $mail_msg_file >> $LOGFILE
		fi
	fi
fi

#
# ELF verification: ABI (-A) and runtime (-r) checks
#
if [[ ($build_ok == y) && (($A_FLAG == y) || ($r_FLAG == y)) ]]; then
	# Directory ELF-data.$MACH holds the files produced by these tests.
	elf_ddir=$SRC/ELF-data.$MACH

	# If there is a previous ELF-data backup directory, remove it. Then,
	# rotate current ELF-data directory into its place and create a new
	# empty directory
	rm -rf $elf_ddir.ref
	if [[ -d $elf_ddir ]]; then
		mv $elf_ddir $elf_ddir.ref
	fi
	mkdir -p $elf_ddir

	# Call find_elf to produce a list of the ELF objects in the proto area.
	# This list is passed to check_rtime and interface_check, preventing
	# them from separately calling find_elf to do the same work twice.
	find_elf -fr $checkroot > $elf_ddir/object_list

	if [[ $A_FLAG == y ]]; then
		echo "\n==== Check versioning and ABI information ====\n"  | \
		    tee -a $LOGFILE >> $mail_msg_file

		# Produce interface description for the proto. Report errors.
		interface_check -o -w $elf_ddir -f object_list \
			-i interface -E interface.err
		if [[ -s $elf_ddir/interface.err ]]; then
			tee -a $LOGFILE < $elf_ddir/interface.err \
			    >> $mail_msg_file
			build_extras_ok=n
		fi

		# If ELF_DATA_BASELINE_DIR is defined, compare the new interface
		# description file to that from the baseline gate. Issue a
		# warning if the baseline is not present, and keep going.
		if [[ "$ELF_DATA_BASELINE_DIR" != '' ]]; then
			base_ifile="$ELF_DATA_BASELINE_DIR/interface"

			echo "\n==== Compare versioning and ABI information" \
			    "to baseline ====\n"  | \
			    tee -a $LOGFILE >> $mail_msg_file
			echo "Baseline:	 $base_ifile\n" >> $LOGFILE

			if [[ -f $base_ifile ]]; then
				interface_cmp -d -o $base_ifile \
				    $elf_ddir/interface > $elf_ddir/interface.cmp
				if [[ -s $elf_ddir/interface.cmp ]]; then
					echo | tee -a $LOGFILE >> $mail_msg_file
					tee -a $LOGFILE < \
					    $elf_ddir/interface.cmp \
					    >> $mail_msg_file
					build_extras_ok=n
				fi
			else
				echo "baseline not available. comparison" \
                                    "skipped" | \
				    tee -a $LOGFILE >> $mail_msg_file
			fi

		fi
	fi

	if [[ $r_FLAG == y ]]; then
		echo "\n==== Check ELF runtime attributes ====\n" | \
		    tee -a $LOGFILE >> $mail_msg_file

		# If we're doing a DEBUG build the proto area will be left
		# with debuggable objects, thus don't assert -s.
		if [[ $D_FLAG == y ]]; then
			rtime_sflag=""
		else
			rtime_sflag="-s"
		fi
		check_rtime -i -m -v $rtime_sflag -o -w $elf_ddir \
			-D object_list  -f object_list -E runtime.err \
			-I runtime.attr.raw
		if (( $? != 0 )); then
			build_extras_ok=n
		fi

		# check_rtime -I output needs to be sorted in order to
		# compare it to that from previous builds.
		sort $elf_ddir/runtime.attr.raw > $elf_ddir/runtime.attr
		rm $elf_ddir/runtime.attr.raw

		# Report errors
		if [[ -s $elf_ddir/runtime.err ]]; then
			tee -a $LOGFILE < $elf_ddir/runtime.err \
				>> $mail_msg_file
			build_extras_ok=n
		fi

		# If there is an ELF-data directory from a previous build,
		# then diff the attr files. These files contain information
		# about dependencies, versioning, and runpaths. There is some
		# overlap with the ABI checking done above, but this also
		# flushes out non-ABI interface differences along with the
		# other information.
		echo "\n==== Diff ELF runtime attributes" \
		    "(since last build) ====\n" | \
		    tee -a $LOGFILE >> $mail_msg_file >> $mail_msg_file

		if [[ -f $elf_ddir.ref/runtime.attr ]]; then
			diff $elf_ddir.ref/runtime.attr \
				$elf_ddir/runtime.attr \
				>> $mail_msg_file
		fi
	fi

	# If -u set, copy contents of ELF-data.$MACH to the parent workspace.
	if [[ "$u_FLAG" == "y" ]]; then
		p_elf_ddir=$PARENT_WS/usr/src/ELF-data.$MACH

		# If parent lacks the ELF-data.$MACH directory, create it
		if [[ ! -d $p_elf_ddir ]]; then
			staffer mkdir -p $p_elf_ddir
		fi

		# These files are used asynchronously by other builds for ABI
		# verification, as above for the -A option. As such, we require
		# the file replacement to be atomic. Copy the data to a temp
		# file in the same filesystem and then rename into place.
		(
			cd $elf_ddir
			for elf_dfile in *; do
				staffer cp $elf_dfile \
				    ${p_elf_ddir}/${elf_dfile}.new
				staffer mv -f ${p_elf_ddir}/${elf_dfile}.new \
				    ${p_elf_ddir}/${elf_dfile}
			done
		)
	fi
fi

# "make check" begins

if [ "$i_CMD_LINE_FLAG" = "n" -a "$C_FLAG" = "y" ]; then
	# remove old check.out
	rm -f $SRC/check.out

	rm -f $SRC/check-${MACH}.out
	cd $SRC
	$MAKE -ek check ROOT="$checkroot" 2>&1 | tee -a $SRC/check-${MACH}.out \
	    >> $LOGFILE
	echo "\n==== cstyle/hdrchk errors ====\n" >> $mail_msg_file

	grep ":" $SRC/check-${MACH}.out |
		egrep -v ": (Entering|Leaving) directory " | \
		egrep -v "Ignoring unknown host" | \
		sort | uniq | tee $TMPDIR/check_errors >> $mail_msg_file

	if [[ -s $TMPDIR/check_errors ]]; then
		build_extras_ok=n
	fi
else
	echo "\n==== No '$MAKE check' ====\n" >> $LOGFILE
fi

echo "\n==== Find core files ====\n" | \
    tee -a $LOGFILE >> $mail_msg_file

find $abssrcdirs -name core -a -type f -exec file {} \; | \
	tee -a $LOGFILE >> $mail_msg_file

if [ "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
	echo "\n==== Diff unreferenced files (since last build) ====\n" \
	    | tee -a $LOGFILE >>$mail_msg_file
	rm -f $SRC/unref-${MACH}.ref
	if [ -f $SRC/unref-${MACH}.out ]; then
		mv $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
	fi

	findunref -S $SCM_TYPE -t $SRC/.build.tstamp -s usr $CODEMGR_WS \
	    ${TOOLS}/findunref/exception_list 2>> $mail_msg_file | \
	    sort > $SRC/unref-${MACH}.out

	if [ ! -f $SRC/unref-${MACH}.ref ]; then
		cp $SRC/unref-${MACH}.out $SRC/unref-${MACH}.ref
	fi

	diff $SRC/unref-${MACH}.ref $SRC/unref-${MACH}.out >>$mail_msg_file
fi

# Verify that the usual lists of files, such as exception lists,
# contain only valid references to files.  If the build has failed,
# then don't check the proto area.
CHECK_PATHS=${CHECK_PATHS:-y}
if [ "$CHECK_PATHS" = y -a "$N_FLAG" != y ]; then
	echo "\n==== Check lists of files ====\n" | tee -a $LOGFILE \
		>>$mail_msg_file
	arg=-b
	[ "$build_ok" = y ] && arg=
	checkpaths $arg $checkroot > $SRC/check-paths.out 2>&1
	if [[ -s $SRC/check-paths.out ]]; then
		tee -a $LOGFILE < $SRC/check-paths.out >> $mail_msg_file
		build_extras_ok=n
	fi
fi

# Hammerhead: pmodes block removed. pmodes was a Sun-era file-permission
# checker driven by pkg/ manifests; with IPS removed and Coral handling
# packaging+permissions, this is dead. The -M flag (skip pmodes) is now
# a no-op accepted for backward compatibility.

if [ "$w_FLAG" = "y" -a "$build_ok" = "y" ]; then
	if [[ "$MULTI_PROTO" == no || "$D_FLAG" == y ]]; then
		do_wsdiff DEBUG $ROOT.prev $ROOT
	fi

	if [[ "$MULTI_PROTO" == yes && "$F_FLAG" == n ]]; then
		do_wsdiff non-DEBUG $ROOT-nd.prev $ROOT-nd
	fi
fi

#
# Fix proto area ownership so it's deployment-ready.
# The build runs as a non-root user, so all proto files are owned by the
# build user.  This step applies correct illumos ownership conventions
# so that the proto area can be tar'd with --preserve and deployed directly.
#
if [ "$build_ok" = "y" ]; then
	echo "\n==== Fixing proto area ownership ====\n" | \
	    tee -a $LOGFILE >> $build_time_file

	# Bulk ownership: root:sys default, root:bin for binaries/libraries
	if $SUDO chown -R root:sys "$ROOT"; then
		for d in usr/bin usr/sbin usr/lib usr/xpg4 usr/xpg6 lib sbin; do
			[ -d "$ROOT/$d" ] && \
			    $SUDO chown -R root:bin "$ROOT/$d"
		done
		echo "Proto bulk ownership applied" | \
		    tee -a $LOGFILE >> $build_time_file
	else
		echo "WARNING: chown failed (proto ownership not fixed)" | \
		    tee -a $LOGFILE >> $build_time_file
	fi

	# Per-file overrides from ownership manifest
	_manifest="${CODEMGR_WS}/../rootfs/ownership.manifest"
	if [ -f "$_manifest" ]; then
		while read _path _mode _owner _group; do
			# Skip comments and blank lines
			case "$_path" in
			\#*|"") continue ;;
			esac
			_target="$ROOT$_path"
			if [ -e "$_target" ]; then
				$SUDO chown "$_owner:$_group" "$_target"
				$SUDO chmod "$_mode" "$_target"
			fi
		done < "$_manifest"
		echo "Proto per-file ownership manifest applied" | \
		    tee -a $LOGFILE >> $build_time_file
	else
		echo "NOTE: No ownership manifest found at $_manifest" | \
		    tee -a $LOGFILE >> $build_time_file
	fi
fi

END_DATE=`date`
build_status "Hammerhead build completed (${_build_commit:-unknown})"
echo "==== Hammerhead $maketype build completed: $END_DATE ====" | \
    tee -a $LOGFILE >> $build_time_file

typeset -i10 hours
typeset -Z2 minutes
typeset -Z2 seconds

elapsed_time=$SECONDS
((hours = elapsed_time / 3600 ))
((minutes = elapsed_time / 60  % 60))
((seconds = elapsed_time % 60))

echo "\n==== Total build time ====" | \
    tee -a $LOGFILE >> $build_time_file
echo "\nreal    ${hours}:${minutes}:${seconds}" | \
    tee -a $LOGFILE >> $build_time_file

if [ "$u_FLAG" = "y" -a "$f_FLAG" = "y" -a "$build_ok" = "y" ]; then
	staffer cp ${SRC}/unref-${MACH}.out $PARENT_WS/usr/src/

	#
	# Produce a master list of unreferenced files -- ideally, we'd
	# generate the master just once after all of the nightlies
	# have finished, but there's no simple way to know when that
	# will be.  Instead, we assume that we're the last nightly to
	# finish and merge all of the unref-${MACH}.out files in
	# $PARENT_WS/usr/src/.  If we are in fact the final ${MACH} to
	# finish, then this file will be the authoritative master
	# list.  Otherwise, another ${MACH}'s nightly will eventually
	# overwrite ours with its own master, but in the meantime our
	# temporary "master" will be no worse than any older master
	# which was already on the parent.
	#

	set -- $PARENT_WS/usr/src/unref-*.out
	cp "$1" ${TMPDIR}/unref.merge
	shift

	for unreffile; do
		comm -12 ${TMPDIR}/unref.merge "$unreffile" > ${TMPDIR}/unref.$$
		mv ${TMPDIR}/unref.$$ ${TMPDIR}/unref.merge
	done

	staffer cp ${TMPDIR}/unref.merge $PARENT_WS/usr/src/unrefmaster.out
fi

#
# All done save for the sweeping up.
# (whichever exit we hit here will trigger the "cleanup" trap which
# optionally sends mail on completion).
#
if [[ "$build_ok" == "y" ]]; then
	exit 0
fi

exit 1