1# Check for GMP
2# Zhuliang Chen, 2004-07
3# Modified by Pascal Giorgi, 2003-12-03
4
5dnl IML_CHECK_GMP ([MINIMUM-VERSION [, ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]])
6dnl
7dnl Test for the GNU Multiprecision library and define GMP_CFLAGS and GMP_LIBS
8
9AC_DEFUN([IML_CHECK_GMP],
10[
11
12AC_ARG_WITH(gmp-include,
13	     [  --with-gmp-include=<path>
14		Specify the path of GMP header gmp.h
15		If argument is <empty> that means the header is reachable
16		with the default search path: /usr/include or /usr/local/include
17		or other path you add as default path using --with-default.
18		Otherwise you give the <path> to the directory which contains
19		the header gmp.h.
20	     ],
21             [
22		GMP_INPUT_HEADER=$withval
23		GMP_HEADER_PATH="${GMP_INPUT_HEADER}"
24		for GMP_HOME in ${DEFAULT_CHECKING_PATH}
25		do
26			GMP_HEADER_PATH="${GMP_HEADER_PATH} ${GMP_HOME}/include"
27		done
28	     ],
29	     [
30		for GMP_HOME in ${DEFAULT_CHECKING_PATH}
31		do
32			GMP_HEADER_PATH="${GMP_HEADER_PATH} ${GMP_HOME}/include"
33		done
34	    ])
35
36AC_ARG_WITH(gmp-lib,
37	     [  --with-gmp-lib=<path>
38		Specify the path of GMP library.
39	        If argument is <empty> that means the header is reachable
40                with the default search path: /usr/include or /usr/local/include
41		or path you add as default path using --with-default.
42		Otherwise you give the <path> to the directory which contains
43		the GMP library.
44	     ],
45	     [
46		GMP_INPUT_LIB=$withval
47		GMP_LIBRARY_PATH="${GMP_INPUT_LIB}"
48		for GMP_HOME in ${DEFAULT_CHECKING_PATH}
49		do
50			GMP_LIBRARY_PATH="${GMP_LIBRARY_PATH} ${GMP_HOME}/lib"
51		done
52	     ],
53	     [
54		for GMP_HOME in ${DEFAULT_CHECKING_PATH}
55		do
56			GMP_LIBRARY_PATH="${GMP_LIBRARY_PATH} ${GMP_HOME}/lib"
57		done
58	    ])
59
60
61dnl input data check
62if test  "x${GMP_INPUT_HEADER}" != x; then
63	if test -z "${GMP_INPUT_LIB}"; then
64		echo 'error: since you have specified the GMP header path, you must specify the'
65		echo 'GMP library path using --with-gmp-lib.'
66		exit 1
67	fi
68fi
69
70if test "x${GMP_INPUT_LIB}" != x; then
71	if test -z "${GMP_INPUT_HEADER}"; then
72		echo 'error: since you have specified the GMP library path, you must specify the'
73		echo 'GMP header path using --with-gmp-include.'
74		exit 1
75	fi
76fi
77
78min_gmp_version=ifelse([$1], ,3.1.1,$1)
79
80dnl Check for existence
81BACKUP_CFLAGS=${CFLAGS}
82BACKUP_LIBS=${LIBS}
83
84AC_MSG_CHECKING(for GMP >= $min_gmp_version)
85
86gmp_found="no"
87
88
89for GMP_HEADER in ${GMP_HEADER_PATH}
90do
91	if test -r "${GMP_HEADER}/gmp.h"; then
92		if test "x${GMP_HEADER}" != "x/usr/include" -a "x${GMP_HEADER}" != "x/usr/local/include"; then
93			for GMP_LIBRARY in ${GMP_LIBRARY_PATH}
94			do
95				if test "x${GMP_LIBRARY}" != "x/usr/lib" -a "x${GMP_LIBRARY}" != "x/usr/local/lib"; then
96					GMP_CFLAGS="-I${GMP_HEADER}"
97					GMP_LIBS="${GMP_LIBS} -L${GMP_LIBRARY} -lgmp"
98				fi
99			done
100		else
101			GMP_CFLAGS=
102			GMP_LIBS="-lgmp"
103		fi
104
105		CFLAGS="${CFLAGS} ${GMP_CFLAGS}"
106		LIBS="${LIBS} ${GMP_LIBS}"
107
108
109		AC_TRY_LINK(
110		[#include <gmp.h>],
111		[mpz_t a; mpz_init (a);],
112		[
113        		AC_TRY_RUN(
114 			[#include <gmp.h>
115			 int main() { if (__GNU_MP_VERSION < 3) return -1;
116	 			      else return 0; }
117		  	],[
118				AC_MSG_RESULT(found)
119				gmp_found="yes"
120				AC_SUBST(GMP_CFLAGS)
121		  		AC_SUBST(GMP_LIBS)
122				AC_DEFINE(HAVE_GMP,1,[Define if GMP is installed])
123				# See if we are running GMP 4.0
124	   			AC_MSG_CHECKING(whether GMP is 4.0 or greater)
125		   		AC_TRY_RUN(
126		   		[#include <gmp.h>
127	    			int main () { if (__GNU_MP_VERSION < 4) return -1; else return 0; }
128	   			],[
129					AC_MSG_RESULT(yes)
130				],[
131					AC_MSG_RESULT(no)
132					AC_DEFINE(GMP_VERSION_3,1,[Define if GMP is version 3.xxx])
133					GMP_VERSION="-DGMP_VERSION_3"
134					AC_SUBST(GMP_VERSION)
135				],[
136					dnl This should never happen
137					AC_MSG_RESULT(no)
138				])
139				ifelse([$2], , :, [$2])
140				break
141			],[
142				gmp_problem="$gmp_problem $GMP_HEADER"
143				unset GMP_CFLAGS
144				unset GMP_LIBS
145			],[
146				AC_MSG_RESULT(unknown)
147				gmp_found="yes"
148				echo 'You appear to be cross compiling, so there is'
149				echo 'no way to determine whether your GMP version is new enough.'
150				echo 'I am assuming it is'
151				AC_SUBST(GMP_CFLAGS)
152				AC_SUBST(GMP_LIBS)
153				AC_DEFINE(HAVE_GMP,1,[Define if GMP is installed])
154				ifelse([$2], , :, [$2])
155				break
156			])
157		],[
158		unset GMP_CFLAGS
159		unset GMP_LIBS
160		])
161	fi
162done
163
164if test "x$gmp_found" = "xno"; then
165	if test -n "$gmp_problem"; then
166		AC_MSG_RESULT(problem: your GMP version is too old. Disabling.)
167	else
168		AC_MSG_RESULT(not found)
169	fi
170	ifelse($3, , :, $3)
171fi
172
173CFLAGS=${BACKUP_CFLAGS}
174LIBS=${BACKUP_LIBS}
175#unset LD_LIBRARY_PATH
176
177])
178