libgpr_24.0.0_d9c96bda/src/gprbuild-compile.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
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
------------------------------------------------------------------------------
--                                                                          --
--                             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.Containers.Indefinite_Ordered_Maps;
with Ada.Exceptions;                         use Ada.Exceptions;
with Ada.Strings.Fixed;                      use Ada, Ada.Strings.Fixed;
with Ada.Strings.Unbounded;                  use Ada.Strings.Unbounded;
with Ada.Text_IO;                            use Ada.Text_IO;

with GNAT.Directory_Operations; use GNAT, GNAT.Directory_Operations;
with GNAT.Dynamic_HTables;
with GNAT.OS_Lib;               use GNAT.OS_Lib;

with Gpr_Build_Util;               use Gpr_Build_Util;
with GPR.Compilation;              use GPR.Compilation;

with GPR.Compilation.Process;      use GPR.Compilation.Process;
with GPR.Compilation.Slave;
with GPR.Debug;
with GPR.Env;
with GPR.Jobserver;                use GPR.Jobserver;
with GPR.Names;                    use GPR.Names;
with GPR.Opt;                      use GPR.Opt;
with GPR.Snames;                   use GPR.Snames;
with GPR.Tempdir;
with GPR.Util;                     use GPR.Util;

package body Gprbuild.Compile is

   procedure Add_Compilation_Switches (Source : Source_Id);
   --  Add to the compilation option, the switches declared in
   --  Compiler'Switches(<source file name>), if it is defined, otherwise in
   --  Compiler'Default_Switches (<language name>), if it is defined.

   procedure Await_Compile
     (Source : out Queue.Source_Info;
      OK     : out Boolean;
      Slave  : out Unbounded_String);
   --  Wait for the end of a compilation and indicate that the object directory
   --  is free.

   procedure Compilation_Phase
     (Main_Project : Project_Id;
      Project_Tree : Project_Tree_Ref);

   procedure Recursive_Import (Project : Project_Id);
   --  Add to table Imports the projects imported by Project, recursively

   function Project_Extends
     (Extending : Project_Id;
      Extended  : Project_Id) return Boolean;
   --  Returns True if Extending is Extended or is extending Extended directly
   --  or indirectly.

   function Directly_Imports
     (Project  : Project_Id;
      Imported : Project_Id) return Boolean;
   --  Returns True if Project directly withs Imported or a project extending
   --  Imported.

   procedure Create_Config_File
     (For_Project  : Project_Id;
      Config       : Language_Config;
      Language     : Name_Id);
   --  Create a new config file

   function Config_File_For
     (Project        : Project_Id;
      Package_Name   : Name_Id;
      Attribute_Name : Name_Id;
      Language       : Name_Id) return Path_Information;
   --  Returns the name of a config file. Returns No_Name if there is no
   --  config file.

   procedure Create_Object_Path_File
     (Project : Project_Id; Shared : Shared_Project_Tree_Data_Access);
   --  Create a temporary file that contains the list of object directories
   --  in the correct order.

   procedure Print_Compilation_Outputs
     (For_Source : Source_Id;
      Always     : Boolean := False);
   --  In complete output mode, or when Always is True, put the outputs from
   --  last compilation to standard output and/or standard error.

   function "<" (Left, Right : Source_Id) return Boolean
     is (Left.File < Right.File);

   package Bad_Compilations_Set is new
     Containers.Indefinite_Ordered_Maps (Source_Id, String);

   Bad_Compilations : Bad_Compilations_Set.Map;
   --  Records bad compilation with the given slave name if any

   Outstanding_Compiles : Natural := 0;
   --  The number of compilation jobs currently spawned

   Slave_Initialized    : Boolean := False;
   --  Record wether the remote compilation slaves have been initialized when
   --  running in distributed mode.

   type Process_Purpose is (Compilation, Dependency);
   --  A type to distinguish between compilation jobs and dependency file
   --  building jobs.

   type Process_Data is record
      Process        : GPR.Compilation.Id  :=
                         GPR.Compilation.Invalid_Process;
      Source         : Queue.Source_Info  := Queue.No_Source_Info;
      Source_Project : Project_Id         := null;
      Mapping_File   : Path_Name_Type     := No_Path;
      Purpose        : Process_Purpose    := Compilation;
      Options        : String_Vectors.Vector;
   end record;
   --  Data recorded for each spawned jobs, compilation of dependency file
   --  building.

   No_Process_Data : constant Process_Data :=
                       (Process        => GPR.Compilation.Invalid_Process,
                        Source         => Queue.No_Source_Info,
                        Source_Project => null,
                        Mapping_File   => No_Path,
                        Purpose        => Compilation,
                        Options        => String_Vectors.Empty_Vector);

   package Compilation_Htable is new GNAT.HTable.Simple_HTable
     (Header_Num => GPR.Compilation.Process.Header_Num,
      Element    => Process_Data,
      No_Element => No_Process_Data,
      Key        => GPR.Compilation.Id,
      Hash       => Hash,
      Equal      => GPR.Compilation."=");
   --  Hash table to keep data for all spawned jobs

   package Naming_Data_Vectors is new Ada.Containers.Vectors
     (Positive, Lang_Naming_Data);

   Naming_Datas : Naming_Data_Vectors.Vector;
   --  Naming data when creating config files

   package Imports is new GNAT.HTable.Simple_HTable
     (Header_Num => GPR.Header_Num,
      Element    => Boolean,
      No_Element => False,
      Key        => Project_Id,
      Hash       => Hash,
      Equal      => "=");
   --  When --direct-import-only is used, contains the project ids a non Ada
   --  source is allowed to import source from.

   Included_Sources : Source_Vectors.Vector;

   Subunits         : String_Vectors.Vector;
   --  A table to store the subunit names when switch --no-split-units is used

   ------------------------------
   -- Add_Compilation_Switches --
   ------------------------------

   Inner_Cargs : constant String := "-inner-cargs";
   --  When the --compiler-pkg-subst switch is given, this is used to pass
   --  switches from "package Compiler" to the ASIS tool and thence through to
   --  the actual compiler.

   procedure Add_Compilation_Switches (Source : Source_Id) is
      procedure Process_One_Package (Pkg_Name : Name_Id);
      --  Get the switches for the named package

      -------------------------
      -- Process_One_Package --
      -------------------------

      procedure Process_One_Package (Pkg_Name : Name_Id) is
         Options : Variable_Value;
         Ignored : Boolean;
      begin
         Get_Switches
           (Source, Pkg_Name, Project_Tree, Options, Ignored);

         if Options /= Nil_Variable_Value then
            declare
               List            : String_List_Id := Options.Values;
               Element         : String_Element;
            begin
               while List /= Nil_String loop
                  Element := Project_Tree.Shared.String_Elements.Table (List);

                  --  Ignore empty options

                  if Element.Value /= Empty_String then
                     Add_Option_Internal_Codepeer
                       (Value       => Get_Option (Element.Value),
                        To          => Compilation_Options,
                        Display     => True);
                  end if;

                  List := Element.Next;
               end loop;
            end;
         end if;
      end Process_One_Package;

   begin
      --  If the --compiler-pkg-subst switch was given, get switches from the
      --  relevant package (e.g. "package Pretty_Printer").

      if Compiler_Pkg_Subst /= No_Name then
         Process_One_Package (Compiler_Pkg_Subst);
         Add_Option_Internal_Codepeer
           (Value       => Inner_Cargs,
            To          => Compilation_Options,
            Display     => True);
      end if;

      --  Always get switches from "package Compiler". If the
      --  --compiler-pkg-subst switch was given, these are preceded by
      --  -inner-cargs (see above) to indicate that the ASIS tool should pass
      --  them along to gcc.

      Process_One_Package (Name_Compiler);
   end Add_Compilation_Switches;

   -------------------
   -- Await_Compile --
   -------------------

   procedure Await_Compile
     (Source : out Queue.Source_Info;
      OK     : out Boolean;
      Slave  : out Unbounded_String)
   is
      Process   : GPR.Compilation.Id;
      Comp_Data : Process_Data;
      Language  : Language_Ptr;
      Config    : Language_Config;

   begin
      loop
         Source := Queue.No_Source_Info;

         Wait_Result (Process, OK);

         if Process = GPR.Compilation.Invalid_Process then
            return;
         end if;

         Comp_Data := Compilation_Htable.Get (Process);

         if Comp_Data /= No_Process_Data then
            Source := Comp_Data.Source;

            Queue.Set_Obj_Dir_Free (Source.Id.Project.Object_Directory.Name);

            if Comp_Data.Purpose = Compilation then
               Print_Compilation_Outputs
                 (Source.Id, Always => not No_Complete_Output);

               if OK then
                  --  We created a new dependency file, so reset the attributes
                  --  of the old one.

                  Source.Id.Dep_TS := Unknown_Attributes;

                  if not Comp_Data.Options.Is_Empty
                    and then Source.Id.Switches_Path /= No_Path
                    and then Opt.Check_Switches
                  then
                     --  First, update the time stamp of the object file that
                     --  will be written in the switches file.

                     Source.Id.Object_TS := File_Stamp (Source.Id.Object_Path);
                     GPR.Util.Update_File_Stamp
                       (Source.Id.Object_Path, Source.Id.Object_TS);

                     --  Write the switches file, now that we have the updated
                     --  time stamp for the object file.

                     declare
                        File : Text_IO.File_Type;

                     begin
                        Create
                          (File,
                           Out_File,
                           Get_Name_String (Source.Id.Switches_Path));

                        Put_Line (File, String (Source.Id.Object_TS));

                        for Arg of Comp_Data.Options loop
                           Put_Line (File, Arg);
                        end loop;

                        Close (File);

                     exception
                        when others =>
                           Fail_Program
                             (Source.Tree,
                              "could not create switches file """ &
                              Get_Name_String (Source.Id.Switches_Path) & '"');
                     end;

                     --  For all languages other than Ada, update the time
                     --  stamp of the object file as it is written in the
                     --  global archive dependency file. For all languages,
                     --  update the time stamp of the object file if it is in
                     --  a library project.

                  elsif Source.Id.Language.Config.Dependency_Kind not in
                        ALI_Dependency
                    or else Source.Id.Project.Library
                  then
                     Source.Id.Object_TS := File_Stamp (Source.Id.Object_Path);
                     GPR.Util.Update_File_Stamp
                       (Source.Id.Object_Path, Source.Id.Object_TS);
                  end if;

               else
                  Set_Failed_Compilation_Status (Comp_Data.Source_Project);

                  Slave := To_Unbounded_String (Get_Slave_For (Process));
               end if;

               Language := Source.Id.Language;

               --  If there is a mapping file used, recycle it in the hash
               --  table of the language.

               if Comp_Data.Mapping_File /= No_Path
                 and then Language /= No_Language_Index
               then
                  Mapping_Files_Htable.Set
                    (T => Language.Mapping_Files,
                     K => Comp_Data.Mapping_File,
                     E => Comp_Data.Mapping_File);
               end if;

               Config := Language.Config;

               if OK
                 and then Config.Dependency_Kind = Makefile
                 and then Config.Compute_Dependency /= No_Name_List
               then
                  declare
                     Current_Dir : constant Dir_Name_Str := Get_Current_Dir;

                     List      : Name_List_Index :=
                                   Config.Compute_Dependency;
                     Nam       : Name_Node :=
                                   Source.Tree.Shared.Name_Lists.Table (List);
                     Exec_Name : constant String :=
                                   Get_Name_String (Nam.Name);
                     Exec_Path : OS_Lib.String_Access;
                  begin
                     Change_Dir
                       (Get_Name_String
                          (Source.Id.Project.Object_Directory.Display_Name));
                     Comp_Data.Mapping_File := No_Path;
                     Comp_Data.Purpose := Dependency;

                     --  ??? We search for it on the PATH for every file,
                     --  this is very inefficient
                     Exec_Path := Locate_Exec_On_Path (Exec_Name);

                     if Exec_Path = null then
                        Fail_Program
                          (Source.Tree,
                           "unable to find dependency builder " & Exec_Name);
                     end if;

                     List := Nam.Next;
                     Compilation_Options.Clear;

                     if List = No_Name_List then
                        Name_Len := 0;

                     else
                        loop
                           Nam := Source.Tree.Shared.Name_Lists.Table (List);
                           List := Nam.Next;

                           if List = No_Name_List then
                              Get_Name_String (Nam.Name);
                              exit;
                           end if;

                           Add_Option
                             (Nam.Name, Compilation_Options, Opt.Verbose_Mode);
                        end loop;
                     end if;

                     Get_Name_String_And_Append (Source.Id.Path.Display_Name);
                     Add_Option
                       (Name_Buffer (1 .. Name_Len),
                        Compilation_Options,
                        Opt.Verbose_Mode,
                        Simple_Name => not Opt.Verbose_Mode);

                     if not Opt.Quiet_Output then
                        if Opt.Verbose_Mode then
                           Put (Exec_Path.all);
                        else
                           Put (Exec_Name);
                        end if;

                        Put (" ");

                        for Option of Compilation_Options loop
                           if Option.Displayed then
                              Put (Option.Name);
                              Put (" ");
                           end if;
                        end loop;

                        New_Line;
                     end if;

                     Comp_Data.Process :=
                       Run
                         (Executable  => Exec_Path.all,
                          Options     => Options_List (Compilation_Options),
                          Project     => Comp_Data.Source_Project,
                          Obj_Name    => Get_Name_String (Source.Id.Object),
                          Output_File => Get_Name_String (Source.Id.Dep_Path),
                          Err_To_Out  => True,
                          Force_Local => True);

                     Compilation_Htable.Set (Comp_Data.Process, Comp_Data);

                     Free (Exec_Path);

                     Change_Dir (Current_Dir);
                  end;

               else
                  Outstanding_Compiles := Outstanding_Compiles - 1;

                  if Opt.Use_GNU_Make_Jobserver then
                     Jobserver.Unregister_Token_Id (Id => Process);
                  end if;

                  return;
               end if;

            elsif Comp_Data.Purpose = Dependency then
               Outstanding_Compiles := Outstanding_Compiles - 1;

               if Opt.Use_GNU_Make_Jobserver then
                  Jobserver.Unregister_Token_Id (Id => Process);
               end if;

               return;
            end if;
         end if;
      end loop;
   end Await_Compile;

   ---------------------
   -- Config_File_For --
   ---------------------

   function Config_File_For
     (Project        : Project_Id;
      Package_Name   : Name_Id;
      Attribute_Name : Name_Id;
      Language       : Name_Id) return Path_Information
   is
      function Normalize_Path
        (Path    : Path_Name_Type;
         Project : Project_Id) return String
      is
        (GNAT.OS_Lib.Normalize_Pathname
           (Name      => Get_Name_String (Path),
            Directory => Get_Name_String (Project.Directory.Display_Name)));
      --  Returns an normalized path for a config file

      Config_Package  : constant Package_Id :=
                          Value_Of
                            (Name        => Package_Name,
                             In_Packages => Project.Decl.Packages,
                             Shared      => Project_Tree.Shared);

      Config_Variable : Variable_Value :=
                          Value_Of
                            (Name                    => Language,
                             Attribute_Or_Array_Name => Attribute_Name,
                             In_Package              => Config_Package,
                             Shared                  => Project_Tree.Shared);

   begin
      --  Get the config pragma attribute when the language is Ada and the
      --  config file attribute is not declared.

      if Config_Variable = Nil_Variable_Value
        and then Config_Package /= No_Package
        and then Language = Name_Ada
      then
         Config_Variable :=
           Value_Of
             (Variable_Name =>
                (if Attribute_Name = Name_Global_Config_File
                 then Name_Global_Configuration_Pragmas
                 elsif Attribute_Name = Name_Local_Config_File
                 then Name_Local_Configuration_Pragmas
                 else raise Program_Error with
                   "Unexpected " & Get_Name_String (Attribute_Name)),
              In_Variables => Project_Tree.Shared.Packages.Table
                                 (Config_Package).Decl.Attributes,
              Shared       => Project_Tree.Shared);
      end if;

      if Config_Variable = Nil_Variable_Value
        or else Config_Variable.Value = Snames.The_Empty_String
      then
         return No_Path_Information;

      else
         declare
            Path : String :=
                     Normalize_Path
                       (Path_Name_Type (Config_Variable.Value),
                        Config_Variable.Project);
            Result : Path_Information;
         begin
            Result.Display_Name := Get_Path_Name_Id (Path);
            Canonical_Case_File_Name (Path);
            Result.Name := Get_Path_Name_Id (Path);

            return Result;
         end;
      end if;
   end Config_File_For;

   ------------------------
   -- Create_Config_File --
   ------------------------

   procedure Create_Config_File
     (For_Project : Project_Id;
      Config      : Language_Config;
      Language    : Name_Id)
   is

      File_Name : Path_Name_Type  := No_Path;
      File      : File_Descriptor := Invalid_FD;

      Source   : Source_Id;
      Iter     : Source_Iterator;

      procedure Check
        (Project : Project_Id;
         Tree    : Project_Tree_Ref;
         Dummy   : in out Boolean);
      --  Check the naming schemes of the different projects of the project
      --  tree. For each different naming scheme issue the pattern config
      --  declarations.

      procedure Check_Temp_File;
      --  Check if a temp file has been created. If not, create one

      procedure Copy_Config_File
        (Project        : Project_Id;
         Package_Name   : Name_Id;
         Attribute_Name : Name_Id;
         Language       : Name_Id);
      --  If a specified config file exists, copy it in the temporary config
      --  file.

      procedure Put_Line (File : File_Descriptor; S : String);
      --  Output procedure, analogous to normal Text_IO proc of same name

      -----------
      -- Check --
      -----------

      procedure Check
        (Project : Project_Id;
         Tree    : Project_Tree_Ref;
         Dummy   : in out Boolean)
      is
         pragma Unreferenced (Dummy, Tree);
         Lang_Id : Language_Ptr := Project.Languages;

         Current_Naming : Natural := 0;

         procedure Replace;

         -------------
         -- Replace --
         -------------

         procedure Replace is
            Cur : Positive := 1;

            procedure Substitute (N : File_Name_Type);
            procedure Substitute (Name : String);

            ----------------
            -- Substitute --
            ----------------

            procedure Substitute (N : File_Name_Type) is
            begin
               if N = No_File then
                  Cur := Cur + 1;

               else
                  Substitute (Get_Name_String (N));
               end if;
            end Substitute;

            procedure Substitute (Name : String) is
            begin
               Name_Buffer
                 (Cur + Name'Length .. Name_Len - 2 + Name'Length) :=
                 Name_Buffer (Cur + 2 .. Name_Len);
               Name_Buffer (Cur .. Cur + Name'Length - 1) := Name;
               Name_Len := Name_Len - 2 + Name'Length;
               Cur := Cur + Name'Length;
            end Substitute;

         begin
            while Cur < Name_Len loop
               if Name_Buffer (Cur) = '%' then
                  case Name_Buffer (Cur + 1) is
                     when 'b' =>
                        Substitute (Lang_Id.Config.Naming_Data.Body_Suffix);

                     when 's' =>
                        Substitute (Lang_Id.Config.Naming_Data.Spec_Suffix);

                     when 'd' =>
                        Substitute
                          (Lang_Id.Config.Naming_Data.Dot_Replacement);

                     when 'c' =>
                        Substitute
                          (Image (Lang_Id.Config.Naming_Data.Casing));

                     when '%' =>
                        Name_Buffer (Cur .. Name_Len - 1) :=
                          Name_Buffer (Cur + 1 .. Name_Len);
                        Name_Len := Name_Len - 1;
                        Cur := Cur + 1;

                     when others =>
                        Cur := Cur + 1;
                  end case;

               else
                  Cur := Cur + 1;
               end if;
            end loop;
         end Replace;

      begin
         if Current_Verbosity = High then
            Put ("Checking project file """);
            Put (Get_Name_String (Project.Name));
            Put (""".");
            New_Line;
         end if;

         while Lang_Id /= No_Language_Index loop
            exit when Lang_Id.Name = Language;
            Lang_Id := Lang_Id.Next;
         end loop;

         if Lang_Id /= No_Language_Index then
            Current_Naming :=
              Natural (Naming_Datas.Find_Index (Lang_Id.Config.Naming_Data));

            if Current_Naming = 0 then
               Naming_Datas.Append (Lang_Id.Config.Naming_Data);

               Check_Temp_File;

               if Lang_Id.Config.Config_Spec_Pattern /= No_Name then
                  Get_Name_String (Lang_Id.Config.Config_Spec_Pattern);
                  Replace;
                  Put_Line (File, Name_Buffer (1 .. Name_Len));
               end if;

               if Lang_Id.Config.Config_Body_Pattern /= No_Name then
                  Get_Name_String (Lang_Id.Config.Config_Body_Pattern);
                  Replace;
                  Put_Line (File, Name_Buffer (1 .. Name_Len));
               end if;
            end if;
         end if;
      end Check;

      ---------------------
      -- Check_Temp_File --
      ---------------------

      procedure Check_Temp_File is
      begin
         if File = Invalid_FD then
            Tempdir.Create_Temp_File (File, Name => File_Name);

            if File = Invalid_FD then
               Fail_Program
                 (Project_Tree,
                  "unable to create temporary configuration pragmas file");

            else
               Record_Temp_File (Project_Tree.Shared, File_Name);

               if Opt.Verbosity_Level > Opt.Low then
                  Put ("Creating temp file """);
                  Put (Get_Name_String (File_Name));
                  Put_Line ("""");
               end if;
            end if;
         end if;
      end Check_Temp_File;

      ----------------------
      -- Copy_Config_File --
      ----------------------

      procedure Copy_Config_File
        (Project        : Project_Id;
         Package_Name   : Name_Id;
         Attribute_Name : Name_Id;
         Language       : Name_Id)
      is
         Config_File_Path : constant Path_Name_Type :=
                              Config_File_For
                                (Project, Package_Name,
                                 Attribute_Name, Language).Display_Name;
         Config_File      : Text_IO.File_Type;
         Line             : String (1 .. 1_000);
         Last             : Natural;

      begin
         if Config_File_Path /= No_Path then
            begin
               Open (Config_File, In_File, Get_Name_String (Config_File_Path));

            exception
               when others =>
                  Fail_Program
                    (Project_Tree,
                     "unable to open config file "
                     & Get_Name_String (Config_File_Path));
            end;

            Check_Temp_File;

            while not End_Of_File (Config_File) loop
               Get_Line (Config_File, Line, Last);
               Put_Line (File, Line (1 .. Last));
            end loop;

            Close (Config_File);
         end if;
      end Copy_Config_File;

      --------------
      -- Put_Line --
      --------------

      procedure Put_Line (File : File_Descriptor; S : String) is
         S0   : String (1 .. S'Length + 1);
         Last : Natural;

      begin
         --  Add an ASCII.LF to the string. As this config file is supposed to
         --  be used only by the compiler, we don't care about the characters
         --  for the end of line. In fact we could have put a space, but
         --  it is more convenient to be able to read gnat.adc during
         --  development, for which the ASCII.LF is fine.

         S0 (1 .. S'Length) := S;
         S0 (S0'Last) := ASCII.LF;
         Last := Write (File, S0'Address, S0'Length);

         if Last /= S'Length + 1 then
            Fail_Program (Project_Tree, "Disk full");
         end if;

         if Current_Verbosity = High then
            Put_Line (S);
         end if;
      end Put_Line;

      procedure Check_All_Projects is
        new For_Every_Project_Imported (Boolean, Check);
      Dummy : Boolean := False;

      --  Start of processing for Create_Config_File

   begin
      --  Nothing to do if config has already been checked

      if For_Project.Config_Checked then
         return;
      end if;

      if Config.Config_File_Unique then
         --  Copy an eventual global config file

         Copy_Config_File
           (Main_Project, Name_Builder, Name_Global_Config_File, Language);

         --  Copy an eventual local config file

         Copy_Config_File
           (For_Project, Name_Compiler, Name_Local_Config_File, Language);
      end if;

      For_Project.Config_Checked := True;

      Naming_Datas.Clear;

      Check_All_Projects (For_Project, Project_Tree, Dummy);

      --  Visit all the units and issue the config declarations for those that
      --  need one.

      Iter := For_Each_Source (Project_Tree);

      loop
         Source := GPR.Element (Iter);
         exit when Source = No_Source;

         if Source.Language.Name = Language
           and then Source.Naming_Exception /= No
           and then Source.Unit /= No_Unit_Index
           and then not Source.Locally_Removed
           and then Source.Replaced_By = No_Source
         then
            Name_Len := 0;

            if Source.Kind = Spec then
               if Source.Index = 0 and then Config.Config_Spec /= No_Name then
                  Get_Name_String (Config.Config_Spec);

               elsif
                 Source.Index /= 0 and then Config.Config_Spec_Index /= No_Name
               then
                  Get_Name_String (Config.Config_Spec_Index);
               end if;

            else
               if Source.Index = 0 and then Config.Config_Body /= No_Name then
                  Get_Name_String (Config.Config_Body);

               elsif
                 Source.Index /= 0 and then Config.Config_Body_Index /= No_Name
               then
                  Get_Name_String (Config.Config_Body_Index);
               end if;
            end if;

            if Name_Len /= 0 then
               declare
                  Cur       : Positive := 1;
                  Unit      : constant String :=
                                Get_Name_String (Source.Unit.Name);
                  File_Name : constant String :=
                                Get_Name_String (Source.Display_File);

               begin
                  while Cur < Name_Len loop
                     if Name_Buffer (Cur) /= '%' then
                        Cur := Cur + 1;

                     else
                        case Name_Buffer (Cur + 1) is
                           when 'u' =>
                              Name_Buffer
                                (Cur + Unit'Length ..
                                   Name_Len - 2 + Unit'Length) :=
                                  Name_Buffer (Cur + 2 .. Name_Len);
                              Name_Buffer (Cur .. Cur + Unit'Length - 1) :=
                                Unit;
                              Cur := Cur + Unit'Length;
                              Name_Len := Name_Len - 2 + Unit'Length;

                           when 'f' =>
                              Name_Buffer
                                (Cur + File_Name'Length ..
                                   Name_Len - 2 + File_Name'Length) :=
                                  Name_Buffer (Cur + 2 .. Name_Len);
                              Name_Buffer
                                (Cur .. Cur + File_Name'Length - 1) :=
                                File_Name;
                              Cur := Cur + File_Name'Length;
                              Name_Len := Name_Len - 2 + File_Name'Length;

                           when 'i' =>
                              declare
                                 Index_String : constant String :=
                                   Source.Index'Img;

                              begin
                                 Name_Buffer
                                   (Cur + Index_String'Length ..
                                      Name_Len - 2 + Index_String'Length) :=
                                     Name_Buffer (Cur + 2 .. Name_Len);
                                 Name_Buffer
                                   (Cur .. Cur + Index_String'Length - 1) :=
                                   Index_String;
                                 Cur := Cur + Index_String'Length;
                                 Name_Len :=
                                   Name_Len - 2 + Index_String'Length;
                              end;

                           when '%' =>
                              Name_Buffer (Cur .. Name_Len - 1) :=
                                Name_Buffer (Cur + 1 .. Name_Len);
                              Cur := Cur + 1;
                              Name_Len := Name_Len - 1;

                           when others =>
                              Cur := Cur + 1;
                        end case;
                     end if;
                  end loop;

                  Put_Line (File, Name_Buffer (1 .. Name_Len));
               end;
            end if;
         end if;

         Next (Iter);
      end loop;

      if File /= Invalid_FD then
         Close (File);
         For_Project.Config_File_Name := File_Name;
      end if;
   end Create_Config_File;

   -----------------------------
   -- Create_Object_Path_File --
   -----------------------------

   procedure Create_Object_Path_File
     (Project : Project_Id; Shared : Shared_Project_Tree_Data_Access)
   is
      FD   : File_Descriptor;
      Name : Path_Name_Type;

      LF : constant String := (1 => ASCII.LF);

      procedure Add
        (Project : Project_Id;
         In_Tree : Project_Tree_Ref;
         Dummy   : in out Boolean);
      --  Add the object directory of a project to the file

      ---------
      -- Add --
      ---------

      procedure Add
        (Project : Project_Id;
         In_Tree : Project_Tree_Ref;
         Dummy   : in out Boolean)
      is
         pragma Unreferenced (In_Tree);

         Path : constant Path_Name_Type :=
                  Get_Object_Directory
                    (Project,
                     Including_Libraries => True,
                     Only_If_Ada         => False);

         Last : Natural;

         pragma Unreferenced (Last);

      begin
         if Path /= No_Path then
            Get_Name_String (Path);
            Last := Write (FD, Name_Buffer (1)'Address, Name_Len);
            Last := Write (FD, LF (1)'Address, 1);
         end if;

         Dummy := True;
      end Add;

      procedure For_All_Projects is
        new For_Every_Project_Imported (Boolean, Add);

      Status : Boolean := False;
      pragma Warnings (Off, Status);

   begin
      GPR.Env.Create_Temp_File (Shared, FD, Name, "object path file");
      Project.Object_Path_File := Name;

      For_All_Projects
        (Project, Project_Tree, Status, Include_Aggregated => True);

      Close (FD, Status);
   end Create_Object_Path_File;

   ----------------------
   -- Recursive_Import --
   ----------------------

   procedure Recursive_Import (Project : Project_Id) is
      Ext : constant Project_Id := Project.Extends;
      L   : Project_List := Project.Imported_Projects;

   begin
      if Ext /= No_Project
        and then not Imports.Get (Ext)
      then
         Imports.Set (Ext, True);
         Recursive_Import (Ext);
      end if;

      while L /= null loop
         if not Imports.Get (L.Project) then
            Imports.Set (L.Project, True);
            Recursive_Import (L.Project);
         end if;

         L := L.Next;
      end loop;
   end Recursive_Import;

   ----------------------
   -- Directly_Imports --
   ----------------------

   function Directly_Imports
     (Project  : Project_Id;
      Imported : Project_Id) return Boolean
   is
      L : Project_List := Project.Imported_Projects;
      P : Project_Id;

   begin
      while L /= null loop
         P := L.Project;
         while P /= No_Project loop
            if Imported = P then
               return True;
            end if;

            P := P.Extends;
         end loop;

         L := L.Next;
      end loop;

      return False;
   end Directly_Imports;

   -------------------------------
   -- Print_Compilation_Outputs --
   -------------------------------

   procedure Print_Compilation_Outputs
     (For_Source : Source_Id;
      Always     : Boolean := False)
   is
      procedure Display_Content (Stream : File_Type; File_Path : String);
      --  Display content of the given Filename

      ---------------------
      -- Display_Content --
      ---------------------

      procedure Display_Content (Stream : File_Type; File_Path : String) is
         File : Ada.Text_IO.File_Type;
         Line : String (1 .. 1_024);
         Last : Natural;
         Print_New_Line : Boolean := False;
      begin
         if OS_Lib.Is_Regular_File (File_Path) then
            Open (File, In_File, File_Path);

            while not End_Of_File (File) loop
               Get_Line (File, Line, Last);
               if Last = Line'Last then
                  Put (Stream, Line (1 .. Last));
                  Print_New_Line := True;
               else
                  Put_Line (Stream, Line (1 .. Last));
               end if;
            end loop;

            if Print_New_Line then
               Put_Line (Stream, "");
            end if;
            Close (File);
         end if;
      end Display_Content;

   begin
      if Complete_Output or else Always then
         declare
            Proj      : constant Project_Id :=
                          Ultimate_Extending_Project_Of (For_Source.Project);
            File_Path : constant String :=
                          Get_Name_String (Proj.Object_Directory.Name)
                          & Directory_Separator
                          & Get_Name_String (For_Source.File);
         begin
            Display_Content (Standard_Output, File_Path & ".stdout");
            Display_Content (Standard_Error, File_Path & ".stderr");
         end;
      end if;
   end Print_Compilation_Outputs;

   ---------
   -- Run --
   ---------

   procedure Run is

      procedure Do_Compile (Project : Project_Id; Tree : Project_Tree_Ref);

      ----------------
      -- Do_Compile --
      ----------------

      procedure Do_Compile (Project : Project_Id; Tree : Project_Tree_Ref) is
      begin
         if Builder_Data (Tree).Need_Compilation then
            Compilation_Phase (Project, Tree);

            if Total_Errors_Detected > 0
              or else not Bad_Compilations.Is_Empty
            then
               --  If switch -k or -jnn (with nn > 1), output a summary of the
               --  sources that could not be compiled.

               if (Opt.Keep_Going or else Get_Maximum_Processes > 1)
                 and then not Bad_Compilations.Is_Empty
                 and then not Opt.No_Exit_Message
               then
                  New_Line;

                  for Index in Bad_Compilations.Iterate loop
                     declare
                        Source : constant Source_Id :=
                                   Bad_Compilations_Set.Key (Index);
                        Slave  : constant String :=
                                   Bad_Compilations_Set.Element (Index);
                     begin
                        if Source /= No_Source then
                           Put ("   compilation of ");
                           Put (Get_Name_String (Source.Display_File));
                           Put (" failed");
                           if Slave /= "" then
                              Put (" on " & Slave);
                           end if;
                           New_Line;
                        end if;
                     end;
                  end loop;

                  New_Line;
               end if;

               if Exit_Code = Osint.E_Success then
                  Exit_Code := (if Bad_Compilations.Is_Empty then E_Fatal
                                else E_Subtool);
               end if;

               if Opt.Keep_Going and then Project.Qualifier = Aggregate then
                  Bad_Compilations.Clear;

               else
                  if Distributed_Mode and then Slave_Initialized then
                     GPR.Compilation.Slave.Unregister_Remote_Slaves;
                  end if;

                  Compilation_Phase_Failed
                    (Tree, Exit_Code, No_Message => Opt.No_Exit_Message);
               end if;
            end if;
         end if;
      end Do_Compile;

      procedure Compile_All is new For_Project_And_Aggregated (Do_Compile);

   begin
      Compile_All (Main_Project, Project_Tree);

      --  Unregister the slaves and get back compiled object code. This is a
      --  nop if no compilation has been done.

      GPR.Compilation.Slave.Unregister_Remote_Slaves;
   end Run;

   -----------------------
   -- Compilation_Phase --
   -----------------------

   procedure Compilation_Phase
     (Main_Project : Project_Id;
      Project_Tree : Project_Tree_Ref)
   is
      type Local_Project_Data is record
         Include_Language       : Language_Ptr := No_Language_Index;
         --  Prepared arguments for "include" parameters (-I or include file).
         --  These are specific to each language and project.

         Include_Path_File      : Path_Name_Type;
         --  The path name of the source search directory file

         Imported_Dirs_Switches : Argument_List_Access;
         --  List of the source search switches (-I<source dir>) to be used
         --  when compiling.

         Include_Path           : OS_Lib.String_Access := null;
         --  The search source path for the project. Used as the value for an
         --  environment variable, specified by attribute Include_Path
         --  (<langu>). The names of the environment variables are in component
         --  Include_Path of the records Language_Config.

         Include_Switches_Spec_File : Path_Name_Type;

      end record;
      --  project-specific data required for this procedure. These are not
      --  stored in the Project_Data record so that projects kept in memory do
      --  not have to allocate space for these temporary data

      No_Local_Project_Data : constant Local_Project_Data :=
                             (Include_Language           => No_Language_Index,
                              Include_Path               => null,
                              Imported_Dirs_Switches     => null,
                              Include_Path_File          => No_Path,
                              Include_Switches_Spec_File => No_Path);

      package Local_Projects_HT is new Dynamic_HTables.Simple_HTable
        (Header_Num => GPR.Header_Num,
         Element    => Local_Project_Data,
         No_Element => No_Local_Project_Data,
         Key        => Project_Id,
         Hash       => GPR.Hash,
         Equal      => "=");

      Local_Projects : Local_Projects_HT.Instance;

      Keep_Dep_File : Boolean := False;
      --  We need to keep dependency file in some error cases for diagnostic

      The_Config_Paths : Config_Paths
                           (1 .. 2 + Natural (Cmd_Line_Adc_Files.Length));
      --  Paths of eventual global and local configuration pragmas files
      --  and files from -gnatec= command line parameters.

      Last_Config_Path : Natural := 0;

      procedure Add_Config_File_Switch
        (Config    : Language_Config;
         Path_Name : Path_Name_Type);

      procedure Record_ALI_For
        (Source_Identity : Queue.Source_Info;
         The_ALI         : ALI.ALI_Id := ALI.No_ALI_Id);
      --  Record the Id of an ALI file in Good_ALI table.
      --  The_ALI can contain the pre-parsed ali file, to save time.
      --  Tree is the tree to which Source_Identity belongs

      function Phase_2_Makefile (Src_Data : Queue.Source_Info) return Boolean;
      function Phase_2_ALI (Src_Data : Queue.Source_Info) return Boolean;
      --  Process Wait_For_Available_Slot depending on Src_Data.Dependency type
      --  This returns whether the compilation is considered as successful or
      --  not.

      procedure Set_Options_For_File (Id : Source_Id);
      --  Prepare the compiler options to use when building Id

      procedure Process_Project_Phase_1 (Source : Queue.Source_Info);
      --  If some compilation is needed for this project, perform it

      function Must_Exit_Because_Of_Error return Boolean;
      --  Return True if there were errors and the user decided to exit in such
      --  a case. This waits for any outstanding compilation.

      function Check_Switches_File (Id : Source_Id) return Boolean;
      --  Check in its switches file where Id was compiled with the same
      --  switches

      procedure Update_Object_Path
        (Id : Source_Id; Source_Project : Project_Id);
      --  Update, if necessary, the path of the object file, of the dependency
      --  file and of the switches file, in the case of the compilation of a
      --  source in an extended project, when the source is in a project being
      --  extended.

      procedure Add_Dependency_Options (Id : Source_Id);
      --  Add switches to the compilation command line to create the
      --  dependency file

      procedure Add_Object_File_Switches (Id : Source_Id);
      --  If there are switches to specify the name of the object file, add
      --  them.

      procedure Add_Object_Path_Switches (Id : Source_Id);
      --  If attribute Compiler'Object_Path_Switches has been specified, create
      --  the temporary object path file, if not already done, and add the
      --  switch(es) to the invocation of the compiler.

      procedure Get_Config_Paths
        (Id             : Source_Id;
         Source_Project : Project_Id);
      --  Find the config files for the source and put their paths in
      --  The_Config_Paths.

      procedure Add_Config_File_Switches
        (Id             : Source_Id;
         Source_Project : Project_Id);
      --  If Config_File_Switches is specified, check if a config file need to
      --  be specified. Return the path to the config file

      procedure Add_Trailing_Switches (Id : Source_Id);
      --  Add the trailing required switches, if any, so that they will be put
      --  in the switches file.

      procedure Add_Name_Of_Source_Switches (Id : Source_Id);
      --  Add the name of the source to be compiled

      function Add_Mapping_File_Switches
        (Source         : Queue.Source_Info;
         Source_Project : Project_Id) return Path_Name_Type;
      --  If the compiler supports mapping files, add the necessary switch.
      --  Returns the name of the mapping file to use (or No_File)

      procedure Add_Multi_Unit_Switches (Id : Source_Id);
      --  Add, if needed, the required switches to compile a multi-unit source
      --  file.

      procedure Spawn_Compiler_And_Register
        (Source                 : Queue.Source_Info;
         Source_Project         : Project_Id;
         Compiler_Path          : String;
         Mapping_File_Path      : Path_Name_Type;
         Last_Switches_For_File : Integer);
      --  Spawn the compiler with the arguments currently set in
      --  Compiler_Options. It registers the process we just spawned, so that
      --  we start monitoring it.
      --  This also displays on the output the command we are spawning.
      --  Last_Switches_For_File is the index in Compilation_Options of the
      --  last switch that should be written to the switches file. All
      --  following switches are not output in that file.

      function Get_Compatible_Languages (Lang : Language_Ptr) return Name_Ids;
      --  Return the list of languages that Id could potentially include (for
      --  instance "C" if Id is a "C++" file. This also includes Id's own
      --  language.

      procedure Prepare_Imported_Dirs_Switches
        (Data    : out Local_Project_Data;
         Project : Project_Id;
         Lang    : Language_Ptr);
      --  Add the switches for include directories to the command line (these
      --  are the "-I" switches in the case of C for instance).

      procedure Prepare_Include_Path_File
        (Data    : out Local_Project_Data;
         Project : Project_Id;
         Lang    : Language_Ptr);
      --  Create a file to pass the include directories to the compiler

      procedure Start_Compile_If_Possible;
      --  Checks if there is more work that we can do (ie the Queue is non
      --  empty). If there is, do it only if we have not yet used up all the
      --  available processes.

      procedure Wait_For_Available_Slot;
      --  Check if we should wait for a compilation to finish. This is the case
      --  if all the available processes are busy compiling sources or there is
      --  nothing else to do (that is the Q is empty and there are outstanding
      --  compilations).

      procedure Set_Env_For_Include_Dirs
        (Id : Source_Id;  Source_Project : Project_Id);
      --  Set environment variables or switches to pass the include directories
      --  to the compiler

      procedure Check_Interface_And_Indirect_Imports
        (The_ALI      : ALI.ALI_Id;
         Src_Data     : Queue.Source_Info;
         Success      : in out Boolean);
      --  From the given ALI data and the associated source Src_Data, checks
      --  the withed units for the following error cases:
      --    - The unit is not in the interface of the source's project
      --    - The unit is from an indirect import and the --no-indirect-import
      --      flag is set.
      --  Success is set to False if those occur.

      ------------------------------------------
      -- Check_Interface_And_Indirect_Imports --
      ------------------------------------------

      procedure Check_Interface_And_Indirect_Imports
        (The_ALI      : ALI.ALI_Id;
         Src_Data     : Queue.Source_Info;
         Success      : in out Boolean)
      is
         Sfile    : File_Name_Type;
         Afile    : File_Name_Type;
         Source_2 : Source_Id;

      begin
         for J in ALI.ALIs.Table (The_ALI).First_Unit ..
           ALI.ALIs.Table (The_ALI).Last_Unit
         loop
            for K in ALI.Units.Table (J).First_With ..
              ALI.Units.Table (J).Last_With
            loop
               if not
                 ALI.Withs.Table (K).Implicit_With_From_Instantiation
               then
                  Sfile := ALI.Withs.Table (K).Sfile;

                  --  Skip generics

                  if Sfile /= No_File then

                     --  Look for this source

                     Afile := ALI.Withs.Table (K).Afile;
                     Source_2 := Source_Files_Htable.Get
                       (Src_Data.Tree.Source_Files_HT, Sfile);

                     while Source_2 /= No_Source loop
                        if Is_Compilable (Source_2)
                          and then  Source_2.Dep_Name = Afile
                        then
                           case Source_2.Kind is
                              when Spec => null;

                              when Impl =>
                                 if Is_Subunit (Source_2) then
                                    Source_2 := No_Source;
                                 end if;

                              when Sep =>
                                 Source_2 := No_Source;
                           end case;

                           exit;
                        end if;

                        Source_2 := Source_2.Next_With_File_Name;
                     end loop;

                     --  If it is the source of a project that is not the
                     --  project of the source just compiled, check if it
                     --  is allowed to be imported.

                     if Source_2 /= No_Source then
                        if not Project_Extends
                          (Src_Data.Id.Project, Source_2.Project)
                          and then
                            not Project_Extends
                              (Source_2.Project, Src_Data.Id.Project)
                        then
                           if not Indirect_Imports
                             and then not Directly_Imports
                               (Src_Data.Id.Project, Source_2.Project)
                           then
                              --  It is in a project that is not directly
                              --  imported. Report an error and
                              --  invalidate the compilation.

                              Put ("Unit """);
                              Put (Get_Name_String (Src_Data.Id.Unit.Name));
                              Put (""" cannot import unit """);
                              Put (Get_Name_String (Source_2.Unit.Name));
                              Put_Line (""":");

                              Put ("  """);
                              Put
                                (Get_Name_String
                                   (Src_Data.Id.Project.Display_Name));
                              Put (""" does not directly import project """);
                              Put
                                (Get_Name_String
                                   (Source_2.Project.Display_Name));
                              Put_Line ("""");

                              Exit_Code := E_General;
                              Success   := False;

                           elsif not Source_2.In_Interfaces then
                              --  It is not an interface of its project.
                              --  Report an error and invalidate the
                              --  compilation.

                              Put ("Unit """);
                              Put
                                (Get_Name_String
                                   (Src_Data.Id.Unit.Name));
                              Put (""" cannot import unit """);
                              Put
                                (Get_Name_String (Source_2.Unit.Name));
                              Put_Line (""":");

                              Put
                                ("  it is not part of the "
                                 & "interfaces of its project """);
                              Put
                                (Get_Name_String
                                   (Source_2.Project.Display_Name));
                              Put_Line ("""");

                              Success := False;
                           end if;
                        end if;
                     end if;
                  end if;
               end if;
            end loop;
         end loop;
      end Check_Interface_And_Indirect_Imports;

      ----------------------------
      -- Add_Config_File_Switch --
      ----------------------------

      procedure Add_Config_File_Switch
        (Config    : Language_Config;
         Path_Name : Path_Name_Type)
      is
         List : Name_List_Index := Config.Config_File_Switches;
         Nam  : Name_Node;

      begin
         while List /= No_Name_List loop
            Nam := Project_Tree.Shared.Name_Lists.Table (List);
            Get_Name_String (Nam.Name);

            if Nam.Next = No_Name_List then
               Get_Name_String_And_Append (Path_Name);
            end if;

            Add_Option
              (Name_Buffer (1 .. Name_Len),
               To      => Compilation_Options,
               Display => Opt.Verbose_Mode);

            List := Nam.Next;
         end loop;
      end Add_Config_File_Switch;

      --------------------
      -- Record_ALI_For --
      --------------------

      procedure Record_ALI_For
        (Source_Identity : Queue.Source_Info;
         The_ALI         : ALI.ALI_Id := ALI.No_ALI_Id)
      is
         Local_ALI : ALI.ALI_Id := The_ALI;
         Text      : Text_Buffer_Ptr;

      begin
         if The_ALI = ALI.No_ALI_Id then
            Text := Read_Library_Info_From_Full
              (File_Name_Type (Source_Identity.Id.Dep_Path),
               Source_Identity.Id.Dep_TS'Access);

            if Text /= null then
               --  Read the ALI file but read only the necessary lines

               Local_ALI :=
                 ALI.Scan_ALI
                   (File_Name_Type (Source_Identity.Id.Dep_Path),
                    Text,
                    Ignore_ED     => False,
                    Err           => True,
                    Read_Lines    => "W");
               Free (Text);
            end if;
         end if;

         if Local_ALI /= ALI.No_ALI_Id then
            Queue.Insert_Withed_Sources_For (Local_ALI, Source_Identity.Tree);

            ALI.Initialize_ALI;
            --  ALI.Util.Initialize_ALI_Source;
         end if;
      end Record_ALI_For;

      ----------------------
      -- Phase_2_Makefile --
      ----------------------

      function Phase_2_Makefile
        (Src_Data : Queue.Source_Info) return Boolean is

         Object_Path : GNAT.OS_Lib.String_Access;
         Dep_File    : Text_File;
         Start       : Natural;
         Finish      : Natural;
         Last_Obj    : Natural;
         Was         : Boolean := False;

         type Src_Record (F_Len : Natural) is record
            File : String (1 .. F_Len);
            TS   : Time_Stamp_Type;
         end record;

         package Src_Vectors is new Ada.Containers.Indefinite_Vectors
           (Positive, Src_Record);

         Srcs : Src_Vectors.Vector;

         Compilation_OK  : Boolean := True;
         Dep_File_OK     : Boolean := False;

         Dep_Path : constant String := Get_Name_String (Src_Data.Id.Dep_Path);

      begin
         Open (Dep_File, Dep_Path);

         if Is_Valid (Dep_File) then

            Big_Loop :
            loop
               Skip_Loop :
               while not End_Of_File (Dep_File) loop
                  Get_Line (Dep_File, Name_Buffer, Name_Len);

                  if Name_Len > 0 and then Name_Buffer (1) /= '#' then
                     --  Skip a first line that is an empty continuation line

                     for J in 1 .. Name_Len - 1 loop
                        exit Skip_Loop when Name_Buffer (J) /= ' ';
                     end loop;

                     exit Skip_Loop when Name_Buffer (Name_Len) /= '\';
                  end if;
               end loop Skip_Loop;

               Start  := 1;
               Finish := Index (Name_Buffer (1 .. Name_Len), ": ");

               exit Big_Loop when Finish = 0;

               Last_Obj := Finish;
               loop
                  Last_Obj := Last_Obj - 1;
                  exit when Last_Obj = Start
                    or else Name_Buffer (Last_Obj) /= ' ';
               end loop;

               while Start < Last_Obj
                 and then Name_Buffer (Start) = ' '
               loop
                  Start := Start + 1;
               end loop;

               Object_Path := new String'(Name_Buffer (Start .. Last_Obj));
               Dep_File_OK := True;

               Start := Finish + 2;

               --  Process each line

               Line_Loop : loop
                  declare
                     Line : constant String  := Name_Buffer (1 .. Name_Len);
                     Last : constant Natural := Name_Len;

                  begin
                     Name_Loop : loop

                        --  Find the beginning of the next source path
                        --  name.

                        while Start < Last and then Line (Start) = ' ' loop
                           Start := Start + 1;
                        end loop;

                        --  Go to next line when there is a
                        --  continuation character \ at the end of the
                        --  line.

                        exit Name_Loop when Start = Last
                          and then Line (Start) = '\';

                        --  We should not be at the end of the line,
                        --  without a continuation character \.

                        exit Line_Loop when Start = Last;

                        --  Look for the end of the source path name

                        Finish := Start;

                        while Finish < Last loop
                           if Line (Finish) = '\' then
                              --  On Windows, a '\' is part of the path name,
                              --  except when it is not the first character
                              --  followed by another '\' or by a space.
                              --  On other platforms, when we are getting a '\'
                              --  that is not the last character of the line,
                              --  the next character is part of the path name,
                              --  even if it is a space.

                              if On_Windows
                                and then Finish = Start
                                and then Line (Finish + 1) = '\'
                              then
                                 Finish := Finish + 2;

                                 if Finish > Last then
                                    Put ("file """);
                                    Put (Dep_Path);
                                    Put_Line (""" has wrong format");

                                    Keep_Dep_File  := True;
                                    Compilation_OK := False;
                                    exit Big_Loop;
                                 end if;

                              else
                                 Finish := Finish + 1;
                              end if;

                           else
                              --  A space that is not preceded by '\'
                              --  indicates the end of the path name.

                              exit when Line (Finish + 1) = ' ';
                              Finish := Finish + 1;
                           end if;
                        end loop;

                        --  Check this source

                        declare
                           Src_Name : constant String :=
                                        Line (Start .. Finish);
                           --  This is a filename encoded by GCC for use as a
                           --  Makefile dependency, with some characters
                           --  escaped for this specific purpose. We are about
                           --  to reuse it in a rewritten dependency file.
                           --  We used to Normalize the path name at this
                           --  point, and this turned out both wrong and
                           --  unnecessary.
                           --  It would be an error to apply Normalize_Pathname
                           --  on it because normalised it can be different
                           --  filename. For example on windows
                           --  c:\path\filename.c escaped became
                           --  c\:path\\filename.c. Normalize_Pathname would
                           --  not understand that it is drive letter at first
                           --  characters and prepend it with current
                           --  directory. We do not need filename to be
                           --  normalised in the GPR rewritten dependency file
                           --  because it is going to be normalised relatively
                           --  to the object directory at reading in
                           --  GPR.Util.Need_To_Compile.Process_Makefile_Deps.

                           Unescaped : constant String :=
                                         OS_Lib.Normalize_Pathname
                                           (Unescape (Src_Name),
                                            Directory => Dir_Name (Dep_Path),
                                            Case_Sensitive => False);
                           Source_2 : Source_Id;
                           Src_TS   : Time_Stamp_Type;

                        begin
                           Source_2 := Source_Paths_Htable.Get
                             (Src_Data.Tree.Source_Paths_HT,
                              Get_Path_Name_Id (Unescaped));
                           Src_TS := File_Stamp (Unescaped);

                           if Src_TS = Empty_Time_Stamp then
                              --  File from dependency list does not exist

                              Put ('"');
                              Put
                                (Get_Name_String
                                   (Src_Data.Id.Path.Display_Name));
                              Put_Line ("""");

                              Put (ASCII.HT & "depends on non-existent """);
                              Put (Unescaped);
                              Put_Line ("""");

                              Put (ASCII.HT & "noted in the """);
                              Put (Dep_Path);
                              Put_Line ("""");

                              Keep_Dep_File  := True;
                              Compilation_OK := False;
                           end if;

                           if Source_2 /= No_Source then
                              --  It is a source of a project

                              if not Project_Extends
                                (Src_Data.Id.Project, Source_2.Project)
                                and then
                                  not Project_Extends
                                    (Source_2.Project, Src_Data.Id.Project)
                              then
                                 --  It is not a source of the same project
                                 --  as the source just compiled. Check if
                                 --  it can be imported.

                                 if not Indirect_Imports then
                                    if Directly_Imports
                                      (Src_Data.Id.Project, Source_2.Project)
                                    then
                                       --  It is a source of a directly
                                       --  imported project. Record its
                                       --  project, for later processing.

                                       Imports.Set (Source_2.Project, True);

                                    else
                                       --  It is a source of a project that
                                       --  is not directly imported. Record
                                       --  the source for later processing.

                                       Included_Sources.Append (Source_2);
                                    end if;
                                 end if;

                                 if not Source_2.In_Interfaces
                                   and then not Source_2.Locally_Removed
                                 then
                                    --  It is not a source in the interfaces
                                    --  of its project. Report an error and
                                    --  invalidate the compilation.

                                    Put ('"');
                                    Put
                                      (Get_Name_String
                                         (Src_Data.Id.Path.Display_Name));
                                    Put (""" cannot import """);
                                    Put (Unescaped);
                                    Put_Line (""":");

                                    Put
                                      ("  it is not part of the "
                                       & "interfaces of its project """);
                                    Put
                                      (Get_Name_String
                                         (Source_2.Project.Display_Name));
                                    Put_Line ("""");

                                    Compilation_OK := False;
                                 end if;
                              end if;
                           end if;

                           Srcs.Append
                             (Src_Record'
                                (F_Len => Src_Name'Length,
                                 File  => Src_Name,
                                 TS    => Src_TS));
                        end;

                        exit Line_Loop when Finish = Last;

                        --  Go get the next source on the line

                        Start := Finish + 1;

                     end loop Name_Loop;
                  end;

                  --  If we are here, we had a continuation character
                  --  \ at the end of the line, so we continue with
                  --  the next line.

                  Get_Line (Dep_File, Name_Buffer, Name_Len);
                  Start  := 1;
                  Finish := 1;
               end loop Line_Loop;
            end loop Big_Loop;

            Close (Dep_File);

            if not Included_Sources.Is_Empty then
               --  Sources in project that are not directly imported
               --  have been found. Check if they may be imported by
               --  other allowed imported sources.

               declare
                  L : Project_List := Src_Data.Id.Project.Imported_Projects;

               begin
                  --  Put in hash table Imports the project trees
                  --  rooted at the projects that are already in
                  --  Imports.

                  while L /= null loop
                     if Imports.Get (L.Project) then
                        Recursive_Import (L.Project);
                     end if;

                     L := L.Next;
                  end loop;

                  --  For all the imported sources from project not
                  --  directly imported, check if their projects are
                  --  in table imports.

                  for Included of Included_Sources loop
                     if not Imports.Get (Included.Project) then
                        --  This source is either directly imported or
                        --  imported from another source that should not be
                        --  imported. Report an error and invalidate the
                        --  compilation.

                        Put ('"');
                        Put (Get_Name_String (Src_Data.Id.Path.Display_Name));
                        Put (""" cannot import """);
                        Put (Get_Name_String (Included.Path.Display_Name));
                        Put_Line (""":");

                        Put ("  """);
                        Put
                          (Get_Name_String (Src_Data.Id.Project.Display_Name));
                        Put (""" does not directly import project """);
                        Put (Get_Name_String (Included.Project.Display_Name));
                        Put_Line ("""");

                        Compilation_OK := False;
                     end if;
                  end loop;
               end;
            end if;
         end if;

         if Compilation_OK and Dep_File_OK then
            Create (Dep_File, Dep_Path);
            Put (Dep_File, Object_Path.all);
            Put (Dep_File, ": ");

            for Src of Srcs loop
               if Was then
                  Put_Line (Dep_File, " \");
               else
                  Was := True;
               end if;

               Put (Dep_File, Src.File);
               Put (Dep_File, " ");
               Put (Dep_File, String (Src.TS));
            end loop;
            Put_Line (Dep_File, "");

            Close (Dep_File);
         end if;

         Free (Object_Path);

         return Compilation_OK;
      end Phase_2_Makefile;

      -----------------
      -- Phase_2_ALI --
      -----------------

      function Phase_2_ALI (Src_Data : Queue.Source_Info) return Boolean is
         Compilation_OK : Boolean := True;
         Text           : Text_Buffer_Ptr :=
                            Read_Library_Info_From_Full
                              (File_Name_Type (Src_Data.Id.Dep_Path),
                               Src_Data.Id.Dep_TS'Access);
         The_ALI        : ALI.ALI_Id := ALI.No_ALI_Id;

         procedure Check_Source (Sfile : File_Name_Type);
         --  Check if source Sfile is in the same project file as the Src_Data
         --  source file. Invalidate the compilation if it is not.

         ------------------
         -- Check_Source --
         ------------------

         procedure Check_Source (Sfile : File_Name_Type) is
            Source_3 : constant Source_Id :=
                         Find_Source
                           (Src_Data.Tree, No_Project, Base_Name => Sfile);

         begin
            if Source_3 = No_Source then
               Put ("source ");
               Put (Get_Name_String (Sfile));
               Put_Line (" is not a source of a project");
               Compilation_OK := False;

            elsif Ultimate_Extending_Project_Of (Source_3.Project) /=
              Ultimate_Extending_Project_Of (Src_Data.Id.Project)
            then
               Put ("sources ");
               Put (Get_Name_String (Source_3.File));
               Put (" and ");
               Put (Get_Name_String (Src_Data.Id.File));
               Put (" belong to different projects: ");
               Put (Get_Name_String (Source_3.Project.Display_Name));
               Put (" and ");
               Put_Line (Get_Name_String (Src_Data.Id.Project.Display_Name));
               Compilation_OK := False;
            end if;
         end Check_Source;

      begin
         if Text /= null then
            --  Read the ALI file but read only the necessary lines

            The_ALI :=
              ALI.Scan_ALI
                (File_Name_Type (Src_Data.Id.Dep_Path),
                 Text,
                 Ignore_ED  => False,
                 Err        => True,
                 Read_Lines => "DW");

            if The_ALI /= ALI.No_ALI_Id then
               Check_Interface_And_Indirect_Imports
                 (The_ALI      => The_ALI,
                  Src_Data     => Src_Data,
                  Success      => Compilation_OK);

               if Opt.No_Split_Units then

                  --  Initialized the list of subunits with the unit name

                  Subunits.Clear;
                  Subunits.Append (Get_Name_String (Src_Data.Id.Unit.Name));

                  --  First check that the spec and the body are in the same
                  --  project.

                  for J in ALI.ALIs.Table (The_ALI).First_Unit ..
                    ALI.ALIs.Table (The_ALI).Last_Unit
                  loop
                     Check_Source (ALI.Units.Table (J).Sfile);
                  end loop;

                  --  Next, check the subunits, if any

                  declare
                     Subunit_Found : Boolean;
                     Already_Found : Boolean;
                     Last          : Positive;
                  begin
                     --  Loop until we don't find new subunits

                     loop
                        Subunit_Found := False;

                        for D in ALI.ALIs.Table (The_ALI).First_Sdep
                          .. ALI.ALIs.Table (The_ALI).Last_Sdep
                        loop
                           if ALI.Sdep.Table (D).Subunit_Name /= No_Name then
                              Get_Name_String
                                (ALI.Sdep.Table (D).Subunit_Name);

                              --  First check if we already found this subunit

                              Already_Found :=
                                Subunits.Contains
                                  (Name_Buffer (1 .. Name_Len));

                              if not Already_Found then
                                 --  Find the name of the parent

                                 Last := Name_Len - 1;
                                 while Last > 1
                                   and then Name_Buffer (Last + 1) /= '.'
                                 loop
                                    Last := Last - 1;
                                 end loop;

                                 if Subunits.Contains
                                   (Name_Buffer (1 .. Last))
                                 then
                                    --  It is a new subunit, add it o the
                                    --  list and check if it is in the right
                                    --  project.

                                    Subunits.Append
                                      (Name_Buffer (1 .. Name_Len));
                                    Subunit_Found := True;
                                    Check_Source (ALI.Sdep.Table (D).Sfile);
                                 end if;
                              end if;
                           end if;
                        end loop;

                        exit when not Subunit_Found;
                     end loop;
                  end;
               end if;

               if Compilation_OK
                 and then
                   (Builder_Data (Src_Data.Tree).Closure_Needed
                    or else Src_Data.Closure)
               then
                  Record_ALI_For (Src_Data, The_ALI);
               end if;
            end if;

            Free (Text);
         end if;

         return Compilation_OK;
      end Phase_2_ALI;

      --------------------------
      -- Set_Options_For_File --
      --------------------------

      procedure Set_Options_For_File (Id : Source_Id) is
         Config                   : Language_Config renames Id.Language.Config;
         Builder_Options_Instance : constant String_Vector_Access :=
                                      Builder_Compiling_Options_HTable.Get
                                        (Id.Language.Name);
         Comp_Opt                 : constant String_Vector_Access :=
                                      Compiling_Options_HTable.Get
                                        (Id.Language.Name);

         List    : Name_List_Index;
         Nam_Nod : Name_Node;
         First   : Boolean;
         Index   : Natural := 0;

      begin
         Compilation_Options.Clear;

         --  1a) The leading required switches

         List := Config.Compiler_Leading_Required_Switches;
         First := True;
         while List /= No_Name_List loop
            Nam_Nod := Project_Tree.Shared.Name_Lists.Table (List);

            if Nam_Nod.Name /= Empty_String then
               Add_Option_Internal_Codepeer
                 (Value   => Get_Name_String (Nam_Nod.Name),
                  To      => Compilation_Options,
                  Display => First or Opt.Verbose_Mode);
               First := False;
            end if;

            List := Nam_Nod.Next;
         end loop;

         --  1b) The switches in CodePeer mode

         if Opt.CodePeer_Mode then
            --  Replace -x ada with -x adascil

            for J in 1 .. Compilation_Options.Last_Index loop
               if Compilation_Options (J).Name = "-x" then
                  Compilation_Options.Replace_Element
                    (J + 1,
                     Option_Type'
                       (Name_Len    => 7,
                        Name        => "adascil",
                        Displayed   => True,
                        Simple_Name => False));
                  Index := J;
                  exit;
               end if;
            end loop;

            if Index = 0 then
               Add_Option
                 (Value       => "-x",
                  To          => Compilation_Options,
                  Display     => True,
                  Simple_Name => False);
               Add_Option
                 (Value       => "adascil",
                  To          => Compilation_Options,
                  Display     => True,
                  Simple_Name => False);
            end if;

            Add_Option
              (Value   => "-gnatcC",
               To      => Compilation_Options,
               Display => True);
         end if;

         --  2) The compilation switches specified in package Builder
         --  for all compilers, following "-cargs", if any.

         for Option of All_Language_Builder_Compiling_Options loop
            Add_Option_Internal_Codepeer
              (Value   => Option,
               To      => Compilation_Options,
               Display => True);
         end loop;

         --  3) The compilation switches specified in package Builder
         --  for the compiler of the language, following
         --  -cargs:<language>.

         if Builder_Options_Instance /= null then
            for Option of Builder_Options_Instance.all loop
               Add_Option_Internal_Codepeer
                 (Value   => Option,
                  To      => Compilation_Options,
                  Display => True);
            end loop;
         end if;

         --  4) Compiler'Switches(<source file name>), if it is
         --  defined, otherwise Compiler'Switches (<language name>),
         --  if defined.

         Add_Compilation_Switches (Id);

         --  5) The switches specified on the gprbuild command line
         --  for all compilers, following "-cargs", if any.

         for Option of All_Language_Compiling_Options loop
            Add_Option_Internal_Codepeer
              (Value   => Option,
               To      => Compilation_Options,
               Display => True);
         end loop;

         --  6) The switches specified on the gprbuild command line
         --  for the compiler of the language, following
         --  -cargs:<language>.

         if Comp_Opt /= null then
            for Opt of Comp_Opt.all loop
               Add_Option_Internal_Codepeer
                 (Value   => Opt,
                  To      => Compilation_Options,
                  Display => True);
            end loop;
         end if;

         --  7) The PIC option if it exists, for shared and "static-pic"
         --     libraries.

         if Id.Project.Library
           and then Id.Project.Library_Kind /= Static
         then
            List := Config.Compilation_PIC_Option;
            while List /= No_Name_List loop
               Nam_Nod := Project_Tree.Shared.Name_Lists.Table (List);
               Add_Option_Internal_Codepeer
                 (Value   => Get_Name_String (Nam_Nod.Name),
                  To      => Compilation_Options,
                  Display => True);
               List := Nam_Nod.Next;
            end loop;
         end if;
      end Set_Options_For_File;

      -------------------------
      -- Check_Switches_File --
      -------------------------

      function Check_Switches_File (Id : Source_Id) return Boolean is

         File : Text_IO.File_Type;

         File_Content     : String_Vectors.Vector;
         Expected_Content : String_Vectors.Vector;

         function Assert_Line (Current : String) return Boolean;
         --  Return False if Current is not the next line in the switches file

         -----------------
         -- Assert_Line --
         -----------------

         function Assert_Line (Current : String) return Boolean is
            Line : String (1 .. 1_000);
            Last : Natural;
         begin
            if End_Of_File (File) then
               if Opt.Verbosity_Level > Opt.Low then
                  Put_Line ("    -> switches file has fewer switches");
               end if;

               Close (File);
               return False;
            end if;

            Get_Line (File, Line, Last);

            if Line (1 .. Last) /= Current then
               if Opt.Verbosity_Level > Opt.Low then
                  Put_Line ("    -> switches file '"
                              & Get_Name_String (Id.Switches_Path)
                              & "' has different line");
                  Put_Line ("       " & Line (1 .. Last));
                  Put_Line ("       " & Current);
               end if;

               Close (File);
               return False;
            end if;
            return True;
         end Assert_Line;

         List    : Name_List_Index;
         Nam_Nod : Name_Node;

         use GPR.Debug;

      begin
         if Opt.Verbosity_Level > Opt.Low and then Debug.Debug_Flag_S then
            Expected_Content.Append (String (Id.Object_TS));

            for Opt of Compilation_Options loop
               Expected_Content.Append (Opt.Name);
            end loop;

            List := Id.Language.Config.Compiler_Trailing_Required_Switches;

            while List /= No_Name_List loop
               Nam_Nod := Project_Tree.Shared.Name_Lists.Table (List);
               Expected_Content.Append (Get_Name_String (Nam_Nod.Name));
               List := Nam_Nod.Next;
            end loop;
         end if;

         Open (File, In_File, Get_Name_String (Id.Switches_Path));

         if Opt.Verbosity_Level > Opt.Low and then Debug.Debug_Flag_S then
            declare
               Line : String (1 .. 1_000);
               Last : Natural;
            begin
               while not End_Of_File (File) loop
                  Get_Line (File, Line, Last);
                  File_Content.Append (Line (1 .. Last));
               end loop;
            end;
            Reset (File);
            Put_Line ("       expected .cswi file content:");
            for S of Expected_Content loop
               Put_Line ("          " & S);
            end loop;
            Put_Line ("       actual .cswi file content:");
            for S of File_Content loop
               Put_Line ("          " & S);
            end loop;
         end if;

         if not Assert_Line (String (Id.Object_TS)) then
            return True;
         end if;

         for Opt of Compilation_Options loop
            if not Assert_Line (Opt.Name) then
               return True;
            end if;
         end loop;

         List := Id.Language.Config.Compiler_Trailing_Required_Switches;

         while List /= No_Name_List loop
            Nam_Nod := Project_Tree.Shared.Name_Lists.Table (List);

            if not Assert_Line (Get_Name_String (Nam_Nod.Name)) then
               return True;
            end if;

            List := Nam_Nod.Next;
         end loop;

         if not End_Of_File (File) then
            if Opt.Verbosity_Level > Opt.Low then
               Put_Line ("    -> switches file has more switches");
            end if;

            Close (File);
            return True;
         end if;

         Close (File);
         return False;

      exception
         when others =>
            if Opt.Verbosity_Level > Opt.Low then
               Put_Line ("    -> no switches file");
            end if;
            return True;
      end Check_Switches_File;

      ------------------------
      -- Update_Object_Path --
      ------------------------

      procedure Update_Object_Path
        (Id : Source_Id; Source_Project : Project_Id) is
      begin
         Id.Object_Project := Source_Project;

         if Id.Object_Project /= Id.Project then
            if Id.Object /= No_File then
               Get_Name_String
                 (Id.Object_Project.Object_Directory.Display_Name);
               Get_Name_String_And_Append (Id.Object);
               Id.Object_Path := Name_Find;
            end if;

            if Id.Dep_Name /= No_File then
               Get_Name_String
                 (Id.Object_Project.Object_Directory.Display_Name);
               Get_Name_String_And_Append (Id.Dep_Name);
               Id.Dep_Path := Name_Find;
            end if;

            if Id.Switches /= No_File then
               Get_Name_String
                 (Id.Object_Project.Object_Directory.Display_Name);
               Get_Name_String_And_Append (Id.Switches);
               Id.Switches_Path := Name_Find;
            end if;
         end if;
      end Update_Object_Path;

      ----------------------------
      -- Add_Dependency_Options --
      ----------------------------

      procedure Add_Dependency_Options (Id : Source_Id) is
         List : Name_List_Index := Id.Language.Config.Dependency_Option;
         Node : Name_Node;
      begin
         if Id.Language.Config.Dependency_Kind /= None then
            while List /= No_Name_List loop
               Node := Project_Tree.Shared.Name_Lists.Table (List);
               List := Node.Next;

               if List = No_Name_List then
                  Add_Option
                    (Value   => Get_Name_String (Node.Name)
                     & Get_Name_String (Id.Dep_Name),
                     To      => Compilation_Options,
                     Display => Opt.Verbose_Mode);
               else
                  Add_Option
                    (Value   => Node.Name,
                     To      => Compilation_Options,
                     Display => Opt.Verbose_Mode);
               end if;
            end loop;
         end if;
      end Add_Dependency_Options;

      ------------------------------
      -- Add_Object_File_Switches --
      ------------------------------

      procedure Add_Object_File_Switches (Id : Source_Id) is
         List : Name_List_Index := Id.Language.Config.Object_File_Switches;
         Node : Name_Node;
      begin
         if List /= No_Name_List then
            loop
               Node := Project_Tree.Shared.Name_Lists.Table (List);
               exit when Node.Next = No_Name_List;

               Add_Option
                 (Node.Name,
                  To      => Compilation_Options,
                  Display => Opt.Verbose_Mode or else Id.Index /= 0);
               List := Node.Next;
            end loop;

            Get_Name_String (Node.Name);
            Get_Name_String_And_Append (Id.Object);

            Add_Option
              (Name_Buffer (1 .. Name_Len),
               To      => Compilation_Options,
               Display => Opt.Verbose_Mode or else Id.Index /= 0);

         --  Always specify object-file for a multi-unit source file

         elsif Id.Index /= 0 then
            Add_Option
              ("-o",
               To      => Compilation_Options,
               Display => True);
            Add_Option
              (Get_Name_String (Id.Object),
               To      => Compilation_Options,
               Display => True);
         end if;
      end Add_Object_File_Switches;

      ------------------------------
      -- Add_Object_Path_Switches --
      ------------------------------

      procedure Add_Object_Path_Switches (Id : Source_Id) is
         List : Name_List_Index := Id.Language.Config.Object_Path_Switches;
         Node : Name_Node;

      begin
         if List /= No_Name_List then
            if Id.Project.Object_Path_File = No_Path then
               Create_Object_Path_File (Id.Project, Project_Tree.Shared);
            end if;

            while List /= No_Name_List loop
               Node := Project_Tree.Shared.Name_Lists.Table (List);
               exit when Node.Next = No_Name_List;

               Add_Option
                 (Node.Name,
                  To      => Compilation_Options,
                  Display => Opt.Verbose_Mode);
               List := Node.Next;
            end loop;

            Get_Name_String (Node.Name);
            Get_Name_String_And_Append (Id.Project.Object_Path_File);

            Add_Option
              (Name_Buffer (1 .. Name_Len),
               To      => Compilation_Options,
               Display => Opt.Verbose_Mode);
         end if;
      end Add_Object_Path_Switches;

      ----------------------
      -- Get_Config_Paths --
      ----------------------

      procedure Get_Config_Paths
        (Id             : Source_Id;
         Source_Project : Project_Id)
      is
         Config : constant Language_Config := Id.Language.Config;

         procedure Add_Config_File (Project : Project_Id; Pkg, Attr : Name_Id);

         procedure Add_Config_File (Project : Project_Id; Pkg, Attr : Name_Id)
         is
            Config_File_Path : constant Path_Information :=
                                 Config_File_For
                                   (Project        => Project,
                                    Package_Name   => Pkg,
                                    Attribute_Name => Attr,
                                    Language       => Id.Language.Name);
         begin
            if Config_File_Path /= No_Path_Information
              and then not Cmd_Line_Adc_Files.Contains
                             (Name_Id (Config_File_Path.Name))
            then
               Last_Config_Path := Last_Config_Path + 1;
               The_Config_Paths (Last_Config_Path) := Config_File_Path;
            end if;
         end Add_Config_File;

      begin
         Last_Config_Path := 0;

         if Config.Config_File_Switches /= No_Name_List
           and then (Config.Config_Body /= No_Name
                     or else Config.Config_Body_Index /= No_Name
                     or else Config.Config_Body_Pattern /= No_Name
                     or else Config.Config_Spec /= No_Name
                     or else Config.Config_Spec_Index /= No_Name
                     or else Config.Config_Spec_Pattern /= No_Name)
           and then not Config.Config_File_Unique
         then
            Add_Config_File
              (Main_Project, Name_Builder, Name_Global_Config_File);
            Add_Config_File
              (Source_Project, Name_Compiler, Name_Local_Config_File);
         end if;

         for CF in Cmd_Line_Adc_Files.Iterate loop
            Last_Config_Path := Last_Config_Path + 1;
            The_Config_Paths (Last_Config_Path) :=
              (Name         => Path_Name_Type (Name_Id_Maps.Key (CF)),
               Display_Name => Path_Name_Type (Name_Id_Maps.Element (CF)));
         end loop;
      end Get_Config_Paths;

      ------------------------------
      -- Add_Config_File_Switches --
      ------------------------------

      procedure Add_Config_File_Switches
        (Id             : Source_Id;
         Source_Project : Project_Id)
      is
         Config           : constant Language_Config := Id.Language.Config;
         --  Config_File_Path : Path_Name_Type;
      begin
         if Config.Config_File_Switches /= No_Name_List
           and then (Config.Config_Body /= No_Name
                     or else Config.Config_Body_Index /= No_Name
                     or else Config.Config_Body_Pattern /= No_Name
                     or else Config.Config_Spec /= No_Name
                     or else Config.Config_Spec_Index /= No_Name
                     or else Config.Config_Spec_Pattern /= No_Name)
         then
            Create_Config_File
              (For_Project => Source_Project,
               Config      => Config,
               Language    => Id.Language.Name);

            if Source_Project.Config_File_Name /= No_Path then
               Add_Config_File_Switch
                 (Config    => Config,
                  Path_Name => Source_Project.Config_File_Name);
            end if;

            for J in 1 .. Last_Config_Path loop
               Add_Config_File_Switch
                 (Config    => Config,
                  Path_Name => The_Config_Paths (J).Display_Name);
            end loop;
         end if;
      end Add_Config_File_Switches;

      -------------------------------
      -- Add_Mapping_File_Switches --
      -------------------------------

      function Add_Mapping_File_Switches
        (Source         : Queue.Source_Info;
         Source_Project : Project_Id) return Path_Name_Type
      is
         List              : Name_List_Index :=
                               Source.Id.Language.Config.Mapping_File_Switches;
         Node              : Name_Node;
         Mapping_File_Path : Path_Name_Type;
      begin
         if List /= No_Name_List then
            --  Check if there is a temporary mapping file we can use

            Mapping_File_Path := Mapping_Files_Htable.Get_First
              (Source.Id.Language.Mapping_Files);

            if Mapping_File_Path /= No_Path then
               --  Reuse this temporary mapping file and remove its
               --  name from the HTable so that it is not reused
               --  before the compilation terminates.

               Mapping_Files_Htable.Remove
                 (Source.Id.Language.Mapping_Files, Mapping_File_Path);

            else
               --  Create a new temporary mapping file, as there are
               --  none that can be reused.

               GPR.Env.Create_Mapping_File
                 (Project  => Source_Project,
                  Language => Source.Id.Language.Name,
                  In_Tree  => Source.Tree,
                  Name     => Mapping_File_Path);
            end if;

            while List /= No_Name_List loop
               Node := Source.Tree.Shared.Name_Lists.Table (List);
               List := Node.Next;

               if List /= No_Name_List then
                  Add_Option
                    (Value   => Node.Name,
                     To      => Compilation_Options,
                     Display => Opt.Verbose_Mode);

               else
                  Get_Name_String (Node.Name);
                  Get_Name_String_And_Append (Mapping_File_Path);
                  Add_Option
                    (Name_Buffer (1 .. Name_Len),
                     To      => Compilation_Options,
                     Display => Opt.Verbose_Mode);
               end if;
            end loop;

            return Mapping_File_Path;

         else
            return No_Path;
         end if;
      end Add_Mapping_File_Switches;

      -----------------------------
      -- Add_Multi_Unit_Switches --
      -----------------------------

      procedure Add_Multi_Unit_Switches (Id : Source_Id) is
         List : Name_List_Index := Id.Language.Config.Multi_Unit_Switches;
      begin
         if Id.Index /= 0
           and then List /= No_Name_List
         then
            declare
               Index_Img : constant String := Id.Index'Img;
               Node      : Name_Node;

            begin
               loop
                  Node := Project_Tree.Shared.Name_Lists.Table (List);
                  exit when Node.Next = No_Name_List;

                  Add_Option
                    (Node.Name,
                     To      => Compilation_Options,
                     Display => True);
                  List := Node.Next;
               end loop;

               Get_Name_String (Node.Name);
               Add_Str_To_Name_Buffer (Index_Img (2 .. Index_Img'Last));
               Add_Option
                 (Name_Buffer (1 .. Name_Len),
                  To      => Compilation_Options,
                  Display => True);
            end;
         end if;
      end Add_Multi_Unit_Switches;

      ---------------------------
      -- Add_Trailing_Switches --
      ---------------------------

      procedure Add_Trailing_Switches (Id : Source_Id) is
         List : Name_List_Index :=
                  Id.Language.Config.Compiler_Trailing_Required_Switches;
         Node : Name_Node;
      begin
         while List /= No_Name_List loop
            Node := Project_Tree.Shared.Name_Lists.Table (List);
            Add_Option
              (Node.Name,
               To      => Compilation_Options,
               Display => Opt.Verbose_Mode);
            List := Node.Next;
         end loop;
      end Add_Trailing_Switches;

      ---------------------------------
      -- Add_Name_Of_Source_Switches --
      ---------------------------------

      procedure Add_Name_Of_Source_Switches (Id : Source_Id) is
         List : Name_List_Index := Id.Language.Config.Source_File_Switches;
         Node : Name_Node;
      begin
         --  Add any source file prefix

         if List /= No_Name_List then
            loop
               Node := Project_Tree.Shared.Name_Lists.Table (List);
               exit when Node.Next = No_Name_List;

               Add_Option
                 (Node.Name,
                  To      => Compilation_Options,
                  Display => Opt.Verbose_Mode or else Id.Index /= 0);
               List := Node.Next;
            end loop;
         end if;

         --  Then handle the source file

         Add_Option
           (Get_Name_String_Or_Null (Node.Name)
            & Get_Name_String (Id.Path.Display_Name),
            To          => Compilation_Options,
            Display     => True,
            Simple_Name => not Opt.Verbose_Mode);
      end Add_Name_Of_Source_Switches;

      ---------------------------------
      -- Spawn_Compiler_And_Register --
      ---------------------------------

      procedure Spawn_Compiler_And_Register
        (Source                 : Queue.Source_Info;
         Source_Project         : Project_Id;
         Compiler_Path          : String;
         Mapping_File_Path      : Path_Name_Type;
         Last_Switches_For_File : Integer)
      is

         procedure Add_Process
           (Process        : GPR.Compilation.Id;
            Source         : Queue.Source_Info;
            Source_Project : Project_Id;
            Mapping_File   : Path_Name_Type;
            Purpose        : Process_Purpose;
            Options        : String_Vectors.Vector);
         --  Add compilation process and indicate that the object directory is
         --  busy.

         procedure Escape_Options (Options : in out Options_Data);
         --  On all platforms, escapes the characters '\', ' ' and '"' with
         --  character '\' before them.

         -----------------
         -- Add_Process --
         -----------------

         procedure Add_Process
           (Process        : GPR.Compilation.Id;
            Source         : Queue.Source_Info;
            Source_Project : Project_Id;
            Mapping_File   : Path_Name_Type;
            Purpose        : Process_Purpose;
            Options        : String_Vectors.Vector) is
         begin
            Compilation_Htable.Set
              (Process,
               (Process, Source, Source_Project,
                Mapping_File, Purpose, Options));
            Outstanding_Compiles := Outstanding_Compiles + 1;

            Queue.Set_Obj_Dir_Busy (Source.Id.Project.Object_Directory.Name);
         end Add_Process;

         --------------------
         -- Escape_Options --
         --------------------

         procedure Escape_Options (Options : in out Options_Data)
         is
            Last : constant Natural := Options.Last_Index;
         begin
            for J in 1 .. Last loop
               declare
                  Opt  : constant String := Options (J).Name;
                  Nopt : constant String := Escape_Path (Opt);
               begin
                  if Nopt'Length > Opt'Length then
                     Options.Replace_Element
                       (J,
                        Option_Type'
                          (Name_Len    => Nopt'Length,
                           Name        => Nopt,
                           Displayed   => Options.Element (J).Displayed,
                           Simple_Name => Options.Element (J).Simple_Name));
                  end if;
               end;
            end loop;
         end Escape_Options;

         ------------------
         -- Get_Language --
         ------------------

         function Get_Language return String is
           (if Source.Id.Language /= null
            then Get_Name_String (Source.Id.Language.Name)
            else "");

         Process       : GPR.Compilation.Id;
         Response_File : Path_Name_Type := No_Path;

      --  Start of processing of Spawn_Compiler_And_Register

      begin

         if Opt.Use_GNU_Make_Jobserver
           and then not Preorder_Token
         then
            return;
         else
            if not Opt.Quiet_Output then
               Name_Len := 0;

               if Opt.Verbose_Mode then
                  Add_Str_To_Name_Buffer (Compiler_Path);

                  for Opt of Compilation_Options loop
                     Add_Str_To_Name_Buffer (" ");

                     if Opt.Simple_Name then
                        Add_Str_To_Name_Buffer (Base_Name (Opt.Name));

                     else
                        Add_Str_To_Name_Buffer (Opt.Name);
                     end if;
                  end loop;

                  Put_Line (Name_Buffer (1 .. Name_Len));

               else
                  Display
                    (Section  => GPR.Compile,
                     Command  =>
                       Get_Name_String (Source.Id.Language.Display_Name),
                     Argument => Get_Name_String (Source.Id.File));
               end if;
            end if;

            if Source_Project.Config.Max_Command_Line_Length > 0 and then
              Source.Id.Language.Config.Resp_File_Format = GCC_GNU
            then
               declare
                  Arg_Length : Natural := 0;
               begin
                  for Opt of Compilation_Options loop
                     Arg_Length :=
                       Arg_Length + 1 + Opt.Name'Length;
                  end loop;

                  if Arg_Length
                    > Source_Project.Config.Max_Command_Line_Length
                  then
                     declare
                        use GPR.Tempdir;
                        FD             : File_Descriptor;
                        Status         : Integer;
                        Closing_Status : Boolean;

                     begin
                        --  Escape the following characters in the options:
                        --  '\', ' ' and '"'.

                        Escape_Options (Compilation_Options);

                        Create_Temp_File (FD, Response_File);
                        Record_Temp_File
                          (Shared => Source.Tree.Shared,
                           Path   => Response_File);

                        Option_Loop : for Opt of Compilation_Options loop
                           Status :=
                             Write
                               (FD,
                                Opt.Name (1)'Address,
                                Opt.Name'Length);

                           if Status /= Opt.Name'Length
                           then
                              Put_Line
                                ("Could not write option """ &
                                   Opt.Name &
                                   """ in response file """ &
                                   Get_Name_String (Response_File) &
                                   """");
                              Response_File := No_Path;
                              exit Option_Loop;
                           end if;

                           Status := Write (FD, ASCII.LF'Address, 1);
                        end loop Option_Loop;

                        Close (FD, Closing_Status);

                        if not Closing_Status and then Response_File /= No_Path
                        then
                           Put_Line
                             ("Could not close response file """ &
                                Get_Name_String (Response_File) &
                                """");
                           Response_File := No_Path;
                        end if;
                     end;

                     if Opt.Verbosity_Level > Opt.Low and then
                       Response_File /= No_Path
                     then
                        Put_Line ("using a response file");
                     end if;
                  end if;
               end;
            end if;

            Process := Run
              (Compiler_Path,
               Options_List (Compilation_Options),
               Source_Project,
               Source        => Get_Name_String (Source.Id.File),
               Language      => Get_Language,
               Dep_Name      => (if Source.Id.Dep_Name = No_File
                                 then ""
                                 else Get_Name_String (Source.Id.Dep_Name)),
               Obj_Name      => (if Source.Id.Object = No_File
                                 then ""
                                 else Get_Name_String (Source.Id.Object)),
               Response_File => Response_File);

            if Last_Switches_For_File >= 0 then
               while Compilation_Options.Last_Index > Last_Switches_For_File
               loop
                  Compilation_Options.Delete_Last;
               end loop;

               Add_Trailing_Switches (Source.Id);
            end if;

            Add_Process
              (Process        => Process,
               Source         => Source,
               Source_Project => Source_Project,
               Mapping_File   => Mapping_File_Path,
               Purpose        => Compilation,
               Options        => Options_List (Compilation_Options));

            if Opt.Use_GNU_Make_Jobserver then
               Register_Token_Id (Id => Process);
            end if;
         end if;
      end Spawn_Compiler_And_Register;

      ------------------------------
      -- Get_Compatible_Languages --
      ------------------------------

      function Get_Compatible_Languages
        (Lang : Language_Ptr) return Name_Ids
      is
         NL        : Name_List_Index :=
                       Lang.Config.Include_Compatible_Languages;
         Languages : Name_Ids
           (1 .. 1 + Length (Project_Tree.Shared.Name_Lists, NL));
         Index     : Positive := 1;
      begin
         Languages (Index) := Lang.Name;

         while NL /= No_Name_List loop
            Index := Index + 1;
            Languages (Index) :=
              Project_Tree.Shared.Name_Lists.Table (NL).Name;
            NL := Project_Tree.Shared.Name_Lists.Table (NL).Next;
         end loop;

         return Languages;
      end Get_Compatible_Languages;

      -------------------------------
      -- Prepare_Include_Path_File --
      -------------------------------

      procedure Prepare_Include_Path_File
        (Data    : out Local_Project_Data;
         Project : Project_Id;
         Lang    : Language_Ptr)
      is
         FD     : File_Descriptor;
         Status : Boolean;
      begin
         Get_Directories
           (Project_Tree => Project_Tree,
            For_Project  => Project,
            Activity     => Compilation,
            Languages    => Get_Compatible_Languages (Lang));

         GPR.Env.Create_New_Path_File
           (Shared    => Project_Tree.Shared,
            Path_FD   => FD,
            Path_Name => Data.Include_Path_File);

         if FD = Invalid_FD then
            Fail_Program
              (Project_Tree, "could not create temporary path file");
         end if;

         for Index in 1 .. Directories.Last loop
            Get_Name_String (Directories.Table (Index));
            Name_Len := Name_Len + 1;
            Name_Buffer (Name_Len) := ASCII.LF;
            if Write (FD, Name_Buffer (1)'Address, Name_Len) /= Name_Len then
               Fail_Program
                 (Project_Tree,
                  "disk full when writing include path file");
            end if;
         end loop;

         Close (FD, Status);

         if not Status then
            Fail_Program
              (Project_Tree,
               "disk full when writing include path file");
         end if;
      end Prepare_Include_Path_File;

      ------------------------------------
      -- Prepare_Imported_Dirs_Switches --
      ------------------------------------

      procedure Prepare_Imported_Dirs_Switches
        (Data    : out Local_Project_Data;
         Project : Project_Id;
         Lang    : Language_Ptr)
      is
         Len       : constant Natural :=
                       Length
                         (Project_Tree.Shared.Name_Lists,
                          Lang.Config.Include_Option);
--           Host_Path : OS_Lib.String_Access;
         Last      : Natural := 0;
         List      : Name_List_Index;
         Nam       : Name_Node;
      begin
         Get_Directories
           (Project_Tree => Project_Tree,
            For_Project  => Project,
            Activity     => Compilation,
            Languages    => Get_Compatible_Languages (Lang));

         Free (Data.Imported_Dirs_Switches);
         Data.Imported_Dirs_Switches :=
           new String_List (1 .. Directories.Last * Len);

         for Index in 1 .. Directories.Last loop
            List := Lang.Config.Include_Option;
            while List /= No_Name_List loop
               Nam := Project_Tree.Shared.Name_Lists.Table (List);
               exit when Nam.Next = No_Name_List;
               Last := Last + 1;
               Data.Imported_Dirs_Switches (Last) :=
                 new String'(Get_Name_String (Nam.Name));
               List := Nam.Next;
            end loop;

            Get_Name_String (Directories.Table (Index));

            while Name_Len > 1
              and then (Name_Buffer (Name_Len) = Directory_Separator
                        or else Name_Buffer (Name_Len) = '/')
            loop
               Name_Len := Name_Len - 1;
            end loop;

            Last := Last + 1;

            --  Concatenate the last switch and the path in a single option

            Data.Imported_Dirs_Switches (Last) := new String'
              (Get_Name_String (Nam.Name) & Name_Buffer (1 .. Name_Len));
         end loop;
      end Prepare_Imported_Dirs_Switches;

      ------------------------------
      -- Set_Env_For_Include_Dirs --
      ------------------------------

      procedure Set_Env_For_Include_Dirs
        (Id : Source_Id; Source_Project : Project_Id)
      is
         Current_Project      : Project_Id   := No_Project;
         Current_Language_Ind : Language_Ptr := No_Language_Index;
         --  The project for which the include path environment has been set
         --  last, to avoid computing it several times.

         Data : Local_Project_Data :=
                  Local_Projects_HT.Get (Local_Projects, Id.Object_Project);
      begin
         --  Prepare (if not already done) the data for Project/Lang.
         --  All files for a given language are processed sequentially, before
         --  we switch to the next language, so we are only preparing once per
         --  language here.

         if Data.Include_Language /= Id.Language then
            Free (Data.Include_Path);
            Free (Data.Imported_Dirs_Switches);
            Data := No_Local_Project_Data;

            if Id.Language.Config.Include_Option /= No_Name_List then
               Prepare_Imported_Dirs_Switches
                 (Data, Id.Object_Project, Id.Language);

            elsif
              Id.Language.Config.Include_Switches_Via_Spec /= No_Name_List
            then
               declare
                  Include_Switches_Spec : File_Descriptor := Invalid_FD;
                  Switches_File_Name    : Path_Name_Type;
                  Switches_File         : File_Descriptor := Invalid_FD;
                  Status                : Boolean := False;

                  Compiler : OS_Lib.String_Access;
                  Switch   : OS_Lib.String_Access;

                  List : Name_List_Index :=
                          Id.Language.Config.Include_Switches_Via_Spec;
                  Elem : Name_Node;
               begin
                  Elem := Project_Tree.Shared.Name_Lists.Table (List);
                  Compiler := new String'(Get_Name_String (Elem.Name));
                  List := Elem.Next;
                  Elem := Project_Tree.Shared.Name_Lists.Table (List);
                  Switch := new String'(Get_Name_String (Elem.Name));

                  Get_Directories
                    (Project_Tree => Project_Tree,
                     For_Project  => Id.Object_Project,
                     Activity     => Compilation,
                     Languages    => Get_Compatible_Languages (Id.Language));

                  GPR.Env.Create_Temp_File
                    (Project_Tree.Shared,
                     Switches_File,
                     Switches_File_Name,
                     "include switches");

                  for Index in 1 .. Directories.Last loop
                     Set_Name_Buffer (Switch.all);
                     Add_Str_To_Name_Buffer
                       (Escape_Path
                          (Get_Name_String (Directories.Table (Index))));
                     Name_Len := Name_Len + 1;
                     Name_Buffer (Name_Len) := ASCII.LF;

                     if Write
                       (Switches_File, Name_Buffer (1)'Address, Name_Len)
                         /= Name_Len
                     then
                        Fail_Program
                          (Project_Tree,
                           "disk full when writing include switches file");
                     end if;
                  end loop;

                  Close (Switches_File, Status);

                  if not Status then
                     Fail_Program
                       (Project_Tree,
                        "disk full when writing include switches file");
                  end if;

                  GPR.Env.Create_Temp_File
                    (Project_Tree.Shared,
                     Include_Switches_Spec,
                     Data.Include_Switches_Spec_File,
                     "include switches spec");

                  Name_Len := 1;
                  Name_Buffer (1) := '*';
                  Add_Str_To_Name_Buffer (Compiler.all);
                  Name_Len := Name_Len + 1;
                  Name_Buffer (Name_Len) := ':';
                  Name_Len := Name_Len + 1;
                  Name_Buffer (Name_Len) := ASCII.LF;

                  if Write
                    (Include_Switches_Spec,
                     Name_Buffer (1)'Address, Name_Len) /= Name_Len
                  then
                     Fail_Program
                       (Project_Tree,
                        "disk full when writing include switches spec file");
                  end if;

                  Set_Name_Buffer ("+ @");
                  Add_Str_To_Name_Buffer
                    (Escape_Path (Get_Name_String (Switches_File_Name)));
                  Name_Len := Name_Len + 1;
                  Name_Buffer (Name_Len) := ASCII.LF;

                  if Write
                    (Include_Switches_Spec,
                     Name_Buffer (1)'Address, Name_Len) /= Name_Len
                  then
                     Fail_Program
                       (Project_Tree,
                        "disk full when writing include switches spec file");
                  end if;

                  Close (Include_Switches_Spec, Status);

                  if not Status then
                     Fail_Program
                       (Project_Tree,
                        "disk full when writing include switches spec file");
                  end if;

                  Free (Compiler);
                  Free (Switch);

                  declare
                     Path : constant String :=
                              Get_Name_String
                                (Data.Include_Switches_Spec_File);
                  begin
                     Data.Imported_Dirs_Switches :=
                       new String_List'
                         (1 => new String'("-specs=" & Path));
                  end;
               end;

            elsif Id.Language.Config.Include_Path_File /= No_Name then
               if Id.Language.Config.Mapping_File_Switches = No_Name_List
                 or else Opt.Use_Include_Path_File
               then
                  Prepare_Include_Path_File
                    (Data, Id.Object_Project, Id.Language);
               end if;

            elsif Id.Language.Config.Include_Path /= No_Name then
               Get_Directories
                 (Project_Tree => Project_Tree,
                  For_Project  => Id.Object_Project,
                  Activity     => Compilation,
                  Languages    => Get_Compatible_Languages (Id.Language));
               Data.Include_Path := Create_Path_From_Dirs;
            end if;

            Data.Include_Language := Id.Language;

            Local_Projects_HT.Set (Local_Projects, Id.Object_Project, Data);
         end if;

         --  Reset environment variables if they have changed

         if Id.Object_Project /= Current_Project
           or else Id.Language /= Current_Language_Ind
         then
            Current_Project      := Id.Object_Project;
            Current_Language_Ind := Id.Language;

            if Data.Include_Path_File /= No_Path then
               Setenv (Get_Name_String (Id.Language.Config.Include_Path_File),
                       Get_Name_String (Data.Include_Path_File));

            elsif Data.Include_Path /= null then
               GPR.Compilation.Process.Record_Environment
                 (Source_Project,
                  Id.Language.Name,
                  Get_Name_String (Id.Language.Config.Include_Path),
                  Data.Include_Path.all);

               if Opt.Verbosity_Level > Opt.Low then
                  Put
                    (Get_Name_String (Id.Language.Config.Include_Path));
                  Put (" = ");
                  Put_Line (Data.Include_Path.all);
               end if;
            end if;
         end if;

         --  But always set the switches

         if Data.Imported_Dirs_Switches /= null then
            for J in Data.Imported_Dirs_Switches'Range loop
               if Data.Imported_Dirs_Switches (J)'Length > 0 then
                  Add_Option
                    (Value   => Data.Imported_Dirs_Switches (J).all,
                     To      => Compilation_Options,
                     Display => Opt.Verbose_Mode);
               end if;
            end loop;
         end if;
      end Set_Env_For_Include_Dirs;

      -----------------------------
      -- Process_Project_Phase_1 --
      -----------------------------

      procedure Process_Project_Phase_1 (Source : Queue.Source_Info) is
         Id                     : constant Source_Id := Source.Id;
         Project_Tree           : constant Project_Tree_Ref := Source.Tree;
         Source_Project         : constant Project_Id :=
                                    Ultimate_Extending_Project_Of (Id.Project);
         Dummy                  : Boolean;
         Compilation_Needed     : Boolean := True;
         Last_Switches_For_File : Integer;
         Mapping_File           : Path_Name_Type;
         The_ALI                : ALI.ALI_Id;
         Compiler               : OS_Lib.String_Access;

      begin
         Get_Config_Paths (Id, Source_Project);

         if Always_Compile or else not Source_Project.Externally_Built then
            Need_To_Compile
              (Source         => Id,
               Tree           => Source.Tree,
               In_Project     => Source_Project,
               Conf_Paths     => The_Config_Paths (1 .. Last_Config_Path),
               Must_Compile   => Compilation_Needed,
               The_ALI        => The_ALI,
               Object_Check   => Object_Checked,
               Always_Compile => Always_Compile);

            if Total_Errors_Detected > 0 then
               Compilation_Phase_Failed
                 (Source.Tree, No_Message => Opt.No_Exit_Message);
            end if;

            if The_ALI /= ALI.No_ALI_Id then
               declare
                  Success : Boolean := True;
               begin
                  Check_Interface_And_Indirect_Imports
                    (The_ALI  => The_ALI,
                     Src_Data => Source,
                     Success  => Success);

                  if not Success then
                     Compilation_Phase_Failed
                       (Source.Tree,
                        (if Exit_Code = E_Success then E_Fatal else Exit_Code),
                        No_Message => Opt.No_Exit_Message);
                  end if;
               end;
            end if;

            if Compilation_Needed and then Opt.Keep_Going then
               --  When in Keep_Going mode first check that we did not already
               --  tried to compile this source as part of another import of
               --  the corresponding project file.

               if Bad_Compilations.Contains (Id) then
                  Compilation_Needed := False;
               end if;
            end if;

            if Compilation_Needed or else Opt.Check_Switches then
               Set_Options_For_File (Id);

               if Opt.Check_Switches and then not Compilation_Needed then
                  Compilation_Needed := Check_Switches_File (Id);
               end if;
            end if;

            if Compilation_Needed then
               --  If Distributed_Mode activated, parse Remote package to
               --  register and initialize the slaves.

               if Distributed_Mode and then not Slave_Initialized then
                  begin
                     GPR.Compilation.Slave.Register_Remote_Slaves
                       (Project_Tree, Main_Project);
                     Slave_Initialized := True;
                  exception
                     when E : Constraint_Error =>
                        Fail_Program (Project_Tree, Exception_Information (E));
                  end;
               end if;

               Update_Object_Path (Id, Source_Project);
               Change_To_Object_Directory
                 (Source_Project, Must_Be_Writable => True);

               --  Record the last recorded option index, to be able to
               --  write the switches file later.

               if Id.Language.Config.Object_Generated then
                  Last_Switches_For_File := Compilation_Options.Last_Index;
               else
                  Last_Switches_For_File := -1;
               end if;

               Add_Dependency_Options (Id);
               Set_Env_For_Include_Dirs (Id, Source_Project);
               Add_Config_File_Switches (Id, Source_Project);
               Mapping_File := Add_Mapping_File_Switches
                 (Source, Source_Project);
               Add_Trailing_Switches (Id);
               Add_Name_Of_Source_Switches (Id);
               Add_Object_File_Switches (Id);
               Add_Multi_Unit_Switches (Id);
               Add_Object_Path_Switches (Id);
               Compiler :=
                 Get_Compiler_Driver_Path (Source_Project, Id.Language);

               if Compiler /= null then
                  if Id.Switches_Path /= No_Path then
                     --  Need to remove .cswi file so that it doesn't get
                     --  reused in case of compilation failure.

                     OS_Lib.Delete_File
                       (Get_Name_String (Id.Switches_Path), Dummy);
                  end if;

                  Spawn_Compiler_And_Register
                    (Source                 => Source,
                     Source_Project         => Source_Project,
                     Compiler_Path          => Compiler.all,
                     Mapping_File_Path      => Mapping_File,
                     Last_Switches_For_File => Last_Switches_For_File);
               end if;

            else
               Print_Compilation_Outputs (Id);

               if Source.Closure
                 or else
                   (Builder_Data (Source.Tree).Closure_Needed
                    and then
                    Id.Language.Config.Dependency_Kind in ALI_Dependency)
               then
                  Record_ALI_For (Source, The_ALI);

               else
                  ALI.Initialize_ALI;
               end if;
            end if;
         end if;
      end Process_Project_Phase_1;

      --------------------------------
      -- Must_Exit_Because_Of_Error --
      --------------------------------

      function Must_Exit_Because_Of_Error return Boolean is
         Source_Identity : Queue.Source_Info;
         Compilation_OK  : Boolean;
         Slave           : Unbounded_String;

         Cur : Bad_Compilations_Set.Cursor;
         OK  : Boolean;
      begin
         if not Bad_Compilations.Is_Empty and then not Opt.Keep_Going then
            while Outstanding_Compiles > 0 loop
               Await_Compile (Source_Identity, Compilation_OK, Slave);

               if not Compilation_OK then
                  Bad_Compilations.Insert
                    (Source_Identity.Id, To_String (Slave), Cur, OK);
               end if;
            end loop;

            return True;
         end if;
         return False;
      end Must_Exit_Because_Of_Error;

      -------------------------------
      -- Start_Compile_If_Possible --
      -------------------------------

      procedure Start_Compile_If_Possible is
         Found  : Boolean;
         Source : Queue.Source_Info;
      begin
         if not Queue.Is_Empty
           and then (Opt.Use_GNU_Make_Jobserver
                     or else Outstanding_Compiles < Get_Maximum_Processes)
         then
            Queue.Get (Found, Source);

            if Found then
               Initialize_Source_Record (Source.Id);
               Process_Project_Phase_1 (Source);
            end if;

            if Opt.Use_GNU_Make_Jobserver
              and then Unavailable_Token
            then
               null;
            elsif Found then
               Queue.Next;
            end if;
         end if;
      end Start_Compile_If_Possible;

      -----------------------------
      -- Wait_For_Available_Slot --
      -----------------------------

      procedure Wait_For_Available_Slot is
         Source_Identity : Queue.Source_Info;
         Compilation_OK  : Boolean;
         No_Check        : Boolean;
         Slave           : Unbounded_String;
         use Queue;

         Cur : Bad_Compilations_Set.Cursor;
         OK  : Boolean;

         function No_Slot_Available return Boolean;

         -----------------------
         -- No_Slot_Available --
         -----------------------

         function No_Slot_Available return Boolean is
         begin
            if Opt.Use_GNU_Make_Jobserver then
               return (Unavailable_Token
                       and then Registered_Processes);
            else
               return (Outstanding_Compiles = Get_Maximum_Processes);
            end if;
         end No_Slot_Available;
      begin
         if No_Slot_Available
           or else (Queue.Is_Virtually_Empty and then Outstanding_Compiles > 0)
         then
            Await_Compile (Source_Identity, Compilation_OK, Slave);

            if Compilation_OK
              and then Source_Identity /= Queue.No_Source_Info
            then
               --  Check if dependencies are on sources in Interfaces and,
               --  when --direct-import-only is used, the imported sources
               --  come from directly withed projects.

               Imports.Reset;
               Included_Sources.Clear;

               case Source_Identity.Id.Language.Config.Dependency_Kind is
                  when None     => null;
                  when Makefile =>
                     Compilation_OK := Phase_2_Makefile (Source_Identity);
                  when ALI_Dependency =>
                     Compilation_OK := Phase_2_ALI (Source_Identity);
               end case;

               --  If the compilation was invalidated, delete the compilation
               --  artifacts.

               if not Compilation_OK then
                  if Source_Identity.Id.Dep_Path /= No_Path
                    and then not Keep_Dep_File
                  then
                     Delete_File
                       (Get_Name_String (Source_Identity.Id.Dep_Path),
                        No_Check);
                  end if;

                  if Source_Identity.Id.Object_Path /= No_Path then
                     Delete_File
                       (Get_Name_String (Source_Identity.Id.Object_Path),
                        No_Check);
                  end if;

                  if Source_Identity.Id.Switches_Path /= No_Path then
                     Delete_File
                       (Get_Name_String (Source_Identity.Id.Switches_Path),
                        No_Check);
                  end if;
               end if;
            end if;

            if not Compilation_OK then
               Bad_Compilations.Insert
                 (Source_Identity.Id, To_String (Slave), Cur, OK);
            end if;
         end if;
      end Wait_For_Available_Slot;

   --  Start of processing for Compilation_Phase

   begin
      Outstanding_Compiles := 0;

      --  Then process each files in the queue (new files might be added to
      --  the queue as a result).

      Compilation_Loop :
      while not Queue.Is_Empty or else Outstanding_Compiles > 0 loop
         exit Compilation_Loop when Must_Exit_Because_Of_Error;
         Start_Compile_If_Possible;
         Wait_For_Available_Slot;

         if Opt.Display_Compilation_Progress then
            Put_Line
              ("completed" &
               Queue.Processed'Img &
               " out of" &
               Queue.Size'Img &
               " (" &
               Trim
                 (Source => Int (((Queue.Processed) * 100) / Queue.Size)'Img,
                  Side => Ada.Strings.Left) &
               "%)...");
         end if;

      end loop Compilation_Loop;

      --  Release local memory

      declare
         Data : Local_Project_Data :=
                  Local_Projects_HT.Get_First (Local_Projects);
      begin
         while Data /= No_Local_Project_Data loop
            Free (Data.Include_Path);
            Free (Data.Imported_Dirs_Switches);
            Data := Local_Projects_HT.Get_Next (Local_Projects);
         end loop;

         Local_Projects_HT.Reset (Local_Projects);
      end;
   end Compilation_Phase;

   ---------------------
   -- Project_Extends --
   ---------------------

   function Project_Extends
     (Extending : Project_Id;
      Extended  : Project_Id) return Boolean
   is
      Current : Project_Id := Extending;
   begin
      loop
         if Current = No_Project then
            return False;

         elsif Current = Extended then
            return True;
         end if;

         Current := Current.Extends;
      end loop;
   end Project_Extends;

end Gprbuild.Compile;