libgpr_22.0.0_30e39dcc/src/gprbuild-post_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
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
------------------------------------------------------------------------------
--                                                                          --
--                             GPR TECHNOLOGY                               --
--                                                                          --
--                     Copyright (C) 2011-2021, 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.Calendar;
with Ada.Containers.Hashed_Maps;
with Ada.Containers.Ordered_Sets;
with Ada.Directories;
with Ada.Strings.Fixed;
with Ada.Text_IO;               use Ada, Ada.Text_IO;

with GNAT.Case_Util;            use GNAT.Case_Util;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
with GNAT.Dynamic_HTables;
with GNAT.MD5;                  use GNAT.MD5;
with GNAT.OS_Lib;               use GNAT.OS_Lib;

with Gpr_Build_Util; use Gpr_Build_Util;
with Gprexch;        use Gprexch;
with GPR.Env;
with GPR.Err;        use GPR.Err;
with GPR.Names;      use GPR.Names;
with GPR.Opt;
with GPR.Script;     use GPR.Script;
with GPR.Snames;     use GPR.Snames;
with GPR.Tempdir;
with GPR.Util;       use GPR.Util;

package body Gprbuild.Post_Compile is

   type Lang_Names is array (Positive range <>) of Language_Ptr;
   type Lang_Names_Ptr is access Lang_Names;

   Langs : Lang_Names_Ptr := new Lang_Names (1 .. 4);
   Last_Lang : Natural := 0;

   Libs_Are_Building : Name_Id_Set.Set;
   --  Libraries currently being built

   package FNHS is new GNAT.Dynamic_HTables.Simple_HTable
     (Header_Num => GPR.Header_Num,
      Element    => Boolean,
      No_Element => False,
      Key        => File_Name_Type,
      Hash       => GPR.Hash,
      Equal      => "=");

   procedure Build_Library
     (For_Project  : Project_Id;
      Project_Tree : Project_Tree_Ref;
      No_Create    : Boolean);
   --  Build, if necessary, the library of a library project. If No_Create
   --  is True then the actual static or shared library is not built, yet
   --  the exchange file with dependencies is created.

   procedure Emit_Compiler_Switches
     (Exchange_File : Text_IO.File_Type; Index : Name_List_Index);
   --  Helper subprogram to emit and filter compiler switches given by
   --  Index, one per line in the givenfile Exchange_File.

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

   function Is_Included_In_Global_Archive
     (Object_Name : File_Name_Type;
      Project     : Project_Id) return Boolean;
   --  Return True if the object Object_Name is not overridden by a source
   --  in a project extending project Project.

   procedure Wait_For_Slots_Less_Than (Count : Positive);
   --  Wait for the number of available process slots less then Count

   type Library_Object is record
      Path  : Path_Name_Type;
      TS    : Time_Stamp_Type;
      Known : Boolean;
   end record;

   function "<" (Left, Right : Library_Object) return Boolean
   is (Get_Name_String (Left.Path) < Get_Name_String (Right.Path));
   --  Operator uses for the ordered set Library_Objs in procedure
   --  Build_Library. Left < Right if Left path as a string is before
   --  Right path in alphabetical order.

   --  Dependency Files

   type Dep_Name;
   type Dep_Ptr is access Dep_Name;
   type Dep_Name is record
      Name : String_Access;
      Next : Dep_Ptr;
   end record;

   First_Dep : Dep_Ptr;
   --  Head of the list of dependency file path names

   procedure Add_Dep (Name : String);
   --  Insert a dependency file path name in the list starting at First_Dep,
   --  at the right place so that the list is sorted.

   ----------------
   -- Add_Dep --
   ----------------

   procedure Add_Dep (Name : String) is
      Next : Dep_Ptr := First_Dep;
   begin
      if Next = null or else Name < Next.Name.all then
         First_Dep := new Dep_Name'(new String'(Name), Next);

      else
         while Next.Next /= null and then
           Name > Next.Next.Name.all
         loop
            Next := Next.Next;
         end loop;

         Next.Next := new Dep_Name'(new String'(Name), Next.Next);
      end if;
   end Add_Dep;

   -------------------
   -- Build_Library --
   -------------------

   procedure Build_Library
     (For_Project  : Project_Id;
      Project_Tree : Project_Tree_Ref;
      No_Create    : Boolean)
   is
      package Objects is new Containers.Ordered_Sets (Library_Object);

      Library_Objs : Objects.Set;
      --  Objects that are in the library file with their time stamps, ordered
      --  by increasing path names.

      Library_SAL_Projs : Project_Vectors.Vector;
      --  List of non extended projects that are part of a Stand-Alone
      --  (aggregate) library project.

      Library_Sources   : Source_Vectors.Vector;
      --  Library Ada sources of Stand-Alone library, that is sources of the
      --  project in the closure of the interface.

      Complete_Interface_ALIs : FNHS.Instance;
      --  The ALI files in the complete interface set

      Expected_File_Name : String_Access;
      --  Expected library file name

      Mapping_Path : Path_Name_Type := No_Path;
      --  The path name of an eventual binder mapping file

      Mapping_FD : File_Descriptor := Invalid_FD;
      --  A File Descriptor for an eventual binder mapping file

      Library_Options_Success : Boolean := False;

      package Lang_Set renames GPR.Name_Id_Set;

      procedure Get_Objects;
      --  Get the paths of the object files of the library in ordered set
      --  Library_Objs.

      procedure Write_List (Label : Library_Section; List : String_List_Id);
      --  Write values in list into section Label in the given file. Ouptut
      --  Label is written first if it is not the current section.

      procedure Write_Name_List
        (Label : Library_Section; List : Name_List_Index);
      --  Write name list values into the Exchange_File, output Label first.
      --  Output Label is written first if it is not the current section.

      procedure Write_Name (Label : Library_Section; Name : Name_Id);
      --  Write name with label if Name /= No_Name

      procedure Write_Filename
        (Label : Library_Section; Filename : File_Name_Type);
      --  Write Filename with label if Filename /= No_File

      procedure Check_Section (Section : Library_Section);
      --  Check that current exchange file output section is Section and set it
      --  if not.

      --  Procedures to write specific sections of the exchange file

      procedure Write_Object_Files;
      procedure Write_Object_Directory;
      procedure Write_Compilers;
      procedure Write_Compiler_Leading_Switches;
      procedure Write_Compiler_Trailing_Switches;
      procedure Write_Partial_Linker;
      procedure Write_Shared_Lib_Minimum_Options;
      procedure Write_Library_Version;
      procedure Write_Runtime_Library_Dir;
      procedure Write_Auto_Init;
      procedure Write_Binding_Options;
      procedure Write_Run_Path_Option;
      procedure Write_Leading_Library_Options;
      procedure Write_Library_Options (Success : out Boolean);
      procedure Write_Library_Rpath_Options;
      procedure Write_Imported_Libraries;
      procedure Write_Dependency_Files;
      procedure Write_Toolchain_Version;
      procedure Write_Interface_Dep_Files;
      procedure Write_Other_Interfaces;
      procedure Write_Interface_Obj_Files;
      procedure Write_Sources;
      procedure Write_Response_Files;
      procedure Write_Mapping_File;

      procedure Wait_For_Dependency (P : Project_Id);
      --  Wait for dependent library project P build completed

      function In_Library_SAL_Projs (Src : Source_Id) return Boolean is
        (Library_SAL_Projs.Contains
           (Ultimate_Extending_Project_Of (Src.Project)));
      --  Returns True of Src.Project founder is in the Library_SAL_Projs

      Project_Name       : constant String :=
                             Get_Name_String (For_Project.Name);
      Current_Dir        : constant String := Get_Current_Dir;

      Exchange_File      : Text_IO.File_Type;
      Exchange_File_Name : String_Access;

      Latest_Object_TS : Time_Stamp_Type := Empty_Time_Stamp;

      Library_Builder_Name      : String_Access;
      Library_Builder           : String_Access;
      Library_Needs_To_Be_Built : Boolean := False;
      Dependencies_Ready        : Boolean := False;

      Object_Path : Path_Name_Type;
      Object_TS   : Time_Stamp_Type;
      Source      : Source_Id;
      Project     : Project_Id;
      Disregard   : Boolean;
      Path_Found  : Boolean;
      Iter        : Source_Iterator;

      Current_Section : Library_Section := No_Library_Section;

      -------------------
      -- Check_Section --
      -------------------

      procedure Check_Section (Section : Library_Section) is
      begin
         if Current_Section /= Section then
            Current_Section := Section;
            Put_Line (Exchange_File, Library_Label (Section));
         end if;
      end Check_Section;

      -----------------
      -- Get_Objects --
      -----------------

      procedure Get_Objects is

         Library_ALIs : FNHS.Instance;
         --  The ALI files of the Stand-Alone Library project

         Processed_ALIs : FNHS.Instance;
         --  The ALI files that have been processed to check if the
         --  corresponding library unit is in the interface set.

         Never : constant Time_Stamp_Type := (others => '9');
         --  A time stamp that is greater than any real one

         procedure Check_Latest_Object_TS (Source : Source_Id);
         --  Check if source object timestamp later than in Latest_Object_TS
         --  and update it if this is the case. If object is absent, set the
         --  Latest_Object_TS to Never and set Library_Needs_To_Be_Built to
         --  True.

         procedure Check_Interface
           (Proj : Project_Id;
            Tree : Project_Tree_Ref);
         --  Check if the interface of SAL project Proj is complete

         procedure Find_ALI_Path
           (The_ALI  : File_Name_Type;
            ALI_Path : in out Path_Name_Type;
            Proj     : Project_Id;
            Tree     : Project_Tree_Ref);
         --  Find the path of the ALI file The_ALI. It may be in project
         --  Proj, or if Proj is an aggregate library in one of its aggregated
         --  projects.

         procedure Get_Roots (Source : Source_Id);
         --  Get Roots of the SAL Source into object or ALI containers

         procedure Process
           (Proj : Project_Id;
            Tree : Project_Tree_Ref);
         --  Get objects for non Stand-Alone library

         procedure Process_ALI
           (The_ALI : File_Name_Type;
            Proj    : Project_Id;
            Tree    : Project_Tree_Ref);
         --  Check if the closure of a library unit which is or should be in
         --  the interface set is also in the interface set. Issue a warning
         --  for each missing library unit.

         procedure Process_Standalone
           (Proj : Project_Id;
            Tree : Project_Tree_Ref);
         --  Get objects for a Stand-Alone Library

         procedure Get_Closure;
         --  For Stand-Alone libraries, get the closure of the Ada interface
         --  and put the object files in Library_Objs.

         ----------------------------
         -- Check_Latest_Object_TS --
         ----------------------------

         procedure Check_Latest_Object_TS (Source : Source_Id) is
         begin
            if Source.Object_TS = Empty_Time_Stamp then
               Latest_Object_TS := Never;

               if not Library_Needs_To_Be_Built then
                  Library_Needs_To_Be_Built := True;

                  if Opt.Verbosity_Level > Opt.Low then
                     Put ("      -> missing object file: ");
                     Put_Line (Get_Name_String (Source.Object));
                  end if;
               end if;

            elsif Source.Object_TS > Latest_Object_TS then
               Latest_Object_TS := Source.Object_TS;
            end if;
         end Check_Latest_Object_TS;

         -------------
         -- Process --
         -------------

         procedure Process
           (Proj  : Project_Id;
            Tree  : Project_Tree_Ref)
         is
            pragma Unreferenced (Tree);

            Source : Source_Id;
            Iter   : Source_Iterator;
         begin
            Iter := For_Each_Source (Project_Tree, Proj);
            loop
               Source := GPR.Element (Iter);
               exit when Source = No_Source;

               --  Always get the time stamps when the main project is an
               --  aggregate project.

               Initialize_Source_Record
                 (Source, Always => Main_Project.Qualifier = Aggregate);

               if Is_Compilable (Source)
                 and then Source.Replaced_By = No_Source
                 and then Source.Language.Config.Objects_Linked
                 and then
                   ((Source.Unit = No_Unit_Index
                     and then Source.Kind = Impl)
                    or else
                      (Source.Unit /= No_Unit_Index
                       and then (Source.Kind = Impl
                                 or else Other_Part (Source) = No_Source)
                       and then not Is_Subunit (Source)))
                 and then
                   (not Source.Project.Externally_Built
                    or else not For_Project.Externally_Built
                    or else Source.Project.Extended_By /= No_Project)
               then
                  Library_Objs.Insert
                    ((Path  => Source.Object_Path,
                      TS    => Source.Object_TS,
                      Known => False));

                  Check_Latest_Object_TS (Source);
               end if;

               Next (Iter);
            end loop;
         end Process;

         -------------------
         -- Find_ALI_Path --
         -------------------

         procedure Find_ALI_Path
           (The_ALI  : File_Name_Type;
            ALI_Path : in out Path_Name_Type;
            Proj     : Project_Id;
            Tree     : Project_Tree_Ref)
         is

            Source : Source_Id;
            Iter   : Source_Iterator;

            Aggr_Projs : Aggregated_Project_List;

            Prj : Project_Id := Proj;
         begin
            while Prj /= No_Project loop
               Iter := For_Each_Source (Tree, Prj);
               loop
                  Source := GPR.Element (Iter);
                  exit when Source = No_Source;

                  Initialize_Source_Record (Source);

                  if Source.Dep_Name = The_ALI then
                     ALI_Path := Source.Dep_Path;
                     return;
                  end if;

                  Next (Iter);
               end loop;

               Prj := Prj.Extends;
            end loop;

            if Proj.Qualifier = Aggregate_Library then
               Aggr_Projs := Proj.Aggregated_Projects;
               while Aggr_Projs /= null loop
                  Find_ALI_Path
                    (The_ALI, ALI_Path, Aggr_Projs.Project, Aggr_Projs.Tree);
                  exit when ALI_Path /= No_Path;
                  Aggr_Projs := Aggr_Projs.Next;
               end loop;
            end if;
         end Find_ALI_Path;

         -----------------
         -- Process_ALI --
         -----------------

         Interface_Incomplete : Boolean := False;

         procedure Process_ALI
           (The_ALI : File_Name_Type;
            Proj    : Project_Id;
            Tree    : Project_Tree_Ref)
         is
            use ALI;
            Text       : Text_Buffer_Ptr;
            Idread     : ALI_Id;
            First_Unit : Unit_Id;
            Last_Unit  : ALI.Unit_Id;
            Unit_Data  : ALI.Unit_Record;
            Afile      : File_Name_Type;
            ALI_Path   : Path_Name_Type;

         begin
            --  Nothing to do if the ALI file has already been processed.
            --  This happens if an interface imports another interface.

            if not FNHS.Get (Processed_ALIs, The_ALI) then
               FNHS.Set (Processed_ALIs, The_ALI, True);

               ALI_Path := No_Path;
               Find_ALI_Path (The_ALI, ALI_Path, Proj, Tree);

               if ALI_Path /= No_Path then
                  Text := Read_Library_Info (File_Name_Type (ALI_Path));
               end if;

               if Text /= null then
                  Idread :=
                    Scan_ALI
                      (F          => The_ALI,
                       T          => Text,
                       Ignore_ED  => False,
                       Err        => True,
                       Read_Lines => "W");
                  Free (Text);

                  if Idread /= No_ALI_Id then
                     First_Unit := ALI.ALIs.Table (Idread).First_Unit;
                     Last_Unit  := ALI.ALIs.Table (Idread).Last_Unit;

                     --  Process both unit (spec and body) if the body is
                     --  needed by the spec (inline or generic). Otherwise,
                     --  just process the spec.

                     if First_Unit /= Last_Unit and then
                       not ALI.Units.Table (Last_Unit).Body_Needed_For_SAL
                     then
                        First_Unit := Last_Unit;
                     end if;

                     for Unit in First_Unit .. Last_Unit loop
                        Unit_Data := ALI.Units.Table (Unit);

                        --  Check if each withed unit which is in the library
                        --  is also in the interface set, if it has not yet
                        --  been processed.

                        for W in Unit_Data.First_With ..
                                 Unit_Data.Last_With
                        loop
                           Afile := Withs.Table (W).Afile;

                           if Afile /= No_File
                             and then FNHS.Get (Library_ALIs, Afile)
                             and then not FNHS.Get (Processed_ALIs, Afile)
                           then
                              if not FNHS.Get (Complete_Interface_ALIs, Afile)
                              then
                                 if not Interface_Incomplete then
                                    Put ("Warning: In library project """);
                                    Get_Name_String (Proj.Name);
                                    To_Mixed (Name_Buffer (1 .. Name_Len));
                                    Put (Name_Buffer (1 .. Name_Len));
                                    Put_Line ("""");
                                    Interface_Incomplete := True;
                                 end if;

                                 Put ("         Unit """);
                                 Get_Name_String (Withs.Table (W).Uname);
                                 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
                                 Put (Name_Buffer (1 .. Name_Len - 2));
                                 Put_Line (""" is not in the interface set");
                                 Put ("         but it is needed by ");

                                 case Unit_Data.Utype is
                                    when Is_Spec => Put ("the spec of ");
                                    when Is_Body => Put ("the body of ");
                                    when others  => null;
                                 end case;

                                 Put ('"');
                                 Get_Name_String (Unit_Data.Uname);
                                 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
                                 Put (Name_Buffer (1 .. Name_Len - 2));
                                 Put_Line ("""");

                                 FNHS.Set
                                   (Complete_Interface_ALIs, Afile, True);
                              end if;

                              --  Now, process this unit

                              Process_ALI (Afile, Proj, Tree);
                           end if;
                        end loop;
                     end loop;
                  end if;
               end if;
            end if;
         end Process_ALI;

         ------------------------
         -- Process_Standalone --
         ------------------------

         procedure Process_Standalone
           (Proj : Project_Id;
            Tree : Project_Tree_Ref)
         is
            pragma Unreferenced (Tree);

            Source : Source_Id;
            Iter   : Source_Iterator;

            List : String_List_Id;
            Elem : String_Element;
            OK   : Boolean;

         begin
            if Proj.Qualifier /= Aggregate_Library
              and then Proj.Extended_By = No_Project
            then
               Library_SAL_Projs.Append (Proj);
            end if;

            Iter := For_Each_Source (Project_Tree, Proj);
            loop
               Source := GPR.Element (Iter);
               exit when Source = No_Source;

               Initialize_Source_Record (Source);

               if Is_Compilable (Source)
                 and then Source.Replaced_By = No_Source
                 and then Source.Language.Config.Objects_Linked
                 and then
                   ((Source.Unit = No_Unit_Index
                     and then Source.Kind = Impl)
                    or else
                      (Source.Unit /= No_Unit_Index
                       and then (Source.Kind = Impl
                                 or else Other_Part (Source) = No_Source)
                       and then not Is_Subunit (Source)))
                 and then
                   (not Source.Project.Externally_Built
                    or else not For_Project.Externally_Built
                    or else Source.Project.Extended_By /= No_Project)
               then
                  if Source.Unit = No_Unit_Index then
                     OK := True;
                     Library_Objs.Insert
                       ((Path  => Source.Object_Path,
                         TS    => Source.Object_TS,
                         Known => False));

                     Get_Roots (Source);

                  else
                     FNHS.Set (Library_ALIs, Source.Dep_Name, True);

                     --  Check if it is an interface and record if it is one

                     OK := False;
                     List := For_Project.Lib_Interface_ALIs;

                     while List /= Nil_String loop
                        Elem := Project_Tree.Shared.String_Elements.Table
                          (List);

                        --  Checking against Lib_Interface_ALIs will never
                        --  succeed if Source is in a multi-unit file because
                        --  Dep_Name will be in the format <base>~<n>.ali
                        --  whereas the corresponding Elem will be <base>.ali.
                        --  ??? Fix the computation of Lib_Interface_ALIs ???
                        --  As a quick fix we compare <base>.

                        declare
                           use Ada.Strings.Fixed;

                           Dep_Str  : constant String :=
                                        Get_Name_String (Source.Dep_Name);
                           Elem_Str : constant String :=
                                        Get_Name_String (Elem.Value);

                           Dep_Multi_Index : constant Natural :=
                                               Index (Dep_Str, "~");
                           Elem_Dot_Index  : constant Natural :=
                                               Index (Elem_Str, ".");
                        begin
                           if Elem.Value = Name_Id (Source.Dep_Name)
                             or else
                               (Source.Index /= 0
                                and then Dep_Multi_Index = Elem_Dot_Index
                                and then Head (Dep_Str, Dep_Multi_Index - 1)
                                       = Head (Elem_Str, Elem_Dot_Index - 1))
                           then
                              OK := True;
                              Library_Sources.Append (Source);
                              FNHS.Set
                                (Complete_Interface_ALIs, Source.Dep_Name,
                                 True);
                              exit;
                           end if;
                        end;

                        List := Elem.Next;
                     end loop;
                  end if;

                  if OK then
                     Check_Latest_Object_TS (Source);
                  end if;
               end if;

               Next (Iter);
            end loop;
         end Process_Standalone;

         ---------------------
         -- Check_Interface --
         ---------------------

         procedure Check_Interface
           (Proj : Project_Id;
            Tree : Project_Tree_Ref)
         is
            Iface : String_List_Id := Proj.Lib_Interface_ALIs;
         begin
            while Iface /= Nil_String loop
               Process_ALI
                 (File_Name_Type
                    (Tree.Shared.String_Elements.Table (Iface).Value),
                  Proj, Tree);
               Iface := Tree.Shared.String_Elements.Table (Iface).Next;
            end loop;
         end Check_Interface;

         -----------------
         -- Get_Closure --
         -----------------

         procedure Get_Closure is
            Index    : Natural := 0;
            The_ALI  : ALI.ALI_Id;
            Text     : Text_Buffer_Ptr;
            Dep_Path : Path_Name_Type;
            Dep_TS   : aliased File_Attributes := Unknown_Attributes;
            Sfile    : File_Name_Type;
            Afile    : File_Name_Type;
            Src_Id   : GPR.Source_Id;
            Source   : Source_Id;

            procedure Add_To_Mapping
              (Source : Source_Id; From_Object_Dir : Boolean);
            --  Add data for Source in binder mapping file. Use the ALI file
            --  in the library ALI directory if From_Object_Dir is False and
            --  the project is a library project. Otherwise, use the ALI file
            --  in the object directory.

            --------------------
            -- Add_To_Mapping --
            --------------------

            procedure Add_To_Mapping
              (Source : Source_Id; From_Object_Dir : Boolean)
            is
               Unit : Unit_Index;

               ALI_Unit : Unit_Name_Type := No_Unit_Name;
               --  The unit name of an ALI file

               ALI_Name : File_Name_Type := No_File;
               --  The file name of the ALI file

               ALI_Project : Project_Id := No_Project;
               --  The project of the ALI file

            begin
               if Source = No_Source then
                  return;
               end if;

               Unit := Source.Unit;

               if Source.Replaced_By /= No_Source
                 or else Unit = No_Unit_Index
                 or else Unit.Name = No_Name
               then
                  ALI_Name := No_File;

               --  If this is a body, put it in the mapping

               elsif Source.Kind = Impl
                 and then Unit.File_Names (Impl) /= No_Source
                 and then Unit.File_Names (Impl).Project /= No_Project
               then
                  Get_Name_String (Unit.Name);
                  Add_Str_To_Name_Buffer ("%b");
                  ALI_Unit := Name_Find;
                  ALI_Name :=
                    Lib_File_Name (Unit.File_Names (Impl).Display_File);
                  ALI_Project := Unit.File_Names (Impl).Project;

               --  Otherwise, if this is a spec and there is no body, put it in
               --  the mapping.

               elsif Source.Kind = Spec
                 and then Unit.File_Names (Impl) = No_Source
                 and then Unit.File_Names (Spec) /= No_Source
                 and then Unit.File_Names (Spec).Project /= No_Project
               then
                  Get_Name_String (Unit.Name);
                  Add_Str_To_Name_Buffer ("%s");
                  ALI_Unit := Name_Find;
                  ALI_Name :=
                    Lib_File_Name (Unit.File_Names (Spec).Display_File);
                  ALI_Project := Unit.File_Names (Spec).Project;

               else
                  ALI_Name := No_File;
               end if;

               --  If we have something to put in the mapping then do it now.
               --  If the project is extended, look for the ALI file in the
               --  project, then in the extending projects in order, and use
               --  the last one found.

               if ALI_Name /= No_File then
                  --  Look in the project and the projects that are extending
                  --  it to find the real ALI

                  declare
                     ALI      : constant String := Get_Name_String (ALI_Name);
                     ALI_Path : Name_Id         := No_Name;

                     procedure Write_Mapping (Id : Name_Id);
                     --  Write name and line feed to Mapping_FD

                     ----------------
                     -- Write_Name --
                     ----------------

                     procedure Write_Mapping (Id : Name_Id) is
                     begin
                        Get_Name_String (Id);
                        Add_Char_To_Name_Buffer (ASCII.LF);

                        if Write (Mapping_FD, Name_Buffer'Address, Name_Len)
                          /= Name_Len
                        then
                           raise Program_Error with "Disk full";
                        end if;
                     end Write_Mapping;

                  begin
                     loop
                        --  For library projects, use the library ALI
                        --  directory, for other projects, use the
                        --  object directory.

                        if ALI_Project.Library and then not From_Object_Dir
                        then
                           Get_Name_String
                             (ALI_Project.Library_ALI_Dir.Display_Name);
                        else
                           Get_Name_String
                             (ALI_Project.Object_Directory.Display_Name);
                        end if;

                        Add_Str_To_Name_Buffer (ALI);

                        if Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
                           ALI_Path := Name_Find;
                        end if;

                        ALI_Project := ALI_Project.Extended_By;
                        exit when ALI_Project = No_Project;
                     end loop;

                     if ALI_Path /= No_Name then
                        --  First line is the unit name

                        Write_Mapping (Name_Id (ALI_Unit));

                        --  Second line is the ALI file name

                        Write_Mapping (Name_Id (ALI_Name));

                        --  Third line is the ALI path name

                        Write_Mapping (ALI_Path);
                     end if;
                  end;
               end if;
            end Add_To_Mapping;

            Closure_Sources : Source_Vectors.Vector := Library_Sources;
            --  Library Ada sources of Stand-Alone library, that is sources
            --  in the closure of the interface, including in imported
            --  projects.

         --  Start of processing for Get_Closure

         begin
            Over_Sources : while Index < Closure_Sources.Last_Index loop
               Index := Index + 1;
               Source := Closure_Sources (Index);

               Add_To_Mapping
                 (Source,
                  From_Object_Dir => Library_Sources.Contains (Source));

               Dep_Path := Source.Dep_Path;
               Dep_TS   := Source.Dep_TS;
               Text := Read_Library_Info_From_Full
                 (File_Name_Type (Dep_Path), Dep_TS'Access);

               if Text /= null then
                  The_ALI :=
                    ALI.Scan_ALI
                      (File_Name_Type (Dep_Path),
                       Text,
                       Ignore_ED  => False,
                       Err        => True,
                       Read_Lines => "W");
                  Free (Text);

                  --  Get the withed sources

                  Over_Units : for J in ALI.ALIs.Table (The_ALI).First_Unit
                                     .. ALI.ALIs.Table (The_ALI).Last_Unit
                  loop
                     Over_Imports : for K in ALI.Units.Table (J).First_With
                                          .. ALI.Units.Table (J).Last_With
                     loop
                        Sfile := ALI.Withs.Table (K).Sfile;

                        --  Skip generics

                        if Sfile /= No_File then
                           Afile := ALI.Withs.Table (K).Afile;

                           Src_Id := Source_Files_Htable.Get
                             (Project_Tree.Source_Files_HT, Sfile);
                           while Src_Id /= No_Source loop
                              Initialize_Source_Record (Src_Id);

                              if Is_Compilable (Src_Id)
                                and then Src_Id.Dep_Name = Afile
                              then
                                 case Src_Id.Kind is
                                 when Spec =>
                                    declare
                                       Bdy : constant GPR.Source_Id :=
                                         Other_Part (Src_Id);
                                    begin
                                       if Bdy /= No_Source
                                         and then not Bdy.Locally_Removed
                                       then
                                          Src_Id := Bdy;
                                       end if;
                                    end;

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

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

                                 exit;
                              end if;

                              Src_Id := Src_Id.Next_With_File_Name;
                           end loop;

                           if Src_Id /= No_Source then
                              if not Closure_Sources.Contains (Src_Id) then
                                 Closure_Sources.Append (Src_Id);
                              end if;

                              if In_Library_SAL_Projs (Src_Id)
                                and then not Library_Sources.Contains
                                               (Src_Id)
                              then
                                 Library_Sources.Append (Src_Id);
                                 Initialize_Source_Record (Src_Id);

                                 Check_Latest_Object_TS (Src_Id);
                              end if;
                           end if;
                        end if;
                     end loop Over_Imports;
                  end loop Over_Units;
               end if;

               Get_Roots (Source);

            end loop Over_Sources;
         end Get_Closure;

         ---------------
         -- Get_Roots --
         ---------------

         procedure Get_Roots (Source : Source_Id) is
            Root     : Roots_Access := Source.Roots;
            Position : Objects.Cursor;
            Inserted : Boolean;
         begin
            while Root /= null loop
               Initialize_Source_Record (Root.Root);

               if Root.Root.Unit = No_Unit_Index then
                  Library_Objs.Insert
                    ((Path  => Root.Root.Object_Path,
                      TS    => Root.Root.Object_TS,
                      Known => False), Position, Inserted);

                  Get_Roots (Root.Root);

               elsif In_Library_SAL_Projs (Root.Root)
                 and then not Library_Sources.Contains (Root.Root)
               then
                  Library_Sources.Append (Root.Root);
                  Check_Latest_Object_TS (Root.Root);
               end if;

               Root := Root.Next;
            end loop;
         end Get_Roots;

         procedure Process_Non_Standalone_Aggregate_Library is
           new For_Project_And_Aggregated (Process);

         procedure Process_Standalone_Aggregate_Library is
           new For_Project_And_Aggregated (Process_Standalone);

         Proj : Project_Id := For_Project;

      --  Start of processing of Get_Objects

      begin
         Library_Objs.Clear;
         Library_Sources.Clear;
         Library_Projs.Clear;
         Library_SAL_Projs.Clear;
         FNHS.Reset (Processed_ALIs);
         FNHS.Reset (Library_ALIs);
         FNHS.Reset (Complete_Interface_ALIs);

         if For_Project.Qualifier = Aggregate_Library then
            if For_Project.Standalone_Library = No then
               Process_Non_Standalone_Aggregate_Library
                 (For_Project, Project_Tree);
            else
               Process_Standalone_Aggregate_Library
                 (For_Project, Project_Tree);
            end if;

         else
            while Proj /= No_Project loop
               if For_Project.Standalone_Library = No then
                  Process (Proj, Project_Tree);
               else
                  Process_Standalone (Proj, Project_Tree);
               end if;

               Proj := Proj.Extends;
            end loop;
         end if;

         if For_Project.Standalone_Library /= No then
            --  Check the interface

            Check_Interface (For_Project, Project_Tree);

            --  Create the binder maping file

            Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
            Record_Temp_File (Project_Tree.Shared, Mapping_Path);

            Get_Closure;

            Close (Mapping_FD);

            --  Put all the object files in the closure in Library_Objs

            for Source of Library_Sources loop
               Library_Objs.Insert
                 ((Path  => Source.Object_Path,
                   TS    => Source.Object_TS,
                   Known => False));
            end loop;
         end if;
      end Get_Objects;

      -------------------------
      -- Wait_For_Dependency --
      -------------------------

      procedure Wait_For_Dependency (P : Project_Id) is
      begin
         while Libs_Are_Building.Contains (P.Name) loop
            --  There may be some executable binding in parallel to the library
            --  build operations, i.e. Outstanding_Processes may be greater
            --  than the list of libs being built.

            pragma Assert
              (Natural (Libs_Are_Building.Length) <= Outstanding_Processes,
               "more libs building than outstanding processes "
               & Libs_Are_Building.Length'Img & Outstanding_Processes'Img
               & ' ' & Get_Name_String (P.Name));

            --  Wait for any process to be done to check is the dependency
            --  resolved.

            Wait_For_Slots_Less_Than (Outstanding_Processes);
         end loop;
      end Wait_For_Dependency;

      ------------------------
      -- Write_Object_Files --
      ------------------------

      procedure Write_Object_Files is
      begin
         if not Library_Objs.Is_Empty then
            Put_Line (Exchange_File, Library_Label (Object_Files));

            for Item of Library_Objs loop
               Put_Line (Exchange_File, Get_Name_String (Item.Path));
            end loop;
         end if;
      end Write_Object_Files;

      ----------------------------
      -- Write_Object_Directory --
      ----------------------------

      procedure Write_Object_Directory is

         Object_Projects : Project_Vectors.Vector;
         Prj             : Project_Id;
         Index           : Natural;
         --  The projects that have already be found when looking for object
         --  directories.

         package PNHT renames Path_Name_HTable;

         Object_Directories : PNHT.Instance;
         --  The object directories that have already be found

         procedure Get_Object_Projects (Prj : Project_Id);
         --  Recursive procedure to collect the aggregated projects

         function Is_In_Object_Projects (Prj : Project_Id) return Boolean;
         --  Returns True iff Prj is in table Object_Projects

         function Is_In_Object_Directories
           (Dir : Path_Name_Type) return Boolean
         is (PNHT.Get (Object_Directories, Dir));
         --  Returns True iff Dir is in table Object_Directories

         -------------------------
         -- Get_Object_Projects --
         -------------------------

         procedure Get_Object_Projects (Prj : Project_Id) is
         begin
            if Prj.Qualifier = Aggregate_Library then
               declare
                  List : Aggregated_Project_List := Prj.Aggregated_Projects;
               begin
                  while List /= null loop
                     Get_Object_Projects (List.Project);
                     List := List.Next;
                  end loop;
               end;
            else
               --  Add object directories of the project and of the projects it
               --  extends, if any.

               declare
                  Proj : Project_Id := Prj;
               begin
                  while Proj /= No_Project loop
                     if not Is_In_Object_Projects (Proj) then
                        Object_Projects.Append (Proj);

                        if Proj.Object_Directory /= No_Path_Information
                          and then not Is_In_Object_Directories
                            (Proj.Object_Directory.Display_Name)
                        then
                           PNHT.Set
                             (Object_Directories,
                              Proj.Object_Directory.Display_Name, True);

                           Put_Line
                             (Exchange_File,
                              Get_Name_String
                                (Proj.Object_Directory.Display_Name));
                        end if;
                     end if;

                     Proj := Proj.Extends;
                  end loop;
               end;
            end if;
         end Get_Object_Projects;

         ---------------------------
         -- Is_In_Object_Projects --
         ---------------------------

         function Is_In_Object_Projects (Prj : Project_Id) return Boolean is
         begin
            return Object_Projects.Contains (Prj);
         end Is_In_Object_Projects;

      --  Start of processing for Write_Object_Directory

      begin
         Object_Projects.Clear;
         PNHT.Reset (Object_Directories);
         Put_Line (Exchange_File, Library_Label (Object_Directory));
         Get_Object_Projects (For_Project);

         Index := Object_Projects.First_Index;
         --  Note: cannot iterate on Object_Projects as we're modifying
         --  the container within the loop.
         while Index <= Object_Projects.Last_Index loop
            Prj := Object_Projects.Element (Index);
            Index := Index + 1;
            --  Add object directories of imported non library projects

            Process_Imported_Non_Libraries (Prj);

            for Proj of Non_Library_Projs loop
               Get_Object_Projects (Proj);
            end loop;

            --  Add ALI dir directories of imported projects (only if it
            --  is not an externally built project or if the project has
            --  sources). This skip the library projects with no sources
            --  used for example to add a system library to the linker.

            declare
               List : Project_List := Prj.All_Imported_Projects;
            begin
               while List /= null loop
                  if not Is_In_Object_Projects (List.Project) and then
                     (not List.Project.Externally_Built
                      or else List.Project.Source_Dirs /= Nil_String)
                  then
                     if List.Project.Library_ALI_Dir /= No_Path_Information
                     then
                        Put_Line
                          (Exchange_File,
                           Get_Name_String
                             (List.Project.Library_ALI_Dir.Display_Name));

                     elsif List.Project.Library_Dir /= No_Path_Information
                     then
                        Put_Line
                          (Exchange_File,
                           Get_Name_String
                             (List.Project.Library_Dir.Display_Name));
                     end if;
                  end if;

                  List := List.Next;
               end loop;
            end;
         end loop;
      end Write_Object_Directory;

      ---------------------
      -- Write_Compilers --
      ---------------------

      procedure Write_Compilers is

         procedure Compilers_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean);
         --  Write compilers for the given project

         Dummy     : Boolean := True;
         Lang_Seen : Lang_Set.Set;

         -------------------
         -- Compilers_For --
         -------------------

         procedure Compilers_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean)
         is
            pragma Unreferenced (Tree, Dummy);
            Lang     : Language_Ptr := Project.Languages;
            Compiler : String_Access;
         begin
            --  Exchange file, Compilers section

            while Lang /= No_Language_Index loop
               if not Lang_Seen.Contains (Lang.Name) then
                  Lang_Seen.Insert (Lang.Name);

                  Compiler := Get_Compiler_Driver_Path (Project, Lang);
                  if Compiler /= null then
                     Put_Line
                       (Exchange_File,
                        Get_Name_String (Lang.Name) & ASCII.LF
                        & Compiler.all);

                  elsif Lang.Config.Compiler_Driver /= No_File then
                     Put_Line
                       (Exchange_File,
                        Get_Name_String (Lang.Name) & ASCII.LF
                        & Get_Name_String (Lang.Config.Compiler_Driver));
                  end if;
               end if;

               Lang := Lang.Next;
            end loop;
         end Compilers_For;

         procedure For_Imported is
           new For_Every_Project_Imported (Boolean, Compilers_For);

      --  Start of processing for Write_Compilers

      begin
         Put_Line (Exchange_File, Library_Label (Compilers));

         Compilers_For (For_Project, Project_Tree, Dummy);

         if For_Project.Qualifier = Aggregate_Library then
            For_Imported (For_Project, Project_Tree, Dummy);
         end if;
      end Write_Compilers;

      -------------------------------------
      -- Write_Compiler_Leading_Switches --
      -------------------------------------

      procedure Write_Compiler_Leading_Switches is

         procedure Compiler_Leading_Switches_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean);
         --  Write compilers for the given project

         Dummy     : Boolean := True;
         Lang_Seen : Lang_Set.Set;

         -----------------------------------
         -- Compiler_Leading_Switches_For --
         -----------------------------------

         procedure Compiler_Leading_Switches_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean)
         is
            pragma Unreferenced (Tree, Dummy);
            Lang : Language_Ptr := Project.Languages;
            Indx : Name_List_Index;

         begin
            while Lang /= No_Language_Index loop
               if not Lang_Seen.Contains (Lang.Name) then
                  Lang_Seen.Insert (Lang.Name);
                  Indx := Lang.Config.Compiler_Leading_Required_Switches;

                  if Indx /= No_Name_List then
                     Put_Line
                       (Exchange_File,
                        "language=" & Get_Name_String (Lang.Name));
                     Emit_Compiler_Switches (Exchange_File, Indx);

                     if Opt.CodePeer_Mode then
                        Put_Line (Exchange_File, "-gnatcC");
                     end if;
                  end if;
               end if;

               Lang := Lang.Next;
            end loop;
         end Compiler_Leading_Switches_For;

         procedure For_Imported is new For_Every_Project_Imported
           (Boolean, Compiler_Leading_Switches_For);

      --  Start of processing for Write_Compiler_Leading_Switches

      begin
         Put_Line (Exchange_File, Library_Label (Compiler_Leading_Switches));

         Compiler_Leading_Switches_For (For_Project, Project_Tree, Dummy);

         if For_Project.Qualifier = Aggregate_Library then
            For_Imported (For_Project, Project_Tree, Dummy);
         end if;
      end Write_Compiler_Leading_Switches;

      --------------------------------------
      -- Write_Compiler_Trailing_Switches --
      --------------------------------------

      procedure Write_Compiler_Trailing_Switches is

         procedure Compiler_Trailing_Switches_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean);
         --  Write compilers for the given project

         Dummy     : Boolean := True;
         Lang_Seen : Lang_Set.Set;

         ------------------------------------
         -- Compiler_Trailing_Switches_For --
         ------------------------------------

         procedure Compiler_Trailing_Switches_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean)
         is
            pragma Unreferenced (Tree, Dummy);
            Lang : Language_Ptr := Project.Languages;
            Indx : Name_List_Index;
            Node : Name_Node;
         begin
            while Lang /= No_Language_Index loop
               if not Lang_Seen.Contains (Lang.Name) then
                  Lang_Seen.Insert (Lang.Name);
                  Indx := Lang.Config.Compiler_Trailing_Required_Switches;

                  if Indx /= No_Name_List then
                     Put_Line
                       (Exchange_File,
                        "language=" & Get_Name_String (Lang.Name));

                     while Indx /= No_Name_List loop
                        Node := Project_Tree.Shared.Name_Lists.Table (Indx);
                        Put_Line (Exchange_File, Get_Name_String (Node.Name));
                        Indx := Node.Next;
                     end loop;
                  end if;
               end if;

               Lang := Lang.Next;
            end loop;
         end Compiler_Trailing_Switches_For;

         procedure For_Imported is new For_Every_Project_Imported
           (Boolean, Compiler_Trailing_Switches_For);

      --  Start of processing for Write_Compiler_Trailing_Switches

      begin
         Put_Line (Exchange_File, Library_Label (Compiler_Trailing_Switches));

         Compiler_Trailing_Switches_For (For_Project, Project_Tree, Dummy);

         if For_Project.Qualifier = Aggregate_Library then
            For_Imported (For_Project, Project_Tree, Dummy);
         end if;
      end Write_Compiler_Trailing_Switches;

      ----------------
      -- Write_Name --
      ----------------

      procedure Write_Name (Label : Library_Section; Name : Name_Id) is
      begin
         if Name /= No_Name then
            Put_Line
              (Exchange_File,
               Library_Label (Label) & ASCII.LF & Get_Name_String (Name));
         end if;
      end Write_Name;

      --------------------
      -- Write_Filename --
      --------------------

      procedure Write_Filename
        (Label : Library_Section; Filename : File_Name_Type) is
      begin
         Write_Name (Label, Name_Id (Filename));
      end Write_Filename;

      ----------------
      -- Write_List --
      ----------------

      procedure Write_List (Label : Library_Section; List : String_List_Id) is
         Current : String_List_Id := List;
         Element : String_Element;
      begin
         while Current /= Nil_String loop
            Element := Project_Tree.Shared.String_Elements.Table (Current);
            Get_Name_String (Element.Value);

            if Name_Len /= 0 then
               Check_Section (Label);
               Put_Line (Exchange_File, Name_Buffer (1 .. Name_Len));
            end if;

            Current := Element.Next;
         end loop;
      end Write_List;

      ---------------------
      -- Write_Name_List --
      ---------------------

      procedure Write_Name_List
        (Label : Library_Section; List : Name_List_Index)
      is
         Current : Name_List_Index := List;
         Nam     : Name_Node;
      begin
         if List /= No_Name_List then
            Check_Section (Label);

            loop
               Nam := Project_Tree.Shared.Name_Lists.Table (Current);
               Put_Line (Exchange_File, Get_Name_String (Nam.Name));
               Current := Nam.Next;
               exit when Current = No_Name_List;
            end loop;
         end if;
      end Write_Name_List;

      --------------------------
      -- Write_Partial_Linker --
      --------------------------

      procedure Write_Partial_Linker is
         List : constant Name_List_Index :=
                  For_Project.Config.Lib_Partial_Linker;
      begin
         if List /= No_Name_List then
            Write_Name_List (Partial_Linker, List);
         end if;
      end Write_Partial_Linker;

      --------------------------------------
      -- Write_Shared_Lib_Minimum_Options --
      --------------------------------------

      procedure Write_Shared_Lib_Minimum_Options is
         Library_Options : Variable_Value := Nil_Variable_Value;
      begin
         --  Output the minimal options to build a shared library (standard
         --  or encapsulated).

         if For_Project.Standalone_Library = Encapsulated then
            Library_Options :=
              Value_Of
              (Name_Library_Encapsulated_Options,
               For_Project.Decl.Attributes, Project_Tree.Shared);

            if not Library_Options.Default then
               Write_List
                 (Gprexch.Shared_Lib_Minimum_Options, Library_Options.Values);
            end if;

         else
            Write_Name_List
              (Shared_Lib_Minimum_Options,
               For_Project.Config.Shared_Lib_Min_Options);
         end if;
      end Write_Shared_Lib_Minimum_Options;

      ---------------------------
      -- Write_Library_Version --
      ---------------------------

      procedure Write_Library_Version is
         List : constant Name_List_Index :=
                  For_Project.Config.Lib_Version_Options;
      begin
         if List /= No_Name_List then
            Write_Name_List (Library_Version_Options, List);
         end if;
      end Write_Library_Version;

      -------------------------------
      -- Write_Runtime_Library_Dir --
      -------------------------------

      procedure Write_Runtime_Library_Dir is

         procedure RTL_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean);
         --  Write runtime libraries for the given project

         Dummy     : Boolean := True;
         Lang_Seen : Lang_Set.Set;

         -------------
         -- RTL_For --
         -------------

         procedure RTL_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean)
         is
            pragma Unreferenced (Tree, Dummy);
            List : Language_Ptr := Project.Languages;
            Lib_Dirs : Name_List_Index;
            Nam_Nod : Name_Node;
         begin
            while List /= No_Language_Index loop
               if List.Config.Runtime_Library_Dirs /= No_Name_List then
                  Lib_Dirs := List.Config.Runtime_Library_Dirs;

                  while Lib_Dirs /= No_Name_List loop
                     Nam_Nod :=
                       Project_Tree.Shared.Name_Lists.Table (Lib_Dirs);
                     if not Lang_Seen.Contains (Nam_Nod.Name)
                     then
                        if Lang_Seen.Length = 0 then
                           Put_Line
                             (Exchange_File,
                              Library_Label (Runtime_Library_Dir));
                        end if;
                        Lang_Seen.Insert (Nam_Nod.Name);

                        Put_Line
                          (Exchange_File,
                           Get_Name_String (List.Name) & ASCII.LF
                           & Get_Name_String (Nam_Nod.Name));
                     end if;

                     Lib_Dirs := Nam_Nod.Next;
                  end loop;
               end if;

               List := List.Next;
            end loop;
         end RTL_For;

         procedure For_Imported is
           new For_Every_Project_Imported (Boolean, RTL_For);

      --  Start of processing for Write_Runtime_Library_Dir

      begin
         RTL_For (For_Project, Project_Tree, Dummy);

         if For_Project.Qualifier = Aggregate_Library then
            For_Imported (For_Project, Project_Tree, Dummy);
         end if;
      end Write_Runtime_Library_Dir;

      ---------------------
      -- Write_Auto_Init --
      ---------------------

      procedure Write_Auto_Init is
      begin
         if For_Project.Standalone_Library /= No and then
           For_Project.Lib_Auto_Init
         then
            Put_Line (Exchange_File, Library_Label (Auto_Init));
         end if;
      end Write_Auto_Init;

      ---------------------------
      -- Write_Binding_Options --
      ---------------------------

      procedure Write_Binding_Options is
      begin
         if For_Project.Standalone_Library /= No then
            declare
               Binder_Package : constant Package_Id :=
                                  Value_Of
                                    (Name        => Name_Binder,
                                     In_Packages =>
                                       For_Project.Decl.Packages,
                                     Shared      => Project_Tree.Shared);

            begin
               if Binder_Package /= No_Package then
                  declare
                     Defaults : constant Array_Element_Id :=
                                  Value_Of
                                    (Name      => Name_Default_Switches,
                                     In_Arrays =>
                                       Project_Tree.Shared.Packages.Table
                                         (Binder_Package).Decl.Arrays,
                                     Shared    => Project_Tree.Shared);
                     Switch_Array : constant Array_Element_Id :=
                                  Value_Of
                                    (Name      => Name_Switches,
                                     In_Arrays =>
                                       Project_Tree.Shared.Packages.Table
                                         (Binder_Package).Decl.Arrays,
                                     Shared    => Project_Tree.Shared);
                     Switches : Variable_Value := Nil_Variable_Value;

                  begin
                     if Defaults /= No_Array_Element then
                        Switches :=
                          Value_Of
                            (Index     => Name_Ada,
                             Src_Index => 0,
                             In_Array  => Defaults,
                             Shared    => Project_Tree.Shared);

                        if not Switches.Default then
                           Write_List
                             (Gprexch.Binding_Options, Switches.Values);
                        end if;
                     end if;

                     if Switch_Array /= No_Array_Element then
                        Switches :=
                          Value_Of
                            (Index                  => Name_Ada,
                             Src_Index              => 0,
                             In_Array               => Switch_Array,
                             Force_Lower_Case_Index => True,
                             Shared                 => Project_Tree.Shared);

                        if not Switches.Default then
                           Write_List
                             (Gprexch.Binding_Options, Switches.Values);
                        end if;
                     end if;
                  end;
               end if;
            end;
         end if;
      end Write_Binding_Options;

      ---------------------------
      -- Write_Run_Path_Option --
      ---------------------------

      procedure Write_Run_Path_Option is
         List : constant Name_List_Index :=
                  For_Project.Config.Run_Path_Option;
      begin
         if Opt.Run_Path_Option and then List /= No_Name_List then
            Write_Name_List (Run_Path_Option, List);
            Put_Line (Exchange_File,
                      Library_Label (Gprexch.Run_Path_Origin));

            if For_Project.Config.Run_Path_Origin /= No_Name then
               Put_Line (Exchange_File,
                         Get_Name_String (For_Project.Config.Run_Path_Origin));
            end if;

            if For_Project.Config.Separate_Run_Path_Options then
               Put_Line
                 (Exchange_File,
                  Library_Label (Gprexch.Separate_Run_Path_Options));
            end if;
         end if;
      end Write_Run_Path_Option;

      -----------------------------------
      -- Write_Leading_Library_Options --
      -----------------------------------

      procedure Write_Leading_Library_Options is
         Leading_Library_Options : Variable_Value := Nil_Variable_Value;
      begin
         --  If attribute Leading_Library_Options was specified, add these
         --  additional options.

         Leading_Library_Options :=
           Value_Of
           (Name_Leading_Library_Options,
            For_Project.Decl.Attributes, Project_Tree.Shared);

         if not Leading_Library_Options.Default then
            Write_List
              (Gprexch.Leading_Library_Options,
               Leading_Library_Options.Values);
         end if;
      end Write_Leading_Library_Options;

      ---------------------------
      -- Write_Library_Options --
      ---------------------------

      procedure Write_Library_Options (Success : out Boolean) is

         procedure Write_Linker_Options (P : Project_Id);
         --  Write linker options for Project

         ------------------------------
         -- Write_Linker_Options --
         ------------------------------

         procedure Write_Linker_Options (P : Project_Id) is
            Linker_Package : constant Package_Id :=
                               Value_Of
                                 (Name        => Name_Linker,
                                  In_Packages => P.Decl.Packages,
                                  Shared      => Project_Tree.Shared);
         begin
            --  Check linker package for a definition of Linker_Options

            if Linker_Package /= No_Package then
               Check_Attribute : declare
                  Opts : constant Variable_Value :=
                           Value_Of
                             (Variable_Name => Name_Linker_Options,
                              In_Variables  =>
                                Project_Tree.Shared.Packages.Table
                                  (Linker_Package).Decl.Attributes,
                              Shared => Project_Tree.Shared);

               begin
                  --  If a Linker_Options attribute is found, output it
                  --  into the Library_Options section.

                  if not Opts.Default then
                     Output_Options : declare
                        List : String_List_Id := Opts.Values;
                        Elem : String_Element;
                     begin
                        if List /= Nil_String then
                           --  First ensure the section is opended

                           Check_Section (Library_Options);

                           if P.Library_Dir.Name /= No_Path then
                              Put_Line
                                (Exchange_File,
                                 "-L" & Get_Name_String (P.Library_Dir.Name));
                           end if;

                           loop
                              Elem :=
                                Project_Tree.Shared.String_Elements.Table
                                  (List);

                              Put_Line
                                (Exchange_File, Get_Name_String (Elem.Value));

                              List := Elem.Next;

                              exit when List = Nil_String;
                           end loop;
                        end if;
                     end Output_Options;
                  end if;
               end Check_Attribute;
            end if;
         end Write_Linker_Options;

         Library_Options : Variable_Value := Nil_Variable_Value;

      --  Start of processing for Write_Library_Options

      begin
         Success := True;

         --  If attribute Library_Options was specified, add these
         --  additional options.

         Library_Options :=
           Value_Of
           (Name_Library_Options,
            For_Project.Decl.Attributes, Project_Tree.Shared);

         if not Library_Options.Default then
            Write_List (Gprexch.Library_Options, Library_Options.Values);

            --  For static libraries, check that the library options are
            --  existing object files.
            --  Also, skip this check for SALs so that Library_Options other
            --  than object files may be specified for the partial linking.

            if For_Project.Standalone_Library = No
              and then Is_Static (For_Project)
            then
               declare
                  List : String_List_Id := Library_Options.Values;
                  Elem : String_Element;
                  OK   : Boolean;
               begin
                  while List /= Nil_String loop
                     Elem := Project_Tree.Shared.String_Elements.Table (List);
                     Get_Name_String (Elem.Value);

                     if Is_Absolute_Path (Name_Buffer (1 .. Name_Len)) then
                        OK := Is_Regular_File (Name_Buffer (1 .. Name_Len));

                     else
                        OK := Is_Regular_File
                          (Get_Name_String (For_Project.Object_Directory.Name)
                           & Directory_Separator
                           & Name_Buffer (1 .. Name_Len));
                     end if;

                     if not OK then
                        Error_Msg
                          (Msg           => "unknown object file " &
                                             Name_Buffer (1 .. Name_Len),
                           Flag_Location => Library_Options.Location);
                        Success := False;
                     end if;

                     List := Elem.Next;
                  end loop;
               end;
            end if;
         end if;

         --  For encapsulated and shared libraries we also want to add the
         --  Linker_Options for all imported projects.

         if not Is_Static (For_Project)
           or else For_Project.Standalone_Library = Encapsulated
         then
            declare
               L : Project_List := For_Project.All_Imported_Projects;
            begin
               while L /= null loop
                  Write_Linker_Options (L.Project);
                  L := L.Next;
               end loop;
            end;
         end if;

         --  Get -largs section from command line for shared libraries

         if not Is_Static (For_Project) then
            for Arg of Command_Line_Linker_Options loop
               Check_Section (Gprexch.Library_Options);
               Put_Line (Exchange_File, Arg);
            end loop;
         end if;
      end Write_Library_Options;

      ---------------------------------
      -- Write_Library_Rpath_Options --
      ---------------------------------

      procedure Write_Library_Rpath_Options is

         procedure Add_Language (Lang : Language_Ptr);
         --  Add language Name in array Langs if not already there

         procedure Find_Languages
           (Project    : Project_Id;
            Tree       : Project_Tree_Ref;
            With_State : in out Boolean);
         --  Find the languages of a project

         procedure Find_All_Languages is new
           For_Every_Project_Imported (Boolean, Find_Languages);

         procedure Get_Directory;
         --  Get a directory for one language

         procedure Get_Languages;
         --  Put in Langs the languages of the project tree rooted at project
         --  For_Project.

         ------------------
         -- Add_Language --
         ------------------

         procedure Add_Language (Lang : Language_Ptr) is
         begin

            --  Only add a language if it is not already in the list

            for J in 1 .. Last_Lang loop
               if Lang.Name = Langs (J).Name then
                  return;
               end if;
            end loop;

            --  Double array Langs if already full

            if Last_Lang = Langs'Last then
               declare
                  New_Langs : constant Lang_Names_Ptr :=
                    new Lang_Names (1 .. 2 * Langs'Length);

               begin
                  New_Langs (Langs'Range) := Langs.all;
                  Langs := New_Langs;
               end;
            end if;

            Last_Lang := Last_Lang + 1;
            Langs (Last_Lang) := Lang;
         end Add_Language;

         --------------------
         -- Find_Languages --
         --------------------

         procedure Find_Languages
           (Project    : Project_Id;
            Tree       : Project_Tree_Ref;
            With_State : in out Boolean)
         is
            pragma Unreferenced (Tree);
            pragma Unreferenced (With_State);

            Lang : Language_Ptr := Project.Languages;

         begin
            while Lang /= No_Language_Index loop
               Add_Language (Lang);
               Lang := Lang.Next;
            end loop;
         end Find_Languages;

         -------------------
         -- Get_Languages --
         -------------------

         procedure Get_Languages is
            OK : Boolean := True;
         begin
            Last_Lang := 0;

            Find_Languages (For_Project, Project_Tree, OK);

            Find_All_Languages
              (By                 => For_Project,
               Tree               => Project_Tree,
               With_State         => OK,
               Include_Aggregated => False);
         end Get_Languages;

         List : Array_Element_Id;
         Elem : Array_Element;
         Label_Issued : Boolean := False;
         Lang_Index : Natural;
         Lang_Ptr   : Language_Ptr;

         Opt_List : String_List_Id;
         Opt_Elem : String_Element;

         -------------------
         -- Get_Directory --
         -------------------

         procedure Get_Directory is
            Opt_Nmb : Natural := 0;

            Args : Argument_List_Access;
            FD : File_Descriptor;
            Pname : Path_Name_Type;
            Return_Code : Integer;
            pragma Unreferenced (Return_Code);

            File : Text_File;
            Line : String (1 .. 1000);
            Last : Natural;

            Disregard : Boolean;
            pragma Unreferenced (Disregard);

         begin
            --  Check that the compiler driver exists

            if Lang_Ptr.Config.Compiler_Driver_Path = null then
               Lang_Ptr.Config.Compiler_Driver_Path :=
                 Locate_Exec_On_Path
                   (Get_Name_String (Lang_Ptr.Config.Compiler_Driver));
            end if;

            if Lang_Ptr.Config.Compiler_Driver_Path /= null then

               --  Count the options

               while Opt_List /= Nil_String loop
                  Opt_Elem :=
                    Project_Tree.Shared.String_Elements.Table (Opt_List);
                  Opt_Nmb := Opt_Nmb + 1;
                  Opt_List := Opt_Elem.Next;
               end loop;

               Args := new Argument_List (1 .. Opt_Nmb);

               --  Put the options in Args

               Opt_Nmb := 0;
               Opt_List := Elem.Value.Values;

               while Opt_List /= Nil_String loop
                  Opt_Elem :=
                    Project_Tree.Shared.String_Elements.Table (Opt_List);
                  Opt_Nmb := Opt_Nmb + 1;
                  Args (Opt_Nmb) :=
                    new String'(Get_Name_String (Opt_Elem.Value));
                  Opt_List := Opt_Elem.Next;
               end loop;

               --  Create a temporary file and invoke the compiler with the
               --  options redirecting the output to this temporary file.

               Tempdir.Create_Temp_File (FD, Pname);
               Spawn
                 (Program_Name => Lang_Ptr.Config.Compiler_Driver_Path.all,
                  Args => Args.all,
                  Output_File_Descriptor => FD,
                  Return_Code => Return_Code);
               Close (FD);
               Free (Args);

               --  Now read the temporary file and get the first non empty
               --  line, if any.

               Open (File, Get_Name_String (Pname));

               if Is_Valid (File) then
                  Last := 0;
                  while not End_Of_File (File) loop
                     Get_Line (File, Line, Last);
                     exit when Last > 0;
                  end loop;

                  --  Get the directory name of the path

                  if Last /= 0 then
                     declare
                        Dir : constant String :=
                          Dir_Name
                            (Normalize_Pathname
                               (Line (1 .. Last),
                                Resolve_Links => Opt.Follow_Links_For_Files));

                     begin

                        --  If it is in fact a directory, put it in the
                        --  exchange file.

                        if Is_Directory (Dir) then
                           if not Label_Issued then
                              Put_Line
                                (Exchange_File,
                                 Library_Label
                                   (Gprexch.Library_Rpath_Options));
                              Label_Issued := True;
                           end if;

                           Put_Line (Exchange_File, Dir);
                        end if;
                     end;
                  end if;
               end if;

               if Is_Valid (File) then
                  Close (File);
               end if;

               --  Delete the temporary file, if gprbuild was not invoked
               --  with -dn.

               if not Opt.Keep_Temporary_Files then
                  Delete_File (Get_Name_String (Pname), Disregard);
               end if;
            end if;
         end Get_Directory;

      --  Start of processing for Write_Library_Rpath_Options

      begin
         if Opt.Run_Path_Option
           and then For_Project.Config.Run_Path_Option /= No_Name_List
         then
            List :=
              Value_Of
                (Name_Library_Rpath_Options,
                 For_Project.Decl.Arrays, Project_Tree.Shared);

            if List /= No_Array_Element then
               Get_Languages;

               while Last_Lang /= 0 and then List /= No_Array_Element loop
                  Elem := Project_Tree.Shared.Array_Elements.Table (List);
                  Lang_Index := 0;

                  for J in 1 .. Last_Lang loop
                     if Elem.Index = Langs (J).Name then
                        Lang_Index := J;
                        exit;
                     end if;
                  end loop;

                  if Lang_Index /= 0 then
                     Lang_Ptr := Langs (Lang_Index);

                     --  Remove language from the list so that rpath options
                     --  are not looked for twice for the same language.

                     Langs (Lang_Index .. Last_Lang - 1) :=
                       Langs (Lang_Index + 1 .. Last_Lang);
                     Last_Lang := Last_Lang - 1;

                     --  Invoke the compiler for the language, followed by
                     --  the options and put the result into a temporary file.

                     Opt_List := Elem.Value.Values;

                     --  Nothing to do if there is no options

                     if Opt_List /= Nil_String then
                        Get_Directory;
                     end if;
                  end if;

                  List := Elem.Next;
               end loop;
            end if;
         end if;
      end Write_Library_Rpath_Options;

      ------------------------------
      -- Write_Imported_Libraries --
      ------------------------------

      procedure Write_Imported_Libraries is
      begin
         --  If there are imported libraries, put their data in the exchange
         --  file.

         if not Library_Projs.Is_Empty then
            Put_Line (Exchange_File, Library_Label (Imported_Libraries));

            for J in reverse 1 .. Library_Projs.Last_Index loop
               if For_Project.Qualifier /= Aggregate_Library
                 or else Library_Projs (J).Proj.Externally_Built
               then
                  Put_Line
                    (Exchange_File,
                     Get_Name_String
                       (Library_Projs (J).Proj.Library_Dir.Display_Name)
                     & ASCII.LF
                     & Get_Name_String (Library_Projs (J).Proj.Library_Name));
               end if;
            end loop;
         end if;
      end Write_Imported_Libraries;

      ----------------------------
      -- Write_Dependency_Files --
      ----------------------------

      procedure Write_Dependency_Files is

         procedure Process (Proj : Project_Id; Tree : Project_Tree_Ref);

         procedure Add (Source : Source_Id);

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

         procedure Add (Source : Source_Id) is
         begin
            if Source.Unit = No_Unit_Index
              or else For_Project.Standalone_Library = No
            then
               Add_Dep (Get_Name_String (Source.Dep_Path));
            end if;
         end Add;

         -------------
         -- Process --
         -------------

         procedure Process (Proj : Project_Id; Tree : Project_Tree_Ref) is
            pragma Unreferenced (Tree);
            Current_Proj : Project_Id := Proj;
            Source       : Source_Id;

         begin
            while Current_Proj /= No_Project loop
               declare
                  Iter : Source_Iterator;
               begin
                  Iter := For_Each_Source (Project_Tree, Current_Proj);

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

                     if not Source.Locally_Removed
                       and then Source.Dep_Path /= No_Path
                       and then
                         (not Source.Project.Externally_Built
                          or else not For_Project.Externally_Built
                          or else Source.Project.Extended_By /= No_Project)
                     then
                        if Source.Kind = Spec then
                           if Other_Part (Source) = No_Source then
                              Add (Source);
                           end if;

                        elsif not Is_Subunit (Source) then
                           Add (Source);
                        end if;
                     end if;

                     Next (Iter);
                  end loop;
               end;

               Current_Proj := Current_Proj.Extends;
            end loop;
         end Process;

         procedure Process_Aggregate_Library is
           new For_Project_And_Aggregated (Process);

      --  Start of processing for Write_Dependency_Files

      begin
         Put_Line (Exchange_File, Library_Label (Dependency_Files));
         First_Dep := null;

         Process_Aggregate_Library (For_Project, Project_Tree);

         if For_Project.Standalone_Library /= No then
            for Source of Library_Sources loop
               Add_Dep (Get_Name_String (Source.Dep_Path));
            end loop;
         end if;

         while First_Dep /= null loop
            Put_Line (Exchange_File, First_Dep.Name.all);
            First_Dep := First_Dep.Next;
         end loop;
      end Write_Dependency_Files;

      ------------------------
      -- Write_Mapping_File --
      ------------------------

      procedure Write_Mapping_File is
      begin
         if Mapping_Path /= No_Path then
            Put_Line
              (Exchange_File,
               Library_Label (Mapping_File) & ASCII.LF
               & Get_Name_String (Mapping_Path));
         end if;
      end Write_Mapping_File;

      -----------------------------
      -- Write_Toolchain_Version --
      -----------------------------

      procedure Write_Toolchain_Version is

         procedure Toolchain_Version_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean);
         --  Write runtime libraries for the given project

         Dummy     : Boolean := True;
         Lang_Seen : Lang_Set.Set;

         ---------------------------
         -- Toolchain_Version_For --
         ---------------------------

         procedure Toolchain_Version_For
           (Project : Project_Id;
            Tree    : Project_Tree_Ref;
            Dummy   : in out Boolean)
         is
            pragma Unreferenced (Tree, Dummy);
            List : Language_Ptr := Project.Languages;
         begin
            while List /= No_Language_Index loop
               if (List.Config.Toolchain_Version /= No_Name or else
                   List.Config.Runtime_Library_Version /= No_Name)
                 and then not Lang_Seen.Contains (List.Name)
               then
                  if Lang_Seen.Length = 0 then
                     Put_Line
                       (Exchange_File, Library_Label (Toolchain_Version));
                  end if;
                  Lang_Seen.Insert (List.Name);

                  Put_Line (Exchange_File, Get_Name_String (List.Name));

                  if List.Config.Runtime_Library_Version /= No_Name then
                     Put_Line
                       (Exchange_File,
                        Get_Name_String (List.Config.Runtime_Library_Version));
                  else
                     Put_Line
                       (Exchange_File,
                        Get_Name_String (List.Config.Toolchain_Version));
                  end if;
               end if;

               List := List.Next;
            end loop;
         end Toolchain_Version_For;

         procedure For_Imported is
           new For_Every_Project_Imported (Boolean, Toolchain_Version_For);

      --  Start of processing for Write_Toolchain_Version

      begin
         Toolchain_Version_For (For_Project, Project_Tree, Dummy);

         if For_Project.Qualifier = Aggregate_Library then
            For_Imported (For_Project, Project_Tree, Dummy);
         end if;
      end Write_Toolchain_Version;

      -------------------------------
      -- Write_Interface_Dep_Files --
      -------------------------------

      procedure Write_Interface_Dep_Files is
         Key : FNHS.Key_Option;

         function Interface_ALI return File_Name_Type
         is (Key.K);

      begin
         Put_Line (Exchange_File, Library_Label (Interface_Dep_Files));

         Key := FNHS.Get_First_Key (Complete_Interface_ALIs);

         while Key.Present loop
            --  Find the source to get the absolute path of the ALI file

            declare
               Next_Proj : Project_Id;
               Iter      : Source_Iterator;
            begin
               Next_Proj := For_Project.Extends;

               if For_Project.Qualifier = Aggregate_Library then
                  Iter := For_Each_Source (Project_Tree);
               else
                  Iter := For_Each_Source (Project_Tree, For_Project);
               end if;

               loop
                  while GPR.Element (Iter) /= No_Source
                    and then
                    (GPR.Element (Iter).Unit = null
                     or else GPR.Element (Iter).Dep_Name /= Interface_ALI)
                  loop
                     Next (Iter);
                  end loop;

                  Source := GPR.Element (Iter);
                  exit when Source /= No_Source
                    or else Next_Proj = No_Project;

                  Iter := For_Each_Source (Project_Tree, Next_Proj);
                  Next_Proj := Next_Proj.Extends;
               end loop;

               if Source /= No_Source then
                  if Source.Kind = Sep then
                     Source := No_Source;

                  elsif Source.Kind = Spec
                    and then Other_Part (Source) /= No_Source
                  then
                     Source := Other_Part (Source);
                  end if;
               end if;

               if Source /= No_Source then
                  if Source.Project /= Project
                    and then not Is_Extending (For_Project, Source.Project)
                    and then For_Project.Qualifier /= Aggregate_Library
                  then
                     Source := No_Source;
                  end if;
               end if;

               if Source /= No_Source then
                  Put_Line (Exchange_File, Get_Name_String (Source.Dep_Path));
               end if;
            end;

            Key := FNHS.Get_Next_Key (Complete_Interface_ALIs);
         end loop;
      end Write_Interface_Dep_Files;

      ----------------------------
      -- Write_Other_Interfaces --
      ----------------------------

      procedure Write_Other_Interfaces is
         Interfaces : String_List_Id := For_Project.Other_Interfaces;
         Element    : String_Element;
      begin
         Put_Line (Exchange_File, Library_Label (Other_Interfaces));

         while Interfaces /= Nil_String loop
            Element := Project_Tree.Shared.String_Elements.Table (Interfaces);
            Put_Line (Exchange_File, Get_Name_String (Element.Value));
            Interfaces := Element.Next;
         end loop;
      end Write_Other_Interfaces;

      -------------------------------
      -- Write_Interface_Obj_Files --
      -------------------------------

      procedure Write_Interface_Obj_Files is
         List      : String_List_Id := For_Project.Other_Interfaces;
         Element   : String_Element;
         Other_Int : Boolean := False;
         Key       : FNHS.Key_Option;

         function Interface_Dep return File_Name_Type
         is (Key.K);

         function Base_Name (Name : Name_Id) return String;
         --  File name without path nor extension

         procedure Find_Source;
         --  Find the source corresponding to Interface_Dep (when Other_Int is
         --  False) or Element.Value (when Other_Int is True).

         ---------------
         -- Base_Name --
         ---------------

         function Base_Name (Name : Name_Id) return String is
            N : constant String := Get_Name_String (Name);
         begin
            return Base_Name (N, File_Extension (N));
         end Base_Name;

         -----------------
         -- Find_Source --
         -----------------

         procedure Find_Source is
            Next_Proj : Project_Id;
            Iter      : Source_Iterator;
         begin
            Next_Proj := For_Project.Extends;

            if For_Project.Qualifier = Aggregate_Library then
               Iter := For_Each_Source (Project_Tree);
            else
               Iter := For_Each_Source (Project_Tree, For_Project);
            end if;

            loop
               --  Look for the Source_Id corresponding to this unit

               while GPR.Element (Iter) /= No_Source
                 and then
               --  Either an foreign language, we need the
               --  implementation of this unit.
                 ((Other_Int
                   and then
                     (Base_Name (Name_Id (GPR.Element (Iter).Object)) /=
                          Base_Name (Element.Value)
                      or else GPR.Element (Iter).Kind = Spec
                      or else GPR.Element (Iter).Locally_Removed))
               --  Or and Ada unit, we need the dependency file
                  or else
                    (not Other_Int and then
                         (GPR.Element (Iter).Unit = null
                          or else GPR.Element (Iter).Dep_Name /=
                                Interface_Dep)))
               loop
                  Next (Iter);
               end loop;

               Source := GPR.Element (Iter);

               exit when Source /= No_Source
                 or else Next_Proj = No_Project;

               Iter := For_Each_Source (Project_Tree, Next_Proj);
               Next_Proj := Next_Proj.Extends;
            end loop;

            if Source /= No_Source then
               if Source.Kind = Sep then
                  Source := No_Source;

               elsif Source.Kind = Spec
                 and then Other_Part (Source) /= No_Source
               then
                  Source := Other_Part (Source);
               end if;
            end if;

            if Source /= No_Source
              and then Source.Project /= Project
              and then not Is_Extending (For_Project, Source.Project)
              and then For_Project.Qualifier /= Aggregate_Library
            then
               Source := No_Source;
            end if;

            if Source /= No_Source then
               Put_Line (Exchange_File, Get_Name_String (Source.Object_Path));
            end if;
         end Find_Source;

      begin
         Put_Line (Exchange_File, Library_Label (Interface_Obj_Files));

         --  First the Ada sources

         Other_Int := False;
         Key := FNHS.Get_First_Key (Complete_Interface_ALIs);

         while Key.Present loop
            Find_Source;
            Key := FNHS.Get_Next_Key (Complete_Interface_ALIs);
         end loop;

         --  Then the foreign language objects

         Other_Int := True;

         while List /= Nil_String loop
            Element := Project_Tree.Shared.String_Elements.Table (List);
            Find_Source;
            List := Element.Next;
         end loop;
      end Write_Interface_Obj_Files;

      -------------------
      -- Write_Sources --
      -------------------

      procedure Write_Sources is
      begin
         Put_Line (Exchange_File, Library_Label (Sources));

         --  Copy the path of the sources

         Project := For_Project;

         while Project /= No_Project loop
            Iter := For_Each_Source (Project_Tree, Project);

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

               if not Source.Locally_Removed
                 and then Source.Replaced_By = No_Source
               then
                  Put_Line
                    (Exchange_File,
                     Get_Name_String (Source.Path.Display_Name));
               end if;

               Next (Iter);
            end loop;

            Project := Project.Extends;
         end loop;
      end Write_Sources;

      --------------------------
      -- Write_Response_Files --
      --------------------------

      procedure Write_Response_Files is
      begin
         if For_Project.Config.Max_Command_Line_Length > 0
           and then For_Project.Config.Resp_File_Format /= None
         then
            Put_Line
              (Exchange_File,
               Library_Label (Max_Command_Line_Length) & ASCII.LF
               & For_Project.Config.Max_Command_Line_Length'Img & ASCII.LF
               & Library_Label (Gprexch.Response_File_Format) & ASCII.LF
               & For_Project.Config.Resp_File_Format'Img);

            if For_Project.Config.Resp_File_Options /= No_Name_List then
               Write_Name_List
                 (Response_File_Switches,
                  For_Project.Config.Resp_File_Options);
            end if;
         end if;
      end Write_Response_Files;

   --  Start of processing for Build_Library

   begin
      --  Check if there is an object directory

      if For_Project.Object_Directory.Display_Name = No_Path then
         Fail_Program
           (Project_Tree,
            "no object directory for library project "
            & Get_Name_String (For_Project.Display_Name));
      end if;

      --  Check consistency and build environment

      if For_Project.Config.Lib_Support = None then
         Fail_Program
           (Project_Tree, "library projects not supported on this platform");

      elsif not Is_Static (For_Project)
        and then For_Project.Config.Lib_Support /= Full
      then
         Fail_Program
           (Project_Tree,
            "shared library projects not supported on this platform");

      elsif not For_Project.Config.Lib_Encapsulated_Supported
        and then For_Project.Standalone_Library = Encapsulated
      then
         Fail_Program
           (Project_Tree,
            "encapsulated library projects not supported on this platform");
      end if;

      if For_Project.Config.Library_Builder = No_Path then
         Fail_Program (Project_Tree, "no library builder specified");
      end if;

      Library_Builder :=
        Locate_Exec_On_Path
          (Get_Name_String (For_Project.Config.Library_Builder));

      if Library_Builder = null then
         Fail_Program
           (Project_Tree,
            "could not locate library builder """
            & Get_Name_String (For_Project.Config.Library_Builder) & '"');
      end if;

      Library_Builder_Name :=
        new String'(Ada.Directories.Base_Name (Library_Builder.all));

      if Opt.CodePeer_Mode then
         null;
      elsif Is_Static (For_Project) then
         Check_Archive_Builder;

      elsif For_Project.Standalone_Library /= No then
         Check_Object_Lister;
         Check_Export_File;
         Check_Library_Symbol_File;
      end if;

      Library_Needs_To_Be_Built :=
        Opt.Force_Compilations or else For_Project.Need_Build;

      if not Library_Needs_To_Be_Built and then
         Opt.Verbosity_Level > Opt.Low
      then
         Put ("   Checking library ");
         Put (Get_Name_String (For_Project.Library_Name));
         Put_Line (" ...");
      end if;

      Get_Objects;

      --  Work occurs in the object directory

      Change_To_Object_Directory (For_Project);

      --  Get the name of the library exchange file

      Get_Name_String (For_Project.Library_Name);
      Add_Str_To_Name_Buffer (Library_Exchange_Suffix);
      Exchange_File_Name := new String'(Name_Buffer (1 .. Name_Len));

      if not Library_Needs_To_Be_Built then
         declare
            TS : constant Time_Stamp_Type :=
                   File_Stamp (Exchange_File_Name.all);
         begin
            if String (TS) < String (Latest_Object_TS) then
               Library_Needs_To_Be_Built := True;

               if Opt.Verbosity_Level > Opt.Low then
                  if TS = Empty_Time_Stamp then
                     Put ("      -> library exchange file ");
                     Put (Exchange_File_Name.all);
                     Put_Line (" does not exist");

                  else
                     Put
                       ("      -> object files more recent than library"
                        & " exchange file ");
                     Put_Line (Exchange_File_Name.all);
                  end if;
               end if;

            else
               begin
                  Open (Exchange_File, In_File, Exchange_File_Name.all);

                  if End_Of_File (Exchange_File) then
                     if Opt.Verbosity_Level > Opt.Low then
                        Put ("      -> library exchange file """);
                        Put (Exchange_File_Name.all);
                        Put_Line (""" is empty");
                     end if;

                     Library_Needs_To_Be_Built := True;
                  end if;

               exception
                  when others =>
                     if Opt.Verbosity_Level > Opt.Low then
                        Put ("      -> library exchange file """);
                        Put (Exchange_File_Name.all);
                        Put_Line (""" cannot be open");
                     end if;

                     Library_Needs_To_Be_Built := True;
               end;
            end if;
         end;
      end if;

      if not Library_Needs_To_Be_Built then
         --  The exchange file is open in input

         --  Get the path of the library file that should be the first field

         Get_Line (Exchange_File, Name_Buffer, Name_Len);

         if Name_Buffer (1 .. Name_Len) /= Library_Label (Library_Path) then
            Library_Needs_To_Be_Built := True;
            Close (Exchange_File);

            if Opt.Verbosity_Level > Opt.Low then
               Put ("      -> library exchange file ");
               Put (Exchange_File_Name.all);
               Put_Line (" has wrong format");
            end if;

         else
            Get_Line (Exchange_File, Name_Buffer, Name_Len);

            declare
               Lib_File_Name     : constant String :=
                                     Base_Name (Name_Buffer (1 .. Name_Len));
               Shared_Lib_Prefix : String_Access := new String'("lib");
               Shared_Lib_Suffix : String_Access := new String'(".so");
               Archive_Suffix    : String_Access := new String'(".a");
            begin
               if Is_Static (For_Project) then
                  if For_Project.Config.Archive_Suffix /= No_File then
                     Archive_Suffix :=
                       new String'
                         (Get_Name_String (For_Project.Config.Archive_Suffix));
                  end if;

                  Expected_File_Name :=
                    new String'
                      ("lib" &
                       Get_Name_String (For_Project.Library_Name) &
                       Archive_Suffix.all);
               else
                  if For_Project.Config.Shared_Lib_Prefix /= No_File then
                     Shared_Lib_Prefix :=
                       new String'
                         (Get_Name_String
                            (For_Project.Config.Shared_Lib_Prefix));
                  end if;

                  if For_Project.Config.Shared_Lib_Suffix /= No_File then
                     Shared_Lib_Suffix :=
                       new String'
                         (Get_Name_String
                            (For_Project.Config.Shared_Lib_Suffix));
                  end if;

                  Expected_File_Name :=
                    new String'
                      (Shared_Lib_Prefix.all &
                         Get_Name_String (For_Project.Library_Name) &
                           Shared_Lib_Suffix.all);
               end if;

               if Lib_File_Name /= Expected_File_Name.all then
                  Library_Needs_To_Be_Built := True;
                  Close (Exchange_File);

                  if  Opt.Verbosity_Level > Opt.Low then
                     Put_Line ("      -> incorrect library file name");
                     Put_Line ("   expected " & Expected_File_Name.all);
                     Put_Line ("     actual " & Lib_File_Name);
                  end if;
               end if;
            end;

            if not Library_Needs_To_Be_Built then
               For_Project.Library_TS :=
                 File_Stamp (Name_Buffer (1 .. Name_Len));

               if For_Project.Library_TS < Latest_Object_TS then
                  Library_Needs_To_Be_Built := True;
                  Close (Exchange_File);

                  if Opt.Verbosity_Level > Opt.Low then
                     Put_Line
                       ("      -> "
                        & "object file(s) more recent than library file "
                        & Exchange_File_Name.all);
                  end if;
               end if;
            end if;
         end if;
      end if;

      if not Library_Needs_To_Be_Built then
         --  The next line should be the object file label, followed by the
         --  object paths and time stamps.

         Get_Line (Exchange_File, Name_Buffer, Name_Len);

         if Name_Buffer (1 .. Name_Len) /= Library_Label (Object_Files) then
            Library_Needs_To_Be_Built := True;

            if Opt.Verbosity_Level > Opt.Low then
               Put_Line
                 ("      -> library exchange file " & Exchange_File_Name.all
                  & " has wrong format");
            end if;
         end if;

         while not Library_Needs_To_Be_Built
           and then not End_Of_File (Exchange_File)
         loop
            Get_Line (Exchange_File, Name_Buffer, Name_Len);

            exit when Name_Buffer (1) = '[';

            Object_Path := Name_Find;

            Library_Needs_To_Be_Built := True;

            if End_Of_File (Exchange_File) then
               if Opt.Verbosity_Level > Opt.Low then
                  Put_Line
                    ("      -> library exchange file " & Exchange_File_Name.all
                     & " has wrong format");
               end if;

            else
               Get_Line (Exchange_File, Name_Buffer, Name_Len);

               if Name_Len = Time_Stamp_Length then
                  Object_TS :=
                    Time_Stamp_Type (Name_Buffer (1 .. Name_Len));

                  Path_Found := False;

                  declare
                     Elem : Library_Object;
                     Cursor : Objects.Cursor := Library_Objs.First;
                     use Objects;
                  begin
                     --  Look in the Library_Objs set. If the path name is in
                     --  the set, indicate that it has been found.
                     --  The library need to be re-build if the time stamp is
                     --  different in the set.

                     while Cursor /= No_Element loop
                        Elem := Element (Cursor);

                        if Object_Path = Elem.Path then
                           Path_Found := True;
                           Library_Needs_To_Be_Built := Object_TS /= Elem.TS;
                           Elem.Known := True;
                           Library_Objs.Replace_Element (Cursor, Elem);
                           exit;
                        end if;

                        Next (Cursor);
                     end loop;
                  end;

                  --  If the object file is not found, it may be that the path
                  --  in the library is the same as the path of the object
                  --  files, but with different symbolic links. So, we try
                  --  again resolving the symbolic links.

                  if not Path_Found then
                     declare
                        Norm_Path : constant String :=
                          Normalize_Pathname
                            (Get_Name_String (Object_Path),
                             Resolve_Links => Opt.Follow_Links_For_Dirs);
                        Elem : Library_Object;
                        Cursor : Objects.Cursor := Library_Objs.First;
                        use Objects;

                     begin
                        while Cursor /= No_Element loop
                           Elem := Element (Cursor);
                           if Norm_Path =
                             Normalize_Pathname
                               (Get_Name_String (Elem.Path),
                                Resolve_Links => Opt.Follow_Links_For_Dirs)
                           then
                              Library_Needs_To_Be_Built :=
                                Object_TS /= Elem.TS;
                              Elem.Known := True;
                              Library_Objs.Replace_Element (Cursor, Elem);
                              exit;
                           end if;

                           Next (Cursor);
                        end loop;
                     end;
                  end if;

                  if Library_Needs_To_Be_Built and then
                    Opt.Verbosity_Level > Opt.Low
                  then
                     Put ("      -> object file ");
                     Put (Get_Name_String (Object_Path));
                     Put_Line (" does not exist or have wrong time stamp");
                  end if;

               else
                  if Opt.Verbosity_Level > Opt.Low then
                     Put_Line
                       ("      -> library exchange file "
                        & Exchange_File_Name.all & " has wrong format");
                  end if;
               end if;
            end if;
         end loop;

         --  Check a possible different library version

         if not Library_Needs_To_Be_Built
           and then not Is_Static (For_Project)
           and then For_Project.Config.Symbolic_Link_Supported
           and then not End_Of_File (Exchange_File)
           and then Name_Buffer (1 .. Name_Len) =
                      Library_Label (Gprexch.Library_Version)
         then
            Get_Line (Exchange_File, Name_Buffer, Name_Len);

            if Name_Buffer (1 .. Name_Len) /=
                 Get_Name_String (For_Project.Lib_Internal_Name)
            then
               Library_Needs_To_Be_Built := True;

               if Opt.Verbosity_Level > Opt.Low then
                  Put_Line ("      -> different library version");
               end if;
            end if;
         end if;

         Close (Exchange_File);

         if not Library_Needs_To_Be_Built then
            declare
               Cursor : Objects.Cursor := Library_Objs.First;
               Elem : Library_Object;
               use Objects;
            begin
               while Cursor /= No_Element loop
                  Elem := Element (Cursor);
                  if not Elem.Known then
                     Library_Needs_To_Be_Built := True;

                     if Opt.Verbosity_Level > Opt.Low then
                        Put
                          ("      -> library was built without object file ");
                        Put_Line (Get_Name_String (Elem.Path));
                     end if;

                     exit;
                  end if;

                  Next (Cursor);
               end loop;
            end;
         end if;
      end if;

      if not Library_Needs_To_Be_Built then
         --  Check if in a project imported directly or indirectly the time
         --  stamp of a library is greater than the time stamp of this library.

         declare
            List    : Project_List;
            Proj2   : Project_Id;

            Lib_Timestamp1 : constant Time_Stamp_Type :=
                               For_Project.Library_TS;
         begin
            List := For_Project.All_Imported_Projects;
            while List /= null loop
               Proj2 := List.Project;

               Wait_For_Dependency (Proj2);

               if not Library_Needs_To_Be_Built
                 and then Proj2.Library
                 and then (Proj2.Was_Built
                           or else Lib_Timestamp1 < Proj2.Library_TS)
               then
                  Library_Needs_To_Be_Built := True;

                  if Opt.Verbosity_Level > Opt.Low then
                     Put_Line
                       ("      -> library file for project "
                        & Get_Name_String (Proj2.Display_Name)
                        & " is more recent than library file for project "
                        & Get_Name_String (For_Project.Display_Name));
                  end if;
               end if;

               exit when Library_Needs_To_Be_Built
                         and then Libs_Are_Building.Is_Empty;

               List := List.Next;
            end loop;

            Dependencies_Ready := True;
         end;
      end if;

      if not Library_Needs_To_Be_Built then
         if Opt.Verbosity_Level > Opt.Low then
            if For_Project = Main_Project then
               Put ('"');
               Put (Expected_File_Name.all);
               Put_Line (""" up to date");

            else
               Put_Line ("      -> up to date");
            end if;
         end if;

      else
         --  The current directory is already the correct object directory.
         --  However, we call again Change_To_Object_Directory with
         --  Must_Be_Writable set to True, to check if the object directory
         --  is writable and to fail graciously if it not.

         Change_To_Object_Directory (For_Project, Must_Be_Writable => True);

         --  Create the library exchange file

         begin
            Create (Exchange_File, Out_File, Exchange_File_Name.all);

         exception
            when others =>
               Fail_Program
                 (Project_Tree,
                  "unable to create library exchange file "
                  & Exchange_File_Name.all);
         end;

         if Opt.CodePeer_Mode then
            Put_Line (Exchange_File, Library_Label (CodePeer_Mode));
         end if;

         if Opt.Quiet_Output then
            Put_Line (Exchange_File, Library_Label (Quiet));

         elsif Opt.Verbose_Mode then
            if Opt.Verbosity_Level = Opt.Low then
               Put_Line (Exchange_File, Library_Label (Verbose_Low));
            else
               Put_Line (Exchange_File, Library_Label (Verbose_Higher));
            end if;
         end if;

         if No_SAL_Binding then
            Put_Line (Exchange_File, Library_Label (Gprexch.No_SAL_Binding));
         end if;

         Write_Object_Files;

         --  Library name

         Put_Line
           (Exchange_File,
            Library_Label (Library_Name) & ASCII.LF
            & Get_Name_String (For_Project.Library_Name));

         --  Library version

         Write_Name (Library_Version, For_Project.Lib_Internal_Name);

         --  Library directory

         Put_Line
           (Exchange_File,
            Library_Label (Library_Directory) & ASCII.LF
            & Get_Name_String (For_Project.Library_Dir.Display_Name) & ASCII.LF

            --  Project directory
            & Library_Label (Project_Directory) & ASCII.LF
            & Get_Name_String (For_Project.Directory.Display_Name));

         if For_Project.Library_ALI_Dir /= No_Path_Information
           and then
             For_Project.Library_ALI_Dir.Name /= For_Project.Library_Dir.Name
         then
            Put_Line
              (Exchange_File,
               Library_Label (Library_Dependency_Directory) & ASCII.LF
               & Get_Name_String (For_Project.Library_ALI_Dir.Display_Name));
         end if;

         Write_Object_Directory;

         Write_Compilers;

         Write_Compiler_Leading_Switches;

         Write_Compiler_Trailing_Switches;

         Write_Partial_Linker;

         if No_Create then
            Put_Line (Exchange_File, Library_Label (Gprexch.No_Create));
         end if;

         if Opt.CodePeer_Mode then
            Put_Line (Exchange_File, Library_Label (Gprexch.CodePeer_Mode));

         elsif Is_Static (For_Project) then
            Put_Line
              (Exchange_File, Library_Label (Static) & ASCII.LF
               & Library_Label (Archive_Builder) & ASCII.LF
               & Archive_Builder_Path.all);

            for Opt of Archive_Builder_Opts loop
               Put_Line (Exchange_File, Opt.Name);
            end loop;

            if not Archive_Builder_Append_Opts.Is_Empty then
               Put_Line
                 (Exchange_File,
                  Library_Label (Archive_Builder_Append_Option));

               for Option of Archive_Builder_Append_Opts loop
                  Put_Line (Exchange_File, Option.Name);
               end loop;
            end if;

            Write_Filename (Archive_Suffix, For_Project.Config.Archive_Suffix);

            if Archive_Indexer_Path /= null then
               Put_Line
                 (Exchange_File,
                  Library_Label (Archive_Indexer) & ASCII.LF
                  & Archive_Indexer_Path.all);

               for Option of Archive_Indexer_Opts loop
                  Put_Line (Exchange_File, Option.Name);
               end loop;
            end if;

         else
            --  Driver_Name

            Write_Filename (Driver_Name, For_Project.Config.Shared_Lib_Driver);

            --  Shared_Lib_Prefix

            Write_Filename
              (Shared_Lib_Prefix, For_Project.Config.Shared_Lib_Prefix);

            --  Shared_Lib_Suffix

            Write_Filename
              (Shared_Lib_Suffix, For_Project.Config.Shared_Lib_Suffix);

            Write_Shared_Lib_Minimum_Options;

            Write_Library_Version;

            --  Symbolic_Link_Supported

            if For_Project.Config.Symbolic_Link_Supported then
               Put_Line
                 (Exchange_File, Library_Label (Symbolic_Link_Supported));
            end if;

            --  Major_Minor_Id_Supported

            if For_Project.Config.Lib_Maj_Min_Id_Supported then
               Put_Line
                 (Exchange_File, Library_Label (Major_Minor_Id_Supported));
            end if;

            Process_Imported_Libraries
              (For_Project, There_Are_SALs => Disregard);

            Write_Runtime_Library_Dir;

            --  Relocatable

            Put_Line (Exchange_File, Library_Label (Relocatable));

            --  Auto_init

            Write_Auto_Init;

            --  Gprexch.Install_Name

            if Opt.Run_Path_Option then
               Write_Name
                 (Gprexch.Install_Name,
                  For_Project.Config.Library_Install_Name_Option);
            end if;

            Write_Run_Path_Option;

            Write_Leading_Library_Options;

            Write_Library_Rpath_Options;

            Write_Imported_Libraries;
         end if;

         Write_Binding_Options;

         Write_Library_Options (Library_Options_Success);

         Write_Dependency_Files;

         Write_Mapping_File;

         Write_Toolchain_Version;

         if For_Project.Standalone_Library /= No then
            if For_Project.Lib_Auto_Init then
               Put_Line (Exchange_File, Library_Label (Auto_Init));
            end if;

            Write_Interface_Dep_Files;

            if For_Project.Other_Interfaces /= Nil_String then
               Write_Other_Interfaces;
            end if;

            if For_Project.Library_Src_Dir /= No_Path_Information then
               --  Copy_Source_Dir

               Put_Line
                 (Exchange_File, Library_Label (Copy_Source_Dir) & ASCII.LF
                  & Get_Name_String
                      (For_Project.Library_Src_Dir.Display_Name));

               Write_Sources;
            end if;

            --  Standalone mode

            Put_Line
              (Exchange_File, Library_Label (Standalone_Mode) & ASCII.LF
               & Standalone'Image (For_Project.Standalone_Library));

            if For_Project.Symbol_Data.Symbol_Policy = Restricted then
               if Library_Symbol_File /= null then
                  Put_Line
                    (Exchange_File,
                     Library_Label (Gprexch.Library_Symbol_File) & ASCII.LF
                     & Library_Symbol_File.all);

               elsif Object_Lister_Path /= null then
                  --  Write interface objects

                  Write_Interface_Obj_Files;

                  --  Write object lister

                  Put_Line
                    (Exchange_File,
                     Library_Label (Object_Lister) & ASCII.LF
                     & Object_Lister_Path.all);

                  for Option of Object_Lister_Opts loop
                     Put_Line (Exchange_File, Option.Name);
                  end loop;

                  Put_Line
                    (Exchange_File,
                     Library_Label (Gprexch.Object_Lister_Matcher) & ASCII.LF
                     & Object_Lister_Matcher.all);
               end if;

               if Export_File_Switch /= null then
                  --  Write export symbols format

                  Put_Line
                    (Exchange_File,
                     Library_Label (Export_File) & ASCII.LF
                     & GPR.Export_File_Format'Image (Export_File_Format)
                     & ASCII.LF
                     & Export_File_Switch.all);
               end if;
            end if;

         elsif For_Project.Other_Interfaces /= Nil_String then
            Write_Other_Interfaces;
         end if;

         Write_Response_Files;

         if Opt.Keep_Temporary_Files then
            Put_Line (Exchange_File, Library_Label (Keep_Temporary_Files));
         end if;

         if Build_Script_Name /= null then
            Put_Line
              (Exchange_File,
               Library_Label (Script_Path) & ASCII.LF
               & Build_Script_Name.all);
         end if;

         Close (Exchange_File);

         declare
            Arguments : constant Argument_List := (1 => Exchange_File_Name);
            Success   : Boolean;

         begin
            if Library_Options_Success then
               if not Opt.Quiet_Output then
                  if Opt.Verbose_Mode then
                     Put_Line
                       (Library_Builder.all & " " & Exchange_File_Name.all);

                  else
                     Display
                       (Section  => Build_Libraries,
                        Command  => Library_Builder_Name.all,
                        Argument => Exchange_File_Name.all);
                  end if;
               end if;

               if not Dependencies_Ready then
                  declare
                     L : Project_List := For_Project.All_Imported_Projects;
                  begin
                     while L /= null loop
                        Wait_For_Dependency (L.Project);
                        L := L.Next;
                     end loop;
                  end;
               end if;

               Wait_For_Slots_Less_Than (Opt.Maximum_Binders);

               if Stop_Spawning then
                  return;
               end if;

               declare
                  Pid : Process_Id;
                  MI  : Main_Info;
               begin
                  Pid := Non_Blocking_Spawn (Library_Builder.all, Arguments);

                  Success := Pid /= Invalid_Pid;

                  if Success then
                     MI.File    := No_File;
                     MI.Project := For_Project;
                     MI.Tree    := Project_Tree;

                     Libs_Are_Building.Insert (For_Project.Name);
                     Add_Process (Pid, (Binding, MI));

                     Display_Processes ("bind");
                  end if;
               end;

            else
               Success := False;
            end if;

            if not Success then
               Fail_Program
                 (Project_Tree,
                  "could not build library for project " & Project_Name);
            end if;
         end;
      end if;

      --  Restore the current working directory to its previous value

      Change_Dir (Current_Dir);
   end Build_Library;

   ----------------------------
   -- Emit_Compiler_Switches --
   ----------------------------

   procedure Emit_Compiler_Switches
     (Exchange_File : Text_IO.File_Type; Index : Name_List_Index)
   is
      Node           : Name_Node;
      List           : Name_List_Index := Index;
      Previous_Was_X : Boolean := False;

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

         declare
            Arg : constant String := Get_Name_String (Node.Name);
         begin
            if Opt.CodePeer_Mode then
               if Previous_Was_X then
                  Put_Line (Exchange_File, "adascil");

               --  Strip target specific -m switches in CodePeer mode.

               elsif Arg'Length <= 2 or else Arg (1 .. 2) /= "-m" then
                  Put_Line (Exchange_File, Arg);
               end if;
            else
               Put_Line (Exchange_File, Arg);
            end if;

            Previous_Was_X := Arg = "-x";
         end;

         List := Node.Next;
      end loop;
   end Emit_Compiler_Switches;

   -----------------------------------
   -- Is_Included_In_Global_Archive --
   -----------------------------------

   function Is_Included_In_Global_Archive
     (Object_Name : File_Name_Type;
      Project     : Project_Id) return Boolean
   is
      Proj   : Project_Id;
      Source : Source_Id;
      Iter   : Source_Iterator;

   begin
      --  If a source is overriden in an extending project, then the object
      --  file is not included in the global archive.

      Proj := Project.Extended_By;
      while Proj /= No_Project loop
         Iter := For_Each_Source (Project_Tree, Proj);
         loop
            Source := GPR.Element (Iter);
            exit when Source = No_Source;

            if Object_To_Global_Archive (Source)
              and then Source.Object = Object_Name
            then
               return False;
            end if;

            Next (Iter);
         end loop;
         Proj := Proj.Extended_By;
      end loop;

      Iter := For_Each_Source (Project_Tree, Project);

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

         if Object_To_Global_Archive (Source)
           and then Source.Object =  Object_Name
         then
            return Source.Language.Config.Objects_Linked;
         end if;

         Next (Iter);
      end loop;

      return True;
   end Is_Included_In_Global_Archive;

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

   procedure Run is

      Main : Main_Info;

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

      -------------
      -- Do_Post --
      -------------

      procedure Do_Post (Project : Project_Id; Tree : Project_Tree_Ref) is
      begin
         if Builder_Data (Tree).Need_Binding
           and then not Stop_Spawning
         then
            Post_Compilation_Phase (Project, Tree);
         end if;
      end Do_Post;

      procedure Post_Compile_All is new For_Project_And_Aggregated (Do_Post);

   begin
      Clear_Time_Stamp_Cache;
      Libs_Are_Building.Clear;
      Outstanding_Processes := 0;
      Stop_Spawning := False;

      if Main_Project.Qualifier = Aggregate_Library then
         --  For an aggregate library we do not want to build separate
         --  libraries if any, this means that at this point we want to
         --  handle only the main aggregate library project.

         if Builder_Data (Project_Tree).Need_Binding then
            Post_Compilation_Phase (Main_Project, Project_Tree);
         end if;

      else
         Post_Compile_All (Main_Project, Project_Tree);
      end if;

      Wait_For_Slots_Less_Than (1);

      if Bad_Processes.Length = 1 then
         Main := Bad_Processes.First_Element;
         Fail_Program
           (Main.Tree,
            (if Main.File = No_File -- It was gprlib call
             then "could not build library for project "
                  & Get_Name_String (Main.Project.Name)
             else "unable to bind " & Get_Name_String (Main.File)));

      elsif not Bad_Processes.Is_Empty then
         for Main of Bad_Processes loop
            Put ("   binding of ");
            Put
              (if Main.File = No_File -- gprlib call
               then Get_Name_String (Main.Project.Name)
               else Get_Name_String (Main.File));
            Put_Line (" failed");
         end loop;

         Fail_Program (Bad_Processes.Last_Element.Tree,
                       "*** post compilation phase failed");
      end if;
   end Run;

   ----------------------------
   -- Post_Compilation_Phase --
   ----------------------------

   procedure Post_Compilation_Phase
     (Main_Project : Project_Id; Project_Tree : Project_Tree_Ref)
   is
      use Ada.Calendar;

      Exchange_File : Text_IO.File_Type;
      Line          : String (1 .. 1_000);
      Last          : Natural;

      Proj_List : Project_List;

      Shared_Libs : Boolean := False;

      Bind_Exchange_TS                 : Time;
      Bind_Object_TS                   : Time;
      Binder_Driver_Needs_To_Be_Called : Boolean := False;

      function Get_Project_Checksum
        (Project : Project_Id) return Message_Digest;
      --  Calculate checksum of the Project variables and attributes

      There_Are_Stand_Alone_Libraries : Boolean := False;
      --  Set to True if there are SALS in the project tree

      procedure Bind_Language
        (Main_Proj            : Project_Id;
         Main                 : String;
         Main_Base_Name_Index : File_Name_Type;
         Main_File            : Main_Info;
         Main_Id              : File_Name_Type;
         B_Data               : Binding_Data);
      --  Do the "binding" phase for the language describeb in B_Data

      procedure Add_Dependency_Files
        (For_Project : Project_Id;
         Language    : Language_Ptr;
         Main_Source : Source_Id;
         Dep_Files   : out Boolean);
      --  Put the dependency files of the project in the binder exchange file

      --------------------------
      -- Add_Dependency_Files --
      --------------------------

      procedure Add_Dependency_Files
        (For_Project : Project_Id;
         Language    : Language_Ptr;
         Main_Source : Source_Id;
         Dep_Files   : out Boolean)
      is
         Config : constant Language_Config := Language.Config;
         Roots  : Roots_Access;
         Iter   : Source_Iterator;

         procedure Put_Dependency_File (Source : Source_Id);
         --  Put in the exchange file the dependency file path name for source
         --  Source, if applicable.

         -------------------------
         -- Put_Dependency_File --
         -------------------------

         procedure Put_Dependency_File (Source : Source_Id) is
         begin
            if Source.Language.Name = Language.Name
              and then
                ((Config.Kind = File_Based and then Source.Kind = Impl)
                 or else
                   (Config.Kind = Unit_Based
                    and then Source.Unit not in No_Unit_Index
                                              | Main_Source.Unit
                    and then (Source.Kind = Impl
                              or else Other_Part (Source) = No_Source)
                    and then not Is_Subunit (Source)))
              and then Is_Included_In_Global_Archive
                         (Source.Object, Source.Project)
            then
               if Source.Project = For_Project
                 or not Source.Project.Library
                 or Config.Kind = File_Based
               then
                  Add_Dep (Get_Name_String (Source.Dep_Path));
                  Dep_Files := True;

               elsif Source.Project.Standalone_Library = No then
                  Get_Name_String
                    (Source.Project.Library_ALI_Dir.Display_Name);
                  Get_Name_String_And_Append (Name_Id (Source.Dep_Name));
                  Add_Dep (Name_Buffer (1 .. Name_Len));
                  Dep_Files := True;
               end if;
            end if;
         end Put_Dependency_File;

      --  Start of processing for Add_Dependency_Files

      begin
         Dep_Files := False;

         Roots := Main_Source.Roots;

         if Roots = null then
            if Main_Source.Unit = No_Unit_Index then
               if Main_Project.Qualifier = Aggregate_Library then
                  Iter := For_Each_Source (Project_Tree);
               else
                  Iter := For_Each_Source
                    (Project_Tree, Encapsulated_Libs => False);
               end if;

               while GPR.Element (Iter) /= No_Source loop
                  Initialize_Source_Record (GPR.Element (Iter));

                  --  Do not bind the non compilable sources, such as those
                  --  that have been locally removed.

                  if Is_Compilable (GPR.Element (Iter)) then
                     Put_Dependency_File (GPR.Element (Iter));
                  end if;

                  Next (Iter);
               end loop;
            end if;

         else
            --  Put the Roots
            while Roots /= null loop
               if Roots.Root /= No_Source then
                  Put_Dependency_File (Roots.Root);
               end if;

               Roots := Roots.Next;
            end loop;
         end if;
      end Add_Dependency_Files;

      -------------------
      -- Bind_Language --
      -------------------

      procedure Bind_Language
        (Main_Proj            : Project_Id;
         Main                 : String;
         Main_Base_Name_Index : File_Name_Type;
         Main_File            : Main_Info;
         Main_Id              : File_Name_Type;
         B_Data               : Binding_Data)
      is
         subtype Project_Check_String is String
           (1 .. Time_Stamp_Type'Length + 1 + Message_Digest'Length);
         --  Project timestamp and checksum of the project variables with a
         --  space in between.

         Empty_Check_String : constant Project_Check_String :=
                                (others => ASCII.NUL);

         type Project_Check_Line is record
            Project : Project_Id;
            Line    : Project_Check_String := Empty_Check_String;
         end record;

         function Hash (Item : Path_Name_Type) return Ada.Containers.Hash_Type
         is (Ada.Containers.Hash_Type'Mod (Item));

         function File_Stamp (File : Path_Name_Type) return Time is
           (File_Time_Stamp (Get_Name_String (File)));
         --  Returns file modification time

         package Project_File_Paths is new Ada.Containers.Hashed_Maps
           (Key_Type        => Path_Name_Type,
            Element_Type    => Project_Check_Line,
            Hash            => Hash,
            Equivalent_Keys => "=");

         function Get_Project_Checkline
           (Project : Project_Id) return Project_Check_String;
         --  Returns project check line either from Projects container or
         --  calculate it if absent.

         Projects     : Project_File_Paths.Map;
         Project_Path : Path_Name_Type;
         Position     : Project_File_Paths.Cursor;
         Counter      : Natural := 0;

         Main_Source : constant Source_Id := Main_File.Source;

         Bind_Exchange : constant String_Access :=
                           Binder_Exchange_File_Name
                             (Main_Base_Name_Index, B_Data.Binder_Prefix);

         Dep_Files                        : Boolean;
         Lang_Index                       : Language_Ptr;
         Object_File_Suffix_Label_Written : Boolean;

         ---------------------------
         -- Get_Project_Checkline --
         ---------------------------

         function Get_Project_Checkline
           (Project : Project_Id) return Project_Check_String is
         begin
            Position := Projects.Find (Project.Path.Display_Name);

            if Project_File_Paths.Has_Element (Position)
              and then Project_File_Paths.Element (Position).Line (1)
              /= ASCII.NUL
            then
               return Project_File_Paths.Element (Position).Line;
            end if;

            return String (Osint.File_Stamp (Project.Path.Display_Name)) & ' '
              & Get_Project_Checksum (Project);
         end Get_Project_Checkline;

      begin
         Binder_Driver_Needs_To_Be_Called := Opt.Force_Compilations;

         --  First check if the binder driver needs to be called.
         --  It needs to be called if
         --    1) there is no existing binder exchange file
         --    2) there is no binder generated object file
         --    3) there is a dependency file of the language that
         --       is more recent than any of these two files

         if not Binder_Driver_Needs_To_Be_Called and then
           Opt.Verbosity_Level > Opt.Low
         then
            Put_Line
              ("   Checking binder generated files for " & Main & "...");
         end if;

         Bind_Exchange_TS :=
           File_Stamp
             (Path_Name_Type'(Create_Name (Bind_Exchange.all)));

         if not Binder_Driver_Needs_To_Be_Called then
            if Bind_Exchange_TS = Osint.Invalid_Time then
               Binder_Driver_Needs_To_Be_Called := True;

               if Opt.Verbosity_Level > Opt.Low then
                  Put_Line
                    ("      -> binder exchange file " &
                     Bind_Exchange.all &
                     " does not exist");
               end if;

            else
               begin
                  Open (Exchange_File, In_File, Bind_Exchange.all);

               exception
                  when others =>
                     Binder_Driver_Needs_To_Be_Called := True;

                     if Opt.Verbosity_Level > Opt.Low then
                        Put_Line
                          ("      -> could not open " &
                           "binder exchange file" &
                           Bind_Exchange.all);
                     end if;
               end;
            end if;
         end if;

         if not Binder_Driver_Needs_To_Be_Called then
            begin
               Get_Line (Exchange_File, Line, Last);
            exception
               when others =>
                  Binder_Driver_Needs_To_Be_Called := True;

                  if Opt.Verbosity_Level > Opt.Low then
                     Put_Line
                       ("      -> previous gprbind failed, or " &
                        Bind_Exchange.all &
                        " corrupted");
                  end if;
            end;
         end if;

         --  Check the generated object file

         if not Binder_Driver_Needs_To_Be_Called then
            if Line (1 .. Last) /= Binding_Label (Generated_Object_File)
              or else End_Of_File (Exchange_File)
            then
               Binder_Driver_Needs_To_Be_Called := True;

               if Opt.Verbosity_Level > Opt.Low then
                  Put_Line
                    ("      -> previous gprbind failed, or " &
                     Bind_Exchange.all &
                     " corrupted");
               end if;

            else
               Get_Line (Exchange_File, Line, Last);
               Bind_Object_TS :=
                 File_Stamp
                   (Path_Name_Type'(Create_Name (Line (1 .. Last))));

               --  Do not perform this check in CodePeer mode where there is
               --  no object file per se.

               if Bind_Object_TS = Osint.Invalid_Time
                 and not Opt.CodePeer_Mode
               then
                  Binder_Driver_Needs_To_Be_Called := True;

                  if Opt.Verbosity_Level > Opt.Low then
                     Put_Line
                       ("      -> binder generated object " &
                        Line (1 .. Last) &
                        " does not exist");
                  end if;
               end if;
            end if;
         end if;

         if not Binder_Driver_Needs_To_Be_Called then
            if End_Of_File (Exchange_File) then
               Binder_Driver_Needs_To_Be_Called := True;

            else
               Get_Line (Exchange_File, Line, Last);

               if Line (1 .. Last) /= Binding_Label (Project_Files)
                 or else End_Of_File (Exchange_File)
               then
                  Binder_Driver_Needs_To_Be_Called := True;
               end if;
            end if;

            if Binder_Driver_Needs_To_Be_Called then
               if Opt.Verbosity_Level > Opt.Low then
                  Put_Line
                    ("      -> previous gprbind failed, or " &
                     Bind_Exchange.all & " corrupted");
               end if;

            else
               --  Populate the hash table Project_File_Paths with
               --  the paths of all project files in the closure
               --  of the main project.

               Projects.Insert
                 (Main_Proj.Path.Display_Name,
                  Project_Check_Line'(Main_Proj, Line => <>));

               Proj_List := Main_Proj.All_Imported_Projects;

               while Proj_List /= null loop
                  Projects.Insert
                    (Proj_List.Project.Path.Display_Name,
                     (Proj_List.Project, Line => <>));
                  Proj_List := Proj_List.Next;
               end loop;

               --  Get the project file paths from the exchange
               --  file and check if they are the expected project
               --  files with the same time stamps.

               while not End_Of_File (Exchange_File) loop
                  Get_Line (Exchange_File, Name_Buffer, Name_Len);
                  exit when Name_Len > 0 and then Name_Buffer (1) = '[';

                  if End_Of_File (Exchange_File) then
                     Binder_Driver_Needs_To_Be_Called := True;

                     if Opt.Verbosity_Level > Opt.Low then
                        Put_Line
                          ("      -> previous gprbind failed, "
                           & "or " & Bind_Exchange.all & " corrupted");
                     end if;

                     exit;
                  end if;

                  Project_Path := Name_Find;
                  Position     := Projects.Find (Project_Path);

                  if Project_File_Paths.Has_Element (Position) then
                     Counter := Counter + 1;
                     pragma Assert
                       (Projects (Position).Line = Empty_Check_String);

                     Projects (Position).Line :=
                       String (Osint.File_Stamp (Project_Path))
                       & ' ' & Get_Project_Checksum
                                 (Projects (Position).Project);
                     Get_Line (Exchange_File, Line, Last);

                     if Projects (Position).Line /= Line (1 .. Last) then
                        Binder_Driver_Needs_To_Be_Called := True;

                        if Opt.Verbosity_Level > Opt.Low then
                           Put_Line
                             ("      -> project file "
                              & Get_Name_String (Project_Path)
                              & " has been modified");
                        end if;

                        exit;
                     end if;

                  else
                     Binder_Driver_Needs_To_Be_Called := True;

                     if Opt.Verbosity_Level > Opt.Low then
                        Put_Line
                          ("      -> unknown project file "
                           & Get_Name_String (Project_Path));
                     end if;

                     exit;
                  end if;
               end loop;

               --  Check if there are still project file paths in
               --  the hash table.

               if not Binder_Driver_Needs_To_Be_Called
                 and then Counter < Natural (Projects.Length)
               then
                  Binder_Driver_Needs_To_Be_Called := True;

                  if Opt.Verbosity_Level > Opt.Low then
                     Put_Line ("      -> more project files");
                  end if;
               end if;
            end if;
         end if;

         if Is_Open (Exchange_File) then
            Close (Exchange_File);
         end if;

         if not Binder_Driver_Needs_To_Be_Called then

            Queue.Initialize (Opt.One_Compilation_Per_Obj_Dir, Force => True);

            declare
               Config          : constant Language_Config :=
                                   B_Data.Language.Config;
               Source_Identity : Source_Id;
               Roots           : Roots_Access;
               Source          : Source_Id;
               Iter            : Source_Iterator;

            begin
               --  Put the root sources in the queue

               if Main_Source.Language.Name = B_Data.Language.Name then
                  Queue.Insert
                    (Source => (Tree    => Main_File.Tree,
                                Id      => Main_File.Source,
                                Closure => False));
               end if;

               Roots := Main_Source.Roots;

               while Roots /= null loop
                  Queue.Insert
                    (Source => (Tree    => Main_File.Tree,
                                Id      => Roots.Root,
                                Closure => False));
                  Roots := Roots.Next;
               end loop;

               --  If main is not unit base and there is no root,
               --  check all sources with the language name of the
               --  binder, except those that are not interfaces of
               --  their project.

               if Queue.Is_Empty then
                  Iter := For_Each_Source (Project_Tree);

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

                     if Source.Language.Name = B_Data.Language.Name
                       and then not Source.Locally_Removed
                       and then Is_Compilable (Source)
                       and then
                         ((Config.Kind = File_Based
                           and then Source.Kind = Impl)
                          or else
                            (Config.Kind = Unit_Based
                             and then Source.Unit /= No_Unit_Index
                             and then Source.Unit /= Main_Source.Unit
                             and then (Source.Kind = Impl
                                       or else Other_Part (Source) = No_Source)
                             and then not Is_Subunit (Source)))
                       and then Source.In_Interfaces
                     then
                        declare
                           Proj  : Project_Id;
                           Src   : Source_Id;
                           Iter2 : Source_Iterator;

                        begin
                           --  If a source is overriden in an
                           --  extending project, then the object file
                           --  is not included in the global archive.

                           Proj := Source.Project.Extended_By;
                           Loop2 : while Proj /= No_Project loop
                              Iter2 := For_Each_Source (Project_Tree, Proj);
                              loop
                                 Src := GPR.Element (Iter2);
                                 exit when Src = No_Source;

                                 exit Loop1 when
                                   Src.Object = Source.Object;

                                 Next (Iter2);
                              end loop;
                              Proj := Proj.Extended_By;
                           end loop Loop2;
                        end;

                        Queue.Insert
                          (Source => (Tree    => Main_File.Tree,
                                      Id      => Source,
                                      Closure => False));
                     end if;

                     Next (Iter);
                  end loop Loop1;
               end if;

               --  Get each file from the queue and check its
               --  dependency file.

               declare
                  Dep_TS   : aliased File_Attributes := Unknown_Attributes;
                  Dep_File : File_Name_Type;
                  Dep_Path : Path_Name_Type;
                  The_ALI  : ALI.ALI_Id;
                  Text     : Text_Buffer_Ptr;
                  Found    : Boolean;
                  Source   : Queue.Source_Info;
               begin
                  while not Queue.Is_Empty loop
                     Queue.Extract (Found, Source);
                     Source_Identity := Source.Id;

                     Initialize_Source_Record (Source_Identity);

                     --  Get the dependency file for this source

                     Dep_File := Source_Identity.Dep_Name;
                     Dep_Path := Source_Identity.Dep_Path;
                     Dep_TS   := Source_Identity.Dep_TS;

                     --  For a library file, if there is no ALI file
                     --  in the object directory, check in the Library
                     --  ALI directory.

                     if not Is_Regular_File (Get_Name_String (Dep_Path))
                       and then Source_Identity.Project.Library
                       and then
                         Source_Identity.Project.Library_ALI_Dir /=
                           No_Path_Information
                     then
                        Set_Name_Buffer
                          (Get_Name_String
                             (Source_Identity.Project
                              .Library_ALI_Dir.Display_Name));
                        Add_Char_To_Name_Buffer (Directory_Separator);
                        Get_Name_String_And_Append (Dep_File);

                        Dep_TS := Unknown_Attributes;
                        if Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
                           Dep_Path := Name_Find;
                        end if;
                     end if;

                     declare
                        Proj : Project_Id :=
                                 Source_Identity.Project.Extended_By;
                     begin
                        while Proj /= No_Project loop
                           Name_Len := 0;

                           if Proj.Library
                             and then
                               Proj.Library_ALI_Dir /= No_Path_Information
                           then
                              Get_Name_String_And_Append
                                (Proj.Library_ALI_Dir.Display_Name);

                           else
                              Get_Name_String_And_Append
                                (Proj.Object_Directory.Display_Name);
                           end if;

                           Add_Char_To_Name_Buffer
                             (Directory_Separator);
                           Get_Name_String_And_Append (Dep_File);

                           --  Check if the dependency file exists in
                           --  the extended project, and if it does,
                           --  replace both Dep_Path and Dep_TS with
                           --  the information for it.

                              if Is_Regular_File (Name_Buffer (1 .. Name_Len))
                              then
                                 Dep_Path := Name_Find;
                              end if;

                           Proj := Proj.Extended_By;
                        end loop;
                     end;

                     --  Check the time stamp against the binder
                     --  exchange file time stamp.

                     if File_Time_Stamp (Dep_Path, Dep_TS'Access)
                       = Empty_Time_Stamp
                     then
                        Binder_Driver_Needs_To_Be_Called := True;

                        if Opt.Verbosity_Level > Opt.Low then
                           Put ("      -> cannot find ");
                           Put_Line (Get_Name_String (Dep_Path));
                        end if;

                        exit;

                     elsif File_Stamp (Dep_Path) > Bind_Exchange_TS then
                        Binder_Driver_Needs_To_Be_Called := True;

                        if Opt.Verbosity_Level > Opt.Low then
                           Put ("      -> ");
                           Put (Get_Name_String (Dep_Path));
                           Put_Line
                             (" is more recent than the binder exchange file");
                        end if;

                        exit;

                     else
                        Text := Read_Library_Info_From_Full
                          (File_Name_Type (Dep_Path), Dep_TS'Access);

                        if Text /= null then
                           The_ALI :=
                             ALI.Scan_ALI
                               (File_Name_Type (Dep_Path),
                                Text,
                                Ignore_ED     => False,
                                Err           => True,
                                Read_Lines    => "W");
                           Free (Text);

                           Queue.Insert_Withed_Sources_For
                             (The_ALI,
                              Project_Tree,
                              Excluding_Shared_SALs => True);
                        end if;
                     end if;
                  end loop;
               end;
            end;
         end if;

         if not Binder_Driver_Needs_To_Be_Called then
            if Opt.Verbosity_Level > Opt.Low then
               Put_Line ("      -> up to date");
            end if;

         else
            begin
               Create (Exchange_File, Out_File, Bind_Exchange.all);

            exception
               when others =>
                  Fail_Program
                    (Project_Tree,
                     "unable to create binder exchange file " &
                     Bind_Exchange.all);
            end;

            --  Optional line: Quiet or Verbose

            if Opt.Quiet_Output then
               Put_Line (Exchange_File, Binding_Label (Quiet));

            elsif Opt.Verbose_Mode then
               if Opt.Verbosity_Level = Opt.Low then
                  Put_Line (Exchange_File, Binding_Label (Verbose_Low));
               else
                  Put_Line (Exchange_File, Binding_Label (Verbose_Higher));
               end if;
            end if;

            --  If -dn was used, indicate to gprbind that the
            --  temporary response file, if created, should not
            --  deleted.

            if Opt.Keep_Temporary_Files then
               Put_Line
                 (Exchange_File,
                  Binding_Label (Delete_Temp_Files) & ASCII.LF & "False");
            end if;

            --  If there are Stand-Alone Libraries, tell it to gprbind

            if There_Are_Stand_Alone_Libraries then
               Put_Line
                 (Exchange_File,
                  Binding_Label (Gprexch.There_Are_Stand_Alone_Libraries));
            end if;

            --  If the language is Ada, create a binder mapping file
            --  and pass it to gprbind.

            if B_Data.Language_Name = Name_Ada then
               declare
                  Mapping_Path : constant Path_Name_Type :=
                                   Create_Binder_Mapping_File (Project_Tree);
               begin
                  if Mapping_Path /= No_Path then
                     Put_Line
                       (Exchange_File,
                        Binding_Label (Gprexch.Mapping_File) & ASCII.LF
                        & Get_Name_String (Mapping_Path));
                  end if;
               end;
            end if;

            --  Send the Toolchain Version if there is one for the language

            if B_Data.Language.Config.Toolchain_Version /= No_Name
              or else
                B_Data.Language.Config.Runtime_Library_Version /= No_Name
            then
               Put_Line
                 (Exchange_File,
                  Binding_Label (Toolchain_Version) & ASCII.LF
                  & Get_Name_String (B_Data.Language.Name));

               if B_Data.Language.Config.Runtime_Library_Version /= No_Name
               then
                  Put_Line
                    (Exchange_File,
                     Get_Name_String
                       (B_Data.Language.Config.Runtime_Library_Version));
               else
                  Put_Line
                    (Exchange_File,
                     Get_Name_String
                       (B_Data.Language.Config.Toolchain_Version));
               end if;
            end if;

            --  Send the object file suffix for each language where it
            --  is declared.

            Lang_Index := Main_Proj.Languages;
            Object_File_Suffix_Label_Written := False;

            while Lang_Index /= No_Language_Index loop
               if Lang_Index.Config.Object_File_Suffix /= No_Name then
                  if not Object_File_Suffix_Label_Written then
                     Put_Line
                       (Exchange_File, Binding_Label
                          (Gprexch.Object_File_Suffix));
                     Object_File_Suffix_Label_Written := True;
                  end if;

                  Put_Line
                    (Exchange_File,
                     Get_Name_String (Lang_Index.Name) & ASCII.LF
                     & Get_Name_String (Lang_Index.Config.Object_File_Suffix));
               end if;

               Lang_Index := Lang_Index.Next;
            end loop;

            --  Optional line: shared libs

            if Shared_Libs then
               Put_Line (Exchange_File, Binding_Label (Gprexch.Shared_Libs));
            end if;

            --  First, the main base name

            Put_Line
              (Exchange_File,
               Binding_Label (Gprexch.Main_Base_Name) & ASCII.LF
               & Get_Name_String (Main_Base_Name_Index));

            --  Then, the compiler path and required switches

            declare
               Config  : Language_Config renames B_Data.Language.Config;
               List    : Name_List_Index;
               Nam_Nod : Name_Node;

            begin
               --  Compiler path

               Put_Line
                 (Exchange_File,
                  Binding_Label (Gprexch.Compiler_Path) & ASCII.LF
                  & Get_Compiler_Driver_Path
                    (Main_Proj, B_Data.Language).all);

               --  Leading required switches, if any

               List := Config.Compiler_Leading_Required_Switches;
               if List /= No_Name_List then
                  Put_Line
                    (Exchange_File,
                     Binding_Label (Gprexch.Compiler_Leading_Switches));
                  Emit_Compiler_Switches (Exchange_File, List);

                  if Opt.CodePeer_Mode then
                     Put_Line (Exchange_File, "-gnatcC");
                  end if;
               end if;

               --  Trailing required switches, if any

               List := Config.Compiler_Trailing_Required_Switches;
               if List /= No_Name_List then
                  Put_Line
                    (Exchange_File,
                     Binding_Label
                       (Gprexch.Compiler_Trailing_Switches));

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

            --  Then, the Dependency files

            if Main_Source.Unit /= No_Unit_Index then
               Initialize_Source_Record (Main_Source);
               Put_Line
                 (Exchange_File,
                  Binding_Label (Main_Dependency_File) & ASCII.LF
                  & Get_Name_String (Main_Source.Dep_Path));
            end if;

            --  Add the relevant dependency files, either those in
            --  Roots (<main>) for the project, or all dependency
            --  files in the project tree, if Roots (<main>) is not
            --  specified .

            Put_Line (Exchange_File, Binding_Label (Dependency_Files));
            First_Dep := null;

            Add_Dependency_Files
              (Main_Proj, B_Data.Language, Main_Source, Dep_Files);

            while First_Dep /= null loop
               Put_Line (Exchange_File, First_Dep.Name.all);
               First_Dep := First_Dep.Next;
            end loop;

            --  Put the options, if any

            declare
               The_Packages   : constant Package_Id :=
                                  Main_Proj.Decl.Packages;

               Binder_Package : constant GPR.Package_Id :=
                                  GPR.Util.Value_Of
                                    (Name        => Name_Binder,
                                     In_Packages => The_Packages,
                                     Shared      => Project_Tree.Shared);
               Config         : constant Language_Config :=
                                  B_Data.Language.Config;

               Options_Instance : constant Bind_Option_Table_Ref :=
                                    Binder_Options_HTable.Get
                                      (B_Data.Language_Name);

               Switches    : Variable_Value;
               Switch_List : String_List_Id;
               Element     : String_Element;

            begin
               --  First, check if there are binder options
               --  specified in the main project file.

               if Binder_Package /= No_Package then
                  declare
                     Defaults : constant Array_Element_Id :=
                                  GPR.Util.Value_Of
                                    (Name      => Name_Default_Switches,
                                     In_Arrays =>
                                       Project_Tree.Shared.Packages.Table
                                         (Binder_Package).Decl.Arrays,
                                     Shared    => Project_Tree.Shared);

                     Switches_Array : constant Array_Element_Id :=
                                        GPR.Util.Value_Of
                                          (Name      => Name_Switches,
                                           In_Arrays =>
                                             Project_Tree.Shared.Packages.Table
                                               (Binder_Package).Decl.Arrays,
                                           Shared    => Project_Tree.Shared);

                  begin
                     Switches :=
                       GPR.Util.Value_Of
                         (Index           => Name_Id (Main_Id),
                          Src_Index       => 0,
                          In_Array        => Switches_Array,
                          Shared          => Project_Tree.Shared,
                          Allow_Wildcards => True);

                     if Switches = Nil_Variable_Value then
                        Switches :=
                          GPR.Util.Value_Of
                            (Index                  =>
                                 B_Data.Language_Name,
                             Src_Index              => 0,
                             In_Array               => Switches_Array,
                             Shared                 => Project_Tree.Shared,
                             Force_Lower_Case_Index => True);
                     end if;

                     if Switches = Nil_Variable_Value then
                        Switches :=
                          GPR.Util.Value_Of
                            (Index                  => All_Other_Names,
                             Src_Index              => 0,
                             In_Array               => Switches_Array,
                             Shared                 => Project_Tree.Shared,
                             Force_Lower_Case_Index => True);
                     end if;

                     if Switches = Nil_Variable_Value then
                        Switches :=
                          GPR.Util.Value_Of
                            (Index     => B_Data.Language_Name,
                             Src_Index => 0,
                             In_Array  => Defaults,
                             Shared    => Project_Tree.Shared);
                     end if;
                  end;
               end if;

               --  If there are binder options, either minimum
               --  binder options, or in the main project file or
               --  on the command line, put them in the exchange
               --  file.

               if Config.Binder_Required_Switches /= No_Name_List
                 or else Switches.Kind = GPR.List
                 or else not All_Language_Binder_Options.Is_Empty
                 or else Options_Instance /= No_Bind_Option_Table
                 or else Opt.CodePeer_Mode
                 or else (B_Data.Language_Name = Name_Ada
                          and then Opt.No_Main_Subprogram)
               then
                  Put_Line
                    (Exchange_File, Binding_Label (Gprexch.Binding_Options));

                  --  First, the required switches, if any

                  declare
                     List : Name_List_Index :=
                              Config.Binder_Required_Switches;
                     Elem : Name_Node;

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

                        if Name_Len > 0 then
                           Put_Line
                             (Exchange_File,
                              Name_Buffer (1 .. Name_Len));
                        end if;

                        List := Elem.Next;
                     end loop;
                  end;

                  --  Then, the eventual options in the main
                  --  project file.

                  if Switches.Kind = GPR.List then
                     declare
                        Option : String_Access;

                     begin
                        Switch_List := Switches.Values;

                        while Switch_List /= Nil_String loop
                           Element :=
                             Project_Tree.Shared.String_Elements.Table
                               (Switch_List);

                           Get_Name_String (Element.Value);

                           if Name_Len > 0 then
                              Option :=
                                new String'
                                  (Name_Buffer (1 .. Name_Len));
                              Test_If_Relative_Path
                                (Option,
                                 Main_Project_Dir.all,
                                 No_Name);
                              Put_Line (Exchange_File, Option.all);
                           end if;

                           Switch_List := Element.Next;
                        end loop;
                     end;
                  end if;

                  --  Then -P if in CodePeer mode

                  if Opt.CodePeer_Mode then
                     Put_Line (Exchange_File, "-P");
                  end if;

                  --  Then those on the command line, for all
                  --  binder drivers, if any.

                  for Option of All_Language_Binder_Options loop
                     Put_Line (Exchange_File, Option);
                  end loop;

                  --  Then -z if specified

                  if B_Data.Language_Name = Name_Ada
                    and then Opt.No_Main_Subprogram
                  then
                     Put_Line (Exchange_File, "-z");
                  end if;

                  --  Finally those on the command line for the
                  --  binder driver of the language

                  if Options_Instance /= No_Bind_Option_Table then
                     for Option of Options_Instance.all loop
                        Put_Line (Exchange_File, Option);
                     end loop;
                  end if;

               end if;
            end;

            if Build_Script_Name /= null then
               Put_Line
                 (Exchange_File,
                  Binding_Label (Script_Path) & ASCII.LF
                  & Build_Script_Name.all);
            end if;

            --  Finally, the list of the project paths with their
            --  time stamps.

            Put_Line
              (Exchange_File,
               Binding_Label (Project_Files) & ASCII.LF

               --  The main project file is always the first one, so that
               --  gprbind may know the main project dir.

               & Get_Name_String (Main_Proj.Path.Display_Name) & ASCII.LF
               & Get_Project_Checkline (Main_Proj));

            Proj_List := Main_Proj.All_Imported_Projects;

            while Proj_List /= null loop
               if Main_Proj.Standalone_Library = Encapsulated
                 or else not Proj_List.From_Encapsulated_Lib
               then
                  declare
                     Project : Project_Id := Proj_List.Project;
                  begin
                     while Project.Virtual
                       and then Project.Extends /= No_Project
                     loop
                        Project := Project.Extends;
                     end loop;

                     Put_Line
                       (Exchange_File,
                        Get_Name_String (Project.Path.Display_Name));
                     Put_Line
                       (Exchange_File, Get_Project_Checkline (Project));
                  end;
               end if;

               Proj_List := Proj_List.Next;
            end loop;

            if Main_Source.Unit = No_Unit_Index and then (not Dep_Files) then
               Close (Exchange_File);

               begin
                  Create (Exchange_File, Out_File, Bind_Exchange.all);

               exception
                  when others =>
                     Fail_Program
                       (Project_Tree,
                        "unable to create binder exchange file " &
                          Bind_Exchange.all);
               end;

               Put_Line (Exchange_File, Binding_Label (Nothing_To_Bind));
               Close (Exchange_File);

               if Opt.Verbosity_Level > Opt.Low then
                  Put_Line ("      -> nothing to bind");
               end if;

            else
               Close (Exchange_File);

               if B_Data.Language.Config.Objects_Path /= No_Name then
                  declare
                     Env_Var   : constant String :=
                                   Get_Name_String
                                     (B_Data.Language.Config.
                                                      Objects_Path);
                     Path_Name : String_Access :=
                                   Main_Proj.Objects_Path;
                  begin
                     if Path_Name = null then
                        if Current_Verbosity = High then
                           Put_Line (Env_Var & " :");
                        end if;

                        Get_Directories
                          (Project_Tree => Project_Tree,
                           For_Project  => Main_Proj,
                           Activity     => Executable_Binding,
                           Languages    => No_Names);

                        Path_Name := Create_Path_From_Dirs;
                        Main_Proj.Objects_Path := Path_Name;
                     end if;

                     Setenv (Env_Var, Path_Name.all);

                     if Opt.Verbosity_Level > Opt.Low then
                        Put (Env_Var);
                        Put (" = ");
                        Put_Line (Path_Name.all);
                     end if;
                  end;

               elsif B_Data.Language.Config.Objects_Path_File /= No_Name then
                  declare
                     Env_Var   : constant String :=
                                   Get_Name_String
                                     (B_Data.Language.Config.
                                                      Objects_Path_File);
                     Path_Name : Path_Name_Type :=
                                   Main_Proj.Objects_Path_File_Without_Libs;
                  begin
                     if Path_Name = No_Path then
                        if Current_Verbosity = High then
                           Put_Line (Env_Var & " :");
                        end if;

                        Get_Directories
                          (Project_Tree => Project_Tree,
                           For_Project  => Main_Proj,
                           Activity     => Executable_Binding,
                           Languages    => No_Names);

                        declare
                           FD     : File_Descriptor;
                           Len    : Integer;
                           Status : Boolean;
                        begin
                           GPR.Env.Create_New_Path_File
                             (Shared    => Project_Tree.Shared,
                              Path_FD   => FD,
                              Path_Name =>
                                Main_Proj.Objects_Path_File_Without_Libs);

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

                           Path_Name :=
                             Main_Proj.Objects_Path_File_Without_Libs;

                           for Index in
                             1 .. Gpr_Build_Util.Directories.Last
                           loop
                              Get_Name_String
                                (Gpr_Build_Util.Directories.Table (Index));

                              if Current_Verbosity = High then
                                 Put_Line (Name_Buffer (1 .. Name_Len));
                              end if;

                              Name_Len := Name_Len + 1;
                              Name_Buffer (Name_Len) := ASCII.LF;

                              Len :=
                                Write (FD, Name_Buffer (1)'Address, Name_Len);

                              if Len /= Name_Len then
                                 Fail_Program (Project_Tree, "disk full");
                              end if;
                           end loop;

                           Close (FD, Status);

                           if not Status then
                              Fail_Program (Project_Tree, "disk full");
                           end if;
                        end;
                     end if;

                     Setenv (Env_Var, Get_Name_String (Path_Name));

                     if Opt.Verbosity_Level > Opt.Low then
                        Put (Env_Var);
                        Put (" = ");
                        Put_Line (Get_Name_String (Path_Name));
                     end if;
                  end;
               end if;

               if not Opt.Quiet_Output then
                  if Opt.Verbose_Mode then
                     Set_Name_Buffer (B_Data.Binder_Driver_Path.all);
                     Add_Str_To_Name_Buffer (" ");
                     Add_Str_To_Name_Buffer (Bind_Exchange.all);
                     Put_Line (Name_Buffer (1 .. Name_Len));

                  else
                     Display
                       (Section  => GPR.Bind,
                        Command  => Ada.Directories.Base_Name
                                     (Ada.Directories.Simple_Name
                                       (B_Data.Binder_Driver_Path.all)),
                        Argument => Bind_Exchange.all);
                  end if;
               end if;

               declare
                  Pid : Process_Id;
               begin
                  Pid := Non_Blocking_Spawn
                    (B_Data.Binder_Driver_Path.all, (1 => Bind_Exchange));

                  if Pid = Invalid_Pid then
                     Record_Failure (Main_File);

                  else
                     Add_Process (Pid, (Binding, Main_File));

                     Display_Processes ("bind");
                  end if;
               end;
            end if;
         end if;
      end Bind_Language;

      --------------------------
      -- Get_Project_Checksum --
      --------------------------

      function Get_Project_Checksum
        (Project : Project_Id) return Message_Digest
      is
         procedure Update_Vars (Items : Variable_Id);

         Chk : Context;
         Pkg : Package_Id := Project.Decl.Packages;
         Tbl : Package_Table.Table_Ptr renames
           Project_Tree.Shared.Packages.Table;

         -----------------
         -- Update_Vars --
         -----------------

         procedure Update_Vars (Items : Variable_Id) is
            Vars : Variable_Id := Items;
            Strs : String_List_Id;
            Var  : Variable;
            Str  : String_Element;
         begin
            while Vars /= No_Variable loop
               Var := Project_Tree.Shared.Variable_Elements.Table (Vars);
               Update (Chk, Get_Name_String (Var.Name));
               case Var.Value.Kind is
                  when Single =>
                     Update (Chk, Get_Name_String (Var.Value.Value));

                     if Var.Value.Index /= 0 then
                        Update (Chk, Var.Value.Index'Img);
                     end if;

                  when List =>
                     Strs := Var.Value.Values;
                     while Strs /= Nil_String loop
                        Str := Project_Tree.Shared.String_Elements.Table
                                 (Strs);
                        Update (Chk, Get_Name_String (Str.Value));
                        if Str.Index /= 0 then
                           Update (Chk, Str.Index'Img);
                        end if;

                        Strs := Str.Next;
                     end loop;
                  when Undefined => null;
               end case;
               Vars := Var.Next;
            end loop;
         end Update_Vars;

      begin
         Update_Vars (Project.Decl.Variables);
         Update_Vars (Project.Decl.Attributes);

         while Pkg /= No_Package loop
            Update_Vars (Tbl (Pkg).Decl.Variables);
            Update_Vars (Tbl (Pkg).Decl.Attributes);
            Pkg := Tbl (Pkg).Next;
         end loop;

         Update (Chk, Hex_Image (Project.Checksum));

         return Digest (Chk);
      end Get_Project_Checksum;

   --  Start of processing for Post_Compilation_Phase

   begin
      --  Build the libraries, if any

      --  First, get the libraries in building order in table Library_Projs

      Process_Imported_Libraries
        (Main_Project,
         There_Are_SALs     => There_Are_Stand_Alone_Libraries,
         And_Project_Itself => True);

      if not Library_Projs.Is_Empty then
         declare
            Lib_Projs : array (1 .. Library_Projs.Last_Index) of
                          Library_Project;
            Proj      : Library_Project;

         begin
            --  Copy the list of library projects in local array Lib_Projs,
            --  as procedure Build_Library uses table Library_Projs.
            for J in Lib_Projs'Range loop
               Lib_Projs (J) := Library_Projs (J);
            end loop;

            for J in Lib_Projs'Range loop
               Proj := Lib_Projs (J);

               if not Proj.Is_Aggregated then
                  --  Try building a library only if no error occurred in
                  --  library project and projects it depends on.
                  --  Do not actually create the library for aggregate projects
                  --  or in CodePeer mode or when generating C or Java byte
                  --  code, since there is no notion of library is this case,
                  --  only the rest of the processing (creation and compilation
                  --  of binder file in particular, possibly copying ALI files)
                  --  is useful.

                  if not Project_Compilation_Failed (Proj.Proj) then
                     if Proj.Proj.Extended_By = No_Project then
                        if not Proj.Proj.Externally_Built then
                           while Libs_Are_Building.Contains (Proj.Proj.Name)
                           loop
                              Wait_For_Slots_Less_Than (Outstanding_Processes);
                           end loop;

                           Build_Library
                             (Proj.Proj, Project_Tree,
                              No_Create =>
                                Proj.Is_Aggregated
                                  or else Opt.CodePeer_Mode
                                  or else No_Link_Target (Target_Name.all));

                           exit when Stop_Spawning;
                        end if;

                        if not Is_Static (Proj.Proj) then
                           Shared_Libs := True;
                        end if;

                     end if;
                  end if;
               end if;
            end loop;
         end;
      end if;

      --  If no main is specified, there is nothing else to do

      if Mains.Number_Of_Mains (Project_Tree) = 0 then
         return;
      end if;

      --  Check if there is a need to call a binder driver

      Find_Binding_Languages (Project_Tree, Main_Project);

      --  Proceed to bind (or rebind if needed) for each main

      Mains.Reset;

      Wait_For_Slots_Less_Than (1);

      loop
         declare
            Main_File : constant Main_Info := Mains.Next_Main;
         begin
            exit when Main_File = No_Main_Info;

            if Main_File.Tree /= Project_Tree
              or else Project_Compilation_Failed (Main_File.Project)
            then
               --  Will be processed later, or do not need any processing in
               --  the case of compilation errors in the project.
               null;

            elsif
               not Builder_Data (Main_File.Tree).There_Are_Binder_Drivers
            then
               if Current_Verbosity = High then
                  Debug_Output ("Post-compilation, no binding required for",
                                Debug_Name (Main_File.Tree));
               end if;

            else
               declare
                  Main                 : constant String :=
                                           Get_Name_String (Main_File.File);
                  Main_Id              : constant File_Name_Type :=
                                           Create_Name (Base_Name (Main));
                  Main_Base_Name_Index : constant File_Name_Type :=
                                           Base_Name_Index_For
                                             (Main, Main_File.Index,
                                              Main_File.Source.Language.Config
                                              .Multi_Unit_Object_Separator);

                  Main_Proj : constant Project_Id :=
                                Ultimate_Extending_Project_Of
                                  (Main_File.Source.Project);
                  B_Data    : Binding_Data :=
                                Builder_Data (Main_File.Tree).Binding;
               begin
                  while B_Data /= null loop
                     if B_Data.Language.Config.Compiler_Driver /= Empty_File
                     then
                        Wait_For_Slots_Less_Than (Opt.Maximum_Binders);
                        exit when Stop_Spawning;
                        Change_To_Object_Directory (Main_Proj);
                        Bind_Language
                          (Main_Proj, Main, Main_Base_Name_Index,
                           Main_File, Main_Id, B_Data);
                        exit when Stop_Spawning;
                     end if;

                     B_Data := B_Data.Next;
                  end loop;
               end;
            end if;
         end;
      end loop;
   end Post_Compilation_Phase;

   ------------------------------
   -- Wait_For_Slots_Less_Then --
   ------------------------------

   procedure Wait_For_Slots_Less_Than (Count : Positive) is
      Data : Process_Data;
      OK   : Boolean;
   begin
      while Outstanding_Processes >= Count loop
         Await_Process (Data, OK);

         if Data /= No_Process_Data then
            Libs_Are_Building.Exclude (Data.Main.Project.Name);
         end if;

         if OK then
            Data.Main.Project.Was_Built := True;
         else
            Record_Failure (Data.Main);
         end if;

         Display_Processes ("bind");
      end loop;
   end Wait_For_Slots_Less_Than;

end Gprbuild.Post_Compile;