libgpr_24.0.0_d9c96bda/src/gprbuild-main.adb

   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
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
------------------------------------------------------------------------------
--                                                                          --
--                             GPR TECHNOLOGY                               --
--                                                                          --
--                     Copyright (C) 2011-2023, AdaCore                     --
--                                                                          --
-- This is  free  software;  you can redistribute it and/or modify it under --
-- terms of the  GNU  General Public License as published by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  This software is distributed in the hope  that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details.  You should have received  a copy of the  GNU  --
-- General Public License distributed with GNAT; see file  COPYING. If not, --
-- see <http://www.gnu.org/licenses/>.                                      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Command_Line; use Ada.Command_Line;
with Ada.Directories;
with Ada.Exceptions;   use Ada.Exceptions;
with Ada.Text_IO;      use Ada.Text_IO;

pragma Warnings (Off);
with System;
with GNAT.Case_Util;         use GNAT.Case_Util;
with System.Multiprocessors; use System.Multiprocessors;
pragma Warnings (On);

with GNAT.Command_Line;         use GNAT.Command_Line;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
with GNAT.OS_Lib;

with Gpr_Build_Util;            use Gpr_Build_Util;
with Gprbuild.Compile;
with Gprbuild.Link;
with Gprbuild.Post_Compile;

with GPR.Compilation.Process.Waiter;
with GPR.Compilation.Slave;
with GPR;                        use GPR;
with GPR.Debug;                  use GPR.Debug;
with GPR.Conf;                   use GPR.Conf;
with GPR.Names;                  use GPR.Names;
with GPR.Osint;                  use GPR.Osint;
with GPR.Output;                 use GPR.Output;
with GPR.Proc;                   use GPR.Proc;
with GPR.Env;
with GPR.Err;
with GPR.Jobserver;
with GPR.Opt;                    use GPR.Opt;
with GPR.Script;                 use GPR.Script;
with GPR.Snames;                 use GPR.Snames;
with GPR.Tree;                   use GPR.Tree;
with GPR.Util.Aux;               use GPR.Util;

procedure Gprbuild.Main is

   CodePeer_String : constant String := "codepeer";
   --  Used in CopePeer mode for the target and the subdirs

   Dumpmachine : constant String := "--dumpmachine";
   --  Switch to display the normalized hostname

   Dash_A_Warning : constant String :=
     "warning: switch -a is ignored and no additional source is compiled";
   --  Warning issued when gprbuild is invoked with switch -a

   Dash_A_Warning_Issued : Boolean := False;
   --  Flag used to avoid issuing the several times the warning for switch -a

   Subst_Switch_Present : Boolean := False;
   --  True if --compiler-subst=... or --compiler-pkg-subst=... appears on the
   --  command line. Used to detect switches that are incompatible with these.
   --  Also used to prevent passing builder args to the "compiler". These
   --  switches are used by ASIS-based tools such as gnatpp when the
   --  --incremental switch is given.

   Main_On_Command_Line : Boolean := False;
   --  True if there is at least one main specified on the command line

   Is_Unix : constant Boolean := GNAT.OS_Lib.Path_Separator = ':';

   procedure Initialize;
   --  Do the necessary package intialization and process the command line
   --  arguments.

   procedure Usage;
   --  Display the usage

   function Add_Global_Switches
     (Switch                          : String;
      For_Lang                        : Name_Id;
      For_Builder                     : Boolean;
      Has_Global_Compilation_Switches : Boolean) return Boolean;
   --  Take into account a global switch (builder or global compilation switch)
   --  read from the project file.

   procedure Add_Mains_To_Queue;
   --  Check that each main is a single file name and that it is a source
   --  of a project from the tree.

   procedure Scan_Arg
     (Arg          : String;
      Command_Line : Boolean;
      Language     : Name_Id;
      Success      : out Boolean);
   --  Process one gprbuild argument Arg. Command_Line is True if the argument
   --  is specified on the command line.

   procedure Add_Option (Arg : String; Command_Line : Boolean);
   --  Add a switch for a compiler or all compilers, or for the binder or for
   --  the linker. The table where this option is stored depends on the value
   --  of Current_Processor and other global variables.

   procedure Copyright;
   --  Output the Copyright notice

   type Sigint_Handler is access procedure;
   pragma Convention (C, Sigint_Handler);

   procedure Install_Int_Handler (Handler : Sigint_Handler);
   pragma Import (C, Install_Int_Handler, "__gnat_install_int_handler");
   --  Called by Gnatmake to install the SIGINT handler below

   No_Object_Check_Switch     : constant String := "--no-object-check";
   Direct_Import_Only_Switch  : constant String := "--direct-import-only";
   Indirect_Imports_Switch    : constant String := "--indirect-imports";
   No_Indirect_Imports_Switch : constant String := "--no-indirect-imports";

   Current_Working_Dir : constant String := Get_Current_Dir;
   --  The current working directory

   type Processor is (None, Linker, Binder, Compiler, Gprconfig);
   Current_Processor : Processor := None;
   --  This variable changes when switches -*args are used

   Current_Builder_Comp_Option_Table : String_Vector_Access :=
                                         No_Builder_Comp_Option_Table;

   -------------------------------------------
   -- Options specified on the command line --
   -------------------------------------------

   package Options is

      type Option_Type is
        (Force_Compilations_Option,
         Keep_Going_Option,
         Maximum_Compilers_Option,
         Maximum_Binders_Option,
         Maximum_Linkers_Option,
         Quiet_Output_Option,
         Check_Switches_Option,
         Verbose_Mode_Option,
         Verbose_Low_Mode_Option,
         Verbose_Medium_Mode_Option,
         Verbose_High_Mode_Option,
         Warnings_Treat_As_Error,
         Warnings_Normal,
         Warnings_Suppress,
         Indirect_Imports);

      subtype Maximum_Processes_Range is Option_Type range
        Maximum_Compilers_Option .. Maximum_Linkers_Option;

      procedure Register_Command_Line_Option
        (Option : Option_Type; Value : Natural := 0);
      --  Record a command line option

      procedure Process_Command_Line_Options;
      --  Reprocess the recorded command line options that have priority over
      --  the options in package Builder of the main project.

   end Options;

   use Options;

   ------------------------
   -- Add_Mains_To_Queue --
   ------------------------

   procedure Add_Mains_To_Queue is
      Main_Id : Main_Info;

   begin
      Mains.Reset;

      loop
         Main_Id := Mains.Next_Main;
         exit when Main_Id = No_Main_Info;

         if Main_Id.Source /= No_Source then
            --  Fail if any main is declared as an excluded source file

            if Main_Id.Source.Locally_Removed then
               Fail_Program
                 (Project_Tree,
                  "main """ & Get_Name_String (Main_Id.Source.File)
                  & """ cannot also be an excluded file",
                  Exit_Code => E_General);
            end if;

            if Is_Allowed_Language (Main_Id.Source.Language.Name) then
               Queue.Insert
                 (Source     => (Tree    => Main_Id.Tree,
                                 Id      => Main_Id.Source,
                                 Closure => False),
                  With_Roots => Builder_Data (Main_Id.Tree).Closure_Needed);

               --  If a non Ada main has no roots, then all sources need to be
               --  compiled, so no need to check for closure.

               if Main_Id.Source.Language.Config.Kind /= Unit_Based
                 and then Main_Id.Source.Roots = null
               then
                  Builder_Data (Main_Id.Tree).Closure_Needed := False;
               end if;
            end if;
         end if;
      end loop;

      if Total_Errors_Detected /= 0 then
         Fail_Program (Project_Tree, "cannot continue");
      end if;

      --  If the main project is an aggregated project and there is at least
      --  one main on the command line, do not add the sources of the projects
      --  without mains to the queue.

      if Main_Project.Qualifier = Aggregate and then Main_On_Command_Line then
         Mains.Reset;

         loop
            Main_Id := Mains.Next_Main;
            exit when Main_Id = No_Main_Info;

            Queue.Insert_Project_Sources
              (Project        => Main_Id.Project,
               Project_Tree   => Main_Id.Tree,
               Unique_Compile => Unique_Compile,
               All_Projects =>
                  not Unique_Compile
               or else (Unique_Compile_All_Projects or Recursive));
         end loop;

      else
         Queue.Insert_Project_Sources
           (Project        => Main_Project,
            Project_Tree   => Project_Tree,
            Unique_Compile => Unique_Compile,
            All_Projects =>
               not Unique_Compile
            or else (Unique_Compile_All_Projects or Recursive));
      end if;
   end Add_Mains_To_Queue;

   -------------------------
   -- Add_Global_Switches --
   -------------------------

   function Add_Global_Switches
     (Switch                          : String;
      For_Lang                        : Name_Id;
      For_Builder                     : Boolean;
      Has_Global_Compilation_Switches : Boolean) return Boolean
   is
      Success : Boolean;
   begin
      if For_Builder then
         if Has_Global_Compilation_Switches then
            Builder_Switches_Lang := No_Name;
         else
            Builder_Switches_Lang := For_Lang;
         end if;

         Scan_Arg
           (Switch,
            Command_Line => False,
            Language     => For_Lang,
            Success      => Success);
         return Success;

      else
         Current_Processor := Compiler;

         Current_Builder_Comp_Option_Table :=
           Builder_Compiling_Options_HTable.Get (For_Lang);

         if Current_Builder_Comp_Option_Table =
           No_Builder_Comp_Option_Table
         then
            Current_Builder_Comp_Option_Table := new String_Vectors.Vector'
              (String_Vectors.Empty_Vector);
            Builder_Compiling_Options_HTable.Set
              (For_Lang, Current_Builder_Comp_Option_Table);
         end if;

         Add_Option (Switch, Command_Line => False);

         Current_Processor := None;
         return True;
      end if;
   end Add_Global_Switches;

   ----------------
   -- Add_Option --
   ----------------

   procedure Add_Option (Arg : String; Command_Line : Boolean) is
      Option : String_Access := new String'(Arg);

   begin
      case Current_Processor is
         when None =>
            null;

         when Linker =>

            --  Add option to the linker table

            if Command_Line then
               Test_If_Relative_Path
                 (Switch           => Option,
                  Parent           => Current_Working_Dir,
                  Including_Switch => Dash_L);

            else
               Test_If_Relative_Path
                 (Switch           => Option,
                  Parent           => Main_Project_Dir.all,
                  Including_Switch => Dash_L);
            end if;

            Command_Line_Linker_Options.Append (Option.all);

         when Binder =>

            if Command_Line then
               Test_If_Relative_Path
                 (Switch           => Option,
                  Parent           => Current_Working_Dir,
                  Including_Switch => No_Name);

            else
               Test_If_Relative_Path
                 (Switch           => Option,
                  Parent           => Main_Project_Dir.all,
                  Including_Switch => No_Name);
            end if;

            if Current_Bind_Option_Table = No_Bind_Option_Table then
               --  Option for all binder

               All_Language_Binder_Options.Append (Option.all);

            else
               --  Option for a single binder

               Current_Bind_Option_Table.Append (Option.all);
            end if;

         when Compiler =>
            if Command_Line then
               if Starts_With (Arg, "-gnatec=") then
                  declare
                     Key : String :=
                             GNAT.OS_Lib.Normalize_Pathname
                               (Arg (Arg'First + 8 .. Arg'Last));
                     Value : constant Name_Id := Get_Name_Id (Key);
                  begin
                     Canonical_Case_File_Name (Key);
                     Cmd_Line_Adc_Files.Include (Get_Name_Id (Key), Value);
                  end;
               end if;

               if Current_Comp_Option_Table = No_Comp_Option_Table then
                  --  Option for all compilers

                  All_Language_Compiling_Options.Append (Arg);

               else
                  --  Option for a single compiler

                  Current_Comp_Option_Table.Append (Arg);
               end if;

            else
               if Current_Builder_Comp_Option_Table =
                    No_Builder_Comp_Option_Table
               then
                  --  Option for all compilers

                  All_Language_Builder_Compiling_Options.Append (Arg);

               else
                  --  Option for a single compiler

                  Current_Builder_Comp_Option_Table.Append (Arg);
               end if;
            end if;

         when Gprconfig =>

            Command_Line_Gprconfig_Options.Append (Option.all);

      end case;
   end Add_Option;

   ---------------
   -- Copyright --
   ---------------

   procedure Copyright is
   begin
      --  Only output the Copyright notice once

      if not Copyright_Output then
         Copyright_Output := True;
         Display_Version ("GPRBUILD", "2004");
      end if;
   end Copyright;

   -------------
   -- Options --
   -------------

   package body Options is

      type Option_Data is record
         Option : Option_Type;
         Value  : Natural := 0;
      end record;

      package Option_Data_Vectors is new Ada.Containers.Vectors
        (Positive, Option_Data);

      Command_Line_Options : Option_Data_Vectors.Vector;
      --  Table to store the command line options

      ----------------------------------
      -- Process_Command_Line_Options --
      ----------------------------------

      procedure Process_Command_Line_Options is
      begin
         for Item of Command_Line_Options loop
            case Item.Option is
               when Force_Compilations_Option =>
                  Opt.Force_Compilations := True;

               when Keep_Going_Option =>
                  Opt.Keep_Going := True;

               when Maximum_Compilers_Option =>
                  Opt.Maximum_Compilers := Item.Value;

               when Maximum_Binders_Option =>
                  Opt.Maximum_Binders := Item.Value;

               when Maximum_Linkers_Option =>
                  Opt.Maximum_Linkers := Item.Value;

               when Quiet_Output_Option =>
                  Opt.Quiet_Output    := True;
                  Opt.Verbose_Mode    := False;
                  Opt.Verbosity_Level := Opt.None;

               when Check_Switches_Option =>
                  Opt.Check_Switches := True;

               when Verbose_Mode_Option =>
                  Opt.Verbose_Mode    := True;
                  Opt.Verbosity_Level := Opt.Low;
                  Opt.Quiet_Output    := False;

               when Verbose_Low_Mode_Option =>
                  Opt.Verbose_Mode    := True;
                  Opt.Verbosity_Level := Opt.Low;
                  Opt.Quiet_Output    := False;

               when Verbose_Medium_Mode_Option =>
                  Opt.Verbose_Mode    := True;
                  Opt.Verbosity_Level := Opt.Medium;
                  Opt.Quiet_Output    := False;

               when Verbose_High_Mode_Option =>
                  Opt.Verbose_Mode    := True;
                  Opt.Verbosity_Level := Opt.High;
                  Opt.Quiet_Output    := False;

               when Warnings_Treat_As_Error =>
                  Opt.Warning_Mode := Opt.Treat_As_Error;

               when Warnings_Normal =>
                  Opt.Warning_Mode := Opt.Normal;

               when Warnings_Suppress =>
                  Opt.Warning_Mode := Opt.Suppress;

               when Indirect_Imports =>
                  Gprbuild.Indirect_Imports := Item.Value /= 0;
            end case;
         end loop;
      end Process_Command_Line_Options;

      ----------------------------------
      -- Register_Command_Line_Option --
      ----------------------------------

      procedure Register_Command_Line_Option
        (Option : Option_Type; Value : Natural := 0)
      is
      begin
         Command_Line_Options.Append
           (Option_Data'(Option => Option, Value => Value));
      end Register_Command_Line_Option;

   end Options;

   --------------
   -- Scan_Arg --
   --------------

   procedure Scan_Arg
     (Arg          : String;
      Command_Line : Boolean;
      Language     : Name_Id;
      Success      : out Boolean)
   is
      Processed : Boolean := True;

      procedure Forbidden_In_Package_Builder;
      --  Fail if switch Arg is found in package Builder

      ----------------------------------
      -- Forbidden_In_Package_Builder --
      ----------------------------------

      procedure Forbidden_In_Package_Builder is
      begin
         if not Command_Line then
            Fail_Program
              (Project_Tree,
               Arg & " can only be used on the command line",
               Exit_Code => E_General);
         end if;
      end Forbidden_In_Package_Builder;

   begin
      pragma Assert (Arg'First = 1);

      Success := True;

      if Arg'Length = 0 then
         return;
      end if;

      --  If preceding switch was -P, a project file name need to be
      --  specified, not a switch.

      if Project_File_Name_Expected then
         if Arg (1) = '-' then
            Fail_Program
              (Project_Tree, "project file name missing after -P",
               Exit_Code => E_General);
         else
            Project_File_Name_Expected := False;
            Project_File_Name := new String'(Arg);
         end if;

         --  If preceding switch was -o, an executable name need to be
         --  specified, not a switch.

      elsif Output_File_Name_Expected then
         if Arg (1) = '-' then
            Fail_Program
              (Project_Tree, "output file name missing after -o",
               Exit_Code => E_General);
         else
            Output_File_Name_Expected := False;
            Output_File_Name := new String'(Arg);
         end if;

      elsif Search_Project_Dir_Expected then
         if Arg (1) = '-' then
            Fail_Program
              (Project_Tree, "directory name missing after -aP",
               Exit_Code => E_General);
         else
            Search_Project_Dir_Expected := False;
            GPR.Env.Add_Directories (Root_Environment.Project_Path, Arg);
         end if;

      elsif Db_Directory_Expected then
         Db_Directory_Expected := False;
         Knowledge.Parse_Knowledge_Base (Project_Tree, Arg);

         Add_Db_Switch_Arg (Get_Name_Id (Arg));

         --  Set the processor/language for the following switches

         --  -cargs         all compiler arguments

      elsif Arg = "-cargs" then
         Current_Processor := Compiler;

         if Command_Line then
            Current_Comp_Option_Table := No_Comp_Option_Table;

         else
            Current_Builder_Comp_Option_Table := No_Builder_Comp_Option_Table;
         end if;

         --  -cargs:lang    arguments for compiler of language lang

      elsif Arg'Length > 7 and then Arg (1 .. 7) = "-cargs:" then
         Current_Processor := Compiler;

         declare
            Lang : constant Name_Id := Get_Lower_Name_Id (Arg (8 .. Arg'Last));
         begin
            if Command_Line then
               Current_Comp_Option_Table :=
                 Compiling_Options_HTable.Get (Lang);

               if Current_Comp_Option_Table = No_Comp_Option_Table then
                  Current_Comp_Option_Table := new String_Vectors.Vector'
                    (String_Vectors.Empty_Vector);
                  Compiling_Options_HTable.Set
                    (Lang, Current_Comp_Option_Table);
               end if;

            else
               Current_Builder_Comp_Option_Table :=
                 Builder_Compiling_Options_HTable.Get (Lang);

               if Current_Builder_Comp_Option_Table =
                 No_Builder_Comp_Option_Table
               then
                  Current_Builder_Comp_Option_Table :=
                    new String_Vectors.Vector'(String_Vectors.Empty_Vector);
                  Builder_Compiling_Options_HTable.Set
                    (Lang, Current_Builder_Comp_Option_Table);
               end if;
            end if;
         end;

         --  -bargs     all binder arguments

      elsif Arg = "-bargs" then
         if Subst_Switch_Present then
            return; -- ignore switch incompatible with --compiler-subst
         end if;

         Current_Processor := Binder;
         Current_Bind_Option_Table := No_Bind_Option_Table;

         --  -bargs:lang    arguments for binder of language lang

      elsif Arg'Length > 7 and then Arg (1 .. 7) = "-bargs:" then
         if Subst_Switch_Present then
            return; -- ignore switch incompatible with --compiler-subst
         end if;

         Current_Processor := Binder;

         declare
            Lang : constant Name_Id := Get_Lower_Name_Id (Arg (8 .. Arg'Last));
         begin
            Current_Bind_Option_Table :=
              Binder_Options_HTable.Get (Lang);

            if Current_Bind_Option_Table = No_Bind_Option_Table then
               Current_Bind_Option_Table :=
                 new String_Vectors.Vector'(String_Vectors.Empty_Vector);
               Binder_Options_HTable.Set
                 (Lang, Current_Bind_Option_Table);
            end if;
         end;

         --  -largs     linker arguments

      elsif Arg = "-largs" then
         if Subst_Switch_Present then
            return; -- ignore switch incompatible with --compiler-subst
         end if;

         Current_Processor := Linker;

         --  -gargs/margs     options directly for gprbuild
         --  support -margs for compatibility with gnatmake

      elsif Arg = "-kargs" then
         Current_Processor := Gprconfig;

      elsif Arg = "-gargs"
        or else Arg = "-margs"
      then
         Current_Processor := None;

         --  A special test is needed for the -o switch within a -largs since
         --  that is another way to specify the name of the final executable.

      elsif Command_Line
        and then Current_Processor = Linker
        and then Arg = "-o"
      then
         Fail_Program
           (Project_Tree,
            "switch -o not allowed within a -largs. Use -o directly.",
            Exit_Code => E_General);

         --  If current processor is not gprbuild directly, store the option
         --  in the appropriate table.

      elsif Current_Processor /= None then
         Add_Option (Arg, Command_Line);

         --  Switches start with '-'

      elsif Arg (1) = '-' then

         if Arg = Keep_Temp_Files_Option then
            --  This is equivalent to switch -dn: Keep temporary files
            Set_Debug_Flag ('n');
            Opt.Keep_Temporary_Files := True;

         elsif Arg = Complete_Output_Option then
            Forbidden_In_Package_Builder;

            if Distributed_Mode then
               Fail_Program
                 (Project_Tree,
                  "options " & Complete_Output_Option & Distributed_Option
                  & " are not compatible",
                  Exit_Code => E_General);
            end if;

            Complete_Output    := True;
            No_Complete_Output := False;

         elsif Arg = No_Complete_Output_Option or else Arg = "-n" then
            Forbidden_In_Package_Builder;
            No_Complete_Output := True;
            Complete_Output := False;

         elsif Arg = No_Project_Option then
            Forbidden_In_Package_Builder;
            No_Project_File := True;

            if Project_File_Name /= null then
               Fail_Program
                 (Project_Tree,
                  "cannot specified --no-project with a project file",
                  Exit_Code => E_General);
            end if;

         elsif Arg'Length >= Distributed_Option'Length
            and then
            Arg (1 .. Distributed_Option'Length) = Distributed_Option
         then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            if Complete_Output then
               Fail_Program
                 (Project_Tree,
                  "options " & Complete_Output_Option & Distributed_Option
                  & " are not compatible",
                  Exit_Code => E_General);
            end if;

            if Build_Script_Name /= null then
               Fail_Program
                 (Project_Tree,
                  "options " & Build_Script_Option &
                    Distributed_Option & " are not compatible");
            end if;

            Distributed_Mode := True;

            declare
               Hosts : constant String :=
                         Aux.Get_Slaves_Hosts (Project_Tree, Arg);
            begin
               if Hosts = "" then
                  Fail_Program
                    (Project_Tree,
                     "missing hosts for distributed mode compilation",
                     Exit_Code => E_General);

               else
                  GPR.Compilation.Slave.Record_Slaves (Hosts);
               end if;
            end;

         elsif Arg'Length >= Hash_Option'Length
            and then Arg (1 .. Hash_Option'Length) = Hash_Option
         then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            Hash_Value :=
              new String'(Arg (Hash_Option'Length + 2 .. Arg'Last));

         elsif Arg'Length >= Slave_Env_Option'Length
            and then
            Arg (1 .. Slave_Env_Option'Length) = Slave_Env_Option
         then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            if Arg = Slave_Env_Option then
               --  Just --slave-env, it is up to gprbuild to build a sensible
               --  slave environment value.
               Slave_Env_Auto := True;
            else
               Slave_Env :=
                 new String'(Arg (Slave_Env_Option'Length + 2 .. Arg'Last));
            end if;

         elsif Arg'Length >= Compiler_Subst_Option'Length
            and then
            Arg (1 .. Compiler_Subst_Option'Length) = Compiler_Subst_Option
         then
            Forbidden_In_Package_Builder;

            --  We should have Arg set to something like:
            --     "compiler-subst=ada,gnatpp".
            --  We need to pick out the "ada" and "gnatpp".

            declare
               function Scan_To_Comma (Start : Positive) return Positive;
               --  Scan forward from Start until we find a comma or end of
               --  string. Return the index just before the ",", or Arg'Last.

               function Scan_To_Comma (Start : Positive) return Positive is
               begin
                  if Start >= Arg'Last then
                     return Arg'Last;
                  end if;

                  return Result : Positive := Start do
                     while Result < Arg'Last
                       and then Arg (Result + 1) /= ','
                     loop
                        Result := Result + 1;
                     end loop;
                  end return;
               end Scan_To_Comma;

               Lang_Start : constant Positive :=
                 Compiler_Subst_Option'Length + 1;
               Lang_End : constant Positive := Scan_To_Comma (Lang_Start);
               Comp_Start : constant Positive := Lang_End + 2;
               Comp_End : constant Positive := Scan_To_Comma (Comp_Start);

               Lang : String renames Arg (Lang_Start .. Lang_End);
               Comp : String renames Arg (Comp_Start .. Comp_End);

            begin
               if Lang = "" or else Comp = "" then
                  Fail_Program
                    (Project_Tree, "invalid switch " & Arg,
                     Exit_Code => E_General);
                  --  This switch is intended for internal use by ASIS tools,
                  --  so a friendlier error message isn't needed here.
               end if;

               Compiler_Subst_HTable.Include
                 (Get_Lower_Name_Id (Lang), Get_Name_Id (Comp));
            end;

         elsif Arg'Length >= Compiler_Pkg_Subst_Option'Length
            and then
            Arg (1 .. Compiler_Pkg_Subst_Option'Length) =
              Compiler_Pkg_Subst_Option
         then
            Forbidden_In_Package_Builder;

            declare
               Package_Name : String renames
                 Arg (Compiler_Pkg_Subst_Option'Length + 1 .. Arg'Last);
            begin
               if Package_Name = "" then
                  Fail_Program (Project_Tree, "invalid switch " & Arg);
                  --  This switch is intended for internal use by ASIS tools,
                  --  so a friendly error message isn't needed here.
                  --  No error if the package doesn't exist; gnatpp might pass
                  --  --compiler-pkg-subst=pretty_printer even when there is no
                  --  package Pretty_Printer in the project file.
               end if;

               Compiler_Pkg_Subst := Get_Lower_Name_Id (Package_Name);
            end;

         elsif Arg'Length > Build_Script_Option'Length
           and then
             Arg (1 .. Build_Script_Option'Length) = Build_Script_Option
         then
            Forbidden_In_Package_Builder;

            if Distributed_Mode then
               Fail_Program
                 (Project_Tree,
                  "options " & Build_Script_Option & Distributed_Option
                  & " are not compatible",
                  Exit_Code => E_General);
            end if;

            declare
               Script_Name : constant String :=
                 Arg (Build_Script_Option'Length + 1 .. Arg'Last);
            begin
               if Is_Absolute_Path (Script_Name) then
                  Build_Script_Name := new String'(Script_Name);

               else
                  Build_Script_Name :=
                    new String'(Get_Current_Dir & Script_Name);
               end if;
            end;

         elsif Arg = "--db-" then
            Forbidden_In_Package_Builder;

            Load_Standard_Base := False;

         elsif Arg = "--db" then
            Forbidden_In_Package_Builder;

            Db_Directory_Expected := True;

         elsif Arg = "--display-paths" then
            Forbidden_In_Package_Builder;
            Display_Paths := True;

         elsif Arg = "--no-split-units" then
            Opt.No_Split_Units := True;

         elsif Arg = Single_Compile_Per_Obj_Dir_Switch then
            Opt.One_Compilation_Per_Obj_Dir := True;

         elsif Arg'Length > Source_Info_Option'Length
           and then Arg (1 .. Source_Info_Option'Length) = Source_Info_Option
         then
            Forbidden_In_Package_Builder;
            Project_Tree.Source_Info_File_Name :=
               new String'(Arg (Source_Info_Option'Length + 1 .. Arg'Last));

         elsif Arg'Length > Config_Project_Option'Length
           and then
               Arg (1 .. Config_Project_Option'Length) = Config_Project_Option
         then
            if Config_Project_File_Name /= null
              and then Command_Line
              and then (Autoconf_Specified
                        or else Config_Project_File_Name.all /=
                          Arg (Config_Project_Option'Length + 1 .. Arg'Last))
            then
               Fail_Program
                 (Project_Tree,
                  "several different configuration switches cannot be"
                  & " specified",
                  Exit_Code => E_General);

            else
               Autoconfiguration := False;
               Autoconf_Specified := False;
               Config_Project_File_Name :=
                 new String'
                   (Arg (Config_Project_Option'Length + 1 .. Arg'Last));
            end if;

         elsif Arg'Length > Autoconf_Project_Option'Length
           and then
            Arg (1 .. Autoconf_Project_Option'Length) =
              Autoconf_Project_Option
         then
            Forbidden_In_Package_Builder;

            if Config_Project_File_Name /= null
              and then (not Autoconf_Specified
                        or else Config_Project_File_Name.all /=
                          Arg (Autoconf_Project_Option'Length + 1 .. Arg'Last))
            then
               Fail_Program
                 (Project_Tree,
                  "several different configuration switches cannot be"
                  & " specified",
                  Exit_Code => E_General);

            else
               Config_Project_File_Name :=
                 new String'
                   (Arg (Autoconf_Project_Option'Length + 1 .. Arg'Last));
               Autoconf_Specified := True;
            end if;

         elsif Arg'Length > Target_Project_Option'Length
                 and then
               Arg (1 .. Target_Project_Option'Length) = Target_Project_Option
         then
            Forbidden_In_Package_Builder;

            if Target_Name /= null then
               if Target_Name.all /=
                 Arg (Target_Project_Option'Length + 1 .. Arg'Last)
               then
                  Fail_Program
                    (Project_Tree,
                     "several different target switches cannot be specified",
                     Exit_Code => E_General);
               end if;

            else
               Target_Name :=
                 new String'
                   (Arg (Target_Project_Option'Length + 1 .. Arg'Last));
            end if;

         elsif Arg'Length > RTS_Option'Length
           and then Arg (1 .. RTS_Option'Length) = RTS_Option
         then
            declare
               Set : constant Boolean := Runtime_Name_Set_For (Name_Ada);
               Old : constant String := Runtime_Name_For (Name_Ada);
               RTS : constant String :=
                        Arg (RTS_Option'Length + 1 .. Arg'Last);
            begin
               if Command_Line then
                  if Set and then Old /= RTS then
                     Fail_Program
                       (Project_Tree,
                        "several different run-times cannot be specified",
                        Exit_Code => E_General);
                  end if;

                  Set_Runtime_For (Name_Ada, RTS);
                  Set_Default_Runtime_For (Name_Ada, RTS);
               end if;

               --  Ignore any --RTS= switch in package Builder. These are only
               --  taken into account to create the config file in
               --  auto-configuration.
            end;

         elsif Arg'Length > RTS_Language_Option'Length
           and then Arg (1 .. RTS_Language_Option'Length) = RTS_Language_Option
         then
            declare
               Language_Name : Name_Id := No_Name;
               RTS_Start : Natural := Arg'Last + 1;

            begin
               for J in RTS_Language_Option'Length + 2 .. Arg'Last loop
                  if Arg (J) = '=' then
                     Language_Name := Get_Lower_Name_Id
                       (Arg (RTS_Language_Option'Length + 1 .. J - 1));
                     RTS_Start := J + 1;
                     exit;
                  end if;
               end loop;

               if Language_Name = No_Name then
                  Fail_Program
                    (Project_Tree, "illegal switch: " & Arg,
                     Exit_Code => E_General);

               elsif Command_Line then
                  --  Ignore any --RTS:<lang>= switch in package Builder. These
                  --  are only taken into account to create the config file in
                  --  auto-configuration.

                  declare
                     RTS : constant String := Arg (RTS_Start .. Arg'Last);
                     Set : constant Boolean :=
                       Runtime_Name_Set_For (Language_Name);
                     Old : constant String := Runtime_Name_For (Language_Name);

                  begin
                     if Set and then Old /= RTS then
                        Fail_Program
                          (Project_Tree,
                           "several different run-times cannot be specified" &
                           " for the same language");

                     else
                        Set_Runtime_For (Language_Name, RTS);
                        Set_Default_Runtime_For (Language_Name, RTS);
                     end if;
                  end;
               end if;
            end;

         elsif Arg'Length > Implicit_With_Option'Length
           and then Arg (Implicit_With_Option'Range) = Implicit_With_Option
         then
            Forbidden_In_Package_Builder;

            if Implicit_With /= null then
               Fail_Program
                 (Project_Tree,
                  "several " & Implicit_With_Option
                  & " options cannot be specified",
                  Exit_Code => E_General);
            end if;

            Implicit_With := new String'
              (Ensure_Suffix
                 (Arg (Implicit_With_Option'Last + 1 .. Arg'Last),
                  Project_File_Extension));

         elsif Arg'Length > Subdirs_Option'Length
           and then Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
         then
            Forbidden_In_Package_Builder;
            Subdirs :=
              new String'(Arg (Subdirs_Option'Length + 1 .. Arg'Last));

         elsif Is_Unix
           and then Arg'Length > Getrusage_Option'Length
           and then Arg (1 .. Getrusage_Option'Length) = Getrusage_Option
         then
            Forbidden_In_Package_Builder;
            Getrusage :=
              new String'
                (GNAT.OS_Lib.Normalize_Pathname
                   (Arg (Getrusage_Option'Length + 1 .. Arg'Last)));

         elsif Arg'Length > Src_Subdirs_Option'Length
           and then Arg (1 .. Src_Subdirs_Option'Length) = Src_Subdirs_Option
         then
            Forbidden_In_Package_Builder;
            Src_Subdirs :=
              new String'(Arg (Src_Subdirs_Option'Length + 1 .. Arg'Last));

         elsif Arg'Length >= Relocate_Build_Tree_Option'Length
           and then Arg (1 .. Relocate_Build_Tree_Option'Length)
                    = Relocate_Build_Tree_Option
         then
            Forbidden_In_Package_Builder;

            if Arg'Length = Relocate_Build_Tree_Option'Length then
               Build_Tree_Dir := new String'(Current_Working_Dir);

            else
               Build_Tree_Dir :=
                 new String'
                   (Normalize_Pathname
                      (Arg (Relocate_Build_Tree_Option'Length + 2 .. Arg'Last),
                       Current_Working_Dir,
                       Resolve_Links => Opt.Follow_Links_For_Dirs) &
                    Dir_Separator);
            end if;

            --  Out-of-tree compilation also imply -p (create missing dirs)

            Opt.Create_Dirs := Create_All_Dirs;

         elsif Arg'Length >= Root_Dir_Option'Length
           and then Arg (1 .. Root_Dir_Option'Length) = Root_Dir_Option
         then
            Forbidden_In_Package_Builder;

            Root_Dir :=
              new String'
                (Normalize_Pathname
                   (Arg (Root_Dir_Option'Length + 2 .. Arg'Last),
                    Current_Working_Dir,
                    Resolve_Links => Opt.Follow_Links_For_Dirs) &
                   Dir_Separator);

         elsif Command_Line and then Arg = "--no-sal-binding" then
            No_SAL_Binding := True;

         elsif Command_Line
           and then Arg'Length > Restricted_To_Languages_Option'Length
           and then Arg (1 .. Restricted_To_Languages_Option'Length) =
                      Restricted_To_Languages_Option
         then
            declare
               Start  : Positive := Restricted_To_Languages_Option'Length + 1;
               Finish : Positive;

            begin
               Processed := False;

               while Start <= Arg'Last loop
                  Finish := Start;
                  loop
                     exit when Finish > Arg'Last or else Arg (Finish) = ',';
                     Finish := Finish + 1;
                  end loop;

                  if Finish > Start then
                     Add_Restricted_Language (Arg (Start .. Finish - 1));
                     Processed := True;
                  end if;

                  Start := Finish + 1;
               end loop;
            end;

         elsif Arg = Indirect_Imports_Switch then
            Indirect_Imports := True;

            if Command_Line then
               Register_Command_Line_Option (Options.Indirect_Imports, 1);
            end if;

         elsif Arg in No_Indirect_Imports_Switch | Direct_Import_Only_Switch
         then
            Indirect_Imports := False;

            if Command_Line then
               Register_Command_Line_Option (Options.Indirect_Imports, 0);
            end if;

         elsif Arg = Gpr_Build_Util.Unchecked_Shared_Lib_Imports then
            Forbidden_In_Package_Builder;
            Opt.Unchecked_Shared_Lib_Imports := True;

         elsif Arg = No_Object_Check_Switch then
            Object_Checked := False;

         elsif Arg = No_Exit_Message_Option then
            Opt.No_Exit_Message := True;

         elsif Arg = "--codepeer" then
            Forbidden_In_Package_Builder;

            if not CodePeer_Mode then
               CodePeer_Mode := True;
               Object_Checked := False;

               if Target_Name = null then
                  Target_Name := new String'(CodePeer_String);
               end if;

               if Subdirs = null then
                  Subdirs := new String'(CodePeer_String);
               end if;
            end if;

         elsif Arg = "--gnatprove" then
            Forbidden_In_Package_Builder;

            if not GnatProve_Mode then
               GnatProve_Mode := True;
            end if;

         elsif Arg = Create_Map_File_Switch then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            Map_File := new String'("");

         elsif Arg'Length > Create_Map_File_Switch'Length + 1
           and then
             Arg (1 .. Create_Map_File_Switch'Length) = Create_Map_File_Switch
           and then
             Arg (Create_Map_File_Switch'Length + 1) = '='
         then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            Map_File :=
              new String'(Arg (Create_Map_File_Switch'Length + 2 .. Arg'Last));

         elsif Arg'Length >= 3 and then Arg (1 .. 3) = "-aP" then
            Forbidden_In_Package_Builder;

            if Arg'Length = 3 then
               Search_Project_Dir_Expected := True;

            else
               GPR.Env.Add_Directories
                 (Root_Environment.Project_Path, Arg (4 .. Arg'Last));
            end if;
         elsif Arg = "-a" then
            if not Dash_A_Warning_Issued then
               Put_Line (Dash_A_Warning);
               Dash_A_Warning_Issued := True;
            end if;

         elsif Arg = "-b" then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            Opt.Bind_Only  := True;

         elsif Arg = "-c" then
            Opt.Compile_Only := True;

            if Opt.Link_Only then
               Opt.Bind_Only  := True;
            end if;

         elsif Arg = "-C" then
            --  This switch is only for upward compatibility

            null;

         elsif Arg = "-d" then
            Opt.Display_Compilation_Progress := True;

         elsif Arg'Length = 3 and then Arg (2) = 'd' then
            Set_Debug_Flag (Arg (3));

         elsif Arg'Length > 3 and then Arg (1 .. 3) = "-eI" then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            Forbidden_In_Package_Builder;

            begin
               Main_Index := Int'Value (Arg (4 .. Arg'Last));

            exception
               when Constraint_Error =>
                  Fail_Program
                    (Project_Tree, "invalid switch " & Arg,
                     Exit_Code => E_General);
            end;

         elsif Arg = "-eL" then
            Forbidden_In_Package_Builder;
            Opt.Follow_Links_For_Files := True;
            Opt.Follow_Links_For_Dirs  := True;

         elsif Arg = "-eS" then
            Forbidden_In_Package_Builder;

            --  Accept switch for compatibility with gnatmake

         elsif Arg = "-f" then
            Opt.Force_Compilations := True;

            if Command_Line then
               Register_Command_Line_Option (Force_Compilations_Option);
            end if;

         elsif Arg = "-F" then
            Forbidden_In_Package_Builder;
            Opt.Full_Path_Name_For_Brief_Errors := True;

         elsif Arg = "-h" then
            Forbidden_In_Package_Builder;

         elsif Arg'Length > 2 and then Arg (2) = 'j' then

            if Opt.Use_GNU_Make_Jobserver then
               Put_Line ("warning: -j is ignored when using "
                         & Use_GNU_Make_Jobserver_Option);
               return;
            end if;

            declare
               Max_Proc : Natural   := 0;
               Phase    : Character := 'a'; -- all by default
               First    : Positive;

               Opts : constant array (Maximum_Processes_Range) of
                 access Positive :=
                   (Maximum_Compilers_Option => Opt.Maximum_Compilers'Access,
                    Maximum_Binders_Option   => Opt.Maximum_Binders'Access,
                    Maximum_Linkers_Option   => Opt.Maximum_Linkers'Access);

               procedure Register (Opt : Maximum_Processes_Range);

               --------------
               -- Register --
               --------------

               procedure Register (Opt : Maximum_Processes_Range) is
               begin
                  if Command_Line then
                     Register_Command_Line_Option (Opt, Max_Proc);
                  end if;

                  Opts (Opt).all := Max_Proc;
               end Register;

            begin
               if Arg'Length > 3 and then Arg (3) not in '0' .. '9' then
                  Phase := Arg (3);
                  First := 4;
               else
                  First := 3;
               end if;

               Max_Proc := Natural'Value (Arg (First .. Arg'Last));

               if Max_Proc = 0 then
                  Max_Proc := Natural (Number_Of_CPUs);

                  if Max_Proc = 0 then
                     Max_Proc := 1;
                  end if;
               end if;

               case Phase is
                  when 'a' =>
                     for J in Maximum_Processes_Range loop
                        Register (J);
                     end loop;
                  when 'c' => Register (Maximum_Compilers_Option);
                  when 'b' => Register (Maximum_Binders_Option);
                  when 'l' => Register (Maximum_Linkers_Option);
                  when others => Processed := False;
               end case;

            exception
               when Constraint_Error =>
                  Processed := False;
            end;

         elsif Arg = Use_GNU_Make_Jobserver_Option then

            if Opt.Maximum_Compilers > 1 then
               Put_Line ("warning: -j is ignored when using "
                         & Use_GNU_Make_Jobserver_Option);
            end if;

            Opt.Use_GNU_Make_Jobserver := True;

            begin
               GPR.Jobserver.Initialize;
            exception
               when E : GPR.Jobserver.JS_Initialize_Error =>
                  Fail_Program
                    (Project_Tree, Ada.Exceptions.Exception_Name (E) & " - "
                     & Ada.Exceptions.Exception_Message (E));
               when GPR.Jobserver.JS_Makeflags_Parsing_Detects_Dry_Run =>
                  Finish_Program (Project_Tree, Exit_Code);
            end;

         elsif Arg = "-k" then
            Opt.Keep_Going := True;

            if Command_Line then
               Register_Command_Line_Option (Keep_Going_Option);
            end if;

         elsif Arg = "-l" then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            Opt.Link_Only  := True;

            if Opt.Compile_Only then
               Opt.Bind_Only := True;
            end if;

         elsif Arg = "-m" then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            Opt.Minimal_Recompilation := True;

         elsif Arg = "-o" then
            Forbidden_In_Package_Builder;

            if Output_File_Name /= null then
               Fail_Program
                 (Project_Tree, "cannot specify several -o switches");

            else
               Output_File_Name_Expected := True;
            end if;

         elsif Arg = "-p" or else Arg = "--create-missing-dirs" then
            Forbidden_In_Package_Builder;
            Opt.Create_Dirs := Create_All_Dirs;

         elsif Arg'Length >= 2 and then Arg (2) = 'P' then
            Forbidden_In_Package_Builder;

            if No_Project_File then
               Fail_Program
                 (Project_Tree,
                  "cannot specify --no-project with a project file",
                  Exit_Code => E_General);

            elsif Project_File_Name /= null then
               Fail_Program
                 (Project_Tree,
                  "cannot have several project files specified",
                  Exit_Code => E_General);

            elsif Arg'Length = 2 then
               Project_File_Name_Expected := True;

            else
               Project_File_Name := new String'(Arg (3 .. Arg'Last));
            end if;

         elsif Arg = "-q" then
            Opt.Quiet_Output    := True;
            Opt.Verbose_Mode    := False;
            Opt.Verbosity_Level := None;

            if Command_Line then
               Register_Command_Line_Option (Quiet_Output_Option);
            end if;

         elsif Arg = "-r" then
            Forbidden_In_Package_Builder;
            Recursive := True;

         elsif Arg = "-R" then
            if Subst_Switch_Present then
               return; -- ignore switch incompatible with --compiler-subst
            end if;

            Opt.Run_Path_Option := False;

         elsif Arg = "-s" then
            Opt.Check_Switches := True;

            if Command_Line then
               Register_Command_Line_Option (Check_Switches_Option);
            end if;

         elsif Arg = "-u" then
            Forbidden_In_Package_Builder;
            Unique_Compile := True;

         elsif Arg = "-U" then
            Forbidden_In_Package_Builder;
            Unique_Compile_All_Projects := True;
            Unique_Compile := True;

         elsif Arg = "-v" or else Arg = "-vl" then
            Opt.Verbose_Mode    := True;
            Opt.Verbosity_Level := Opt.Low;
            Opt.Quiet_Output    := False;

            if Command_Line then
               Register_Command_Line_Option (Verbose_Low_Mode_Option);
            end if;

         elsif Arg = "-vm" then
            Opt.Verbose_Mode    := True;
            Opt.Verbosity_Level := Opt.Medium;
            Opt.Quiet_Output    := False;

            if Command_Line then
               Register_Command_Line_Option (Verbose_Medium_Mode_Option);
            end if;

         elsif Arg = "-vh" then
            Opt.Verbose_Mode    := True;
            Opt.Verbosity_Level := Opt.High;
            Opt.Quiet_Output    := False;

            if Command_Line then
               Register_Command_Line_Option (Verbose_High_Mode_Option);
            end if;

         elsif Arg'Length >= 3 and then Arg (1 .. 3) = "-vP" then
            Forbidden_In_Package_Builder;

            if Arg'Length = 4 and then  Arg (4) in '0' .. '2' then
               case Arg (4) is
               when '0' =>
                  Current_Verbosity := GPR.Default;
               when '1' =>
                  Current_Verbosity := GPR.Medium;
               when '2' =>
                  Current_Verbosity := GPR.High;
               when others =>
                  null;
               end case;

            else
               Fail_Program
                 (Project_Tree,
                  "invalid verbosity level " & Arg (4 .. Arg'Last),
                  Exit_Code => E_General);
            end if;

         elsif Arg = "-we" then
            Opt.Warning_Mode := Opt.Treat_As_Error;

            if Command_Line then
               Register_Command_Line_Option (Warnings_Treat_As_Error);
            end if;

         elsif Arg = "-wn" then
            Opt.Warning_Mode := Opt.Normal;

            if Command_Line then
               Register_Command_Line_Option (Warnings_Normal);
            end if;

         elsif Arg = "-ws" then
            Opt.Warning_Mode  := Opt.Suppress;

            if Command_Line then
               Register_Command_Line_Option (Warnings_Suppress);
            end if;

         elsif Arg = "-m2" then
            Opt.Checksum_Recompilation := True;

         elsif Arg = "-x" then
            Opt.Use_Include_Path_File := True;

         elsif Arg = "-z" then
            Opt.No_Main_Subprogram := True;

         elsif Arg'Length >= 3
           and then Arg (2) = 'X'
           and then Is_External_Assignment (Root_Environment, Arg)
         then
            Forbidden_In_Package_Builder;

            --  Is_External_Assignment has side effects when it returns True

            null;

         elsif (Language = No_Name or else Language = Name_Ada)
           and then not Command_Line
           and then Arg = "-x"
         then
            --  For compatibility with gnatmake, ignore -x if found in the
            --  Builder switches.

            null;

         elsif (Language = No_Name or else Language = Name_Ada)
            and then not Subst_Switch_Present
            and then
             (Arg = "-fstack-check"
              or else Arg = "-fno-inline"
              or else
                (Arg'Length >= 2
                 and then (Arg (2) = 'O' or else Arg (2) = 'g')))
         then
            --  For compatibility with gnatmake, use switch to compile Ada
            --  code. We don't do this if the --compiler-pkg-subst switch was
            --  given, because the tool won't understand normal compiler
            --  options.

            if Command_Line then
               Current_Comp_Option_Table :=
                 Compiling_Options_HTable.Get (Name_Ada);

               if Current_Comp_Option_Table = No_Comp_Option_Table then
                  Current_Comp_Option_Table := new String_Vectors.Vector'
                    (String_Vectors.Empty_Vector);
                  Compiling_Options_HTable.Set
                    (Name_Ada, Current_Comp_Option_Table);
               end if;

            else
               Current_Builder_Comp_Option_Table :=
                 Builder_Compiling_Options_HTable.Get (Name_Ada);

               if Current_Builder_Comp_Option_Table =
                  No_Builder_Comp_Option_Table
               then
                  Current_Builder_Comp_Option_Table :=
                    new String_Vectors.Vector'(String_Vectors.Empty_Vector);
                  Builder_Compiling_Options_HTable.Set
                    (Name_Ada, Current_Builder_Comp_Option_Table);
               end if;
            end if;

            Current_Processor := Compiler;
            Add_Option (Arg, Command_Line);
            Current_Processor := None;

         elsif (Language = No_Name or else Language = Name_Ada)
            and then
             (Arg = "-nostdlib" or else Arg = "-nostdinc")
         then
            --  For compatibility with gnatmake, use switch to bind Ada code
            --  code and for -nostdlib to link.

            Current_Bind_Option_Table :=
              Binder_Options_HTable.Get (Name_Ada);

            if Current_Bind_Option_Table = No_Bind_Option_Table then
               Current_Bind_Option_Table :=
                 new String_Vectors.Vector'(String_Vectors.Empty_Vector);
               Binder_Options_HTable.Set
                 (Name_Ada, Current_Bind_Option_Table);
            end if;

            Current_Processor := Binder;
            Add_Option (Arg, Command_Line);

            --  For -nostdlib, use the switch to link too

            if Arg = "-nostdlib" then
               Current_Processor := Linker;
               Add_Option (Arg, Command_Line);
            end if;

            Current_Processor := None;

         else
            Processed := False;
         end if;

      elsif Command_Line then
         --  The file name of a main or a project file

         declare
            File_Name : String := Arg;

         begin
            Canonical_Case_File_Name (File_Name);

            if File_Name'Length > Project_File_Extension'Length
              and then File_Name
                (File_Name'Last - Project_File_Extension'Length + 1
                 .. File_Name'Last) = Project_File_Extension
            then
               if No_Project_File then
                  Fail_Program
                    (Project_Tree,
                     "cannot specify --no-project with a project file",
                     Exit_Code => E_General);

               elsif Project_File_Name /= null then
                  Fail_Program
                    (Project_Tree,
                     "cannot have several project files specified",
                     Exit_Code => E_General);

               else
                  Project_File_Name := new String'(File_Name);
               end if;

            else
               --  Not a project file, then it is a main

               Mains.Add_Main (Arg);
               Always_Compile := True;
               Main_On_Command_Line := True;
            end if;
         end;

      else
         Processed := False;
      end if;

      if not Processed then
         if Command_Line then
            Fail_Program
              (Project_Tree,
               "illegal option """ & Arg & """ on the command line",
               Exit_Code => E_General);

         else
            Success := False;
         end if;
      end if;
   end Scan_Arg;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
      procedure Check_Version_And_Help is new
        Check_Version_And_Help_G (Usage);
   begin
      --  Do some necessary package initializations

      Snames.Initialize;

      Set_Program_Name ("gprbuild");

      Set_Default_Verbosity;

      GPR.Tree.Initialize (Root_Environment, Gprbuild_Flags);
      GPR.Tree.Initialize (Project_Node_Tree);

      GPR.Initialize (Project_Tree);
      Mains.Delete;

      --  Get the name id for "-L";

      Dash_L := Get_Name_Id ("-L");

      --  Get the command line arguments, starting with --version and --help

      Check_Version_And_Help ("GPRBUILD", "2004");

      --  Check for switch --dumpmachine and, if found, output the normalized
      --  hostname and exit.

      for Arg in 1 .. Argument_Count loop
         if Argument (Arg) = Dumpmachine then
            Knowledge.Parse_Knowledge_Base (Project_Tree);
            Put_Line (Knowledge.Normalized_Hostname);
            OS_Exit (0);
         end if;
      end loop;

      --  Check for switch -h an, if found, display usage and exit

      for Arg in 1 .. Argument_Count loop
         if Argument (Arg) = "-h" then
            Usage;
            OS_Exit (0);
         end if;
      end loop;

      --  By default, gprbuild should create artefact dirs if they are
      --  relative to the project directory

      Opt.Create_Dirs := Create_Relative_Dirs_Only;

      --  Now process the other options

      Autoconfiguration := True;

      Get_Command_Line_Arguments;

      declare
         Do_Not_Care : Boolean;

      begin
         for Next_Arg in 1 .. Last_Command_Line_Argument loop
            declare
               Arg : constant String := Command_Line_Argument (Next_Arg);
            begin
               if (Arg'Length >= Compiler_Subst_Option'Length
                     and then
                   Arg (1 .. Compiler_Subst_Option'Length) =
                     Compiler_Subst_Option)
                 or else
                   (Arg'Length >= Compiler_Pkg_Subst_Option'Length
                    and then
                    Arg (1 .. Compiler_Pkg_Subst_Option'Length) =
                      Compiler_Pkg_Subst_Option)
               then
                  Subst_Switch_Present := True;
               end if;
            end;
         end loop;

         Scan_Args : for Next_Arg in 1 .. Last_Command_Line_Argument loop
            Scan_Arg
              (Command_Line_Argument (Next_Arg),
               Command_Line => True,
               Language     => No_Name,
               Success      => Do_Not_Care);
         end loop Scan_Args;
      end;

      if Debug.Debug_Flag_N then
         Opt.Keep_Temporary_Files := True;
      end if;

      if CodePeer_Mode then
         if Languages_Are_Restricted then
            Remove_All_Restricted_Languages;
         end if;

         Add_Restricted_Language ("ada");

         Opt.Link_Only := False;

         if not Opt.Compile_Only and not Opt.Bind_Only then
            Opt.Compile_Only := True;
            Opt.Bind_Only    := True;
         end if;

      elsif Languages_Are_Restricted then
         Opt.Compile_Only := True;
         Opt.Bind_Only    := False;
         Opt.Link_Only    := False;
      end if;

      Mains.Set_Multi_Unit_Index (Project_Tree, Main_Index);

      Current_Processor := None;

      GPR.Env.Initialize_Default_Project_Path
        (Root_Environment.Project_Path, Target_Name => "-");

      --  If --display-paths was specified, display the config and the user
      --  project paths and exit.

      if Display_Paths then
         Put ('.');

         declare
            Prefix_Path : constant String := Executable_Prefix_Path;
         begin
            if Prefix_Path'Length /= 0 then
               Put (Path_Separator);
               Put (Prefix_Path);
               Put ("share");
               Put (Directory_Separator);
               Put ("gpr");
            end if;

            New_Line;

            Put_Line (Env.Get_Path (Root_Environment.Project_Path));
            Exit_Program (E_Success);
         end;
      end if;

      if Opt.Verbosity_Level > Opt.Low then
         Copyright;
      end if;

      --  Fail if command line ended with "-P"

      if Project_File_Name_Expected then
         Fail_Program
           (Project_Tree, "project file name missing after -P",
            Exit_Code => E_General);

         --  Or if it ended with "-o"

      elsif Output_File_Name_Expected then
         Fail_Program
           (Project_Tree, "output file name missing after -o",
            Exit_Code => E_General);

         --  Or if it ended with "-aP"

      elsif Search_Project_Dir_Expected then
         Fail_Program
           (Project_Tree, "directory name missing after -aP",
            Exit_Code => E_General);

      elsif Db_Directory_Expected then
         Fail_Program
           (Project_Tree, "directory name missing after --db",
            Exit_Code => E_General);

      elsif Slave_Env /= null and then not Distributed_Mode then
         Fail_Program
           (Project_Tree, "cannot use --slave-env in non distributed mode");
      end if;

      if Load_Standard_Base then
         --  We need to parse the knowledge base so that we are able to
         --  normalize the target names. Unfortunately, if we have to spawn
         --  gprconfig, it will also have to parse that knowledge base on
         --  its own.
         Knowledge.Parse_Knowledge_Base (Project_Tree);
      end if;

      --  If no project file is specified, look for a default

      if Project_File_Name = null then
         Look_For_Default_Project;

      else
         No_Project_File_Found := False;
      end if;

      if Project_File_Name = null then
         Try_Help;
         Fail_Program
           (Project_Tree,
            "no project file specified and no default project file");
      end if;

      --  Check consistency of out-of-tree build options.

      if Root_Dir /= null and then Build_Tree_Dir = null then
         Fail_Program
           (Project_Tree,
            "cannot use --root-dir without --relocate-build-tree option",
            Exit_Code => E_General);
      end if;
   end Initialize;

   -----------
   -- Usage --
   -----------

   procedure Usage is
   begin
      if not Usage_Output then
         Usage_Output := True;

         Put ("Usage: ");
         Put ("gprbuild [-P<proj>] [<proj>.gpr] [opts] [name]");
         New_Line;
         Put ("    {[-cargs opts] [-cargs:lang opts] [-largs opts]" &
                    " [-kargs opts] [-gargs opts]}");
         New_Line;
         New_Line;
         Put ("  name is zero or more file names");
         New_Line;
         New_Line;

         --  GPRBUILD switches

         Put ("gprbuild switches:");
         New_Line;

         Display_Usage_Version_And_Help;

         --  Line for --no-project

         Put_Line ("  --no-project");
         Put_Line ("           Do not use project file");

         --  Line for --distributed

         Put ("  --distributed=slave1[,slave2]");
         New_Line;
         Put ("           Activate the remote/distributed compilations");
         New_Line;

         --  Line for --hash

         Put ("  --hash=string");
         New_Line;
         Put ("           Set an hash string to identified environment");
         New_Line;

         --  Line for --slave-env

         Put ("  --slave-env[=name]");
         New_Line;
         Put ("           Use a specific slave's environment");
         New_Line;
         New_Line;

         --  Line for --complete-output

         Put ("  --complete-output");
         New_Line;
         Put ("           Display all previous errors and warnings");
         New_Line;

         --  Line for --no-complete-output

         Put ("  --no-complete-output, -n");
         New_Line;
         Put ("           Do not store compilation outputs in files");
         New_Line;
         New_Line;

         --  Line for Config_Project_Option

         Put ("  ");
         Put (Config_Project_Option);
         Put ("file.cgpr");
         New_Line;
         Put ("           Specify the main config project file name");
         New_Line;

         --  Line for Autoconf_Project_Option

         Put ("  ");
         Put (Autoconf_Project_Option);
         Put ("file.cgpr");
         New_Line;
         Put
           ("           Specify/create the main config project file name");
         New_Line;

         --  Line for Target_Project_Option

         Put ("  ");
         Put (Target_Project_Option);
         Put ("targetname");
         New_Line;
         Put
           ("           Specify a target for cross platforms");
         New_Line;

         --  Line for --db

         Put ("  --db dir Parse dir as an additional knowledge base");
         New_Line;

         --  Line for --db-

         Put ("  --db-    Do not load the standard knowledge base");
         New_Line;

         Put ("  --implicit-with=filename");
         New_Line;
         Put ("           Add the given projects as a dependency on all loaded"
              & " projects");
         New_Line;

         --  Line for --relocate-build-tree=

         Put ("  --relocate-build-tree[=dir]");
         New_Line;
         Put ("           Root obj/lib/exec dirs are current-directory" &
                    " or dir");
         New_Line;

         --  Line for --root-dir=

         Put ("  --root-dir=dir");
         New_Line;
         Put ("           Root directory of obj/lib/exec to relocate");
         New_Line;

         --  Line for --src-subdirs=

         Put ("  --src-subdirs=dir");
         New_Line;
         Put ("           Prepend <obj>/dir to the list of source dirs" &
              " for each project");
         New_Line;

         --  Line for --subdirs=

         Put ("  --subdirs=dir");
         New_Line;
         Put ("           Use dir as suffix to obj/lib/exec directories");
         New_Line;

         if Is_Unix then
            Put_Line ("  --getrusage=file");
            Put_Line ("           Print getrusage call results into file");
         end if;

         --  Line for --single-compile-per-obj-dir

         Put ("  ");
         Put (Single_Compile_Per_Obj_Dir_Switch);
         New_Line;
         Put
           ("           No simultaneous compilations for the same obj dir");
         New_Line;

         --  Line for --build-script=

         Put ("  ");
         Put (Build_Script_Option);
         Put_Line ("script_file");
         Put ("           Create build script script_file");
         New_Line;

         Put ("  ");
         Put (No_Indirect_Imports_Switch);
         New_Line;
         Put
           ("           Sources can import only from directly imported " &
            "projects");
         New_Line;

         Put ("  ");
         Put (Indirect_Imports_Switch);
         New_Line;
         Put
           ("           Sources can import from directly and indirectly " &
            "imported projects");
         New_Line;

         Put ("  --RTS=<runtime>");
         New_Line;
         Put ("           Use runtime <runtime> for language Ada");
         New_Line;

         Put ("  --RTS:<lang>=<runtime>");
         New_Line;
         Put ("           Use runtime <runtime> for language <lang>");
         New_Line;

         Put ("  ");
         Put (Gpr_Build_Util.Unchecked_Shared_Lib_Imports);
         New_Line;
         Put ("           Shared lib projects may import any project");
         New_Line;

         Put ("  ");
         Put (No_Object_Check_Switch);
         New_Line;
         Put ("           Do not check object files");
         New_Line;

         Put ("  --no-sal-binding");
         New_Line;
         Put ("           Reuse binder files when linking SALs");
         New_Line;

         Put ("  ");
         Put (Restricted_To_Languages_Option);
         Put ("<list of languages>");
         New_Line;
         Put ("           Restrict the languages of the sources");
         New_Line;
         New_Line;

         Put ("  ");
         Put (Create_Map_File_Switch);
         New_Line;
         Put ("           Create map file mainprog.map");
         New_Line;

         Put ("  ");
         Put (Create_Map_File_Switch);
         Put ("=mapfile");
         New_Line;
         Put ("           Create map file mapfile");
         New_Line;

         Put ("  ");
         Put (Source_Info_Option & "<filename>");
         New_Line;
         Put
           ("           Specify/create the project sources cache file");
         New_Line;

         Put ("  ");
         Put (Keep_Temp_Files_Option);
         New_Line;
         Put ("           Do not delete temporary files");
         New_Line;
         New_Line;

         Put ("  ");
         Put (Use_GNU_Make_Jobserver_Option);
         New_Line;
         Put ("           Share job slots with GNU make");
         New_Line;
         New_Line;

         --  Line for -aP

         Put ("  -aP dir  Add directory dir to project search path");
         New_Line;

         --  Line for -b

         Put ("  -b       Bind only");
         New_Line;

         --  Line for -c

         Put ("  -c       Compile only");
         New_Line;

         --  Line for -d

         Put ("  -d       Display compilation progress");
         New_Line;

         --  Line for -eInn

         Put ("  -eInn    Index of main unit in multi-unit source file");
         New_Line;

         --  Line for -eL

         Put ("  -eL      " &
                    "Follow symbolic links when processing project files");
         New_Line;

         --  Line for -eS

         Put ("  -eS      " &
                    "(no action, for compatibility with gnatmake only)");
         New_Line;

         --  Line for -f

         Put ("  -f       Force recompilations");
         New_Line;

         --  Line for -F

         Put
           ("  -F       Full project path name in brief error messages");
         New_Line;

         --  Line for -jnnn

         Put ("  -j<num>    Use <num> processes to compile, bind, and link");
         New_Line;

         Put ("  -jc<num>    Use <num> processes to compile");
         New_Line;

         Put ("  -jb<num>    Use <num> processes to bind");
         New_Line;

         Put ("  -jl<num>    Use <num> processes to link");
         New_Line;

         --  Line for -k

         Put ("  -k       Keep going after compilation errors");
         New_Line;

         --  Line for -l

         Put ("  -l       Link only");
         New_Line;

         --  Line for -m

         Put ("  -m       Minimum Ada recompilation");
         New_Line;

         --  Line for -m2

         Put ("  -m2      Checksum based Ada recompilation");
         New_Line;

         --  Line for -o

         Put ("  -o name  Choose an alternate executable name");
         New_Line;

         --  Line for -p

         Put ("  -p       Create missing obj, lib and exec dirs");
         New_Line;

         --  Line for -P

         Put ("  -P proj  Use Project File proj");
         New_Line;

         --  Line for -q

         Put ("  -q       Be quiet/terse");
         New_Line;

         --  Line for -r

         Put ("  -r       Recursive (default except when using -c)");
         New_Line;

         --  Line for -R

         Put ("  -R       Do not use run path option");
         New_Line;

         --  Line for -s

         Put ("  -s       Recompile if compiler switches have changed");
         New_Line;

         --  Line for -u

         Put
           ("  -u       Unique compilation, only compile the given files");
         New_Line;

         --  Line for -U

         Put
           ("  -U       Unique compilation for all sources of all projects");
         New_Line;

         --  Line for -v

         Put ("  -v       Verbose output");
         New_Line;

         --  Line for -vl

         Put ("  -vl      Verbose output (low verbosity)");
         New_Line;

         --  Line for -vm

         Put ("  -vm      Verbose output (medium verbosity)");
         New_Line;

         --  Line for -vh

         Put ("  -vh      Verbose output (high verbosity)");
         New_Line;

         --  Line for -vPx

         Put ("  -vPx     Specify verbosity when parsing Project Files" &
                    " (x = 0/1/2)");
         New_Line;

         --  Line for -we

         Put ("  -we      Treat all warnings as errors");
         New_Line;

         --  Line for -wn

         Put ("  -wn      Treat warnings as warnings");
         New_Line;

         --  Line for -ws

         Put ("  -ws      Suppress all gprbuild-specific warnings");
         New_Line;

         --  Line for -x

         Put ("  -x       Always create include path file");
         New_Line;

         --  Line for -X

         Put ("  -Xnm=val Specify an external reference for " &
                    "Project Files");
         New_Line;
         New_Line;

         --  Line for -z

         Put ("  -z       No main subprogram (zero main)");
         New_Line;

         --  Line for --compiler-subst

         Put_Line ("  --compiler-subst=lang,tool    Specify alternate " &
                     "compiler");

         --  Line for --compiler-pkg-subst

         Put_Line ("  --compiler-pkg-subst=pkg    Specify alternate " &
                     "package");
         New_Line;
         New_Line;

         --  Line for -cargs

         Put_Line ("  -cargs opts    opts are passed to all compilers");

         --  Line for -cargs:lang

         Put_Line ("  -cargs:<lang> opts");
         Put_Line ("                 opts are passed to the compiler " &
                     "for language <lang> ");

         --  Line for -bargs

         Put_Line ("  -bargs opts    opts are passed to all binders");

         --  Line for -cargs:lang

         Put_Line ("  -bargs:<lang> opts");
         Put_Line ("                 opts are passed to the binder " &
                     "for language <lang> ");

         --  Line for -largs

         Put ("  -largs opts    opts are passed to the linker");
         New_Line;

         --  Line for -kargs

         Put ("  -kargs opts    opts are passed to gprconfig");
         New_Line;

         --  Line for -gargs

         Put ("  -gargs opts    opts directly interpreted by gprbuild");
         New_Line;

         --  Line for -margs

         Put ("  -margs opts    equivalent to -gargs opts");
         New_Line;

         New_Line;

         Put
           ("For compatibility with gnatmake, these switches are passed " &
            "to the Ada compiler:");
         New_Line;

         Put ("  -nostdlib");
         New_Line;

         Put ("  -nostdinc");
         New_Line;

         Put ("  -fstack-check");
         New_Line;

         Put ("  -fno-inline");
         New_Line;

         Put ("  -gxxx");
         New_Line;

         Put ("  -Oxx");
         New_Line;

         New_Line;
      end if;
   end Usage;

   User_Project_Node : Project_Node_Id;

   procedure Do_Compute_Builder_Switches is
      new Compute_Builder_Switches (Add_Global_Switches);

begin
   --  First initialize and read the command line arguments

   Initialize;

   --  And install Ctrl-C handler

   Install_Int_Handler (Gprbuild.Sigint_Intercepted'Access);

   --  Add the external variable GPR_TOOL (default value "gprbuild")

   Add_Gpr_Tool_External;

   --  Check command line arguments. These will be overridden when looking
   --  for the configuration file

   if Target_Name = null then
      Target_Name := new String'("");
   end if;

   if Config_Project_File_Name = null then
      Config_Project_File_Name := new String'("");

   elsif Autoconf_Specified then
      --  Check if path needs to be created

      declare
         Config_Path : constant String :=
                         Ada.Directories.Containing_Directory
                           (Config_Project_File_Name.all);
      begin
         if not Ada.Directories.Exists (Config_Path) then
            Ada.Directories.Create_Path (Config_Path);
         end if;
      end;
   end if;

   --  Then, parse the user's project and the configuration file. Apply the
   --  configuration file to the project so that its settings are
   --  automatically inherited by the project.
   --  If either the project or the configuration file contains errors, the
   --  following call with call Fail_Program and never return

   begin
      Main_Project := No_Project;
      Parse_Project_And_Apply_Config
        (Main_Project               => Main_Project,
         User_Project_Node          => User_Project_Node,
         Config_File_Name           => Config_Project_File_Name.all,
         Autoconf_Specified         => Autoconf_Specified,
         Project_File_Name          => Project_File_Name.all,
         Project_Tree               => Project_Tree,
         Env                        => Root_Environment,
         Project_Node_Tree          => Project_Node_Tree,
         Packages_To_Check          => Packages_To_Check,
         Allow_Automatic_Generation => Autoconfiguration,
         Automatically_Generated    => Delete_Autoconf_File,
         Config_File_Path           => Configuration_Project_Path,
         Target_Name                => Target_Name.all,
         Normalized_Hostname        => Knowledge.Normalized_Hostname,
         Implicit_Project           => No_Project_File_Found,
         Gprconfig_Options          => Command_Line_Gprconfig_Options);
   exception
      when E : GPR.Conf.Invalid_Config =>
         Fail_Program
           (Project_Tree, Exception_Message (E), Exit_Code => E_Project);
   end;

   if Main_Project = No_Project then
      --  Don't flush messages in case of parsing error. This has already
      --  been taken care when parsing the tree. Otherwise, it results in
      --  the same message being displayed twice.

      Fail_Program
        (Project_Tree,
         """" & Project_File_Name.all & """ processing failed",
         Flush_Messages => Present (User_Project_Node),
         Exit_Code      => E_Project);
   end if;

   if Configuration_Project_Path /= null then
      Free (Config_Project_File_Name);
      Config_Project_File_Name := new String'
        (Base_Name (Configuration_Project_Path.all));
   end if;

   if Total_Errors_Detected > 0 then
      GPR.Err.Finalize;
      Fail_Program
        (Project_Tree,
         "problems while getting the configuration",
         Flush_Messages => False);
   end if;

   --  Warn if there have been binder option specified on the command line
   --  and the main project is a Stand-Alone Library project.

   declare
      Options_Instance : constant Bind_Option_Table_Ref :=
                           Binder_Options_HTable.Get (Name_Ada);
   begin
      if not All_Language_Binder_Options.Is_Empty
        or else (Options_Instance /= No_Bind_Option_Table
                 and then not Options_Instance.Is_Empty)
      then
         if Main_Project.Standalone_Library /= No then
            GPR.Err.Error_Msg
              ("?binding options on the command line are not taken " &
               "into account when the main project is a Stand-Alone " &
               "Library project",
               Main_Project.Location);
         end if;
      end if;
   end;

   Main_Project_Dir :=
     new String'(Get_Name_String (Main_Project.Directory.Display_Name));

   if Warnings_Detected > 0 then
      GPR.Err.Finalize;
      GPR.Err.Initialize;
   end if;

   --  Adjust switches for C and jvm targets: never perform the link phase

   declare
      No_Link  : Boolean := False;
      Variable : Variable_Value;
   begin
      if No_Link_Target (Target_Name.all) then
         No_Link := True;

      else
         Variable := GPR.Util.Value_Of
           (Name_Target, Main_Project.Decl.Attributes, Project_Tree.Shared);

         if Variable /= Nil_Variable_Value
           and then No_Link_Target (Get_Name_String (Variable.Value))
         then
            No_Link := True;

            --  Set Target_Name so that e.g. gprbuild-post_compile.adb knows
            --  that we have Target = c/ccg/jvm.

            Free (Target_Name);
            Target_Name := new String'(Get_Name_String (Variable.Value));
         end if;
      end if;

      if No_Link then
         Opt.Link_Only := False;

         if not Opt.Compile_Only and not Opt.Bind_Only then
            Opt.Compile_Only := True;
            Opt.Bind_Only    := True;
         end if;
      end if;
   end;

   Compute_All_Imported_Projects (Main_Project, Project_Tree);

   if Main_Project.Qualifier = Aggregate_Library then
      if Main_On_Command_Line then
         if (not Opt.Compile_Only or else Opt.Bind_Only)
            and then not Unique_Compile
         then
            Fail_Program
              (Project_Tree,
               "cannot specify a main program " &
               "on the command line for a library project file",
               Exit_Code => E_General);

         else
            Mains.Complete_Mains
              (Root_Environment.Flags,
               Main_Project,
               Project_Tree,
               Unique_Compile);
         end if;
      end if;

   else
      if Mains.Number_Of_Mains (Project_Tree) = 0
        and then not Unique_Compile
      then
         --  Register the Main units from the projects.
         --  No need to waste time when we are going to compile all files
         --  anyway (Unique_Compile).
         Mains.Fill_From_Project (Main_Project, Project_Tree);
      end if;

      Mains.Complete_Mains
        (Root_Environment.Flags, Main_Project, Project_Tree, Unique_Compile);

      if not Unique_Compile
        and then Output_File_Name /= null
        and then Mains.Number_Of_Mains (null) > 1
      then
         Fail_Program
           (Project_Tree,
            "cannot specify -o when there are several mains",
            Exit_Code => E_General);
      end if;
   end if;

   Do_Compute_Builder_Switches
     (Project_Tree     => Project_Tree,
      Env              => Root_Environment,
      Main_Project     => Main_Project);

   Queue.Initialize (Opt.One_Compilation_Per_Obj_Dir);

   Compute_Compilation_Phases
     (Project_Tree,
      Main_Project,
      Option_Unique_Compile => Unique_Compile,
      Option_Compile_Only   => Opt.Compile_Only,
      Option_Bind_Only      => Opt.Bind_Only,
      Option_Link_Only      => Opt.Link_Only);

   if Mains.Number_Of_Mains (Project_Tree) > 0
     and then Main_Project.Library
     and then Builder_Data (Project_Tree).Need_Binding
   then
      Fail_Program
        (Project_Tree,
         "cannot specify a main program " &
           "on the command line for a library project file",
         Exit_Code => E_General);
   end if;

   Add_Mains_To_Queue;

   --  If no sources to compile, then there is nothing to do

   if Queue.Size = 0 then
      if not Opt.Quiet_Output
        and then not Main_Project.Externally_Built
      then
         Write_Program_Name;
         Write_Line ("no sources to compile");
      end if;

      Finish_Program (Project_Tree, E_Success);
   end if;

   Always_Compile :=
     Always_Compile
     and then Opt.Force_Compilations
     and then Unique_Compile
     and then not Unique_Compile_All_Projects;

   --  Reprocess recorded command line options that have priority over
   --  those in the main project file.

   Options.Process_Command_Line_Options;

   Check_Maximum_Processes;

   --  If a build script is declared, try to create the file. Fail if the file
   --  cannot be created.

   if Build_Script_Name /= null then
      begin
         Create (Build_Script_File, Out_File, Build_Script_Name.all);

      exception
         when others =>
            Fail_Program
              (null,
               "build script """ & Build_Script_Name.all
               & """ could not be created");
      end;
   end if;

   if Debug.Debug_Flag_M then
      Put_Line
        ("Maximum number of simultaneous compilations ="
         & Opt.Maximum_Compilers'Img);
   end if;

   --  Warn if --create-map-file is not supported

   if Map_File /= null
     and then Main_Project.Config.Map_File_Option = No_Name
   then
      Put ("warning: option ");
      Put (Create_Map_File_Switch);
      Put (" is not supported in this configuration");
      New_Line;
   end if;

   --  Set slave-env

   if Distributed_Mode then
      if Slave_Env = null then
         Slave_Env :=
           new String'(Aux.Compute_Slave_Env (Project_Tree, Slave_Env_Auto));

         if Slave_Env_Auto and not Opt.Quiet_Output then
            Put ("slave environment is ");
            Put (Slave_Env.all);
            New_Line;
         end if;
      end if;
   end if;

   Compile.Run;

   --  If the build script file is opened, close it, so that it can be reopened
   --  by gprlib and gprbind.

   if Is_Open (Build_Script_File) then
      Close (Build_Script_File);
      Opt.Maximum_Binders := 1;
      Opt.Maximum_Linkers := 1;
   end if;

   Post_Compile.Run;
   Link.Run;

   if Warnings_Detected /= 0 then
      GPR.Err.Finalize;
   end if;

   if Getrusage /= null then
      Put_Resource_Usage (Getrusage.all);
   end if;

   Finish_Program (Project_Tree, Exit_Code);

exception
   when C : Constraint_Error =>
      if Distributed_Mode then
         GPR.Compilation.Slave.Unregister_Remote_Slaves (From_Signal => True);
      end if;

      Fail_Program (Project_Tree, Exception_Information (C));

   when Project_Error =>
      Fail_Program
        (Project_Tree, '"' & Project_File_Name.all & """ processing failed");

   when E : others =>
      Fail_Program (Project_Tree, Exception_Information (E));
end Gprbuild.Main;