summaryrefslogtreecommitdiff
path: root/src/configure.in
blob: d8417a1c6348f5a3f5c409c74b327d1d35cad617 (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
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
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
# 
# Description: configure.in
#   Autonconf produce a ./configure from this file
#   that's used to discover various programs/dependencies
#   usage: autoconf
#
# Authors: paul_c alex_joni jepler
# License: GPL Version 2
# System: Linux
#    
# Copyright (c) 2004 All rights reserved.
#
# Last change:
# This file is released under GPL, refer to docs/README for details
#
# This file is adapted to replace the old ./configure script
# therefor it includes parts of that script

##############################################################################
# Section 1                                                                  #
# Autoconf initialisation                                                    #
##############################################################################

AC_PREREQ(2.53)
AC_INIT(LinuxCNC, m4_normalize(esyscmd([cat ../VERSION])), emc-developers@lists.sourceforge.net)
AC_CONFIG_SRCDIR(emc/motion/motion.c)
if test "$srcdir" != "."; then
    AC_MSG_ERROR([Building outside of srcdir is not supported])
fi

## _PYTHON_SYSCONFIG(expression, [action-if-succeeded], [action-if-failed])
##
m4_define([_PYTHON_SYSCONFIG],
[dnl
py_val=`$PYTHON -c "dnl
[import sys; from distutils import sysconfig; sys.stdout.write]dnl
([sysconfig.]$1 or '')" 2>&AS_MESSAGE_LOG_FD`
AS_IF([test "[$]?" -eq 0 && test "x$py_val" != x], [$2], [$3])[]dnl
])

# adapted from AX_BOOST_PYTHON_MURRAYC
# http://ftp.gnome.org/pub/GNOME/sources/glom/1.19/
m4_define([AX_BOOST_PYTHON_EMC],
[
AC_LANG_PUSH([C++])
saved_CPPFLAGS=$CPPFLAGS
saved_LIBS=$LIBS
# Note that this requires PYTHON_CPPFLAGS 
# Note that this expects boost/ to be at some top-level such as /usr/include/
# We couldn't check for anything else anyway because there's no pkg-config file to tell us where it is
CPPFLAGS="$PYTHON_CPPFLAGS $saved_CPPFLAGS"
AC_CACHE_CHECK([whether the Boost::Python headers are available],
               [ac_cv_boost_python],
               [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <boost/python/module.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(test) { (void)0; }
]], [])],
    [ac_cv_boost_python=yes], [ac_cv_boost_python=no])])

AS_IF([test "x$ac_cv_boost_python" = xyes],
[
  BOOST_PYTHON_LIBS=
  ax_python_lib=boost_python
  AC_ARG_WITH([boost-python],
              [AS_HELP_STRING([--with-boost-python],
                              [specify the boost python shared library to use. For instance, --with-boost-python=boost_python-py25. Defaults to boost-python. If you use this then you should probably set PYTHON too, to avoid using multiple python versions.])],
  [if test "x$with_boost_python" != xno; then
    ax_python_lib=$with_boost_python
    ax_boost_python_lib=boost_python-$with_boost_python
  fi])
  AC_MSG_CHECKING([for boost::python shared library])
  for ax_lib in "$ax_python_lib" "$ax_boost_python_lib" boost_python
  do
    # Note that this requires LIBPYTHON from configure.in
    LIBS="$saved_LIBS -l$LIBPYTHON -l$ax_lib"
    AC_LINK_IFELSE(
      [AC_LANG_PROGRAM([[#include <boost/python.hpp>]], [[boost::python::object test_object;]])],
      [BOOST_PYTHON_LIBS="-l$ax_lib"; break])
  done
  AS_IF([test "x$BOOST_PYTHON_LIBS" != x], [ax_result=$BOOST_PYTHON_LIBS], [ax_result=])
  AC_MSG_RESULT([$ax_result])
])
CPPFLAGS=$saved_CPPFLAGS
LIBS=$saved_LIBS
AC_LANG_POP([C++])
AS_IF([test "x$ac_cv_boost_python" = xyes && test "x$BOOST_PYTHON_LIBS" != x],
      [AC_DEFINE([HAVE_BOOST_PYTHON], [1],
                 [define if the Boost::Python headers and library are available])],
      [AC_MSG_ERROR([[boost::python is required to build $PACKAGE_NAME]])])
AC_SUBST([BOOST_PYTHON_LIBS])
])

AC_MSG_CHECKING(build toplevel)
BUILD_TOPLEVEL="$(cd ..; pwd -P)"
AC_MSG_RESULT($BUILD_TOPLEVEL)

AC_CONFIG_HEADER(config.h)

# Support some of the standard configure options for directories
AC_PREFIX_DEFAULT(run-in-place)

AC_MSG_CHECKING(installation prefix)
if test "x$prefix" = "xNONE"; then
  RUN_IN_PLACE=yes
  AC_MSG_RESULT(run in place)
  prefix=${BUILD_TOPLEVEL}
else
  RUN_IN_PLACE=no
  AC_MSG_RESULT($prefix)
fi
AC_SUBST(AUTODIRS)

# internationalisation package name:
PACKAGE="linuxcnc"
AC_SUBST([PACKAGE])

if test -f ../VERSION; then
    EMC2VERSION="$(cat ../VERSION)"
else
    EMC2VERSION="Undetermined"
fi
AC_SUBST([EMC2VERSION])

AC_PATH_PROG(GREP, grep)
AC_PATH_PROG(EGREP, egrep)

##############################################################################
# Section 2                                                                  #
# RealTime checking (either RTAI, or RT-Linux)                               #
# Subsection 2.1 - arguments to ./configure (--with-realtime)                #
# Subsection 2.2 - if none specified above, RTDIR needs to be found by search#
# Subsection 2.3 - RTS(RT-script) is found, it gets queried for values       #
##############################################################################

AC_MSG_CHECKING(for RT dir)

# set up some default values
RTDIR=""
RTNAME=""
RTAI=""
RTLINUX=""
RTLINUXPRO=""
RTPREFIX=""
KERNELDIR=""
RTFLAGS=""

RTAI3_MOD=""
RTAI_MOD=""
RTL_MOD=""

##############################################################################
# Subsection 2.1                                                             #
# Check for arguments to ./configure                                         #
# accept --with-realtime=<path> from commandline                             #
##############################################################################

AC_ARG_ENABLE(simulator,
    [  --enable-simulator      Run as a pure userspace program],
    [
        case "$enableval" in
            Y*|y*)
                SIMULATOR=yes
                RTS=simulator
                BUILD_SYS=sim
                MODEXT=.so
                MODULE_DIR=${prefix}/lib/linuxcnc/modules
                AC_MSG_RESULT([configuring for simulator])
            ;;
            *)
                SIMULATOR=no
        esac
    ],
    [
        SIMULATOR=no
    ])
AC_SUBST(SIMULATOR)

AC_ARG_WITH(realtime,
    [  --with-realtime=<path to realtime>     Path where RTAI or RTlinux is installed],
    [   case "$withval" in
	"" | y | ye | yes | n | no)
	    AC_MSG_ERROR([You must supply a path for --with-realtime.]) ;;
	*)
		case "$SIMULATOR" in
			yes) AC_MSG_ERROR([--with-realtime and --enable-simulator are mutually exclusive]) ;;
			*) RTDIR="$withval" ;;
		esac
	;;
        esac])

##############################################################################
# Subsection 2.2                                                             #
# 1. If a RT has been specified by the user it needs to be checked for       #
# validity. That is done by checking default locations for a certain script  #
# (similar to 2)                                                             #
#                                                                            #
# 2. If no RT has been specified explicitely by the user, one has to be      #
# found by search. In order to do that the following code will search for a  #
# RT-specific script:                                                        #
# rtlinux-config for RTLinux                                                 #
# realtime-config for RTAI 24.1.x                                            #
# rtai-config for RTAI 3.x                                                   #
##############################################################################

#at this point if RTDIR is empty, we need to find RT ourselves
if test $SIMULATOR = yes; then
  RTS=sim
  AC_PATH_PROG(PTH_CONFIG,pth-config,"")
  if test "$PTH_CONFIG" = ""; then
    AC_MSG_ERROR([GNU PTH library is required: get it with apt-get install libpth-dev])
  fi
else
  if test -z "$RTDIR"; then
    DIRS="/usr/realtime-`uname -r` /usr/realtime /usr/realtime* /usr /usr/src/rtai*"
  else
    DIRS="$RTDIR"
  fi
  for location in $DIRS; do
    for subdir in . scripts bin; do
      for signature in rtai rtl realtime; do
	candidate=$location/$subdir/$signature-config
	if test -z "$RTS" -a -e $candidate; then
	  RTS=$candidate
	fi
      done
    done
 done
 if test -z "$RTS"; then
    AC_MSG_ERROR([RT not found.  Specify:
	--with-realtime=<path>
or use
	--enable-simulator
to build without a realtime system.])
  fi
  AC_MSG_RESULT([Using $RTS as the RT signature])
fi


##############################################################################
# Subsection 2.3                                                             #
# we have the RT-script now, it's time to query it for some values           #
# it can provide us with default values such as CC, CFLAGS, etc              #
# see each case for more details                                             #
##############################################################################

RTARCH=`uname -m | sed s/i.86/i386/`
case $RTS in
*/rtai-config)
    RTNAME=rtai-3.0
    RTPREFIX=rtai
    RTAI=3
    RTDIR=`$RTS --prefix`
    RTDIR=$(cd $RTDIR ; pwd -P )
    RTFLAGS=`$RTS --module-cflags`
    RTFLAGS="$RTFLAGS -DRTAI=3"
    KERNELDIR=`$RTS --linux-dir`
    RTARCH=`$RTS --arch`
;;
*/realtime-config)
# For RTAI-24.1x $RTS = realtime-config
# Options and their return values for realtime-config
#	 Option		Returns
#	--prefix	Path to the rtai installation (usually /usr/realtime), not the original source tree. - USE WITH CAUTION
#	--version	Should return the version number - Appears to return 0.0.0 for most 24.1.x releases.
#	--cc		The compiler used for the C sources - Not necessarily the default cc
#	--cflags	Flags used to compile the realtime modules with.
#	--cflags-fp	Flags to use when compiling realtime modules with floating point math.
#	--dump		Dumps the .buildvars to screen - Grep for linuxdir
    RTNAME=rtai
    RTPREFIX=rtai
    RTAI=1

    # any other rtai-24.1.x system
    TEMP=`$RTS --prefix`
    if (test -d $TEMP) ; then
        RTDIR=$(cd $TEMP ; pwd -P )
    else
        RTDIR=${RTS%%/scripts/realtime-config}
    fi
    RTFLAGS=`$RTS --cflags-fp | head -n 1`
    RTFLAGS="$RTFLAGS -DRTAI=1"
    KERNELDIR=$($RTS --dump | grep -i linuxdir | awk -F'[[ ="]]+' '{print $2}')
#"
    KERNELDIR=$(cd $KERNELDIR ; pwd -P )
;;
*/rtl-config)
# For RTLinux-3.? $RTS = rtl-config
# Options and their return values for rtl-config
#	 Option		Returns
#	--prefix	Path to the RTLinux installation (usually /usr/rtlinux-{rtlVersion}), not the original source tree.
#	--rtlVersion	Version number of the installed RTLinux (e.g. 3.0)
#	--cc		The compiler used for the C sources - Not necessarily the default cc
#	--cflags	Flags used to compile the realtime modules with - It is a good idea to use these !
#	--linux		Path to the source directory of the patched kernel.
#	--module_dir	Path to the directory where the RTLinux modules are installed
#	--modules	A list of the available realtime modules.
    RTPREFIX=rtl
    RTDIR=`$RTS --prefix`
    RTDIR=$(cd $RTDIR ; pwd -P )
    RTFLAGS=`$RTS --cflags`
    KERNELDIR=`$RTS --linux`
;;
sim)
    RTPREFIX=sim
    RTDIR=$(pwd -P)
    RTFLAGS=-DSIMULATOR
esac

if test $RTS != sim; then
    AC_MSG_CHECKING([for location of kernel headers])
    AC_ARG_WITH(kernel-headers,
	[  --with-kernel-headers=<directory>       Location for kernel headers],
	[
	    KERNELDIR="$withval"
	    AC_MSG_RESULT(using specified value: $KERNELDIR)
	],[
	    AC_MSG_RESULT(using value from RTS: $KERNELDIR)
	])
fi

AC_SUBST([RTNAME])
AC_SUBST([RTAI])
AC_SUBST([RTLINUX])
AC_SUBST([RTLINUXPRO])
AC_SUBST([RTPREFIX])
AC_SUBST([KERNELDIR])
AC_SUBST([RTFLAGS])
AC_SUBST([RTDIR])
AC_SUBST([RTARCH])

if test $RTPREFIX = sim; then
    AC_DEFINE(RTAPI_SIM, [], [Realtime system is simulated])
elif test $RTPREFIX = rtai; then
    AC_DEFINE(RTAPI_RTAI, [], [Realtime system is RTAI])
elif test $RTPREFIX = rtl; then
    AC_DEFINE(RTAPI_RTL, [], [Realtime system is RTL])
fi


EMC2_HOME=`(cd .. ; pwd)`
AC_SUBST([EMC2_HOME])

AC_DEFINE_UNQUOTED([RTDIR], "$RTDIR", [Directory of the installed realtime system])

##############################################################################
# Section 3                                                                  #
# aditional checking                                                         #
# Subsection 3.1 - checking for a CC (we must use the same one used for RT)  #
# Subsection 3.2 - check for the kernel version, with RT patches             #
# Subsection 3.3 - check the moduledir where modules can get installed       #
# Subsection 3.4 - check for GTK                                             #
# Subsection 3.5 - check for utility programs needed to build and run EMC    #
##############################################################################


##############################################################################
# Subsection 3.1 - checking for a CC (we must use the same one used for RT)  #
# Check CC version - (P.C.) We need to verify the compiler is the same one   #
# used for the realtime extensions... Hence the need to find the RT signature# 
# first.                                                                     #
# $RTS --cc should return the C compiler used - Verify this is acceptable.   #
# if version is gcc-2.96 check for alternative                               #
##############################################################################
AC_MSG_CHECKING([for cc version])

# The compiler MUST match the one used to compile the realtime modules if the
# kernel level code is to function correctly. But if the kernel has been
# compiled with gcc-2.96, it is unlikely to ever work !
# However realtime-config doesn't seem to have such an option
if (test ${RTS##*/} = rtai-config ) ; then
  RTSCC=`$RTS --cc`
  AC_MSG_RESULT(found $RTSCC in rtai-config)
elif (test ${RTS##*/} = rtl-config ) ; then
  RTSCC=`$RTS --cc`
  AC_MSG_RESULT(found $RTSCC in rtl-config)
else
  AC_MSG_RESULT(not specified)
fi

AC_PROG_CC([$RTSCC gcc])
AC_PROG_CPP()

if test -z "$CC"; then
  AC_MSG_ERROR([no acceptable cc found])
elif ! test `$CC -dumpversion | cut -d '.' -f 1` -gt 2 ; then
  AC_MSG_ERROR([Compilers older than gcc 3.x are no longer supported])
fi

AC_MSG_CHECKING([for usability of rdtscll from asm/msr.h])
AC_TRY_LINK(
    [
#include <asm/msr.h>
#include <stdio.h>
void use_rdtscll(void) { long long tsc; rdtscll(tsc); }
],
    [],
    [
        MSR_H_USABLE=yes
        AC_DEFINE([MSR_H_USABLE], [], [Define to 1 if asm/msr.h is usable and defines rdtscll])
    ], [MSR_H_USABLE=no])
AC_MSG_RESULT($MSR_H_USABLE)

AC_MSG_CHECKING([for usability of linux/hidraw.h])
AC_TRY_LINK(
    [
#include <sys/ioctl.h>
#include <linux/hidraw.h>
long hidiocgrawinfo = HIDIOCGRAWINFO;
    ],
    [],
    [
	HIDRAW_H_USABLE=yes
        AC_DEFINE([HIDRAW_H_USABLE], [], [Define to 1 if linux/hidraw.h is usable and defines HIDIOCGRAWINFO])
],
    [HIDRAW_H_USABLE=no])
AC_MSG_RESULT($HIDRAW_H_USABLE)
AC_SUBST([HIDRAW_H_USABLE])


#
# check for libmodbus3
#

AC_ARG_WITH(
    [libmodbus],
    [  --with-libmodbus                        Specify whether or not to build
                                          the drivers that use libmodbus
                                          (defaults to "yes")],
    [WITH_LIBMODBUS=$withval],
    [WITH_LIBMODBUS=yes]
)

AS_IF(
    [test "x$WITH_LIBMODBUS" = "xyes"],
    [
        AC_MSG_CHECKING([for libmodbus3])
        if pkg-config libmodbus --atleast-version 3 >/dev/null 2>&1; then
            LIBMODBUS_VER=`pkg-config libmodbus --modversion`
            AC_MSG_RESULT(yes - version [$LIBMODBUS_VER])
            LIBMODBUS_CFLAGS=`pkg-config libmodbus --cflags`
            AC_SUBST([LIBMODBUS_CFLAGS])
            LIBMODBUS_LIBS=`pkg-config libmodbus --libs`
            AC_SUBST([LIBMODBUS_LIBS])
            AC_DEFINE(
                [HAVE_LIBMODBUS3],
                [yes],
                [define if the libmodbus3 headers and library are available]
            )
            AC_SUBST(HAVE_LIBMODBUS3, yes)
        else
          AC_MSG_RESULT(no)
          AC_MSG_ERROR([libmodbus3 not found!
install with "sudo apt-get install libmodbus-dev" or disable with
"configure --without-libmodbus"])
        fi
    ]
)


#
# check for libusb-1.0
#

AC_ARG_WITH(
    [libusb-1.0],
    [  --with-libusb-1.0                       Specify whether or not to build
                                          the drivers that use libusb-1.0
                                          (defaults to "yes")],
    [WITH_LIBUSB10=$withval],
    [WITH_LIBUSB10=yes]
)

AS_IF(
    [test "x$WITH_LIBUSB10" = "xyes"],
    [
        AC_MSG_CHECKING([for libusb-1.0])
        if pkg-config libusb-1.0 >/dev/null 2>&1; then
            AC_MSG_RESULT(yes)
            LIBUSB10_CFLAGS=`pkg-config libusb-1.0 --cflags`
            AC_SUBST([LIBUSB10_CFLAGS])
            LIBUSB10_LIBS=`pkg-config libusb-1.0 --libs`
            AC_SUBST([LIBUSB10_LIBS])
            AC_DEFINE(
                [HAVE_LIBUSB10],
                [yes],
                [define if the libusb-1.0 headers and library are available]
            )
            AC_SUBST(HAVE_LIBUSB10, yes)
        else
          AC_MSG_RESULT(no)
          AC_MSG_ERROR([libusb-1.0 not found!
install with "sudo apt-get install libusb-1.0-0-dev" or disable with
"configure --without-libusb-1.0"])
        fi
    ]
)


##############################################################################
# Subsection 3.2 - check for the kernel version, with RT patches             #
#----------------------------------------------------------------------------#
# Check with the version.h to find the uts-kernel version.                   #
# This should be a more reliable way of determining the kernel that          #
# is being used to compile against - `uname -r` only returns the             #
# running kernel version which is may not be the one the code is             #
# compiled against.                                                          #
##############################################################################

if test $RTS '!=' sim; then
AC_CHECK_HEADERS([$KERNELDIR/include/linux/version.h],[],[AC_MSG_ERROR([version.h not found - Is the kernel headers package installed ?])])

# Unfortunately, many distributions (redhat, mandrake) have #defines
# inside version.h, so a simple cat|grep|cut test won't work... But then again,
# RH & Mandrake kernels are notorious for their use of patches that break
# the RT patching - Both RTAI and RTLinux strongly recommend using
# a virgin source from kernel.org.

# Get the kernel version from version.h
AC_MSG_CHECKING([for kernel version string])

if test -e $KERNELDIR/include/linux/utsrelease.h; then
    VERSION_FILE=$KERNELDIR/include/linux/utsrelease.h
elif test -e $KERNELDIR/include/generated/utsrelease.h; then
    VERSION_FILE=$KERNELDIR/include/generated/utsrelease.h
else
    VERSION_FILE=$KERNELDIR/include/linux/version.h
fi
KERNEL_VERS=`$CC -E -dM ${VERSION_FILE} | grep UTS | cut -d '"' -f 2`

if test -z "$KERNEL_VERS"; then 
    AC_MSG_ERROR(Kernel version string not found)
fi

AC_MSG_RESULT([$KERNEL_VERS])
AC_SUBST([KERNEL_VERS])
AC_DEFINE_UNQUOTED([RTAPI_KERNEL_VERSION], "$KERNEL_VERS", [Required kernel version])

# Do a quick test for module extension..
AC_MSG_CHECKING([for kernel module extension])
MODEXT=`$RTS --module-ext 2>/dev/null` || MODEXT=".o"
if test "x" = "x$MODEXT"; then
    MODEXT=".o"
fi
AC_MSG_RESULT([$MODEXT])

# This check should see if kbuild is or isn't used
AC_MSG_CHECKING([kernel build system])
# FIXME FIXME FIXME
# right now it will assume based on the kernel module extension
EXT_RTFLAGS=""
BUILD_SYS="normal"

if test "x$MODEXT" = "x.ko" ; then
    BUILD_SYS="kbuild"
else
    EXT_RTFLAGS="-DEXPORT_SYMTAB"
fi

AC_ARG_WITH(kbuild,
    [  --with-kbuild=<y/n>                     Specify if your kernel uses kbuild or not],
    [
        case "$withval" in
        "" | y | ye | yes )
	    BUILD_SYS="kbuild"
          ;;
        n | no )
	    BUILD_SYS="normal"
          ;;
        esac
    ])
AC_MSG_RESULT([$BUILD_SYS])
fi
AC_SUBST([MODEXT])
AC_DEFINE_UNQUOTED([MODULE_EXT], "$MODEXT", [Extension for realtime modules])
AC_SUBST([BUILD_SYS])
AC_SUBST([EXT_RTFLAGS])

##############################################################################
# Subsection 3.3 - check the moduledir where modules can get installed       #
#                                                                            #
# Provides the user the possibility to specify (--with-module-dir=<path>)    #
# If not specified default inside /lib/modules/${KERNEL_VERS}/               #
# to the location where a RT-module gets found (mbuff.o or rtai_shm.o)       #
# (maybe the default should be /lib/modules/$KERNEL_VERS/extra if it exists? #
##############################################################################

# Create a module-dir option, similar to rtai's...
AC_MSG_CHECKING([for module installation directory])

AC_ARG_WITH(module-dir,
    [  --with-module-dir=<module-dir>          Installation directory for kernel modules],
    [
        case "$withval" in
        "" | y | ye | yes | n | no)
            AC_MSG_ERROR([You must supply a path for --with-module-dir.])
          ;;
        esac
        MODULE_DIR="$withval"
    ])


# if not specified, ask the realtime config script where the directory is
# if the config script returns an error, set MODULE_DIR to nothing
if test "x" = "x$MODULE_DIR" ; then
  MODULE_DIR=`$RTS --module-dir 2>/dev/null`
  if ! test -d $MODULE_DIR ; then
    AC_MSG_ERROR([$RTS --module-dir did not specify an existing directory.  You must use specify it using --with-module-dir=...])
  fi
fi
    
if test "$RUN_IN_PLACE" = "yes"; then
    RIP_MODULE_DIR="${BUILD_TOPLEVEL}/rtlib"
    AC_MSG_RESULT([configuring for run-in-place])
    prefix=${BUILD_TOPLEVEL}
    DEFAULT_NMLFILE=${prefix}/configs/common/linuxcnc.nml
    DEFAULT_TOOLTABLE=${prefix}/configs/sim/sim.tbl
    AC_MSG_RESULT([$RIP_MODULE_DIR])
else
    RUN_IN_PLACE=no
    EMC2_HOME=$prefix
    DEFAULT_NMLFILE=${prefix}/share/linuxcnc/linuxcnc.nml
    AC_MSG_RESULT([$MODULE_DIR])
fi

AC_ARG_ENABLE(run-in-place,
    [  --enable-run-in-place   A deprecated flag
                          (instead, configure without specifying --prefix)],
    [
        if ! test -z "$enableval"; then
            AC_MSG_WARN([--enable-run-in-place will be removed soon.
Configure without specifying --prefix to get a run-in-place build.])
        fi
    ])

AC_SUBST([MODULE_DIR])
AC_SUBST([RIP_MODULE_DIR])
AC_SUBST([RUN_IN_PLACE])
AC_SUBST([DEFAULT_NMLFILE])
AC_DEFINE_UNQUOTED([EMC2_DEFAULT_NMLFILE], "$DEFAULT_NMLFILE", [Default nml file])
AC_DEFINE_UNQUOTED([EMC2_DEFAULT_TOOLTABLE], "$DEFAULT_TOOLTABLE", [Default nml file])

##############################################################################
# Subsection 3.4 - define default dirs, these get used in a lot of places    #
# we decide based on RIP or install where stuff goes                         #
##############################################################################

fe () {
    # Fully expand argument.  Example:
    # ${datadir} -> ${datarootdir} -> ${prefix}/share -> /usr/local
    case "$1" in
    *\$*) eval fe \""$1"\" ;;
    *) echo "$1" ;;
    esac
}

if test "xyes" = "x$RUN_IN_PLACE"; then
    EMC2_LATENCY_SCRIPT=$EMC2_HOME/scripts/latency-test
    EMC2_SCRIPT=$EMC2_HOME/scripts/emc
    EMC2_SUFFIX=" (run-in-place)"
    EMC2_ICON=$EMC2_HOME/linuxcncicon.png
    EMC2_BIN_DIR=$EMC2_HOME/bin
    EMC2_TCL_DIR=$EMC2_HOME/tcl
    EMC2_TCL_LIB_DIR=$EMC2_HOME/tcl
    EMC2_LANG_DIR=$EMC2_HOME/src/objects
    EMC2_PO_DIR=$EMC2_HOME/share/locale
    EMC2_HELP_DIR=$EMC2_HOME/docs/help
    EMC2_RTLIB_DIR=$EMC2_HOME/rtlib
    LINUXCNC_CONFIG_PATH="~/linuxcnc/configs:$EMC2_HOME/configs"
    EMC2_NCFILES_DIR=$EMC2_HOME/nc_files
    REALTIME=$EMC2_HOME/scripts/realtime
    EMC2_IMAGE_DIR=$EMC2_HOME
else
    EMC2_HOME=${prefix}
    EMC2_BIN_DIR=${prefix}/bin
    EMC2_LATENCY_SCRIPT=$EMC2_BIN_DIR/latency-test
    EMC2_SCRIPT=$EMC2_BIN_DIR/emc
    EMC2_SUFFIX=""
    EMC2_ICON=linuxcncicon
    EMC2_TCL_DIR=${prefix}/lib/tcltk/linuxcnc
    EMC2_TCL_LIB_DIR=${prefix}/lib/tcltk/linuxcnc
    EMC2_LANG_DIR=${prefix}/share/linuxcnc/tcl/msgs
    EMC2_PO_DIR=${prefix}/share/locale
    EMC2_HELP_DIR=${prefix}/share/doc/linuxcnc
    case $MODULE_DIR in
        */linuxcnc*) EMC2_RTLIB_DIR=$MODULE_DIR ;;
	*) EMC2_RTLIB_DIR=$MODULE_DIR/linuxcnc
    esac
    LINUXCNC_CONFIG_PATH="~/linuxcnc/configs:/usr/local/etc/linuxcnc/configs:"$(eval echo $EMC2_HELP_DIR)"/examples/sample-configs"
    EMC2_NCFILES_DIR=${prefix}/share/linuxcnc/ncfiles
    REALTIME=/etc/init.d/realtime
    EMC2_IMAGE_DIR=$(fe "$datadir")/linuxcnc
fi

AC_DEFINE_UNQUOTED([EMC2_HOME], "$EMC2_HOME", [Prefix or RIP directory])
AC_DEFINE_UNQUOTED([EMC2_BIN_DIR], "$EMC2_BIN_DIR", [Directory for binaries])
AC_DEFINE_UNQUOTED([EMC2_TCL_DIR], "$EMC2_TCL_DIR", [Directory for tcl scripts])
AC_DEFINE_UNQUOTED([EMC2_HELP_DIR], "$EMC2_HELP_DIR", [Directory for help files])
AC_DEFINE_UNQUOTED([EMC2_RTLIB_DIR], "$EMC2_RTLIB_DIR", [Directory of realtime system])
AC_DEFINE_UNQUOTED([EMC2_LANG_DIR], "$EMC2_LANG_DIR", [Directory for tcl translation files])
AC_DEFINE_UNQUOTED([EMC2_PO_DIR], "$EMC2_PO_DIR", [Directory for po/mo translation files])
AC_DEFINE_UNQUOTED([EMC2_CONFIG_DIR], "$EMC2_CONFIG_DIR", [Directory for configuration files])
AC_DEFINE_UNQUOTED([EMC2_NCFILES_DIR], "$EMC2_NCFILES_DIR", [Directory for nc files])
AC_DEFINE_UNQUOTED([EMC2_IMAGE_DIR], "$EMC2_IMAGE_DIR", [Directory for images])
AC_DEFINE_UNQUOTED([PRELOAD_WORKAROUND], "$PRELOAD_WORKAROUND", [library pathname to LD_PRELOAD])

AC_SUBST([EMC2_BIN_DIR])
AC_SUBST([EMC2_TCL_DIR])
AC_SUBST([EMC2_TCL_LIB_DIR])
AC_SUBST([EMC2_HELP_DIR])
AC_SUBST([EMC2_RTLIB_DIR])
AC_SUBST([EMC2_LANG_DIR])
AC_SUBST([LINUXCNC_CONFIG_PATH])
AC_SUBST([EMC2_NCFILES_DIR])
AC_SUBST([REALTIME])
AC_SUBST([EMC2_IMAGE_DIR])
AC_SUBST([EMC2_SCRIPT])
AC_SUBST([EMC2_LATENCY_SCRIPT])
AC_SUBST([EMC2_LATENCYPLOT_SCRIPT])
AC_SUBST([EMC2_SUFFIX])
AC_SUBST([EMC2_ICON])
AC_SUBST([PRELOAD_WORKAROUND])

##############################################################################
# Subsection 3.5 - check for GTK                                             #
# FIXME: allow it to be enabled or disabled command line                     #
##############################################################################

AC_MSG_CHECKING([for glib])
if pkg-config glib-2.0 >/dev/null 2>&1; then
  GLIB_VER=`pkg-config glib-2.0 --modversion`
  AC_MSG_RESULT(yes - $GLIB_VER)
  GLIB_CFLAGS=`pkg-config glib-2.0 --cflags`
  GLIB_LIBS=`pkg-config glib-2.0 --libs`
else
  AC_MSG_ERROR(no -- required until somebody makes glib optional)
fi


AC_ARG_ENABLE(gtk,
    [  --disable-gtk        Disable the parts of LinuxCNC that depend on GTK],
    [
	case "$enableval" in
	Y*|y*)
	    BUILD_GTK=yes ;;
	*)
	    BUILD_GTK=no ;;
	esac
    ],
    [BUILD_GTK=yes])

if test "$BUILD_GTK" = "yes"; then
  AC_MSG_CHECKING([for GTK 2.4.0 or above])
  if pkg-config gtk+-2.0 --atleast-version 2.4.0 >/dev/null 2>&1; then
    GTK_VER=`pkg-config gtk+-2.0 --modversion`
    AC_MSG_RESULT(yes - $GTK_VER)
    GTK_CFLAGS=`pkg-config gtk+-2.0 --cflags`
    GTK_LIBS=`pkg-config gtk+-2.0 --libs`
    AC_MSG_CHECKING(for libgnomeprintui-2.2)
    if pkg-config --exists libgnomeprintui-2.2; then
      AC_MSG_RESULT(yes)
      GTK_CFLAGS="$GTK_CFLAGS `pkg-config --cflags libgnomeprintui-2.2`"
      GTK_LIBS="$GTK_LIBS `pkg-config --libs libgnomeprintui-2.2`"
      HAVE_GNOMEPRINT=yes
    else
      AC_MSG_RESULT(no -- printing from classicladder will not be possible)
      HAVE_GNOMEPRINT= 
    fi
  else
    AC_MSG_RESULT(no)
    AC_MSG_ERROR([GTK2 missing.  Install it or specify --disable-gtk to skip the parts of LinuxCNC that depend on GTK])
  fi
fi

AC_SUBST(HAVE_GNOMEPRINT)
AC_SUBST(GTK_VER)
AC_SUBST(GTK_LIBS)
AC_SUBST(GTK_CFLAGS)
AC_SUBST(GLIB_LIBS)
AC_SUBST(GLIB_CFLAGS)

##############################################################################
# Subsection 3.6 - check for utility programs needed to build and run EMC    #
#                                                                            #
# Check for all the utility programs we need to build and install emc        #
# as well as those we use in the emc scripts...                              #
##############################################################################

SPATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin
AC_PROG_CXX

m4_include([m4/ax_cxx_compile_stdcxx_11.m4])
# make this optional - it will likely only result in warnings
AX_CXX_COMPILE_STDCXX_11(noext,optional)

AC_PROG_MAKE_SET
AC_PROG_RANLIB
AC_PATH_PROG(AR,ar)
AC_PATH_PROG(INSTALL, install)

AC_PATH_PROG(SED, sed, "none")
if test $SED = "none"
then
    AC_MSG_ERROR([sed not found])
fi

AC_PATH_PROG(PS, ps, "none")
if test $PS = "none"
then
    AC_MSG_ERROR([ps not found])
fi

AC_PATH_PROG(KILL, kill, "none")
if test $KILL = "none"
then
    AC_MSG_ERROR([kill not found])
fi

AC_PATH_PROG(WHOAMI, whoami, "none")
if test $WHOAMI = "none"
then
    AC_MSG_ERROR([whoami not found])
fi

AC_PATH_PROG(AWK, awk, "none")
if test $AWK = "none"
then
    AC_MSG_ERROR([awk not found])
fi

AC_PATH_PROG(INSMOD, insmod, "none", $SPATH)
if test $INSMOD = "none"
then
    AC_MSG_ERROR([insmod not found])
fi

AC_PATH_PROG(RMMOD, rmmod, "none", $SPATH)
if test $RMMOD = "none"
then
    AC_MSG_ERROR([rmmod not found])
fi

AC_PATH_PROG(LSMOD, lsmod, "none", $SPATH)
if test $LSMOD = "none"
then
    AC_MSG_ERROR([lsmod not found])
fi

AC_PATH_PROG(PIDOF, pidof, "none", $SPATH)
if test $PIDOF = "none"
then
    AC_MSG_ERROR([pidof not found])
fi

AC_PATH_PROG(IPCS, ipcs, "none")
if test $IPCS = "none"
then
    AC_MSG_ERROR([ipcs not found])
fi

AC_PATH_PROG(FUSER, fuser, "none", $SPATH)
if test $FUSER = "none"
then
    AC_MSG_ERROR([fuser not found])
fi

# MANDB empty is handled in doc/Submakefile
AC_PATH_PROG(MANDB, mandb, "")

AC_ARG_WITH(rmmod,
    [  --with-rmmod=</path/rmmod>              rmmod variant],
    [
	case "$withval" in
	"" | y | ye | yes)
	    AC_MSG_ERROR([You must supply an argument to --with-rmmod.])
	  ;;
	n | no | none)
	    RMMOD=""
	  ;;
	*)
	    RMMOD="$withval"
	esac
    ],
    [
            RMMOD="$EMC2_BIN_DIR/linuxcnc_module_helper remove"
    ])

AC_ARG_WITH(insmod,
    [  --with-insmod=</path/insmod>            insmod variant],
    [
	case "$withval" in
	"" | y | ye | yes)
	    AC_MSG_ERROR([You must supply an argument to --with-insmod.])
	  ;;
	n | no | none)
	    INSMOD=""
	  ;;
	*)
	    INSMOD="$withval"
	esac
    ],
    [
            INSMOD="$EMC2_BIN_DIR/linuxcnc_module_helper insert"
    ])

##############################################################################
# Subsection 3.7 - check for programs needed to build documentation          #
#                                                                            #
# Check for LyX and other programs we need to build and install docs.        #
# (Optional, if not present, just don't build the docs.)                     #
##############################################################################

AC_ARG_WITH(python,
    [  --with-python=<path>                    Specify the Python interpreter],
    [
        PYTHON=$withval
    ],[
        PYTHON=python
    ])
AC_PATH_PROG(PYTHON,$PYTHON,"none")
if test $PYTHON = "none"
then
    AC_MSG_ERROR([python not found])
fi

AC_MSG_CHECKING([whether to build documentation])
AC_ARG_ENABLE(build-documentation,
    [  --enable-build-documentation[=format]   Build documentation.
                          Format may be "pdf" or "html" if only one format is desired.],
    [
        case "$enableval" in
        Y*|y*|html,pdf|pdf,html)
            BUILD_DOCS=yes
            BUILD_DOCS_PDF=yes
            BUILD_DOCS_HTML=yes
            AC_MSG_RESULT([PDF and HTML requested])
        ;;
        html)
            BUILD_DOCS=yes
            BUILD_DOCS_PDF=no
            BUILD_DOCS_HTML=yes
            AC_MSG_RESULT([HTML requested])
	;;
        pdf)
            BUILD_DOCS=yes
            BUILD_DOCS_PDF=yes
            BUILD_DOCS_HTML=no
            AC_MSG_RESULT([PDF requested])
	;;
        *)
            BUILD_DOCS=no
            AC_MSG_RESULT([no])
        ;;
    esac
    ],
    [
        BUILD_DOCS=no
        AC_MSG_RESULT([no])
    ])

# Programs required for building all documentation
if ( test "$BUILD_DOCS" = "yes" ) ; then
    if ( test "none" = "$PYTHON" ) ; then
        AC_MSG_ERROR([no python, documentation cannot be built])
    fi

    AC_PATH_PROG(ASCIIDOC,asciidoc,"none")
    if ( test "none" = "$ASCIIDOC" ) ; then
	AC_MSG_ERROR([no AsciiDoc, documentation cannot be built])
    fi

    AC_PATH_PROG(A2X,a2x,"none")
    if ( test "none" = "$A2X" ) ; then
	AC_MSG_ERROR([no a2x, documentation cannot be built])
    fi

    AC_MSG_CHECKING([asciidoc version])
    set -- `asciidoc --version`; ASCIIDOC_VER=$2
    set -- `echo $ASCIIDOC_VER | sed 's/\./ /g'`

    if test $1 -lt 8 -o \( $1 -eq 8 -a $2 -lt 5 \); then
        AC_MSG_ERROR([asciidoc version $ASCIIDOC_VER less than 8.5.
Documentation cannot be built.
On Lucid, install the regular asciidoc from Ubuntu by running "sudo apt-get install asciidoc".
On Hardy, install the backported asciidoc available from the linuxcnc.org debian archive.
On other systems, do what you need to install asciidoc version 8.5 or newer.])
    fi
    AC_MSG_RESULT([$ASCIIDOC_VER])

    AC_MSG_CHECKING([whether to specify latex.encoding])
    temp_asciidoc=`tempfile --suffix=.txt`
    cat > $temp_asciidoc <<EOF
:lang: fr
:ascii-ids:

....
umläut nuß
....
EOF
    if $A2X --dblatex-opts "-P latex.encoding=utf8" $temp_asciidoc > /dev/null 2>&1;
    then
        A2X_LATEX_ENCODING="-P latex.encoding=utf8"
        AC_MSG_RESULT(yes)
    else
        A2X_LATEX_ENCODING=""
        AC_MSG_RESULT(no)
    fi
    AC_SUBST(A2X_LATEX_ENCODING)
    rm -f $temp_asciidoc ${temp_asciidoc%.txt}.pdf

    AC_PATH_PROG(DBLATEX,dblatex,"none")
    if ( test "none" = "$DBLATEX" ) ; then
	AC_MSG_ERROR([no dblatex, documentation cannot be built])
    fi

    AC_MSG_CHECKING([dblatex version])
    set -- `dblatex --version`; DBLATEX_VER=$3
    set -- `echo $DBLATEX_VER | sed 's/[[.-]]/ /g'`

    if test $1 -eq 0 -a \( $2 -lt 2 -o \( $2 -eq 2 -a ${3:-0} -lt 12 \) \); then
        AC_MSG_ERROR([dblatex version $DBLATEX_VER less than 0.2.12.
Documentation cannot be built.])
    fi
    AC_MSG_RESULT([$DBLATEX_VER])


    AC_PATH_PROG(SOURCE_HIGHLIGHT,source-highlight,"none")
    if ( test "none" = "$SOURCE_HIGHLIGHT" ) ; then
	AC_MSG_ERROR([no source-highlight, documentation cannot be built])
    fi


    AC_PATH_PROG(CONVERT,convert,"none")
    if ( test "none" = "$CONVERT" ) ; then
	AC_MSG_ERROR([no convert, documentation cannot be built])
    fi

    AC_PATH_PROG(GS,gs,"none")
    if ( test "none" = "$GS" ) ; then
	AC_MSG_ERROR([no gs, documentation cannot be built])
    fi
fi

# Programs required only for building the PDF documentation
if ( test "$BUILD_DOCS_PDF" = "yes" ) ; then
    AC_PATH_PROG(DBLATEX,dblatex,"none")
    if ( test "none" = "$DBLATEX" ) ; then
	AC_MSG_ERROR([no dblatex, PDF documentation cannot be built])
    fi

    AC_PATH_PROG(PDFLATEX,pdflatex,"none")
    if ( test "none" = "$PDFLATEX" ) ; then
	AC_MSG_ERROR([no pdflatex, PDF documentation cannot be built])
    fi
fi

# Programs required only for building the HTML documentation
if ( test "$BUILD_DOCS_HTML" = "yes" ) ; then
    AC_PATH_PROG(XSLTPROC,xsltproc,"none")
    if ( test "none" = "$XSLTPROC" ) ; then
	AC_MSG_ERROR([no xsltproc, HTML documentation cannot be built])
    fi

    AC_PATH_PROG(DVIPNG,dvipng,"none")
    if ( test "none" = "$DVIPNG" ) ; then
	AC_MSG_ERROR([no dvipng, HTML documentation cannot be built])
    fi

    AC_MSG_CHECKING([for HTML support in groff])
    if ! groff -Thtml < /dev/null > /dev/null 2>&1 ; then
        AC_MSG_ERROR([no groff -Thtml, HTML documentation cannot be built])
    else
        AC_MSG_RESULT(yes)
    fi
fi
AC_SUBST(BUILD_DOCS)
AC_SUBST(BUILD_DOCS_PDF)
AC_SUBST(BUILD_DOCS_HTML)

#############################################################################
# Section 4 - Check for math support                                         #
#                                                                            #
# Because some of the realtime kernel modules use floating point math        #
# which is not supported by standard kernel calls, we need to link to        #
# libm. RTAI provides a loadable module derived from uclibc (a minimal       #
# glibc implimentaion for embedded systems). Alternatively, we can link      #
# to dietlibc - Most distros package this as diet or dietlibc. Debian        #
# splits the headers and static libs into dietlibc-dev..                     #
# If neither of the two preferred options are available, then the default    #
# GNU glibc math library will have to be used. This will cause problems      #
# if certain functions found in libc.so are not stubbed, hence the following #
# tests and linking to mathstubs.o at a later stage.                         #
##############################################################################

if test "$SIMULATOR" != "yes"
then
AC_MSG_CHECKING([for kernel math support])
USE_LIBM=""
USE_RTLIBM=""
USE_STUBS=""
MATHLIB=""
MATHINC=""

#we need to make sure that $RTS has a --module-dir option
#only rtai-config has, well rtlinux-config has a --module_dir and lacks the rtai_* modules
if (test ${RTS##*/} = rtai-config ) ; then
    if (test -r $($RTS --module-dir)/rtai_math${MODEXT} ) ; then
       USE_RTLIBM=1
    fi
fi

if (test -r $RTDIR/modules/rtai_libm${MODEXT} ) || (test $USE_RTLIBM) || (test -r /lib/modules/${KERNEL_VERS}/rtai/rtai_libm${MODEXT}) ; then
    # Found a suitable kernel module..
    MATHLIB=
    USE_RTLIBM=1
    AC_MSG_RESULT([ok, using RTAI's libm kernel module])
# Note: need to search /lib/modules/$KERNEL_VER for the math modules..
#       or `$RTS --module-dir`...
else
    AC_MSG_RESULT([no])
    AC_MSG_CHECKING([for a suitable libm])
    rm -f tests/mathtest.o tests/mathtest 2>/dev/null
    # compile a sample first.
    $CC -c tests/mathtest.c $RTFLAGS -o tests/mathtest.o 2>/dev/null
    
    DIET_DIR=$(find /usr/lib -maxdepth 2 -name diet* | head -n1)
    if (test "$DIET_DIR" != "" ) ; then
        # Run a test to see if it will work...
        ld -r -static tests/mathtest.o -o tests/mathtest -L$DIET_DIR/lib-i386 -lm 2>/dev/null
        if (test "$(nm -u tests/mathtest | grep -v ende)" = "" ) ; then
            AC_MSG_RESULT([diet lib OK to use.])
            USE_DIET=1
            MATHLIB="-L$DIET_DIR/lib-i386 -lm"
            MATHINC="-I$DIET_DIR/include"
        fi
    fi
    if (test "$USE_DIET" = "" ) ; then
        # Test the standard libm
    rm -f tests/mathtest 2>/dev/null
    ld -r -static tests/mathtest.o -o tests/mathtest -L/usr/lib -lm 2>/dev/null
        if ! nm -u tests/mathtest ; then
            AC_MSG_RESULT([libm OK to use.])
            USE_LIBM=1
            MATHLIB="-L/usr/lib -lm"
        else
            # We need to link to mathstubs if we are to use the glibc libm
            AC_MSG_WARN([Using glibc libm with mathstubs.
			If unresolved symbols are reported when loading kernel modules
			please file a bug report.])
            USE_LIBM=1
	    USE_STUBS=1
            MATHLIB="-L/usr/lib -lm"
        fi
    fi
    rm -f tests/mathtest.o tests/mathtest 2>/dev/null
fi

AC_SUBST([USE_LIBM])
AC_SUBST([USE_STUBS])
AC_SUBST([USE_RTLIBM])
AC_SUBST([MATHINC])
AC_SUBST([MATHLIB])

# Export substitution symbols for module loading
AC_SUBST([RTAI3_MOD])
AC_SUBST([RTAI_MOD])
AC_SUBST([RTL_MOD])

for m in adeos rtai_hal rtai_ksched rtai_fifos rtai_shm rtai_sem rtai_math \
        rtai rt_mem_mgr rtai_sched rtai_libm \
        rtl mbuff rtl_time rtl_sched rtl_posixio rtl_fifo; do
    AC_MSG_CHECKING([for $m])
    TMP=$(find $MODULE_DIR -name $m$MODEXT 2>/dev/null)
    if test -z "$TMP"; then
        TMP=$(find /lib/modules/$KERNEL_VERS -name $m$MODEXT)
    fi
    eval MODPATH_$m=${TMP/$KERNEL_VERS/\\\$\\\(uname\\\ -r\\\)}
    AC_MSG_RESULT([${TMP:-not found}])
done

AC_SUBST(MODPATH_adeos)
AC_SUBST(MODPATH_rtai_hal)
AC_SUBST(MODPATH_rtai_ksched)
AC_SUBST(MODPATH_rtai_fifos)
AC_SUBST(MODPATH_rtai_shm)
AC_SUBST(MODPATH_rtai_sem)
AC_SUBST(MODPATH_rtai_math)
AC_SUBST(MODPATH_adeos)
AC_SUBST(MODPATH_rtai)
AC_SUBST(MODPATH_rt_mem_mgr)
AC_SUBST(MODPATH_rtai_sched)
AC_SUBST(MODPATH_rtai_shm)
AC_SUBST(MODPATH_rtai_libm)
AC_SUBST(MODPATH_rtl)
AC_SUBST(MODPATH_mbuff)
AC_SUBST(MODPATH_rtl_time)
AC_SUBST(MODPATH_rtl_sched)
AC_SUBST(MODPATH_rtl_posixio)
AC_SUBST(MODPATH_rtl_fifo)
fi

# Checks for header files.

##############################################################################
# Section 4.1 - Important headers, functions and gloabl defines.             #
#                                                                            #
##############################################################################

# This section gets inserted in to config.h.in when autoheader is run.
# Global and common defines should be here rather than duplicated in
# multiple places. Keep screwball constructs out of this, and it can
# be used in both kernel space and user space.
AH_TOP([/********************************************************************
* Description: config.h
*
*	Common defines used in many emc2 source files.
*       To add to this file, edit Section 4.1 in configure.in
*       and then run autoheader.
*
* Author: Autogenerated by autoheader
* License: LGPL Version 2
* System: Linux
*    
* Copyright (c) 2004 All rights reserved.
********************************************************************/
#ifndef EMC2_CONFIG_H
#define EMC2_CONFIG_H

/* LINELEN is used throughout for buffer sizes, length of file name strings,
   etc. Let's just have one instead of a multitude of defines all the same. */
#define LINELEN 255
/* Used in a number of places for sprintf() buffers. */
#define BUFFERLEN 80

#define MM_PER_INCH 25.4
#define INCH_PER_MM (1.0/25.4)
])

AH_VERBATIM([_GNU_SOURCE],
[/* Enable GNU extensions on systems that have them.  */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
])

AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_FUNCS(semtimedop)
LIBS_hold=$LIBS
LIBS="$LIBS -lm"

AC_MSG_CHECKING([for sincos function])
sincos_ok=yes
AC_TRY_LINK(
    [double a, b;],
    [sincos(1.0, &a, &b);],
    AC_DEFINE(
        [HAVE_SINCOS],
        1,
        [Define to 1 if sincos is available.]
    ),
    [sincos_ok=no]
)
AC_MSG_RESULT([$sincos_ok])

AC_CHECK_FUNCS(__sincos)
LIBS=$LIBS_hold

AH_BOTTOM(#endif
)

##############################################################################
# Section 5 - Check for tcl/tk support                                       #
#                                                                            #
# Test for tcl/tk headers fails if they are not in /usr/include              #
# so testing is done by finding tclConfig.sh and tkConfig.sh                 #
# if the search fails the traditional check is done                          #
##############################################################################
AC_MSG_CHECKING([for tcl])

AC_ARG_WITH(tclConfig,
    [  --with-tclConfig=<path>                 Specify the path to tclConfig],
    [
        TCLCONFIG=$withval
    ],[
        TCLCONFIG=
    ])

# if not specified, search for tclConfig.sh
if (test "x$TCLCONFIG" = "x"); then
    TCLCONFIG=$(find /usr/lib* -maxdepth 2 -name tclConfig.sh | head -n1)
    if (test "x$TCLCONFIG" = "x"); then
	TCLCONFIG=$(find /usr/local/lib* -maxdepth 2 -name tclConfig.sh | head -n1)
    fi
fi

if (test "x$TCLCONFIG" = "x"); then
   AC_MSG_RESULT([tclConfig.sh not found, trying tcl.h and libs.
		  If it doesn't work try running ./configure --with-tclConfig=<path to tclConfig.sh>])
   AC_CHECK_HEADERS([tcl.h])
   AC_SEARCH_LIBS(Tcl_Init,[tcl tcl8.1 tcl8.2 tcl8.3 tcl8.4 tcl8.5],[],[AC_MSG_ERROR([tcl lib not found])])
else
# tclConfig.sh found,
# extracting vars
   AC_MSG_RESULT([$TCLCONFIG found])
   . "$TCLCONFIG"
   TCL_CFLAGS="$TCL_INCLUDE_SPEC -DUSE_TCL_STUBS"
   TCL_LIBS="$TCL_STUB_LIB_SPEC"
fi

AC_MSG_CHECKING([for tk])

AC_ARG_WITH(tkConfig,
    [  --with-tkConfig=<path>                  Specify the path to tkConfig],
    [
        TKCONFIG=$withval
    ],[
        TKCONFIG=
    ])

# if not specified, search for tclConfig.sh
if test -z "$TKCONFIG"; then
    TKCONFIG=$(dirname $TCLCONFIG)/tkConfig.sh
    if ! test -f $TKCONFIG; then unset TKCONFIG; fi
fi
if (test "x$TKCONFIG" = "x"); then
    # search for tkConfig.sh
    TKCONFIG=$(find /usr/lib -maxdepth 2 -name tkConfig.sh | head -n1)
    if (test "x$TKCONFIG" = "x"); then
	TKCONFIG=$(find /usr/local/lib -maxdepth 2 -name tkConfig.sh | head -n1)
    fi
fi

if (test "x$TKCONFIG" = "x"); then
   AC_MSG_RESULT([tkConfig.sh not found, trying tk.h and libs.
		  If it doesn't work try running ./configure --with-tkConfig=<path to tkConfig.sh>])
   AC_CHECK_HEADERS([tk.h])
   AC_SEARCH_LIBS(Tk_Init,[tk tk80 tk8.0 tk8.1 tk8.2 tk8.3 tk8.4 tk8.5],[],[AC_MSG_ERROR([tk lib not found])])
else
# tkConfig.sh found,
# extracting vars
   AC_MSG_RESULT([$TKCONFIG found])
   . "$TKCONFIG"
   TK_CFLAGS="$TK_INCLUDE_SPEC $TK_XINCLUDES" 
   TK_LIBS="$TK_LIBS $TK_LIB_SPEC"
fi

if ! test -z "$TCL_VERSION" && ! test -z "$TK_VERSION" && ! test "$TCL_VERSION" = "$TK_VERSION" ; then
    AC_MSG_ERROR([Tcl and Tk versions must be the same, but configure found
Tcl $TCL_VERSION and Tk $TK_VERSION.  You can use --with-tkConfig= and --with-tclConfig=
to override the autodetected versions.])
fi

if test -f $TCL_EXEC_PREFIX/bin/wish$TCL_VERSION; then
    WISH=$TCL_EXEC_PREFIX/bin/wish$TCL_VERSION
elif test -f $TCL_EXEC_PREFIX/bin/wish; then
    WISH=$TCL_EXEC_PREFIX/bin/wish
else
    WISH=wish
fi

if test -f $TCL_EXEC_PREFIX/bin/tclsh$TCL_VERSION; then
    TCLSH=$TCL_EXEC_PREFIX/bin/tclsh$TCL_VERSION
elif test -f $TCL_EXEC_PREFIX/bin/tclsh; then
    TCLSH=$TCL_EXEC_PREFIX/bin/tclsh
else
    TCLSH=tclsh
fi

AC_MSG_CHECKING([for BWidget using $TCLSH])
if (unset DISPLAY; echo ["catch { package require bwidget }; exit [expr [lsearch [package names] BWidget] == -1]"] | $TCLSH); then
    AC_MSG_RESULT([found])
else
    AC_MSG_ERROR([not found])
fi

HAVE_WORKING_BLT=no
AC_MSG_CHECKING([for BLT using tclsh8.5])
if (test -f $TCL_EXEC_PREFIX/bin/tclsh8.5) && (echo ['if [catch {package require BLT}] { exit 1; } else { exit 0; }'] | tclsh8.5); then
    AC_MSG_RESULT([found])
    HAVE_WORKING_BLT=yes
else
    AC_MSG_RESULT([not found])
fi

AC_SUBST([TCL_DBGX])
AC_SUBST([TK_DBGX])
AC_SUBST([TCL_CFLAGS])
AC_SUBST([TK_CFLAGS])
AC_SUBST([TCL_LIBS])
AC_SUBST([TK_LIBS])
AC_SUBST([WISH])
AC_SUBST([TCLSH])
AC_SUBST([HAVE_WORKING_BLT])

##############################################################################
# Section 6 - Miscelaneous support                                           #
#                                                                            #
# Subsection 6.1. - provides DATE to output it to the generated files        #
# Subsection 6.2. - user option to convince make to be quiet during make     #
##############################################################################

# provides DATE in order to output it to the generated files
DATE=$(date)
AC_SUBST([DATE])
AC_PATH_XTRA
#X_CFLAGS gets set
#X_LIBS and X_EXTRA_LIBS likewise

AC_SUBST([X_LIBS])
AC_SUBST([X_CFLAGS])

AC_CHECK_HEADERS(X11/extensions/Xinerama.h,
	    [HAVE_XINERAMA=yes],
	    [HAVE_XINERAMA=no])

AC_CHECK_LIB(Xinerama, XineramaQueryExtension,
	    [],
	    [HAVE_XINERAMA=no])

if test "x$HAVE_XINERAMA" = "xno"; then
    AC_MSG_ERROR([Xinerama library or headers not found])
fi

# check for Xaw, that is needed for emc2/src/emc/usr_intf/xemc.cc

if test "$have_x" = "yes"; then
CFLAGS_hold=$CFLAGS
LIBS_hold=$LIBS
CFLAGS="$CFLAGS $X_CFLAGS"
LIBS="$LIBS $X_PRE_LIBS $X_LIBS"

AC_CHECK_HEADERS(X11/Xaw/XawInit.h,
	    [HAVE_XAW=yes],
	    [HAVE_XAW=no],
	    [[#include <X11/Intrinsic.h>
	    ]])

AC_CHECK_LIB(Xaw, XawDialogGetValueString,
	    [],
	    [HAVE_XAW=no])
CFLAGS=$CFLAGS_hold
LIBS=$LIBS_hold
fi

if test "x$HAVE_XAW" = "xno"; then
    AC_MSG_WARN([Xaw lib missing, you won't be able to build/run xemc. try installing it with 'apt-get install libxaw7-dev'])
else    
    XAW_LIBS="$X_PRE_LIBS $X_LIBS -lX11 -lXaw -lXt"
fi
AC_SUBST([HAVE_XAW])
AC_SUBST([XAW_LIBS])

#clean out LIBS, or else all future tests will check with -lXaw (which fails on odd systems)
temp_LIBS=$LIBS
LIBS=

# check for readline.h and friends, optional for halcmd
AC_CHECK_HEADERS(readline/readline.h, [HAVE_READLINE=yes], [HAVE_READLINE=no])
AC_CHECK_HEADERS(readline/history.h, [], [HAVE_READLINE=no])

echo "Trying readline without -ltermcap"
AC_CHECK_LIB(readline, readline, [HAVE_READLINE_NOTERMCAP=yes], [])
unset ac_cv_lib_readline_readline
echo "Trying readline with -ltermcap"
AC_CHECK_LIB(readline, readline, [HAVE_READLINE_TERMCAP=yes], [], -ltermcap)
unset ac_cv_lib_readline_readline

if test "x$HAVE_READLINE_NOTERMCAP" = "xyes"; then
    echo "Readline does not require -ltermcap"
    READLINE_LIBS="-lreadline"
elif test "x$HAVE_READLINE_TERMCAP" = "xyes"; then
    echo "Readline requires -ltermcap"
    READLINE_LIBS="-lreadline -ltermcap"
else
    HAVE_READLINE=no
fi

if test "x$HAVE_READLINE" = "xyes"; then
    AC_DEFINE([HAVE_READLINE], [], [Define to 1 if you have the 'readline' library (-lreadline) and required headers])
else
    AC_MSG_ERROR([Required library 'readline' missing.])
fi
AC_SUBST([HAVE_READLINE])
AC_SUBST([READLINE_LIBS])


#check for ncurses.h needed for emc2/src/emc/usr_intf/keystick.cc

AC_CHECK_HEADERS(ncurses.h,
	    [HAVE_NCURSES=yes],
	    [HAVE_NCURSES=no])

AC_CHECK_LIB(ncurses, initscr,
	    [],
	    [HAVE_NCURSES=no])

if test "x$HAVE_NCURSES" = "xno"; then
    AC_MSG_WARN(ncurses lib missing, you won't be able to build/run keystick. try installing it with 'apt-get install libncurses-dev')
else    
    NCURSES_LIBS="-lncurses"
fi
AC_SUBST([HAVE_NCURSES])
AC_SUBST([NCURSES_LIBS])


##############################################################################
# Section 7 - Language support                                               #
#                                                                            #
# i18n Language support:                                                     #
# see http://cvs.gnome.org/viewcvs/intltool/doc/I18N-HOWTO?rev=1.3           #
##############################################################################

# if you don't have nls tools, allow a way out!

AC_ARG_ENABLE(nls,
              [  --disable-nls           don't use NLS],
              USE_NLS=no, USE_NLS=yes)
AC_SUBST(USE_NLS)

if test "$USE_NLS" = "yes"; then
  AC_CHECK_HEADERS(locale.h)
  AC_CHECK_FUNCS(setlocale)
  AC_SUBST(HAVE_LOCALE_H)  

# Add languages here when they are translated.
# Grep the LINGUAS file.
  LANGUAGES=""
  AC_SUBST(LANGUAGES)  
  AC_CHECK_HEADERS(libintl.h)
  AC_CHECK_LIB(intl,gettext)
  AC_SUBST(HAVE_LIBINTL_H)  
  AC_SUBST(HAVE_LIBINTL)

# The default locale directory is /usr/share/locale - This is generally fixed
# by the distribution....
  dnl Handle localedir
  LOCALEDIR='/usr/share/locale'
  AC_ARG_WITH(locale-dir,
  [  --with-locale-dir=DIR   Location of the locale file(s)
                          [DATADIR/locale]],[
    if test x$withval = xyes; then
      AC_MSG_WARN(Usage is: --with-locale-dir=basedir)
    else
      if test x$withval = xno; then
        AC_MSG_WARN(Usage is: --with-locale-dir=basedir)
      else
        LOCALEDIR=$withval
      fi
    fi
  ])
  AC_SUBST(LOCALEDIR)
fi

#restore LIBS
LIBS="$X_LIBS $temp_LIBS"

AC_PATH_PROG(XGETTEXT,xgettext,none)
XGETTEXT_TCL_WORKS=no
if test "$XGETTEXT" = "none"; then
   AC_MSG_ERROR(install gettext 0.14.5 or above)
else
   AC_MSG_CHECKING(for xgettext tcl bug)
   set -- $(xgettext --version | head -1)
   version=$4
   set -- $(echo $4 | tr . " ")
   if test ${1-0} -gt 0 -o \( ${1-0} -eq 0 -a \( ${2-0} -gt 14 -o \( ${2-0} -eq 14 -a ${3-0} -ge 5 \) \) \)
   then
	XGETTEXT_TCL_WORKS=yes
	AC_MSG_RESULT(xgettext version $version OK)
    else
	AC_MSG_RESULT(xgettext version $version buggy -- need at least 0.14.5)
        AC_MSG_ERROR(install xgettext 0.14.5 or above)
   fi
fi
AC_SUBST(XGETTEXT)

AC_PATH_PROG(MSGFMT,msgfmt,none)
if test "$MSGFMT" = "none"; then
   AC_MSG_ERROR(install gettext 0.14.5 or above)
else
    AC_MSG_CHECKING(for --tcl support in msgfmt)
    if $MSGFMT --tcl -l de -d /tmp /dev/null > /dev/null 2>&1; then
        AC_MSG_RESULT(yes)
    else
        AC_MSG_ERROR(install xgettext 0.14.5 or above)
    fi
fi
AC_SUBST(MSGFMT)

AC_MSG_CHECKING(for Python support)
AC_ARG_ENABLE(python,
    [  --disable-python        Disable the parts of LinuxCNC that depend on Python],
    [
	case "$enableval" in
	Y*|y*)
	    BUILD_PYTHON=yes ;;
	*)
	    BUILD_PYTHON=no ;;
	esac
    ],
    [BUILD_PYTHON=yes])

AC_MSG_RESULT($BUILD_PYTHON)

if test "$BUILD_PYTHON" = "yes"; then
    if test "$PYTHON" = "none"; then
	AC_MSG_ERROR([Python missing.  Install it or specify --disable-python to skip the parts of LinuxCNC that depend on Python])
    fi

    AC_MSG_CHECKING([python version])
    if ! $PYTHON -c 'import sys;raise SystemExit,sys.hexversion<0x2040000'
    then
	AC_MSG_ERROR(Python version too old (2.4 or newer required))
    fi
    AC_MSG_RESULT(OK)

    AC_MSG_CHECKING([version of python libraries])
    LIBPYTHON=`$PYTHON -c 'import distutils.sysconfig; print "python" + distutils.sysconfig.get_config_vars().get("VERSION")'`
    AC_MSG_RESULT($LIBPYTHON)

    AC_MSG_CHECKING([match between tk and Tkinter versions])
	
    PYTHON_TK_VERSION="`$PYTHON -c 'import _tkinter; print _tkinter.TK_VERSION'`"
    PYTHON_TCL_VERSION="`$PYTHON -c 'import _tkinter; print _tkinter.TCL_VERSION'`"

    if test "$PYTHON_TCL_VERSION" != "$TCL_VERSION"; then
	AC_MSG_RESULT([TCL mismatch: $TCL_VERSION vs $PYTHON_TCL_VERSION])
	AC_MSG_ERROR([Python requires use of Tcl $PYTHON_TCL_VERSION and Tk $PYTHON_TK_VERSION.
Install this version and specify --with-tclConfig and --with-tkConfig if necessary])
    fi
    if test "$PYTHON_TK_VERSION" != "$TK_VERSION"; then
	AC_MSG_RESULT([Tk mismatch: $TK_VERSION vs $PYTHON_TK_VERSION])
	AC_MSG_ERROR(["Python requires use of Tk $TK_VERSION.  Install this version and specify --with-tkConfig if necessary"])
    fi
    AC_MSG_RESULT([$PYTHON_TK_VERSION])

    AC_MSG_CHECKING(location of Python header files)
    INCLUDEPY=`$PYTHON -c 'import distutils.sysconfig; print distutils.sysconfig.get_config_vars().get("INCLUDEPY")'`
    AC_MSG_RESULT($INCLUDEPY)

    AC_MSG_CHECKING([for Python headers])
    AS_IF([test "x$PYTHON_CPPFLAGS" = x],
    [
	_PYTHON_SYSCONFIG([[get_python_inc()]], [PYTHON_CPPFLAGS="-I$py_val"])
  	_PYTHON_SYSCONFIG([[get_python_inc(True)]],
  	[test "x$PYTHON_CPPFLAGS" = "x-I$py_val" || PYTHON_CPPFLAGS="$PYTHON_CPPFLAGS -I$py_val"])
    ])
    AC_MSG_RESULT([$PYTHON_CPPFLAGS])
    AC_SUBST(PYTHON_CPPFLAGS)

    AC_MSG_CHECKING([for Python libraries])
    AS_IF([test "x$PYTHON_LIBS" = x],
    [
	_PYTHON_SYSCONFIG([[get_config_var('LIBS')]], [PYTHON_LIBS=$py_val])
    ])
    AC_MSG_RESULT([$PYTHON_LIBS])
    AC_SUBST(PYTHON_LIBS)

    # Get the CFLAGS and LIBS for boost::python.
    # This does an AC_SUBST() of BOOST_PYTHON_LIBS
    # For the CFLAGS we must assume that boost is at the top-level, for instance in /usr/include/:
    AX_BOOST_PYTHON_EMC

    AC_CHECK_HEADER($INCLUDEPY/Python.h,[],
	[AC_MSG_ERROR([Required header Python.h missing.  Install it, or specify --disable-python to skip the parts of LinuxCNC that depend on Python])])

    AC_MSG_CHECKING(for site-package location)
    SITEPY=`$PYTHON -c 'import distutils.sysconfig; print distutils.sysconfig.get_python_lib()'`
    AC_MSG_RESULT($SITEPY)

    AC_CHECK_HEADERS(GL/gl.h GL/glu.h,[],[AC_MSG_ERROR([Required OpenGL header missing.  Install it, or specify --disable-python to skip the parts of LinuxCNC that depend on Python])])

    AC_CHECK_LIB(GL, glBegin, [], [AC_MSG_ERROR([Required GL library missing.  Install it, or specify --disable-python to skip the parts of LinuxCNC that depend on Python])])

    AC_MSG_CHECKING(for working GLU quadrics)
    AC_TRY_COMPILE([
#include <GL/gl.h>
#include <GL/glu.h>
],
	[GLUquadric *q;],
	[AC_MSG_RESULT(yes)],[
		AC_MSG_ERROR([Required GLU library missing.  Install it or specify --disable-python to skip the parts of LinuxCNC that depend on Python])]
	
    )

    AC_MSG_CHECKING(for Xmu headers)
    AC_CHECK_HEADERS(X11/Xmu/Xmu.h,[],[AC_MSG_ERROR([Required Xmu header missing.  Install it, or specify --disable-python to skip the parts of LinuxCNC that depend on Python])])
fi

AC_MSG_CHECKING(libgl1-mesa-dri workaround)

PRELOAD_WORKAROUND=`../scripts/test-libgl-bug.sh 2>/dev/null`
if test  $? -ne 0 ; then
   # real bad, the test failed - let me know
   AC_MSG_RESULT([test for libgl1-mesa-dri workaround failed, please file a bug])
   exit 1
fi
if test "x$PRELOAD_WORKAROUND" != "x" ; then
   AC_MSG_RESULT(required - need to preload $PRELOAD_WORKAROUND)
else
   AC_MSG_RESULT(not required)
fi

AC_SUBST([BUILD_PYTHON])
AC_SUBST([LIBPYTHON])
AC_SUBST([INCLUDEPY])
AC_SUBST([SITEPY])


##############################################################################
# files that get created by ./configure                                      #
##############################################################################
#AC_CONFIG_FILES([../configs/rtapi.conf ../configs/emc.conf ../configs/hal.conf config.h])
AC_CONFIG_FILES([../scripts/rtapi.conf])
AC_CONFIG_FILES([../scripts/linuxcnc], [chmod +x ../scripts/linuxcnc])
AC_CONFIG_FILES([../scripts/halrun], [chmod +x ../scripts/halrun])
AC_CONFIG_FILES([../scripts/rip-environment], [chmod +x ../scripts/rip-environment])
AC_CONFIG_FILES([../scripts/haltcl], [chmod +x ../scripts/haltcl])
AC_CONFIG_FILES([../scripts/realtime], [chmod +x ../scripts/realtime])
AC_CONFIG_FILES([../scripts/linuxcnc_var], [chmod +x ../scripts/linuxcnc_var])
AC_CONFIG_FILES(Makefile.inc)
AC_CONFIG_FILES(Makefile.modinc)
AC_CONFIG_FILES(../tcl/linuxcnc.tcl)
AC_CONFIG_FILES(../lib/python/nf.py)
AC_CONFIG_FILES([../scripts/linuxcncmkdesktop], [chmod +x ../scripts/linuxcncmkdesktop])
AC_CONFIG_FILES(../share/applications/linuxcnc-latency.desktop)
AC_CONFIG_FILES(../share/applications/linuxcnc.desktop)
AC_CONFIG_FILES(../share/desktop-directories/cnc.directory)
AC_CONFIG_FILES(../share/menus/CNC.menu)
AC_OUTPUT()

##############################################################################
# message to the user what to do next, after a succesfull ./configure        #
##############################################################################
bold () {
    if tty > /dev/null 2>&1 && type -path tput > /dev/null 2>&1;
    then
        tput smso
    fi
}

offbold () {
    if tty > /dev/null 2>&1 && type -path tput > /dev/null 2>&1;
    then
        tput rmso
    fi
}

echo ""
echo ""
echo "######################################################################"
echo "#                LinuxCNC - Enhanced Machine Controller              #"
echo "######################################################################"
echo "#                                                                    #"
echo "#   LinuxCNC is a software system for computer control of machine    #"
echo "#   tools such as milling machines. LinuxCNC is released under the   #"
echo "#   GPL.  Check out http://www.linuxcnc.org/ for more details.       #"
echo "#                                                                    #"
echo "#                                                                    #"
echo "#   It seems that ./configure completed successfully.                #"
if test $SIMULATOR = yes; then
echo "#   However, the configuration is for a simulator only, no RT        #"
echo "#   capabilities will be used. In this mode no control of hardware   #"
echo "#   is possible. Drivers won't be built.                             #"
else
echo "#   This means that RT is properly installed                         #"
fi
echo "#   If things don't work check config.log for errors & warnings      #"
echo "#                                                                    #"

if test "xyes" = "x$RUN_IN_PLACE"; then
echo "#   Next compile by typing                                           #"
echo "#         make                                                       #"
if test $SIMULATOR != yes; then
echo "#         sudo make setuid                                           #"
fi
echo "#                                                                    #"
echo "#   Before running the software, set the environment:                #"
echo "#         . (top dir)/scripts/rip-environment                        #"
else
bold
echo "#   warning: If you already have an installed linuxcnc, this will    #"
echo "#         replace an existing installation.  If you have installed   #"
echo "#         a linuxcnc package, this will damage the package.          #"
offbold
echo "#   hint: To test a self-built version of linuxcnc without damaging  #"
echo "#         the package version, don't specify a --prefix              #"
echo "#                                                                    #"
echo "#   Next compile by typing                                           #"
echo "#         make                                                       #"
echo "#   then install it by typing                                        #"
echo "#         sudo make install                                          #"
fi
echo "#                                                                    #"
echo "#   To run the software type                                         #"
echo "#         linuxcnc                                                   #"
echo "#                                                                    #"
echo "######################################################################"
echo ""
echo ""