This file is indexed.

/usr/share/perl5/Config/AutoConf.pm is in libconfig-autoconf-perl 0.315-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
package Config::AutoConf;

use warnings;
use strict;

use base 'Exporter';

our @EXPORT = ('$LIBEXT', '$EXEEXT');

use constant QUOTE => do { $^O eq "MSWin32" ? q["] : q['] };

use Config;
use Carp qw/croak/;

use File::Temp qw/tempfile/;
use File::Basename;
use File::Spec;
use Text::ParseWords qw//;

use Capture::Tiny qw/capture/;

# in core since 5.7.3
eval "use Scalar::Util qw/looks_like_number/;";
__PACKAGE__->can("looks_like_number") or eval <<'EOP';
=begin private

=head2 looks_like_number

=end private

=cut

# from PP part of Params::Util
sub looks_like_number {
    local $_ = shift;

    # checks from perlfaq4
    return 0 if !defined($_);
    if (ref($_)) {
        return overload::Overloaded($_) ? defined(0 + $_) : 0;
    }
    return 1 if (/^[+-]?[0-9]+$/); # is a +/- integer
    return 1 if (/^([+-]?)(?=[0-9]|\.[0-9])[0-9]*(\.[0-9]*)?([Ee]([+-]?[0-9]+))?$/); # a C float
    return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);

    0;
}
EOP

eval "use File::Slurp::Tiny qw/read_file/;";
__PACKAGE__->can("read_file") or eval <<'EOP';
=begin private

=head2 read_file

=end private

=cut

sub read_file {
  my $fn = shift;
  local $@ = "";
  open( my $fh, "<", $fn ) or croak "Error opening $fn: $!";
  my $fc = <$fh>;
  close($fh) or croak "I/O error closing $fn: $!";
  return $fc;
}
EOP

# PA-RISC1.1-thread-multi
my %special_dlext = (
    darwin  => ".dylib",
    MSWin32 => ".dll",
    ($Config{archname} =~ m/PA-RISC/i ? ("hpux" => ".sl") : ()),
);

our ($LIBEXT, $EXEEXT);

defined $LIBEXT
  or $LIBEXT =
    defined $Config{so}         ? "." . $Config{so}
  : defined $special_dlext{$^O} ? $special_dlext{$^O}
  :                               ".so";
defined $EXEEXT
  or $EXEEXT = ($^O eq "MSWin32") ? ".exe" : "";

=encoding UTF-8

=head1 NAME

Config::AutoConf - A module to implement some of AutoConf macros in pure perl.

=cut

our $VERSION = '0.315';
$VERSION = eval $VERSION;

=head1 ABSTRACT

With this module I pretend to simulate some of the tasks AutoConf
macros do. To detect a command, to detect a library, etc.

=head1 SYNOPSIS

    use Config::AutoConf;

    Config::AutoConf->check_prog("agrep");
    my $grep = Config::AutoConf->check_progs("agrep", "egrep", "grep");

    Config::AutoConf->check_header("ncurses.h");
    my $curses = Config::AutoConf->check_headers("ncurses.h","curses.h");

    Config::AutoConf->check_prog_awk;
    Config::AutoConf->check_prog_egrep;

    Config::AutoConf->check_cc();

    Config::AutoConf->check_lib("ncurses", "tgoto");

    Config::AutoConf->check_file("/etc/passwd"); # -f && -r

=head1 DESCRIPTION

Config::AutoConf is intended to provide the same opportunities to Perl
developers as L<GNU Autoconf|http://www.gnu.org/software/autoconf/>
does for Shell developers.

As Perl is the second most deployed language (mind: every Unix comes
with Perl, several mini-computers have Perl and even lot's of Windows
machines run Perl software - which requires deployed Perl there, too),
this gives wider support than Shell based probes.

The API is leaned against GNU Autoconf, but we try to make the API
(especially optional arguments) more Perl'ish than m4 abilities allow
to the original.

=head1 CONSTRUCTOR

=cut

my $glob_instance;

=head2 new

This function instantiates a new instance of Config::AutoConf, eg. to
configure child components. The constructor adds also values set via
environment variable C<PERL5_AUTOCONF_OPTS>.

=cut

sub new
{
    my $class = shift;
    ref $class and $class = ref $class;
    my %args = @_;

    my %flags = map {
        my ($k, $v) = split("=", $_, 2);
        defined $v or $v = 1;
        ($k, $v)
    } split(":", $ENV{PERL5_AC_OPTS}) if ($ENV{PERL5_AC_OPTS});

    my %instance = (
        msg_prefix     => 'configure: ',
        lang           => "C",
        lang_stack     => [],
        lang_supported => {
            "C" => $class->can("check_prog_cc"),
        },
        cache                  => {},
        defines                => {},
        extra_libs             => [],
        extra_lib_dirs         => [],
        extra_include_dirs     => [],
        extra_preprocess_flags => [],
        extra_compile_flags    => {
            "C" => [],
        },
        extra_link_flags => [],
        logfile          => "config.log",
        c_ac_flags       => {%flags},
        %args
    );
    bless(\%instance, $class);
}

=head1 METHODS

=head2 check_file

This function checks if a file exists in the system and is readable by
the user. Returns a boolean. You can use '-f $file && -r $file' so you
don't need to use a function call.

=cut

sub check_file
{
    my $self = shift->_get_instance();
    my $file = shift;

    my $cache_name = $self->_cache_name("file", $file);
    $self->check_cached(
        $cache_name,
        "for $file",
        sub {
            -f $file && -r $file;
        }
    );
}

=head2 check_files

This function checks if a set of files exist in the system and are
readable by the user. Returns a boolean.

=cut

sub check_files
{
    my $self = shift->_get_instance();

    for (@_)
    {
        return 0 unless $self->check_file($_);
    }

    1;
}

sub _sanitize_prog
{
    my ($self, $prog) = @_;
    (scalar Text::ParseWords::shellwords $prog) > 1 and $prog = QUOTE . $prog . QUOTE;
    $prog;
}

my @exe_exts = ($^O eq "MSWin32" ? qw(.exe .com .bat .cmd) : (""));

=head2 check_prog( $prog, \@dirlist?, \%options? )

This function checks for a program with the supplied name. In success
returns the full path for the executable;

An optional array reference containing a list of directories to be searched
instead of $PATH is gracefully honored.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.

=cut

sub check_prog
{
    my $self = shift->_get_instance();
    # sanitize ac_prog
    my $ac_prog = _sanitize(shift @_);

    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;

    my @dirlist;
    @_ and scalar @_ > 1 and @dirlist = @_;
    @_ and scalar @_ == 1 and ref $_[0] eq "ARRAY" and @dirlist = @{$_[0]};
    @dirlist or @dirlist = split(/$Config{path_sep}/, $ENV{PATH});

    for my $p (@dirlist)
    {
        for my $e (@exe_exts)
        {
            my $cmd = $self->_sanitize_prog(File::Spec->catfile($p, $ac_prog . $e));
            -x $cmd
              and $options->{action_on_true}
              and ref $options->{action_on_true} eq "CODE"
              and $options->{action_on_true}->();
            -x $cmd and return $cmd;
        }
    }

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and $options->{action_on_false}->();

    return;
}

=head2 check_progs(progs, [dirlist])

This function takes a list of program names. Returns the full path for
the first found on the system. Returns undef if none was found.

An optional array reference containing a list of directories to be searched
instead of $PATH is gracefully honored.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively. The
name of the I<$prog> to check and the found full path are passed as first
and second argument to the I<action_on_true> callback.

=cut

sub check_progs
{
    my $self = shift->_get_instance();

    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;

    my @dirlist;
    scalar @_ > 1 and ref $_[-1] eq "ARRAY" and @dirlist = @{pop @_};
    @dirlist or @dirlist = split(/$Config{path_sep}/, $ENV{PATH});

    my @progs = @_;
    foreach my $prog (@progs)
    {
        defined $prog or next;

        my $ans = $self->check_prog($prog, \@dirlist);
              $ans
          and $options->{action_on_true}
          and ref $options->{action_on_true} eq "CODE"
          and $options->{action_if_true}->($prog, $ans);

        $ans and return $ans;
    }

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and $options->{action_on_false}->();

    return;
}

sub _append_prog_args
{
    my $self = shift->_get_instance();
    my $prog = shift;
    join(" ", $self->_sanitize_prog($prog), @_);
}

=head2 check_prog_yacc

From the autoconf documentation,

  If `bison' is found, set [...] `bison -y'.
  Otherwise, if `byacc' is found, set [...] `byacc'. 
  Otherwise set [...] `yacc'.  The result of this test can be influenced
  by setting the variable YACC or the cache variable ac_cv_prog_YACC.

Returns the full path, if found.

=cut

sub check_prog_yacc
{
    my $self = shift->_get_instance();

    # my ($self, $cache_name, $message, $check_sub) = @_;

    my $cache_name = $self->_cache_name("prog", "YACC");
    $self->check_cached(
        $cache_name,
        "for yacc",
        sub {
            defined $ENV{YACC} and return $ENV{YACC};
            my $binary = $self->check_progs(qw/bison byacc yacc/);
            defined $binary
              and $binary =~ /bison(?:\.(?:exe|com|bat|cmd))?$/
              and $binary = $self->_append_prog_args($binary, "-y");
            return $binary;
        }
    );
}

=head2 check_prog_awk

From the autoconf documentation,

  Check for `gawk', `mawk', `nawk', and `awk', in that order, and
  set output [...] to the first one that is found.  It tries
  `gawk' first because that is reported to be the best implementation.
  The result can be overridden by setting the variable AWK or the
  cache variable ac_cv_prog_AWK.

Note that it returns the full path, if found.

=cut

sub check_prog_awk
{
    my $self = shift->_get_instance();
    my $cache_name = $self->_cache_name("prog", "AWK");
    $self->check_cached($cache_name, "for awk", sub { $ENV{AWK} || $self->check_progs(qw/gawk mawk nawk awk/) });
}

=head2 check_prog_egrep

From the autoconf documentation,

  Check for `grep -E' and `egrep', in that order, and [...] output
  [...] the first one that is found.  The result can be overridden by
  setting the EGREP variable and is cached in the ac_cv_path_EGREP
  variable. 

Note that it returns the full path, if found.

=cut

sub check_prog_egrep
{
    my $self = shift->_get_instance();

    my $cache_name = $self->_cache_name("prog", "EGREP");
    $self->check_cached(
        $cache_name,
        "for egrep",
        sub {
            defined $ENV{EGREP} and return $ENV{EGREP};
            my $grep;
            $grep = $self->check_progs("egrep") and return $grep;

            if ($grep = $self->check_prog("grep"))
            {
                # check_run - Capture::Tiny, Open3 ... ftw!
                my $ans = `echo a | ($grep -E '(a|b)') 2>/dev/null`;
                chomp $ans;
                $ans eq "a" and return $self->_append_prog_args($grep, "-E");
            }
        }
    );
}

=head2 check_prog_lex

From the autoconf documentation,

  If flex is found, set output [...] to ‘flex’ and [...] to -lfl, if that
  library is in a standard place. Otherwise set output [...] to ‘lex’ and
  [...] to -ll, if found. If [...] packages [...] ship the generated
  file.yy.c alongside the source file.l, this [...] allows users without a
  lexer generator to still build the package even if the timestamp for
  file.l is inadvertently changed.

Note that it returns the full path, if found.

The structure $self->{lex} is set with attributes

  prog => $LEX
  lib => $LEXLIB
  root => $lex_root

=cut

sub check_prog_lex
{
    my $self       = shift->_get_instance;
    my $cache_name = $self->_cache_name("prog", "LEX");
    my $lex        = $self->check_cached($cache_name, "for lex", sub { $ENV{LEX} || $self->check_progs(qw/flex lex/) });
    if ($lex)
    {
        defined $self->{lex}->{prog} or $self->{lex}->{prog} = $lex;
        my $lex_root_var = $self->check_cached(
            "ac_cv_prog_lex_root",
            "for lex output file root",
            sub {
                my ($fh, $filename) = tempfile(
                    "testXXXXXX",
                    SUFFIX => '.l',
                    UNLINK => 0
                );
                my $src = <<'EOLEX';
%%
a { ECHO; }
b { REJECT; }
c { yymore (); }
d { yyless (1); }
e { /* IRIX 6.5 flex 2.5.4 underquotes its yyless argument.  */
    yyless ((input () != 0)); }
f { unput (yytext[0]); }
. { BEGIN INITIAL; }
%%
#ifdef YYTEXT_POINTER
extern char *yytext;
#endif
int
main (void)
{
  return ! yylex () + ! yywrap ();
}
EOLEX

                print {$fh} $src;
                close $fh;

                my ($stdout, $stderr, $exit) =
                  capture { system($lex, $filename); };
                chomp $stdout;
                unlink $filename;
                -f "lex.yy.c" and return "lex.yy";
                -f "lexyy.c"  and return "lexyy";
                $self->msg_error("cannot find output from $lex; giving up");
            }
        );
        defined $self->{lex}->{root} or $self->{lex}->{root} = $lex_root_var;

        my $conftest = read_file($lex_root_var . ".c");
        unlink $lex_root_var . ".c";

        $cache_name = $self->_cache_name("lib", "lex");
        my $check_sub = sub {
            my @save_libs = @{$self->{extra_libs}};
            my $have_lib  = 0;
            foreach my $libstest (undef, qw(-lfl -ll))
            {
                # XXX would local work on array refs? can we omit @save_libs?
                $self->{extra_libs} = [@save_libs];
                defined($libstest) and unshift(@{$self->{extra_libs}}, $libstest);
                $self->link_if_else($conftest)
                  and ($have_lib = defined($libstest) ? $libstest : "none required")
                  and last;
            }
            $self->{extra_libs} = [@save_libs];

            if ($have_lib)
            {
                $self->define_var(_have_lib_define_name("lex"), $have_lib, "defined when lex library is available");
            }
            else
            {
                $self->define_var(_have_lib_define_name("lex"), undef, "defined when lex library is available");
            }
            return $have_lib;
        };

        my $lex_lib = $self->check_cached($cache_name, "lex library", $check_sub);
        defined $self->{lex}->{lib} or $self->{lex}->{lib} = $lex_lib;
    }

    $lex;
}

=head2 check_prog_sed

From the autoconf documentation,

  Set output variable [...] to a Sed implementation that conforms to Posix
  and does not have arbitrary length limits. Report an error if no
  acceptable Sed is found. See Limitations of Usual Tools, for more
  information about portability problems with Sed.

  The result of this test can be overridden by setting the SED variable and
  is cached in the ac_cv_path_SED variable. 

Note that it returns the full path, if found.

=cut

sub check_prog_sed
{
    my $self = shift->_get_instance();
    my $cache_name = $self->_cache_name("prog", "SED");
    $self->check_cached($cache_name, "for sed", sub { $ENV{SED} || $self->check_progs(qw/gsed sed/) });
}

=head2 check_prog_pkg_config

Checks for C<pkg-config> program. No additional tests are made for it ...

=cut

sub check_prog_pkg_config
{
    my $self = shift->_get_instance();
    my $cache_name = $self->_cache_name("prog", "PKG_CONFIG");
    $self->check_cached($cache_name, "for pkg-config", sub { $self->check_prog("pkg-config") });
}

=head2 check_prog_cc

Determine a C compiler to use. Currently the probe is delegated to L<ExtUtils::CBuilder>.

=cut

sub check_prog_cc
{
    my $self = shift->_get_instance();
    my $cache_name = $self->_cache_name("prog", "CC");

    $self->check_cached(
        $cache_name,
        "for cc",
        sub {
            $self->{lang_supported}->{C} = undef;
            eval "use ExtUtils::CBuilder;";
            $@ and return;
            my $cb = ExtUtils::CBuilder->new(quiet => 1);
            $cb->have_compiler or return;
            $self->{lang_supported}->{C} = "ExtUtils::CBuilder";
            $cb->{config}->{cc};
        }
    );
}

=head2 check_cc

(Deprecated) Old name of L</check_prog_cc>.

=cut

sub check_cc { shift->check_prog_cc(@_) }

=head2 check_valid_compiler

This function checks for a valid compiler for the currently active language.
At the very moment only C<C> is understood (corresponding to your compiler
default options, e.g. -std=gnu89).

=cut

sub check_valid_compiler
{
    my $self = shift->_get_instance;
    my $lang = $self->{lang};
    $lang eq "C" or $self->msg_error("Language $lang is not supported");
    $self->check_prog_cc;
}

=head2 check_valid_compilers(;\@)

Checks for valid compilers for each given language. When unspecified
defaults to C<[ "C" ]>.

=cut

sub check_valid_compilers
{
    my $self = shift;
    for my $lang (@{$_[0]})
    {
        $self->push_lang($lang);
        my $supp = $self->check_valid_compiler;
        $self->pop_lang($lang);
        $supp or return 0;
    }

    1;
}

=head2 msg_checking

Prints "Checking @_ ..."

=cut

sub msg_checking
{
    my $self = shift->_get_instance();
    $self->{quiet}
      or print "Checking " . join(" ", @_) . "... ";
    $self->_add_log_entry("Checking " . join(" ", @_, "..."));
    return;
}

=head2 msg_result

Prints result \n

=cut

my @_num_to_msg = qw/no yes/;

sub _neat
{
    defined $_[0] or return "";
    looks_like_number($_[0]) and defined $_num_to_msg[$_[0]] and return $_num_to_msg[$_[0]];
    $_[0];
}

sub msg_result
{
    my $self = shift->_get_instance();
    $self->{quiet}
      or print join(" ", map { _neat $_ } @_), "\n";
    $self->_add_log_entry(join(" ", map { _neat $_ } @_), "\n");
    return;
}

=head2 msg_notice

Prints "configure: " @_ to stdout

=cut

sub msg_notice
{
    my $self = shift->_get_instance();
    $self->{quiet}
      or print $self->{msg_prefix} . join(" ", @_) . "\n";
    $self->_add_log_entry($self->{msg_prefix} . join(" ", @_) . "\n");
    return;
}

=head2 msg_warn

Prints "configure: " @_ to stderr

=cut

sub msg_warn
{
    my $self = shift->_get_instance();
    print STDERR $self->{msg_prefix} . join(" ", @_) . "\n";
    $self->_add_log_entry("WARNING: " . $self->{msg_prefix} . join(" ", @_) . "\n");
    return;
}

=head2 msg_error

Prints "configure: " @_ to stderr and exits with exit code 0 (tells
toolchain to stop here and report unsupported environment)

=cut

sub msg_error
{
    my $self = shift->_get_instance();
    print STDERR $self->{msg_prefix} . join(" ", @_) . "\n";
    $self->_add_log_entry("ERROR: " . $self->{msg_prefix} . join(" ", @_) . "\n");
    exit(0);    # #toolchain agreement: prevents configure stage to finish
}

=head2 msg_failure

Prints "configure: " @_ to stderr and exits with exit code 0 (tells
toolchain to stop here and report unsupported environment). Additional
details are provides in config.log (probably more information in a
later stage).

=cut

sub msg_failure
{
    my $self = shift->_get_instance();
    print STDERR $self->{msg_prefix} . join(" ", @_) . "\n";
    $self->_add_log_entry("FAILURE: " . $self->{msg_prefix} . join(" ", @_) . "\n");
    exit(0);    # #toolchain agreement: prevents configure stage to finish
}

=head2 define_var( $name, $value [, $comment ] )

Defines a check variable for later use in further checks or code to compile.
Returns the value assigned value

=cut

sub define_var
{
    my $self = shift->_get_instance();
    my ($name, $value, $comment) = @_;

    defined($name) or croak("Need a name to add a define");
    $self->{defines}->{$name} = [$value, $comment];
    $value;
}

=head2 write_config_h( [$target] )

Writes the defined constants into given target:

  Config::AutoConf->write_config_h( "config.h" );

=cut

sub write_config_h
{
    my $self = shift->_get_instance();
    my $tgt;

    defined($_[0])
      ? (
        ref($_[0])
        ? $tgt = $_[0]
        : open($tgt, ">", $_[0])
      )
      : open($tgt, ">", "config.h");

    my $conf_h = <<'EOC';
/**
 * Generated from Config::AutoConf
 *
 * Do not edit this file, all modifications will be lost,
 * modify Makefile.PL or Build.PL instead.
 *
 * Inspired by GNU AutoConf.
 *
 * (c) 2011 Alberto Simoes & Jens Rehsack
 */
#ifndef __CONFIG_H__

EOC

    while (my ($defname, $defcnt) = each(%{$self->{defines}}))
    {
        if ($defcnt->[0])
        {
            defined $defcnt->[1] and $conf_h .= "/* " . $defcnt->[1] . " */\n";
            $conf_h .= join(" ", "#define", $defname, $defcnt->[0]) . "\n";
        }
        else
        {
            defined $defcnt->[1] and $conf_h .= "/* " . $defcnt->[1] . " */\n";
            $conf_h .= "/* " . join(" ", "#undef", $defname) . " */\n\n";
        }
    }
    $conf_h .= "#endif /* ?__CONFIG_H__ */\n";

    print {$tgt} $conf_h;

    return;
}

=head2 push_lang(lang [, implementor ])

Puts the current used language on the stack and uses specified language
for subsequent operations until ending pop_lang call.

=cut

sub push_lang
{
    my $self = shift->_get_instance();

    push @{$self->{lang_stack}}, [$self->{lang}];

    $self->_set_language(@_);
}

=head2 pop_lang([ lang ])

Pops the currently used language from the stack and restores previously used
language. If I<lang> specified, it's asserted that the current used language
equals to specified language (helps finding control flow bugs).

=cut

sub pop_lang
{
    my $self = shift->_get_instance();

    scalar(@{$self->{lang_stack}}) > 0 or croak("Language stack empty");
    defined($_[0])
      and $self->{lang} ne $_[0]
      and croak("pop_lang( $_[0] ) doesn't match language in use (" . $self->{lang} . ")");

    $self->_set_language(@{pop @{$self->{lang_stack}}});
}

=head2 lang_build_program( prologue, body )

Builds program for current chosen language. If no prologue is given
(I<undef>), the default headers are used. If body is missing, default
body is used.

Typical call of

  Config::AutoConf->lang_build_program( "const char hw[] = \"Hello, World\\n\";",
                                        "fputs (hw, stdout);" )

will create

  const char hw[] = "Hello, World\n";

  /* Override any gcc2 internal prototype to avoid an error.  */
  #ifdef __cplusplus
  extern "C" {
  #endif

  int
  main (int argc, char **argv)
  {
    (void)argc;
    (void)argv;
    fputs (hw, stdout);;
    return 0;
  }

  #ifdef __cplusplus
  }
  #endif

=cut

sub lang_build_program
{
    my ($self, $prologue, $body) = @_;
    ref $self or $self = $self->_get_instance();

    defined($prologue) or $prologue = $self->_default_includes();
    defined($body)     or $body     = "";
    $body = $self->_build_main($body);

    $self->_fill_defines() . "\n$prologue\n\n$body\n";
}

sub _lang_prologue_func
{
    my ($self, $prologue, $function) = @_;
    ref $self or $self = $self->_get_instance();

    defined($prologue) or $prologue = $self->_default_includes();
    $prologue .= <<"_ACEOF";
/* Override any GCC internal prototype to avoid an error.
   Use char because int might match the return type of a GCC
   builtin and then its argument prototype would still apply.  */
#ifdef __cplusplus
extern "C" {
#endif
char $function ();
#ifdef __cplusplus
}
#endif
_ACEOF

    return $prologue;
}

sub _lang_body_func
{
    my ($self, $function) = @_;
    ref $self or $self = $self->_get_instance();

    my $func_call = "return $function ();";
    return $func_call;
}

=head2 lang_call( [prologue], function )

Builds program which simply calls given function.
When given, prologue is prepended otherwise, the default
includes are used.

=cut

sub lang_call
{
    my ($self, $prologue, $function) = @_;
    ref $self or $self = $self->_get_instance();

    return $self->lang_build_program($self->_lang_prologue_func($prologue, $function), $self->_lang_body_func($function),);
}

sub _lang_prologue_builtin
{
    my ($self, $prologue, $builtin) = @_;
    ref $self or $self = $self->_get_instance();

    defined($prologue) or $prologue = $self->_default_includes();
    $prologue .= <<"_ACEOF";
#if !defined(__has_builtin)
#undef $builtin
/* Declare this builtin with the same prototype as __builtin_$builtin.
  This removes a warning about conflicting types for built-in builtin $builtin */
__typeof__(__builtin_$builtin) $builtin;
__typeof__(__builtin_$builtin) *f = $builtin;
#endif
_ACEOF
}

sub _lang_body_builtin
{
    my ($self, $builtin) = @_;
    ref $self or $self = $self->_get_instance();

    my $body = <<"_ACEOF";
#if !defined(__has_builtin)
return f != $builtin;
#else
return __has_builtin($builtin);
#endif
_ACEOF
    return $body;
}

=head2 lang_builtin( [prologue], builtin )

Builds program which simply proves whether a builtin is known to
language compiler.

=cut

sub lang_builtin
{
    my ($self, $prologue, $builtin) = @_;
    ref $self or $self = $self->_get_instance();

    return $self->lang_build_program($self->_lang_prologue_func($prologue, $builtin), $self->_lang_body_builtin($builtin),);
}

=head2 lang_build_bool_test (prologue, test, [@decls])

Builds a static test which will fail to compile when test
evaluates to false. If C<@decls> is given, it's prepended
before the test code at the variable definition place.

=cut

sub lang_build_bool_test
{
    my ($self, $prologue, $test, @decls) = @_;
    ref $self or $self = $self->_get_instance();

    defined($test) or $test = "1";
    my $test_code = <<ACEOF;
  static int test_array [($test) ? 1 : -1 ];
  test_array [0] = 0
ACEOF
    if (@decls)
    {
        $test_code = join("\n", @decls, $test_code);
    }
    $self->lang_build_program($prologue, $test_code);
}

=head2 push_includes

Adds given list of directories to preprocessor/compiler
invocation. This is not proved to allow adding directories
which might be created during the build.

=cut

sub push_includes
{
    my ($self, @includes) = @_;
    ref $self or $self = $self->_get_instance();

    push(@{$self->{extra_include_dirs}}, @includes);

    return;
}

=head2 push_preprocess_flags

Adds given flags to the parameter list for preprocessor invocation.

=cut

sub push_preprocess_flags
{
    my ($self, @cpp_flags) = @_;
    ref $self or $self = $self->_get_instance();

    push(@{$self->{extra_preprocess_flags}}, @cpp_flags);

    return;
}

=head2 push_compiler_flags

Adds given flags to the parameter list for compiler invocation.

=cut

sub push_compiler_flags
{
    my ($self, @compiler_flags) = @_;
    ref $self or $self = $self->_get_instance();
    my $lang = $self->{lang};

    if (scalar(@compiler_flags) && (ref($compiler_flags[-1]) eq "HASH"))
    {
        my $lang_opt = pop(@compiler_flags);
        defined($lang_opt->{lang}) or croak("Missing lang attribute in language options");
        $lang = $lang_opt->{lang};
        defined($self->{lang_supported}->{$lang}) or croak("Unsupported language '$lang'");
    }

    push(@{$self->{extra_compile_flags}->{$lang}}, @compiler_flags);

    return;
}

=head2 push_libraries

Adds given list of libraries to the parameter list for linker invocation.

=cut

sub push_libraries
{
    my ($self, @libs) = @_;
    ref $self or $self = $self->_get_instance();

    push(@{$self->{extra_libs}}, @libs);

    return;
}

=head2 push_library_paths

Adds given list of library paths to the parameter list for linker invocation.

=cut

sub push_library_paths
{
    my ($self, @libdirs) = @_;
    ref $self or $self = $self->_get_instance();

    push(@{$self->{extra_lib_dirs}}, @libdirs);

    return;
}

=head2 push_link_flags

Adds given flags to the parameter list for linker invocation.

=cut

sub push_link_flags
{
    my ($self, @link_flags) = @_;
    ref $self or $self = $self->_get_instance();

    push(@{$self->{extra_link_flags}}, @link_flags);

    return;
}

=head2 compile_if_else( $src, \%options? )

This function tries to compile specified code and returns a boolean value
containing check success state.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.

=cut

sub compile_if_else
{
    my ($self, $src) = @_;
    ref $self or $self = $self->_get_instance();

    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    my $builder = $self->_get_builder();

    my ($fh, $filename) = tempfile(
        "testXXXXXX",
        SUFFIX => '.c',
        UNLINK => 0
    );

    print {$fh} $src;
    close $fh;

    my ($obj_file, $outbuf, $errbuf, $exception);
    ($outbuf, $errbuf) = capture
    {
        eval {
            $obj_file = $builder->compile(
                source               => $filename,
                include_dirs         => $self->{extra_include_dirs},
                extra_compiler_flags => $self->_get_extra_compiler_flags()
            );
        };

        $exception = $@;
    };

    unlink $filename;
    $obj_file and !-f $obj_file and undef $obj_file;
    unlink $obj_file if $obj_file;

    if ($exception || !$obj_file)
    {
        $self->_add_log_lines("compile stage failed" . ($exception ? " - " . $exception : ""));
        $errbuf
          and $self->_add_log_lines($errbuf);
        $self->_add_log_lines("failing program is:\n" . $src);
        $outbuf
          and $self->_add_log_lines("stdout was :\n" . $outbuf);

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and $options->{action_on_false}->();

        return 0;
    }

    $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    1;
}

=head2 link_if_else( $src, \%options? )

This function tries to compile and link specified code and returns a boolean
value containing check success state.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.

=cut

sub link_if_else
{
    my ($self, $src) = @_;
    ref $self or $self = $self->_get_instance();
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    my $builder = $self->_get_builder();

    my ($fh, $filename) = tempfile(
        "testXXXXXX",
        SUFFIX => '.c',
        UNLINK => 0
    );

    print {$fh} $src;
    close $fh;

    my ($obj_file, $outbuf, $errbuf, $exception);
    ($outbuf, $errbuf) = capture
    {
        eval {
            $obj_file = $builder->compile(
                source               => $filename,
                include_dirs         => $self->{extra_include_dirs},
                extra_compiler_flags => $self->_get_extra_compiler_flags()
            );
        };

        $exception = $@;
    };

    $obj_file and !-f $obj_file and undef $obj_file;

    if ($exception || !$obj_file)
    {
        $self->_add_log_lines("compile stage failed" . ($exception ? " - " . $exception : ""));
        $errbuf
          and $self->_add_log_lines($errbuf);
        $self->_add_log_lines("failing program is:\n" . $src);
        $outbuf
          and $self->_add_log_lines("stdout was :\n" . $outbuf);

        unlink $filename;
        unlink $obj_file if $obj_file;

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and $options->{action_on_false}->();

        return 0;
    }

    my $exe_file;
    ($outbuf, $errbuf) = capture
    {
        eval {
            $exe_file = $builder->link_executable(
                objects            => $obj_file,
                extra_linker_flags => $self->_get_extra_linker_flags()
            );
        };

        $exception = $@;
    };

    $exe_file and !-f $exe_file and undef $exe_file;

    unlink $filename;
    unlink $obj_file if $obj_file;
    unlink $exe_file if $exe_file;

    if ($exception || !$exe_file)
    {
        $self->_add_log_lines("link stage failed" . ($exception ? " - " . $exception : ""));
        $errbuf
          and $self->_add_log_lines($errbuf);
        $self->_add_log_lines("failing program is:\n" . $src);
        $outbuf
          and $self->_add_log_lines("stdout was :\n" . $outbuf);

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and $options->{action_on_false}->();

        return 0;
    }

    $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    1;
}

=head2 check_cached( $cache-key, $check-title, \&check-call, \%options? )

Retrieves the result of a previous L</check_cached> invocation from
C<cache-key>, or (when called for the first time) populates the cache
by invoking C<\&check_call>. 

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed on B<every> call
to check_cached (not just the first cache-populating invocation), respectively.

=cut

sub check_cached
{
    my ($self, $cache_name, $message, $check_sub) = @_;
    ref $self or $self = $self->_get_instance();
    my $options = {};
    scalar @_ > 4 and ref $_[-1] eq "HASH" and $options = pop @_;

    $self->msg_checking($message);

    defined $ENV{$cache_name}
      and not defined $self->{cache}->{$cache_name}
      and $self->{cache}->{$cache_name} = $ENV{$cache_name};

    my @cached_result;
    defined($self->{cache}->{$cache_name}) and push @cached_result, "(cached)";
    defined($self->{cache}->{$cache_name}) or $self->{cache}->{$cache_name} = $check_sub->();

    $self->msg_result(@cached_result, $self->{cache}->{$cache_name});

    $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $self->{cache}->{$cache_name}
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$self->{cache}->{$cache_name}
      and $options->{action_on_false}->();

    $self->{cache}->{$cache_name};
}

=head2 cache_val

This function returns the value of a previously check_cached call.

=cut

sub cache_val
{
    my ($self, $cache_name) = @_;
    ref $self or $self = $self->_get_instance();
    defined $self->{cache}->{$cache_name} or return;
    $self->{cache}->{$cache_name};
}

=head2 check_decl( $symbol, \%options? )

This method actually tests whether symbol is defined as a macro or can be
used as an r-value, not whether it is really declared, because it is much
safer to avoid introducing extra declarations when they are not needed.
In order to facilitate use of C++ and overloaded function declarations, it
is possible to specify function argument types in parentheses for types
which can be zero-initialized:

  Config::AutoConf->check_decl("basename(char *)")

This method caches its result in the C<ac_cv_decl_E<lt>set langE<gt>>_symbol
variable.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.

=cut

sub check_decl
{
    my ($self, $symbol) = @_;
    $self = $self->_get_instance();
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    defined($symbol)   or return croak("No symbol to check for");
    ref($symbol) eq "" or return croak("No symbol to check for");
    (my $sym_plain = $symbol) =~ s/ *\(.*//;
    my $sym_call = $symbol;
    $sym_call =~ s/\(/((/;
    $sym_call =~ s/\)/) 0)/;
    $sym_call =~ s/,/) 0, (/g;

    my $cache_name = $self->_cache_name("decl", $self->{lang}, $symbol);
    my $check_sub = sub {

        my $body = <<ACEOF;
#ifndef $sym_plain
  (void) $sym_call;
#endif
ACEOF
        my $conftest = $self->lang_build_program($options->{prologue}, $body);

        my $have_decl = $self->compile_if_else(
            $conftest,
            {
                ($options->{action_on_true}  ? (action_on_true  => $options->{action_on_true})  : ()),
                ($options->{action_on_false} ? (action_on_false => $options->{action_on_false}) : ())
            }
        );

        $have_decl;
    };

    $self->check_cached(
        $cache_name,
        "whether $symbol is declared",
        $check_sub,
        {
            ($options->{action_on_cache_true}  ? (action_on_true  => $options->{action_on_cache_true})  : ()),
            ($options->{action_on_cache_false} ? (action_on_false => $options->{action_on_cache_false}) : ())
        }
    );
}

=head2 check_decls( symbols, \%options? )

For each of the symbols (with optional function argument types for C++
overloads), run L<check_decl>.

Contrary to GNU autoconf, this method does not declare HAVE_DECL_symbol
macros for the resulting C<confdefs.h>, because it differs as C<check_decl>
between compiling languages.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.
Given callbacks for I<action_on_symbol_true> or I<action_on_symbol_false> are
called for each symbol checked using L</check_decl> receiving the symbol as
first argument.

=cut

sub check_decls
{
    my ($self, $symbols) = @_;
    $self = $self->_get_instance();
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    my %pass_options;
    defined $options->{prologue}              and $pass_options{prologue}              = $options->{prologue};
    defined $options->{action_on_cache_true}  and $pass_options{action_on_cache_true}  = $options->{action_on_cache_true};
    defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false};

    my $have_syms = 1;
    foreach my $symbol (@$symbols)
    {
        $have_syms &= $self->check_decl(
            $symbol,
            {
                %pass_options,
                (
                    $options->{action_on_symbol_true} && "CODE" eq ref $options->{action_on_symbol_true}
                    ? (action_on_true => sub { $options->{action_on_symbol_true}->($symbol) })
                    : ()
                ),
                (
                    $options->{action_on_symbol_false} && "CODE" eq ref $options->{action_on_symbol_false}
                    ? (action_on_false => sub { $options->{action_on_symbol_false}->($symbol) })
                    : ()
                ),
            }
        );
    }

          $have_syms
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$have_syms
      and $options->{action_on_false}->();

    $have_syms;
}

sub _have_func_define_name
{
    my $func      = $_[0];
    my $have_name = "HAVE_" . uc($func);
    $have_name =~ tr/_A-Za-z0-9/_/c;
    $have_name;
}

=head2 check_func( $function, \%options? )

This method actually tests whether I<$funcion> can be linked into a program
trying to call I<$function>.  This method caches its result in the
ac_cv_func_FUNCTION variable.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
If any of I<action_on_cache_true>, I<action_on_cache_false> is defined,
both callbacks are passed to L</check_cached> as I<action_on_true> or
I<action_on_false> to C<check_cached>, respectively.

Returns: True if the function was found, false otherwise

=cut

sub check_func
{
    my ($self, $function) = @_;
    $self = $self->_get_instance();
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    # Build the name of the cache variable.
    my $cache_name = $self->_cache_name('func', $function);
    # Wrap the actual check in a closure so that we can use check_cached.
    my $check_sub = sub {
        my $have_func = $self->link_if_else(
            $self->lang_call(q{}, $function),
            {
                ($options->{action_on_true}  ? (action_on_true  => $options->{action_on_true})  : ()),
                ($options->{action_on_false} ? (action_on_false => $options->{action_on_false}) : ())
            }
        );
        $have_func;
    };

    # Run the check and cache the results.
    return $self->check_cached(
        $cache_name,
        "for $function",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(
                    _have_func_define_name($function),
                    $self->cache_val($cache_name),
                    "Defined when $function is available"
                );
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_have_func_define_name($function), undef, "Defined when $function is available");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

=head2 check_funcs( \@functions-list, $action-if-true?, $action-if-false? )

The same as check_func, but takes a list of functions in I<\@functions-list>
to look for and checks for each in turn. Define HAVE_FUNCTION for each
function that was found.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
If any of I<action_on_cache_true>, I<action_on_cache_false> is defined,
both callbacks are passed to L</check_cached> as I<action_on_true> or
I<action_on_false> to C<check_cached>, respectively.  Given callbacks
for I<action_on_function_true> or I<action_on_function_false> are called for
each symbol checked using L</check_func> receiving the symbol as first
argument.

=cut

sub check_funcs
{
    my ($self, $functions_ref) = @_;
    $self = $self->_get_instance();

    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    my %pass_options;
    defined $options->{action_on_cache_true}  and $pass_options{action_on_cache_true}  = $options->{action_on_cache_true};
    defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false};

    # Go through the list of functions and call check_func for each one. We
    # generate new closures for the found and not-found functions that pass in
    # the relevant function name.
    my $have_funcs = 1;
    for my $function (@{$functions_ref})
    {
        # Build the code reference to run when a function was found. This defines
        # a HAVE_FUNCTION symbol, plus runs the current $action-if-true if there is
        # one.
        $pass_options{action_on_true} = sub {
            # Run the user-provided hook, if there is one.
            defined $options->{action_on_function_true}
              and ref $options->{action_on_function_true} eq "CODE"
              and $options->{action_on_function_true}->($function);
        };

        defined $options->{action_on_function_false}
          and ref $options->{action_on_function_false} eq "CODE"
          and $pass_options{action_on_false} = sub { $options->{action_on_function_false}->($function); };

        $have_funcs &= check_func($self, $function, \%pass_options);
    }

          $have_funcs
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$have_funcs
      and $options->{action_on_false}->();

    return $have_funcs;
}

=head2 check_builtin( $builtin, \%options? )

This method actually tests whether I<$builtin> is a supported built-in
known by the compiler. Either, by giving us the type of the built-in or
by taking the value from C<__has_builtin>.  This method caches its result
in the ac_cv_builtin_FUNCTION variable.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
If any of I<action_on_cache_true>, I<action_on_cache_false> is defined,
both callbacks are passed to L</check_cached> as I<action_on_true> or
I<action_on_false> to C<check_cached>, respectively.

Returns: True if the function was found, false otherwise

=cut

sub _have_builtin_define_name
{
    my $builtin   = $_[0];
    my $have_name = "HAVE_BUILTIN_" . uc($builtin);
    $have_name =~ tr/_A-Za-z0-9/_/c;
    $have_name;
}

sub check_builtin
{
    my ($self, $builtin) = @_;
    $self = $self->_get_instance();
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    # Build the name of the cache variable.
    my $cache_name = $self->_cache_name('builtin', $builtin);
    # Wrap the actual check in a closure so that we can use check_cached.
    my $check_sub = sub {
        my $have_builtin = $self->link_if_else(
            $self->lang_builtin(q{}, $builtin),
            {
                ($options->{action_on_true}  ? (action_on_true  => $options->{action_on_true})  : ()),
                ($options->{action_on_false} ? (action_on_false => $options->{action_on_false}) : ())
            }
        );
        $have_builtin;
    };

    # Run the check and cache the results.
    return $self->check_cached(
        $cache_name,
        "for builtin $builtin",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(
                    _have_builtin_define_name($builtin),
                    $self->cache_val($cache_name),
                    "Defined when builtin $builtin is available"
                );
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_have_builtin_define_name($builtin), undef, "Defined when builtin $builtin is available");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

sub _have_type_define_name
{
    my $type      = $_[0];
    my $have_name = "HAVE_" . uc($type);
    $have_name =~ tr/*/P/;
    $have_name =~ tr/_A-Za-z0-9/_/c;
    $have_name;
}

=head2 check_type( $symbol, \%options? )

Check whether type is defined. It may be a compiler builtin type or defined
by the includes.  In C, type must be a type-name, so that the expression
C<sizeof (type)> is valid (but C<sizeof ((type))> is not).

If I<type> type is defined, preprocessor macro HAVE_I<type> (in all
capitals, with "*" replaced by "P" and spaces and dots replaced by
underscores) is defined.

This method caches its result in the C<ac_cv_type_>type variable.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.

=cut

sub check_type
{
    my ($self, $type) = @_;
    $self = $self->_get_instance();
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    defined($type)   or return croak("No type to check for");
    ref($type) eq "" or return croak("No type to check for");

    my $cache_name = $self->_cache_type_name("type", $type);
    my $check_sub = sub {

        my $body = <<ACEOF;
  if( sizeof ($type) )
    return 0;
ACEOF
        my $conftest = $self->lang_build_program($options->{prologue}, $body);

        my $have_type = $self->compile_if_else(
            $conftest,
            {
                ($options->{action_on_true}  ? (action_on_true  => $options->{action_on_true})  : ()),
                ($options->{action_on_false} ? (action_on_false => $options->{action_on_false}) : ())
            }
        );
        $have_type;
    };

    $self->check_cached(
        $cache_name,
        "for $type",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(_have_type_define_name($type), $self->cache_val($cache_name),
                    "defined when $type is available");
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_have_type_define_name($type), undef, "defined when $type is available");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

=head2 check_types( \@type-list, \%options? )

For each type in I<@type-list>, call L<check_type> is called to check
for type and return the accumulated result (accumulation op is binary and).

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.
Given callbacks for I<action_on_type_true> or I<action_on_type_false> are
called for each symbol checked using L</check_type> receiving the symbol as
first argument.

=cut

sub check_types
{
    my ($self, $types) = @_;
    $self = $self->_get_instance();
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;

    my %pass_options;
    defined $options->{prologue}              and $pass_options{prologue}              = $options->{prologue};
    defined $options->{action_on_cache_true}  and $pass_options{action_on_cache_true}  = $options->{action_on_cache_true};
    defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false};

    my $have_types = 1;
    foreach my $type (@$types)
    {
        $have_types &= $self->check_type(
            $type,
            {
                %pass_options,
                (
                    $options->{action_on_type_true} && "CODE" eq ref $options->{action_on_type_true}
                    ? (action_on_true => sub { $options->{action_on_type_true}->($type) })
                    : ()
                ),
                (
                    $options->{action_on_type_false} && "CODE" eq ref $options->{action_on_type_false}
                    ? (action_on_false => sub { $options->{action_on_type_false}->($type) })
                    : ()
                ),
            }
        );
    }

          $have_types
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$have_types
      and $options->{action_on_false}->();

    $have_types;
}

sub _compute_int_compile
{
    my ($self, $expr, $prologue, @decls) = @_;
    $self = $self->_get_instance();

    my ($body, $conftest, $compile_result);

    my ($low, $mid, $high) = (0, 0, 0);
    if ($self->compile_if_else($self->lang_build_bool_test($prologue, "((long int)($expr)) >= 0", @decls)))
    {
        $low = $mid = 0;
        while (1)
        {
            if ($self->compile_if_else($self->lang_build_bool_test($prologue, "((long int)($expr)) <= $mid", @decls)))
            {
                $high = $mid;
                last;
            }
            $low = $mid + 1;
            # avoid overflow
            if ($low <= $mid)
            {
                $low = 0;
                last;
            }
            $mid = $low * 2;
        }
    }
    elsif ($self->compile_if_else($self->lang_build_bool_test($prologue, "((long int)($expr)) < 0", @decls)))
    {
        $high = $mid = -1;
        while (1)
        {
            if ($self->compile_if_else($self->lang_build_bool_test($prologue, "((long int)($expr)) >= $mid", @decls)))
            {
                $low = $mid;
                last;
            }
            $high = $mid - 1;
            # avoid overflow
            if ($mid < $high)
            {
                $high = 0;
                last;
            }
            $mid = $high * 2;
        }
    }

    # perform binary search between $low and $high
    while ($low <= $high)
    {
        $mid = int(($high - $low) / 2 + $low);
        if ($self->compile_if_else($self->lang_build_bool_test($prologue, "((long int)($expr)) < $mid", @decls)))
        {
            $high = $mid - 1;
        }
        elsif ($self->compile_if_else($self->lang_build_bool_test($prologue, "((long int)($expr)) > $mid", @decls)))
        {
            $low = $mid + 1;
        }
        else
        {
            return $mid;
        }
    }

    return;
}

=head2 compute_int( $expression, @decls?, \%options )

Returns the value of the integer I<expression>. The value should fit in an
initializer in a C variable of type signed long.  It should be possible
to evaluate the expression at compile-time. If no includes are specified,
the default includes are used.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.

=cut

sub _expr_value_define_name
{
    my $expr      = $_[0];
    my $have_name = "EXPR_" . uc($expr);
    $have_name =~ tr/*/P/;
    $have_name =~ tr/_A-Za-z0-9/_/c;
    $have_name;
}

sub compute_int
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $expr, @decls) = @_;
    $self = $self->_get_instance();

    my $cache_name = $self->_cache_type_name("compute_int", $self->{lang}, $expr);
    my $check_sub = sub {
        my $val = $self->_compute_int_compile($expr, $options->{prologue}, @decls);

        defined $val
          and $options->{action_on_true}
          and ref $options->{action_on_true} eq "CODE"
          and $options->{action_on_true}->();

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and !defined $val
          and $options->{action_on_false}->();

        $val;
    };

    $self->check_cached(
        $cache_name,
        "for compute result of ($expr)",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(
                    _expr_value_define_name($expr),
                    $self->cache_val($cache_name),
                    "defined when ($expr) could computed"
                );
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_expr_value_define_name($expr), undef, "defined when ($expr) could computed");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

=head2 check_sizeof_type( $type, \%options? )

Checks for the size of the specified type by compiling and define
C<SIZEOF_type> using the determined size.

In opposition to GNU AutoConf, this method can determine size of structure
members, eg.

  $ac->check_sizeof_type( "SV.sv_refcnt", { prologue => $include_perl } );
  # or
  $ac->check_sizeof_type( "struct utmpx.ut_id", { prologue => "#include <utmpx.h>" } );

This method caches its result in the C<ac_cv_sizeof_E<lt>set langE<gt>>_type variable.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.

=cut

sub _sizeof_type_define_name
{
    my $type      = $_[0];
    my $have_name = "SIZEOF_" . uc($type);
    $have_name =~ tr/*/P/;
    $have_name =~ tr/_A-Za-z0-9/_/c;
    $have_name;
}

sub check_sizeof_type
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $type) = @_;
    $self = $self->_get_instance();
    defined($type)   or return croak("No type to check for");
    ref($type) eq "" or return croak("No type to check for");

    my $cache_name = $self->_cache_type_name("sizeof", $self->{lang}, $type);
    my $check_sub = sub {
        my @decls;
        if ($type =~ m/^([^.]+)\.([^.]+)$/)
        {
            my $struct = $1;
            $type = "_ac_test_aggr.$2";
            my $decl = "static $struct _ac_test_aggr;";
            push(@decls, $decl);
        }

        my $typesize = $self->_compute_int_compile("sizeof($type)", $options->{prologue}, @decls);

              $typesize
          and $options->{action_on_true}
          and ref $options->{action_on_true} eq "CODE"
          and $options->{action_on_true}->();

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and !$typesize
          and $options->{action_on_false}->();

        $typesize;
    };

    $self->check_cached(
        $cache_name,
        "for size of $type",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(
                    _sizeof_type_define_name($type),
                    $self->cache_val($cache_name),
                    "defined when sizeof($type) is available"
                );
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_sizeof_type_define_name($type), undef, "defined when sizeof($type) is available");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

=head2 check_sizeof_types( type, \%options? )

For each type L<check_sizeof_type> is called to check for size of type.

If I<action-if-found> is given, it is additionally executed when all of the
sizes of the types could determined. If I<action-if-not-found> is given, it
is executed when one size of the types could not determined.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.
Given callbacks for I<action_on_size_true> or I<action_on_size_false> are
called for each symbol checked using L</check_sizeof_type> receiving the
symbol as first argument.

=cut

sub check_sizeof_types
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $types) = @_;
    $self = $self->_get_instance();

    my %pass_options;
    defined $options->{prologue}              and $pass_options{prologue}              = $options->{prologue};
    defined $options->{action_on_cache_true}  and $pass_options{action_on_cache_true}  = $options->{action_on_cache_true};
    defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false};

    my $have_sizes = 1;
    foreach my $type (@$types)
    {
        $have_sizes &= !!(
            $self->check_sizeof_type(
                $type,
                {
                    %pass_options,
                    (
                        $options->{action_on_size_true} && "CODE" eq ref $options->{action_on_size_true}
                        ? (action_on_true => sub { $options->{action_on_size_true}->($type) })
                        : ()
                    ),
                    (
                        $options->{action_on_size_false} && "CODE" eq ref $options->{action_on_size_false}
                        ? (action_on_false => sub { $options->{action_on_size_false}->($type) })
                        : ()
                    ),
                }
            )
        );
    }

          $have_sizes
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$have_sizes
      and $options->{action_on_false}->();

    $have_sizes;
}

sub _alignof_type_define_name
{
    my $type      = $_[0];
    my $have_name = "ALIGNOF_" . uc($type);
    $have_name =~ tr/*/P/;
    $have_name =~ tr/_A-Za-z0-9/_/c;
    $have_name;
}

=head2 check_alignof_type( type, \%options? )

Define ALIGNOF_type to be the alignment in bytes of type. I<type> must
be valid as a structure member declaration or I<type> must be a structure
member itself.

This method caches its result in the C<ac_cv_alignof_E<lt>set langE<gt>>_type
variable, with I<*> mapped to C<p> and other characters not suitable for a
variable name mapped to underscores.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.

=cut

sub check_alignof_type
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $type) = @_;
    $self = $self->_get_instance();
    defined($type)   or return croak("No type to check for");
    ref($type) eq "" or return croak("No type to check for");

    my $cache_name = $self->_cache_type_name("alignof", $self->{lang}, $type);
    my $check_sub = sub {
        my @decls = (
            "#ifndef offsetof",
            "# ifdef __ICC",
            "#  define offsetof(type,memb) ((size_t)(((char *)(&((type*)0)->memb)) - ((char *)0)))",
            "# else", "#  define offsetof(type,memb) ((size_t)&((type*)0)->memb)",
            "# endif", "#endif"
        );

        my ($struct, $memb);
        if ($type =~ m/^([^.]+)\.([^.]+)$/)
        {
            $struct = $1;
            $memb   = $2;
        }
        else
        {
            push(@decls, "typedef struct { char x; $type y; } ac__type_alignof_;");
            $struct = "ac__type_alignof_";
            $memb   = "y";
        }

        my $typealign = $self->_compute_int_compile("offsetof($struct, $memb)", $options->{prologue}, @decls);

              $typealign
          and $options->{action_on_true}
          and ref $options->{action_on_true} eq "CODE"
          and $options->{action_on_true}->();

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and !$typealign
          and $options->{action_on_false}->();

        $typealign;
    };

    $self->check_cached(
        $cache_name,
        "for align of $type",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(
                    _alignof_type_define_name($type),
                    $self->cache_val($cache_name),
                    "defined when alignof($type) is available"
                );
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_alignof_type_define_name($type), undef, "defined when alignof($type) is available");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

=head2 check_alignof_types (type, [action-if-found], [action-if-not-found], [prologue = default includes])

For each type L<check_alignof_type> is called to check for align of type.

If I<action-if-found> is given, it is additionally executed when all of the
aligns of the types could determined. If I<action-if-not-found> is given, it
is executed when one align of the types could not determined.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.
Given callbacks for I<action_on_align_true> or I<action_on_align_false> are
called for each symbol checked using L</check_alignof_type> receiving the
symbol as first argument.

=cut

sub check_alignof_types
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $types) = @_;
    $self = $self->_get_instance();

    my %pass_options;
    defined $options->{prologue}              and $pass_options{prologue}              = $options->{prologue};
    defined $options->{action_on_cache_true}  and $pass_options{action_on_cache_true}  = $options->{action_on_cache_true};
    defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false};

    my $have_aligns = 1;
    foreach my $type (@$types)
    {
        $have_aligns &= !!(
            $self->check_alignof_type(
                $type,
                {
                    %pass_options,
                    (
                        $options->{action_on_align_true} && "CODE" eq ref $options->{action_on_align_true}
                        ? (action_on_true => sub { $options->{action_on_align_true}->($type) })
                        : ()
                    ),
                    (
                        $options->{action_on_align_false} && "CODE" eq ref $options->{action_on_align_false}
                        ? (action_on_false => sub { $options->{action_on_align_false}->($type) })
                        : ()
                    ),
                }
            )
        );
    }

          $have_aligns
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$have_aligns
      and $options->{action_on_false}->();

    $have_aligns;
}

sub _have_member_define_name
{
    my $member    = $_[0];
    my $have_name = "HAVE_" . uc($member);
    $have_name =~ tr/_A-Za-z0-9/_/c;
    $have_name;
}

=head2 check_member( member, \%options? )

Check whether I<member> is in form of I<aggregate>.I<member> and
I<member> is a member of the I<aggregate> aggregate.

which are used prior to the aggregate under test.

  Config::AutoConf->check_member(
    "struct STRUCT_SV.sv_refcnt",
    {
      action_on_false => sub { Config::AutoConf->msg_failure( "sv_refcnt member required for struct STRUCT_SV" ); },
      prologue => "#include <EXTERN.h>\n#include <perl.h>"
    }
  );

This function will return a true value (1) if the member is found.

If I<aggregate> aggregate has I<member> member, preprocessor
macro HAVE_I<aggregate>_I<MEMBER> (in all capitals, with spaces
and dots replaced by underscores) is defined.

This macro caches its result in the C<ac_cv_>aggr_member variable.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.

=cut

sub check_member
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $member) = @_;
    $self = $self->_get_instance();
    defined($member)   or return croak("No type to check for");
    ref($member) eq "" or return croak("No type to check for");

    $member =~ m/^([^.]+)\.([^.]+)$/ or return croak("check_member(\"struct foo.member\", \%options)");
    my $type = $1;
    $member = $2;

    my $cache_name = $self->_cache_type_name("$type.$member");
    my $check_sub  = sub {

        my $body = <<ACEOF;
  static $type check_aggr;
  if( check_aggr.$member )
    return 0;
ACEOF
        my $conftest = $self->lang_build_program($options->{prologue}, $body);
        my $have_member = $self->compile_if_else($conftest);

        unless ($have_member)
        {
            $body = <<ACEOF;
  static $type check_aggr;
  if( sizeof check_aggr.$member )
    return 0;
ACEOF
            $conftest = $self->lang_build_program($options->{prologue}, $body);
            $have_member = $self->compile_if_else($conftest);
        }

              $have_member
          and $options->{action_on_true}
          and ref $options->{action_on_true} eq "CODE"
          and $options->{action_on_true}->();

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and $options->{action_on_false}->()
          unless $have_member;

        $have_member;
    };

    $self->check_cached(
        $cache_name,
        "for $type.$member",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(
                    _have_member_define_name("$type.$member"),
                    $self->cache_val($cache_name),
                    "defined when $type.$member is available"
                );
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_have_member_define_name("$type.$member"), undef, "defined when $type.$member is available");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

=head2 check_members( members, \%options? )

For each member L<check_member> is called to check for member of aggregate.

This function will return a true value (1) if at least one member is found.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be favoured
over C<default includes> (represented by L</_default_includes>). If any of
I<action_on_cache_true>, I<action_on_cache_false> is defined, both callbacks
are passed to L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.
Given callbacks for I<action_on_member_true> or I<action_on_member_false> are
called for each symbol checked using L</check_member> receiving the symbol as
first argument.

=cut

sub check_members
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $members) = @_;
    $self = $self->_get_instance();

    my %pass_options;
    defined $options->{prologue}              and $pass_options{prologue}              = $options->{prologue};
    defined $options->{action_on_cache_true}  and $pass_options{action_on_cache_true}  = $options->{action_on_cache_true};
    defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false};

    my $have_members = 0;
    foreach my $member (@$members)
    {
        $have_members |= (
            $self->check_member(
                $member,
                {
                    %pass_options,
                    (
                        $options->{action_on_member_true} && "CODE" eq ref $options->{action_on_member_true}
                        ? (action_on_true => sub { $options->{action_on_member_true}->($member) })
                        : ()
                    ),
                    (
                        $options->{action_on_member_false} && "CODE" eq ref $options->{action_on_member_false}
                        ? (action_on_false => sub { $options->{action_on_member_false}->($member) })
                        : ()
                    ),
                }
            )
        );
    }

          $have_members
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$have_members
      and $options->{action_on_false}->();

    $have_members;
}

sub _have_header_define_name
{
    my $header    = $_[0];
    my $have_name = "HAVE_" . uc($header);
    $have_name =~ tr/_A-Za-z0-9/_/c;
    return $have_name;
}

sub _check_header
{
    my $options = {};
    scalar @_ > 4 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $header, $prologue, $body) = @_;

    $prologue .= <<"_ACEOF";
    #include <$header>
_ACEOF
    my $conftest = $self->lang_build_program($prologue, $body);

    $self->compile_if_else($conftest, $options);
}

=head2 check_header( $header, \%options? )

This function is used to check if a specific header file is present in
the system: if we detect it and if we can compile anything with that
header included. Note that normally you want to check for a header
first, and then check for the corresponding library (not all at once).

The standard usage for this module is:

  Config::AutoConf->check_header("ncurses.h");
  
This function will return a true value (1) on success, and a false value
if the header is not present or not available for common usage.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
When a I<prologue> exists in the optional hash at end, it will be prepended
to the tested header. If any of I<action_on_cache_true>,
I<action_on_cache_false> is defined, both callbacks are passed to
L</check_cached> as I<action_on_true> or I<action_on_false> to
C<check_cached>, respectively.

=cut

sub check_header
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $header) = @_;
    $self = $self->_get_instance();
    defined($header)   or return croak("No type to check for");
    ref($header) eq "" or return croak("No type to check for");

    return 0 unless $header;
    my $cache_name = $self->_cache_name($header);
    my $check_sub  = sub {
        my $prologue = defined $options->{prologue} ? $options->{prologue} : "";

        my $have_header = $self->_check_header(
            $header,
            $prologue,
            "",
            {
                ($options->{action_on_true}  ? (action_on_true  => $options->{action_on_true})  : ()),
                ($options->{action_on_false} ? (action_on_false => $options->{action_on_false}) : ())
            }
        );

        $have_header;
    };

    $self->check_cached(
        $cache_name,
        "for $header",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(
                    _have_header_define_name($header),
                    $self->cache_val($cache_name),
                    "defined when $header is available"
                );
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_have_header_define_name($header), undef, "defined when $header is available");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

=head2 check_headers

This function uses check_header to check if a set of include files exist
in the system and can be included and compiled by the available compiler.
Returns the name of the first header file found.

Passes an optional \%options hash to each L</check_header> call.

=cut

sub check_headers
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance();
    $self->check_header($_, $options) and return $_ for (@_);
    return;
}

=head2 check_all_headers

This function checks each given header for usability and returns true
when each header can be used -- otherwise false.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
Each of existing key/value pairs using I<prologue>, I<action_on_cache_true>
or I<action_on_cache_false> as key are passed-through to each call of
L</check_header>.
Given callbacks for I<action_on_header_true> or I<action_on_header_false> are
called for each symbol checked using L</check_header> receiving the symbol as
first argument.

=cut

sub check_all_headers
{
    my $options = {};
    scalar @_ > 2 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance();
    @_ or return;

    my %pass_options;
    defined $options->{prologue}              and $pass_options{prologue}              = $options->{prologue};
    defined $options->{action_on_cache_true}  and $pass_options{action_on_cache_true}  = $options->{action_on_cache_true};
    defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false};

    my $all_headers = 1;
    foreach my $header (@_)
    {
        $all_headers &= $self->check_header(
            $header,
            {
                %pass_options,
                (
                    $options->{action_on_header_true} && "CODE" eq ref $options->{action_on_header_true}
                    ? (action_on_true => sub { $options->{action_on_header_true}->($header) })
                    : ()
                ),
                (
                    $options->{action_on_header_false} && "CODE" eq ref $options->{action_on_header_false}
                    ? (action_on_false => sub { $options->{action_on_header_false}->($header) })
                    : ()
                ),
            }
        );
    }

          $all_headers
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$all_headers
      and $options->{action_on_false}->();

    $all_headers;
}

=head2 check_stdc_headers

Checks for standard C89 headers, namely stdlib.h, stdarg.h, string.h and float.h.
If those are found, additional all remaining C89 headers are checked: assert.h,
ctype.h, errno.h, limits.h, locale.h, math.h, setjmp.h, signal.h, stddef.h,
stdio.h and time.h.

Returns a false value if it fails.

Passes an optional \%options hash to each L</check_all_headers> call.

=cut

my @ansi_c_headers = qw(stdlib stdarg string float assert ctype errno limits locale math setjmp signal stddef stdio time);

sub check_stdc_headers
{
    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance();

    # XXX for C++ the map should look like "c${_}" ...
    my @c_ansi_c_headers = map { "${_}.h" } @ansi_c_headers;
    my $rc = $self->check_all_headers(@c_ansi_c_headers, $options);
    $rc and $self->define_var("STDC_HEADERS", 1, "Define to 1 if you have the ANSI C header files.");
    $rc;
}

=head2 check_default_headers

This function checks for some default headers, the std c89 headers and
sys/types.h, sys/stat.h, memory.h, strings.h, inttypes.h, stdint.h and unistd.h

Passes an optional \%options hash to each L</check_all_headers> call.

=cut

sub check_default_headers
{
    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance();
    $self->check_stdc_headers($options)
      and $self->check_all_headers(qw(sys/types.h sys/stat.h memory.h strings.h inttypes.h stdint.h unistd.h), $options);
}

=head2 check_dirent_header

Check for the following header files. For the first one that is found and
defines 'DIR', define the listed C preprocessor macro:

  dirent.h      HAVE_DIRENT_H
  sys/ndir.h    HAVE_SYS_NDIR_H
  sys/dir.h     HAVE_SYS_DIR_H
  ndir.h        HAVE_NDIR_H

The directory-library declarations in your source code should look
something like the following:

  #include <sys/types.h>
  #ifdef HAVE_DIRENT_H
  # include <dirent.h>
  # define NAMLEN(dirent) strlen ((dirent)->d_name)
  #else
  # define dirent direct
  # define NAMLEN(dirent) ((dirent)->d_namlen)
  # ifdef HAVE_SYS_NDIR_H
  #  include <sys/ndir.h>
  # endif
  # ifdef HAVE_SYS_DIR_H
  #  include <sys/dir.h>
  # endif
  # ifdef HAVE_NDIR_H
  #  include <ndir.h>
  # endif
  #endif

Using the above declarations, the program would declare variables to be of
type C<struct dirent>, not C<struct direct>, and would access the length
of a directory entry name by passing a pointer to a C<struct dirent> to
the C<NAMLEN> macro.

For the found header, the macro HAVE_DIRENT_IN_${header} is defined.

This method might be obsolescent, as all current systems with directory
libraries have C<< E<lt>dirent.hE<gt> >>. Programs supporting only newer OS
might not need to use this method.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
Each of existing key/value pairs using I<prologue>, I<action_on_header_true>
(as I<action_on_true> having the name of the tested header as first argument)
or I<action_on_header_false> (as I<action_on_false> having the name of the
tested header as first argument) as key are passed-through to each call of
L</_check_header>.
Given callbacks for I<action_on_cache_true> or I<action_on_cache_false> are
passed to the call of L</check_cached>.

=cut

sub _have_dirent_header_define_name
{
    my $header    = $_[0];
    my $have_name = "HAVE_DIRENT_IN_" . uc($header);
    $have_name =~ tr/_A-Za-z0-9/_/c;
    return $have_name;
}

sub check_dirent_header
{
    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance();

    my %pass_options;
    defined $options->{prologue} and $pass_options{prologue} = $options->{prologue};

    my $have_dirent;
    foreach my $header (qw(dirent.h sys/ndir.h sys/dir.h ndir.h))
    {
        if ($self->check_header($header))
        {
            my $cache_name = $self->_cache_name("dirent", $header);
            my $check_sub = sub {
                my $have_dirent;
                $have_dirent = $self->_check_header(
                    $header,
                    "#include <sys/types.h>\n",
                    "if ((DIR *) 0) { return 0; }",
                    {
                        %pass_options,
                        (
                            $options->{action_on_header_true} && "CODE" eq ref $options->{action_on_header_true}
                            ? (action_on_true => sub { $options->{action_on_header_true}->($header) })
                            : ()
                        ),
                        (
                            $options->{action_on_header_false} && "CODE" eq ref $options->{action_on_header_false}
                            ? (action_on_false => sub { $options->{action_on_header_false}->($header) })
                            : ()
                        ),
                    }
                );
            };

            $have_dirent = $self->check_cached(
                $cache_name,
                "for header defining DIR *",
                $check_sub,
                {
                    action_on_true => sub {
                        $self->define_var(
                            _have_dirent_header_define_name($header),
                            $self->cache_val($cache_name),
                            "defined when $header is available"
                        );
                        $options->{action_on_cache_true}
                          and ref $options->{action_on_cache_true} eq "CODE"
                          and $options->{action_on_cache_true}->();
                    },
                    action_on_false => sub {
                        $self->define_var(_have_dirent_header_define_name($header), undef, "defined when $header is available");
                        $options->{action_on_cache_false}
                          and ref $options->{action_on_cache_false} eq "CODE"
                          and $options->{action_on_cache_false}->();
                    },
                }
            );

            $have_dirent and $have_dirent = $header and last;
        }
    }

          $have_dirent
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and !$have_dirent
      and $options->{action_on_false}->();

    $have_dirent;
}

=head2 _check_perlapi_program

This method provides the program source which is suitable to do basic
compile/link tests to prove perl development environment.

=cut

sub _check_perlapi_program
{
    my $self = shift;

    my $includes        = $self->_default_includes_with_perl();
    my $perl_check_body = <<'EOB';
  I32 rc;
  SV *foo = newSVpv("Perl rocks", 11);
  rc = SvCUR(foo);
EOB
    $self->lang_build_program($includes, $perl_check_body);
}

=head2 _check_compile_perlapi

This method can be used from other checks to prove whether we have a perl
development environment or not (perl.h, reasonable basic checks - types, etc.)

=cut

sub _check_compile_perlapi
{
    my $self = shift;

    my $conftest = $self->_check_perlapi_program();
    $self->compile_if_else($conftest);
}

=head2 check_compile_perlapi

This method can be used from other checks to prove whether we have a perl
development environment or not (perl.h, reasonable basic checks - types, etc.)

=cut

sub check_compile_perlapi
{
    my $self       = shift->_get_instance;
    my $cache_name = $self->_cache_name(qw(compile perlapi));

    $self->check_cached($cache_name, "whether perlapi is accessible", sub { $self->_check_compile_perlapi });
}

=head2 check_compile_perlapi_or_die

Dies when not being able to compile using the Perl API

=cut

sub check_compile_perlapi_or_die
{
    my $self = shift;
    $self->check_compile_perlapi(@_) or $self->msg_error("Cannot use Perl API - giving up");
}

=head2 check_linkable_xs_so

Checks whether a dynamic loadable object containing an XS module can be
linked or not. Due the nature of the beast, this test currently always
succeed.

=cut

sub check_linkable_xs_so { 1 }

=head2 check_linkable_xs_so_or_die

Dies when L</check_linkable_xs_so> fails.

=cut

sub check_linkable_xs_so_or_die
{
    my $self = shift;
    $self->check_linkable_xs_so(@_) or $self->msg_error("Cannot link XS dynamic loadable - giving up");
}

=head2 check_loadable_xs_so

Checks whether a dynamic loadable object containing an XS module can be
loaded or not. Due the nature of the beast, this test currently always
succeed.

=cut

sub check_loadable_xs_so { 1 }

=head2 check_loadable_xs_so_or_die

Dies when L</check_loadable_xs_so> fails.

=cut

sub check_loadable_xs_so_or_die
{
    my $self = shift;
    $self->check_loadable_xs_so(@_) or $self->msg_error("Cannot load XS dynamic loadable - giving up");
}

=head2 _check_link_perlapi

This method can be used from other checks to prove whether we have a perl
development environment including a suitable libperl or not (perl.h,
reasonable basic checks - types, etc.)

Caller must ensure that the linker flags are set appropriate (C<-lperl>
or similar).

=cut

sub _check_link_perlapi
{
    my $self = shift;

    my $conftest              = $self->_check_perlapi_program();
    my @save_libs             = @{$self->{extra_libs}};
    my @save_extra_link_flags = @{$self->{extra_link_flags}};

    my $libperl = $Config{libperl};
    $libperl =~ s/^lib//;
    $libperl =~ s/\.[^\.]*$//;

    push @{$self->{extra_link_flags}}, "-L" . File::Spec->catdir($Config{installarchlib}, "CORE");
    push @{$self->{extra_libs}}, "$libperl";
    if ($Config{perllibs})
    {
        foreach my $perllib (split(" ", $Config{perllibs}))
        {
            $perllib =~ m/^\-l(\w+)$/ and push @{$self->{extra_libs}}, "$1" and next;
            push @{$self->{extra_link_flags}}, $perllib;
        }
    }

    my $have_libperl = $self->link_if_else($conftest);

    $have_libperl or $self->{extra_libs}       = [@save_libs];
    $have_libperl or $self->{extra_link_flags} = [@save_extra_link_flags];

    $have_libperl;
}

=head2 check_link_perlapi

This method can be used from other checks to prove whether we have a perl
development environment or not (perl.h, libperl.la, reasonable basic
checks - types, etc.)

=cut

sub check_link_perlapi
{
    my $self       = shift->_get_instance;
    my $cache_name = $self->_cache_name(qw(link perlapi));

    $self->check_cached($cache_name, "whether perlapi is linkable", sub { $self->_check_link_perlapi });
}

sub _have_lib_define_name
{
    my $lib       = $_[0];
    my $have_name = "HAVE_LIB" . uc($lib);
    $have_name =~ tr/_A-Za-z0-9/_/c;
    return $have_name;
}

=head2 check_lib( lib, func, @other-libs?, \%options? )

This function is used to check if a specific library includes some
function. Call it with the library name (without the lib portion), and
the name of the function you want to test:

  Config::AutoConf->check_lib("z", "gzopen");

It returns 1 if the function exist, 0 otherwise.

In case of function found, the HAVE_LIBlibrary (all in capitals)
preprocessor macro is defined with 1 and $lib together with @other_libs
are added to the list of libraries to link with.

If linking with library results in unresolved symbols that would be
resolved by linking with additional libraries, give those libraries
as the I<other-libs> argument: e.g., C<[qw(Xt X11)]>.
Otherwise, this routine may fail to detect that library is present,
because linking the test program can fail with unresolved symbols.
The other-libraries argument should be limited to cases where it is
desirable to test for one library in the presence of another that
is not already in LIBS. 

This method caches its result in the C<ac_cv_lib_>lib_func variable.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
If any of I<action_on_cache_true>, I<action_on_cache_false> is defined,
both callbacks are passed to L</check_cached> as I<action_on_true> or
I<action_on_false> to C<check_cached>, respectively.

It's recommended to use L<search_libs> instead of check_lib these days.

=cut

sub check_lib
{
    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance();
    my ($lib, $func, @other_libs) = @_;

    return 0 unless $lib and $func;

    scalar(@other_libs) == 1
      and ref($other_libs[0]) eq "ARRAY"
      and @other_libs = @{$other_libs[0]};

    my $cache_name = $self->_cache_name("lib", $lib, $func);
    my $check_sub = sub {
        my $conftest = $self->lang_call("", $func);

        my @save_libs = @{$self->{extra_libs}};
        push(@{$self->{extra_libs}}, $lib, @other_libs);
        my $have_lib = $self->link_if_else(
            $conftest,
            {
                ($options->{action_on_true}  ? (action_on_true  => $options->{action_on_true})  : ()),
                ($options->{action_on_false} ? (action_on_false => $options->{action_on_false}) : ())
            }
        );
        $self->{extra_libs} = [@save_libs];

        $have_lib;
    };

    $self->check_cached(
        $cache_name,
        "for $func in -l$lib",
        $check_sub,
        {
            action_on_true => sub {
                $self->define_var(
                    _have_lib_define_name($lib),
                    $self->cache_val($cache_name),
                    "defined when library $lib is available"
                );
                push(@{$self->{extra_libs}}, $lib, @other_libs);
                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            action_on_false => sub {
                $self->define_var(_have_lib_define_name($lib), undef, "defined when library $lib is available");
                $options->{action_on_cache_false}
                  and ref $options->{action_on_cache_false} eq "CODE"
                  and $options->{action_on_cache_false}->();
            },
        }
    );
}

=head2 search_libs( function, search-libs, @other-libs?, \%options? )

Search for a library defining function if it's not already available.
This equates to calling

    Config::AutoConf->link_if_else(
        Config::AutoConf->lang_call( "", "$function" ) );

first with no libraries, then for each library listed in search-libs.
I<search-libs> must be specified as an array reference to avoid
confusion in argument order.

Prepend -llibrary to LIBS for the first library found to contain function.

If linking with library results in unresolved symbols that would be
resolved by linking with additional libraries, give those libraries as
the I<other-libraries> argument: e.g., C<[qw(Xt X11)]>. Otherwise, this
method fails to detect that function is present, because linking the
test program always fails with unresolved symbols.

The result of this test is cached in the ac_cv_search_function variable
as "none required" if function is already available, as C<0> if no
library containing function was found, otherwise as the -llibrary option
that needs to be prepended to LIBS.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
If any of I<action_on_cache_true>, I<action_on_cache_false> is defined,
both callbacks are passed to L</check_cached> as I<action_on_true> or
I<action_on_false> to C<check_cached>, respectively.  Given callbacks
for I<action_on_lib_true> or I<action_on_lib_false> are called for
each library checked using L</link_if_else> receiving the library as
first argument and all C<@other_libs> subsequently.

=cut

sub search_libs
{
    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance();
    my ($func, $libs, @other_libs) = @_;

    (defined($libs) and "ARRAY" eq ref($libs) and scalar(@{$libs}) > 0)
      or return 0;    # XXX would prefer croak
    return 0 unless $func;

    scalar(@other_libs) == 1
      and ref($other_libs[0]) eq "ARRAY"
      and @other_libs = @{$other_libs[0]};

    my $cache_name = $self->_cache_name("search", $func);
    my $check_sub = sub {
        my $conftest = $self->lang_call("", $func);

        my @save_libs = @{$self->{extra_libs}};
        my $have_lib  = 0;
        foreach my $libstest (undef, @$libs)
        {
            # XXX would local work on array refs? can we omit @save_libs?
            $self->{extra_libs} = [@save_libs];
            defined($libstest) and unshift(@{$self->{extra_libs}}, $libstest, @other_libs);
            $self->link_if_else(
                $conftest,
                {
                    (
                        $options->{action_on_lib_true} && "CODE" eq ref $options->{action_on_lib_true}
                        ? (action_on_true => sub { $options->{action_on_lib_true}->($libstest, @other_libs, @_) })
                        : ()
                    ),
                    (
                        $options->{action_on_lib_false} && "CODE" eq ref $options->{action_on_lib_false}
                        ? (action_on_false => sub { $options->{action_on_lib_false}->($libstest, @other_libs, @_) })
                        : ()
                    ),
                }
              )
              and ($have_lib = defined($libstest) ? $libstest : "none required")
              and last;
        }
        $self->{extra_libs} = [@save_libs];

              $have_lib
          and $options->{action_on_true}
          and ref $options->{action_on_true} eq "CODE"
          and $options->{action_on_true}->();

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and !$have_lib
          and $options->{action_on_false}->();

        $have_lib;
    };

    return $self->check_cached(
        $cache_name,
        "for library containing $func",
        $check_sub,
        {
            action_on_true => sub {
                $self->cache_val($cache_name) eq "none required"
                  or unshift(@{$self->{extra_libs}}, $self->cache_val($cache_name));

                $options->{action_on_cache_true}
                  and ref $options->{action_on_cache_true} eq "CODE"
                  and $options->{action_on_cache_true}->();
            },
            ($options->{action_on_cache_false} ? (action_on_false => $options->{action_on_cache_false}) : ())
        }
    );
}

sub _check_lm_funcs { qw(log2 pow log10 log exp sqrt) }

=head2 check_lm( \%options? )

This method is used to check if some common C<math.h> functions are
available, and if C<-lm> is needed. Returns the empty string if no
library is needed, or the "-lm" string if libm is needed.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
Each of existing key/value pairs using I<action_on_func_true> (as
I<action_on_true> having the name of the tested functions as first argument),
I<action_on_func_false> (as I<action_on_false> having the name of the tested
functions as first argument), I<action_on_func_lib_true> (as
I<action_on_lib_true> having the name of the tested functions as first
argument), I<action_on_func_lib_false> (as I<action_on_lib_false> having
the name of the tested functions as first argument) as key are passed-
through to each call of L</search_libs>.
Given callbacks for I<action_on_lib_true>, I<action_on_lib_false>,
I<action_on_cache_true> or I<action_on_cache_false> are passed to the
call of L</search_libs>.

B<Note> that I<action_on_lib_true> and I<action_on_func_lib_true> or
I<action_on_lib_false> and I<action_on_func_lib_false> cannot be used
at the same time, respectively.

=cut

sub check_lm
{
    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance();

    defined $options->{action_on_lib_true}
      and defined $options->{action_on_func_lib_true}
      and croak("action_on_lib_true and action_on_func_lib_true cannot be used together");
    defined $options->{action_on_lib_false}
      and defined $options->{action_on_func_lib_false}
      and croak("action_on_lib_false and action_on_func_lib_false cannot be used together");

    my %pass_options;
    defined $options->{action_on_cache_true}  and $pass_options{action_on_cache_true}  = $options->{action_on_cache_true};
    defined $options->{action_on_cache_false} and $pass_options{action_on_cache_false} = $options->{action_on_cache_false};
    defined $options->{action_on_lib_true}    and $pass_options{action_on_lib_true}    = $options->{action_on_lib_true};
    defined $options->{action_on_lib_false}   and $pass_options{action_on_lib_false}   = $options->{action_on_lib_false};

    my $fail       = 0;
    my $required   = "";
    my @math_funcs = $self->_check_lm_funcs;
    for my $func (@math_funcs)
    {
        my $ans = $self->search_libs(
            $func,
            ['m'],
            {
                %pass_options,
                (
                    $options->{action_on_func_true} && "CODE" eq ref $options->{action_on_func_true}
                    ? (action_on_true => sub { $options->{action_on_func_true}->($func, @_) })
                    : ()
                ),
                (
                    $options->{action_on_func_false} && "CODE" eq ref $options->{action_on_func_false}
                    ? (action_on_false => sub { $options->{action_on_func_false}->($func, @_) })
                    : ()
                ),
                (
                    $options->{action_on_func_lib_true} && "CODE" eq ref $options->{action_on_func_lib_true}
                    ? (action_on_lib_true => sub { $options->{action_on_func_lib_true}->($func, @_) })
                    : ()
                ),
                (
                    $options->{action_on_func_lib_false} && "CODE" eq ref $options->{action_on_func_lib_false}
                    ? (action_on_lib_false => sub { $options->{action_on_func_lib_false}->($func, @_) })
                    : ()
                ),
            },
        );

        $ans or $fail = 1;
        $ans ne "none required" and $required = $ans;
    }

         !$fail
      and $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

          $fail
      and $options->{action_on_false}
      and ref $options->{action_on_false} eq "CODE"
      and $options->{action_on_false}->();

    $required;
}

=head2 pkg_config_package_flags($package, \%options?)

Search for pkg-config flags for package as specified. The flags which are
extracted are C<--cflags> and C<--libs>. The extracted flags are appended
to the global C<extra_compile_flags> and C<extra_link_flags>, respectively.

Call it with the package you're looking for and optional callback whether
found or not.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.
If any of I<action_on_cache_true>, I<action_on_cache_false> is defined,
both callbacks are passed to L</check_cached> as I<action_on_true> or
I<action_on_false> to L</check_cached>, respectively.

=cut

my $_pkg_config_prog;

sub _pkg_config_flag
{
    defined $_pkg_config_prog or croak("pkg_config_prog required");
    my @pkg_config_args = @_;
    my ($stdout, $stderr, $exit) =
      capture { system($_pkg_config_prog, @pkg_config_args); };
    chomp $stdout;
    0 == $exit and return $stdout;
    return;
}

sub pkg_config_package_flags
{
    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;
    my ($self, $package) = @_;
    $self = $self->_get_instance();

    (my $pkgpfx = $package) =~ s/^(\w+).*?$/$1/;
    my $cache_name = $self->_cache_name("pkg", $pkgpfx);

    defined $_pkg_config_prog or $_pkg_config_prog = $self->check_prog_pkg_config;
    my $check_sub = sub {
        my (@pkg_cflags, @pkg_libs);

        (my $ENV_CFLAGS = $package) =~ s/^(\w+).*?$/$1_CFLAGS/;
        my $CFLAGS =
          defined $ENV{$ENV_CFLAGS}
          ? $ENV{$ENV_CFLAGS}
          : _pkg_config_flag($package, "--cflags");
        $CFLAGS and @pkg_cflags = (
            map { $_ =~ s/^\s+//; $_ =~ s/\s+$//; Text::ParseWords::shellwords $_; }
              split(m/\n/, $CFLAGS)
        ) and push @{$self->{extra_preprocess_flags}}, @pkg_cflags;

        (my $ENV_LIBS = $package) =~ s/^(\w+).*?$/$1_LIBS/;
        # do not separate between libs and extra (for now) - they come with -l prepended
        my $LIBS =
          defined $ENV{$ENV_LIBS}
          ? $ENV{$ENV_LIBS}
          : _pkg_config_flag($package, "--libs");
        $LIBS and @pkg_libs = (
            map { $_ =~ s/^\s+//; $_ =~ s/\s+$//; Text::ParseWords::shellwords $_; }
              split(m/\n/, $LIBS)
        ) and push @{$self->{extra_link_flags}}, @pkg_libs;

        my $pkg_config_flags = join(" ", @pkg_cflags, @pkg_libs);

              $pkg_config_flags
          and $options->{action_on_true}
          and ref $options->{action_on_true} eq "CODE"
          and $options->{action_on_true}->();

        $options->{action_on_false}
          and ref $options->{action_on_false} eq "CODE"
          and !$pkg_config_flags
          and $options->{action_on_false}->();

        $pkg_config_flags;
    };

    $self->check_cached(
        $cache_name,
        "for pkg-config package of $package",
        $check_sub,
        {
            ($options->{action_on_cache_true}  ? (action_on_true  => $options->{action_on_cache_true})  : ()),
            ($options->{action_on_cache_false} ? (action_on_false => $options->{action_on_cache_false}) : ())
        }
    );
}

=head2 _check_mm_pureperl_build_wanted

This method proves the C<_argv> attribute and (when set) the C<PERL_MM_OPT>
whether they contain I<PUREPERL_ONLY=(0|1)> or not. The attribute C<_force_xs>
is set as appropriate, which allows a compile test to bail out when C<Makefile.PL>
is called with I<PUREPERL_ONLY=0>.

=cut

sub _check_mm_pureperl_build_wanted
{
    my $self = shift->_get_instance;

    defined $ENV{PERL_MM_OPT} and my @env_args = split " ", $ENV{PERL_MM_OPT};

    foreach my $arg (@{$self->{_argv}}, @env_args)
    {
        $arg =~ m/^PUREPERL_ONLY=(.*)$/ and return int($1);
    }

    0;
}

=head2 _check_mb_pureperl_build_wanted

This method proves the C<_argv> attribute and (when set) the C<PERL_MB_OPT>
whether they contain I<--pureperl-only> or not.

=cut

sub _check_mb_pureperl_build_wanted
{
    my $self = shift->_get_instance;

    defined $ENV{PERL_MB_OPT} and my @env_args = split " ", $ENV{PERL_MB_OPT};

    foreach my $arg (@{$self->{_argv}}, @env_args)
    {
        $arg eq "--pureperl-only" and return 1;
    }

    0;
}

=head2 _check_pureperl_required

This method calls C<_check_mm_pureperl_build_wanted> when running under
L<ExtUtils::MakeMaker> (C<Makefile.PL>) or C<_check_mb_pureperl_build_wanted>
when running under a C<Build.PL> (L<Module::Build> compatible) environment.

When neither is found (C<$0> contains neither C<Makefile.PL> nor C<Build.PL>),
simply 0 is returned.

=cut

sub _check_pureperl_required
{
    my $self = shift;
    $0 =~ m/Makefile\.PL$/i and return $self->_check_mm_pureperl_build_wanted(@_);
    $0 =~ m/Build\.PL$/i    and return $self->_check_mb_pureperl_build_wanted(@_);

    0;
}

=head2 check_pureperl_required

This check method proves whether a pure perl build is wanted or not by
cached-checking C<< $self->_check_pureperl_required >>.

=cut

sub check_pureperl_required
{
    my $self       = shift->_get_instance;
    my $cache_name = $self->_cache_name(qw(pureperl required));
    $self->check_cached($cache_name, "whether pureperl is required", sub { $self->_check_pureperl_required });
}

=head2 check_produce_xs_build

This routine checks whether XS can be produced. Therefore it does
following checks in given order:

=over 4

=item *

check pure perl environment variables (L</check_pureperl_required>) or
command line arguments and return false when pure perl is requested

=item *

check whether a compiler is available (L</check_valid_compilers>) and
return false if none found

=item *

check whether a test program accessing Perl API can be compiled and
die with error if not

=back

When all checks passed successfully, return a true value.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.

=cut

sub check_produce_xs_build
{
    my $options = {};
    scalar @_ > 1 and ref $_[-1] eq "HASH" and $options = pop @_;
    my $self = shift->_get_instance;
    $self->check_pureperl_required() and return _on_return_callback_helper(0, $options, "action_on_false");
    eval { $self->check_valid_compilers($_[0] || [qw(C)]) }
      or return _on_return_callback_helper(0, $options, "action_on_false");
    # XXX necessary check for $Config{useshrlib}? (need to dicuss with eg. TuX, 99% likely return 0)
    $self->check_compile_perlapi_or_die();

    $options->{action_on_true}
      and ref $options->{action_on_true} eq "CODE"
      and $options->{action_on_true}->();

    return 1;
}

=head2 check_produce_loadable_xs_build

This routine proves whether XS should be built and it's possible to create
a dynamic linked object which can be loaded using Perl's Dynaloader.

The extension over L</check_produce_xs_build> can be avoided by adding the
C<notest_loadable_xs> to C<$ENV{PERL5_AC_OPTS}>.

If the very last parameter contains a hash reference, C<CODE> references
to I<action_on_true> or I<action_on_false> are executed, respectively.

=cut

sub check_produce_loadable_xs_build
{
    my $self = shift->_get_instance;
    $self->check_produce_xs_build(@_)
      and !$self->{c_ac_flags}->{notest_loadable_xs}
      and $self->check_linkable_xs_so_or_die
      and $self->check_loadable_xs_so_or_die;
}

#
#
# Auxiliary funcs
#

=head2 _set_argv

Intended to act as a helper for evaluating given command line arguments.
Stores given arguments in instances C<_argv> attribute.

Call once at very begin of C<Makefile.PL> or C<Build.PL>:

  Your::Pkg::Config::AutoConf->_set_args(@ARGV);

=cut

sub _set_argv
{
    my ($self, @argv) = @_;
    $self = $self->_get_instance;
    $self->{_argv} = \@argv;
    return;
}

sub _sanitize
{
    # This is hard coded, and maybe a little stupid...
    my $x = shift;
    $x =~ s/ //g;
    $x =~ s/\///g;
    $x =~ s/\\//g;
    $x;
}

sub _get_instance
{
    ref $_[0] and return $_[0];
    defined $glob_instance or $glob_instance = $_[0]->new();
    $glob_instance;
}

sub _get_builder
{
    my $self = $_[0]->_get_instance();

    ref $self->{lang_supported}->{$self->{lang}} eq "CODE" and $self->{lang_supported}->{$self->{lang}}->($self);
    defined($self->{lang_supported}->{$self->{lang}}) or croak("Unsupported compile language \"" . $self->{lang} . "\"");

    $self->{lang_supported}->{$self->{lang}}->new();
}

sub _set_language
{
    my $self = shift->_get_instance();
    my ($lang, $impl) = @_;

    defined($lang) or croak("Missing language");

          defined($impl)
      and defined($self->{lang_supported}->{$lang})
      and $impl ne $self->{lang_supported}->{$lang}
      and croak("Language implementor ($impl) doesn't match exisiting one (" . $self->{lang_supported}->{$lang} . ")");

    defined($impl)
      and !defined($self->{lang_supported}->{$lang})
      and $self->{lang_supported}->{$lang} = $impl;

    ref $self->{lang_supported}->{$lang} eq "CODE" and $self->{lang_supported}->{$lang}->($self);
    defined($self->{lang_supported}->{$lang}) or croak("Unsupported language \"$lang\"");

    defined($self->{extra_compile_flags}->{$lang}) or $self->{extra_compile_flags}->{$lang} = [];

    $self->{lang} = $lang;

    return;
}

sub _on_return_callback_helper
{
    my $callback = pop @_;
    my $options  = pop @_;
    $options->{$callback}
      and ref $options->{$callback} eq "CODE"
      and $options->{$callback}->();
    @_ and wantarray and return @_;
    1 == scalar @_ and return $_[0];
    return;
}

sub _fill_defines
{
    my ($self, $src, $action_if_true, $action_if_false) = @_;
    ref $self or $self = $self->_get_instance();

    my $conftest = "";
    while (my ($defname, $defcnt) = each(%{$self->{defines}}))
    {
        $defcnt->[0] or next;
        defined $defcnt->[1] and $conftest .= "/* " . $defcnt->[1] . " */\n";
        $conftest .= join(" ", "#define", $defname, $defcnt->[0]) . "\n";
    }
    $conftest .= "/* end of conftest.h */\n";

    $conftest;
}

#
# default includes taken from autoconf/headers.m4
#

=head2 _default_includes

returns a string containing default includes for program prologue taken
from autoconf/headers.m4:

  #include <stdio.h>
  #ifdef HAVE_SYS_TYPES_H
  # include <sys/types.h>
  #endif
  #ifdef HAVE_SYS_STAT_H
  # include <sys/stat.h>
  #endif
  #ifdef STDC_HEADERS
  # include <stdlib.h>
  # include <stddef.h>
  #else
  # ifdef HAVE_STDLIB_H
  #  include <stdlib.h>
  # endif
  #endif
  #ifdef HAVE_STRING_H
  # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
  #  include <memory.h>
  # endif
  # include <string.h>
  #endif
  #ifdef HAVE_STRINGS_H
  # include <strings.h>
  #endif
  #ifdef HAVE_INTTYPES_H
  # include <inttypes.h>
  #endif
  #ifdef HAVE_STDINT_H
  # include <stdint.h>
  #endif
  #ifdef HAVE_UNISTD_H
  # include <unistd.h>
  #endif

=cut

my $_default_includes = <<"_ACEOF";
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
#  include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# if !defined STDC_HEADERS && defined HAVE_MEMORY_H
#  include <memory.h>
# endif
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
_ACEOF

sub _default_includes { $_default_includes }

sub _default_main { $_[0]->_build_main("") }

my $_main_tpl = <<"_ACEOF";
  int
  main ()
  {
    %s;
    return 0;
  }
_ACEOF

sub _build_main
{
    my $self = shift->_get_instance();
    my $body = shift || "";
    sprintf($_main_tpl, $body);
}

=head2 _default_includes_with_perl

returns a string containing default includes for program prologue containing
I<_default_includes> plus

  #include <EXTERN.h>
  #include <perl.h>

=cut

my $_include_perl = <<"_ACEOF";
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h> /* for perl context in threaded perls */
_ACEOF

sub _default_includes_with_perl
{
    join("\n", $_[0]->_default_includes, $_include_perl);
}

sub _cache_prefix { "ac" }

sub _cache_name
{
    my ($self, @names) = @_;
    my $cache_name = join("_", $self->_cache_prefix(), "cv", @names);
    $cache_name =~ tr/_A-Za-z0-9/_/c;
    $cache_name;
}

sub _get_log_fh
{
    my $self = $_[0]->_get_instance();
    unless (defined($self->{logfh}))
    {
        my $open_mode = defined $self->{logfile_mode} ? $self->{logfile_mode} : ">";
        open(my $fh, $open_mode, $self->{logfile}) or croak "Could not open file $self->{logfile}: $!";
        $self->{logfh} = [$fh];
    }

    $self->{logfh};
}

sub _add_log_entry
{
    my ($self, @logentries) = @_;
    ref($self) or $self = $self->_get_instance();
    $self->_get_log_fh();
    foreach my $logentry (@logentries)
    {
        foreach my $fh (@{$self->{logfh}})
        {
            print {$fh} "$logentry";
        }
    }

    return;
}

sub _add_log_lines
{
    my ($self, @logentries) = @_;
    ref($self) or $self = $self->_get_instance();
    $self->_get_log_fh();
    my $logmsg = join("\n", @logentries) . "\n";
    foreach my $fh (@{$self->{logfh}})
    {
        print {$fh} $logmsg;
    }

    return;
}

=head2 add_log_fh

Push new file handles at end of log-handles to allow tee-ing log-output

=cut

sub add_log_fh
{
    my ($self, @newh) = @_;
    $self->_get_log_fh();
  SKIP_DUP:
    foreach my $fh (@newh)
    {
        foreach my $eh (@{$self->{logfh}})
        {
            $fh == $eh and next SKIP_DUP;
        }
        push @{$self->{logfh}}, $fh;
    }
    return;
}

=head2 delete_log_fh

Removes specified log file handles. This method allows you to shoot
yourself in the foot - it doesn't prove whether the primary nor the last handle
is removed. Use with caution.

=cut

sub delete_log_fh
{
    my ($self, @xh) = @_;
    $self->_get_log_fh();
  SKIP_DUP:
    foreach my $fh (@xh)
    {
        foreach my $ih (0 .. $#{$self->{logfh}})
        {
            $fh == $self->{logfh}->[$ih] or next;
            splice @{$self->{logfh}}, $ih, 1;
            last;
        }
    }
    return;
}

sub _cache_type_name
{
    my ($self, @names) = @_;
    $self->_cache_name(map { $_ =~ tr/*/p/; $_ } @names);
}

sub _get_extra_compiler_flags
{
    my $self    = shift->_get_instance();
    my @ppflags = @{$self->{extra_preprocess_flags}};
    my @cflags  = @{$self->{extra_compile_flags}->{$self->{lang}}};
    join(" ", @ppflags, @cflags);
}

sub _get_extra_linker_flags
{
    my $self     = shift->_get_instance();
    my @libs     = @{$self->{extra_libs}};
    my @lib_dirs = @{$self->{extra_lib_dirs}};
    my @ldflags  = @{$self->{extra_link_flags}};
    join(" ", @ldflags, map('-L' . $self->_sanitize_prog($_), @lib_dirs), map("-l$_", @libs));
}

=head1 AUTHOR

Alberto Simões, C<< <ambs@cpan.org> >>

Jens Rehsack, C<< <rehsack@cpan.org> >>

=head1 NEXT STEPS

Although a lot of work needs to be done, these are the next steps I
intend to take.

  - detect flex/lex
  - detect yacc/bison/byacc
  - detect ranlib (not sure about its importance)

These are the ones I think not too much important, and will be
addressed later, or by request.

  - detect an 'install' command
  - detect a 'ln -s' command -- there should be a module doing
    this kind of task.

=head1 BUGS

A lot. Portability is a pain. B<<Patches welcome!>>.

Please report any bugs or feature requests to
C<bug-Config-AutoConf@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Config-AutoConf>.  We will
be notified, and then you'll automatically be notified of progress
on your bug as we make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Config::AutoConf

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Config-AutoConf>

=item * CPAN Ratings

L<http://cpanratings.perl.org/dist/Config-AutoConf>

=item * MetaCPAN

L<https://metacpan.org/release/Config-AutoConf>

=item * Git Repository

L<https://github.com/ambs/Config-AutoConf>

=back

=head1 ACKNOWLEDGEMENTS

Michael Schwern for kind MacOS X help.

Ken Williams for ExtUtils::CBuilder

Peter Rabbitson for help on refactoring and making the API more Perl'ish

=head1 COPYRIGHT & LICENSE

Copyright 2004-2017 by the Authors

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 SEE ALSO

ExtUtils::CBuilder(3)

=cut

1;    # End of Config::AutoConf