1dnl a lower case to upper case utility function
2define(tvf_upper,[translit($1,abcdefghijklmnopqrstuvwxyz,ABCDEFGHIJKLMNOPQRSTUVWXYZ)])
3
4dnl set the variable in the first argument to the value in the second if
5dnl the c++ compiler is a GNU compiler.  Export the variable as a substitution
6AC_DEFUN([TVF_PROG_GXX_SUBST],[
7AC_REQUIRE([AC_PROG_CXX])
8# Set g++ specific compiler options
9if test "$GXX" = "yes" ; then
10	$1="$2"
11else
12	$1=""
13fi
14AC_SUBST($1)
15])
16
17dnl set the variable in the first argument to the value in the second if
18dnl the C compiler is a GNU compiler
19AC_DEFUN([TVF_PROG_GCC_SUBST],[
20AC_REQUIRE([AC_PROG_CC])
21# Set g++ specific compiler options
22if test "$GCC" = "yes" ; then
23	$1="$2"
24else
25	$1=""
26fi
27AC_SUBST($1)
28])
29
30dnl if the install doesn't handle -d properly, fall back to the install script
31AC_DEFUN([TVF_PROG_INSTALL_D],[
32AC_REQUIRE([AC_PROG_INSTALL])
33AC_CACHE_CHECK(whether $INSTALL supports -d, tvf_cv_prog_install_d,
34	if $INSTALL -d /tmp/test$$ && test -d /tmp/test$$ ; then
35		tvf_cv_prog_install_d="yes"
36	else
37		tvf_cv_prog_install_d="no"
38	fi
39	rm -rf /tmp/test$$
40)
41
42if test "$tvf_cv_prog_install_d" = "no" ; then
43	INSTALL=$ac_install_sh
44fi
45])
46
47dnl check for various methods of making dependencies, from the mkdep
48dnl script to copying precomputed defaults.  Both MKDEP and MKDEPFLAGS are
49dnl set and seported via AC_SUBST
50AC_DEFUN([TVF_PROG_MKDEP],[
51AC_CHECK_PROGS(MKDEP,mkdep gcc, cp)
52define(tvf_extra_flags,$1)
53# depending on which mkdep method has been found, the args are
54# slightly different.  -f is redundant on FreeBSD, but needed other places.
55# The test for that is completely superfluous.
56if test $ac_ext = "C" || test $ac_ext = "cc" ; then
57	tvf_compflags='${CXXFLAGS}'
58	tvf_mk_gcc=g++
59	tvf_mk_include="<iostream>"
60else
61	tvf_compflags='${CFLAGS}'
62	tvf_mk_gcc=gcc
63	tvf_mk_include="<stdio.h>"
64fi
65
66# make sure that mkdep plays well with others.  Be careful.  Bad mkdep
67# implementations are very cranky about parameter order.
68if test "$MKDEP" = mkdep; then
69	tvf_here=`pwd`
70	cd /tmp
71	mkdir test.$$
72	cd test.$$
73	# solaris mkdep wants a .depend there already
74	touch .depend
75	touch test.h
76	cat > test.$ac_ext << END
77#include $tvf_mk_include
78#include "test.h"
79int main(int argc, char **argv) { }
80END
81	$MKDEP -f .depend tvf_extra_flags -MM test.$ac_ext 2> /dev/null > /dev/null
82	grep test.h .depend > /dev/null 2>&1
83	if  test "$?" != "0"; then
84		# MKDEP does not play well with us, use gcc or cp
85		cd $tvf_here
86		unset ac_cv_prog_MKDEP
87		unset MKDEP
88		AC_CHECK_PROGS(MKDEP,gcc, cp)
89	else
90		cd $tvf_here
91	fi
92	rm -rf /tmp/test.$$
93fi
94
95case "$MKDEP" in
96	mkdep)
97		if test `uname` = 'FreeBSD'; then
98			MKDEPFLAGS="tvf_extra_flags -MM $tvf_compflags \${SOURCES}"
99		else
100			MKDEPFLAGS="-f .depend tvf_extra_flags -MM $tvf_compflags \${SOURCES}"
101		fi
102	;;
103	gcc)
104		MKDEP=$tvf_mk_gcc
105		MKDEPFLAGS="-MM $tvf_compflags \${SOURCES} >> .depend"
106	;;
107	cp)
108		MKDEPFLAGS='default_depend .depend'
109	;;
110esac
111undefine([tvf_extra_flags])
112AC_SUBST(MKDEPFLAGS)
113])
114
115dnl See if the given function is both present and declared.  The first
116dnl argument is the function to check, the second is a space spearated
117dnl list of headers to check and the last is an optional CHECK or REPLACE
118dnl that determines whether AC_CHECK_FUNCS or AC_REPLACE_FUNCS is called
119dnl internally.  Both the usual HAVE_function and function_DECLARED are
120dnl AC_DEFINEd.
121AC_DEFUN([TVF_FUNC_DECL],[
122define(tvf_call,[AC_]ifelse($3,,CHECK,$3)[_FUNCS(]$1[)])
123tvf_call
124if test "$ac_cv_func_$1" = "yes" ; then
125	AC_CACHE_CHECK(for declaration of $1,tvf_cv_decl_$1,
126		for f in $2; do
127			AC_EGREP_HEADER($1,$f,tvf_cv_decl_$1="yes",
128					tvf_cv_decl_$1="no")
129			if test $tvf_cv_decl_$1 = "yes"; then
130				break;
131			fi
132		done
133	)
134	if test "$tvf_cv_decl_$1" = "yes"; then
135		AC_DEFINE(tvf_upper($1)_DECLARED)
136	fi
137fi
138undefine([tvf_call])
139])
140
141dnl determine if one of rand or random is present (preferring random)
142dnl and if they are declared.  HAVE_RANDOM and RANDOM_DECLARED (or their
143dnl RAND analogs) are set.  stdlib.h and math.h are checked.  If the function
144dnl is declated in math.h RANDOM_IN_MATH or the analog is defined
145AC_DEFUN([TVF_DECL_RANDOM], [
146if test "x$ac_cv_header_stdc" = "x" -a "x$ac_cv_header_stdlib_h" = "x";
147then
148	AC_HEADER_STDC
149	if test $ac_cv_header_stdc = "no"; then
150		AC_CHECK_HEADERS(stdlib.h)
151	fi
152fi
153AC_CHECK_FUNCS(random)
154if test $ac_cv_func_random = "no"; then
155	AC_CHECK_FUNCS(rand)
156else
157	# not really, but we never need it if we have random
158	ac_cv_func_rand="no"
159fi
160
161if test "$ac_cv_func_random" = "no" -a "$ac_cv_func_rand" = "no";  then
162	# It's possible to get here because the above tests failed due to
163	# autoconf freakiness.  These tests are more portable.  Look for random
164	# with either a long or int return type, and try rand with an int.  If
165	# none of these show up, punt.
166	AC_TRY_LINK([
167		extern "C" { long random(); }
168	], [
169		long x = random();
170	],
171	AC_DEFINE(HAVE_RANDOM)
172	ac_cv_func_random="yes",
173	AC_TRY_LINK([
174		extern "C" { int random(); }
175	], [
176		int x = random();
177	],
178	AC_DEFINE(HAVE_RANDOM)
179	ac_cv_func_random="yes"))
180	AC_TRY_LINK([
181		extern "C" { int rand(); }
182	], [
183		int x = rand();
184	],
185	AC_DEFINE(HAVE_RAND)
186	ac_cv_func_random="yes")
187	if test "$ac_cv_func_random" = "no" -a $ac_cv_func_rand = "no";  then
188		AC_MSG_ERROR(requires either random or rand)
189	fi
190fi
191
192if test "$ac_cv_func_random" = "yes" ; then
193AC_CACHE_CHECK(for location of random() declaration,tvf_cv_header_random_declared,[
194	if test "$ac_cv_header_stdc" = "yes" -o "x$ac_cv_header_stdlib_h" = "xyes";
195	then
196		AC_EGREP_HEADER(srandom,stdlib.h,tvf_cv_header_random_declared="stdlib.h",tvf_cv_header_random_declared="none")
197	else
198		tvf_cv_header_random_declared="none"
199	fi
200
201	if test "$tvf_cv_header_random_declared" = "none"; then
202		#check math.h
203		AC_EGREP_HEADER(srandom,math.h,tvf_cv_header_random_declared="math.h",tvf_cv_header_random_declared="none")
204	fi
205
206]
207)
208case "$tvf_cv_header_random_declared" in
209	"stdlib.h" )
210		AC_DEFINE(RANDOM_DECLARED)
211	;;
212	"math.h" )
213		AC_DEFINE(RANDOM_DECLARED)
214		AC_DEFINE(RANDOM_IN_MATH)
215	;;
216esac
217else
218AC_CACHE_CHECK(for location of rand() declaration,tvf_cv_header_rand_declared,[
219	if test "$ac_cv_header_stdc" = "yes"; then
220		AC_EGREP_HEADER(srand,stdlib.h,tvf_cv_header_rand_declared="stdlib.h",tvf_cv_header_rand_declared="none")
221	else
222		tvf_cv_header_rand_declared="none"
223	fi
224
225	if test "$tvf_cv_header_rand_declared" = "none"; then
226		#check math.h
227		AC_EGREP_HEADER(srand,math.h,tvf_cv_header_rand_declared="math.h",tvf_cv_header_rand_declared="none")
228	fi
229
230]
231)
232case "$tvf_cv_header_rand_declared" in
233	"stdlib.h" )
234		AC_DEFINE(RAND_DECLARED)
235	;;
236	"math.h" )
237		AC_DEFINE(RAND_DECLARED)
238		AC_DEFINE(RAND_IN_MATH)
239	;;
240esac
241fi
242])
243
244dnl see if optarg is defined in unistd.h and cache the result.
245dnl OPTARG_DEFINED is set if optarg is found.
246AC_DEFUN([TVF_DECL_OPTARG],[
247if test "$ac_cv_header_unistd_h" = "yes" ; then
248AC_CACHE_CHECK(for optarg in unistd.h,tvf_cv_header_optarg,
249AC_EGREP_HEADER(optarg,unistd.h,tvf_cv_header_optarg="yes";
250,tvf_cv_header_optarg="no"))
251if test "$tvf_cv_header_optarg" = "yes"; then
252AC_DEFINE(OPTARG_DEFINED)
253fi
254fi
255])
256
257dnl see the sh comment
258AC_DEFUN([TVF_CREATE_DOT_DEPEND],[
259AC_MSG_RESULT(creating default depend file)
260# Create an empty .depend that is older than Makefile
261# if touch will take a time, set the time explicitly,
262# if not wait a bit so that the created Makefile is newer
263
264if test "$MKDEP" = "cp" ; then
265	cp default_depend .depend
266else
267	cat < /dev/null > .depend
268	touch -t 9912311000 .depend > /dev/null 2>&1 || sleep 1
269fi
270])
271
272dnl get the OS version.  Export it as OS_VERSION in an AC_SUBST
273AC_DEFUN([TVF_OS_VERSION],[
274AC_CACHE_CHECK(for OS version,tvf_cv_os_version,
275	tvf_cv_os_version=`uname -sr`
276)
277OS_VERSION=$tvf_cv_os_version
278AC_SUBST(OS_VERSION)
279AC_DEFINE_UNQUOTED(OS_VERSION,"$OS_VERSION")
280])
281
282dnl determine if $MAKE uses .ifdef or ifdef .  If one of these choices
283dnl works, the output variables MAKE_IFDEF, MAKE_IFNDEF, MAKE_ELSE,
284dnl and MAKE_ENDIF are set to appropriate values.  If the routine
285dnl can't tell which ones to use, the output variables are defined as
286dnl comment characters.  Can be  onverriden with --enable-make-ifdef=.
287
288AC_DEFUN([TVF_MAKE_IFDEF], [
289AC_ARG_ENABLE(make-ifdef,
290[  --enable-make-ifdef=X   set the string format used for ifdef in Makefiles
291                          either .ifdef, ifdef or no ],
292[
293AC_MSG_CHECKING([make ifdef directive])
294tvf_cv_make_ifdef=$enableval
295AC_MSG_RESULT($tvf_cv_make_ifdef (arg))
296],
297AC_CACHE_CHECK([make ifdef directive],tvf_cv_make_ifdef,
298	tvf_cv_make_ifdef="no"
299	tvf_here=`pwd`
300	cd /tmp
301	mkdir make.$$
302	cd make.$$
303	cat > Makefile <<END
304ifdef TEST
305test:
306	touch test
307endif
308END
309	if ${MAKE-make} TEST=y -f ./Makefile > /dev/null 2> /dev/null && \
310	   test -e ./test ; then
311		tvf_cv_make_ifdef="ifdef"
312	else
313		rm -f test
314		cat > Makefile <<END
315.ifdef TEST
316test:
317	touch test
318.endif
319
320END
321		if ${MAKE-make} TEST=y -f ./Makefile > /dev/null 2>/dev/null \
322		   && test -e ./test ; then
323			tvf_cv_make_ifdef=".ifdef"
324		fi
325	fi
326	cd /tmp
327	rm -rf make.$$
328	cd $tvf_here
329))
330	case $tvf_cv_make_ifdef in
331		no)
332			MAKE_IFDEF='# ifdef'
333			MAKE_IFNDEF='# ifndef'
334			MAKE_ELSE='# else'
335			MAKE_ENDIF='# endif'
336		;;
337		.ifdef)
338			MAKE_IFDEF='.ifdef'
339			MAKE_IFNDEF='.ifndef'
340			MAKE_ELSE='.else'
341			MAKE_ENDIF='.endif'
342		;;
343		ifdef)
344			MAKE_IFDEF='ifdef'
345			MAKE_IFNDEF='ifndef'
346			MAKE_ELSE='else'
347			MAKE_ENDIF='endif'
348		;;
349	esac
350	AC_SUBST(MAKE_IFDEF)
351	AC_SUBST(MAKE_IFNDEF)
352	AC_SUBST(MAKE_ELSE)
353	AC_SUBST(MAKE_ENDIF)
354])
355
356dnl determine if the -mdoc macros are available.  Export them into MAN_MACROS.
357AC_DEFUN([TVF_MAN_MACROS],[
358AC_CACHE_CHECK(for manpage macros, tvf_cv_man_macros,[
359if troff -mdoc < /dev/null > /dev/null 2> /dev/null; then
360	tvf_cv_man_macros=doc
361else
362	tvf_cv_man_macros=an
363fi
364])
365MAN_MACROS=$tvf_cv_man_macros
366AC_SUBST(MAN_MACROS)
367])
368
369dnl test a 3-place version number.  The first is the actual version,
370dnl the second the limit.  The third is the program name
371AC_DEFUN([TVF_CHECK_VERSION_THREE],[
372changequote(<<,>>)
373tvf_lim_major=`echo "$2" | sed 's/\..*//'`
374tvf_lim_minor=`echo "$2" | sed 's/[^\.]*\.\([^.]*\)\..*/\1/'`
375tvf_lim_three=`echo "$2" | sed 's/[^\.]*\.[^.]*\.\(.*\)/\1/'`
376tvf_act_major=`echo "$1" | sed 's/\..*//'`
377tvf_act_minor=`echo "$1" | sed 's/[^\.]*\.\([^.]*\)\..*/\1/'`
378tvf_act_three=`echo "$1" | sed 's/[^\.]*\.[^.]*\.\(.*\)/\1/'`
379changequote([,])
380define([tvf_ok],ifelse("$3","",[true],$3))
381define([tvf_bad],ifelse("$4","",[true],$4))
382if test $tvf_act_major -lt $tvf_lim_major ; then
383	tvf_bad
384else
385	if test $tvf_act_major -eq $tvf_lim_major && \
386	test $tvf_act_minor -lt $tvf_lim_minor ; then
387		tvf_bad
388	else
389		if test $tvf_act_major -eq $tvf_lim_major && \
390			test $tvf_act_minor -eq $tvf_lim_minor &&\
391			test $tvf_act_three -lt $tvf_lim_three; then
392			tvf_bad
393		else
394			tvf_ok
395		fi
396	fi
397fi
398undefine([tvf_ok])
399undefine([tvf_bad])
400])
401