summaryrefslogtreecommitdiff
path: root/configure
blob: 511f473b814d33b624083c1ff2e3b8ddb4703aa8 (plain)
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
#! /bin/sh

#########################################################################
#                                                                       #
#                            Objective Caml                             #
#                                                                       #
#            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 Library General Public License, with     #
#   the special exception on linking described in file LICENSE.         #
#                                                                       #
#########################################################################

# $Id$

configure_options="$*"
prefix=/usr/local
bindir=''
libdir=''
mandir=''
manext=1
host_type=unknown
ccoption=''
asoption=''
asppoption=''
cclibs=''
curseslibs=''
mathlib='-lm'
dllib=''
x11_include_dir=''
x11_lib_dir=''
tk_wanted=no
pthread_wanted=yes
tk_defs=''
tk_libs=''
tk_x11=yes
dl_defs=''
verbose=no
withcurses=yes
withsharedlibs=yes
gcc_warnings="-Wall"
partialld="ld -r"
ocamlc=''

# 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

# Parse command-line arguments

while : ; do
  case "$1" in
    "") break;;
    -prefix|--prefix)
        prefix=$2; shift;;
    -bindir|--bindir)
        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;;
    -ocamlc)
        ocamlc=$2; shift;;
    -ocamllib)
        ocamllib=$2; shift;;
    -host*|--host*)
        host_type=$2; shift;;
    -cc*)
        ccoption="$2"; shift;;
    -as)
        asoption="$2"; shift;;
    -aspp)
        asppoption="$2"; shift;;
    -lib*)
        cclibs="$2 $cclibs"; shift;;
    -dldefs*|--dldefs*)
         dl_defs="$2"; shift;;
    -no-curses)
        withcurses=no;;
    -no-shared-libs)
        withsharedlibs=no;;
    -x11include*|--x11include*)
        x11_include_dir=$2; shift;;
    -x11lib*|--x11lib*)
        x11_lib_dir=$2; shift;;
    -dllibs*|--dllibs*)
        dllib="$2"; shift;;
    -verbose|--verbose)
        verbose=yes;;
    *) echo "Unknown option \"$1\"." 1>&2; exit 2;;
  esac
  shift
done

# Sanity checks

case "$prefix" in
  /*) ;;
   *) echo "The -prefix directory must be absolute." 1>&2; exit 2;;
esac
case "$bindir" in
  /*) ;;
  "") ;;
   *) echo "The -bindir directory must be absolute." 1>&2; exit 2;;
esac
case "$libdir" in
  /*) ;;
  "") ;;
   *) echo "The -libdir directory must be absolute." 1>&2; exit 2;;
esac
case "$mandir" in
  /*) ;;
  "") ;;
   *) echo "The -mandir directory must be absolute." 1>&2; exit 2;;
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
# To use ocamlc compiler for bootstrap: NOJOIN=
echo "NOJOIN=-nojoin" >> Makefile
# Where to install

echo "PREFIX=$prefix" >> Makefile
case "$bindir" in
  "") echo 'BINDIR=$(PREFIX)/bin' >> Makefile
      bindir="$prefix/bin";;
   *) echo "BINDIR=$bindir" >> Makefile;;
esac
case "$libdir" in
  "") echo 'LIBDIR=$(PREFIX)/lib/jocaml' >> Makefile
      libdir="$prefix/lib/jocaml";;
   *) 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

# Companion ocaml
if test -z "$ocamllib"
then
  case "$ocamlc" in
   "")
     if sh ./searchpath ocamlc
     then
       ocamlc="ocamlc"
     fi
     ;;
   *)
     if test ! -e $ocamlc
     then
       ocamlc=''
     fi
  esac
  has_ocaml=false
  jocaml_version=`head -1 ../../VERSION`
  case "$ocamlc" in
   "") echo "Warning: could not find ocamlc";;
   *)
     ocaml_version=`$ocamlc -version`
     if test "$jocaml_version" = "$ocaml_version"
     then
       has_ocaml=true
       ocamllib=`$ocamlc -where`
     else
       echo "Warning: Version mismatch ocaml is $ocaml_version, jocaml is $jocaml_version"
     fi
   ;;
  esac
else
  has_ocaml=true
  ocaml_version=unchecked
fi
if $has_ocaml
then
  echo OCAMLLIB=$ocamllib >> Makefile
fi

# Determine the system type

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

# Do we have gcc?

if test -z "$ccoption"; then
  if sh ./searchpath gcc; then
    echo "gcc found"
    cc=gcc
  else
    cc=cc
  fi
else
  cc="$ccoption"
fi

# Check for buggy versions of GCC

buggycc="no"

case "$host,$cc" in
  i[3456]86-*-*,gcc*)
    case `$cc --version` in
      2.7.2.1) cat <<'EOF'

WARNING: you are using gcc version 2.7.2.1 on an Intel x86 processor.
This version of gcc is known to generate incorrect code for the
Objective Caml runtime system on some Intel x86 machines. (The symptom
is a crash of boot/ocamlc when compiling stdlib/pervasives.mli.)
In particular, the version of gcc 2.7.2.1 that comes with
Linux RedHat 4.x / Intel is affected by this problem.
Other Linux distributions might also be affected.
If you are using one of these configurations, you are strongly advised
to use another version of gcc, such as 2.95, which are
known to work well with Objective Caml.

Press <enter> to proceed or <interrupt> to stop.
EOF
        read reply;;
      2.96*) cat <<'EOF'

WARNING: you are using gcc version 2.96 on an Intel x86 processor.
Certain patched versions of gcc 2.96 are known to generate incorrect
code for the Objective Caml runtime system.  (The symptom is a segmentation
violation on boot/ocamlc.)  Those incorrectly patched versions can be found
in RedHat 7.2 and Mandrake 8.0 and 8.1; other Linux distributions
might also be affected.  (See bug #57760 on bugzilla.redhat.com)

Auto-configuration will now select gcc compiler flags that work around
the problem.  Still, if you observe segmentation faults while running
ocamlc or ocamlopt, you are advised to try another version of gcc,
such as 2.95.3 or 3.2.

EOF
        buggycc="gcc.2.96";;

            esac;;
esac

# Configure the bytecode compiler

bytecc="$cc"
mkexe="\$(BYTECC)"
bytecccompopts=""
bytecclinkopts=""
dllccompopts=""
ostype="Unix"
exe=""
iflexdir=""

case "$bytecc,$host" in
  cc,*-*-nextstep*)
    # GNU C extensions disabled, but __GNUC__ still defined!
    bytecccompopts="-fno-defer-pop $gcc_warnings -U__GNUC__ -posix"
    bytecclinkopts="-posix";;
  *,*-*-rhapsody*)
    # Almost the same as NeXTStep
    bytecccompopts="-fno-defer-pop $gcc_warnings -DSHRINKED_GNUC"
    mathlib="";;
  *,*-*-darwin*)
    bytecccompopts="-fno-defer-pop -no-cpp-precomp $gcc_warnings"
    mathlib=""
    # 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;;
  *,*-*-beos*)
    bytecccompopts="-fno-defer-pop $gcc_warnings"
    # No -lm library
    mathlib="";;
  gcc,alpha*-*-osf*)
    bytecccompopts="-fno-defer-pop $gcc_warnings"
    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;;
  cc,mips-*-irix6*)
    # Add -n32 flag to ensure compatibility with native-code compiler
    bytecccompopts="-n32"
    # Turn off warning "unused library"
    bytecclinkopts="-n32 -Wl,-woff,84";;
  cc*,mips-*-irix6*)
    # (For those who want to force "cc -64")
    # Turn off warning "unused library"
    bytecclinkopts="-Wl,-woff,84";;
  *,alpha*-*-unicos*)
    # For the Cray T3E
    bytecccompopts="-DUMK";;
  gcc*,powerpc-*-aix*)
    # Avoid name-space pollution by requiring Unix98-conformant includes
    bytecccompopts="-fno-defer-pop $gcc_warnings -D_XOPEN_SOURCE=500";;
  *,powerpc-*-aix*)
    bytecccompopts="-D_XOPEN_SOURCE=500";;
  gcc*,*-*-cygwin*)
    bytecccompopts="-fno-defer-pop $gcc_warnings -U_WIN32"
    dllccompopts="-D_WIN32 -DCAML_DLL"
    if test $withsharedlibs = yes; then
      flexlink="flexlink -chain cygwin -merge-manifest"
      flexdir=`$flexlink -where | dos2unix`
      if test -z "$flexdir"; then
        echo "flexlink not found: native shared libraries won't be available"
        withsharedlibs=no
      else
        iflexdir="-I\"$flexdir\""
        mkexe="$flexlink -exe"
      fi
    fi
    exe=".exe"
    ostype="Cygwin";;
  gcc*,x86_64-*-linux*)
    bytecccompopts="-fno-defer-pop $gcc_warnings"
    # 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;;
  gcc*)
    bytecccompopts="-fno-defer-pop $gcc_warnings";;
esac

# Configure compiler to use in further tests

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

# Check C compiler

sh ./runtest ansi.c
case $? in
  0) echo "The C compiler is ANSI-compliant.";;
  1) echo "The C compiler $cc is not ANSI-compliant."
     echo "You need an ANSI C compiler to build Objective Caml."
     exit 2;;
  *) echo "Unable to compile the test program."
     echo "Make sure the C compiler $cc is properly installed."
     exit 2;;
esac

# Check the sizes of data types

echo "Checking the sizes of integers and pointers..."
set `sh ./runtest sizes.c`
case "$2,$3" in
  4,4) echo "OK, this is a regular 32 bit architecture."
       echo "#undef ARCH_SIXTYFOUR" >> m.h
       arch64=false;;
  *,8) echo "Wow! A 64 bit architecture!"
       echo "#define ARCH_SIXTYFOUR" >> m.h
       arch64=true;;
  *,*) echo "This architecture seems to be neither 32 bits nor 64 bits."
       echo "Objective Caml won't run on this architecture."
       exit 2;;
    *) echo "Unable to compile the test program."
       echo "Make sure the C compiler $cc is properly installed."
       exit 2;;
esac
if test $1 != 4 && test $2 != 4 && test $4 != 4; then
  echo "Sorry, we can't find a 32-bit integer type"
  echo "(sizeof(short) = $4, sizeof(int) = $1, sizeof(long) = $2)"
  echo "Objective Caml won't run on this architecture."
  exit 2
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

if test $2 = 8; then
     echo "#define ARCH_INT64_TYPE long" >> m.h
     echo "#define ARCH_UINT64_TYPE unsigned long" >> m.h
     echo '#define ARCH_INT64_PRINTF_FORMAT "l"' >> m.h
     int64_native=true
else
  sh ./runtest longlong.c
  case $? in
  0) echo "64-bit \"long long\" integer type found (printf with \"%ll\")."
     echo "#define ARCH_INT64_TYPE long long" >> m.h
     echo "#define ARCH_UINT64_TYPE unsigned long long" >> m.h
     echo '#define ARCH_INT64_PRINTF_FORMAT "ll"' >> m.h
     int64_native=true;;
  1) echo "64-bit \"long long\" integer type found (printf with \"%q\")."
     echo "#define ARCH_INT64_TYPE long long" >> m.h
     echo "#define ARCH_UINT64_TYPE unsigned long long" >> m.h
     echo '#define ARCH_INT64_PRINTF_FORMAT "q"' >> m.h
     int64_native=true;;
  2) echo "64-bit \"long long\" integer type found (but no printf)."
     echo "#define ARCH_INT64_TYPE long long" >> m.h
     echo "#define ARCH_UINT64_TYPE unsigned long long" >> m.h
     echo '#undef ARCH_INT64_PRINTF_FORMAT' >> m.h
     int64_native=true;;
  *) echo "No suitable 64-bit integer type found, will use software emulation."
     echo "#undef ARCH_INT64_TYPE" >> m.h
     echo "#undef ARCH_UINT64_TYPE" >> m.h
     echo '#undef ARCH_INT64_PRINTF_FORMAT' >> m.h
     int64_native=false;;
  esac
fi

if test $3 = 8 && test $int64_native = false; then
  echo "This architecture has 64-bit pointers but no 64-bit integer type."
  echo "Objective Caml won't run on this architecture."
  exit 2
fi

# Determine endianness

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

# Determine alignment constraints

case "$host" in
  sparc*-*-*|hppa*-*-*)
    # On Sparc V9 with certain versions of gcc, determination of double
    # alignment is not reliable (PR#1521), hence force it.
    # Same goes for hppa.
    # 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
      echo "Doubles can be word-aligned."
      echo "#undef ARCH_ALIGN_DOUBLE" >> m.h
    else
      echo "Doubles must be doubleword-aligned."
      echo "#define ARCH_ALIGN_DOUBLE" >> m.h
    fi;;
  *)
    sh ./runtest dblalign.c
    case $? in
      0) echo "Doubles can be word-aligned."
         echo "#undef ARCH_ALIGN_DOUBLE" >> m.h;;
      1) echo "Doubles must be doubleword-aligned."
         echo "#define ARCH_ALIGN_DOUBLE" >> m.h;;
      *) echo "Something went wrong during alignment determination for doubles."
         echo "I'm going to assume this architecture has alignment constraints over doubles."
         echo "That's a safe bet: Objective Caml will work even if"
         echo "this architecture has actually no alignment constraints."
         echo "#define ARCH_ALIGN_DOUBLE" >> m.h;;
    esac;;
esac

if $int64_native; then
  case "$host" in
    sparc*-*-*|hppa*-*-*)
      if test $2 = 8; then
        echo "64-bit integers can be word-aligned."
        echo "#undef ARCH_ALIGN_INT64" >> m.h
      else
        echo "64-bit integers must be doubleword-aligned."
        echo "#define ARCH_ALIGN_INT64" >> m.h
      fi;;
    *)
      sh ./runtest int64align.c
      case $? in
        0) echo "64-bit integers can be word-aligned."
           echo "#undef ARCH_ALIGN_INT64" >> m.h;;
        1) echo "64-bit integers must be doubleword-aligned."
           echo "#define ARCH_ALIGN_INT64" >> m.h;;
        *) echo "Something went wrong during alignment determination for 64-bit integers."
           echo "I'm going to assume this architecture has alignment constraints."
           echo "That's a safe bet: Objective Caml will work even if"
           echo "this architecture has actually no alignment constraints."
           echo "#define ARCH_ALIGN_INT64" >> m.h;;
      esac
  esac
else
  echo "#undef ARCH_ALIGN_INT64" >> m.h
fi

# Check semantics of division and modulus

sh ./runtest divmod.c
case $? in
  0) echo "Native division and modulus have round-towards-zero semantics, will use them."
     echo "#undef NONSTANDARD_DIV_MOD" >> m.h;;
  1) echo "Native division and modulus do not have round-towards-zero semantics, will use software emulation."
     echo "#define NONSTANDARD_DIV_MOD" >> m.h;;
  *) echo "Something went wrong while checking native division and modulus, please report it."
     echo "#define NONSTANDARD_DIV_MOD" >> m.h;;
esac

# Shared library support

shared_libraries_supported=false
dl_needs_underscore=false
sharedcccompopts=''
mksharedlib=''
byteccrpath=''
mksharedlibrpath=''
natdynlinkopts=""
cmxs="cmxa"

if test $withsharedlibs = "yes"; then
  case "$host" in
    *-*-cygwin*)
      cmxs="cmxs"
      mksharedlib="$flexlink"
      mkmaindll="$flexlink -maindll"
      shared_libraries_supported=true;;
    *-*-linux-gnu|*-*-linux|*-*-freebsd[3-9]*|*-*-openbsd*|*-*-netbsd*|*-*-gnu*)
      cmxs="cmxs"
      sharedcccompopts="-fPIC"
      mksharedlib="$bytecc -shared"
      bytecclinkopts="$bytecclinkopts -Wl,-E"
      byteccrpath="-Wl,-rpath,"
      mksharedlibrpath="-Wl,-rpath,"
      natdynlinkopts="-Wl,-E"
      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="$bytecc -shared"
            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*)
      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"
      bytecccompopts="$dl_defs $bytecccompopts"
      dl_needs_underscore=false
      shared_libraries_supported=true;;
    m88k-*-openbsd*)
      shared_libraries_supported=false;;
    vax-*-openbsd*)
      shared_libraries_supported=false;;
    *-*-openbsd*)
      sharedcccompopts="-fPIC"
      mksharedlib="$bytecc -shared"
      bytecclinkopts="$bytecclinkopts -Wl,-E"
      natdynlinkopts="-Wl,-E"
      byteccrpath="-Wl,-rpath,"
      mksharedlibrpath="-Wl,-rpath,"
      shared_libraries_supported=true;;
  esac
fi

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

# Configure the native-code compiler

arch=none
model=default
system=unknown

case "$host" in
  alpha*-*-osf*)                arch=alpha; system=digital;;
  alpha*-*-linux*)              arch=alpha; system=linux;;
  alpha*-*-gnu*)                arch=alpha; system=gnu;;
  alpha*-*-freebsd*)            arch=alpha; system=freebsd;;
  alpha*-*-netbsd*)             arch=alpha; system=netbsd;;
  alpha*-*-openbsd*)            arch=alpha; system=openbsd;;
  sparc*-*-sunos4.*)            arch=sparc; system=sunos;;
  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-*-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;;
  mips-*-irix6*)                arch=mips; system=irix;;
  hppa1.1-*-hpux*)              arch=hppa; system=hpux;;
  hppa2.0*-*-hpux*)             arch=hppa; system=hpux;;
  hppa*-*-linux*)               arch=hppa; system=linux;;
  hppa*-*-gnu*)                 arch=hppa; system=gnu;;
  powerpc*-*-linux*)            arch=power; model=ppc; system=elf;;
  powerpc-*-netbsd*)            arch=power; model=ppc; system=elf;;
  powerpc-*-rhapsody*)          arch=power; model=ppc; system=rhapsody;;
  powerpc-*-darwin*)            arch=power; system=rhapsody
                                if $arch64; then model=ppc64; else model=ppc; fi;;
  arm*-*-linux*)                arch=arm; system=linux;;
  arm*-*-gnu*)                  arch=arm; system=gnu;;
  ia64-*-linux*)                arch=ia64; system=linux;;
  ia64-*-gnu*)                  arch=ia64; system=gnu;;
  ia64-*-freebsd*)              arch=ia64; system=freebsd;;
  x86_64-*-linux*)              arch=amd64; system=linux;;
  x86_64-*-gnu*)                arch=amd64; system=gnu;;
  x86_64-*-freebsd*)            arch=amd64; system=freebsd;;
  x86_64-*-netbsd*)             arch=amd64; system=netbsd;;
  x86_64-*-openbsd*)            arch=amd64; system=openbsd;;
  x86_64-*-darwin9.5)           arch=amd64; system=macosx;;
esac

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

if $arch64; then
  case "$arch,$model" in
    sparc,default|mips,default|hppa,default|power,ppc)
      arch=none; model=default; system=unknown;;
  esac
fi

if test -z "$ccoption"; then
  case "$arch,$system,$cc" in
    alpha,digital,gcc*) nativecc=cc;;
    mips,*,gcc*) nativecc=cc;;
    *) nativecc="$bytecc";;
  esac
else
  nativecc="$ccoption"
fi

nativecccompopts=''
nativecclinkopts=''
nativeccrpath="$byteccrpath"

case "$arch,$nativecc,$system,$host_type" in
  alpha,cc*,digital,*) nativecccompopts=-std1;;
  mips,cc*,irix,*)     nativecccompopts=-n32
                       nativecclinkopts="-n32 -Wl,-woff,84";;
  *,*,nextstep,*)      nativecccompopts="$gcc_warnings -U__GNUC__ -posix"
                       nativecclinkopts="-posix";;
  *,*,rhapsody,*darwin[1-5].*)
                       nativecccompopts="$gcc_warnings -DSHRINKED_GNUC";;
  *,*,rhapsody,*)      nativecccompopts="$gcc_warnings -DDARWIN_VERSION_6 $dl_defs"
                       if $arch64; then partialld="ld -r -arch ppc64"; fi;;
  *,gcc*,cygwin,*)     nativecccompopts="$gcc_warnings -U_WIN32";;
  amd64,gcc*,macosx,*) partialld="ld -r -arch x86_64";;
  amd64,gcc*,solaris,*) partialld="ld -r -m elf_x86_64";;
  *,gcc*,*,*)          nativecccompopts="$gcc_warnings";;
esac

asppprofflags='-DPROFILING'

case "$arch,$model,$system" in
  alpha,*,digital)  as='as -O2 -nocpp'
                    aspp='as -O2'
                    asppprofflags='-pg -DPROFILING';;
  alpha,*,*)        as='as'
                    aspp='gcc -c';;
  amd64,*,macosx)   as='as -arch x86_64'
                    aspp='gcc -arch x86_64 -c';;
  amd64,*,solaris)  as='as --64'
                    aspp='gcc -m64 -c';;
  amd64,*,*)        as='as'
                    aspp='gcc -c';;
  arm,*,*)          as='as';
                    aspp='gcc -c';;
  hppa,*,*)         as='as';
                    aspp='gcc -traditional -c';;
  i386,*,solaris)   as='as'
                    aspp='/usr/ccs/bin/as -P';;
  i386,*,*)         as='as'
                    aspp='gcc -c';;
  ia64,*,*)         as='as -xexplicit'
                    aspp='gcc -c -Wa,-xexplicit';;
  mips,*,irix)      as='as -n32 -O2 -nocpp -g0'
                    aspp='as -n32 -O2';;
  power,*,elf)      as='as -u -m ppc'
                    aspp='gcc -c';;
  power,*,bsd)      as='as'
                    aspp='gcc -c';;
  power,*,rhapsody) as="as -arch $model"
                    aspp="$bytecc -c";;
  sparc,*,solaris)  as='as'
                    case "$cc" in
                      gcc*) aspp='gcc -c';;
                         *) aspp='as -P';;
                    esac;;
  sparc,*,*)        as='as'
                    aspp='gcc -c';;
esac

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

cc_profile='-pg'
case "$arch,$model,$system" in
  alpha,*,digital) profiling='prof';;
  i386,*,linux_elf) profiling='prof';;
  i386,*,gnu) profiling='prof';;
  i386,*,bsd_elf) profiling='prof';;
  amd64,*,macosx) profiling='prof';;
  i386,*,macosx) profiling='prof';;
  sparc,*,solaris)
    profiling='prof'
    case "$nativecc" in gcc*) ;; *) cc_profile='-xpg';; esac;;
  amd64,*,linux) profiling='prof';;
  amd64,*,gnu) profiling='prof';;
  *) profiling='noprof';;
esac

# Where is ranlib?

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

# Do #! scripts work?

if (SHELL=/bin/sh; export SHELL; (./sharpbang || ./sharpbang2) >/dev/null); then
  echo "#! appears to work in shell scripts"
  case "$host" in
    *-*-sunos*|*-*-unicos*)
      echo "We won't use it, though, because under SunOS and Unicos it breaks"
      echo "on pathnames longer than 30 characters"
      echo "SHARPBANGSCRIPTS=false" >> Makefile;;
    *-*-cygwin*)
      echo "We won't use it, though, because of conflicts with .exe extension"
      echo "under Cygwin"
      echo "SHARPBANGSCRIPTS=false" >> Makefile;;
    *)
      echo "SHARPBANGSCRIPTS=true" >> Makefile;;
  esac
else
  echo "No support for #! in shell scripts"
  echo "SHARPBANGSCRIPTS=false" >> Makefile
fi

# Write the OS type (Unix or Cygwin)

echo "#define OCAML_OS_TYPE \"$ostype\"" >> s.h
echo "#define OCAML_STDLIB_DIR \"$libdir\"" >> s.h
if $has_ocaml
then
echo "#define OCAML_LIB \"$ocamllib\"" >> s.h
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
  echo "POSIX signal handling found."
  echo "#define POSIX_SIGNALS" >> s.h
else
  if sh ./runtest signals.c; then
    echo "Signals have the BSD semantics."
    echo "#define BSD_SIGNALS" >> s.h
  else
    echo "Signals have the System V semantics."
  fi
  if sh ./hasgot sigsetmask; then
    echo "sigsetmask() found"
    echo "#define HAS_SIGSETMASK" >> s.h
  fi
fi

# For the sys module

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

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

# For the terminfo module

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

# Configuration for the libraries

otherlibraries="unix dynlink"

# For the Unix library

has_sockets=no
if sh ./hasgot socket socketpair bind listen accept connect; then
  echo "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
  echo "You have BSD sockets (with libraries '-lnsl -lsocket')"
  cclibs="$cclibs -lnsl -lsocket"
  echo "#define HAS_SOCKETS" >> s.h
  has_sockets=yes
fi

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

if sh ./hasgot inet_aton; then
  echo "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
  echo "IPv6 is supported."
  echo "#define HAS_IPV6" >> s.h
fi

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

if sh ./hasgot -i sys/types.h -t off_t; then
  echo "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
  echo "dirent.h found."
  echo "#define HAS_DIRENT" >> s.h
fi

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

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

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

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

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

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

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

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

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

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

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

select_include=''
if sh ./hasgot -i sys/types.h -i sys/select.h; then
  echo "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
  echo "select() found."
  echo "#define HAS_SELECT" >> s.h
  has_select=yes
fi

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

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

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

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

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

# Async I/O under OSF1 3.x are so buggy that the test program hangs...
testasyncio=true
if test -f /usr/bin/uname; then
  case "`/usr/bin/uname -s -r`" in
    "OSF1 V3."*) testasyncio=false;;
  esac
fi
if $testasyncio && sh ./runtest async_io.c; then
  echo "Asynchronous I/O are supported."
  echo "#define HAS_ASYNC_IO" >> s.h
fi

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

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

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

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

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

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

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

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


if sh ./hasgot $dllib dlopen; then
  echo "dlopen() found."
elif sh ./hasgot $dllib -ldl dlopen; then
  echo "dlopen() found in -ldl."
  dllib="$dllib -ldl"
else
  shared_libraries_supported=false
fi

if $shared_libraries_supported; then
  echo "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
  echo "mmap() found."
  echo "#define HAS_MMAP" >> s.h
fi

nargs=none
for i in 5 6; do
  if sh ./trycompile -DNUM_ARGS=${i} gethostbyname.c; then nargs=$i; break; fi
done
if test $nargs != "none"; then
  echo "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 -DNUM_ARGS=${i} gethostbyaddr.c; then nargs=$i; break; fi
done
if test $nargs != "none"; then
  echo "gethostbyaddr_r() found (with ${nargs} arguments)."
  echo "#define HAS_GETHOSTBYADDR_R $nargs" >> s.h
fi

# Determine if the debugger is supported

#if test "$has_sockets" = "yes"; then
#  echo "Replay debugger supported."
#  debugger="ocamldebugger"
#else
#  echo "No replay debugger (missing system calls)"
#  debugger=""
#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,macosx)
    echo "System stack overflow can be detected."
    echo "#define HAS_STACK_OVERFLOW_DETECTION" >> s.h;;
  *)
    echo "Cannot detect system stack overflow.";;
esac

# Determine the target architecture for the "num" library

#case "$arch" in
#  alpha)    bng_arch=alpha; bng_asm_level=1;;
#  i386)     bng_arch=ia32
#            if sh ./trycompile ia32sse2.c
#            then bng_asm_level=2
#            else bng_asm_level=1
#            fi;;
#  mips)     bng_arch=mips; bng_asm_level=1;;
#  power)    bng_arch=ppc; bng_asm_level=1;;
#  sparc)    bng_arch=sparc; bng_asm_level=1;;
#  amd64)    bng_arch=amd64; 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 bytecode thread library is supported

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

# Determine if the POSIX threads library is supported

systhread_support=false

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


# Determine the location of X include files and libraries

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

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
    break
  fi
done

if test "$x11_include" = "not found"; then
  x11_try_lib_dir=''
else
  x11_try_lib_dir=`echo $x11_include | 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 \
    ; \
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 "$host" in
        *-*-*bsd*) x11_link="-R$dir -L$dir -lX11";;
        *) x11_link="-L$dir -lX11";;
      esac
    fi
    break
  fi
done

if test "$x11_include" = "not found" || test "$x11_link" = "not found"
then
  echo "X11 not found, the \"graph\" library will not be supported."
  x11_include=""
else
  echo "Location of X11 include files: $x11_include/X11"
  echo "Options for linking with X11: $x11_link"
  otherlibraries="$otherlibraries graph"
  if test "$x11_include" = "/usr/include"; then
    x11_include=""
  else
    x11_include="-I$x11_include"
  fi
fi
echo "X11_INCLUDES=$x11_include" >> Makefile
echo "X11_LINK=$x11_link" >> Makefile

# See if we can compile the dbm library

dbm_include="not found"
dbm_link="not found"
use_gdbm_ndbm=no

#for dir in /usr/include /usr/include/db1 /usr/include/gdbm; do
#  if test -f $dir/ndbm.h; then
#    dbm_include=$dir
#    if sh ./hasgot dbm_open; then
#      dbm_link=""
#    elif sh ./hasgot -lndbm dbm_open; then
#      dbm_link="-lndbm"
#    elif sh ./hasgot -ldb1 dbm_open; then
#      dbm_link="-ldb1"
#    elif sh ./hasgot -lgdbm dbm_open; then
#      dbm_link="-lgdbm"
#    elif sh ./hasgot -lgdbm_compat -lgdbm dbm_open; then
#      dbm_link="-lgdbm_compat -lgdbm"
#    fi
#    break
#  fi
#  if test -f $dir/gdbm-ndbm.h; then
#    dbm_include=$dir
#    use_gdbm_ndbm=yes
#    if sh ./hasgot -lgdbm_compat -lgdbm dbm_open; then
#      dbm_link="-lgdbm_compat -lgdbm"
#    fi
#    break
#  fi
#done
#if test "$dbm_include" = "not found" || test "$dbm_link" = "not found"; then
#  echo "NDBM not found, the \"dbm\" library will not be supported."
#else
#  echo "NDBM found (in $dbm_include)"
#  if test "$dbm_include" = "/usr/include"; then
#    dbm_include=""
#  else
#    dbm_include="-I$dbm_include"
#  fi
#  if test "$use_gdbm_ndbm" = "yes"; then
#    echo "#define DBM_USES_GDBM_NDBM" >> s.h
#  fi
#  otherlibraries="$otherlibraries dbm"
#fi
#echo "DBM_INCLUDES=$dbm_include" >> Makefile
#echo "DBM_LINK=$dbm_link" >> Makefile

# Look for tcl/tk

echo "Configuring LablTk..."

if test $tk_wanted = no; then
  has_tk=false
elif test $tk_x11 = no; then
  has_tk=true
elif test "$x11_include" = "not found" || test "$x11_link" = "not found"; then
  echo "X11 not found."
  has_tk=false
else
  tk_x11_include="$x11_include"
  tk_x11_libs="$x11_libs -lX11"
  has_tk=true
fi

if test $has_tk = true; then
  tcl_version=''
  tcl_version=`sh ./runtest $tk_defs $tk_x11_include tclversion.c`
  for tk_incs in \
    "-I/usr/local/include" \
    "-I/usr/include" \
    "-I/usr/local/include/tcl8.5 -I/usr/local/include/tk8.5" \
    "-I/usr/include/tcl8.5 -I/usr/include/tk8.5" \
    "-I/usr/local/include/tcl8.4 -I/usr/local/include/tk8.4" \
    "-I/usr/include/tcl8.4 -I/usr/include/tk8.4" \
    "-I/usr/local/include/tcl8.3 -I/usr/local/include/tk8.3" \
    "-I/usr/include/tcl8.3 -I/usr/include/tk8.3" \
    "-I/usr/local/include/tcl8.2 -I/usr/local/include/tk8.2" \
    "-I/usr/include/tcl8.2 -I/usr/include/tk8.2" \
    "-I/sw/include" \
    "-I/usr/pkg/include"
  do if test -z "$tcl_version"; then
    tk_defs="$tk_incs"
    tcl_version=`sh ./runtest $tk_defs $tk_x11_include tclversion.c`
  fi; done
  if test -n "$tcl_version" && test "x$tcl_version" != "xnone"; then
    echo "tcl.h and tk.h version $tcl_version found with \"$tk_defs\"."
    case $tcl_version in
    7.5) tclmaj=7 tclmin=5 tkmaj=4 tkmin=1 ;;
    7.6) tclmaj=7 tclmin=6 tkmaj=4 tkmin=2 ;;
    8.0) tclmaj=8 tclmin=0 tkmaj=8 tkmin=0 ;;
    8.1) tclmaj=8 tclmin=1 tkmaj=8 tkmin=1 ;;
    8.2) tclmaj=8 tclmin=2 tkmaj=8 tkmin=2 ;;
    8.3) tclmaj=8 tclmin=3 tkmaj=8 tkmin=3 ;;
    8.4) tclmaj=8 tclmin=4 tkmaj=8 tkmin=4 ;;
    8.5) tclmaj=8 tclmin=5 tkmaj=8 tkmin=5 ;;
    *) echo "This version is not known."; has_tk=false ;;
    esac
  else
    echo "tcl.h and/or tk.h not found."
    has_tk=false
  fi
fi

tkauxlibs="$mathlib $dllib"
tcllib=''
tklib=''
if test $has_tk = true; then
  if test -n "$tk_libs" && \
     sh ./hasgot $tk_libs $tk_x11_libs $tkauxlibs Tcl_DoOneEvent
  then tk_libs="$tk_libs $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj.$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj.$tkmin -ltcl$tclmaj.$tclmin $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin $dllib"
  elif test -z "$tk_libs" && tk_libs=-L/usr/local/lib && \
    sh ./hasgot $tk_libs -ltcl$tclmaj.$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj.$tkmin -ltcl$tclmaj.$tclmin $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin $dllib"
  elif sh ./hasgot -L/sw/lib $tk_libs -ltcl$tclmaj.$tclmin $tkauxlibs \
                   Tcl_DoOneEvent
  then tk_libs="-L/sw/lib -ltk$tkmaj.$tkmin -ltcl$tclmaj.$tclmin $dllib"
  elif sh ./hasgot -L/usr/pkg/lib $tk_libs $tk_x11_libs \
        -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin -lpthread $tkauxlibs \
                   Tcl_DoOneEvent
  then
    case "$host" in
      *-*-*bsd*) tk_libs="-R/usr/pkg/lib -L/usr/pkg/lib $tk_libs $tk_x11_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin -lpthread $tkauxlibs";;
      *) tk_libs="-L/usr/pkg/lib $tk_libs $tk_x11_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin -lpthread $tkauxlibs";;
    esac
  else
    echo "Tcl library not found."
    has_tk=false
  fi
fi

if test $has_tk = true; then
  if sh ./hasgot $tk_libs $tk_x11_libs $tkauxlibs Tk_SetGrid; then
    echo "Tcl/Tk libraries found."
  elif sh ./hasgot -L/sw/lib $tk_libs $tk_x11_libs $tkauxlibs Tk_SetGrid; then
    case "$host" in
      *-*-*bsd*) tk_libs="-R/sw/lib -L/sw/lib $tk_libs";;
      *) tk_libs="-L/sw/lib $tk_libs";;
    esac
    echo "Tcl/Tk libraries found."
  elif sh ./hasgot -L/usr/pkg/lib $tk_libs $tk_x11_libs $tkauxlibs \
                   Tk_SetGrid; then
    case "$host" in
      *-*-*bsd*) tk_libs="-R/usr/pkg/lib -L/usr/pkg/lib $tk_libs";;
      *) tk_libs="-L/usr/pkg/lib $tk_libs";;
    esac
    echo "Tcl/Tk libraries found."
  else
    echo "Tcl library found."
    echo "Tk library not found."
    has_tk=false
  fi
fi

if test $has_tk = true; then
  if test $tk_x11 = yes; then
    echo "TK_DEFS=$tk_defs "'$(X11_INCLUDES)' >> Makefile
    echo "TK_LINK=$tk_libs "'$(X11_LINK)' >> Makefile
  else
    echo "TK_DEFS=$tk_defs" >> Makefile
    echo "TK_LINK=$tk_libs" >> Makefile
  fi
  otherlibraries="$otherlibraries labltk"
else
  echo "Configuration failed, LablTk will not be built."
  echo "TK_DEFS=" >> Makefile
  echo "TK_LINK=" >> Makefile
fi

# Final twiddling of compiler options to work around known bugs

nativeccprofopts="$nativecccompopts"
case "$buggycc" in
  gcc.2.96)
    bytecccompopts="$bytecccompopts -fomit-frame-pointer"
    nativecccompopts="$nativecccompopts -fomit-frame-pointer";;
esac

# Finish generated files

cclibs="$cclibs $mathlib"

echo "BYTECC=$bytecc" >> Makefile
echo "BYTECCCOMPOPTS=$bytecccompopts" >> Makefile
echo "BYTECCLINKOPTS=$bytecclinkopts" >> Makefile
echo "BYTECCLIBS=$cclibs $dllib $curseslibs $pthread_link" >> 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=ar rc \$(1) \$(2); ranlib \$(1)
#ml let mklib out files opts = Printf.sprintf "ar rc %s %s %s; ranlib %s" out opts files out;;
EOF
echo "ARCH=$arch" >> Makefile
echo "MODEL=$model" >> Makefile
echo "SYSTEM=$system" >> Makefile
echo "NATIVECC=$nativecc" >> Makefile
echo "NATIVECCCOMPOPTS=$nativecccompopts" >> 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 join" >> Makefile
echo "DEBUGGER=$debugger" >> Makefile
echo "CC_PROFILE=$cc_profile" >> Makefile
echo "SYSTHREAD_SUPPORT=$systhread_support" >> Makefile
echo "PARTIALLD=$partialld" >> Makefile
echo "PACKLD=\$(PARTIALLD) \$(NATIVECCLINKOPTS) -o " >> Makefile
echo "DLLCCCOMPOPTS=$dllccompopts" >> 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=cc" >> Makefile
echo "CMXS=$cmxs" >> Makefile
echo "MKEXE=$mkexe" >> Makefile
echo "MKDLL=$mksharedlib" >> Makefile
echo "MKMAINDLL=$mkmaindll" >> Makefile

rm -f tst hasgot.c
rm -f ../m.h ../s.h ../Makefile
mv m.h s.h Makefile ..
( cd ../.. ; build/mkconfig.sh )

# Print a summary

echo
echo "** Configuration summary **"
echo
echo "Directories where JoCaml will be installed:"
echo "        binaries.................. $bindir"
echo "        standard library.......... $libdir"
#echo "        manual pages.............. $mandir (with extension .$manext)"
echo "Companion ocaml:"
if $has_ocaml
then
echo "        version: $ocaml_version, library: $ocamllib."
else
echo "        none, JoCaml is alone!"
fi
echo "Configuration for the bytecode compiler:"
echo "        C compiler used........... $bytecc"
echo "        options for compiling..... $bytecccompopts"
echo "        options for linking....... $bytecclinkopts $cclibs $dllib $curseslibs $pthread_link"
if $shared_libraries_supported; then
echo "        shared libraries are supported"
echo "        options for compiling..... $sharedcccompopts $bytecccompopts"
echo "        command for building...... $mksharedlib -o lib.so $mksharedlibrpath/a/path objs"
else
echo "        shared libraries not supported"
fi

echo "Configuration for the native-code compiler:"
if test "$arch" = "none"; then
  echo "        (not supported on this platform)"
else
  if test "$model" = "default"; then
    echo "        hardware architecture..... $arch"
  else
    echo "        hardware architecture..... $arch ($model)"
  fi
  if test "$system" = "unknown"; then : ; else
  echo "        OS variant................ $system"
  fi
  echo "        C compiler used........... $nativecc"
  echo "        options for compiling..... $nativecccompopts"
  echo "        options for linking....... $nativecclinkopts $cclibs"
  echo "        assembler ................ $as"
  echo "        preprocessed assembler ... $aspp"
  if test "$profiling" = "prof"; then
  echo "        profiling with gprof ..... supported"
  else
  echo "        profiling with gprof ..... not supported"
  fi
fi

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

echo "Additional libraries supported:"
echo "        $otherlibraries"

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

if test "$x11_include" != "not found" && test "$x11_lib" != "not found"; then
echo "Configuration for the \"graph\" library:"
echo "        options for compiling .... $x11_include"
echo "        options for linking ...... $x11_link"
fi

#if test $has_tk = true; then
#echo "Configuration for the \"labltk\" library:"
#echo "        use tcl/tk version ....... $tcl_version"
#echo "        options for compiling .... $tk_defs"
#echo "        options for linking ...... $tk_libs"
#else
#echo "The \"labltk\" library: not supported"
#fi

echo
echo "** JoCaml configuration completed successfully **"
echo