File: configure

package info (click to toggle)
ocaml 4.05.0-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 27,060 kB
  • sloc: ml: 199,255; ansic: 44,187; sh: 5,611; makefile: 4,958; lisp: 4,223; asm: 4,220; awk: 306; perl: 87; fortran: 21; cs: 9; sed: 9
file content (2252 lines) | stat: -rwxr-xr-x 70,148 bytes parent folder | download | duplicates (2)
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
#! /bin/sh

#**************************************************************************
#*                                                                        *
#*                                 OCaml                                  *
#*                                                                        *
#*            Xavier Leroy, projet Cristal, INRIA Rocquencourt            *
#*                                                                        *
#*   Copyright 1999 Institut National de Recherche en Informatique et     *
#*     en Automatique.                                                    *
#*                                                                        *
#*   All rights reserved.  This file is distributed under the terms of    *
#*   the GNU Lesser General Public License version 2.1, with the          *
#*   special exception on linking described in the file LICENSE.          *
#*                                                                        *
#**************************************************************************

ocamlversion=`head -1 VERSION`
echo "Configuring OCaml version $ocamlversion"

configure_options="$*"
prefix=/usr/local
bindir=''
target_bindir=''
libdir=''
mandir=''
manext=1
host_type=unknown
target_type=""
ccoption=''
asoption=''
asppoption=''
cclibs=''
curseslibs=''
mathlib='-lm'
dllib=''
x11_include_dir=''
x11_lib_dir=''
libunwind_include_dir=''
libunwind_lib_dir=''
libunwind_available=false
disable_libunwind=false
graph_wanted=yes
pthread_wanted=yes
dl_defs=''
verbose=no
with_curses=yes
debugruntime=false
with_instrumented_runtime=false
with_sharedlibs=yes
partialld="ld -r"
with_debugger=ocamldebugger
with_ocamldoc=ocamldoc
with_frame_pointers=false
with_spacetime=false
with_profinfo=false
profinfo_width=0
no_naked_pointers=false
native_compiler=true
TOOLPREF=""
with_cfi=true
flambda=false
safe_string=false
afl_instrument=false
max_testsuite_dir_retries=0
with_cplugins=true
with_fpic=false

# Try to turn internationalization off, can cause config.guess to malfunction!
unset LANG
unset LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME

# Turn off some MacOS X debugging stuff, same reason
unset RC_TRACE_ARCHIVES RC_TRACE_DYLIBS RC_TRACE_PREBINDING_DISABLED

# The inf(), wrn(), err() functions below can be used to provide a consistent
# way to notify the user. The notification is always given to the stdout
# descriptor.
#
# Their output is redirected to a file-descriptor "3" which is then redirected
# to fd 1 at the level of the whole configure script. This is done to not
# conflict with how values are returned from functions in shell script.
# Consider the following where "It works!" would be mixed with "42".
#   do_foo() {
#     if some_command; then
#       inf "It works!"
#       echo "42"
#     fi
#   }
inf() {
  printf "%b\n" "$*" 1>&3
}

wrn() {
  printf "[WARNING] %b\n" "$*" 1>&3
}

err() {
  printf "[ERROR!] %b\n" "$*" 1>&3
  exit 2
}

exec 3>&1

# Parse command-line arguments

while : ; do
  case "$1" in
    "") break;;
    -prefix|--prefix)
        prefix=$2; shift;;
    -bindir|--bindir)
        bindir=$2; shift;;
    -target-bindir|--target-bindir)
        target_bindir="$2"; shift;;
    -libdir|--libdir)
        libdir=$2; shift;;
    -mandir|--mandir)
        case "$2" in
          */man[1-9ln])
            mandir=`echo $2 | sed -e 's|^\(.*\)/man.$|\1|'`
            manext=`echo $2 | sed -e 's/^.*\(.\)$/\1/'`;;
          *)
            mandir=$2
            manext=1;;
        esac
        shift;;
    -libunwinddir|--libunwinddir)
        libunwind_include_dir=$2/include;
        libunwind_lib_dir=$2/lib;
        shift;;
    -libunwindlib|--libunwindlib)
        libunwind_lib_dir=$2; shift;;
    -libunwindinclude|--libunwindinclude)
        libunwind_include_dir=$2; shift;;
    -disable-libunwind|--disable-libunwind)
        disable_libunwind=true;;
    -host*|--host*)
        host_type=$2; shift;;
    -target*|--target*)
        target_type=$2; shift;;
    -cc*)
        ccoption="$2"; shift;;
    -as)
        asoption="$2"; shift;;
    -aspp)
        asppoption="$2"; shift;;
    -lib*)
        cclibs="$2 $cclibs"; shift;;
    -no-curses|--no-curses)
        with_curses=no;;
    -no-shared-libs|--no-shared-libs)
        with_sharedlibs=no;;
    -x11include*|--x11include*)
        x11_include_dir=$2; shift;;
    -x11lib*|--x11lib*)
        x11_lib_dir=$2; shift;;
    -no-graph|--no-graph) graph_wanted=no;;
    -with-pthread*|--with-pthread*)
        ;; # Ignored for backward compatibility
    -no-pthread*|--no-pthread*)
        pthread_wanted=no;;
    -partialld|--partialld)
        partialld="$2"; shift;;
    -dldefs*|--dldefs*)
        dl_defs="$2"; shift;;
    -dllibs*|--dllibs*)
        dllib="$2"; shift;;
    -verbose|--verbose)
        verbose=yes;;
    -with-debug-runtime|--with-debug-runtime)
        debugruntime=true;;
    -with-instrumented-runtime|--with-instrumented-runtime)
        with_instrumented_runtime=true;;
    -no-debugger|--no-debugger)
        with_debugger="";;
    -no-ocamldoc|--no-ocamldoc)
        with_ocamldoc="";;
    -no-ocamlbuild|--no-ocamlbuild)
        ;; # ignored for backward compatibility
    -with-frame-pointers|--with-frame-pointers)
        with_frame_pointers=true;;
    -no-naked-pointers|--no-naked-pointers)
        no_naked_pointers=true;;
    -spacetime|--spacetime)
        with_spacetime=true;  with_profinfo=true; profinfo_width=26;;
    -reserved-header-bits|--reserved-header-bits)
        with_spacetime=false; with_profinfo=true; profinfo_width=$2;shift
        case $profinfo_width in
            0) with_profinfo=false;;
            [0123456789]);;
            1?|2?);;
            3[012]);;
            *) err "--reserved-header-bits argument must be less than 32"
        esac
        ;;
    -no-cfi|--no-cfi)
        with_cfi=false;;
    -no-native-compiler|--no-native-compiler)
        native_compiler=false;;
    -flambda|--flambda)
        flambda=true;;
    -no-cplugins|--no-cplugins)
        with_cplugins=false;;
    -fPIC|--fPIC)
        with_fpic=true;;
    -safe-string|--safe-string)
        safe_string=true;;
    -afl-instrument)
        afl_instrument=true;;
    *) if echo "$1" | grep -q -e '^--\?[a-zA-Z0-9-]\+='; then
         err "configure expects arguments of the form '-prefix /foo/bar'," \
             "not '-prefix=/foo/bar' (note the '=')."
       else
         err "Unknown option \"$1\"."
       fi;;
  esac
  shift
done

# Sanity checks

case "$prefix" in
  /*) ;;
   *) err "The -prefix directory must be absolute.";;
esac
case "$bindir" in
  /*) ;;
  "") ;;
  '$(PREFIX)/'*) ;;
   *) err 'The -bindir directory must be absolute or relative to $(PREFIX).';;
esac
case "$libdir" in
  /*) ;;
  "") ;;
  '$(PREFIX)/'*) ;;
   *) err 'The -libdir directory must be absolute or relative to $(PREFIX).';;
esac
case "$mandir" in
  /*) ;;
  "") ;;
  '$(PREFIX)/'*) ;;
   *) err 'The -mandir directory must be absolute or relative to $(PREFIX).';;
esac

# Generate the files

cd config/auto-aux
rm -f s.h m.h Makefile
touch s.h m.h Makefile

# Write options to Makefile

echo "# generated by ./configure $configure_options" >> Makefile
echo "CONFIGURE_ARGS=$configure_options" >> Makefile

# Where to install

echo "PREFIX=$prefix" >> Makefile
case "$bindir" in
  "") echo 'BINDIR=$(PREFIX)/bin' >> Makefile
      bindir="$prefix/bin";;
   *) echo "BINDIR=$bindir" >> Makefile;;
esac

echo 'BYTERUN=$(BINDIR)/ocamlrun' >> Makefile

case "$libdir" in
  "") echo 'LIBDIR=$(PREFIX)/lib/ocaml' >> Makefile
      libdir="$prefix/lib/ocaml";;
   *) echo "LIBDIR=$libdir" >> Makefile;;
esac
echo 'STUBLIBDIR=$(LIBDIR)/stublibs' >> Makefile
case "$mandir" in
  "") echo 'MANDIR=$(PREFIX)/man' >> Makefile
      mandir="$prefix/man";;
   *) echo "MANDIR=$mandir" >> Makefile;;
esac
echo "MANEXT=$manext" >> Makefile

# Determine the system type

if test "$host_type" = "unknown"; then
  if host_type=`sh ../gnu/config.guess`; then :; else
    err "Cannot guess host type. You must specify one with the -host option."
  fi
fi
if host=`sh ../gnu/config.sub $host_type`; then :; else
  err "Please specify the correct host type with the -host option"
fi
inf "Configuring for host $host ..."

if test -n "$target_type"; then
  target="$target_type"
  TOOLPREF="${target}-"
else
  target="$host"
fi
inf "Configuring for target $target ..."

if [ x"$host" = x"$target" ]; then
  cross_compiler=false
else
  cross_compiler=true
fi

# Do we have gcc?

if test -z "$ccoption"; then
  if sh ./searchpath "${TOOLPREF}gcc"; then
    cc="${TOOLPREF}gcc"
  else
    if $cross_compiler; then
      err "No cross-compiler found for ${target}.\n" \
          "It should be named ${TOOLPREF}gcc and be in the PATH."
    else
      cc="cc"
    fi
  fi
else
  cc="$ccoption"
fi

inf "Using compiler $cc."

# Determine the C compiler family (GCC, Clang, etc)

ccfamily=`$cc -E cckind.c | grep '^[a-z]' | tr -s ' ' '-'`
case $? in
  0) inf "Compiler family and version: $ccfamily.";;
  *) err "Unable to preprocess the test program.\n" \
         "Make sure the C compiler $cc is properly installed.";;
esac

# Configure the bytecode compiler

# The BYTECC make variable defines which compiler and options to use
# to compile C code intended to be used by OCaml bytecode programs.
# It is used inside OCaml's build system.

# The BYTECODE_C_COMPILER make variable says how the C compiler should be
# invoked to process a third-party C source file passed to ocamlc
# when no -cc command-line option has been specified.

# The BYTECCCOMPOPTS make variable contains options to pass to the C
# compiler but only when compiling C files that belong to the OCaml
# distribution.
# In other words, when ocamlc is called to compile a third-party C
# source file, it will _not_ pass these options to the C compiler.

# The SHAREDCCCOMPOPTS make variable contains options to use to compile C
# source files so that the resulting object files can then be integrated
# into shared libraries. It is passed to BYTECC for both C source files
# in the OCaml distribution and third-party C source files compiled
# with ocamlc.

bytecc="$cc"
mkexe="\$(BYTECC)"
mkexedebugflag="-g"
bytecccompopts=""
byteccprivatecompopts=""
bytecclinkopts=""
ostype="Unix"
exe=""
iflexdir=""
SO="so"
TOOLCHAIN="cc"

# Choose reasonable options based on compiler kind
# We select high optimization levels, provided we can turn off:
# - strict type-based aliasing analysis (too risky for the OCaml runtime)
# - strict no-overflow conditions on signed integer arithmetic
#   (the OCaml runtime assumes Java-style behavior of signed integer arith.)
# Concerning optimization level, -O3 is somewhat risky, so take -O2.
# Concerning language version, gnu99 is ISO C99 plus GNU extensions
# that are often used in standard headers.  Older GCC versions
# defaults to gnu89, which is not C99.  Clang defaults to gnu99 or
# gnu11, which is fine.

case "$ocamlversion" in
  *+dev*) gcc_warnings="-Wall -Werror";;
       *) gcc_warnings="-Wall";;
esac

case "$ccfamily" in
  clang-*)
    bytecccompopts="-O2 -fno-strict-aliasing -fwrapv";
    byteccprivatecompopts="$gcc_warnings";;
  gcc-[012]-*)
    # Some versions known to miscompile OCaml, e,g, 2.7.2.1, some 2.96.
    # Plus: C99 support unknown.
    err "This version of GCC is too old. Please use GCC version 4.2 or above.";;
  gcc-3-*|gcc-4-[01])
    # No -fwrapv option before GCC 3.4.
    # Known problems with -fwrapv fixed in 4.2 only.
    wrn "This version of GCC is rather old.  Reducing optimization level."
    wrn "Consider using GCC version 4.2 or above."
    bytecccompopts="-std=gnu99 -O";
    byteccprivatecompopts="$gcc_warnings";;
  gcc-4-*)
    bytecccompopts="-std=gnu99 -O2 -fno-strict-aliasing -fwrapv \
-fno-builtin-memcmp";
    byteccprivatecompopts="$gcc_warnings";;
  gcc-*)
    bytecccompopts="-O2 -fno-strict-aliasing -fwrapv";
    byteccprivatecompopts="$gcc_warnings";;
  *)
    bytecccompopts="-O";;
esac

byteccprivatecompopts="-DCAML_NAME_SPACE $byteccprivatecompopts"

# Adjust according to target

case "$bytecc,$target" in
  cc,*-*-nextstep*)
    # GNU C extensions disabled, but __GNUC__ still defined!
    bytecccompopts="$bytecccompopts -U__GNUC__ -posix"
    bytecclinkopts="-posix";;
  *,*-*-rhapsody*)
    # Almost the same as NeXTStep
    bytecccompopts="$bytecccompopts -DSHRINKED_GNUC"
    mathlib="";;
  *,*-*-darwin*)
    mathlib=""
    mkexe="$mkexe -Wl,-no_compact_unwind"
    # Tell gcc that we can use 32-bit code addresses for threaded code
    # unless we are compiled for a shared library (-fPIC option)
    echo "#ifndef __PIC__" >> m.h
    echo "#  define ARCH_CODE32" >> m.h
    echo "#endif" >> m.h;;
  *,*-*-haiku*)
    # No -lm library
    mathlib="";;
  *,*-*-beos*)
    # No -lm library
    mathlib="";;
  *gcc,alpha*-*-osf*)
    if cc="$bytecc" sh ./hasgot -mieee; then
      bytecccompopts="-mieee $bytecccompopts";
    fi
    # Put code and static data in lower 4GB
    bytecclinkopts="-Wl,-T,12000000 -Wl,-D,14000000"
    # Tell gcc that we can use 32-bit code addresses for threaded code
    echo "#define ARCH_CODE32" >> m.h;;
  cc,alpha*-*-osf*)
    bytecccompopts="-std1 -ieee";;
  *gcc*,alpha*-*-linux*)
    if cc="$bytecc" sh ./hasgot -mieee; then
      bytecccompopts="-mieee $bytecccompopts";
    fi
    bytecclinkopts="-Wl,--no-relax";;
  *,mips-*-irix6*)
    # Turn off warning "unused library"
    bytecclinkopts="-n32 -Wl,-woff,84";;
  *,alpha*-*-unicos*)
    # For the Cray T3E
    bytecccompopts="$bytecccompopts -DUMK";;
  *,powerpc-*-aix*)
    # Avoid name-space pollution by requiring Unix98-conformant includes
    bytecccompopts="$bytecccompopts -D_XOPEN_SOURCE=500 -D_ALL_SOURCE";;
  *,*-*-cygwin*)
    case $target in
      i686-*) flavor=cygwin;;
      x86_64-*) flavor=cygwin64;;
      *) err "unknown cygwin variant";;
    esac
    bytecccompopts="$bytecccompopts -U_WIN32"
    if test $with_sharedlibs = yes; then
      flexlink="flexlink -chain $flavor -merge-manifest -stack 16777216"
      flexdir=`$flexlink -where | tr -d '\015'`
      if test -z "$flexdir"; then
        wrn "flexlink not found: native shared libraries won't be available."
        with_sharedlibs=no
      else
        iflexdir="-I\"$flexdir\""
        mkexe="$flexlink -exe"
        mkexedebugflag="-link -g"
      fi
    fi
    if test $with_sharedlibs = no; then
      mkexe="$mkexe -Wl,--stack,16777216"
      bytecclinkopts="-Wl,--stack,16777216"
    fi
    exe=".exe"
    ostype="Cygwin";;
  *,*-*-mingw*)
    dllccompopt="-DCAML_DLL"
    if test $with_sharedlibs = yes; then
      case "$target" in
        i686-*-*)   flexlink_chain="mingw";;
        x86_64-*-*) flexlink_chain="mingw64";;
      esac
      flexlink="flexlink -chain $flexlink_chain -merge-manifest -stack 16777216"
      flexdir=`$flexlink -where`
      if test -z "$flexdir"; then
        wrn "flexlink not found: native shared libraries won't be available."
        with_sharedlibs=no
      else
        iflexdir="-I\"$flexdir\""
        mkexe="$flexlink -exe"
        mkexedebugflag="-link -g"
      fi
    fi
    exe=".exe"
    ostype="Win32"
    TOOLCHAIN="mingw"
    SO="dll"
    ;;
  *,x86_64-*-linux*)
    # Tell gcc that we can use 32-bit code addresses for threaded code
    # unless we are compiled for a shared library (-fPIC option)
    echo "#ifndef __PIC__" >> m.h
    echo "#  define ARCH_CODE32" >> m.h
    echo "#endif" >> m.h;;
esac

# Configure compiler to use in further tests.

cc="$bytecc $bytecclinkopts"
export cc cclibs verbose

# Check C compiler.

cc="$bytecc $bytecccompopts $byteccprivatecompopts $bytecclinkopts" sh ./runtest ansi.c
case $? in
  0) inf "The C compiler is ISO C99 compliant." ;;
  1) wrn "The C compiler is ANSI / ISO C90 compliant, but not ISO C99" \
         "compliant.";;
  2) err "The C compiler $cc is not ISO C compliant.\n" \
         "You need an ISO C99 compiler to build OCaml.";;
  *)
     if $cross_compiler; then
       wrn "Unable to compile the test program.\n" \
           "This failure is expected for cross-compilation:\n" \
           "we will assume the C compiler is ISO C99-compliant."
     else
       err "Unable to compile the test program.\n" \
           "Make sure the C compiler $cc is properly installed."
     fi;;
esac

# For cross-compilation, we need a host-based ocamlrun and ocamlyacc,
# and the user must specify the target BINDIR
if $cross_compiler; then
  if ! sh ./searchpath ocamlrun; then
    err "Cross-compilation requires an ocaml runtime environment\n" \
        "(the ocamlrun binary). Moreover, its version must be the same\n" \
        "as the one you're trying to build (`cut -f1 -d+ < ../../VERSION`)."
  else
    ocaml_system_version=`ocamlrun -version | sed 's/[^0-9]*\([0-9.]*\).*/\1/'`
    ocaml_source_version=`sed -n '1 s/\([0-9\.]*\).*/\1/ p' < ../../VERSION`
    if test x"$ocaml_system_version" != x"$ocaml_source_version"; then
      err "While you have an ocaml runtime environment, its version\n" \
          "($ocaml_system_version) doesn't match the version of these\n" \
          "sources ($ocaml_source_version)."
    else
      echo "CAMLRUN=`./searchpath -p ocamlrun`" >> Makefile
    fi
  fi

  if ! sh ./searchpath ocamlyacc; then
    err "Cross-compilation requires an ocamlyacc binary."
  else
    ocamlyacc 2>/dev/null
    if test "$?" -ne 1; then
      err "While you have an ocamlyacc binary, it cannot be executed" \
          "successfully."
    else
      echo "CAMLYACC=`./searchpath -p ocamlyacc`" >> Makefile
    fi
  fi

  if [ -z "$target_bindir" ]; then
    err "Cross-compilation requires -target-bindir."
  else
    echo "TARGET_BINDIR=$target_bindir" >> Makefile
  fi
fi # cross-compiler


# Check the sizes of data types
# OCaml needs a 32 or 64 bit architecture, a 32-bit integer type and
# a 64-bit integer type

inf "Checking the sizes of integers and pointers..."
ret=`sh ./runtest sizes.c`
# $1 = sizeof(int)
# $2 = sizeof(long)
# $3 = sizeof(pointers)
# $4 = sizeof(short)
# $5 = sizeof(long long)

if test "$?" -eq 0; then
  set $ret
  case "$3" in
    4) inf "OK, this is a regular 32 bit architecture."
       echo "#undef ARCH_SIXTYFOUR" >> m.h
       arch64=false;;
    8) inf "Wow! A 64 bit architecture!"
       echo "#define ARCH_SIXTYFOUR" >> m.h
       arch64=true;;
    *) err "This architecture seems to be neither 32 bits nor 64 bits.\n" \
           "OCaml won't run on this architecture.";;
  esac
else
  # For cross-compilation, runtest always fails: add special handling.
  case "$target" in
    i686-*-mingw*) inf "OK, this is a regular 32 bit architecture."
                   echo "#undef ARCH_SIXTYFOUR" >> m.h
                   set 4 4 4 2 8
                   arch64=false;;
    x86_64-*-mingw*) inf "Wow! A 64 bit architecture!"
                     echo "#define ARCH_SIXTYFOUR" >> m.h
                     set 4 4 8 2 8
                     arch64=true;;
    *) err "Since datatype sizes cannot be guessed when cross-compiling,\n" \
           "a hardcoded list is used but your architecture isn't known yet.\n" \
           "You need to determine the sizes yourself.\n" \
           "Please submit a bug report in order to expand the list." ;;
  esac
fi

if test $1 != 4 && test $2 != 4 && test $4 != 4; then
  err "Sorry, we can't find a 32-bit integer type\n" \
      "(sizeof(short) = $4, sizeof(int) = $1, sizeof(long) = $2)\n" \
     "OCaml won't run on this architecture."
fi

if test $2 != 8 && test $5 != 8; then
  err "Sorry, we can't find a 64-bit integer type\n" \
      "(sizeof(long) = $2, sizeof(long long) = $5)\n" \
     "OCaml won't run on this architecture."
fi

echo "#define SIZEOF_INT $1" >> m.h
echo "#define SIZEOF_LONG $2" >> m.h
echo "#define SIZEOF_PTR $3" >> m.h
echo "#define SIZEOF_SHORT $4" >> m.h
echo "#define SIZEOF_LONGLONG $5" >> m.h
echo "#define INT64_LITERAL(s) s ## LL" >> m.h

# Determine endianness

sh ./runtest endian.c
case $? in
  0) inf "This is a big-endian architecture."
     echo "#define ARCH_BIG_ENDIAN" >> m.h;;
  1) inf "This is a little-endian architecture."
     echo "#undef ARCH_BIG_ENDIAN" >> m.h;;
  2) err "This architecture seems to be neither big endian nor little" \
         "endian.\n OCaml won't run on this architecture.";;
  *) case $target in
       *-*-mingw*) inf "This is a little-endian architecture."
                   echo "#undef ARCH_BIG_ENDIAN" >> m.h;;
       *) wrn "Something went wrong during endianness determination.\n" \
              "You will have to figure out endianness yourself\n" \
              "(option ARCH_BIG_ENDIAN in m.h).";;
     esac;;
esac

# Determine alignment constraints

case "$target" in
  sparc*-*-*|hppa*-*-*|arm*-*-*|mips*-*-*)
    # On Sparc V9 with certain versions of gcc, determination of double
    # alignment is not reliable (PR#1521), hence force it.
    # Same goes for hppa.
    # PR#5088 suggests same problem on ARM.
    # PR#5280 reports same problem on MIPS.
    # But there's a knack (PR#2572):
    # if we're in 64-bit mode (sizeof(long) == 8),
    # we must not doubleword-align floats...
    if test $2 = 8; then
      inf "Doubles can be word-aligned."
      echo "#undef ARCH_ALIGN_DOUBLE" >> m.h
    else
      inf "Doubles must be doubleword-aligned."
      echo "#define ARCH_ALIGN_DOUBLE" >> m.h
    fi;;
  *)
    sh ./runtest dblalign.c
    case $? in
      0) inf "Doubles can be word-aligned."
         echo "#undef ARCH_ALIGN_DOUBLE" >> m.h;;
      1) inf "Doubles must be doubleword-aligned."
         echo "#define ARCH_ALIGN_DOUBLE" >> m.h;;
      *) case "$target" in
           *-*-mingw*) inf "Doubles can be word-aligned."
                       echo "#undef ARCH_ALIGN_DOUBLE" >> m.h;;
           *) wrn "Something went wrong during alignment determination for" \
                  "doubles.\n" \
                  "We will assume alignment constraints over doubles.\n" \
                  "That's a safe bet: OCaml will work even if\n" \
                  "this architecture actually has no alignment constraints."
              echo "#define ARCH_ALIGN_DOUBLE" >> m.h;;
         esac;;
    esac;;
esac

case "$target" in
  # PR#5088: autodetection is unreliable on ARM.  PR#5280: also on MIPS.
  sparc*-*-*|hppa*-*-*|arm*-*-*|mips*-*-*)
    if test $2 = 8; then
      inf "64-bit integers can be word-aligned."
      echo "#undef ARCH_ALIGN_INT64" >> m.h
    else
      inf "64-bit integers must be doubleword-aligned."
      echo "#define ARCH_ALIGN_INT64" >> m.h
    fi;;
  *-*-mingw*) true;; # Nothing is in config/m-nt.h so don't add anything.
  *)
    sh ./runtest int64align.c
    case $? in
      0) inf "64-bit integers can be word-aligned."
         echo "#undef ARCH_ALIGN_INT64" >> m.h;;
      1) inf "64-bit integers must be doubleword-aligned."
         echo "#define ARCH_ALIGN_INT64" >> m.h;;
      *) wrn "Something went wrong during alignment determination for\n" \
             "64-bit integers. I'm going to assume this architecture has\n" \
             "alignment constraints. That's a safe bet: OCaml will work\n" \
             "even if this architecture has actually no alignment\n" \
             "constraints." \
         echo "#define ARCH_ALIGN_INT64" >> m.h;;
    esac
esac

# Shared library support

shared_libraries_supported=false
dl_needs_underscore=false
sharedcccompopts=''
mksharedlib='shared-libs-not-available'
byteccrpath=''
mksharedlibrpath=''
natdynlinkopts=""

if test $with_sharedlibs = "yes"; then
  case "$target" in
    *-*-cygwin*)
      mksharedlib="$flexlink"
      mkmaindll="$flexlink -maindll"
      shared_libraries_supported=true;;
    *-*-mingw*)
      mksharedlib="$flexlink"
      mkmaindll="$flexlink -maindll"
      shared_libraries_supported=true;;
    alpha*-*-osf*)
      case "$bytecc" in
        *gcc*)
          sharedcccompopts="-fPIC"
          mksharedlib="$bytecc -shared"
          byteccrpath="-Wl,-rpath,"
          mksharedlibrpath="-Wl,-rpath,"
          shared_libraries_supported=true;;
        cc*)
          sharedcccompopts=""
          mksharedlib="ld -shared -expect_unresolved '*'"
          byteccrpath="-Wl,-rpath,"
          mksharedlibrpath="-rpath "
          shared_libraries_supported=true;;
      esac;;
    *-*-solaris2*)
      case "$bytecc" in
        *gcc*)
          sharedcccompopts="-fPIC"
          if sh ./solaris-ld; then
            mksharedlib="ld -G"
            byteccrpath="-R"
            mksharedlibrpath="-R"
          else
            mksharedlib="$bytecc -shared"
            bytecclinkopts="$bytecclinkopts -Wl,-E"
            natdynlinkopts="-Wl,-E"
            byteccrpath="-Wl,-rpath,"
            mksharedlibrpath="-Wl,-rpath,"
          fi
          shared_libraries_supported=true;;
        *)
          sharedcccompopts="-KPIC"
          byteccrpath="-R"
          mksharedlibrpath="-R"
          mksharedlib="/usr/ccs/bin/ld -G"
          shared_libraries_supported=true;;
      esac;;
    mips*-*-irix[56]*)
      case "$bytecc" in
        cc*) sharedcccompopts="";;
        *gcc*) sharedcccompopts="-fPIC";;
      esac
      mksharedlib="ld -shared -rdata_shared"
      byteccrpath="-Wl,-rpath,"
      mksharedlibrpath="-rpath "
      shared_libraries_supported=true;;
    i[3456]86-*-darwin[89].*)
      mksharedlib="$bytecc -bundle -flat_namespace -undefined suppress \
                   -read_only_relocs suppress"
      bytecccompopts="$dl_defs $bytecccompopts"
      dl_needs_underscore=false
      shared_libraries_supported=true;;
    *-apple-darwin*)
      mksharedlib="$bytecc -bundle -flat_namespace -undefined suppress \
                   -Wl,-no_compact_unwind"
      bytecccompopts="$dl_defs $bytecccompopts"
      dl_needs_underscore=false
      shared_libraries_supported=true;;
    *-*-linux-gnu|*-*-linux|*-*-freebsd[3-9]*|*-*-freebsd[1-9][0-9]*\
    |*-*-openbsd*|*-*-netbsd*|*-*-dragonfly*|*-*-gnu*|*-*-haiku*)
      sharedcccompopts="-fPIC"
      mksharedlib="$bytecc -shared"
      bytecclinkopts="$bytecclinkopts -Wl,-E"
      byteccrpath="-Wl,-rpath,"
      mksharedlibrpath="-Wl,-rpath,"
      natdynlinkopts="-Wl,-E"
      shared_libraries_supported=true;;
  esac
fi
mksharedlib="$mksharedlib $CCLINKFLAGS"
mkexe="$mkexe $CCLINKFLAGS"

if test -z "$mkmaindll"; then
  mkmaindll=$mksharedlib
fi

# Configure native dynlink

natdynlink=false

if test $with_sharedlibs = "yes"; then
  case "$target" in
    *-*-cygwin*)                  natdynlink=true;;
    *-*-mingw*)                   natdynlink=true;;
    i[3456]86-*-linux*)           natdynlink=true;;
    i[3456]86-*-gnu*)             natdynlink=true;;
    x86_64-*-linux*)              natdynlink=true;;
    i[3456]86-*-darwin[89].*)     natdynlink=true;;
    i[3456]86-*-darwin*)
      if test $arch64 == true; then
        natdynlink=true
      fi;;
    x86_64-*-darwin*)             natdynlink=true;;
    s390x*-*-linux*)              natdynlink=true;;
    powerpc*-*-linux*)            natdynlink=true;;
    sparc*-*-linux*)              natdynlink=true;;
    i686-*-kfreebsd*)             natdynlink=true;;
    x86_64-*-kfreebsd*)           natdynlink=true;;
    x86_64-*-dragonfly*)          natdynlink=true;;
    i[3456]86-*-freebsd*)         natdynlink=true;;
    x86_64-*-freebsd*)            natdynlink=true;;
    i[3456]86-*-openbsd*)         natdynlink=true;;
    x86_64-*-openbsd*)            natdynlink=true;;
    i[3456]86-*-netbsd*)          natdynlink=true;;
    x86_64-*-netbsd*)             natdynlink=true;;
    i386-*-gnu0.3)                natdynlink=true;;
    i[3456]86-*-haiku*)           natdynlink=true;;
    arm*-*-linux*)                natdynlink=true;;
    arm*-*-freebsd*)              natdynlink=true;;
    earm*-*-netbsd*)              natdynlink=true;;
    aarch64-*-linux*)             natdynlink=true;;
  esac
fi

if test $natdynlink = "true"; then
  cmxs="cmxs"
else
  cmxs="cmxa"
fi


# Try to work around the Skylake/Kaby Lake processor bug.
case "$bytecc,$target" in
    *gcc*,x86_64-*|*gcc*,i686-*)
        if sh ./hasgot -Werror -fno-tree-vrp; then
            byteccprivatecompopts="$byteccprivatecompopts -fno-tree-vrp"
            inf "Adding -fno-tree-vrp option to work around PR#7452"
        fi;;
esac


# Configure the native-code compiler

# The NATIVECC make variable defines which compiler and options to use
# to compile C code intended to be used by OCaml native programs.
# It is used inside OCaml's build system.

# The NATIVE_C_COMPILER make variable says how the C compiler should be
# invoked to process a third-party C source file passed to ocamlopt
# when no -cc command-line option has been specified.

# The NATIVECCCOMPOPTS make variable contains options to pass to the C
# compiler, but only when compiling C files that belong to the OCaml
# distribution.
# In other words, when ocamlopt is called to compile a third-party C
# source file, it will _not_ pass these options to the C compiler.

arch=none
model=default
system=unknown

case "$target" in
  sparc*-*-solaris2.*)          arch=sparc; system=solaris;;
  sparc*-*-*bsd*)               arch=sparc; system=bsd;;
  sparc*-*-linux*)              arch=sparc; system=linux;;
  sparc*-*-gnu*)                arch=sparc; system=gnu;;
  i[3456]86-*-linux*)           arch=i386; system=linux_`sh ./runtest elf.c`;;
  i[3456]86-*-*bsd*)            arch=i386; system=bsd_`sh ./runtest elf.c`;;
  i[3456]86-*-nextstep*)        arch=i386; system=nextstep;;
  i[3456]86-*-solaris*)         if $arch64; then
                                  arch=amd64; system=solaris
                                else
                                  arch=i386; system=solaris
                                fi;;
  i[3456]86-*-haiku*)           arch=i386; system=beos;;
  i[3456]86-*-beos*)            arch=i386; system=beos;;
  i[3456]86-*-cygwin*)          arch=i386; system=cygwin;;
  i[3456]86-*-darwin*)          if $arch64; then
                                  arch=amd64; system=macosx
                                else
                                  arch=i386; system=macosx
                                fi;;
  i[3456]86-*-gnu*)             arch=i386; system=gnu;;
  i[3456]86-*-mingw*)           arch=i386; system=mingw;;
  powerpc64le*-*-linux*)        arch=power; model=ppc64le; system=elf;;
  powerpc*-*-linux*)            arch=power;
                                if $arch64; then model=ppc64; else model=ppc; fi
                                system=elf;;
  powerpc-*-netbsd*)            arch=power; model=ppc; system=elf;;
  powerpc-*-openbsd*)           arch=power; model=ppc; system=bsd_elf;;
  s390x*-*-linux*)              arch=s390x; model=z10; system=elf;;
  armv6*-*-linux-gnueabihf)     arch=arm; model=armv6; system=linux_eabihf;;
  arm*-*-linux-gnueabihf)       arch=arm; system=linux_eabihf;;
  armv7*-*-linux-gnueabi)       arch=arm; model=armv7; system=linux_eabi;;
  armv6t2*-*-linux-gnueabi)     arch=arm; model=armv6t2; system=linux_eabi;;
  armv6*-*-linux-gnueabi)       arch=arm; model=armv6; system=linux_eabi;;
  armv6*-*-freebsd*)            arch=arm; model=armv6; system=freebsd;;
  earmv6*-*-netbsd*)            arch=arm; model=armv6; system=netbsd;;
  earmv7*-*-netbsd*)            arch=arm; model=armv7; system=netbsd;;
  armv5te*-*-linux-gnueabi)     arch=arm; model=armv5te; system=linux_eabi;;
  armv5*-*-linux-gnueabi)       arch=arm; model=armv5; system=linux_eabi;;
  arm*-*-linux-gnueabi)         arch=arm; system=linux_eabi;;
  arm*-*-openbsd*)              arch=arm; system=bsd;;
  zaurus*-*-openbsd*)           arch=arm; system=bsd;;
  x86_64-*-linux*)              arch=amd64; system=linux;;
  x86_64-*-gnu*)                arch=amd64; system=gnu;;
  x86_64-*-dragonfly*)          arch=amd64; system=dragonfly;;
  x86_64-*-freebsd*)            arch=amd64; system=freebsd;;
  x86_64-*-netbsd*)             arch=amd64; system=netbsd;;
  x86_64-*-openbsd*)            arch=amd64; system=openbsd;;
  x86_64-*-darwin*)             arch=amd64; system=macosx;;
  x86_64-*-mingw*)              arch=amd64; system=mingw;;
  aarch64-*-linux*)             arch=arm64; system=linux;;
  x86_64-*-cygwin*)             arch=amd64; system=cygwin;;
esac

# Some platforms exist both in 32-bit and 64-bit variants, not distinguished
# by $target.  Turn off native code compilation on platforms where 64-bit mode
# is not supported  (PR#4441).
# Sometimes, it's 32-bit mode that is not supported  (PR#6722).

case "$arch64,$arch,$model" in
  true,sparc,*|true,power,ppc|false,amd64,*)
      arch=none; model=default; system=unknown;;
esac

case "$native_compiler" in
    true) ;;
    false)
      arch=none; model=default; system=unknown; natdynlink=false;;
esac

if test -z "$ccoption"; then
  nativecc="$bytecc"
else
  nativecc="$ccoption"
fi

nativecccompopts="$bytecccompopts"
nativeccprivatecompopts="$byteccprivatecompopts"
nativeccprofopts=''
nativecclinkopts=''
# FIXME the naming of nativecclinkopts is broken: these are options for
# ld (for shared libs), not for cc
nativeccrpath="$byteccrpath"

case "$arch,$nativecc,$system,$model" in
  *,*,nextstep,*)      nativecclinkopts="-posix";;
  *,*,rhapsody,*)      if $arch64; then partialld="ld -r -arch ppc64"; fi;;
  alpha,gcc*,linux*,*) nativecclinkopts="-Wl,--no-relax";;
  amd64,gcc*,macosx,*) partialld="ld -r -arch x86_64";;
  amd64,gcc*,solaris,*) partialld="ld -r -m elf_x86_64";;
  power,gcc*,elf,ppc)   partialld="ld -r -m elf32ppclinux";;
  power,gcc*,elf,ppc64)   partialld="ld -r -m elf64ppc";;
  power,gcc*,elf,ppc64le)   partialld="ld -r -m elf64lppc";;
esac

asppprofflags='-DPROFILING'

case "$arch,$system" in
  amd64,macosx)   if sh ./searchpath clang; then
                      as='clang -arch x86_64 -c'
                      aspp='clang -arch x86_64 -c'
                    else
                      as="${TOOLPREF}as -arch x86_64"
                      aspp="${TOOLPREF}gcc -arch x86_64 -c"
                    fi;;
  amd64,solaris)  as="${TOOLPREF}as --64"
                  aspp="${TOOLPREF}gcc -m64 -c";;
  i386,solaris)   as="${TOOLPREF}as"
                  aspp="${TOOLPREF}gcc -c";;
  power,elf)      if $arch64; then
                    as="${TOOLPREF}as -a64 -mppc64"
                    aspp="${TOOLPREF}gcc -m64 -c"
                  else
                    as="${TOOLPREF}as -mppc"
                    aspp="${TOOLPREF}gcc -m32 -c"
                  fi;;
  s390x,elf)      as="${TOOLPREF}as -m 64 -march=$model"
                  aspp="${TOOLPREF}gcc -c -Wa,-march=$model";;
  sparc,solaris)  as="${TOOLPREF}as"
                  case "$cc" in
                    *gcc*) aspp="${TOOLPREF}gcc -c";;
                    *) aspp="${TOOLPREF}as -P";;
                  esac;;
  arm,freebsd)    as="${TOOLPREF}cc -c"
                  aspp="${TOOLPREF}cc -c";;
  *,dragonfly)    as="${TOOLPREF}as"
                  aspp="${TOOLPREF}cc -c";;
  *,freebsd)      as="${TOOLPREF}as"
                  aspp="${TOOLPREF}cc -c";;
  amd64,*|arm,*|arm64,*|i386,*|power,bsd*|sparc,*)
                  as="${TOOLPREF}as"
                  case "$ccfamily" in
                      clang-*)
                          aspp="${TOOLPREF}clang -c"
                          ;;
                      *)
                          aspp="${TOOLPREF}gcc -c"
                          ;;
                  esac;;
esac

if test -n "$asoption"; then as="$asoption"; fi
if test -n "$asppoption"; then aspp="$asppoption"; fi

cc_profile='-pg'
case "$arch,$system" in
  i386,linux_elf) profiling='true';;
  i386,gnu) profiling='true';;
  i386,bsd_elf) profiling='true';;
  amd64,macosx) profiling='true';;
  i386,macosx) profiling='true';;
  sparc,bsd) profiling='true';;
  sparc,solaris)
    profiling='true'
    case "$nativecc" in gcc*) ;; *) cc_profile='-xpg';; esac;;
  amd64,linux) profiling='true';;
  amd64,openbsd) profiling='true';;
  amd64,freebsd) profiling='true';;
  amd64,netbsd) profiling='true';;
  arm,netbsd) profiling='true';;
  amd64,gnu) profiling='true';;
  arm,linux*) profiling='true';;
  power,elf) profiling='true';;
  power,bsd*) profiling='true';;
  *) profiling='false';;
esac

# Where is ranlib?

if sh ./searchpath ${TOOLPREF}ranlib; then
  inf "ranlib found"
  echo "RANLIB=${TOOLPREF}ranlib" >> Makefile
  echo "RANLIBCMD=${TOOLPREF}ranlib" >> Makefile
else
  inf "ranlib not used"
  echo "RANLIB=${TOOLPREF}ar rs" >> Makefile
  echo "RANLIBCMD=" >> Makefile
fi

echo "ARCMD=${TOOLPREF}ar" >> Makefile


# Write the OS type (Unix or Cygwin)

echo "#define OCAML_OS_TYPE \"$ostype\"" >> s.h
echo "#define OCAML_STDLIB_DIR \"$libdir\"" >> s.h

# Do #! scripts work?

printf "#!%s\nexit 1\n" `command -v cat` > hashbang4
chmod +x hashbang4

if ( (./hashbang || ./hashbang2 || ./hashbang3 || ./hashbang4) >/dev/null); then
  inf "#! appears to work in shell scripts."
  case "$target" in
    *-*-sunos*|*-*-unicos*)
      wrn "We won't use it, though, because under SunOS and Unicos it breaks " \
          "on pathnames longer than 30 characters"
      echo "HASHBANGSCRIPTS=false" >> Makefile;;
    *-*-cygwin*)
      wrn "We won't use it, though, because of conflicts with .exe extension " \
          "under Cygwin"
      echo "HASHBANGSCRIPTS=false" >> Makefile;;
    *-*-mingw*)
      inf "We won't use it, though, because it's on the target platform " \
          "it would be used and windows doesn't support it."
      echo "HASHBANGSCRIPTS=false" >> Makefile;;
    *)
      echo "HASHBANGSCRIPTS=true" >> Makefile;;
  esac
else
  inf "No support for #! in shell scripts"
  echo "HASHBANGSCRIPTS=false" >> Makefile
fi

# Use 64-bit file offset if possible

bytecccompopts="$bytecccompopts -D_FILE_OFFSET_BITS=64"
nativecccompopts="$nativecccompopts -D_FILE_OFFSET_BITS=64"

# Check the semantics of signal handlers

if sh ./hasgot sigaction sigprocmask; then
  inf "POSIX signal handling found."
  echo "#define POSIX_SIGNALS" >> s.h
else
  if sh ./runtest signals.c; then
    inf "Signals have the BSD semantics."
    echo "#define BSD_SIGNALS" >> s.h
  else
    inf "Signals have the System V semantics."
  fi
  if sh ./hasgot sigsetmask; then
    inf "sigsetmask() found"
    echo "#define HAS_SIGSETMASK" >> s.h
  fi
fi

# For the Pervasives module

if sh ./hasgot2 -i math.h $mathlib expm1 log1p hypot copysign; then
  inf "expm1(), log1p(), hypot(), copysign() found."
  echo "#define HAS_C99_FLOAT_OPS" >> s.h
fi

# For the Sys module

if sh ./hasgot getrusage; then
  inf "getrusage() found."
  echo "#define HAS_GETRUSAGE" >> s.h
fi

if sh ./hasgot times; then
  inf "times() found."
  echo "#define HAS_TIMES" >> s.h
fi

if sh ./hasgot2 -D_GNU_SOURCE -i stdlib.h secure_getenv; then
  inf "secure_getenv() found."
  echo "#define HAS_SECURE_GETENV" >> s.h
elif sh ./hasgot2 -D_GNU_SOURCE -i stdlib.h __secure_getenv; then
  inf "__secure_getenv() found."
  echo "#define HAS___SECURE_GETENV" >> s.h
fi

if sh ./hasgot -i unistd.h issetugid; then
  inf "issetugid() found."
  echo "#define HAS_ISSETUGID" >> s.h
fi

# For the terminfo module

if test "$with_curses" = "yes"; then
  for libs in "" "-lcurses" "-ltermcap" "-lcurses -ltermcap" "-lncurses"; do
    if sh ./hasgot $libs tgetent tgetstr tgetnum tputs; then
      inf "termcap functions found (with libraries '$libs')"
      echo "#define HAS_TERMCAP" >> s.h
      curseslibs="${libs}"
      break
    fi
  done
fi

# For instrumented runtime
# (clock_gettime needs -lrt for glibc before 2.17)
if $with_instrumented_runtime; then
  with_instrumented_runtime=false #enabled it only if found
  for libs in "" "-lrt"; do
    if sh ./hasgot $libs clock_gettime; then
      inf "clock_gettime functions found (with libraries '$libs')"
      instrumented_runtime_libs="${libs}"
      with_instrumented_runtime=true;
      break
    fi
  done
  if ! $with_instrumented_runtime; then
      err "clock_gettime functions not found. " \
          "Instrumented runtime can't be built."
  fi
fi

# Configuration for the libraries

case "$system" in
  mingw) unix_or_win32="win32"; unixlib="win32unix"; graphlib="win32graph";;
  *) unix_or_win32="unix"; unixlib="unix"; graphlib="graph";;
esac

echo "UNIX_OR_WIN32=$unix_or_win32" >> Makefile
echo "UNIXLIB=$unixlib" >> Makefile
echo "GRAPHLIB=$graphlib" >> Makefile

otherlibraries="$unixlib str num dynlink bigarray"

# Spacetime profiling is only available for native code on 64-bit targets.

case "$native_compiler" in
    true)
      if $arch64; then
        otherlibraries="$otherlibraries raw_spacetime_lib"
      fi
      ;;
    *) ;;
esac

# For the Unix library

has_sockets=no
if sh ./hasgot socket socketpair bind listen accept connect; then
  inf "You have BSD sockets."
  echo "#define HAS_SOCKETS" >> s.h
  has_sockets=yes
elif sh ./hasgot -lnsl -lsocket socket socketpair bind listen accept connect
then
  inf "You have BSD sockets (with libraries '-lnsl -lsocket')"
  cclibs="$cclibs -lnsl -lsocket"
  echo "#define HAS_SOCKETS" >> s.h
  has_sockets=yes
elif sh ./hasgot -lnetwork socket socketpair bind listen accept connect; then
  echo "You have BSD sockets (with library '-lnetwork')"
  cclibs="$cclibs -lnetwork"
  echo "#define HAS_SOCKETS" >> s.h
  has_sockets=yes
else
  case "$target" in
    *-*-mingw*)
      inf "You have BSD sockets (with libraries '-lws2_32')"
      cclibs="$cclibs -lws2_32"
      echo "#define HAS_SOCKETS" >> s.h
      has_sockets=yes ;;
    *) ;;
  esac
fi

if sh ./hasgot -i sys/socket.h -t socklen_t; then
  inf "socklen_t is defined in <sys/socket.h>"
  echo "#define HAS_SOCKLEN_T" >> s.h
fi

if sh ./hasgot inet_aton; then
  inf "inet_aton() found."
  echo "#define HAS_INET_ATON" >> s.h
fi

if sh ./hasgot -i sys/types.h -i sys/socket.h -i netinet/in.h \
               -t 'struct sockaddr_in6' \
&& sh ./hasgot getaddrinfo getnameinfo inet_pton inet_ntop; then
  inf "IPv6 is supported."
  echo "#define HAS_IPV6" >> s.h
fi

if sh ./hasgot -i stdint.h; then
  inf "stdint.h found."
  echo "#define HAS_STDINT_H" >> s.h
fi

if sh ./hasgot -i unistd.h; then
  inf "unistd.h found."
  echo "#define HAS_UNISTD" >> s.h
fi

if sh ./hasgot -i sys/types.h -t off_t; then
  inf "off_t is defined in <sys/types.h>"
  echo "#define HAS_OFF_T" >> s.h
fi

if sh ./hasgot -i sys/types.h -i dirent.h; then
  inf "dirent.h found."
  echo "#define HAS_DIRENT" >> s.h
fi

if sh ./hasgot rewinddir; then
  inf "rewinddir() found."
  echo "#define HAS_REWINDDIR" >> s.h
fi

if sh ./hasgot lockf; then
  inf "lockf() found."
  echo "#define HAS_LOCKF" >> s.h
fi

if sh ./hasgot mkfifo; then
  inf "mkfifo() found."
  echo "#define HAS_MKFIFO" >> s.h
fi

if sh ./hasgot getcwd; then
  inf "getcwd() found."
  echo "#define HAS_GETCWD" >> s.h
fi

if sh ./hasgot getwd; then
  inf "getwd() found."
  echo "#define HAS_GETWD" >> s.h
fi

if sh ./hasgot getpriority setpriority; then
  inf "getpriority() found."
  echo "#define HAS_GETPRIORITY" >> s.h
fi

if sh ./hasgot -i sys/types.h -i utime.h && sh ./hasgot utime; then
  inf "utime() found."
  echo "#define HAS_UTIME" >> s.h
fi

if sh ./hasgot utimes; then
  inf "utimes() found."
  echo "#define HAS_UTIMES" >> s.h
fi

if sh ./hasgot dup2; then
  inf "dup2() found."
  echo "#define HAS_DUP2" >> s.h
fi

if sh ./hasgot fchmod fchown; then
  inf "fchmod() found."
  echo "#define HAS_FCHMOD" >> s.h
fi

if sh ./hasgot truncate ftruncate; then
  inf "truncate() found."
  echo "#define HAS_TRUNCATE" >> s.h
fi

select_include=''
if sh ./hasgot -i sys/types.h -i sys/select.h; then
  inf "sys/select.h found."
  echo "#define HAS_SYS_SELECT_H" >> s.h
  select_include='-i sys/select.h'
fi

has_select=no
if sh ./hasgot select && \
   sh ./hasgot -i sys/types.h $select_include -t fd_set ; then
  inf "select() found."
  echo "#define HAS_SELECT" >> s.h
  has_select=yes
fi

if sh ./hasgot nanosleep ; then
  inf "nanosleep() found."
  echo "#define HAS_NANOSLEEP" >> s.h
fi

if sh ./hasgot symlink readlink lstat;  then
  inf "symlink() found."
  echo "#define HAS_SYMLINK" >> s.h
fi

has_wait=no
if sh ./hasgot waitpid;  then
  inf "waitpid() found."
  echo "#define HAS_WAITPID" >> s.h
  has_wait=yes
fi

if sh ./hasgot wait4;  then
  inf "wait4() found."
  echo "#define HAS_WAIT4" >> s.h
  has_wait=yes
fi

if sh ./hasgot -i limits.h && sh ./runtest getgroups.c; then
  inf "getgroups() found."
  echo "#define HAS_GETGROUPS" >> s.h
fi

if sh ./hasgot -i limits.h -i grp.h && sh ./runtest setgroups.c; then
  inf "setgroups() found."
  echo "#define HAS_SETGROUPS" >> s.h
fi

if sh ./hasgot -i limits.h -i grp.h && sh ./runtest initgroups.c; then
  inf "initgroups() found."
  echo "#define HAS_INITGROUPS" >> s.h
fi

if sh ./hasgot -i termios.h &&
   sh ./hasgot tcgetattr tcsetattr tcsendbreak tcflush tcflow; then
  inf "POSIX termios found."
  echo "#define HAS_TERMIOS" >> s.h
fi

if sh ./runtest async_io.c; then
  inf "Asynchronous I/O are supported."
  echo "#define HAS_ASYNC_IO" >> s.h
fi

has_setitimer=no
if sh ./hasgot setitimer; then
  inf "setitimer() found."
  echo "#define HAS_SETITIMER" >> s.h
  has_setitimer="yes"
fi

if sh ./hasgot gethostname; then
  inf "gethostname() found."
  echo "#define HAS_GETHOSTNAME" >> s.h
fi

if sh ./hasgot -i sys/utsname.h && sh ./hasgot uname; then
  inf "uname() found."
  echo "#define HAS_UNAME" >> s.h
fi

has_gettimeofday=no
if sh ./hasgot gettimeofday; then
  inf "gettimeofday() found."
  echo "#define HAS_GETTIMEOFDAY" >> s.h
  has_gettimeofday="yes"
fi

if sh ./hasgot mktime; then
  inf "mktime() found."
  echo "#define HAS_MKTIME" >> s.h
fi

case "$target" in
  *-*-cygwin*) ;;  # setsid emulation under Cygwin breaks the debugger
  *) if sh ./hasgot setsid; then
       inf "setsid() found."
       echo "#define HAS_SETSID" >> s.h
     fi;;
esac

if sh ./hasgot putenv; then
  inf "putenv() found."
  echo "#define HAS_PUTENV" >> s.h
fi

if sh ./hasgot -i locale.h && sh ./hasgot setlocale; then
  inf "setlocale() and <locale.h> found."
  echo "#define HAS_LOCALE" >> s.h
fi


if sh ./hasgot $dllib dlopen; then
  inf "dlopen() found."
elif sh ./hasgot $dllib -ldl dlopen; then
  inf "dlopen() found in -ldl."
  dllib="$dllib -ldl"
else
  case "$target" in
    *-*-mingw*) ;;
    *) shared_libraries_supported=false
  esac
fi

if $shared_libraries_supported; then
  inf "Dynamic loading of shared libraries is supported."
  echo "#define SUPPORT_DYNAMIC_LINKING" >> s.h
  if $dl_needs_underscore; then
    echo '#define DL_NEEDS_UNDERSCORE' >>s.h
  fi
fi

if sh ./hasgot -i sys/types.h -i sys/mman.h && sh ./hasgot mmap munmap; then
  inf "mmap() found."
  echo "#define HAS_MMAP" >> s.h
fi

if sh ./hasgot pwrite; then
  inf "pwrite() found"
  echo "#define HAS_PWRITE" >> s.h
fi

nanosecond_stat=none
for i in 1 2 3; do
  if sh ./trycompile -DHAS_NANOSECOND_STAT=$i nanosecond_stat.c; then
      nanosecond_stat=$i; break
  fi
done
if test $nanosecond_stat != "none"; then
  inf "stat() supports nanosecond precision."
  echo "#define HAS_NANOSECOND_STAT $nanosecond_stat" >> s.h
fi

nargs=none
for i in 5 6; do
  if sh ./trycompile -DSYS_${system} -DNUM_ARGS=${i} gethostbyname.c; then
      nargs=$i;
      break;
  fi
done
if test $nargs != "none"; then
  inf "gethostbyname_r() found (with ${nargs} arguments)."
  echo "#define HAS_GETHOSTBYNAME_R $nargs" >> s.h
fi

nargs=none
for i in 7 8; do
  if sh ./trycompile -DSYS_${system} -DNUM_ARGS=${i} gethostbyaddr.c; then
      nargs=$i;
      break;
  fi
done
if test $nargs != "none"; then
  inf "gethostbyaddr_r() found (with ${nargs} arguments)."
  echo "#define HAS_GETHOSTBYADDR_R $nargs" >> s.h
fi

if sh ./hasgot mkstemp; then
  inf "mkstemp() found"
  echo "#define HAS_MKSTEMP" >> s.h
fi

if sh ./hasgot nice; then
  inf "nice() found"
  echo "#define HAS_NICE" >> s.h
fi

if sh ./hasgot dup3; then
  inf "dup3() found"
  echo "#define HAS_DUP3" >> s.h
fi

if sh ./hasgot pipe2; then
  inf "pipe2() found"
  echo "#define HAS_PIPE2" >> s.h
fi

if sh ./hasgot accept4; then
  inf "accept4() found"
  echo "#define HAS_ACCEPT4" >> s.h
fi

# Determine if the debugger is supported

if test -n "$with_debugger"; then
  if test "$has_sockets" = "yes"; then
    inf "Replay debugger supported."
    with_debugger="ocamldebugger"
  else
    inf "No replay debugger (missing system calls)"
    with_debugger=""
  fi
fi

# Determine if system stack overflows can be detected

case "$arch,$system" in
  i386,linux_elf|amd64,linux|power,rhapsody|amd64,macosx|i386,macosx \
  |amd64,openbsd|i386,bsd_elf)
    inf "System stack overflow can be detected."
    echo "#define HAS_STACK_OVERFLOW_DETECTION" >> s.h;;
  *)
    inf "Cannot detect system stack overflow.";;
esac

# Determine the target architecture for the "num" library

case "$arch" in
  i386)     bng_arch=ia32
            if sh ./trycompile ia32sse2.c
            then bng_asm_level=2
            else bng_asm_level=1
            fi;;
  power)    bng_arch=ppc; bng_asm_level=1;;
  amd64)    bng_arch=amd64; bng_asm_level=1;;
  arm64)    bng_arch=arm64; bng_asm_level=1;;
  *)        bng_arch=generic; bng_asm_level=0;;
esac

echo "BNG_ARCH=$bng_arch" >> Makefile
echo "BNG_ASM_LEVEL=$bng_asm_level" >> Makefile

# Determine if the POSIX threads library is supported

systhread_support=false

if test "$pthread_wanted" = "yes"; then
  case "$target" in
    *-*-solaris*)  pthread_link="-lpthread -lposix4"
                   pthread_caml_link="-cclib -lpthread -cclib -lposix4";;
    *-*-dragon*)   pthread_link="-pthread"
                   pthread_caml_link="-cclib -pthread";;
    *-*-freebsd*)  pthread_link="-pthread"
                   pthread_caml_link="-cclib -pthread";;
    *-*-openbsd*)  pthread_link="-pthread"
                   pthread_caml_link="-cclib -pthread";;
    *-*-haiku*)    pthread_link=""
                   pthread_caml_link="";;
    *)             pthread_link="-lpthread"
                   pthread_caml_link="-cclib -lpthread";;
  esac
  if sh ./hasgot -i pthread.h $pthread_link pthread_self; then
    inf "POSIX threads library supported."
    systhread_support=true
    otherlibraries="$otherlibraries systhreads"
    bytecccompopts="$bytecccompopts -D_REENTRANT"
    nativecccompopts="$nativecccompopts -D_REENTRANT"
    case "$target" in
      *-*-freebsd*|*-*-dragonfly*)
          bytecccompopts="$bytecccompopts -D_THREAD_SAFE"
          nativecccompopts="$nativecccompopts -D_THREAD_SAFE";;
      *-*-openbsd*)
          bytecccompopts="$bytecccompopts -pthread"
          asppflags="$asppflags -pthread"
          nativecccompopts="$nativecccompopts -pthread";;
    esac
    inf "Options for linking with POSIX threads: $pthread_link"
    if sh ./hasgot $pthread_link sigwait; then
      inf "sigwait() found"
      echo "#define HAS_SIGWAIT" >> s.h
    fi
  else
    inf "POSIX threads not found."
    pthread_link=""
  fi
else
  pthread_link=""
fi
echo "PTHREAD_LINK=$pthread_link" >> Makefile
echo "PTHREAD_CAML_LINK=$pthread_caml_link" >> Makefile

# Determine if the bytecode thread library is supported

if test "$has_select" = "yes" \
&& test "$has_setitimer" = "yes" \
&& test "$has_gettimeofday" = "yes" \
&& test "$has_wait" = "yes"; then
  inf "Bytecode threads library supported."
  otherlibraries="$otherlibraries threads"
else
  inf "Bytecode threads library not supported (missing system calls)"
fi

# Determine the location of X include files and libraries

# If the user specified -x11include and/or -x11lib, these settings
# are used. Otherwise, we check whether there is pkg-config, and take
# the flags from there. Otherwise, we search the location.

x11_include="not found"
x11_link="not found"

if test -z "$x11_include_dir" -a -z "$x11_lib_dir"; then
  if pkg-config --exists x11 2>/dev/null; then
    x11_include=`pkg-config --cflags x11`
    x11_link=`pkg-config --libs x11`
  fi
fi

if test "$x11_include" = "not found"; then
  for dir in \
    $x11_include_dir          \
                              \
    /usr/X11R7/include        \
    /usr/include/X11R7        \
    /usr/local/X11R7/include  \
    /usr/local/include/X11R7  \
    /opt/X11R7/include        \
                              \
    /usr/X11R6/include        \
    /usr/include/X11R6        \
    /usr/local/X11R6/include  \
    /usr/local/include/X11R6  \
    /opt/X11R6/include        \
                              \
    /usr/X11/include          \
    /usr/include/X11          \
    /usr/local/X11/include    \
    /usr/local/include/X11    \
    /opt/X11/include          \
                              \
    /usr/X11R5/include        \
    /usr/include/X11R5        \
    /usr/local/X11R5/include  \
    /usr/local/include/X11R5  \
    /usr/local/x11r5/include  \
    /opt/X11R5/include        \
                              \
    /usr/X11R4/include        \
    /usr/include/X11R4        \
    /usr/local/X11R4/include  \
    /usr/local/include/X11R4  \
                              \
    /usr/X386/include         \
    /usr/x386/include         \
    /usr/XFree86/include/X11  \
                              \
    /usr/include              \
    /usr/local/include        \
    /usr/unsupported/include  \
    /usr/athena/include       \
    /usr/lpp/Xamples/include  \
                              \
    /usr/openwin/include      \
    /usr/openwin/share/include \
    ; \
    do
    if test -f $dir/X11/X.h; then
      x11_include_dir=$dir
      x11_include="-I$dir"
      break
    fi
  done

  if test "$x11_include" = "not found"; then
      x11_try_lib_dir=''
  else
      x11_try_lib_dir=`echo $x11_include_dir | sed -e 's|include|lib|'`
  fi

  for dir in \
    $x11_lib_dir          \
    $x11_try_lib_dir      \
                          \
    /usr/X11R6/lib64      \
    /usr/X11R6/lib        \
    /usr/lib/X11R6        \
    /usr/local/X11R6/lib  \
    /usr/local/lib/X11R6  \
    /opt/X11R6/lib        \
                          \
    /usr/X11/lib          \
    /usr/lib/X11          \
    /usr/local/X11/lib    \
    /usr/local/lib/X11    \
    /opt/X11/lib          \
                          \
    /usr/X11R5/lib        \
    /usr/lib/X11R5        \
    /usr/local/X11R5/lib  \
    /usr/local/lib/X11R5  \
    /usr/local/x11r5/lib  \
    /opt/X11R5/lib        \
                          \
    /usr/X11R4/lib        \
    /usr/lib/X11R4        \
    /usr/local/X11R4/lib  \
    /usr/local/lib/X11R4  \
                          \
    /usr/X386/lib         \
    /usr/x386/lib         \
    /usr/XFree86/lib/X11  \
                          \
    /usr/lib64            \
    /usr/lib              \
    /usr/local/lib        \
    /usr/unsupported/lib  \
    /usr/athena/lib       \
    /usr/lpp/Xamples/lib  \
    /lib/usr/lib/X11      \
                          \
    /usr/openwin/lib      \
    /usr/openwin/share/lib    \
                              \
    /usr/lib/i386-linux-gnu   \
    /usr/lib/x86_64-linux-gnu \
    ; \
    do
    if test -f $dir/libX11.a || \
       test -f $dir/libX11.so || \
       test -f $dir/libX11.dll.a || \
       test -f $dir/libX11.dylib || \
       test -f $dir/libX11.sa; then
      if test $dir = /usr/lib; then
        x11_link="-lX11"
      else
        x11_libs="-L$dir"
        case "$target" in
          *-*-freebsd*|*-*-dragonfly*) x11_link="-L$dir -lX11";;
          *-kfreebsd*-gnu) x11_link="-L$dir -lX11";;
          *-*-*bsd*) x11_link="-R$dir -L$dir -lX11";;
          *) x11_link="-L$dir -lX11";;
        esac
      fi
      break
    fi
  done
fi

if test "x11_include" != "not found"; then
  if test "$x11_include" = "-I/usr/include"; then
    x11_include=""
  fi
  if sh ./hasgot $x11_include $x11_link -i X11/Xlib.h XrmInitialize; then
    inf "X11 works"
  else
    wrn "Cannot compile X11 program."
    x11_include="not found"
  fi
fi

has_graph=false
if test "$x11_include" = "not found" || test "$x11_link" = "not found"
then
  wrn 'X11 not found, the "graph" library will not be supported.'
  x11_include="not found"
  x11_link="not found"
else
  inf "Options for compiling for X11: $x11_include"
  inf "Options for linking with X11: $x11_link"
  if test "$graph_wanted" = yes
  then
    has_graph=true
    otherlibraries="$otherlibraries graph"
  fi
fi
echo "X11_INCLUDES=$x11_include" >> Makefile
echo "X11_LINK=$x11_link" >> Makefile

# Look for BFD library

if sh ./hasgot -DPACKAGE=ocaml -i bfd.h && \
   sh ./hasgot -DPACKAGE=ocaml -lbfd -ldl -liberty -lz bfd_openr; then
  inf "BFD library found."
  echo "#define HAS_LIBBFD" >> s.h
  echo "LIBBFD_LINK=-lbfd -ldl -liberty -lz" >> Makefile
  echo LIBBFD_INCLUDE= >>Makefile
elif sh ./hasgot -DPACKAGE=ocaml -I/opt/local/include -i bfd.h && \
     sh ./hasgot -DPACKAGE=ocaml -L/opt/local/lib -lbfd -ldl \
                 -liberty -lz -lintl bfd_openr
then
  # MacOSX with binutils from MacPorts
  inf "BFD library found."
  echo "#define HAS_LIBBFD" >> s.h
  echo "LIBBFD_LINK=-L/opt/local/lib -lbfd -ldl -liberty -lz -lintl" >> Makefile
  echo LIBBFD_INCLUDE=-I/opt/local/include >>Makefile
else
  wrn "BFD library not found, 'objinfo' will be unable to display info" \
      " on .cmxs files."
  echo "LIBBFD_LINK=" >> Makefile
  echo "LIBBFD_INCLUDE=" >> Makefile
fi

# Check whether assembler supports CFI directives

asm_cfi_supported=false

export as aspp

if ! $with_cfi; then
  echo "CFI support: disabled by command-line option -no-cfi"
elif sh ./tryassemble cfi.S; then
  echo "#define ASM_CFI_SUPPORTED" >> m.h
  asm_cfi_supported=true
  inf "Assembler supports CFI"
else
  inf "Assembler does not support CFI"
fi

if test "$with_frame_pointers" = "true"; then
  case "$target,$cc" in
    x86_64-*-linux*,gcc*|x86_64-*-linux*,clang*)
       nativecccompopts="$nativecccompopts -g  -fno-omit-frame-pointer"
       bytecccompopts="$bytecccompopts -g  -fno-omit-frame-pointer"
       nativecclinkopts="$nativecclinkopts -g"
       echo "#define WITH_FRAME_POINTERS" >> m.h
       ;;
    *) err "Unsupported architecture with frame pointers";;
  esac
fi

if $no_naked_pointers; then
  echo "#define NO_NAKED_POINTERS" >> m.h
fi

# Check for mmap support for huge pages and contiguous heap
if sh ./runtest mmap-huge.c; then
    has_huge_pages=true
    echo "#define HAS_HUGE_PAGES" >>s.h
    echo "#define HUGE_PAGE_SIZE (4 * 1024 * 1024)" >>s.h
    inf "mmap supports huge pages"
else
    has_huge_pages=false
fi

# Spacetime profiling, including libunwind detection

# The number of bits used for profiling information is configurable here.
# The more bits used for profiling, the smaller will be Max_wosize.
# Note that PROFINFO_WIDTH must still be defined even if not configuring
# for Spacetime (see comment in byterun/caml/mlvalues.h on [Profinfo_hd]).
echo "#define PROFINFO_WIDTH $profinfo_width" >> m.h
if $with_profinfo; then
    echo "#define WITH_PROFINFO" >> m.h
fi

if $with_spacetime; then
  case "$arch,$system" in
    amd64,*)
      spacetime_supported=true
      ;;
    *)
      spacetime_supported=false
      ;;
  esac
  libunwind_warning=false
  if $spacetime_supported; then
    echo "Spacetime profiling will be available."
    echo "#define WITH_SPACETIME" >> m.h
    if $disable_libunwind; then
      has_libunwind=no
      libunwind_available=false
      echo "libunwind support for Spacetime profiling was explicitly disabled."
    else
      # On Mac OS X, we always use the system libunwind.
      if test "$libunwind_lib_dir" != ""; then
        case "$arch,$system" in
          amd64,macosx)
            inf "[WARNING] -libunwind* options are ignored on Mac OS X"
            libunwind_warning=true
            libunwind_lib="-framework System"
            libunwind_lib_temp="$libunwind_lib"
            # We need unwinding information at runtime, but since we use
            # -no_compact_unwind, we also need -keep_dwarf_unwind otherwise
            # the OS X linker will chuck away the DWARF-like (.eh_frame)
            # information.  (Older versions of OS X don't provide this.)
            mkexe="$mkexe -Wl,-keep_dwarf_unwind"
            mksharedlib="$mksharedlib -Wl,-keep_dwarf_unwind"
            ;;
          *)
            libunwind_lib="-L$libunwind_lib_dir -lunwind -lunwind-x86_64"
            libunwind_lib_temp="-Xl $libunwind_lib"
            ;;
        esac
      else
        case "$arch,$system" in
          amd64,macosx)
            libunwind_lib="-framework System"
            libunwind_lib_temp="$libunwind_lib"
            mkexe="$mkexe -Wl,-keep_dwarf_unwind"
            mksharedlib="$mksharedlib -Wl,-keep_dwarf_unwind"
            ;;
          *)
            libunwind_lib="-lunwind -lunwind-x86_64"
            libunwind_lib_temp="$libunwind_lib"
            ;;
        esac
      fi
      if test "$libunwind_include_dir" != ""; then
        case "$arch,$system" in
          amd64,macosx)
            if ! $libunwind_warning; then
              inf "[WARNING] -libunwind* options are ignored on Mac OS X"
            fi
            libunwind_include=""
            ;;
          *)
            libunwind_include="-I$libunwind_include_dir"
            ;;
        esac
      else
        libunwind_include=""
      fi
      if sh ./hasgot -i libunwind.h $libunwind_lib_temp $libunwind_include; \
      then
        echo "#define HAS_LIBUNWIND" >> s.h
        has_libunwind=yes
        libunwind_available=true
        echo "libunwind support for Spacetime profiling will be available."
      else
        has_libunwind=no
        libunwind_available=false
        echo "libunwind support for Spacetime profiling will not be available."
      fi
    fi
  else
    echo "Spacetime profiling is not available on 32-bit platforms."
    with_spacetime=false
    libunwind_available=false
    has_libunwind=no
  fi
fi

if ! $shared_libraries_supported; then
  with_cplugins=false
fi

if $with_fpic; then
  bytecccompopts="$bytecccompopts $sharedcccompopts"
  nativecccompopts="$nativecccompopts $sharedcccompopts"
  aspp="$aspp $sharedcccompopts"
fi


if $with_cplugins; then
  echo "#define CAML_WITH_CPLUGINS" >> m.h
fi

if $with_fpic; then
  echo "#define CAML_WITH_FPIC" >> m.h
fi

# Finish generated files

cclibs="$cclibs $mathlib"

echo "BYTECC=$bytecc $bytecccompopts" >> Makefile
echo "BYTECODE_C_COMPILER=$bytecc $bytecccompopts $sharedcccompopts" \
  >> Makefile
echo "BYTECCCOMPOPTS=$byteccprivatecompopts" >> Makefile
echo "BYTECCLINKOPTS=$bytecclinkopts" >> Makefile
echo "BYTECCLIBS=$cclibs $dllib $curseslibs $pthread_link \
                 $instrumented_runtime_libs" >> Makefile
echo "BYTECCRPATH=$byteccrpath" >> Makefile
echo "EXE=$exe" >> Makefile
echo "SUPPORTS_SHARED_LIBRARIES=$shared_libraries_supported" >> Makefile
echo "SHAREDCCCOMPOPTS=$sharedcccompopts" >> Makefile
echo "MKSHAREDLIBRPATH=$mksharedlibrpath" >> Makefile
echo "NATDYNLINKOPTS=$natdynlinkopts" >> Makefile
cat >> Makefile <<EOF
SYSLIB=-l\$(1)
#ml let syslib x = "-l"^x;;

### How to build a static library
MKLIB=${TOOLPREF}ar rc \$(1) \$(2); ${TOOLPREF}ranlib \$(1)
#ml let mklib out files opts =      (* "" *)
#ml   Printf.sprintf "${TOOLPREF}ar rc %s %s %s; ${TOOLPREF}ranlib %s"
#ml                  out opts files out;;
EOF
echo "ARCH=$arch" >> Makefile
echo "MODEL=$model" >> Makefile
echo "SYSTEM=$system" >> Makefile
echo "NATIVECC=$nativecc $nativecccompopts" >> Makefile
echo "NATIVE_C_COMPILER=$nativecc $nativecccompopts" >> Makefile
echo "NATIVECCCOMPOPTS=$nativeccprivatecompopts" >> Makefile
echo "NATIVECCPROFOPTS=$nativeccprofopts" >> Makefile
echo "NATIVECCLINKOPTS=$nativecclinkopts" >> Makefile
echo "NATIVECCRPATH=$nativeccrpath" >> Makefile
echo "NATIVECCLIBS=$cclibs $dllib" >> Makefile
echo "ASM=$as" >> Makefile
echo "ASPP=$aspp" >> Makefile
echo "ASPPPROFFLAGS=$asppprofflags" >> Makefile
echo "PROFILING=$profiling" >> Makefile
echo "DYNLINKOPTS=$dllib" >> Makefile
echo "OTHERLIBRARIES=$otherlibraries" >> Makefile
echo "CC_PROFILE=$cc_profile" >> Makefile
echo "SYSTHREAD_SUPPORT=$systhread_support" >> Makefile
echo "PACKLD=$partialld $(echo $nativecclinkopts|sed s/-Wl,//g) -o\\ " >> Makefile
echo "IFLEXDIR=$iflexdir" >> Makefile
echo "O=o" >> Makefile
echo "A=a" >> Makefile
echo "SO=$SO" >> Makefile
echo "EXT_OBJ=.o" >> Makefile
echo "EXT_ASM=.s" >> Makefile
echo "EXT_LIB=.a" >> Makefile
echo "EXT_DLL=.$SO" >> Makefile
echo "EXTRALIBS=" >> Makefile
echo "CCOMPTYPE=cc" >> Makefile
echo "TOOLCHAIN=$TOOLCHAIN" >> Makefile
echo "NATDYNLINK=$natdynlink" >> Makefile
echo "CMXS=$cmxs" >> Makefile
echo "MKEXE=$mkexe" >> Makefile
echo "MKEXEDEBUGFLAG=$mkexedebugflag" >> Makefile
echo "MKDLL=$mksharedlib" >> Makefile
echo "MKMAINDLL=$mkmaindll" >> Makefile
echo "RUNTIMED=${debugruntime}" >>Makefile
echo "RUNTIMEI=${with_instrumented_runtime}" >>Makefile
echo "WITH_DEBUGGER=${with_debugger}" >>Makefile
echo "WITH_OCAMLDOC=${with_ocamldoc}" >>Makefile
echo "ASM_CFI_SUPPORTED=$asm_cfi_supported" >> Makefile
echo "WITH_FRAME_POINTERS=$with_frame_pointers" >> Makefile
echo "WITH_SPACETIME=$with_spacetime" >> Makefile
echo "WITH_PROFINFO=$with_profinfo" >> Makefile
echo "LIBUNWIND_AVAILABLE=$libunwind_available" >> Makefile
echo "LIBUNWIND_INCLUDE_FLAGS=$libunwind_include" >> Makefile
echo "LIBUNWIND_LINK_FLAGS=$libunwind_lib" >> Makefile
echo "PROFINFO_WIDTH=$profinfo_width" >> Makefile
echo "WITH_CPLUGINS=$with_cplugins" >> Makefile
echo "WITH_FPIC=$with_fpic" >> Makefile
echo "TARGET=$target" >> Makefile
echo "HOST=$host" >> Makefile
if [ "$ostype" = Cygwin ]; then
  echo "DIFF=diff -q --strip-trailing-cr" >>Makefile
fi
echo "FLAMBDA=$flambda" >> Makefile
echo "SAFE_STRING=$safe_string" >> Makefile
echo "AFL_INSTRUMENT=$afl_instrument" >> Makefile
echo "MAX_TESTSUITE_DIR_RETRIES=$max_testsuite_dir_retries" >> Makefile


rm -f tst hasgot.c
rm -f ../m.h ../s.h ../Makefile
mv m.h s.h Makefile ..

# Print a summary

inf
inf "** Configuration summary **"
inf
inf "Directories where OCaml will be installed:"
inf "        binaries.................. $bindir"
inf "        standard library.......... $libdir"
inf "        manual pages.............. $mandir (with extension .$manext)"

inf "Configuration for the bytecode compiler:"
inf "        C compiler used........... $bytecc"
inf "        options for compiling..... $bytecccompopts"
inf "        options for linking....... $bytecclinkopts $cclibs $dllib" \
                                       "$curseslibs $pthread_link"
if $shared_libraries_supported; then
inf "        shared libraries are supported"
inf "        options for compiling..... $sharedcccompopts $bytecccompopts"
inf "        command for building...... $mksharedlib -o lib.so" \
                                       "$mksharedlibrpath/a/path objs"
else
inf "        shared libraries not supported"
fi

inf "Configuration for the native-code compiler:"
if test "$arch" = "none"; then
  inf "        (not supported on this platform)"
else
  if test "$model" = "default"; then
    inf "        hardware architecture..... $arch"
  else
    inf "        hardware architecture..... $arch ($model)"
  fi
  if test "$system" = "unknown"; then : ; else
  inf "        OS variant................ $system"
  fi
  inf "        C compiler used........... $nativecc"
  inf "        options for compiling..... $nativecccompopts"
  inf "        options for linking....... $nativecclinkopts $cclibs"
  inf "        assembler ................ $as"
  inf "        preprocessed assembler ... $aspp"
  if test "$asm_cfi_supported" = "true"; then
  inf "        assembler supports CFI ... yes"
  else
  inf "        assembler supports CFI ... no"
  fi
  if test "$with_frame_pointers" = "true"; then
  inf "        with frame pointers....... yes"
  else
  inf "        with frame pointers....... no"
  fi
  if $no_naked_pointers; then
  inf "        naked pointers forbidden.. yes"
  else
  inf "        naked pointers forbidden.. no"
  fi
  if $with_spacetime; then
    inf "        spacetime profiling....... yes"
    inf "          ... with libunwind...... $has_libunwind"
  else
    inf "        spacetime profiling....... no"
  fi
  if $with_profinfo; then
    inf "        reserved bits in header... $profinfo_width"
  else
    inf "        reserved bits in header... no"
  fi
  case "$arch,$system" in
    amd64,macosx)
      ;;
    amd64,*)
      if test "$has_libunwind" = "yes"; then
        if test "$libunwind_include_dir" != ""; then
          inf "        libunwind include dir..... $libunwind_include_dir"
        fi
        if test "$libunwind_lib_dir" != ""; then
          inf "        libunwind library dir..... $libunwind_lib_dir"
        fi
      fi
      ;;
    *)
      ;;
  esac
  if $with_cplugins; then
  inf "        C plugins................. yes"
  else
  inf "        C plugins................. no"
  fi
  if $with_fpic; then
  inf "        compile with -fPIC........ yes"
  else
  inf "        compile with -fPIC........ no"
  fi
  inf "        native dynlink ........... $natdynlink"
  if $profiling; then
  inf "        profiling with gprof ..... supported"
  else
  inf "        profiling with gprof ..... not supported"
  fi
  if test "$flambda" = "true"; then
  inf "        using flambda middle-end . yes"
  else
  inf "        using flambda middle-end . no"
  fi
  if test "$safe_string" = "true"; then
  inf "        safe strings ............. yes"
  else
  inf "        safe strings ............. no"
  fi
  if test "$afl_instrument" = "true"; then
  inf "        afl-fuzz always enabled .. yes"
  else
  inf "        afl-fuzz always enabled .. no"
  fi
fi

if test "$with_debugger" = "ocamldebugger"; then
  inf "Source-level replay debugger: supported"
else
  inf "Source-level replay debugger: not supported"
fi

if $debugruntime; then
  inf "Debug runtime will be compiled and installed"
fi

if $with_instrumented_runtime; then
  inf "Instrumented runtime will be compiled and installed"
fi

inf "Additional libraries supported:"
inf "        $otherlibraries"

inf "Configuration for the \"num\" library:"
inf "        target architecture ...... $bng_arch (asm level $bng_asm_level)"

if $has_graph; then
inf "Configuration for the \"graph\" library:"
inf "        options for compiling .... $x11_include"
inf "        options for linking ...... $x11_link"
else
inf "The \"graph\" library: not supported"
fi

inf
inf "** OCaml configuration completed successfully **"
inf

if test ! -z "$MACOSX_DEPLOYMENT_TARGET"; then
  wrn "The environment variable MACOSX_DEPLOYMENT_TARGET is set.\n" \
      "This will probably prevent compiling the OCaml system."
fi