New build path variable
[freeradius.git] / acinclude.m4
1 dnl See whether we need a declaration for a function.
2 dnl RADIUSD_NEED_DECLARATION(FUNCTION [, EXTRA-HEADER-FILES])
3 AC_DEFUN([RADIUSD_NEED_DECLARATION],
4 [AC_MSG_CHECKING([whether $1 must be declared])
5 AC_CACHE_VAL(radius_cv_decl_needed_$1,
6 [AC_TRY_COMPILE([
7 #include <stdio.h>
8 #include <string.h>
9 #ifdef HAVE_UNISTD_H
10 #include <unistd.h>
11 #endif
12 #ifdef HAVE_CRYPT_H
13 #include <crypt.h>
14 #endif
15 #ifdef HAVE_ERRNO_H
16 #include <errno.h>
17 #endif
18 #ifdef HAVE_RESOURCE_H
19 #include <resource.h>
20 #endif
21 #ifdef HAVE_GETOPT_H
22 #include <getopt.h>
23 #endif
24 #ifdef HAVE_MALLOC_H
25 #include <malloc.h>
26 #endif
27 #ifdef HAVE_UTMP_H
28 #include <utmp.h>
29 #endif
30 #ifdef HAVE_UTMPX_H
31 #include <utmpx.h>
32 #endif
33 #ifdef HAVE_SYS_SELECT_H
34 #include <sys/select.h>
35 #endif
36 #ifdef HAVE_DLFCN_H
37 #include <dlfcn.h>
38 #endif
39 #ifdef HAVE_REGEX_H
40 #include <regex.h>
41 #endif
42 #ifdef HAVE_SYSLOG_H
43 #include <syslog.h>
44 #endif
45 #ifdef HAVE_INTTYPES_H
46 #include <inttypes.h>
47 #endif
48 #ifdef HAVE_STDINT_H
49 #include <stdint.h>
50 #endif
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
53 #endif
54 $2],
55 [char *(*pfn) = (char *(*)) $1],
56 eval "radius_cv_decl_needed_$1=no", eval "radius_cv_decl_needed_$1=yes")])
57 if eval "test \"`echo '$radius_cv_decl_needed_'$1`\" = yes"; then
58   AC_MSG_RESULT(yes)
59   radius_tr_decl=NEED_DECLARATION_`echo $1 | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
60   AC_DEFINE_UNQUOTED($radius_tr_decl)
61 else
62   AC_MSG_RESULT(no)
63 fi
64 ])dnl
65
66 dnl Check multiple functions to see whether each needs a declaration.
67 dnl RADIUSD_NEED_DECLARATIONS(FUNCTION... [, EXTRA-HEADER-FILES])
68 AC_DEFUN([RADIUSD_NEED_DECLARATIONS],
69 [for ac_func in $1
70 do
71 RADIUSD_NEED_DECLARATION($ac_func, $2)
72 done
73 ])
74
75 dnl Checks to see if this is SUNPro we're building with
76 dnl Usage:
77 dnl AC_PROG_CC_SUNPRO
78 AC_DEFUN([AC_PROG_CC_SUNPRO],
79 [AC_CACHE_CHECK(whether we are using SUNPro C, ac_cv_prog_suncc,
80 [dnl The semicolon is to pacify NeXT's syntax-checking cpp.
81 cat > conftest.c <<EOF
82 #ifdef __SUNPRO_C
83   yes;
84 #endif
85 EOF
86 if AC_TRY_COMMAND(${CC-cc} -E conftest.c) | egrep yes >/dev/null 2>&1; then
87   ac_cv_prog_suncc=yes
88 else
89   ac_cv_prog_suncc=no
90 fi])])
91
92 dnl #
93 dnl # FR_CHECK_TYPE_INCLUDE([#includes ...], type, default-C-types)
94 dnl #
95 dnl # This function is like AC_CHECK_TYPE, but you can give this one
96 dnl # a list of include files to check.
97 dnl #
98 AC_DEFUN([FR_CHECK_TYPE_INCLUDE],
99 [
100   AC_CACHE_CHECK(for $2, ac_cv_type_$2,
101     [ ac_cv_type_$2=no
102       AC_TRY_COMPILE($1,
103         [$2 foo],
104         ac_cv_type_$2=yes,
105       )
106     ]
107   )
108
109   if test "$ac_cv_type_$2" != "yes"; then
110          AC_DEFINE($2, $3, $4)
111   fi
112 ])
113
114 dnl #
115 dnl #  Locate the directory in which a particular file is found.
116 dnl #
117 dnl #  Usage: FR_LOCATE_DIR(MYSQLLIB_DIR, libmysqlclient.a)
118 dnl #
119 dnl #    Defines the variable MYSQLLIB_DIR to be the directory(s) in
120 dnl #    which the file libmysqlclient.a is to be found.
121 dnl #
122 dnl #
123 AC_DEFUN([FR_LOCATE_DIR],
124 [
125 dnl # If we have the program 'locate', then the problem of finding a
126 dnl # particular file becomes MUCH easier.
127 dnl #
128
129 dnl #
130 dnl #  No 'locate' defined, do NOT do anything.
131 dnl #
132 if test "x$LOCATE" != "x"; then
133   dnl #
134   dnl #  Root through a series of directories, looking for the given file.
135   dnl #
136   DIRS=
137   file=$2
138
139   for x in `${LOCATE} $file 2>/dev/null`; do
140     dnl #
141     dnl #  When asked for 'foo', locate will also find 'foo_bar', which we
142     dnl #  don't want.  We want that EXACT filename.
143     dnl #
144     dnl #  We ALSO want to be able to look for files like 'mysql/mysql.h',
145     dnl #  and properly match them, too.  So we try to strip off the last
146     dnl #  part of the filename, using the name of the file we're looking
147     dnl #  for.  If we CANNOT strip it off, then the name will be unchanged.
148     dnl #
149     base=`echo $x | sed "s%/${file}%%"`
150     if test "x$x" = "x$base"; then
151       continue;
152     fi
153
154     dir=`${DIRNAME} $x 2>/dev/null`
155     dnl #
156     dnl #  Exclude a number of directories.
157     dnl #
158     exclude=`echo ${dir} | ${GREP} /home`
159     if test "x$exclude" != "x"; then
160       continue
161     fi
162
163     dnl #
164     dnl #  OK, we have an exact match.  Let's be sure that we only find ONE
165     dnl #  matching directory.
166     dnl #
167     already=`echo \$$1 ${DIRS} | ${GREP} ${dir}`
168     if test "x$already" = "x"; then
169       DIRS="$DIRS $dir"
170     fi
171   done
172 fi
173
174 dnl #
175 dnl #  And remember the directory in which we found the file.
176 dnl #
177 eval "$1=\"\$$1 $DIRS\""
178 ])
179
180
181 dnl #######################################################################
182 dnl #
183 dnl #  Look for a library in a number of places.
184 dnl #
185 dnl #  FR_SMART_CHECK_LIB(library, function)
186 dnl #
187 AC_DEFUN([FR_SMART_CHECK_LIB], [
188
189 sm_lib_safe=`echo "$1" | sed 'y%./+-%__p_%'`
190 sm_func_safe=`echo "$2" | sed 'y%./+-%__p_%'`
191 AC_MSG_CHECKING([for $2 in -l$1])
192
193 old_LIBS="$LIBS"
194 smart_lib=
195 smart_lib_dir=
196
197 dnl #
198 dnl #  Try first any user-specified directory, otherwise we may pick up
199 dnl #  the wrong version.
200 dnl #
201 if test "x$smart_try_dir" != "x"; then
202   for try in $smart_try_dir; do
203     LIBS="-L$try -l$1 $old_LIBS"
204     AC_TRY_LINK([extern char $2();],
205                 [ $2()],
206                 smart_lib="-L$try -l$1")
207     if test "x$smart_lib" != "x"; then
208       break;
209     fi
210   done
211   LIBS="$old_LIBS"
212 fi
213
214 dnl #
215 dnl #  Try using the default library path
216 dnl #
217 if test "x$smart_lib" = "x"; then
218   LIBS="-l$1 $old_LIBS"
219   AC_TRY_LINK([extern char $2();],
220               [ $2()],
221               smart_lib="-l$1")
222   LIBS="$old_LIBS"
223 fi
224
225 dnl #
226 dnl #  Try to guess possible locations.
227 dnl #
228 if test "x$smart_lib" = "x"; then
229   FR_LOCATE_DIR(smart_lib_dir,[lib$1${libltdl_cv_shlibext}])
230   FR_LOCATE_DIR(smart_lib_dir,[lib$1.a])
231
232   for try in $smart_lib_dir /usr/local/lib /opt/lib; do
233     LIBS="-L$try -l$1 $old_LIBS"
234     AC_TRY_LINK([extern char $2();],
235                 [ $2()],
236                 smart_lib="-L$try -l$1")
237     if test "x$smart_lib" != "x"; then
238       break;
239     fi
240   done
241   LIBS="$old_LIBS"
242 fi
243
244 dnl #
245 dnl #  Found it, set the appropriate variable.
246 dnl #
247 if test "x$smart_lib" != "x"; then
248   AC_MSG_RESULT(yes)
249   eval "ac_cv_lib_${sm_lib_safe}_${sm_func_safe}=yes"
250   LIBS="$smart_lib $old_LIBS"
251   SMART_LIBS="$smart_lib $SMART_LIBS"
252 else
253   AC_MSG_RESULT(no)
254 fi
255 ])
256
257 dnl #######################################################################
258 dnl #
259 dnl #  Look for a header file in a number of places.
260 dnl #
261 dnl #  FR_SMART_CHECK_INCLUDE(foo.h, [ #include <other.h> ])
262 dnl #
263 AC_DEFUN([FR_SMART_CHECK_INCLUDE], [
264
265 ac_safe=`echo "$1" | sed 'y%./+-%__pm%'`
266 AC_MSG_CHECKING([for $1])
267
268 old_CFLAGS="$CFLAGS"
269 smart_include=
270 smart_include_dir=
271
272 dnl #
273 dnl #  Try first any user-specified directory, otherwise we may pick up
274 dnl #  the wrong version.
275 dnl #
276 if test "x$smart_try_dir" != "x"; then
277   for try in $smart_try_dir; do
278     CFLAGS="$old_CFLAGS -I$try"
279     AC_TRY_COMPILE([$2
280                     #include <$1>],
281                    [ int a = 1;],
282                    smart_include="-I$try",
283                    smart_include=)
284     if test "x$smart_include" != "x"; then
285       break;
286     fi
287   done
288   CFLAGS="$old_CFLAGS"
289 fi
290
291 dnl #
292 dnl #  Try using the default includes.
293 dnl #
294 if test "x$smart_include" = "x"; then
295   AC_TRY_COMPILE([$2
296                   #include <$1>],
297                  [ int a = 1;],
298                  smart_include=" ",
299                  smart_include=)
300 fi
301
302 dnl #
303 dnl #  Try to guess possible locations.
304 dnl #
305 if test "x$smart_include" = "x"; then
306   FR_LOCATE_DIR(smart_include_dir,$1)
307
308   for try in $smart_include_dir /usr/local/include /opt/include; do
309     CFLAGS="$old_CFLAGS -I$try"
310     AC_TRY_COMPILE([$2
311                     #include <$1>],
312                    [ int a = 1;],
313                    smart_include="-I$try",
314                    smart_include=)
315     if test "x$smart_include" != "x"; then
316       break;
317     fi
318   done
319   CFLAGS="$old_CFLAGS"
320 fi
321
322 dnl #
323 dnl #  Found it, set the appropriate variable.
324 dnl #
325 if test "x$smart_include" != "x"; then
326   AC_MSG_RESULT(yes)
327   eval "ac_cv_header_$ac_safe=yes"
328   CFLAGS="$old_CFLAGS $smart_include"
329   SMART_CFLAGS="$SMART_CFLAGS $smart_include"
330 else
331   AC_MSG_RESULT(no)
332 fi
333 ])
334
335 dnl #######################################################################
336 dnl #
337 dnl #  Look for a header file in a number of places.
338 dnl #
339 dnl #  Usage:  FR_CHECK_STRUCT_HAS_MEMBER([#include <foo.h>], [struct foo], member)
340 dnl #  If the member is defined, then the variable
341 dnl #     ac_cv_type_struct_foo_has_member is set to 'yes'
342 dnl #
343 AC_DEFUN([FR_CHECK_STRUCT_HAS_MEMBER], [
344   AC_MSG_CHECKING([for $3 in $2])
345
346 dnl BASED on 'offsetof':
347 dnl #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
348 dnl
349
350   AC_TRY_COMPILE([
351 $1
352 #ifndef offsetof
353 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
354 #endif
355 ],
356                  [ int foo = offsetof($2, $3) ],
357                  has_element=" ",
358                  has_element=)
359
360   ac_safe_type=`echo "$2" | sed 'y% %_%'`
361   if test "x$has_element" != "x"; then
362     AC_MSG_RESULT(yes)
363     eval "ac_cv_type_${ac_safe_type}_has_$3=yes"
364   else
365     AC_MSG_RESULT(no) 
366     eval "ac_cv_type_${ac_safe_type}_has_$3="
367  fi
368 ])
369
370 dnl Autoconf 2.61 breaks the support for chained configure scripts
371 dnl in combination with config.cache
372 m4_pushdef([AC_OUTPUT],
373 [
374   unset ac_cv_env_LIBS_set
375   unset ac_cv_env_LIBS_value
376   m4_popdef([AC_OUTPUT])
377   AC_OUTPUT([$1],[$2],[$3])
378 ])
379
380
381 #  See if the compilation works with __thread, for thread-local storage
382 #
383 AC_DEFUN([FR_TLS],
384 [
385     AC_MSG_CHECKING(for TLS)
386     AC_RUN_IFELSE([AC_LANG_SOURCE([[ static __thread int val; int main(int argc, char *argv[]) { return val = argc; } ]])],[have_tls=yes],[have_tls=no],[have_tls=no ])
387     AC_MSG_RESULT($have_tls)
388     if test "$have_tls" = "yes"; then
389         AC_DEFINE([HAVE_THREAD_TLS],[1],[Define if the compiler supports __thread])
390     fi
391 ])
392
393
394 AC_DEFUN([AC_LIB_READLINE], [
395   AC_CACHE_CHECK([for a readline compatible library],
396                  ac_cv_lib_readline, [
397     ORIG_LIBS=$LIBS
398     for readline_lib in readline edit editline; do
399       for termcap_lib in "" termcap curses ncurses; do
400         if test -z "$termcap_lib"; then
401           TRY_LIB="-l$readline_lib"
402         else
403           TRY_LIB="-l$readline_lib -l$termcap_lib"
404         fi
405         LIBS="$ORIG_LIBS $TRY_LIB"
406         AC_TRY_LINK_FUNC(readline, ac_cv_lib_readline="$TRY_LIB")
407         if test -n "$ac_cv_lib_readline"; then
408           LIBREADLINE="$TRY_LIB"
409           AC_SUBST(LIBREADLINE)
410           break
411         fi
412       done
413       if test -n "$ac_cv_lib_readline"; then
414         break
415       fi
416     done
417     if test -z "$ac_cv_lib_readline"; then
418       ac_cv_lib_readline="no"
419     fi
420     LIBS=$ORIG_LIBS
421   ])
422
423   if test "$ac_cv_lib_readline" != "no"; then
424     AC_DEFINE(HAVE_LIBREADLINE, 1,
425               [Define if you have a readline compatible library])
426     AC_CHECK_HEADERS(readline.h readline/readline.h)
427     AC_CACHE_CHECK([whether readline supports history],
428                    ac_cv_lib_readline_history, [
429       ac_cv_lib_readline_history="no"
430       AC_TRY_LINK_FUNC(add_history, ac_cv_lib_readline_history="yes")
431     ])
432     if test "$ac_cv_lib_readline_history" = "yes"; then
433       AC_DEFINE(HAVE_READLINE_HISTORY, 1,
434                 [Define if your readline library has \`add_history'])
435       AC_CHECK_HEADERS(history.h readline/history.h)
436     fi
437   fi
438 ])
439
440 AC_INCLUDE(aclocal.m4)