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
|
<!DOCTYPE Article PUBLIC "-//OASIS//DTD DocBook V3.1//EN">
<article id="building-guide">
<artheader>
<title>Building the Glasgow Functional Programming Tools Suite</title>
<author><othername>The GHC Team</othername></author>
<address><email>glasgow-haskell-{users,bugs}@haskell.org</email></address>
<abstract>
<para>The Glasgow fptools suite is a collection of Functional
Programming related tools, including the Glasgow Haskell
Compiler (GHC). The source code for the whole suite is kept in
a single CVS repository and shares a common build and
installation system.</para>
<para>This guide is intended for people who want to build or
modify programs from the Glasgow <literal>fptools</literal>
suite (as distinct from those who merely want to
<emphasis>run</emphasis> them). Installation instructions are
now provided in the user guide.</para>
<para>The bulk of this guide applies to building on Unix
systems; see <xref linkend="winbuild"> for Windows notes.</para>
</abstract>
</artheader>
<sect1 id="sec-getting">
<title>Getting the sources</title>
<para>You can get your hands on the <literal>fptools</literal>
in two ways:</para>
<variablelist>
<varlistentry>
<term><indexterm><primary>Source
distributions</primary></indexterm>Source distributions</term>
<listitem>
<para>You have a supported platform, but (a) you like
the warm fuzzy feeling of compiling things yourself;
(b) you want to build something ``extra”—e.g., a
set of libraries with strictness-analysis turned off; or
(c) you want to hack on GHC yourself.</para>
<para>A source distribution contains complete sources for
one or more projects in the <literal>fptools</literal>
suite. Not only that, but the more awkward
machine-independent steps are done for you. For example, if
you don't have
<command>happy</command><indexterm><primary>happy</primary></indexterm>
you'll find it convenient that the source distribution
contains the result of running <command>happy</command> on
the parser specifications. If you don't want to alter the
parser then this saves you having to find and install
<command>happy</command>. You will still need a working
version of GHC (version 5.x or later) on your machine in
order to compile (most of) the sources, however.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>The CVS repository.<indexterm><primary>CVS repository</primary></indexterm></term>
<listitem>
<para>We make releases infrequently. If you want more
up-to-the minute (but less tested) source code then you need
to get access to our CVS repository.</para>
<para>All the <literal>fptools</literal> source code is held
in a CVS repository. CVS is a pretty good source-code
control system, and best of all it works over the
network.</para>
<para>The repository holds source code only. It holds no
mechanically generated files at all. So if you check out a
source tree from CVS you will need to install every utility
so that you can build all the derived files from
scratch.</para>
<para>More information about our CVS repository can be found
in <xref linkend="sec-cvs">.</para>
</listitem>
</varlistentry>
</variablelist>
<para>If you are going to do any building from sources (either
from a source distribution or the CVS repository) then you need to
read all of this manual in detail.</para>
</sect1>
<sect1 id="sec-cvs">
<title>Using the CVS repository</title>
<para>We use <ulink url="http://www.cvshome.org/">CVS</ulink> (Concurrent Version System) to keep track of our
sources for various software projects. CVS lets several people
work on the same software at the same time, allowing changes to be
checked in incrementally. </para>
<para>This section is a set of guidelines for how to use our CVS
repository, and will probably evolve in time. The main thing to
remember is that most mistakes can be undone, but if there's
anything you're not sure about feel free to bug the local CVS
meister (namely Jeff Lewis
<email>jlewis@galois.com</email>). </para>
<sect2 id="cvs-access">
<title>Getting access to the CVS Repository</title>
<para>You can access the repository in one of two ways:
read-only (<xref linkend="cvs-read-only">), or read-write (<xref
linkend="cvs-read-write">).</para>
<sect3 id="cvs-read-only">
<title>Remote Read-only CVS Access</title>
<para>Read-only access is available to anyone - there's no
need to ask us first. With read-only CVS access you can do
anything except commit changes to the repository. You can
make changes to your local tree, and still use CVS's merge
facility to keep your tree up to date, and you can generate
patches using 'cvs diff' in order to send to us for
inclusion. </para>
<para>To get read-only access to the repository:</para>
<orderedlist>
<listitem>
<para>Make sure that <application>cvs</application> is
installed on your machine.</para>
</listitem>
<listitem>
<para>Set your <literal>$CVSROOT</literal> environment variable to
<literal>:pserver:anoncvs@glass.cse.ogi.edu:/cvs</literal></para>
<para>If you set <literal>$CVSROOT</literal> in a shell script, be sure not to
have any trailing spaces on that line, otherwise CVS will respond with
a perplexing message like
<screen>/cvs : no such repository</screen></para>
</listitem>
<listitem>
<para>Run the command</para>
<screen>$ cvs login</screen>
<para>The password is simply <literal>cvs</literal>. This
sets up a file in your home directory called
<literal>.cvspass</literal>, which squirrels away the
dummy password, so you only need to do this step once.</para>
</listitem>
<listitem>
<para>Now go to <xref linkend="cvs-first">.</para>
</listitem>
</orderedlist>
</sect3>
<sect3 id="cvs-read-write">
<title>Remote Read-Write CVS Access</title>
<para>We generally supply read-write access to folk doing
serious development on some part of the source tree, when
going through us would be a pain. If you're developing some
feature, or think you have the time and inclination to fix
bugs in our sources, feel free to ask for read-write
access. There is a certain amount of responsibility that goes
with commit privileges; we are more likely to grant you access
if you've demonstrated your competence by sending us patches
via mail in the past.</para>
<para>To get remote read-write CVS access, you need to do the
following steps.</para>
<orderedlist>
<listitem>
<para>Make sure that <literal>cvs</literal> and
<literal>ssh</literal> are both installed on your
machine.</para>
</listitem>
<listitem>
<para>Generate a DSA private-key/public-key pair, thus:</para>
<screen>$ ssh-keygen -d</screen>
<para>(<literal>ssh-keygen</literal> comes with
<literal>ssh</literal>.) Running <literal>ssh-keygen
-d</literal> creates the private and public keys in
<literal>$HOME/.ssh/id_dsa</literal> and
<literal>$HOME/.ssh/id_dsa.pub</literal> respectively
(assuming you accept the standard defaults).</para>
<para><literal>ssh-keygen -d</literal> will only work if
you have Version 2 <literal>ssh</literal> installed; it
will fail harmlessly otherwise. If you only have Version
1 you can instead generate an RSA key pair using plain</para>
<screen>$ ssh-keygen</screen>
<para>Doing so creates the private and public RSA keys in
<literal>$HOME/.ssh/identity</literal> and
<literal>$HOME/.ssh/identity.pub</literal>
respectively.</para>
<para>[Deprecated.] Incidentally, you can force a Version
2 <literal>ssh</literal> to use the Version 1 protocol by
creating <literal>$HOME/config</literal> with the
following in it:</para>
<programlisting>BatchMode Yes
Host cvs.haskell.org
Protocol 1</programlisting>
<para>In both cases, <literal>ssh-keygen</literal> will
ask for a <firstterm>passphrase</firstterm>. The
passphrase is a password that protects your private key.
In response to the 'Enter passphrase' question, you can
either:</para>
<itemizedlist>
<listitem>
<para>[Recommended.] Enter a passphrase, which you
will quote each time you use CVS.
<literal>ssh-agent</literal> makes this entirely
un-tiresome.</para>
</listitem>
<listitem>
<para>[Deprecated.] Just hit return (i.e. use an empty
passphrase); then you won't need to quote the
passphrase when using CVS. The downside is that
anyone who can see into your <literal>.ssh</literal>
directory, and thereby get your private key, can mess
up the repository. So you must keep the
<literal>.ssh</literal> directory with draconian
no-access permissions.</para>
</listitem>
</itemizedlist>
<para>
<emphasis>Windows users: see the notes in <xref linkend="configure-ssh"> about <command>ssh</command> wrinkles!</emphasis>
</para>
</listitem>
<listitem>
<para>Send a message to to the CVS repository
administrator (currently Jeff Lewis
<email>jeff@galois.com</email>), containing:</para>
<itemizedlist>
<listitem>
<para>Your desired user-name.</para>
</listitem>
<listitem>
<para>Your <literal>.ssh/id_dsa.pub</literal> (or
<literal>.ssh/identity.pub</literal>).</para>
</listitem>
</itemizedlist>
<para>He will set up your account.</para>
</listitem>
<listitem>
<para>Set the following environment variables:</para>
<itemizedlist>
<listitem>
<para>
<constant>$HOME</constant>: points to your home directory. This is where CVS
will look for its <filename>.cvsrc</filename> file.
</para>
</listitem>
<listitem>
<para>
<constant>$CVS_RSH</constant> to <filename>ssh</filename>
</para>
<para>[Windows users.] Setting your <literal>CVS_RSH</literal> to
<literal>ssh</literal> assumes that your CVS client
understands how to execute shell script
("#!"s,really), which is what
<literal>ssh</literal> is. This may not be the case on
Win32 platforms, so in that case set <literal>CVS_RSH</literal> to
<literal>ssh1</literal>.</para>
</listitem>
<listitem>
<para><literal>$CVSROOT</literal> to
<literal>:ext:</literal><replaceable>your-username</replaceable>
<literal>@cvs.haskell.org:/home/cvs/root</literal>
where <replaceable>your-username</replaceable> is your user name on
<literal>cvs.haskell.org</literal>.
</para>
<para>The <literal>CVSROOT</literal> environment variable will
be recorded in the checked-out tree, so you don't need to set
this every time. </para>
</listitem>
<listitem>
<para>
<constant>$CVSEDITOR</constant>: <filename>bin/gnuclient.exe</filename>
if you want to use an Emacs buffer for typing in those long commit messages.
</para>
</listitem>
<listitem>
<para>
<constant>$SHELL</constant>: To use bash as the shell in Emacs, you need to
set this to point to <filename>bash.exe</filename>.
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>
Put the following in <filename>$HOME/.cvsrc</filename>:
</para>
<programlisting>checkout -P
release -d
update -P
diff -u</programlisting>
<para>
These are the default options for the specified CVS commands,
and represent better defaults than the usual ones. (Feel
free to change them.)
</para>
<para>
[Windows users.] Filenames starting with <filename>.</filename> were illegal in
the 8.3 DOS filesystem, but that restriction should have
been lifted by now (i.e., you're using VFAT or later filesystems.) If
you're still having problems creating it, don't worry; <filename>.cvsrc</filename> is entirely
optional.
</para>
</listitem>
</orderedlist>
<para>[Experts.] Once your account is set up, you can get
access from other machines without bothering Jeff, thus:</para>
<orderedlist>
<listitem>
<para>Generate a public/private key pair on the new
machine.</para>
</listitem>
<listitem>
<para>Use ssh to log in to
<literal>cvs.haskell.org</literal>, from your old
machine.</para>
</listitem>
<listitem>
<para>Add the public key for the new machine to the file
<literal>$HOME/ssh/authorized_keys</literal> on
<literal>cvs.haskell.org</literal>.
(<literal>authorized_keys2</literal>, I think, for Version
2 protocol.)</para>
</listitem>
<listitem>
<para>Make sure that the new version of
<literal>authorized_keys</literal> still has 600 file
permissions.</para>
</listitem>
</orderedlist>
</sect3>
</sect2>
<sect2 id="cvs-first">
<title>Checking Out a Source Tree</title>
<itemizedlist>
<listitem>
<para>Make sure you set your <literal>CVSROOT</literal>
environment variable according to either of the remote
methods above. The Approved Way to check out a source tree
is as follows:</para>
<screen>$ cvs checkout fpconfig</screen>
<para>At this point you have a new directory called
<literal>fptools</literal> which contains the basic stuff
for the fptools suite, including the configuration files and
some other junk. </para>
<para>[Windows users.] The following messages appear to be harmless:
<screen>setsockopt IPTOS_LOWDELAY: Invalid argument
setsockopt IPTOS_THROUGHPUT: Invalid argument</screen>
</para>
<para>You can call the fptools directory whatever you like,
CVS won't mind: </para>
<screen>$ mv fptools <replaceable>directory</replaceable></screen>
<para> NB: after you've read the CVS manual you might be
tempted to try</para>
<screen>$ cvs checkout -d <replaceable>directory</replaceable> fpconfig</screen>
<para>instead of checking out <literal>fpconfig</literal>
and then renaming it. But this doesn't work, and will
result in checking out the entire repository instead of just
the <literal>fpconfig</literal> bit.</para>
<screen>$ cd <replaceable>directory</replaceable>
$ cvs checkout ghc hslibs libraries</screen>
<para>The second command here checks out the relevant
modules you want to work on. For a GHC build, for instance,
you need at least the <literal>ghc</literal>,
<literal>hslibs</literal> and <literal>libraries</literal>
modules (for a full list of the projects available, see
<xref linkend="projects">).</para>
<para>Remember that if you do not have
<literal>happy</literal> and/or <literal>Alex</literal>
installed, you need to check them out as well.</para>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="cvs-committing">
<title>Committing Changes</title>
<para>This is only if you have read-write access to the
repository. For anoncvs users, CVS will issue a "read-only
repository" error if you try to commit changes.</para>
<itemizedlist>
<listitem>
<para>Build the software, if necessary. Unless you're just
working on documentation, you'll probably want to build the
software in order to test any changes you make.</para>
</listitem>
<listitem>
<para>Make changes. Preferably small ones first.</para>
</listitem>
<listitem>
<para>Test them. You can see exactly what changes you've
made by using the <literal>cvs diff</literal> command:</para>
<screen>$ cvs diff</screen>
<para>lists all the changes (using the
<literal>diff</literal> command) in and below the current
directory. In emacs, <literal>C-c C-v =</literal> runs
<literal>cvs diff</literal> on the current buffer and shows
you the results.</para>
</listitem>
<listitem>
<para>If you changed something in the
<literal>fptools/libraries</literal> subdirectories, also run
<literal>make html</literal> to check if the documentation can
be generated successfully, too.</para>
</listitem>
<listitem>
<para>Before checking in a change, you need to update your
source tree:</para>
<screen>$ cd fptools
$ cvs update</screen>
<para>This pulls in any changes that other people have made,
and merges them with yours. If there are any conflicts, CVS
will tell you, and you'll have to resolve them before you
can check your changes in. The documentation describes what
to do in the event of a conflict.</para>
<para>It's not always necessary to do a full cvs update
before checking in a change, since CVS will always tell you
if you try to check in a file that someone else has changed.
However, you should still update at regular intervals to
avoid making changes that don't work in conjuction with
changes that someone else made. Keeping an eye on what goes
by on the mailing list can help here.</para>
</listitem>
<listitem>
<para>When you're happy that your change isn't going to
break anything, check it in. For a one-file change:</para>
<screen>$ cvs commit <replaceable>filename</replaceable></screen>
<para>CVS will then pop up an editor for you to enter a
"commit message", this is just a short description
of what your change does, and will be kept in the history of
the file.</para>
<para>If you're using emacs, simply load up the file into a
buffer and type <literal>C-x C-q</literal>, and emacs will
prompt for a commit message and then check in the file for
you.</para>
<para>For a multiple-file change, things are a bit
trickier. There are several ways to do this, but this is the
way I find easiest. First type the commit message into a
temporary file. Then either</para>
<screen>$ cvs commit -F <replaceable>commit-message</replaceable> <replaceable>file_1</replaceable> .... <replaceable>file_n</replaceable></screen>
<para>or, if nothing else has changed in this part of the
source tree, </para>
<screen>$ cvs commit -F <replaceable>commit-message</replaceable> <replaceable>directory</replaceable></screen>
<para>where <replaceable>directory</replaceable> is a common
parent directory for all your changes, and
<replaceable>commit-message</replaceable> is the name of the
file containing the commit message.</para>
<para>Shortly afterwards, you'll get some mail from the
relevant mailing list saying which files changed, and giving
the commit message. For a multiple-file change, you should
still get only <emphasis>one</emphasis> message.</para>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="cvs-update">
<title>Updating Your Source Tree</title>
<para>It can be tempting to cvs update just part of a source
tree to bring in some changes that someone else has made, or
before committing your own changes. This is NOT RECOMMENDED!
Quite often changes in one part of the tree are dependent on
changes in another part of the tree (the
<literal>mk/*.mk</literal> files are a good example where
problems crop up quite often). Having an inconsistent tree is a
major cause of headaches. </para>
<para>So, to avoid a lot of hassle, follow this recipe for
updating your tree:</para>
<screen>$ cd fptools
$ cvs update -P 2>&1 | tee log</screen>
<para>Look at the log file, and fix any conflicts (denoted by a
<quote>C</quote> in the first column). New directories may have
appeared in the repository; CVS doesn't check these out by
default, so to get new directories you have to explicitly do
<screen>$ cvs update -d</screen>
in each project subdirectory. Don't do this at the top level,
because then <emphasis>all</emphasis> the projects will be
checked out.</para>
<para>If you're using multiple build trees, then for every build
tree you have pointing at this source tree, you need to update
the links in case any new files have appeared: </para>
<screen>$ cd <replaceable>build-tree</replaceable>
$ lndir <replaceable>source-tree</replaceable></screen>
<para>Some files might have been removed, so you need to remove
the links pointing to these non-existent files:</para>
<screen>$ find . -xtype l -exec rm '{}' \;</screen>
<para>To be <emphasis>really</emphasis> safe, you should do
</para>
<screen>$ gmake all</screen>
<para>from the top-level, to update the dependencies and build
any changed files. </para>
</sect2>
<sect2 id="cvs-tags">
<title>GHC Tag Policy</title>
<para>If you want to check out a particular version of GHC,
you'll need to know how we tag versions in the repository. The
policy (as of 4.04) is:</para>
<itemizedlist>
<listitem>
<para>The tree is branched before every major release. The
branch tag is <literal>ghc-x-xx-branch</literal>, where
<literal>x-xx</literal> is the version number of the release
with the <literal>'.'</literal> replaced by a
<literal>'-'</literal>. For example, the 4.04 release lives
on <literal>ghc-4-04-branch</literal>.</para>
</listitem>
<listitem>
<para>The release itself is tagged with
<literal>ghc-x-xx</literal> (on the branch). eg. 4.06 is
called <literal>ghc-4-06</literal>.</para>
</listitem>
<listitem>
<para>We didn't always follow these guidelines, so to see
what tags there are for previous versions, do <literal>cvs
log</literal> on a file that's been around for a while (like
<literal>fptools/ghc/README</literal>).</para>
</listitem>
</itemizedlist>
<para>So, to check out a fresh GHC 4.06 tree you would
do:</para>
<screen>$ cvs co -r ghc-4-06 fpconfig
$ cd fptools
$ cvs co -r ghc-4-06 ghc hslibs</screen>
</sect2>
<sect2 id="cvs-hints">
<title>General Hints</title>
<itemizedlist>
<listitem>
<para>As a general rule: commit changes in small units,
preferably addressing one issue or implementing a single
feature. Provide a descriptive log message so that the
repository records exactly which changes were required to
implement a given feature/fix a bug. I've found this
<emphasis>very</emphasis> useful in the past for finding out
when a particular bug was introduced: you can just wind back
the CVS tree until the bug disappears.</para>
</listitem>
<listitem>
<para>Keep the sources at least *buildable* at any given
time. No doubt bugs will creep in, but it's quite easy to
ensure that any change made at least leaves the tree in a
buildable state. We do nightly builds of GHC to keep an eye
on what things work/don't work each day and how we're doing
in relation to previous verions. This idea is truely wrecked
if the compiler won't build in the first place!</para>
</listitem>
<listitem>
<para>To check out extra bits into an already-checked-out
tree, use the following procedure. Suppose you have a
checked-out fptools tree containing just ghc, and you want
to add nofib to it:</para>
<screen>$ cd fptools
$ cvs checkout nofib</screen>
<para>or: </para>
<screen>$ cd fptools
$ cvs update -d nofib</screen>
<para>(the -d flag tells update to create a new
directory). If you just want part of the nofib suite, you
can do </para>
<screen>$ cd fptools
$ cvs checkout nofib/spectral</screen>
<para>This works because <literal>nofib</literal> is a
module in its own right, and spectral is a subdirectory of
the nofib module. The path argument to checkout must always
start with a module name. There's no equivalent form of this
command using <literal>update</literal>.</para>
</listitem>
</itemizedlist>
</sect2>
</sect1>
<sect1 id="projects">
<title>What projects are there?</title>
<para>The <literal>fptools</literal> suite consists of several
<firstterm>projects</firstterm>, most of which can be downloaded,
built and installed individually. Each project corresponds to a
subdirectory in the source tree, and if checking out from CVS then
each project can be checked out individually by sitting in the top
level of your source tree and typing <command>cvs checkout
<replaceable>project</replaceable></command>.</para>
<para>Here is a list of the projects currently available:</para>
<variablelist>
<varlistentry>
<term>
<literal>alex</literal>
<indexterm><primary><literal>alex</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The <ulink
url="http://www.haskell.org/alex/">Alex</ulink> lexical
analyser generator for Haskell.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>ghc</literal>
<indexterm><primary><literal>ghc</literal></primary>
<secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The <ulink url="http://www.haskell.org/ghc/">Glasgow
Haskell Compiler</ulink> (minus libraries). Absolutely
required for building GHC.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>glafp-utils</literal>
<indexterm><primary><literal>glafp-utils</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>Utility programs, some of which are used by the
build/installation system. Required for pretty much
everything.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>greencard</literal>
<indexterm><primary><literal>greencard</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The <ulink
url="http://www.haskell.org/greencard/">GreenCard</ulink>
system for generating Haskell foreign function
interfaces.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>haggis</literal>
<indexterm><primary><literal>haggis</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The <ulink
url="http://www.dcs.gla.ac.uk/fp/software/haggis/">Haggis</ulink>
Haskell GUI framework.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>haddock</literal>
<indexterm><primary><literal>haddock</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The <ulink
url="http://www.haskell.org/haddock/">Haddock</ulink>
documentation tool.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>happy</literal>
<indexterm><primary><literal>happy</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The <ulink
url="http://www.haskell.org/happy/">Happy</ulink> Parser
generator.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>hdirect</literal>
<indexterm><primary><literal>hdirect</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The <ulink
url="http://www.haskell.org/hdirect/">H/Direct</ulink>
Haskell interoperability tool.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>hood</literal>
<indexterm><primary><literal>hood</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The <ulink url="http://www.haskell.org/hood/">Haskell
Object Observation Debugger</ulink>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>hslibs</literal>
<indexterm><primary><literal>hslibs</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>Supplemental libraries for GHC
(<emphasis>required</emphasis> for building GHC).</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>libraries</literal>
<indexterm><primary><literal></literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>Hierarchical Haskell library suite
(<emphasis>required</emphasis> for building GHC).</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>mhms</literal>
<indexterm><primary><literal></literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The Modular Haskell Metric System.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>nofib</literal>
<indexterm><primary><literal>nofib</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>The NoFib suite: A collection of Haskell programs used
primarily for benchmarking.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>testsuite</literal>
<indexterm><primary><literal>testsuite</literal></primary><secondary>project</secondary></indexterm>
</term>
<listitem>
<para>A testing framework, including GHC's regression test
suite.</para>
</listitem>
</varlistentry>
</variablelist>
<para>So, to build GHC you need at least the
<literal>ghc</literal>, <literal>libraries</literal> and
<literal>hslibs</literal> projects (a GHC source distribution will
already include the bits you need).</para>
</sect1>
<sect1 id="sec-build-checks">
<title>Things to check before you start</title>
<para>Here's a list of things to check before you get
started.</para>
<orderedlist>
<listitem><para><indexterm><primary>Disk space needed</primary></indexterm>Disk
space needed: from about 100Mb for a basic GHC
build, up to probably 500Mb for a GHC build with everything
included (libraries built several different ways,
etc.).</para>
</listitem>
<listitem>
<para>Use an appropriate machine / operating system. <xref
linkend="sec-port-info"> lists the supported platforms; if
yours isn't amongst these then you can try porting GHC (see
<xref linkend="sec-porting-ghc">).</para>
</listitem>
<listitem>
<para>Be sure that the “pre-supposed” utilities are
installed. <xref linkend="sec-pre-supposed">
elaborates.</para>
</listitem>
<listitem>
<para>If you have any problem when building or installing the
Glasgow tools, please check the “known pitfalls” (<xref
linkend="sec-build-pitfalls">). Also check the FAQ for the
version you're building, which is part of the User's Guide and
available on the <ulink url="http://www.haskell.org/ghc/" >GHC web
site</ulink>.</para>
<indexterm><primary>bugs</primary><secondary>known</secondary></indexterm>
<para>If you feel there is still some shortcoming in our
procedure or instructions, please report it.</para>
<para>For GHC, please see the <ulink
url="http://www.haskell.org/ghc/docs/latest/set/bug-reporting.html">bug-reporting
section of the GHC Users' Guide</ulink>, to maximise the
usefulness of your report.</para>
<indexterm><primary>bugs</primary><secondary>seporting</secondary></indexterm>
<para>If in doubt, please send a message to
<email>glasgow-haskell-bugs@haskell.org</email>.
<indexterm><primary>bugs</primary><secondary>mailing
list</secondary></indexterm></para>
</listitem>
</orderedlist>
</sect1>
<sect1 id="sec-port-info">
<title>What machines the Glasgow tools run on</title>
<indexterm><primary>ports</primary><secondary>GHC</secondary></indexterm>
<indexterm><primary>GHC</primary><secondary>ports</secondary></indexterm>
<indexterm><primary>platforms</primary><secondary>supported</secondary></indexterm>
<para>The main question is whether or not the Haskell compiler
(GHC) runs on your platform.</para>
<para>A “platform” is a
architecture/manufacturer/operating-system combination, such as
<literal>sparc-sun-solaris2</literal>. Other common ones are
<literal>alpha-dec-osf2</literal>,
<literal>hppa1.1-hp-hpux9</literal>,
<literal>i386-unknown-linux</literal>,
<literal>i386-unknown-solaris2</literal>,
<literal>i386-unknown-freebsd</literal>,
<literal>i386-unknown-cygwin32</literal>,
<literal>m68k-sun-sunos4</literal>,
<literal>mips-sgi-irix5</literal>,
<literal>sparc-sun-sunos4</literal>,
<literal>sparc-sun-solaris2</literal>,
<literal>powerpc-ibm-aix</literal>.</para>
<para>Some libraries may only work on a limited number of
platforms; for example, a sockets library is of no use unless the
operating system supports the underlying BSDisms.</para>
<sect2>
<title>What platforms the Haskell compiler (GHC) runs on</title>
<indexterm><primary>fully-supported platforms</primary></indexterm>
<indexterm><primary>native-code generator</primary></indexterm>
<indexterm><primary>registerised ports</primary></indexterm>
<indexterm><primary>unregisterised ports</primary></indexterm>
<para>The GHC hierarchy of Porting Goodness: (a) Best is a
native-code generator; (b) next best is a
“registerised” port; (c) the bare minimum is an
“unregisterised” port.
(“Unregisterised” is so terrible that we won't say
more about it).</para>
<para>We use Sparcs running Solaris 2.7 and x86 boxes running
FreeBSD and Linux, so those are the best supported platforms,
unsurprisingly.</para>
<para>Here's everything that's known about GHC ports. We
identify platforms by their “canonical”
CPU/Manufacturer/OS triple.</para>
<variablelist>
<varlistentry>
<term>alpha-dec-{osf,linux,freebsd,openbsd,netbsd}:
<indexterm><primary>alpha-dec-osf</primary></indexterm>
<indexterm><primary>alpha-dec-linux</primary></indexterm>
<indexterm><primary>alpha-dec-freebsd</primary></indexterm>
<indexterm><primary>alpha-dec-openbsd</primary></indexterm>
<indexterm><primary>alpha-dec-netbsd</primary></indexterm>
</term>
<listitem>
<para>The OSF port is currently working (as of GHC version
5.02.1) and well supported. The native code generator is
currently non-working. Other operating systems will
require some minor porting.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>sparc-sun-sunos4
<indexterm><primary>sparc-sun-sunos4</primary></indexterm>
</term>
<listitem>
<para>Probably works with minor tweaks, hasn't been tested
for a while.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>sparc-sun-solaris2
<indexterm><primary>sparc-sun-solaris2</primary></indexterm>
</term>
<listitem>
<para>Fully supported (at least for Solaris 2.7 and 2.6),
including native-code generator.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>sparc-unknown-openbsd
<indexterm><primary>sparc-unknown-openbsd</primary></indexterm>
</term>
<listitem>
<para>Supported, including native-code generator. The
same should also be true of NetBSD</para>
</listitem>
</varlistentry>
<varlistentry>
<term>hppa1.1-hp-hpux (HP-PA boxes running HPUX 9.x)
<indexterm><primary>hppa1.1-hp-hpux</primary></indexterm>
</term>
<listitem>
<para>A registerised port is available for version 4.08,
but GHC hasn't been built on that platform since (as far
as we know). No native-code generator.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>i386-unknown-linux (PCs running Linux, ELF binary format)
<indexterm><primary>i386-*-linux</primary></indexterm>
</term>
<listitem>
<para>GHC works registerised and has a native code
generator. You <emphasis>must</emphasis> have GCC 2.7.x
or later. NOTE about <literal>glibc</literal> versions:
GHC binaries built on a system running <literal>glibc
2.0</literal> won't work on a system running
<literal>glibc 2.1</literal>, and vice versa. In general,
don't expect compatibility between
<literal>glibc</literal> versions, even if the shared
library version hasn't changed.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>i386-unknown-freebsd (PCs running FreeBSD 2.2 or higher)
<indexterm><primary>i386-unknown-freebsd</primary></indexterm>
</term>
<listitem>
<para>GHC works registerised. Pre-built packages are
available in the native package format, so if you just
need binaries you're better off just installing the
package (it might even be on your installation
CD!).</para>
</listitem>
</varlistentry>
<varlistentry>
<term>i386-unknown-openbsd (PCs running OpenBSD)
<indexterm><primary>i386-unknown-openbsd</primary></indexterm>
</term>
<listitem>
<para>Supported, with native code generator. Packages are
available through the ports system in the native package
format.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>i386-unknown-netbsd (PCs running NetBSD)
<indexterm><primary>i386-unknown-netbsd</primary></indexterm>
</term>
<listitem>
<para>Will require some minor porting effort, but should
work registerised.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>i386-unknown-mingw32 (PCs running Windows)
<indexterm><primary>i386-unknown-mingw32</primary></indexterm>
</term>
<listitem>
<para>Fully supported under Win9x, WinNT, Win2k, and
WinXP. Includes a native code generator. Building from
source requires a recent <ulink
url="http://www.cygwin.com/">Cygwin</ulink> distribution
to be installed.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ia64-unknown-linux
<indexterm><primary>ia64-unknown-linux</primary></indexterm>
</term>
<listitem>
<para>Supported, except there is no native code
generator.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>x86_64-unknown-linux
<indexterm><primary>x86_64-unknown-linux</primary></indexterm>
</term>
<listitem>
<para>GHC currently works unregisterised. A registerised
port is in progress.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>amd64-unknown-openbsd
<indexterm><primary>amd64-unknown-linux</primary></indexterm>
</term>
<listitem>
<para>(This is the same as x86_64-unknown-openbsd). GHC
currently works unregisterised. A registerised port is in
progress.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>mips-sgi-irix5
<indexterm><primary>mips-sgi-irix[5-6]</primary></indexterm>
</term>
<listitem>
<para>Port has worked in the past, but hasn't been tested
for some time (and will certainly have rotted in various
ways). As usual, we don't have access to machines and
there hasn't been an overwhelming demand for this port,
but feel free to get in touch.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>mips64-sgi-irix6
<indexterm><primary>mips-sgi-irix6</primary></indexterm>
</term>
<listitem>
<para>GHC currently works unregisterised.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>powerpc-ibm-aix
<indexterm><primary>powerpc-ibm-aix</primary></indexterm>
</term>
<listitem>
<para>Port currently doesn't work, needs some minimal
porting effort. As usual, we don't have access to
machines and there hasn't been an overwhelming demand for
this port, but feel free to get in touch.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>powerpc-apple-darwin
<indexterm><primary>powerpc-apple-darwin</primary></indexterm>
</term>
<listitem>
<para>Supported registerised. Native code generator is
almost working.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>powerpc-apple-linux
<indexterm><primary>powerpc-apple-linux</primary></indexterm>
</term>
<listitem>
<para>Not supported (yet).</para>
</listitem>
</varlistentry>
</variablelist>
<para>Various other systems have had GHC ported to them in the
distant past, including various Motorola 68k boxes. The 68k
support still remains, but porting to one of these systems will
certainly be a non-trivial task.</para>
</sect2>
<sect2>
<title>What machines the other tools run on</title>
<para>Unless you hear otherwise, the other tools work if GHC
works.</para>
</sect2>
</sect1>
<sect1 id="sec-pre-supposed">
<title>Installing pre-supposed utilities</title>
<indexterm><primary>pre-supposed utilities</primary></indexterm>
<indexterm><primary>utilities, pre-supposed</primary></indexterm>
<para>Here are the gory details about some utility programs you
may need; <command>perl</command>, <command>gcc</command> and
<command>happy</command> are the only important
ones. (PVM<indexterm><primary>PVM</primary></indexterm> is
important if you're going for Parallel Haskell.) The
<command>configure</command><indexterm><primary>configure</primary></indexterm>
script will tell you if you are missing something.</para>
<variablelist>
<varlistentry>
<term>GHC
<indexterm><primary>pre-supposed: GHC</primary></indexterm>
<indexterm><primary>GHC, pre-supposed</primary></indexterm>
</term>
<listitem>
<para>GHC is required to build many of the tools, including
GHC itself. If you need to port GHC to your platform
because there isn't a binary distribution of GHC available,
then see <xref linkend="sec-porting-ghc">.</para>
<para>Which version of GHC you need will depend on the
packages you intend to build. GHC itself will normally
build using one of several older versions of itself - check
the announcement or release notes for details.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Perl
<indexterm><primary>pre-supposed: Perl</primary></indexterm>
<indexterm><primary>Perl, pre-supposed</primary></indexterm>
</term>
<listitem>
<para><emphasis>You have to have Perl to proceed!</emphasis>
Perl version 5 at least is required. GHC has been known to
tickle bugs in Perl, so if you find that Perl crashes when
running GHC try updating (or downgrading) your Perl
installation. Versions of Perl that we use and are known to
be fairly stable are 5.005 and 5.6.1.</para>
<para>For Win32 platforms, you should use the binary
supplied in the InstallShield (copy it to
<filename>/bin</filename>). The Cygwin-supplied Perl seems
not to work.</para>
<para>Perl should be put somewhere so that it can be invoked
by the <literal>#!</literal> script-invoking
mechanism. The full pathname may need to be less than 32
characters long on some systems.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>GNU C (<command>gcc</command>)
<indexterm><primary>pre-supposed: GCC (GNU C compiler)</primary></indexterm>
<indexterm><primary>GCC (GNU C compiler), pre-supposed</primary></indexterm>
</term>
<listitem>
<para>We recommend using GCC version 2.95.2 on all
platforms. Failing that, version 2.7.2 is stable on most
platforms. Earlier versions of GCC can be assumed not to
work, and versions in between 2.7.2 and 2.95.2 (including
<command>egcs</command>) have varying degrees of stability
depending on the platform.</para>
<para>GCC 3.2 is currently known to have problems building
GHC on Sparc, but is stable on x86.</para>
<para>If your GCC dies with “internal error” on
some GHC source file, please let us know, so we can report
it and get things improved. (Exception: on x86
boxes—you may need to fiddle with GHC's
<option>-monly-N-regs</option> option; see the User's
Guide)</para>
</listitem>
</varlistentry>
<varlistentry>
<term>GNU Make
<indexterm><primary>make</primary><secondary>GNU</secondary></indexterm>
</term>
<listitem>
<para>The fptools build system makes heavy use of features
specific to GNU <command>make</command>, so you must have
this installed in order to build any of the fptools
suite.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Happy
<indexterm><primary>Happy</primary></indexterm>
</term>
<listitem>
<para>Happy is a parser generator tool for Haskell, and is
used to generate GHC's parsers. Happy is written in
Haskell, and is a project in the CVS repository
(<literal>fptools/happy</literal>). It can be built from
source, but bear in mind that you'll need GHC installed in
order to build it. To avoid the chicken/egg problem,
install a binary distribution of either Happy or GHC to get
started. Happy distributions are available from <ulink
url="http://www.haskell.org/happy/">Happy's Web
Page</ulink>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Alex
<indexterm><primary>Alex</primary></indexterm>
</term>
<listitem>
<para>Alex is a lexical-analyser generator for Haskell,
which GHC uses to generate its lexer. Like Happy, Alex is
written in Haskell and is a project in the CVS repository.
Alex distributions are available from <ulink
url="http://www.haskell.org/alex/">Alex's Web
Page</ulink>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>autoconf
<indexterm><primary>pre-supposed: autoconf</primary></indexterm>
<indexterm><primary>autoconf, pre-supposed</primary></indexterm>
</term>
<listitem>
<para>GNU autoconf is needed if you intend to build from the
CVS sources, it is <emphasis>not</emphasis> needed if you
just intend to build a standard source distribution.</para>
<para>Version 2.52 or later of the autoconf package is required.
NB. version 2.13 will no longer work, as of GHC version
6.1.</para>
<para><command>autoreconf</command> (from the autoconf package)
recursively builds <command>configure</command> scripts from
the corresponding <filename>configure.ac</filename> and
<filename>aclocal.m4</filename> files. If you modify one of
the latter files, you'll need <command>autoreconf</command> to
rebuild the corresponding <filename>configure</filename>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><command>sed</command>
<indexterm><primary>pre-supposed: sed</primary></indexterm>
<indexterm><primary>sed, pre-supposed</primary></indexterm>
</term>
<listitem>
<para>You need a working <command>sed</command> if you are
going to build from sources. The build-configuration stuff
needs it. GNU sed version 2.0.4 is no good! It has a bug
in it that is tickled by the build-configuration. 2.0.5 is
OK. Others are probably OK too (assuming we don't create too
elaborate configure scripts.)</para>
</listitem>
</varlistentry>
</variablelist>
<para>One <literal>fptools</literal> project is worth a quick note
at this point, because it is useful for all the others:
<literal>glafp-utils</literal> contains several utilities which
aren't particularly Glasgow-ish, but Occasionally Indispensable.
Like <command>lndir</command> for creating symbolic link
trees.</para>
<sect2 id="pre-supposed-gph-tools">
<title>Tools for building parallel GHC (GPH)</title>
<variablelist>
<varlistentry>
<term>PVM version 3:
<indexterm><primary>pre-supposed: PVM3 (Parallel Virtual Machine)</primary></indexterm>
<indexterm><primary>PVM3 (Parallel Virtual Machine), pre-supposed</primary></indexterm>
</term>
<listitem>
<para>PVM is the Parallel Virtual Machine on which
Parallel Haskell programs run. (You only need this if you
plan to run Parallel Haskell. Concurrent Haskell, which
runs concurrent threads on a uniprocessor doesn't need
it.) Underneath PVM, you can have (for example) a network
of workstations (slow) or a multiprocessor box
(faster).</para>
<para>The current version of PVM is 3.3.11; we use 3.3.7.
It is readily available on the net; I think I got it from
<literal>research.att.com</literal>, in
<filename>netlib</filename>.</para>
<para>A PVM installation is slightly quirky, but easy to
do. Just follow the <filename>Readme</filename>
instructions.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><command>bash</command>:
<indexterm><primary>bash, presupposed (Parallel Haskell only)</primary></indexterm>
</term>
<listitem>
<para>Sadly, the <command>gr2ps</command> script, used to
convert “parallelism profiles” to PostScript,
is written in Bash (GNU's Bourne Again shell). This bug
will be fixed (someday).</para>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2 id="pre-supposed-other-tools">
<title>Other useful tools</title>
<variablelist>
<varlistentry>
<term>Flex
<indexterm><primary>pre-supposed: flex</primary></indexterm>
<indexterm><primary>flex, pre-supposed</primary></indexterm>
</term>
<listitem>
<para>This is a quite-a-bit-better-than-Lex lexer. Used
to build a couple of utilities in
<literal>glafp-utils</literal>. Depending on your
operating system, the supplied <command>lex</command> may
or may not work; you should get the GNU version.</para>
</listitem>
</varlistentry>
</variablelist>
<para>More tools are required if you want to format the documentation
that comes with GHC and other fptools projects. See <xref
linkend="building-docs">.</para>
</sect2>
</sect1>
<sect1 id="sec-building-from-source">
<title>Building from source</title>
<indexterm><primary>Building from source</primary></indexterm>
<indexterm><primary>Source, building from</primary></indexterm>
<para>You've been rash enough to want to build some of the Glasgow
Functional Programming tools (GHC, Happy, nofib, etc.) from
source. You've slurped the source, from the CVS repository or
from a source distribution, and now you're sitting looking at a
huge mound of bits, wondering what to do next.</para>
<para>Gingerly, you type <command>make</command>. Wrong
already!</para>
<para>This rest of this guide is intended for duffers like me, who
aren't really interested in Makefiles and systems configurations,
but who need a mental model of the interlocking pieces so that
they can make them work, extend them consistently when adding new
software, and lay hands on them gently when they don't
work.</para>
<sect2 id="quick-start">
<title>Quick Start</title>
<para>If you are starting from a source distribution, and just
want a completely standard build, then the following should
work:</para>
<screen>$ autoreconf
$ ./configure
$ make
$ make install</screen>
<para>For GHC, this will do a 2-stage bootstrap build of the
compiler, with profiling libraries, and install the
results.</para>
<para>If you want to do anything at all non-standard, or you
want to do some development, read on...</para>
</sect2>
<sect2 id="sec-source-tree">
<title>Your source tree</title>
<para>The source code is held in your <emphasis>source
tree</emphasis>. The root directory of your source tree
<emphasis>must</emphasis> contain the following directories and
files:</para>
<itemizedlist>
<listitem>
<para><filename>Makefile</filename>: the root
Makefile.</para>
</listitem>
<listitem>
<para><filename>mk/</filename>: the directory that contains
the main Makefile code, shared by all the
<literal>fptools</literal> software.</para>
</listitem>
<listitem>
<para><filename>configure.ac</filename>,
<filename>config.sub</filename>,
<filename>config.guess</filename>: these files support the
configuration process.</para>
</listitem>
<listitem>
<para><filename>install-sh</filename>.</para>
</listitem>
</itemizedlist>
<para>All the other directories are individual
<emphasis>projects</emphasis> of the <literal>fptools</literal>
system—for example, the Glasgow Haskell Compiler
(<literal>ghc</literal>), the Happy parser generator
(<literal>happy</literal>), the <literal>nofib</literal>
benchmark suite, and so on. You can have zero or more of these.
Needless to say, some of them are needed to build others.</para>
<para>The important thing to remember is that even if you want
only one project (<literal>happy</literal>, say), you must have
a source tree whose root directory contains
<filename>Makefile</filename>, <filename>mk/</filename>,
<filename>configure.ac</filename>, and the project(s) you want
(<filename>happy/</filename> in this case). You cannot get by
with just the <filename>happy/</filename> directory.</para>
</sect2>
<sect2>
<title>Build trees</title>
<indexterm><primary>build trees</primary></indexterm>
<indexterm><primary>link trees, for building</primary></indexterm>
<para>If you just want to build the software once on a single
platform, then your source tree can also be your build tree, and
you can skip the rest of this section.</para>
<para>We often want to build multiple versions of our software
for different architectures, or with different options
(e.g. profiling). It's very desirable to share a single copy of
the source code among all these builds.</para>
<para>So for every source tree we have zero or more
<emphasis>build trees</emphasis>. Each build tree is initially
an exact copy of the source tree, except that each file is a
symbolic link to the source file, rather than being a copy of
the source file. There are “standard” Unix
utilities that make such copies, so standard that they go by
different names:
<command>lndir</command><indexterm><primary>lndir</primary></indexterm>,
<command>mkshadowdir</command><indexterm><primary>mkshadowdir</primary></indexterm>
are two (If you don't have either, the source distribution
includes sources for the X11
<command>lndir</command>—check out
<filename>fptools/glafp-utils/lndir</filename>). See <xref
linkend="sec-storysofar"> for a typical invocation.</para>
<para>The build tree does not need to be anywhere near the
source tree in the file system. Indeed, one advantage of
separating the build tree from the source is that the build tree
can be placed in a non-backed-up partition, saving your systems
support people from backing up untold megabytes of
easily-regenerated, and rapidly-changing, gubbins. The golden
rule is that (with a single exception—<xref
linkend="sec-build-config">) <emphasis>absolutely everything in
the build tree is either a symbolic link to the source tree, or
else is mechanically generated</emphasis>. It should be
perfectly OK for your build tree to vanish overnight; an hour or
two compiling and you're on the road again.</para>
<para>You need to be a bit careful, though, that any new files
you create (if you do any development work) are in the source
tree, not a build tree!</para>
<para>Remember, that the source files in the build tree are
<emphasis>symbolic links</emphasis> to the files in the source
tree. (The build tree soon accumulates lots of built files like
<filename>Foo.o</filename>, as well.) You can
<emphasis>delete</emphasis> a source file from the build tree
without affecting the source tree (though it's an odd thing to
do). On the other hand, if you <emphasis>edit</emphasis> a
source file from the build tree, you'll edit the source-tree
file directly. (You can set up Emacs so that if you edit a
source file from the build tree, Emacs will silently create an
edited copy of the source file in the build tree, leaving the
source file unchanged; but the danger is that you think you've
edited the source file whereas actually all you've done is edit
the build-tree copy. More commonly you do want to edit the
source file.)</para>
<para>Like the source tree, the top level of your build tree
must be (a linked copy of) the root directory of the
<literal>fptools</literal> suite. Inside Makefiles, the root of
your build tree is called
<constant>$(FPTOOLS_TOP)</constant><indexterm><primary>FPTOOLS_TOP</primary></indexterm>.
In the rest of this document path names are relative to
<constant>$(FPTOOLS_TOP)</constant> unless
otherwise stated. For example, the file
<filename>ghc/mk/target.mk</filename> is actually
<filename>$(FPTOOLS_TOP)/ghc/mk/target.mk</filename>.</para>
</sect2>
<sect2 id="sec-build-config">
<title>Getting the build you want</title>
<para>When you build <literal>fptools</literal> you will be
compiling code on a particular <emphasis>host
platform</emphasis>, to run on a particular <emphasis>target
platform</emphasis> (usually the same as the host
platform)<indexterm><primary>platform</primary></indexterm>.
The difficulty is that there are minor differences between
different platforms; minor, but enough that the code needs to be
a bit different for each. There are some big differences too:
for a different architecture we need to build GHC with a
different native-code generator.</para>
<para>There are also knobs you can turn to control how the
<literal>fptools</literal> software is built. For example, you
might want to build GHC optimised (so that it runs fast) or
unoptimised (so that you can compile it fast after you've
modified it. Or, you might want to compile it with debugging on
(so that extra consistency-checking code gets included) or off.
And so on.</para>
<para>All of this stuff is called the
<emphasis>configuration</emphasis> of your build. You set the
configuration using a three-step process.</para>
<variablelist>
<varlistentry>
<term>Step 1: get ready for configuration.</term>
<listitem>
<para>NOTE: if you're starting from a source distribution,
rather than CVS sources, you can skip this step.</para>
<para>Change directory to
<constant>$(FPTOOLS_TOP)</constant> and
issue the command</para>
<programlisting>autoreconf</programlisting>
<indexterm><primary>autoreconf</primary></indexterm>
<para>(with no arguments). This GNU program (recursively) converts
<filename>$(FPTOOLS_TOP)/configure.ac</filename> and
<filename>$(FPTOOLS_TOP)/aclocal.m4</filename>
to a shell script called
<filename>$(FPTOOLS_TOP)/configure</filename>.
If <command>autoreconf</command> bleats that it can't write the file <filename>configure</filename>,
then delete the latter and try again. Note that you must use <command>autoreconf</command>,
and not the old <command>autoconf</command>! If you erroneously use the latter, you'll get
a message like "No rule to make target 'mk/config.h.in'".
</para>
<para>Some projects, including GHC, have their own configure script.
<command>autoreconf</command> takes care of that, too, so all you have
to do is calling <command>autoreconf</command> in the top-level directory
<filename>$(FPTOOLS_TOP)</filename>.</para>
<para>These steps are completely platform-independent; they just mean
that the human-written files (<filename>configure.ac</filename> and
<filename>aclocal.m4</filename>) can be short, although the resulting
files (the <command>configure</command> shell scripts and the C header
template <filename>mk/config.h.in</filename>) are long.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Step 2: system configuration.</term>
<listitem>
<para>Runs the newly-created <command>configure</command>
script, thus:</para>
<programlisting>./configure <optional><parameter>args</parameter></optional></programlisting>
<para><command>configure</command>'s mission is to scurry
round your computer working out what architecture it has,
what operating system, whether it has the
<function>vfork</function> system call, where
<command>tar</command> is kept, whether
<command>gcc</command> is available, where various obscure
<literal>#include</literal> files are, whether it's a
leap year, and what the systems manager had for lunch. It
communicates these snippets of information in two
ways:</para>
<itemizedlist>
<listitem>
<para>It translates
<filename>mk/config.mk.in</filename><indexterm><primary>config.mk.in</primary></indexterm>
to
<filename>mk/config.mk</filename><indexterm><primary>config.mk</primary></indexterm>,
substituting for things between
“<literal>@</literal>” brackets. So,
“<literal>@HaveGcc@</literal>” will be
replaced by “<literal>YES</literal>” or
“<literal>NO</literal>” depending on what
<command>configure</command> finds.
<filename>mk/config.mk</filename> is included by every
Makefile (directly or indirectly), so the
configuration information is thereby communicated to
all Makefiles.</para>
</listitem>
<listitem>
<para> It translates
<filename>mk/config.h.in</filename><indexterm><primary>config.h.in</primary></indexterm>
to
<filename>mk/config.h</filename><indexterm><primary>config.h</primary></indexterm>.
The latter is <literal>#include</literal>d by
various C programs, which can thereby make use of
configuration information.</para>
</listitem>
</itemizedlist>
<para><command>configure</command> takes some optional
arguments. Use <literal>./configure --help</literal> to
get a list of the available arguments. Here are some of
the ones you might need:</para>
<variablelist>
<varlistentry>
<term><literal>--with-ghc=<parameter>path</parameter></literal>
<indexterm><primary><literal>--with-ghc</literal></primary></indexterm>
</term>
<listitem>
<para>Specifies the path to an installed GHC which
you would like to use. This compiler will be used
for compiling GHC-specific code (eg. GHC itself).
This option <emphasis>cannot</emphasis> be specified
using <filename>build.mk</filename> (see later),
because <command>configure</command> needs to
auto-detect the version of GHC you're using. The
default is to look for a compiler named
<literal>ghc</literal> in your path.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>--with-hc=<parameter>path</parameter></literal>
<indexterm><primary><literal>--with-hc</literal></primary></indexterm>
</term>
<listitem>
<para>Specifies the path to any installed Haskell
compiler. This compiler will be used for compiling
generic Haskell code. The default is to use
<literal>ghc</literal>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>--with-gcc=<parameter>path</parameter></literal>
<indexterm><primary><literal>--with-gcc</literal></primary></indexterm>
</term>
<listitem>
<para>Specifies the path to the installed GCC. This
compiler will be used to compile all C files,
<emphasis>except</emphasis> any generated by the
installed Haskell compiler, which will have its own
idea of which C compiler (if any) to use. The
default is to use <literal>gcc</literal>.</para>
</listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
<varlistentry>
<term>Step 3: build configuration.</term>
<listitem>
<para>Next, you say how this build of
<literal>fptools</literal> is to differ from the standard
defaults by creating a new file
<filename>mk/build.mk</filename><indexterm><primary>build.mk</primary></indexterm>
<emphasis>in the build tree</emphasis>. This file is the
one and only file you edit in the build tree, precisely
because it says how this build differs from the source.
(Just in case your build tree does die, you might want to
keep a private directory of <filename>build.mk</filename>
files, and use a symbolic link in each build tree to point
to the appropriate one.) So
<filename>mk/build.mk</filename> never exists in the
source tree—you create one in each build tree from
the template. We'll discuss what to put in it
shortly.</para>
</listitem>
</varlistentry>
</variablelist>
<para>And that's it for configuration. Simple, eh?</para>
<para>What do you put in your build-specific configuration file
<filename>mk/build.mk</filename>? <emphasis>For almost all
purposes all you will do is put make variable definitions that
override those in</emphasis>
<filename>mk/config.mk.in</filename>. The whole point of
<filename>mk/config.mk.in</filename>—and its derived
counterpart <filename>mk/config.mk</filename>—is to define
the build configuration. It is heavily commented, as you will
see if you look at it. So generally, what you do is look at
<filename>mk/config.mk.in</filename>, and add definitions in
<filename>mk/build.mk</filename> that override any of the
<filename>config.mk</filename> definitions that you want to
change. (The override occurs because the main boilerplate file,
<filename>mk/boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>,
includes <filename>build.mk</filename> after
<filename>config.mk</filename>.)</para>
<para>For your convenience, there's a file called <filename>build.mk.sample</filename>
that can serve as a starting point for your <filename>build.mk</filename>.</para>
<para>For example, <filename>config.mk.in</filename> contains
the definition:</para>
<programlisting>GhcHcOpts=-O -Rghc-timing</programlisting>
<para>The accompanying comment explains that this is the list of
flags passed to GHC when building GHC itself. For doing
development, it is wise to add <literal>-DDEBUG</literal>, to
enable debugging code. So you would add the following to
<filename>build.mk</filename>:</para>
<para>or, if you prefer,</para>
<programlisting>GhcHcOpts += -DDEBUG</programlisting>
<para>GNU <command>make</command> allows existing definitions to
have new text appended using the “<literal>+=</literal>”
operator, which is quite a convenient feature.)</para>
<para>If you want to remove the <literal>-O</literal> as well (a
good idea when developing, because the turn-around cycle gets a
lot quicker), you can just override
<literal>GhcLibHcOpts</literal> altogether:</para>
<programlisting>GhcHcOpts=-DDEBUG -Rghc-timing</programlisting>
<para>When reading <filename>config.mk.in</filename>, remember
that anything between “@...@” signs is going to be substituted
by <command>configure</command> later. You
<emphasis>can</emphasis> override the resulting definition if
you want, but you need to be a bit surer what you are doing.
For example, there's a line that says:</para>
<programlisting>TAR = @TarCmd@</programlisting>
<para>This defines the Make variables <constant>TAR</constant>
to the pathname for a <command>tar</command> that
<command>configure</command> finds somewhere. If you have your
own pet <command>tar</command> you want to use instead, that's
fine. Just add this line to <filename>mk/build.mk</filename>:</para>
<programlisting>TAR = mytar</programlisting>
<para>You do not <emphasis>have</emphasis> to have a
<filename>mk/build.mk</filename> file at all; if you don't,
you'll get all the default settings from
<filename>mk/config.mk.in</filename>.</para>
<para>You can also use <filename>build.mk</filename> to override
anything that <command>configure</command> got wrong. One place
where this happens often is with the definition of
<constant>FPTOOLS_TOP_ABS</constant>: this
variable is supposed to be the canonical path to the top of your
source tree, but if your system uses an automounter then the
correct directory is hard to find automatically. If you find
that <command>configure</command> has got it wrong, just put the
correct definition in <filename>build.mk</filename>.</para>
</sect2>
<sect2 id="sec-storysofar">
<title>The story so far</title>
<para>Let's summarise the steps you need to carry to get
yourself a fully-configured build tree from scratch.</para>
<orderedlist>
<listitem>
<para> Get your source tree from somewhere (CVS repository
or source distribution). Say you call the root directory
<filename>myfptools</filename> (it does not have to be
called <filename>fptools</filename>). Make sure that you
have the essential files (see <xref
linkend="sec-source-tree">).</para>
</listitem>
<listitem>
<para>(Optional) Use <command>lndir</command> or
<command>mkshadowdir</command> to create a build tree.</para>
<programlisting>$ cd myfptools
$ mkshadowdir . /scratch/joe-bloggs/myfptools-sun4</programlisting>
<para>(N.B. <command>mkshadowdir</command>'s first argument
is taken relative to its second.) You probably want to give
the build tree a name that suggests its main defining
characteristic (in your mind at least), in case you later
add others.</para>
</listitem>
<listitem>
<para>Change directory to the build tree. Everything is
going to happen there now.</para>
<programlisting>$ cd /scratch/joe-bloggs/myfptools-sun4</programlisting>
</listitem>
<listitem>
<para>Prepare for system configuration:</para>
<programlisting>$ autoreconf</programlisting>
<para>(You can skip this step if you are starting from a
source distribution, and you already have
<filename>configure</filename> and
<filename>mk/config.h.in</filename>.)</para>
</listitem>
<listitem>
<para>Do system configuration:</para>
<programlisting>$ ./configure</programlisting>
<para>Don't forget to check whether you need to add any
arguments to <literal>configure</literal>; for example, a
common requirement is to specify which GHC to use with
<option>--with-ghc=<replaceable>ghc</replaceable></option>.</para>
</listitem>
<listitem>
<para>Create the file <filename>mk/build.mk</filename>,
adding definitions for your desired configuration
options.</para>
<programlisting>$ emacs mk/build.mk</programlisting>
</listitem>
</orderedlist>
<para>You can make subsequent changes to
<filename>mk/build.mk</filename> as often as you like. You do
not have to run any further configuration programs to make these
changes take effect. In theory you should, however, say
<command>gmake clean</command>, <command>gmake all</command>,
because configuration option changes could affect
anything—but in practice you are likely to know what's
affected.</para>
</sect2>
<sect2>
<title>Making things</title>
<para>At this point you have made yourself a fully-configured
build tree, so you are ready to start building real
things.</para>
<para>The first thing you need to know is that <emphasis>you
must use GNU <command>make</command>, usually called
<command>gmake</command>, not standard Unix
<command>make</command></emphasis>. If you use standard Unix
<command>make</command> you will get all sorts of error messages
(but no damage) because the <literal>fptools</literal>
<command>Makefiles</command> use GNU <command>make</command>'s
facilities extensively.</para>
<para>To just build the whole thing, <command>cd</command> to
the top of your <literal>fptools</literal> tree and type
<command>gmake</command>. This will prepare the tree and build
the various projects in the correct order.</para>
</sect2>
<sect2 id="sec-bootstrapping">
<title>Bootstrapping GHC</title>
<para>GHC requires a 2-stage bootstrap in order to provide
full functionality, including GHCi. By a 2-stage bootstrap, we
mean that the compiler is built once using the installed GHC,
and then again using the compiler built in the first stage. You
can also build a stage 3 compiler, but this normally isn't
necessary except to verify that the stage 2 compiler is working
properly.</para>
<para>Note that when doing a bootstrap, the stage 1 compiler
must be built, followed by the runtime system and libraries, and
then the stage 2 compiler. The correct ordering is implemented
by the top-level fptools <filename>Makefile</filename>, so if
you want everything to work automatically it's best to start
<command>make</command> from the top of the tree. When building
GHC, the top-level fptools <filename>Makefile</filename> is set
up to do a 2-stage bootstrap by default (when you say
<command>make</command>). Some other targets it supports
are:</para>
<variablelist>
<varlistentry>
<term>stage1</term>
<listitem>
<para>Build everything as normal, including the stage 1
compiler.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>stage2</term>
<listitem>
<para>Build the stage 2 compiler only.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>stage3</term>
<listitem>
<para>Build the stage 3 compiler only.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>bootstrap</term> <term>bootstrap2</term>
<listitem>
<para>Build stage 1 followed by stage 2.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>bootstrap3</term>
<listitem>
<para>Build stages 1, 2 and 3.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>install</term>
<listitem>
<para>Install everything, including the compiler built in
stage 2. To override the stage, say <literal>make install
stage=<replaceable>n</replaceable></literal> where
<replaceable>n</replaceable> is the stage to install.</para>
</listitem>
</varlistentry>
</variablelist>
<para>The top-level <filename>Makefile</filename> also arranges
to do the appropriate <literal>make boot</literal> steps (see
below) before actually building anything.</para>
<para>The <literal>stage1</literal>, <literal>stage2</literal>
and <literal>stage3</literal> targets also work in the
<literal>ghc/compiler</literal> directory, but don't forget that
each stage requires its own <literal>make boot</literal> step:
for example, you must do</para>
<screen>$ make boot stage=2</screen>
<para>before <literal>make stage2</literal> in
<literal>ghc/compiler</literal>.</para>
</sect2>
<sect2 id="sec-standard-targets">
<title>Standard Targets</title>
<indexterm><primary>targets, standard makefile</primary></indexterm>
<indexterm><primary>makefile targets</primary></indexterm>
<para>In any directory you should be able to make the following:</para>
<variablelist>
<varlistentry>
<term><literal>boot</literal></term>
<listitem>
<para>does the one-off preparation required to get ready
for the real work. Notably, it does <command>gmake
depend</command> in all directories that contain programs.
It also builds the necessary tools for compilation to
proceed.</para>
<para>Invoking the <literal>boot</literal> target
explicitly is not normally necessary. From the top-level
<literal>fptools</literal> directory, invoking
<literal>gmake</literal> causes <literal>gmake boot
all</literal> to be invoked in each of the project
subdirectories, in the order specified by
<literal>$(AllTargets)</literal> in
<literal>config.mk</literal>.</para>
<para>If you're working in a subdirectory somewhere and
need to update the dependencies, <literal>gmake
boot</literal> is a good way to do it.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>all</literal></term>
<listitem>
<para>makes all the final target(s) for this Makefile.
Depending on which directory you are in a “final
target” may be an executable program, a library
archive, a shell script, or a Postscript file. Typing
<command>gmake</command> alone is generally the same as
typing <command>gmake all</command>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>install</literal></term>
<listitem>
<para>installs the things built by <literal>all</literal>
(except for the documentation). Where does it install
them? That is specified by
<filename>mk/config.mk.in</filename>; you can override it
in <filename>mk/build.mk</filename>, or by running
<command>configure</command> with command-line arguments
like <literal>--bindir=/home/simonpj/bin</literal>; see
<literal>./configure --help</literal> for the full
details.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>install-docs</literal></term>
<listitem>
<para>installs the documentation. Otherwise behaves just
like <literal>install</literal>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>uninstall</literal></term>
<listitem>
<para>reverses the effect of
<literal>install</literal>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>clean</literal></term>
<listitem>
<para>Delete all files from the current directory that are
normally created by building the program. Don't delete
the files that record the configuration, or files
generated by <command>gmake boot</command>. Also preserve
files that could be made by building, but normally aren't
because the distribution comes with them.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>distclean</literal></term>
<listitem>
<para>Delete all files from the current directory that are
created by configuring or building the program. If you
have unpacked the source and built the program without
creating any other files, <literal>make
distclean</literal> should leave only the files that were
in the distribution.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>mostlyclean</literal></term>
<listitem>
<para>Like <literal>clean</literal>, but may refrain from
deleting a few files that people normally don't want to
recompile.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>maintainer-clean</literal></term>
<listitem>
<para>Delete everything from the current directory that
can be reconstructed with this Makefile. This typically
includes everything deleted by
<literal>distclean</literal>, plus more: C source files
produced by Bison, tags tables, Info files, and so
on.</para>
<para>One exception, however: <literal>make
maintainer-clean</literal> should not delete
<filename>configure</filename> even if
<filename>configure</filename> can be remade using a rule
in the <filename>Makefile</filename>. More generally,
<literal>make maintainer-clean</literal> should not delete
anything that needs to exist in order to run
<filename>configure</filename> and then begin to build the
program.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>check</literal></term>
<listitem>
<para>run the test suite.</para>
</listitem>
</varlistentry>
</variablelist>
<para>All of these standard targets automatically recurse into
sub-directories. Certain other standard targets do not:</para>
<variablelist>
<varlistentry>
<term><literal>configure</literal></term>
<listitem>
<para>is only available in the root directory
<constant>$(FPTOOLS_TOP)</constant>; it has
been discussed in <xref
linkend="sec-build-config">.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>depend</literal></term>
<listitem>
<para>make a <filename>.depend</filename> file in each
directory that needs it. This <filename>.depend</filename>
file contains mechanically-generated dependency
information; for example, suppose a directory contains a
Haskell source module <filename>Foo.lhs</filename> which
imports another module <literal>Baz</literal>. Then the
generated <filename>.depend</filename> file will contain
the dependency:</para>
<programlisting>Foo.o : Baz.hi</programlisting>
<para>which says that the object file
<filename>Foo.o</filename> depends on the interface file
<filename>Baz.hi</filename> generated by compiling module
<literal>Baz</literal>. The <filename>.depend</filename>
file is automatically included by every Makefile.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>binary-dist</literal></term>
<listitem>
<para>make a binary distribution. This is the target we
use to build the binary distributions of GHC and
Happy.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>dist</literal></term>
<listitem>
<para>make a source distribution. Note that this target
does “make distclean” as part of its work;
don't use it if you want to keep what you've built.</para>
</listitem>
</varlistentry>
</variablelist>
<para>Most <filename>Makefile</filename>s have targets other
than these. You can discover them by looking in the
<filename>Makefile</filename> itself.</para>
</sect2>
<sect2>
<title>Using a project from the build tree</title>
<para>If you want to build GHC (say) and just use it direct from
the build tree without doing <literal>make install</literal>
first, you can run the in-place driver script:
<filename>ghc/compiler/ghc-inplace</filename>.</para>
<para> Do <emphasis>NOT</emphasis> use
<filename>ghc/compiler/ghc</filename>, or
<filename>ghc/compiler/ghc-6.xx</filename>, as these are the
scripts intended for installation, and contain hard-wired paths
to the installed libraries, rather than the libraries in the
build tree.</para>
<para>Happy can similarly be run from the build tree, using
<filename>happy/src/happy-inplace</filename>, and similarly for
Alex and Haddock.</para>
</sect2>
<sect2>
<title>Fast Making</title>
<indexterm><primary>fastmake</primary></indexterm>
<indexterm><primary>dependencies, omitting</primary></indexterm>
<indexterm><primary>FAST, makefile variable</primary></indexterm>
<para>Sometimes the dependencies get in the way: if you've made
a small change to one file, and you're absolutely sure that it
won't affect anything else, but you know that
<command>make</command> is going to rebuild everything anyway,
the following hack may be useful:</para>
<programlisting>gmake FAST=YES</programlisting>
<para>This tells the make system to ignore dependencies and just
build what you tell it to. In other words, it's equivalent to
temporarily removing the <filename>.depend</filename> file in
the current directory (where <command>mkdependHS</command> and
friends store their dependency information).</para>
<para>A bit of history: GHC used to come with a
<command>fastmake</command> script that did the above job, but
GNU make provides the features we need to do it without
resorting to a script. Also, we've found that fastmaking is
less useful since the advent of GHC's recompilation checker (see
the User's Guide section on "Separate Compilation").</para>
</sect2>
</sect1>
<sect1 id="sec-makefile-arch">
<title>The <filename>Makefile</filename> architecture</title>
<indexterm><primary>makefile architecture</primary></indexterm>
<para><command>make</command> is great if everything
works—you type <command>gmake install</command> and lo! the
right things get compiled and installed in the right places. Our
goal is to make this happen often, but somehow it often doesn't;
instead some weird error message eventually emerges from the
bowels of a directory you didn't know existed.</para>
<para>The purpose of this section is to give you a road-map to
help you figure out what is going right and what is going
wrong.</para>
<sect2>
<title>Debugging</title>
<para>Debugging <filename>Makefile</filename>s is something of a
black art, but here's a couple of tricks that we find
particularly useful. The following command allows you to see
the contents of any make variable in the context of the current
<filename>Makefile</filename>:</para>
<screen>$ make show VALUE=HS_SRCS</screen>
<para>where you can replace <literal>HS_SRCS</literal> with the
name of any variable you wish to see the value of.</para>
<para>GNU make has a <option>-d</option> option which generates
a dump of the decision procedure used to arrive at a conclusion
about which files should be recompiled. Sometimes useful for
tracking down problems with superfluous or missing
recompilations.</para>
</sect2>
<sect2>
<title>A small project</title>
<para>To get started, let us look at the
<filename>Makefile</filename> for an imaginary small
<literal>fptools</literal> project, <literal>small</literal>.
Each project in <literal>fptools</literal> has its own directory
in <constant>FPTOOLS_TOP</constant>, so the
<literal>small</literal> project will have its own directory
<constant>FPOOLS_TOP/small/</constant>. Inside the
<filename>small/</filename> directory there will be a
<filename>Makefile</filename>, looking something like
this:</para>
<indexterm><primary>Makefile, minimal</primary></indexterm>
<programlisting># Makefile for fptools project "small"
TOP = ..
include $(TOP)/mk/boilerplate.mk
SRCS = $(wildcard *.lhs) $(wildcard *.c)
HS_PROG = small
include $(TOP)/target.mk</programlisting>
<para>this <filename>Makefile</filename> has three
sections:</para>
<orderedlist>
<listitem>
<para>The first section includes
<footnote>
<para>
One of the most important
features of GNU <command>make</command> that we use is the ability for a <filename>Makefile</filename> to
include another named file, very like <command>cpp</command>'s <literal>#include</literal>
directive.
</para>
</footnote>
a file of “boilerplate” code from the level
above (which in this case will be
<filename>FPTOOLS_TOP/mk/boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>).
As its name suggests, <filename>boilerplate.mk</filename>
consists of a large quantity of standard
<filename>Makefile</filename> code. We discuss this
boilerplate in more detail in <xref linkend="sec-boiler">.
<indexterm><primary>include, directive in
Makefiles</primary></indexterm> <indexterm><primary>Makefile
inclusion</primary></indexterm></para>
<para>Before the <literal>include</literal> statement, you
must define the <command>make</command> variable
<constant>TOP</constant><indexterm><primary>TOP</primary></indexterm>
to be the directory containing the <filename>mk</filename>
directory in which the <filename>boilerplate.mk</filename>
file is. It is <emphasis>not</emphasis> OK to simply say</para>
<programlisting>include ../mk/boilerplate.mk # NO NO NO</programlisting>
<para>Why? Because the <filename>boilerplate.mk</filename>
file needs to know where it is, so that it can, in turn,
<literal>include</literal> other files. (Unfortunately,
when an <literal>include</literal>d file does an
<literal>include</literal>, the filename is treated relative
to the directory in which <command>gmake</command> is being
run, not the directory in which the
<literal>include</literal>d sits.) In general,
<emphasis>every file <filename>foo.mk</filename> assumes
that
<filename>$(TOP)/mk/foo.mk</filename>
refers to itself.</emphasis> It is up to the
<filename>Makefile</filename> doing the
<literal>include</literal> to ensure this is the case.</para>
<para>Files intended for inclusion in other
<filename>Makefile</filename>s are written to have the
following property: <emphasis>after
<filename>foo.mk</filename> is <literal>include</literal>d,
it leaves <constant>TOP</constant> containing the same value
as it had just before the <literal>include</literal>
statement</emphasis>. In our example, this invariant
guarantees that the <literal>include</literal> for
<filename>target.mk</filename> will look in the same
directory as that for <filename>boilerplate.mk</filename>.</para>
</listitem>
<listitem>
<para> The second section defines the following standard
<command>make</command> variables:
<constant>SRCS</constant><indexterm><primary>SRCS</primary></indexterm>
(the source files from which is to be built), and
<constant>HS_PROG</constant><indexterm><primary>HS_PROG</primary></indexterm>
(the executable binary to be built). We will discuss in
more detail what the “standard variables” are,
and how they affect what happens, in <xref
linkend="sec-targets">.</para>
<para>The definition for <constant>SRCS</constant> uses the
useful GNU <command>make</command> construct
<literal>$(wildcard $pat$)</literal><indexterm><primary>wildcard</primary></indexterm>,
which expands to a list of all the files matching the
pattern <literal>pat</literal> in the current directory. In
this example, <constant>SRCS</constant> is set to the list
of all the <filename>.lhs</filename> and
<filename>.c</filename> files in the directory. (Let's
suppose there is one of each, <filename>Foo.lhs</filename>
and <filename>Baz.c</filename>.)</para>
</listitem>
<listitem>
<para>The last section includes a second file of standard
code, called
<filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>.
It contains the rules that tell <command>gmake</command> how
to make the standard targets (<xref
linkend="sec-standard-targets">). Why, you ask, can't this
standard code be part of
<filename>boilerplate.mk</filename>? Good question. We
discuss the reason later, in <xref
linkend="sec-boiler-arch">.</para>
<para>You do not <emphasis>have</emphasis> to
<literal>include</literal> the
<filename>target.mk</filename> file. Instead, you can write
rules of your own for all the standard targets. Usually,
though, you will find quite a big payoff from using the
canned rules in <filename>target.mk</filename>; the price
tag is that you have to understand what canned rules get
enabled, and what they do (<xref
linkend="sec-targets">).</para>
</listitem>
</orderedlist>
<para>In our example <filename>Makefile</filename>, most of the
work is done by the two <literal>include</literal>d files. When
you say <command>gmake all</command>, the following things
happen:</para>
<itemizedlist>
<listitem>
<para><command>gmake</command> figures out that the object
files are <filename>Foo.o</filename> and
<filename>Baz.o</filename>.</para>
</listitem>
<listitem>
<para>It uses a boilerplate pattern rule to compile
<filename>Foo.lhs</filename> to <filename>Foo.o</filename>
using a Haskell compiler. (Which one? That is set in the
build configuration.)</para>
</listitem>
<listitem>
<para>It uses another standard pattern rule to compile
<filename>Baz.c</filename> to <filename>Baz.o</filename>,
using a C compiler. (Ditto.)</para>
</listitem>
<listitem>
<para>It links the resulting <filename>.o</filename> files
together to make <literal>small</literal>, using the Haskell
compiler to do the link step. (Why not use
<command>ld</command>? Because the Haskell compiler knows
what standard libraries to link in. How did
<command>gmake</command> know to use the Haskell compiler to
do the link, rather than the C compiler? Because we set the
variable <constant>HS_PROG</constant> rather than
<constant>C_PROG</constant>.)</para>
</listitem>
</itemizedlist>
<para>All <filename>Makefile</filename>s should follow the above
three-section format.</para>
</sect2>
<sect2>
<title>A larger project</title>
<para>Larger projects are usually structured into a number of
sub-directories, each of which has its own
<filename>Makefile</filename>. (In very large projects, this
sub-structure might be iterated recursively, though that is
rare.) To give you the idea, here's part of the directory
structure for the (rather large) GHC project:</para>
<screen>$(FPTOOLS_TOP)/ghc/
Makefile
mk/
boilerplate.mk
rules.mk
docs/
Makefile
...source files for documentation...
driver/
Makefile
...source files for driver...
compiler/
Makefile
parser/...source files for parser...
renamer/...source files for renamer...
...etc...</screen>
<para>The sub-directories <filename>docs</filename>,
<filename>driver</filename>, <filename>compiler</filename>, and
so on, each contains a sub-component of GHC, and each has its
own <filename>Makefile</filename>. There must also be a
<filename>Makefile</filename> in
<filename>$(FPTOOLS_TOP)/ghc</filename>.
It does most of its work by recursively invoking
<command>gmake</command> on the <filename>Makefile</filename>s
in the sub-directories. We say that
<filename>ghc/Makefile</filename> is a <emphasis>non-leaf
<filename>Makefile</filename></emphasis>, because it does little
except organise its children, while the
<filename>Makefile</filename>s in the sub-directories are all
<emphasis>leaf <filename>Makefile</filename>s</emphasis>. (In
principle the sub-directories might themselves contain a
non-leaf <filename>Makefile</filename> and several
sub-sub-directories, but that does not happen in GHC.)</para>
<para>The <filename>Makefile</filename> in
<filename>ghc/compiler</filename> is considered a leaf
<filename>Makefile</filename> even though the
<filename>ghc/compiler</filename> has sub-directories, because
these sub-directories do not themselves have
<filename>Makefile</filename>s in them. They are just used to
structure the collection of modules that make up GHC, but all
are managed by the single <filename>Makefile</filename> in
<filename>ghc/compiler</filename>.</para>
<para>You will notice that <filename>ghc/</filename> also
contains a directory <filename>ghc/mk/</filename>. It contains
GHC-specific <filename>Makefile</filename> boilerplate code.
More precisely:</para>
<itemizedlist>
<listitem>
<para><filename>ghc/mk/boilerplate.mk</filename> is included
at the top of <filename>ghc/Makefile</filename>, and of all
the leaf <filename>Makefile</filename>s in the
sub-directories. It in turn <literal>include</literal>s the
main boilerplate file
<filename>mk/boilerplate.mk</filename>.</para>
</listitem>
<listitem>
<para><filename>ghc/mk/target.mk</filename> is
<literal>include</literal>d at the bottom of
<filename>ghc/Makefile</filename>, and of all the leaf
<filename>Makefile</filename>s in the sub-directories. It
in turn <literal>include</literal>s the file
<filename>mk/target.mk</filename>.</para>
</listitem>
</itemizedlist>
<para>So these two files are the place to look for GHC-wide
customisation of the standard boilerplate.</para>
</sect2>
<sect2 id="sec-boiler-arch">
<title>Boilerplate architecture</title>
<indexterm><primary>boilerplate architecture</primary></indexterm>
<para>Every <filename>Makefile</filename> includes a
<filename>boilerplate.mk</filename><indexterm><primary>boilerplate.mk</primary></indexterm>
file at the top, and
<filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>
file at the bottom. In this section we discuss what is in these
files, and why there have to be two of them. In general:</para>
<itemizedlist>
<listitem>
<para><filename>boilerplate.mk</filename> consists of:</para>
<itemizedlist>
<listitem>
<para><emphasis>Definitions of millions of
<command>make</command> variables</emphasis> that
collectively specify the build configuration. Examples:
<constant>HC_OPTS</constant><indexterm><primary>HC_OPTS</primary></indexterm>,
the options to feed to the Haskell compiler;
<constant>NoFibSubDirs</constant><indexterm><primary>NoFibSubDirs</primary></indexterm>,
the sub-directories to enable within the
<literal>nofib</literal> project;
<constant>GhcWithHc</constant><indexterm><primary>GhcWithHc</primary></indexterm>,
the name of the Haskell compiler to use when compiling
GHC in the <literal>ghc</literal> project.</para>
</listitem>
<listitem>
<para><emphasis>Standard pattern rules</emphasis> that
tell <command>gmake</command> how to construct one file
from another.</para>
</listitem>
</itemizedlist>
<para><filename>boilerplate.mk</filename> needs to be
<literal>include</literal>d at the <emphasis>top</emphasis>
of each <filename>Makefile</filename>, so that the user can
replace the boilerplate definitions or pattern rules by
simply giving a new definition or pattern rule in the
<filename>Makefile</filename>. <command>gmake</command>
simply takes the last definition as the definitive one.</para>
<para>Instead of <emphasis>replacing</emphasis> boilerplate
definitions, it is also quite common to
<emphasis>augment</emphasis> them. For example, a
<filename>Makefile</filename> might say:</para>
<programlisting>SRC_HC_OPTS += -O</programlisting>
<para>thereby adding “<option>-O</option>” to
the end of
<constant>SRC_HC_OPTS</constant><indexterm><primary>SRC_HC_OPTS</primary></indexterm>.</para>
</listitem>
<listitem>
<para><filename>target.mk</filename> contains
<command>make</command> rules for the standard targets
described in <xref linkend="sec-standard-targets">. These
rules are selectively included, depending on the setting of
certain <command>make</command> variables. These variables
are usually set in the middle section of the
<filename>Makefile</filename> between the two
<literal>include</literal>s.</para>
<para><filename>target.mk</filename> must be included at the
end (rather than being part of
<filename>boilerplate.mk</filename>) for several tiresome
reasons:</para>
<itemizedlist>
<listitem>
<para><command>gmake</command> commits target and
dependency lists earlier than it should. For example,
<filename>target.mk</filename> has a rule that looks
like this:</para>
<programlisting>$(HS_PROG) : $(OBJS)
$(HC) $(LD_OPTS) $< -o $@</programlisting>
<para>If this rule was in
<filename>boilerplate.mk</filename> then
<constant>$(HS_PROG)</constant><indexterm><primary>HS_PROG</primary></indexterm>
and
<constant>$(OBJS)</constant><indexterm><primary>OBJS</primary></indexterm>
would not have their final values at the moment
<command>gmake</command> encountered the rule. Alas,
<command>gmake</command> takes a snapshot of their
current values, and wires that snapshot into the rule.
(In contrast, the commands executed when the rule
“fires” are only substituted at the moment
of firing.) So, the rule must follow the definitions
given in the <filename>Makefile</filename> itself.</para>
</listitem>
<listitem>
<para>Unlike pattern rules, ordinary rules cannot be
overriden or replaced by subsequent rules for the same
target (at least, not without an error message).
Including ordinary rules in
<filename>boilerplate.mk</filename> would prevent the
user from writing rules for specific targets in specific
cases.</para>
</listitem>
<listitem>
<para>There are a couple of other reasons I've
forgotten, but it doesn't matter too much.</para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="sec-boiler">
<title>The main <filename>mk/boilerplate.mk</filename> file</title>
<indexterm><primary>boilerplate.mk</primary></indexterm>
<para>If you look at
<filename>$(FPTOOLS_TOP)/mk/boilerplate.mk</filename>
you will find that it consists of the following sections, each
held in a separate file:</para>
<variablelist>
<varlistentry>
<term><filename>config.mk</filename>
<indexterm><primary>config.mk</primary></indexterm>
</term>
<listitem>
<para>is the build configuration file we discussed at
length in <xref linkend="sec-build-config">.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><filename>paths.mk</filename>
<indexterm><primary>paths.mk</primary></indexterm>
</term>
<listitem>
<para>defines <command>make</command> variables for
pathnames and file lists. This file contains code for
automatically compiling lists of source files and deriving
lists of object files from those. The results can be
overriden in the <filename>Makefile</filename>, but in
most cases the automatic setup should do the right
thing.</para>
<para>The following variables may be set in the
<filename>Makefile</filename> to affect how the automatic
source file search is done:</para>
<variablelist>
<varlistentry>
<term><literal>ALL_DIRS</literal>
<indexterm><primary><literal>ALL_DIRS</literal></primary></indexterm>
</term>
<listitem>
<para>Set to a list of directories to search in
addition to the current directory for source
files.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>EXCLUDE_SRCS</literal>
<indexterm><primary><literal>EXCLUDE_SRCS</literal></primary></indexterm>
</term>
<listitem>
<para>Set to a list of source files (relative to the
current directory) to omit from the automatic
search. The source searching machinery is clever
enough to know that if you exclude a source file
from which other sources are derived, then the
derived sources should also be excluded. For
example, if you set <literal>EXCLUDED_SRCS</literal>
to include <filename>Foo.y</filename>, then
<filename>Foo.hs</filename> will also be
excluded.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>EXTRA_SRCS</literal>
<indexterm><primary><literal>EXCLUDE_SRCS</literal></primary></indexterm>
</term>
<listitem>
<para>Set to a list of extra source files (perhaps
in directories not listed in
<literal>ALL_DIRS</literal>) that should be
considered.</para>
</listitem>
</varlistentry>
</variablelist>
<para>The results of the automatic source file search are
placed in the following make variables:</para>
<variablelist>
<varlistentry>
<term><literal>SRCS</literal>
<indexterm><primary><literal>SRCS</literal></primary></indexterm>
</term>
<listitem>
<para>All source files found, sorted and without
duplicates, including those which might not exist
yet but will be derived from other existing sources.
<literal>SRCS</literal> <emphasis>can</emphasis> be
overriden if necessary, in which case the variables
below will follow suit.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>HS_SRCS</literal>
<indexterm><primary><literal>HS_SRCS</literal></primary></indexterm>
</term>
<listitem>
<para>all Haskell source files in the current
directory, including those derived from other source
files (eg. Happy sources also give rise to Haskell
sources).</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>HS_OBJS</literal>
<indexterm><primary><literal>HS_OBJS</literal></primary></indexterm>
</term>
<listitem>
<para>Object files derived from
<literal>HS_SRCS</literal>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>HS_IFACES</literal>
<indexterm><primary><literal>HS_IFACES</literal></primary></indexterm>
</term>
<listitem>
<para>Interface files (<literal>.hi</literal> files)
derived from <literal>HS_SRCS</literal>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>C_SRCS</literal>
<indexterm><primary><literal>C_SRCS</literal></primary></indexterm>
</term>
<listitem>
<para>All C source files found.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>C_OBJS</literal>
<indexterm><primary><literal>C_OBJS</literal></primary></indexterm>
</term>
<listitem>
<para>Object files derived from
<literal>C_SRCS</literal>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SCRIPT_SRCS</literal>
<indexterm><primary><literal>SCRIPT_SRCS</literal></primary></indexterm>
</term>
<listitem>
<para>All script source files found
(<literal>.lprl</literal> files).</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SCRIPT_OBJS</literal>
<indexterm><primary><literal>SCRIPT_OBJS</literal></primary></indexterm>
</term>
<listitem>
<para><quote>object</quote> files derived from
<literal>SCRIPT_SRCS</literal>
(<literal>.prl</literal> files).</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>HSC_SRCS</literal>
<indexterm><primary><literal>HSC_SRCS</literal></primary></indexterm>
</term>
<listitem>
<para>All <literal>hsc2hs</literal> source files
(<literal>.hsc</literal> files).</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>HAPPY_SRCS</literal>
<indexterm><primary><literal>HAPPY_SRCS</literal></primary></indexterm>
</term>
<listitem>
<para>All <literal>happy</literal> source files
(<literal>.y</literal> or <literal>.hy</literal> files).</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>OBJS</literal>
<indexterm><primary>OBJS</primary></indexterm>
</term>
<listitem>
<para>the concatenation of
<literal>$(HS_OBJS)</literal>,
<literal>$(C_OBJS)</literal>, and
<literal>$(SCRIPT_OBJS)</literal>.</para>
</listitem>
</varlistentry>
</variablelist>
<para>Any or all of these definitions can easily be
overriden by giving new definitions in your
<filename>Makefile</filename>.</para>
<para>What, exactly, does <filename>paths.mk</filename>
consider a <quote>source file</quote> to be? It's based
on the file's suffix (e.g. <filename>.hs</filename>,
<filename>.lhs</filename>, <filename>.c</filename>,
<filename>.hy</filename>, etc), but this is the kind of
detail that changes, so rather than enumerate the source
suffices here the best thing to do is to look in
<filename>paths.mk</filename>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><filename>opts.mk</filename>
<indexterm><primary>opts.mk</primary></indexterm>
</term>
<listitem>
<para>defines <command>make</command> variables for option
strings to pass to each program. For example, it defines
<constant>HC_OPTS</constant><indexterm><primary>HC_OPTS</primary></indexterm>,
the option strings to pass to the Haskell compiler. See
<xref linkend="sec-suffix">.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><filename>suffix.mk</filename>
<indexterm><primary>suffix.mk</primary></indexterm>
</term>
<listitem>
<para>defines standard pattern rules—see <xref
linkend="sec-suffix">.</para>
</listitem>
</varlistentry>
</variablelist>
<para>Any of the variables and pattern rules defined by the
boilerplate file can easily be overridden in any particular
<filename>Makefile</filename>, because the boilerplate
<literal>include</literal> comes first. Definitions after this
<literal>include</literal> directive simply override the default
ones in <filename>boilerplate.mk</filename>.</para>
</sect2>
<sect2 id="sec-suffix">
<title>Pattern rules and options</title>
<indexterm><primary>Pattern rules</primary></indexterm>
<para>The file
<filename>suffix.mk</filename><indexterm><primary>suffix.mk</primary></indexterm>
defines standard <emphasis>pattern rules</emphasis> that say how
to build one kind of file from another, for example, how to
build a <filename>.o</filename> file from a
<filename>.c</filename> file. (GNU <command>make</command>'s
<emphasis>pattern rules</emphasis> are more powerful and easier
to use than Unix <command>make</command>'s <emphasis>suffix
rules</emphasis>.)</para>
<para>Almost all the rules look something like this:</para>
<programlisting>%.o : %.c
$(RM) $@
$(CC) $(CC_OPTS) -c $< -o $@</programlisting>
<para>Here's how to understand the rule. It says that
<emphasis>something</emphasis><filename>.o</filename> (say
<filename>Foo.o</filename>) can be built from
<emphasis>something</emphasis><filename>.c</filename>
(<filename>Foo.c</filename>), by invoking the C compiler (path
name held in <constant>$(CC)</constant>), passing to it
the options <constant>$(CC_OPTS)</constant> and
the rule's dependent file of the rule
<literal>$<</literal> (<filename>Foo.c</filename> in
this case), and putting the result in the rule's target
<literal>$@</literal> (<filename>Foo.o</filename> in this
case).</para>
<para>Every program is held in a <command>make</command>
variable defined in <filename>mk/config.mk</filename>—look
in <filename>mk/config.mk</filename> for the complete list. One
important one is the Haskell compiler, which is called
<constant>$(HC)</constant>.</para>
<para>Every program's options are are held in a
<command>make</command> variables called
<constant><prog>_OPTS</constant>. the
<constant><prog>_OPTS</constant> variables are
defined in <filename>mk/opts.mk</filename>. Almost all of them
are defined like this:</para>
<programlisting>CC_OPTS = \
$(SRC_CC_OPTS) $(WAY$(_way)_CC_OPTS) $($*_CC_OPTS) $(EXTRA_CC_OPTS)</programlisting>
<para>The four variables from which
<constant>CC_OPTS</constant> is built have the following
meaning:</para>
<variablelist>
<varlistentry>
<term><constant>SRC_CC_OPTS</constant><indexterm><primary>SRC_CC_OPTS</primary></indexterm>:</term>
<listitem>
<para>options passed to all C compilations.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>WAY_<way>_CC_OPTS</constant>:</term>
<listitem>
<para>options passed to C compilations for way
<literal><way></literal>. For example,
<constant>WAY_mp_CC_OPTS</constant>
gives options to pass to the C compiler when compiling way
<literal>mp</literal>. The variable
<constant>WAY_CC_OPTS</constant> holds
options to pass to the C compiler when compiling the
standard way. (<xref linkend="sec-ways"> dicusses
multi-way compilation.)</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant><module>_CC_OPTS</constant>:</term>
<listitem>
<para>options to pass to the C compiler that are specific
to module <literal><module></literal>. For example,
<constant>SMap_CC_OPTS</constant> gives the
specific options to pass to the C compiler when compiling
<filename>SMap.c</filename>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>EXTRA_CC_OPTS</constant><indexterm><primary>EXTRA_CC_OPTS</primary></indexterm>:</term>
<listitem>
<para>extra options to pass to all C compilations. This
is intended for command line use, thus:</para>
<programlisting>gmake libHS.a EXTRA_CC_OPTS="-v"</programlisting>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2 id="sec-targets">
<title>The main <filename>mk/target.mk</filename> file</title>
<indexterm><primary>target.mk</primary></indexterm>
<para><filename>target.mk</filename> contains canned rules for
all the standard targets described in <xref
linkend="sec-standard-targets">. It is complicated by the fact
that you don't want all of these rules to be active in every
<filename>Makefile</filename>. Rather than have a plethora of
tiny files which you can include selectively, there is a single
file, <filename>target.mk</filename>, which selectively includes
rules based on whether you have defined certain variables in
your <filename>Makefile</filename>. This section explains what
rules you get, what variables control them, and what the rules
do. Hopefully, you will also get enough of an idea of what is
supposed to happen that you can read and understand any weird
special cases yourself.</para>
<variablelist>
<varlistentry>
<term><constant>HS_PROG</constant><indexterm><primary>HS_PROG</primary></indexterm>.</term>
<listitem>
<para>If <constant>HS_PROG</constant> is defined,
you get rules with the following targets:</para>
<variablelist>
<varlistentry>
<term><filename>HS_PROG</filename><indexterm><primary>HS_PROG</primary></indexterm></term>
<listitem>
<para>itself. This rule links
<constant>$(OBJS)</constant> with the Haskell
runtime system to get an executable called
<constant>$(HS_PROG)</constant>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>install</literal><indexterm><primary>install</primary></indexterm></term>
<listitem>
<para>installs
<constant>$(HS_PROG)</constant> in
<constant>$(bindir)</constant>.</para>
</listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>C_PROG</constant><indexterm><primary>C_PROG</primary></indexterm></term>
<listitem>
<para>is similar to <constant>HS_PROG</constant>,
except that the link step links
<constant>$(C_OBJS)</constant> with the C
runtime system.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>LIBRARY</constant><indexterm><primary>LIBRARY</primary></indexterm></term>
<listitem>
<para>is similar to <constant>HS_PROG</constant>,
except that it links
<constant>$(LIB_OBJS)</constant> to make the
library archive <constant>$(LIBRARY)</constant>,
and <literal>install</literal> installs it in
<constant>$(libdir)</constant>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>LIB_DATA</constant><indexterm><primary>LIB_DATA</primary></indexterm></term>
<listitem>
<para>…</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>LIB_EXEC</constant><indexterm><primary>LIB_EXEC</primary></indexterm></term>
<listitem>
<para>…</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>HS_SRCS</constant><indexterm><primary>HS_SRCS</primary></indexterm>, <constant>C_SRCS</constant><indexterm><primary>C_SRCS</primary></indexterm>.</term>
<listitem>
<para>If <constant>HS_SRCS</constant> is defined
and non-empty, a rule for the target
<literal>depend</literal> is included, which generates
dependency information for Haskell programs. Similarly
for <constant>C_SRCS</constant>.</para>
</listitem>
</varlistentry>
</variablelist>
<para>All of these rules are “double-colon” rules,
thus</para>
<programlisting>install :: $(HS_PROG)
...how to install it...</programlisting>
<para>GNU <command>make</command> treats double-colon rules as
separate entities. If there are several double-colon rules for
the same target it takes each in turn and fires it if its
dependencies say to do so. This means that you can, for
example, define both <constant>HS_PROG</constant> and
<constant>LIBRARY</constant>, which will generate two rules for
<literal>install</literal>. When you type <command>gmake
install</command> both rules will be fired, and both the program
and the library will be installed, just as you wanted.</para>
</sect2>
<sect2 id="sec-subdirs">
<title>Recursion</title>
<indexterm><primary>recursion, in makefiles</primary></indexterm>
<indexterm><primary>Makefile, recursing into subdirectories</primary></indexterm>
<para>In leaf <filename>Makefile</filename>s the variable
<constant>SUBDIRS</constant><indexterm><primary>SUBDIRS</primary></indexterm>
is undefined. In non-leaf <filename>Makefile</filename>s,
<constant>SUBDIRS</constant> is set to the list of
sub-directories that contain subordinate
<filename>Makefile</filename>s. <emphasis>It is up to you to
set <constant>SUBDIRS</constant> in the
<filename>Makefile</filename>.</emphasis> There is no automation
here—<constant>SUBDIRS</constant> is too important to
automate.</para>
<para>When <constant>SUBDIRS</constant> is defined,
<filename>target.mk</filename> includes a rather neat rule for
the standard targets (<xref linkend="sec-standard-targets"> that
simply invokes <command>make</command> recursively in each of
the sub-directories.</para>
<para><emphasis>These recursive invocations are guaranteed to
occur in the order in which the list of directories is specified
in <constant>SUBDIRS</constant>. </emphasis>This guarantee can
be important. For example, when you say <command>gmake
boot</command> it can be important that the recursive invocation
of <command>make boot</command> is done in one sub-directory
(the include files, say) before another (the source files).
Generally, put the most independent sub-directory first, and the
most dependent last.</para>
</sect2>
<sect2 id="sec-ways">
<title>Way management</title>
<indexterm><primary>way management</primary></indexterm>
<para>We sometimes want to build essentially the same system in
several different “ways”. For example, we want to build GHC's
<literal>Prelude</literal> libraries with and without profiling,
so that there is an appropriately-built library archive to link
with when the user compiles his program. It would be possible
to have a completely separate build tree for each such “way”,
but it would be horribly bureaucratic, especially since often
only parts of the build tree need to be constructed in multiple
ways.</para>
<para>Instead, the
<filename>target.mk</filename><indexterm><primary>target.mk</primary></indexterm>
contains some clever magic to allow you to build several
versions of a system; and to control locally how many versions
are built and how they differ. This section explains the
magic.</para>
<para>The files for a particular way are distinguished by
munging the suffix. The <quote>normal way</quote> is always
built, and its files have the standard suffices
<filename>.o</filename>, <filename>.hi</filename>, and so on.
In addition, you can build one or more extra ways, each
distinguished by a <emphasis>way tag</emphasis>. The object
files and interface files for one of these extra ways are
distinguished by their suffix. For example, way
<literal>mp</literal> has files
<filename>.mp_o</filename> and
<filename>.mp_hi</filename>. Library archives have their
way tag the other side of the dot, for boring reasons; thus,
<filename>libHS_mp.a</filename>.</para>
<para>A <command>make</command> variable called
<constant>way</constant> holds the current way tag.
<emphasis><constant>way</constant> is only ever set on the
command line of <command>gmake</command></emphasis> (usually in
a recursive invocation of <command>gmake</command> by the
system). It is never set inside a
<filename>Makefile</filename>. So it is a global constant for
any one invocation of <command>gmake</command>. Two other
<command>make</command> variables,
<constant>way_</constant> and
<constant>_way</constant> are immediately derived from
<constant>$(way)</constant> and never altered. If
<constant>way</constant> is not set, then neither are
<constant>way_</constant> and
<constant>_way</constant>, and the invocation of
<command>make</command> will build the <quote>normal
way</quote>. If <constant>way</constant> is set, then the other
two variables are set in sympathy. For example, if
<constant>$(way)</constant> is “<literal>mp</literal>”,
then <constant>way_</constant> is set to
“<literal>mp_</literal>” and
<constant>_way</constant> is set to
“<literal>_mp</literal>”. These three variables are
then used when constructing file names.</para>
<para>So how does <command>make</command> ever get recursively
invoked with <constant>way</constant> set? There are two ways
in which this happens:</para>
<itemizedlist>
<listitem>
<para>For some (but not all) of the standard targets, when
in a leaf sub-directory, <command>make</command> is
recursively invoked for each way tag in
<constant>$(WAYS)</constant>. You set
<constant>WAYS</constant> in the
<filename>Makefile</filename> to the list of way tags you
want these targets built for. The mechanism here is very
much like the recursive invocation of
<command>make</command> in sub-directories (<xref
linkend="sec-subdirs">). It is up to you to set
<constant>WAYS</constant> in your
<filename>Makefile</filename>; this is how you control what
ways will get built.</para>
</listitem>
<listitem>
<para>For a useful collection of targets (such as
<filename>libHS_mp.a</filename>,
<filename>Foo.mp_o</filename>) there is a rule which
recursively invokes <command>make</command> to make the
specified target, setting the <constant>way</constant>
variable. So if you say <command>gmake
Foo.mp_o</command> you should see a recursive
invocation <command>gmake Foo.mp_o way=mp</command>,
and <emphasis>in this recursive invocation the pattern rule
for compiling a Haskell file into a <filename>.o</filename>
file will match</emphasis>. The key pattern rules (in
<filename>suffix.mk</filename>) look like this:
<programlisting>%.$(way_)o : %.lhs
$(HC) $(HC_OPTS) $< -o $@</programlisting>
Neat, eh?</para>
</listitem>
<listitem>
<para>You can invoke <command>make</command> with a
particular <literal>way</literal> setting yourself, in order
to build files related to a particular
<literal>way</literal> in the current directory. eg.
<screen>$ make way=p</screen>
will build files for the profiling way only in the current
directory. </para>
</listitem>
</itemizedlist>
</sect2>
<sect2>
<title>When the canned rule isn't right</title>
<para>Sometimes the canned rule just doesn't do the right thing.
For example, in the <literal>nofib</literal> suite we want the
link step to print out timing information. The thing to do here
is <emphasis>not</emphasis> to define
<constant>HS_PROG</constant> or
<constant>C_PROG</constant>, and instead define a special
purpose rule in your own <filename>Makefile</filename>. By
using different variable names you will avoid the canned rules
being included, and conflicting with yours.</para>
</sect2>
</sect1>
<sect1 id="building-docs">
<title>Building the documentation</title>
<sect2 id="pre-supposed-doc-tools">
<title>Tools for building the Documentation</title>
<para>The following additional tools are required if you want to
format the documentation that comes with the
<literal>fptools</literal> projects:</para>
<variablelist>
<varlistentry>
<term>DocBook
<indexterm><primary>pre-supposed: DocBook</primary></indexterm>
<indexterm><primary>DocBook, pre-supposed</primary></indexterm>
</term>
<listitem>
<para>Much of our documentation is written in SGML, using
the DocBook DTD. Instructions on installing and
configuring the DocBook tools are below.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>TeX
<indexterm><primary>pre-supposed: TeX</primary></indexterm>
<indexterm><primary>TeX, pre-supposed</primary></indexterm>
</term>
<listitem>
<para>A decent TeX distribution is required if you want to
produce printable documentation. We recomment teTeX,
which includes just about everything you need.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Haddock
<indexterm><primary>Haddock</primary></indexterm>
</term>
<listitem>
<para>Haddock is a Haskell documentation tool that we use
for automatically generating documentation from the
library source code. It is an <literal>fptools</literal>
project in itself. To build documentation for the
libraries (<literal>fptools/libraries</literal>) you
should check out and build Haddock in
<literal>fptools/haddock</literal>. Haddock requires GHC
to build.</para>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2>
<title>Installing the DocBook tools</title>
<sect3>
<title>Installing the DocBook tools on Linux</title>
<para>If you're on a recent RedHat system (7.0+), you probably
have working DocBook tools already installed. The configure
script should detect your setup and you're away.</para>
<para>If you don't have DocBook tools installed, and you are
using a system that can handle RedHat RPM packages, you can
probably use the <ulink
url="http://sourceware.cygnus.com/docbook-tools/">Cygnus
DocBook tools</ulink>, which is the most shrink-wrapped SGML
suite that we could find. You need all the RPMs except for
psgml (i.e. <filename>docbook</filename>,
<filename>jade</filename>, <filename>jadetex</filename>,
<filename>sgmlcommon</filename> and
<filename>stylesheets</filename>). Note that most of these
RPMs are architecture neutral, so are likely to be found in a
<filename>noarch</filename> directory. The SuSE RPMs also
work; the RedHat ones <emphasis>don't</emphasis> in RedHat 6.2
(7.0 and later should be OK), but they are easy to fix: just
make a symlink from
<filename>/usr/lib/sgml/stylesheets/nwalsh-modular/lib/dblib.dsl</filename>
to <filename>/usr/lib/sgml/lib/dblib.dsl</filename>. </para>
</sect3>
<sect3>
<title>Installing DocBook on FreeBSD</title>
<para>On FreeBSD systems, the easiest way to get DocBook up
and running is to install it from the ports tree or a
pre-compiled package (packages are available from your local
FreeBSD mirror site).</para>
<para>To use the ports tree, do this:
<screen>$ cd /usr/ports/textproc/docproj
$ make install</screen>
This installs the FreeBSD documentation project tools, which
includes everything needed to format the GHC
documentation.</para>
</sect3>
<sect3>
<title>Installing from binaries on Windows</title>
<para>It's a good idea to use Norman Walsh's <ulink
url="http://nwalsh.com/docbook/dsssl/doc/install.html">installation
notes</ulink> as a guide. You should get version 3.1 of
DocBook, and note that his file <filename>test.sgm</filename>
won't work, as it needs version 3.0. You should unpack Jade
into <filename>\Jade</filename>, along with the entities,
DocBook into <filename>\docbook</filename>, and the DocBook
stylesheets into <filename>\docbook\stylesheets</filename> (so
they actually end up in
<filename>\docbook\stylesheets\docbook</filename>).</para>
</sect3>
<sect3>
<title>Installing the DocBook tools from source</title>
<sect4>
<title>Jade</title>
<para>Install <ulink
url="http://openjade.sourceforge.net/">OpenJade</ulink>
(Windows binaries are available as well as sources). If you
want DVI, PS, or PDF then install JadeTeX from the
<filename>dsssl</filename> subdirectory. (If you get the
error:
<screen>! LaTeX Error: Unknown option implicit=false' for package hyperref'.</screen>
your version of <command>hyperref</command> is out of date;
download it from CTAN
(<filename>macros/latex/contrib/supported/hyperref</filename>),
and make it, ensuring that you have first removed or renamed
your old copy. If you start getting file not found errors
when making the test for <command>hyperref</command>, you
can abort at that point and proceed straight to
<command>make install</command>, or enter them as
<filename>../</filename><emphasis>filename</emphasis>.)</para>
<para>Make links from <filename>virtex</filename> to
<filename>jadetex</filename> and
<filename>pdfvirtex</filename> to
<filename>pdfjadetex</filename> (otherwise DVI, PostScript
and PDF output will not work). Copy
<filename>dsssl/*.{dtd,dsl}</filename> and
<filename>catalog</filename> to
<filename>/usr/[local/]lib/sgml</filename>.</para>
</sect4>
<sect4>
<title>DocBook and the DocBook stylesheets</title>
<para>Get a Zip of <ulink
url="http://www.oasis-open.org/docbook/sgml/3.1/index.html">DocBook</ulink>
and install the contents in
<filename>/usr/[local/]/lib/sgml</filename>.</para>
<para>Get the <ulink
url="http://nwalsh.com/docbook/dsssl/">DocBook
stylesheets</ulink> and install in
<filename>/usr/[local/]lib/sgml/stylesheets</filename>
(thereby creating a subdirectory docbook). For indexing,
copy or link <filename>collateindex.pl</filename> from the
DocBook stylesheets archive in <filename>bin</filename> into
a directory on your <constant>PATH</constant>.</para>
<para>Download the <ulink
url="http://www.oasis-open.org/cover/ISOEnts.zip">ISO
entities</ulink> into
<filename>/usr/[local/]lib/sgml</filename>.</para>
</sect4>
</sect3>
</sect2>
<sect2>
<title>Configuring the DocBook tools</title>
<para>Once the DocBook tools are installed, the configure script
will detect them and set up the build system accordingly. If you
have a system that isn't supported, let us know, and we'll try
to help.</para>
</sect2>
<sect2>
<title>Remaining problems</title>
<para>If you install from source, you'll get a pile of warnings
of the form
<screen>DTDDECL catalog entries are not supported</screen>
every time you build anything. These can safely be ignored, but
if you find them tedious you can get rid of them by removing all
the <constant>DTDDECL</constant> entries from
<filename>docbook.cat</filename>.</para>
</sect2>
<sect2>
<title>Building the documentation</title>
<para>To build documentation in a certain format, you can
say, for example,</para>
<screen>$ make html</screen>
<para>to build HTML documentation below the current directory.
The available formats are: <literal>dvi</literal>,
<literal>ps</literal>, <literal>pdf</literal>,
<literal>html</literal>, and <literal>rtf</literal>. Note that
not all documentation can be built in all of these formats: HTML
documentation is generally supported everywhere, and DocBook
documentation might support the other formats (depending on what
other tools you have installed).</para>
<para>All of these targets are recursive; that is, saying
<literal>make html</literal> will make HTML docs for all the
documents recursively below the current directory.</para>
<para>Because there are many different formats that the DocBook
documentation can be generated in, you have to select which ones
you want by setting the <literal>SGMLDocWays</literal> variable
to a list of them. For example, in
<filename>build.mk</filename> you might have a line:</para>
<screen>SGMLDocWays = html ps</screen>
<para>This will cause the documentation to be built in the requested
formats as part of the main build (the default is not to build
any documentation at all).</para>
</sect2>
<sect2>
<title>Installing the documentation</title>
<para>To install the documentation, use:</para>
<screen>$ make install-docs</screen>
<para>This will install the documentation into
<literal>$(datadir)</literal> (which defaults to
<literal>$(prefix)/share</literal>). The exception is HTML
documentation, which goes into
<literal>$(datadir)/html</literal>, to keep things tidy.</para>
<para>Note that unless you set <literal>$(SGMLDocWays)</literal>
to a list of formats, the <literal>install-docs</literal> target
won't do anything for SGML documentation.</para>
</sect2>
</sect1>
<sect1 id="sec-porting-ghc">
<title>Porting GHC</title>
<para>This section describes how to port GHC to a currenly
unsupported platform. There are two distinct
possibilities:</para>
<itemizedlist>
<listitem>
<para>The hardware architecture for your system is already
supported by GHC, but you're running an OS that isn't
supported (or perhaps has been supported in the past, but
currently isn't). This is the easiest type of porting job,
but it still requires some careful bootstrapping. Proceed to
<xref linkend="sec-booting-from-hc">.</para>
</listitem>
<listitem>
<para>Your system's hardware architecture isn't supported by
GHC. This will be a more difficult port (though by comparison
perhaps not as difficult as porting gcc). Proceed to <xref
linkend="unregisterised-porting">.</para>
</listitem>
</itemizedlist>
<sect2 id="sec-booting-from-hc">
<title>Booting/porting from C (<filename>.hc</filename>) files</title>
<indexterm><primary>building GHC from .hc files</primary></indexterm>
<indexterm><primary>booting GHC from .hc files</primary></indexterm>
<indexterm><primary>porting GHC</primary></indexterm>
<para>Bootstrapping GHC on a system without GHC already
installed is achieved by taking the intermediate C files (known
as HC files) from a GHC compilation on a supported system to the
target machine, and compiling them using gcc to get a working
GHC.</para>
<para><emphasis>NOTE: GHC versions 5.xx were hard to bootstrap
from C. We recommend using GHC 6.0.1 or
later.</emphasis></para>
<para>HC files are platform-dependent, so you have to get a set
that were generated on similar hardware. There may be some
supplied on the GHC download page, otherwise you'll have to
compile some up yourself, or start from
<emphasis>unregisterised</emphasis> HC files - see <xref
linkend="unregisterised-porting">.</para>
<para>The following steps should result in a working GHC build
with full libraries:</para>
<itemizedlist>
<listitem>
<para>Unpack the HC files on top of a fresh source tree
(make sure the source tree version matches the version of
the HC files <emphasis>exactly</emphasis>!). This will
place matching <filename>.hc</filename> files next to the
corresponding Haskell source (<filename>.hs</filename> or
<filename>.lhs</filename>) in the compiler subdirectory
<filename>ghc/compiler</filename> and in the libraries
(subdirectories of <filename>hslibs</filename> and
<literal>libraries</literal>).</para>
</listitem>
<listitem>
<para>The actual build process is fully automated by the
<filename>hc-build</filename> script located in the
<filename>distrib</filename> directory. If you eventually
want to install GHC into the directory
<replaceable>dir</replaceable>, the following
command will execute the whole build process (it won't
install yet):</para>
<screen>foo% distrib/hc-build --prefix=<replaceable>dir</replaceable></screen>
<indexterm><primary>--hc-build</primary></indexterm>
<para>By default, the installation directory is
<filename>/usr/local</filename>. If that is what you want,
you may omit the argument to <filename>hc-build</filename>.
Generally, any option given to <filename>hc-build</filename>
is passed through to the configuration script
<filename>configure</filename>. If
<filename>hc-build</filename> successfully completes the
build process, you can install the resulting system, as
normal, with</para>
<screen>foo% make install</screen>
</listitem>
</itemizedlist>
</sect2>
<sect2 id="unregisterised-porting">
<title>Porting GHC to a new architecture</title>
<para>The first step in porting to a new architecture is to get
an <firstterm>unregisterised</firstterm> build working. An
unregisterised build is one that compiles via vanilla C only.
By contrast, a registerised build uses the following
architecture-specific hacks for speed:</para>
<itemizedlist>
<listitem>
<para>Global register variables: certain abstract machine
<quote>registers</quote> are mapped to real machine
registers, depending on how many machine registers are
available (see
<filename>ghc/includes/MachRegs.h</filename>).</para>
</listitem>
<listitem>
<para>Assembly-mangling: when compiling via C, we feed the
assembly generated by gcc though a Perl script known as the
<firstterm>mangler</firstterm> (see
<filename>ghc/driver/mangler/ghc-asm.lprl</filename>). The
mangler rearranges the assembly to support tail-calls and
various other optimisations.</para>
</listitem>
</itemizedlist>
<para>In an unregisterised build, neither of these hacks are
used — the idea is that the C code generated by the
compiler should compile using gcc only. The lack of these
optimisations costs about a factor of two in performance, but
since unregisterised compilation is usually just a step on the
way to a full registerised port, we don't mind too much.</para>
<para>Notes on GHC portability in general: we've tried to stick
to writing portable code in most parts of the system, so it
should compile on any POSIXish system with gcc, but in our
experience most systems differ from the standards in one way or
another. Deal with any problems as they arise - if you get
stuck, ask the experts on
<email>glasgow-haskell-users@haskell.org</email>.</para>
<para>Lots of useful information about the innards of GHC is
available in the <ulink
url="http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/">GHC
Commentary</ulink>, which might be helpful if you run into some
code which needs tweaking for your system.</para>
<sect3>
<title>Cross-compiling to produce an unregisterised GHC</title>
<para>In this section, we explain how to bootstrap GHC on a
new platform, using unregisterised intermediate C files. We
haven't put a great deal of effort into automating this
process, for two reasons: it is done very rarely, and the
process usually requires human intervention to cope with minor
porting issues anyway.</para>
<para>The following step-by-step instructions should result in
a fully working, albeit unregisterised, GHC. Firstly, you
need a machine that already has a working GHC (we'll call this
the <firstterm>host</firstterm> machine), in order to
cross-compile the intermediate C files that we will use to
bootstrap the compiler on the <firstterm>target</firstterm>
machine.</para>
<itemizedlist>
<listitem>
<para>On the target machine:</para>
<itemizedlist>
<listitem>
<para>Unpack a source tree (preferably a released
version). We will call the path to the root of this
tree <replaceable>T</replaceable>.</para>
</listitem>
<listitem>
<screen>$ cd <replaceable>T</replaceable>
$ ./configure --enable-hc-boot --enable-hc-boot-unregisterised</screen>
<para>You might need to update
<filename>configure.in</filename> to recognise the new
architecture, and re-generate
<filename>configure</filename> with
<literal>autoreconf</literal>.</para>
</listitem>
<listitem>
<screen>$ cd <replaceable>T</replaceable>/ghc/includes
$ make config.h</screen>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>On the host machine:</para>
<itemizedlist>
<listitem>
<para>Unpack a source tree (same released version). Call
this directory <replaceable>H</replaceable>.</para>
</listitem>
<listitem>
<screen>$ cd <replaceable>H</replaceable>
$ ./configure</screen>
</listitem>
<listitem>
<para>Create
<filename><replaceable>H</replaceable>/mk/build.mk</filename>,
with the following contents:</para>
<programlisting>GhcUnregisterised = YES
GhcLibHcOpts = -O -H32m -keep-hc-files
GhcLibWays =
SplitObjs = NO
GhcWithNativeCodeGen = NO
GhcWithInterpreter = NO
GhcStage1HcOpts = -O -H32m -fasm
GhcStage2HcOpts = -O -fvia-C -keep-hc-files</programlisting>
</listitem>
<listitem>
<para>Edit
<filename><replaceable>H</replaceable>/mk/config.mk</filename>:</para>
<itemizedlist>
<listitem>
<para>change <literal>TARGETPLATFORM</literal>
appropriately, and set the variables involving
<literal>TARGET</literal> to the correct values for
the target platform. This step is necessary because
currently <literal>configure</literal> doesn't cope
with specifying different values for the
<literal>--host</literal> and
<literal>--target</literal> flags.</para>
</listitem>
<listitem>
<para>copy <literal>LeadingUnderscore</literal>
setting from target.</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Copy
<filename><replaceable>T</replaceable>/ghc/includes/config.h</filename>
to
<filename><replaceable>H</replaceable>/ghc/includes</filename>.
Note that we are building on the host machine, using the
target machine's <literal>config.h</literal> file. This
is so that the intermediate C files generated here will
be suitable for compiling on the target system.</para>
</listitem>
<listitem>
<para>Touch <literal>config.h</literal>, just to make
sure it doesn't get replaced during the build:</para>
<screen>$ touch <replaceable>H</replaceable>/ghc/includes/config.h</screen>
</listitem>
<listitem>
<para>Now build the compiler:</para>
<screen>$ cd <replaceable>H</replaceable>/glafp-utils && make boot && make
$ cd <replaceable>H</replaceable>/ghc && make boot && make</screen>
<para>Don't worry if the build falls over in the RTS, we
don't need the RTS yet.</para>
</listitem>
<listitem>
<screen>$ cd <replaceable>H</replaceable>/libraries
$ make boot && make</screen>
</listitem>
<listitem>
<screen>$ cd <replaceable>H</replaceable>/ghc
$ make boot stage=2 && make stage=2</screen>
</listitem>
<listitem>
<screen>$ cd <replaceable>H</replaceable>/ghc/utils
$ make clean
$ make -k HC=<replaceable>H</replaceable>/ghc/compiler/stage1/ghc-inplace \
EXTRA_HC_OPTS='-O -fvia-C -keep-hc-files'</screen>
</listitem>
<listitem>
<screen>$ cd <replaceable>H</replaceable>
$ make hc-file-bundle Project=Ghc</screen>
</listitem>
<listitem>
<para>copy
<filename><replaceable>H</replaceable>/*-hc.tar.gz</filename>
to <filename><replaceable>T</replaceable>/..</filename>.</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>On the target machine:</para>
<para>At this stage we simply need to bootstrap a compiler
from the intermediate C files we generated above. The
process of bootstrapping from C files is automated by the
script in <literal>distrib/hc-build</literal>, and is
described in <xref linkend="sec-booting-from-hc">.</para>
<screen>$ ./distrib/hc-build --enable-hc-boot-unregisterised</screen>
<para>However, since this is a bootstrap on a new machine,
the automated process might not run to completion the
first time. For that reason, you might want to treat the
<literal>hc-build</literal> script as a list of
instructions to follow, rather than as a fully automated
script. This way you'll be able to restart the process
part-way through if you need to fix anything on the
way.</para>
<para>Don't bother with running
<literal>make install</literal> in the newly
bootstrapped tree; just use the compiler in that tree to
build a fresh compiler from scratch, this time without
booting from C files. Before doing this, you might want
to check that the bootstrapped compiler is generating
working binaries:</para>
<screen>$ cat >hello.hs
main = putStrLn "Hello World!\n"
^D
$ <replaceable>T</replaceable>/ghc/compiler/ghc-inplace hello.hs -o hello
$ ./hello
Hello World!</screen>
<para>Once you have the unregisterised compiler up and
running, you can use it to start a registerised port. The
following sections describe the various parts of the
system that will need architecture-specific tweaks in
order to get a registerised build going.</para>
</listitem>
</itemizedlist>
</sect3>
<sect3>
<title>Porting the RTS</title>
<para>The following files need architecture-specific code for a
registerised build:</para>
<variablelist>
<varlistentry>
<term><filename>ghc/includes/MachRegs.h</filename>
<indexterm><primary><filename>MachRegs.h</filename></primary></indexterm>
</term>
<listitem>
<para>Defines the STG-register to machine-register
mapping. You need to know your platform's C calling
convention, and which registers are generally available
for mapping to global register variables. There are
plenty of useful comments in this file.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><filename>ghc/includes/TailCalls.h</filename>
<indexterm><primary><filename>TailCalls.h</filename></primary></indexterm>
</term>
<listitem>
<para>Macros that cooperate with the mangler (see <xref
linkend="sec-mangler">) to make proper tail-calls
work.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><filename>ghc/rts/Adjustor.c</filename>
<indexterm><primary><filename>Adjustor.c</filename></primary></indexterm>
</term>
<listitem>
<para>Support for
<literal>foreign import "wrapper"</literal>
(aka
<literal>foreign export dynamic</literal>).
Not essential for getting GHC bootstrapped, so this file
can be deferred until later if necessary.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><filename>ghc/rts/StgCRun.c</filename>
<indexterm><primary><filename>StgCRun.c</filename></primary></indexterm>
</term>
<listitem>
<para>The little assembly layer between the C world and
the Haskell world. See the comments and code for the
other architectures in this file for pointers.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><filename>ghc/rts/MBlock.h</filename>
<indexterm><primary><filename>MBlock.h</filename></primary></indexterm>
</term>
<term><filename>ghc/rts/MBlock.c</filename>
<indexterm><primary><filename>MBlock.c</filename></primary></indexterm>
</term>
<listitem>
<para>These files are really OS-specific rather than
architecture-specific. In <filename>MBlock.h</filename>
is specified the absolute location at which the RTS
should try to allocate memory on your platform (try to
find an area which doesn't conflict with code or dynamic
libraries). In <filename>Mblock.c</filename> you might
need to tweak the call to <literal>mmap()</literal> for
your OS.</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="sec-mangler">
<title>The mangler</title>
<para>The mangler is an evil Perl-script
(<filename>ghc/driver/mangler/ghc-asm.lprl</filename>) that
rearranges the assembly code output from gcc to do two main
things:</para>
<itemizedlist>
<listitem>
<para>Remove function prologues and epilogues, and all
movement of the C stack pointer. This is to support
tail-calls: every code block in Haskell code ends in an
explicit jump, so we don't want the C-stack overflowing
while we're jumping around between code blocks.</para>
</listitem>
<listitem>
<para>Move the <firstterm>info table</firstterm> for a
closure next to the entry code for that closure. In
unregisterised code, info tables contain a pointer to the
entry code, but in registerised compilation we arrange
that the info table is shoved right up against the entry
code, and addressed backwards from the entry code pointer
(this saves a word in the info table and an extra
indirection when jumping to the closure entry
code).</para>
</listitem>
</itemizedlist>
<para>The mangler is abstracted to a certain extent over some
architecture-specific things such as the particular assembler
directives used to herald symbols. Take a look at the
definitions for other architectures and use these as a
starting point.</para>
</sect3>
<sect3>
<title>The splitter</title>
<para>The splitter is another evil Perl script
(<filename>ghc/driver/split/ghc-split.lprl</filename>). It
cooperates with the mangler to support object splitting.
Object splitting is what happens when the
<option>-split-objs</option> option is passed to GHC: the
object file is split into many smaller objects. This feature
is used when building libraries, so that a program statically
linked against the library will pull in less of the
library.</para>
<para>The splitter has some platform-specific stuff; take a
look and tweak it for your system.</para>
</sect3>
<sect3>
<title>The native code generator</title>
<para>The native code generator isn't essential to getting a
registerised build going, but it's a desirable thing to have
because it can cut compilation times in half. The native code
generator is described in some detail in the <ulink
url="http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/">GHC
commentary</ulink>.</para>
</sect3>
<sect3>
<title>GHCi</title>
<para>To support GHCi, you need to port the dynamic linker
(<filename>fptools/ghc/rts/Linker.c</filename>). The linker
currently supports the ELF and PEi386 object file formats - if
your platform uses one of these then things will be
significantly easier. The majority of Unix platforms use the
ELF format these days. Even so, there are some
machine-specific parts of the ELF linker: for example, the
code for resolving particular relocation types is
machine-specific, so some porting of this code to your
architecture will probaly be necessary.</para>
<para>If your system uses a different object file format, then
you have to write a linker — good luck!</para>
</sect3>
</sect2>
</sect1>
<sect1 id="sec-build-pitfalls">
<title>Known pitfalls in building Glasgow Haskell
<indexterm><primary>problems, building</primary></indexterm>
<indexterm><primary>pitfalls, in building</primary></indexterm>
<indexterm><primary>building pitfalls</primary></indexterm></title>
<para>
WARNINGS about pitfalls and known “problems”:
</para>
<para>
<orderedlist>
<listitem>
<para>
One difficulty that comes up from time to time is running out of space
in <literal>TMPDIR</literal>. (It is impossible for the configuration stuff to
compensate for the vagaries of different sysadmin approaches to temp
space.)
<indexterm><primary>tmp, running out of space in</primary></indexterm>
The quickest way around it is <command>setenv TMPDIR /usr/tmp</command><indexterm><primary>TMPDIR</primary></indexterm> or
even <command>setenv TMPDIR .</command> (or the equivalent incantation with your shell
of choice).
The best way around it is to say
<programlisting>export TMPDIR=<dir></programlisting>
in your <filename>build.mk</filename> file.
Then GHC and the other <literal>fptools</literal> programs will use the appropriate directory
in all cases.
</para>
</listitem>
<listitem>
<para>
In compiling some support-code bits, e.g., in <filename>ghc/rts/gmp</filename> and even
in <filename>ghc/lib</filename>, you may get a few C-compiler warnings. We think these
are OK.
</para>
</listitem>
<listitem>
<para>
When compiling via C, you'll sometimes get “warning: assignment from
incompatible pointer type” out of GCC. Harmless.
</para>
</listitem>
<listitem>
<para>
Similarly, <command>ar</command>chiving warning messages like the following are not
a problem:
<screen>ar: filename GlaIOMonad__1_2s.o truncated to GlaIOMonad_
ar: filename GlaIOMonad__2_2s.o truncated to GlaIOMonad_
...</screen>
</para>
</listitem>
<listitem>
<para>
In compiling the compiler proper (in <filename>compiler/</filename>), you <emphasis>may</emphasis>
get an “Out of heap space” error message. These can vary with the
vagaries of different systems, it seems. The solution is simple:
<itemizedlist>
<listitem>
<para>
If you're compiling with GHC 4.00 or later, then the
<emphasis>maximum</emphasis> heap size must have been reached. This
is somewhat unlikely, since the maximum is set to 64M by default.
Anyway, you can raise it with the
<option>-optCrts-M<size></option> flag (add this flag to
<constant><module>_HC_OPTS</constant>
<command>make</command> variable in the appropriate
<filename>Makefile</filename>).
</para>
</listitem>
<listitem>
<para>
For GHC < 4.00, add a suitable <option>-H</option> flag to the <filename>Makefile</filename>, as
above.
</para>
</listitem>
</itemizedlist>
and try again: <command>gmake</command>. (see <xref linkend="sec-suffix"> for information about
<constant><module>_HC_OPTS</constant>.)
Alternatively, just cut to the chase:
<screen>% cd ghc/compiler
% make EXTRA_HC_OPTS=-optCrts-M128M</screen>
</para>
</listitem>
<listitem>
<para>
If you try to compile some Haskell, and you get errors from GCC about
lots of things from <filename>/usr/include/math.h</filename>, then your GCC was
mis-installed. <command>fixincludes</command> wasn't run when it should've been.
As <command>fixincludes</command> is now automagically run as part of GCC installation,
this bug also suggests that you have an old GCC.
</para>
</listitem>
<listitem>
<para>
You <emphasis>may</emphasis> need to re-<command>ranlib</command><indexterm><primary>ranlib</primary></indexterm> your libraries (on Sun4s).
<screen>% cd $(libdir)/ghc-x.xx/sparc-sun-sunos4
% foreach i ( `find . -name '*.a' -print` ) # or other-shell equiv...
? ranlib $i
? # or, on some machines: ar s $i
? end</screen>
We'd be interested to know if this is still necessary.
</para>
</listitem>
<listitem>
<para>
GHC's sources go through <command>cpp</command> before being compiled, and <command>cpp</command> varies
a bit from one Unix to another. One particular gotcha is macro calls
like this:
<programlisting>SLIT("Hello, world")</programlisting>
Some <command>cpp</command>s treat the comma inside the string as separating two macro
arguments, so you get
<screen>:731: macro `SLIT' used with too many (2) args</screen>
Alas, <command>cpp</command> doesn't tell you the offending file!
Workaround: don't put weird things in string args to <command>cpp</command> macros.
</para>
</listitem>
</orderedlist>
</para>
</sect1>
<sect1 id="platforms"><title>Platforms, scripts, and file names</title>
<para>
GHC is designed both to be built, and to run, on both Unix and Windows. This flexibility
gives rise to a good deal of brain-bending detail, which we have tried to collect in this chapter.
</para>
<sect2 id="cygwin-and-mingw"><title>Windows platforms: Cygwin, MSYS, and MinGW</title>
<para> The build system is built around Unix-y makefiles. Because it's not native,
the Windows situation for building GHC is particularly confusing. This section
tries to clarify, and to establish terminology.</para>
<sect3 id="ghc-mingw"><title>MinGW</title>
<para> <ulink url="http://www.mingw.org">MinGW (Minimalist GNU for Windows)</ulink>
is a collection of header
files and import libraries that allow one to use <command>gcc</command> and produce
native Win32 programs that do not rely on any third-party DLLs. The
current set of tools include GNU Compiler Collection (<command>gcc</command>), GNU Binary
Utilities (Binutils), GNU debugger (Gdb), GNU make, and a assorted
other utilities.
</para>
<para> The down-side of MinGW is that the MinGW libraries do not support anything like the full
Posix interface.
</para>
</sect3>
<sect3 id="ghc-cygwin"><title>Cygwin and MSYS</title>
<para>You can't use the MinGW to <emphasis>build</emphasis> GHC, because MinGW doesn't have a shell,
or the standard Unix commands such as <command>mv</command>, <command>rm</command>,
<command>ls</command>, nor build-system stuff such as <command>make</command> and <command>cvs</command>.
For that, there are two choices: <ulink url="http://www.cygwin.com">Cygwin</ulink>
and <ulink url="http://www.mingw.org/msys.shtml">MSYS</ulink>:
<itemizedlist>
<listitem><para>
Cygwin comes with compilation tools (<command>gcc</command>, <command>ld</command> and so on), which
compile code that has access to all of Posix. The price is that the executables must be
dynamically linked with the Cygwin DLL, so that <emphasis>you cannot run a Cywin-compiled program on a machine
that doesn't have Cygwin</emphasis>. Worse, Cygwin is a moving target. The name of the main DLL, <literal>cygwin1.dll</literal>
does not change, but the implementation certainly does. Even the interfaces to functions
it exports seem to change occasionally. </para>
</listitem>
<listitem><para>
MSYS is a fork of the Cygwin tree, so they
are fundamentally similar. However, MSYS is by design much smaller and simpler. Access to the file system goes
through fewer layers, so MSYS is quite a bit faster too.
</para>
<para>Furthermore, MSYS provides no compilation tools; it relies instead on the MinGW tools. These
compile binaries that run with no DLL support, on any Win32 system.
However, MSYS does come with all the make-system tools, such as <command>make</command>, <command>autoconf</command>,
<command>cvs</command>, <command>ssh</command> etc. To get these, you have to download the
MsysDTK (Developer Tool Kit) package, as well as the base MSYS package.
</para>
<para>MSYS does have a DLL, but it's only used by MSYS commands (<command>sh</command>, <command>rm</command>,
<command>ssh</command> and so on),
not by programs compiled under MSYS.
</para></listitem>
</itemizedlist>
</para>
</sect3>
<sect3><title>Targeting MinGW</title>
<para>We want GHC to compile programs that work on any Win32 system. Hence:
<itemizedlist>
<listitem><para>
GHC does invoke a C compiler, assembler, linker and so on, but we ensure that it only
invokes the MinGW tools, not the Cygwin ones. That means that the programs GHC compiles
will work on any system, but it also means that the programs GHC compiles do not have access
to all of Posix. In particular, they cannot import the (Haskell) Posix
library; they have to do
their input output using standard Haskell I/O libraries, or native Win32 bindings.</para>
<para> We will call a GHC that targets MinGW in this way <emphasis>GHC-mingw</emphasis>.</para>
</listitem>
<listitem><para>
To make the GHC distribution self-contained, the GHC distribution includes the MinGW <command>gcc</command>,
<command>as</command>, <command>ld</command>, and a bunch of input/output libraries.
</para></listitem>
</itemizedlist>
So <emphasis>GHC targets MinGW</emphasis>, not Cygwin.
It is in principle possible to build a version of GHC, <emphasis>GHC-cygwin</emphasis>,
that targets Cygwin instead. The up-side of GHC-cygwin is
that Haskell programs compiled by GHC-cygwin can import the (Haskell) Posix library.
<emphasis>We do not support GHC-cygwin, however; it is beyond our resources.</emphasis>
</para>
<para>While GHC <emphasis>targets</emphasis> MinGW, that says nothing about
how GHC is <emphasis>built</emphasis>. We use both MSYS and Cygwin as build environments for
GHC; both work fine, though MSYS is rather lighter weight.</para>
<para>In your build tree, you build a compiler called <command>ghc-inplace</command>. It
uses the <command>gcc</command> that you specify using the
<option>--with-gcc</option> flag when you run
<command>configure</command> (see below).
The makefiles are careful to use <command>ghc-inplace</command> (not <command>gcc</command>)
to compile any C files, so that it will in turn invoke the correct <command>gcc</command> rather that
whatever one happens to be in your path. However, the makefiles do use whatever <command>ld</command>
and <command>ar</command> happen to be in your path. This is a bit naughty, but (a) they are only
used to glom together .o files into a bigger .o file, or a .a file,
so they don't ever get libraries (which would be bogus; they might be the wrong libraries), and (b)
Cygwin and MinGW use the same .o file format. So its ok.
</para>
</sect3>
<sect3><title> File names </title>
<para>Cygwin, MSYS, and the underlying Windows file system all understand file paths of form <literal>c:/tmp/foo</literal>.
However:
<itemizedlist>
<listitem><para>
MSYS programs understand <filename>/bin</filename>, <filename>/usr/bin</filename>, and map Windows's lettered drives as
<filename>/c/tmp/foo</filename> etc. The exact mount table is given in the doc subdirectory of the MSYS distribution.
</para>
<para> When it invokes a command, the MSYS shell sees whether the invoked binary lives in the MSYS <filename>/bin</filename>
directory. If so, it just invokes it. If not, it assumes the program is no an MSYS program, and walks over the command-line
arguments changing MSYS paths into native-compatible paths. It does this inside sub-arguments and inside quotes. For example,
if you invoke
<programlisting>foogle -B/c/tmp/baz</programlisting>
the MSYS shell will actually call <literal>foogle</literal> with argument <literal>-Bc:/tmp/baz</literal>.
</para></listitem>
<listitem><para>
Cygwin programs have a more complicated mount table, and map the lettered drives as <filename>/cygdrive/c/tmp/foo</filename>.
</para>
<para>The Cygwin shell does no argument processing when invoking non-Cygwin programs.
</para></listitem>
</itemizedlist>
</para>
</sect3>
<sect3><title>Host System vs Target System</title>
<para>
In the source code you'll find various ifdefs looking like:
<programlisting>#ifdef mingw32_HOST_OS
...blah blah...
#endif</programlisting>
and
<programlisting>#ifdef mingw32_TARGET_OS
...blah blah...
#endif</programlisting>
These macros are set by the configure script (via the file config.h).
Which is which? The criterion is this. In the ifdefs in GHC's source code:
<itemizedlist>
<listitem>
<para>The "host" system is the one on which GHC itself will be run.</para>
</listitem>
<listitem>
<para>The "target" system is the one for which the program compiled by GHC will be run.</para>
</listitem>
</itemizedlist>
For a stage-2 compiler, in which GHCi is available, the "host" and "target" systems must be the same.
So then it doesn't really matter whether you use the HOST_OS or TARGET_OS cpp macros.
</para>
</sect3>
</sect2>
<sect2><title>Wrapper scripts</title>
<para>
Many programs, including GHC itself and hsc2hs, need to find associated binaries and libraries.
For <emphasis>installed</emphasis> programs, the strategy depends on the platform. We'll use
GHC itself as an example:
<itemizedlist>
<listitem> <para>
On Unix, the command <command>ghc</command> is a shell script, generated by adding installation
paths to the front of the source file <filename>ghc.sh</filename>,
that invokes the real binary, passing "-B<emphasis>path</emphasis>" as an argument to tell <command>ghc</command>
where to find its supporting files.
</para> </listitem>
<listitem> <para>
On vanilla Windows, it turns out to be much harder to make reliable script to be run by the
native Windows shell <command>cmd</command> (e.g. limits on the length
of the command line). So instead we invoke the GHC binary directly, with no -B flag.
GHC uses the Windows <literal>getExecDir</literal> function to find where the executable is,
and from that figures out where the supporting files are.
</para> </listitem>
</itemizedlist>
(You can find the layout of GHC's supporting files in the
section "Layout of installed files" of Section 2 of the GHC user guide.)
</para>
<para>
Things work differently for <emphasis>in-place</emphasis> execution, where you want to
execute a program that has just been built in a build tree. The difference is that the
layout of the supporting files is different.
In this case, whether on Windows or Unix, we always use a shell script. This works OK
on Windows because the script is executed by MSYS or Cygwin, which don't have the
shortcomings of the native Windows <command>cmd</command> shell.
</para>
</sect2>
</sect1>
<sect1 id="winbuild"><title>Instructions for building under Windows</title>
<para>
This section gives detailed instructions for how to build
GHC from source on your Windows machine. Similar instructions for
installing and running GHC may be found in the user guide. In general,
Win95/Win98 behave the same, and WinNT/Win2k behave the same.
</para>
<para>
Make sure you read the preceding section on platforms (<xref linkend="platforms">)
before reading section.
You don't need Cygwin or MSYS to <emphasis>use</emphasis> GHC,
but you do need one or the other to <emphasis>build</emphasis> GHC.</para>
<sect2 id="msys-install"><title>Installing and configuring MSYS</title>
<para>
MSYS is a lightweight alternative to Cygwin.
You don't need MSYS to <emphasis>use</emphasis> GHC,
but you do need it or Cygwin to <emphasis>build</emphasis> GHC.
Here's how to install MSYS.
<itemizedlist>
<listitem><para>
Go to <ulink url="http://www.mingw.org/download.shtml">http://www.mingw.org/download.shtml</ulink> and
download the following (of course, the version numbers will differ):
<itemizedlist>
<listitem><para>The main MSYS package (binary is sufficient): <literal>MSYS-1.0.9.exe</literal>
</para></listitem>
<listitem><para>The MSYS developer's toolkit (binary is sufficient): <literal>msysDTK-1.0.1.exe</literal>.
This provides <command>make</command>, <command>autoconf</command>,
<command>ssh</command>, <command>cvs</command> and probably more besides.
</para></listitem>
</itemizedlist>
Run both executables (in the order given above) to install them. I put them in <literal>c:/msys</literal>
</para></listitem>
<listitem><para>
Set the following environment variables
<itemizedlist>
<listitem><para><literal>PATH</literal>: add <literal>c:/msys/1.0/bin</literal> to your path. (Of course, the version number may differ.)
</para></listitem>
<listitem><para><literal>HOME</literal>: set to your home directory (e.g. <literal>c:/userid</literal>).
This is where, among other things, <command>ssh</command> will look for your <literal>.ssh</literal> directory.
</para></listitem>
<listitem><para><literal>SHELL</literal>: set to <literal>c:/msys/1.0/bin/sh.exe</literal>
</para></listitem>
<listitem><para><literal>CVS_RSH</literal>: set to <literal>c:/msys/1.0/bin/ssh.exe</literal>. Only necessary if
you are using CVS.
</para></listitem>
<listitem><para><literal>MAKE_MODE</literal>: set to <literal>UNIX</literal>. (I'm not certain this is necessary for MSYS.)
</para></listitem>
</itemizedlist>
</para></listitem>
<listitem><para>
Check that the <literal>CYGWIN</literal> environment variable is <emphasis>not</emphasis> set. It's a bad bug
that MSYS is affected by this, but if you have CYGWIN set to "ntsec ntea", which is right for Cygwin, it
causes the MSYS <command>ssh</command> to bogusly fail complaining that your <filename>.ssh/identity</filename>
file has too-liberal permissinos.
</para></listitem>
</itemizedlist>
</para>
<para>Here are some points to bear in mind when using MSYS:
<itemizedlist>
<listitem> <para> MSYS does some kind of special magic to binaries stored in
<filename>/bin</filename> and <filename>/usr/bin</filename>, which are by default both mapped
to <filename>c:/msys/1.0/bin</filename> (assuming you installed MSYS in <filename>c:/msys</filename>).
Do not put any other binaries (such as GHC or Alex) in this directory or its sub-directories:
they fail in mysterious ways. However, it's fine to put other binaries in <filename>/usr/local/bin</filename>,
which maps to <filename>c:/msys/1.0/local/bin</filename>.</para></listitem>
<listitem> <para> MSYS seems to implement symbolic links by copying, so sharing is lost.
</para></listitem>
<listitem> <para>
Win32 has a <command>find</command> command which is not the same as MSYS's find.
You will probably discover that the Win32 <command>find</command> appears in your <constant>PATH</constant>
before the MSYS one, because it's in the <emphasis>system</emphasis> <constant>PATH</constant>
environment variable, whereas you have probably modified the <emphasis>user</emphasis> <constant>PATH</constant>
variable. You can always invoke <command>find</command> with an absolute path, or rename it.
</para></listitem>
<listitem> <para>
MSYS comes with <command>bzip</command>, and MSYS's <command>tar</command>'s <literal>-j</literal>
will bunzip an archive (e.g. <literal>tar xvjf foo.tar.bz2</literal>). Useful when you get a
bzip'd dump.</para></listitem>
</itemizedlist>
</para>
</sect2>
<sect2><title>Installing and configuring Cygwin</title>
<para> Install Cygwin from <ulink url="http://www.cygwin.com/">http://www.cygwin.com/</ulink>.
The installation process is straightforward; we install it in <filename>c:/cygwin</filename>.
During the installation dialogue, make sure that you select all of the following:
<command>cvs</command>,
<command>openssh</command>,
<command>autoconf</command>,
<command>binutils</command> (includes ld and (I think) ar),
<command>gcc</command>,
<command>flex</command>,
<command>make</command>.
If you miss out any of these, strange things will happen to you. To see thse packages,
click on the "View" button in the "Select Packages"
stage of Cygwin's installation dialogue, until the view says "Full". The default view, which is
"Category" isn't very helpful, and the "View" button is rather unobtrousive.
</para>
<para> Now set the following user environment variables:
<itemizedlist>
<listitem><para> Add <filename>c:/cygwin/bin</filename> and <filename>c:/cygwin/usr/bin</filename> to your
<constant>PATH</constant></para></listitem>
<listitem>
<para>
Set <constant>MAKE_MODE</constant> to <literal>UNIX</literal>. If you
don't do this you get very weird messages when you type
<command>make</command>, such as:
<screen>/c: /c: No such file or directory</screen>
</para>
</listitem>
<listitem><para> Set <constant>SHELL</constant> to
<filename>c:/cygwin/bin/bash</filename>. When you invoke a shell in Emacs, this
<constant>SHELL</constant> is what you get.
</para></listitem>
<listitem><para> Set <constant>HOME</constant> to point to your
home directory. This is where, for example,
<command>bash</command> will look for your <filename>.bashrc</filename>
file. Ditto <command>emacs</command> looking for <filename>.emacsrc</filename>
</para></listitem>
</itemizedlist>
</para>
<para>
There are a few other things to do:
<itemizedlist>
<listitem>
<para>
By default, cygwin provides the command shell <filename>ash</filename>
as <filename>sh.exe</filename>. We have often seen build-system problems that
turn out to be due to bugs in <filename>ash</filename>
(to do with quoting
and length of command lines). On the other hand <filename>bash</filename> seems
to be rock solid.
So, in <filename>cygwin/bin</filename>
remove the supplied <filename>sh.exe</filename> (or rename it as <filename>ash.exe</filename>),
and copy <filename>bash.exe</filename> to <filename>sh.exe</filename>.
You'll need to do this in Windows Explorer or the Windows <command>cmd</command> shell, because
you can't rename a running program!
</para>
</listitem>
<listitem>
<para>
Some script files used in the make system start with "<command>#!/bin/perl</command>",
(and similarly for <command>sh</command>). Notice the hardwired path!
So you need to ensure that your <filename>/bin</filename> directory has the following
binaries in it:
<itemizedlist>
<listitem> <para><command>sh</command></para></listitem>
<listitem> <para><command>perl</command></para></listitem>
<listitem> <para><command>cat</command></para></listitem>
</itemizedlist>
All these come in Cygwin's <filename>bin</filename> directory, which you probably have
installed as <filename>c:/cygwin/bin</filename>. By default Cygwin mounts "<filename>/</filename>" as
<filename>c:/cygwin</filename>, so if you just take the defaults it'll all work ok.
(You can discover where your Cygwin
root directory <filename>/</filename> is by typing <command>mount</command>.)
Provided <filename>/bin</filename> points to the Cygwin <filename>bin</filename>
directory, there's no need to copy anything. If not, copy these binaries from the <filename>cygwin/bin</filename>
directory (after fixing the <filename>sh.exe</filename> stuff mentioned in the previous bullet).
</para>
</listitem>
</itemizedlist>
</para>
<para>Finally, here are some things to be aware of when using Cygwin:
<itemizedlist>
<listitem> <para>Cygwin doesn't deal well with filenames that include
spaces. "<filename>Program Files</filename>" and "<filename>Local files</filename>" are
common gotchas.
</para></listitem>
<listitem> <para> Cygwin implements a symbolic link as a text file with some
magical text in it. So other programs that don't use Cygwin's
I/O libraries won't recognise such files as symlinks.
In particular, programs compiled by GHC are meant to be runnable
without having Cygwin, so they don't use the Cygwin library, so
they don't recognise symlinks.
</para></listitem>
<listitem> <para>
See the notes in <xref linkend="msys-install"> about <command>find</command> and <command>bzip</command>,
which apply to Cygwin too.
</para></listitem>
</itemizedlist>
</para>
</sect2>
<sect2 id="configure-ssh"><title>Configuring SSH</title>
<para><command>ssh</command> comes with Cygwin, provided you remember to ask for it when
you install Cygwin. (If not, the installer lets you update easily.) Look for <command>openssh</command>
(not ssh) in the Cygwin list of applications!</para>
<para>There are several strange things about <command>ssh</command> on Windows that you need to know.
<itemizedlist>
<listitem>
<para>
The programs <command>ssh-keygen1</command>, <command>ssh1</command>, and <command>cvs</command>,
seem to lock up <command>bash</command> entirely if they try to get user input (e.g. if
they ask for a password). To solve this, start up <filename>cmd.exe</filename>
and run it as follows:
<screen>c:\tmp> set CYGWIN32=tty
c:\tmp> c:/user/local/bin/ssh-keygen1</screen> </para>
</listitem>
<listitem><para> (Cygwin-only problem, I think.)
<command>ssh</command> needs to access your directory <filename>.ssh</filename>, in your home directory.
To determine your home directory <command>ssh</command> first looks in
<filename>c:/cygwin/etc/passwd</filename> (or wherever you have Cygwin installed). If there's an entry
there with your userid, it'll use that entry to determine your home directory, <emphasis>ignoring
the setting of the environment variable $HOME</emphasis>. If the home directory is
bogus, <command>ssh</command> fails horribly. The best way to see what is going on is to say
<programlisting>ssh -v cvs.haskell.org</programlisting>
which makes <command>ssh</command> print out information about its activity.
</para>
<para> You can fix this problem, either by correcting the home-directory field in
<filename>c:/cygwin/etc/passwd</filename>, or by simply deleting the entire entry for your userid. If
you do that, <command>ssh</command> uses the $HOME environment variable instead.
</para>
</listitem>
<listitem>
<para>To protect your
<literal>.ssh</literal> from access by anyone else,
right-click your <literal>.ssh</literal> directory, and
select <literal>Properties</literal>. If you are not on
the access control list, add yourself, and give yourself
full permissions (the second panel). Remove everyone else
from the access control list. Don't leave them there but
deny them access, because 'they' may be a list that
includes you!</para>
</listitem>
<listitem>
<para>In fact <command>ssh</command> 3.6.1 now seems to <emphasis>require</emphasis>
you to have Unix permissions 600 (read/write for owner only)
on the <literal>.ssh/identity</literal> file, else it
bombs out. For your local C drive, it seems that <literal>chmod 600 identity</literal> works,
but on Windows NT/XP, it doesn't work on a network drive (exact dteails obscure).
The solution seems to be to set the $CYGWIN environment
variable to "<literal>ntsec neta</literal>". The $CYGWIN environment variable is discussed
in <ulink url="http://cygwin.com/cygwin-ug-net/using-cygwinenv.html">the Cygwin User's Guide</ulink>,
and there are more details in <ulink url="http://cygwin.com/faq/faq_4.html#SEC44">the Cygwin FAQ</ulink>.
</para>
</listitem>
</itemizedlist>
</para>
</sect2>
<sect2><title>Other things you need to install</title>
<para>You have to install the following other things to build GHC, listed below.</para>
<para>On Windows you often install executables in directories with spaces, such as
"<filename>Program Files</filename>". However, the <literal>make</literal> system for fptools doesn't
deal with this situation (it'd have to do more quoting of binaries), so you are strongly advised
to put binaries for all tools in places with no spaces in their path.
On both MSYS and Cygwin, it's perfectly OK to install such programs in the standard Unixy places,
<filename>/usr/local/bin</filename> and <filename>/usr/local/lib</filename>. But it doesn't matter,
provided they are in your path.
<itemizedlist>
<listitem>
<para>
Install an executable GHC, from <ulink url="http://www.haskell.org/ghc">http://www.haskell.org/ghc</ulink>.
This is what you will use to compile GHC. Add it in your
<constant>PATH</constant>: the installer tells you the path element
you need to add upon completion.
</para>
</listitem>
<listitem>
<para>
Install an executable Happy, from <ulink url="http://www.haskell.org/happy">http://www.haskell.org/happy</ulink>.
Happy is a parser generator used to compile the Haskell grammar. Under MSYS or Cygwin you can easily
build it from the source distribution using
<programlisting>./configure
make
make install</programlisting>
This should install it in <filename>/usr/local/bin</filename> (which maps to <filename>c:/msys/1.0/local/bin</filename>
on MSYS).
Make sure the installation directory is in your
<constant>PATH</constant>.
</para>
</listitem>
<listitem>
<para>Install Alex. This can be done by building from the
source distribution in the same way as Happy. Sources are
available from <ulink
url="http://www.haskell.org/alex">http://www.haskell.org/alex</ulink>.</para>
</listitem>
<listitem>
<para>GHC uses the <emphasis>mingw</emphasis> C compiler to
generate code, so you have to install that (see <xref linkend="cygwin-and-mingw">).
Just pick up a mingw bundle at
<ulink url="http://www.mingw.org/">http://www.mingw.org/</ulink>.
We install it in <filename>c:/mingw</filename>.
</para>
<para>Do <emphasis>not</emphasis> add any of the <emphasis>mingw</emphasis> binaries to your path.
They are only going to get used by explicit access (via the --with-gcc flag you
give to <command>configure</command> later). If you do add them to your path
you are likely to get into a mess because their names overlap with Cygwin binaries.
</para>
</listitem>
<listitem>
<para>We use <command>emacs</command> a lot, so we install that too.
When you are in <filename>fptools/ghc/compiler</filename>, you can use
"<literal>make tags</literal>" to make a TAGS file for emacs. That uses the utility
<filename>fptools/ghc/utils/hasktags/hasktags</filename>, so you need to make that first.
The most convenient way to do this is by going <literal>make boot</literal> in <filename>fptools/ghc</filename>.
The <literal>make tags</literal> command also uses <command>etags</command>, which comes with <command>emacs</command>,
so you will need to add <filename>emacs/bin</filename> to your <literal>PATH</literal>.
</para>
</listitem>
<listitem>
<para> Finally, check out a copy of GHC sources from
the CVS repository, following the instructions above (<xref linkend="cvs-access">).
</para>
</listitem>
</itemizedlist>
</para>
</sect2>
<sect2><title>Building GHC</title>
<para>OK!
Now go read the documentation above on building from source (<xref linkend="sec-building-from-source">);
the bullets below only tell
you about Windows-specific wrinkles.</para>
<itemizedlist>
<listitem>
<para>
If you used <command>autoconf</command> instead of <command>autoreconf</command>,
you'll get an error when you run <filename>./configure</filename>:
<screen>
...lots of stuff...
creating mk/config.h
mk/config.h is unchanged
configuring in ghc
running /bin/sh ./configure --cache-file=.././config.cache --srcdir=.
./configure: ./configure: No such file or directory
configure: error: ./configure failed for ghc</screen>
</para>
</listitem>
<listitem> <para><command>autoreconf</command> seems to create the file <filename>configure</filename>
read-only. So if you need to run autoreconf again (which I sometimes do for safety's sake),
you get
<screen>/usr/bin/autoconf: cannot create configure: permission denied</screen>
Solution: delete <filename>configure</filename> first.
</para></listitem>
<listitem>
<para>
After <command>autoreconf</command> run <command>./configure</command> in
<filename>fptools/</filename> thus:
<screen>./configure --host=i386-unknown-mingw32 --with-gcc=c:/mingw/bin/gcc</screen>
This is the point at which you specify that you are building GHC-mingw
(see <xref linkend="ghc-mingw">). </para>
<para> Both these options are important! It's possible to get into
trouble using the wrong C compiler!</para>
<para>
Furthermore, it's <emphasis>very important</emphasis> that you specify a
full MinGW path for <command>gcc</command>, not a Cygwin path, because GHC (which
uses this path to invoke <command>gcc</command>) is a MinGW program and won't
understand a Cygwin path. For example, if you
say <literal>--with-gcc=/mingw/bin/gcc</literal>, it'll be interpreted as
<filename>/cygdrive/c/mingw/bin/gcc</filename>, and GHC will fail the first
time it tries to invoke it. Worse, the failure comes with
no error message whatsoever. GHC simply fails silently when first invoked,
typically leaving you with this:
<screen>make[4]: Leaving directory `/cygdrive/e/fptools-stage1/ghc/rts/gmp'
../../ghc/compiler/ghc-inplace -optc-mno-cygwin -optc-O
-optc-Wall -optc-W -optc-Wstrict-prototypes -optc-Wmissing-prototypes
-optc-Wmissing-declarations -optc-Winline -optc-Waggregate-return
-optc-Wbad-function-cast -optc-Wcast-align -optc-I../includes
-optc-I. -optc-Iparallel -optc-DCOMPILING_RTS
-optc-fomit-frame-pointer -O2 -static
-package-name rts -O -dcore-lint -c Adjustor.c -o Adjustor.o
make[2]: *** [Adjustor.o] Error 1
make[1]: *** [all] Error 1
make[1]: Leaving directory `/cygdrive/e/fptools-stage1/ghc'
make: *** [all] Error 1</screen>
Be warned!
</para>
<para>
If you want to build GHC-cygwin (<xref linkend="ghc-cygwin">)
you'll have to do something more like:
<screen>./configure --with-gcc=...the Cygwin gcc...</screen>
</para>
</listitem>
<listitem><para>
If you are paranoid, delete <filename>config.cache</filename> if it exists.
This file occasionally remembers out-of-date configuration information, which
can be really confusing.
</para>
</listitem>
<listitem><para> You almost certainly want to set
<programlisting>SplitObjs = NO</programlisting>
in your <filename>build.mk</filename> configuration file (see <xref linkend="sec-build-config">).
This tells the build system not to split each library into a myriad of little object files, one
for each function. Doing so reduces binary sizes for statically-linked binaries, but on Windows
it dramatically increases the time taken to build the libraries in the first place.
</para>
</listitem>
<listitem><para> Do not attempt to build the documentation.
It needs all kinds of wierd Jade stuff that we haven't worked out for
Win32.</para></listitem>
</itemizedlist>
</sect2>
</sect1>
</article>
|