Use Painless Security signing key
[freeradius.git] / acinclude.m4
1 dnl Checks to see if this is SUNPro we're building with
2 dnl Usage:
3 dnl AC_PROG_CC_SUNPRO
4 AC_DEFUN([AC_PROG_CC_SUNPRO],
5 [AC_CACHE_CHECK(whether we are using SUNPro C, ac_cv_prog_suncc,
6 [dnl The semicolon is to pacify NeXT's syntax-checking cpp.
7 cat > conftest.c <<EOF
8 #ifdef __SUNPRO_C
9   yes;
10 #endif
11 EOF
12 if AC_TRY_COMMAND(${CC-cc} -E conftest.c) | egrep yes >/dev/null 2>&1; then
13   ac_cv_prog_suncc=yes
14 else
15   ac_cv_prog_suncc=no
16 fi])])
17
18 dnl #
19 dnl # FR_CHECK_TYPE_INCLUDE([#includes ...], type, default-C-types)
20 dnl #
21 dnl # This function is like AC_CHECK_TYPE, but you can give this one
22 dnl # a list of include files to check.
23 dnl #
24 AC_DEFUN([FR_CHECK_TYPE_INCLUDE],
25 [
26   AC_CACHE_CHECK(for $2, ac_cv_type_$2,
27     [ ac_cv_type_$2=no
28       AC_TRY_COMPILE($1,
29         [$2 foo],
30         ac_cv_type_$2=yes,
31       )
32     ]
33   )
34
35   if test "$ac_cv_type_$2" != "yes"; then
36          AC_DEFINE($2, $3, $4)
37   fi
38 ])
39
40 dnl #
41 dnl #  Locate the directory in which a particular file is found.
42 dnl #
43 dnl #  Usage: FR_LOCATE_DIR(MYSQLLIB_DIR, libmysqlclient.a)
44 dnl #
45 dnl #    Defines the variable MYSQLLIB_DIR to be the directory(s) in
46 dnl #    which the file libmysqlclient.a is to be found.
47 dnl #
48 dnl #
49 AC_DEFUN([FR_LOCATE_DIR],
50 [
51 dnl # If we have the program 'locate', then the problem of finding a
52 dnl # particular file becomes MUCH easier.
53 dnl #
54
55 dnl #
56 dnl #  No 'locate' defined, do NOT do anything.
57 dnl #
58 if test "x$LOCATE" != "x"; then
59   dnl #
60   dnl #  Root through a series of directories, looking for the given file.
61   dnl #
62   DIRS=
63   file=$2
64
65   for x in `${LOCATE} $file 2>/dev/null`; do
66     dnl #
67     dnl #  When asked for 'foo', locate will also find 'foo_bar', which we
68     dnl #  don't want.  We want that EXACT filename.
69     dnl #
70     dnl #  We ALSO want to be able to look for files like 'mysql/mysql.h',
71     dnl #  and properly match them, too.  So we try to strip off the last
72     dnl #  part of the filename, using the name of the file we're looking
73     dnl #  for.  If we CANNOT strip it off, then the name will be unchanged.
74     dnl #
75     base=`echo $x | sed "s%/${file}%%"`
76     if test "x$x" = "x$base"; then
77       continue;
78     fi
79
80     dir=`${DIRNAME} $x 2>/dev/null`
81     dnl #
82     dnl #  Exclude a number of directories.
83     dnl #
84     exclude=`echo ${dir} | ${GREP} /home`
85     if test "x$exclude" != "x"; then
86       continue
87     fi
88
89     dnl #
90     dnl #  OK, we have an exact match.  Let's be sure that we only find ONE
91     dnl #  matching directory.
92     dnl #
93     already=`echo \$$1 ${DIRS} | ${GREP} ${dir}`
94     if test "x$already" = "x"; then
95       DIRS="$DIRS $dir"
96     fi
97   done
98 fi
99
100 dnl #
101 dnl #  And remember the directory in which we found the file.
102 dnl #
103 eval "$1=\"\$$1 $DIRS\""
104 ])
105
106
107 dnl #######################################################################
108 dnl #
109 dnl #  Look for a library in a number of places.
110 dnl #
111 dnl #  FR_SMART_CHECK_LIB(library, function)
112 dnl #
113 AC_DEFUN([FR_SMART_CHECK_LIB], [
114
115 sm_lib_safe=`echo "$1" | sed 'y%./+-%__p_%'`
116 sm_func_safe=`echo "$2" | sed 'y%./+-%__p_%'`
117
118 dnl #
119 dnl #  We pass all arguments for linker testing in CCPFLAGS as these
120 dnl #  will be passed to the compiler (then linker) first.
121 dnl #
122 dnl #  The linker will search through -L directories in the order they
123 dnl #  appear on the command line.  Unfortunately the same rules appear
124 dnl #  to apply to directories specified with --sysroot, so we must
125 dnl #  pass the user specified directory first.
126 dnl #
127 dnl #  Really we should be using LDFLAGS (-L<dir>) for this.
128 dnl #
129 old_LIBS="$LIBS"
130 old_CPPFLAGS="$CPPFLAGS"
131 smart_lib=
132 smart_ldflags=
133 smart_lib_dir=
134
135 dnl #
136 dnl #  Try first any user-specified directory, otherwise we may pick up
137 dnl #  the wrong version.
138 dnl #
139 if test "x$smart_try_dir" != "x"; then
140   for try in $smart_try_dir; do
141     AC_MSG_CHECKING([for $2 in -l$1 in $try])
142     LIBS="-l$1 $old_LIBS"
143     CPPFLAGS="-L$try -Wl,-rpath,$try $old_CPPFLAGS"
144     AC_TRY_LINK([extern char $2();],
145                 [$2()],
146                 [
147                  smart_lib="-l$1"
148                  smart_ldflags="-L$try -Wl,-rpath,$try"
149                  AC_MSG_RESULT(yes)
150                  break
151                 ],
152                 [AC_MSG_RESULT(no)])
153   done
154   LIBS="$old_LIBS"
155   CPPFLAGS="$old_CPPFLAGS"
156 fi
157
158 dnl #
159 dnl #  Try using the default library path
160 dnl #
161 if test "x$smart_lib" = "x"; then
162   AC_MSG_CHECKING([for $2 in -l$1])
163   LIBS="-l$1 $old_LIBS"
164   AC_TRY_LINK([extern char $2();],
165               [$2()],
166               [
167                 smart_lib="-l$1"
168                 AC_MSG_RESULT(yes)
169               ],
170               [AC_MSG_RESULT(no)])
171   LIBS="$old_LIBS"
172 fi
173
174 dnl #
175 dnl #  Try to guess possible locations.
176 dnl #
177 if test "x$smart_lib" = "x"; then
178   FR_LOCATE_DIR(smart_lib_dir,[lib$1${libltdl_cv_shlibext}])
179   FR_LOCATE_DIR(smart_lib_dir,[lib$1.a])
180
181   for try in $smart_lib_dir /usr/local/lib /opt/lib; do
182     AC_MSG_CHECKING([for $2 in -l$1 in $try])
183     LIBS="-l$1 $old_LIBS"
184     CPPFLAGS="-L$try -Wl,-rpath,$try $old_CPPFLAGS"
185     AC_TRY_LINK([extern char $2();],
186                 [$2()],
187                 [
188                   smart_lib="-l$1"
189                   smart_ldflags="-L$try -Wl,-rpath,$try"
190                   AC_MSG_RESULT(yes)
191                   break
192                 ],
193                 [AC_MSG_RESULT(no)])
194   done
195   LIBS="$old_LIBS"
196   CPPFLAGS="$old_CPPFLAGS"
197 fi
198
199 dnl #
200 dnl #  Found it, set the appropriate variable.
201 dnl #
202 if test "x$smart_lib" != "x"; then
203   eval "ac_cv_lib_${sm_lib_safe}_${sm_func_safe}=yes"
204   LIBS="$smart_ldflags $smart_lib $old_LIBS"
205   SMART_LIBS="$smart_ldflags $smart_lib $SMART_LIBS"
206 fi
207 ])
208
209 dnl #######################################################################
210 dnl #
211 dnl #  Look for a header file in a number of places.
212 dnl #
213 dnl #  FR_SMART_CHECK_INCLUDE(foo.h, [ #include <other.h> ])
214 dnl #
215 AC_DEFUN([FR_SMART_CHECK_INCLUDE], [
216
217 ac_safe=`echo "$1" | sed 'y%./+-%__pm%'`
218 old_CPPFLAGS="$CPPFLAGS"
219 smart_include=
220 dnl #  The default directories we search in (in addition to the compilers search path)
221 smart_include_dir="/usr/local/include /opt/include"
222
223 dnl #  Our local versions
224 _smart_try_dir=
225 _smart_include_dir=
226
227 dnl #  Add variants with the different prefixes and one with no prefix
228 for _prefix in $smart_prefix ""; do
229   for _dir in $smart_try_dir; do
230     _smart_try_dir="${_smart_try_dir} ${_dir}/${_prefix}"
231   done
232
233   for _dir in $smart_include_dir; do
234     _smart_include_dir="${_smart_include_dir} ${_dir}/${_prefix}"
235   done
236 done
237
238 dnl #
239 dnl #  Try any user-specified directory first otherwise we may pick up
240 dnl #  the wrong version.
241 dnl #
242 if test "x$_smart_try_dir" != "x"; then
243   for try in $_smart_try_dir; do
244     AC_MSG_CHECKING([for $1 in $try])
245     CPPFLAGS="-isystem $try $old_CPPFLAGS"
246     AC_TRY_COMPILE([$2
247                     #include <$1>],
248                    [int a = 1;],
249                    [
250                      smart_include="-isystem $try"
251                      AC_MSG_RESULT(yes)
252                      break
253                    ],
254                    [
255                      smart_include=
256                      AC_MSG_RESULT(no)
257                    ])
258   done
259   CPPFLAGS="$old_CPPFLAGS"
260 fi
261
262 dnl #
263 dnl #  Try using the default includes (with prefixes).
264 dnl #
265 if test "x$smart_include" = "x"; then
266   for _prefix in $smart_prefix; do
267     AC_MSG_CHECKING([for ${_prefix}/$1])
268
269     AC_TRY_COMPILE([$2
270                     #include <$1>],
271                    [int a = 1;],
272                    [
273                      smart_include="-isystem ${_prefix}/"
274                      AC_MSG_RESULT(yes)
275                      break
276                    ],
277                    [
278                      smart_include=
279                      AC_MSG_RESULT(no)
280                    ])
281   done
282 fi
283
284 dnl #
285 dnl #  Try using the default includes (without prefixes).
286 dnl #
287 if test "x$smart_include" = "x"; then
288     AC_MSG_CHECKING([for $1])
289
290     AC_TRY_COMPILE([$2
291                     #include <$1>],
292                    [int a = 1;],
293                    [
294                      smart_include=" "
295                      AC_MSG_RESULT(yes)
296                      break
297                    ],
298                    [
299                      smart_include=
300                      AC_MSG_RESULT(no)
301                    ])
302 fi
303
304 dnl #
305 dnl #  Try to guess possible locations.
306 dnl #
307 if test "x$smart_include" = "x"; then
308
309   for prefix in $smart_prefix; do
310     FR_LOCATE_DIR(_smart_include_dir,"${_prefix}/${1}")
311   done
312   FR_LOCATE_DIR(_smart_include_dir, $1)
313
314   for try in $_smart_include_dir; do
315     AC_MSG_CHECKING([for $1 in $try])
316     CPPFLAGS="-isystem $try $old_CPPFLAGS"
317     AC_TRY_COMPILE([$2
318                     #include <$1>],
319                    [int a = 1;],
320                    [
321                      smart_include="-isystem $try"
322                      AC_MSG_RESULT(yes)
323                      break
324                    ],
325                    [
326                      smart_include=
327                      AC_MSG_RESULT(no)
328                    ])
329   done
330   CPPFLAGS="$old_CPPFLAGS"
331 fi
332
333 dnl #
334 dnl #  Found it, set the appropriate variable.
335 dnl #
336 if test "x$smart_include" != "x"; then
337   eval "ac_cv_header_$ac_safe=yes"
338   CPPFLAGS="$smart_include $old_CPPFLAGS"
339   SMART_CPPFLAGS="$smart_include $SMART_CPPFLAGS"
340 fi
341
342 dnl #
343 dnl #  Consume prefix, it's not likely to be used
344 dnl #  between multiple calls.
345 dnl #
346 smart_prefix=
347 ])
348
349 dnl #######################################################################
350 dnl #
351 dnl #  Look for a header file in a number of places.
352 dnl #
353 dnl #  Usage:  FR_CHECK_STRUCT_HAS_MEMBER([#include <foo.h>], [struct foo], member)
354 dnl #  If the member is defined, then the variable
355 dnl #     ac_cv_type_struct_foo_has_member is set to 'yes'
356 dnl #
357 AC_DEFUN([FR_CHECK_STRUCT_HAS_MEMBER], [
358   AC_MSG_CHECKING([for $3 in $2])
359
360 dnl BASED on 'offsetof':
361 dnl #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
362 dnl
363
364   AC_TRY_COMPILE([
365 $1
366 #ifdef HAVE_STDDEF_H
367 #include <stddef.h>
368 #endif
369 #ifndef offsetof
370 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
371 #endif
372 ],
373                  [ int foo = offsetof($2, $3) ],
374                  has_element=" ",
375                  has_element=)
376
377   ac_safe_type=`echo "$2" | sed 'y% %_%'`
378   if test "x$has_element" != "x"; then
379     AC_MSG_RESULT(yes)
380     eval "ac_cv_type_${ac_safe_type}_has_$3=yes"
381   else
382     AC_MSG_RESULT(no)
383     eval "ac_cv_type_${ac_safe_type}_has_$3="
384  fi
385 ])
386
387 dnl Autoconf 2.61 breaks the support for chained configure scripts
388 dnl in combination with config.cache
389 m4_pushdef([AC_OUTPUT],
390 [
391   unset ac_cv_env_LIBS_set
392   unset ac_cv_env_LIBS_value
393   m4_popdef([AC_OUTPUT])
394   AC_OUTPUT([$1],[$2],[$3])
395 ])
396
397 dnl #
398 dnl #  Figure out which storage class specifier for Thread Local Storage is supported by the compiler
399 dnl #
400 AC_DEFUN([FR_TLS],
401 [
402 dnl #
403 dnl #  See if the compilation works with __thread, for thread-local storage
404 dnl #
405   AC_MSG_CHECKING(for __thread support in compiler)
406   AC_RUN_IFELSE(
407     [AC_LANG_SOURCE(
408       [[
409         static __thread int val;
410         int main(int argc, char **argv) {
411           val = 0;
412           return val;
413         }
414       ]])
415     ],[have_tls=yes],[have_tls=no],[have_tls=no])
416   AC_MSG_RESULT($have_tls)
417   if test "x$have_tls" = "xyes"; then
418     AC_DEFINE([TLS_STORAGE_CLASS],[__thread],[Define if the compiler supports a thread local storage class])
419   fi
420
421 dnl #
422 dnl #  __declspec(thread) does exactly the same thing as __thread, but is supported by MSVS
423 dnl #
424   if test "x$have_tls" = "xno"; then
425     AC_MSG_CHECKING(for __declspec(thread) support in compiler)
426     AC_RUN_IFELSE(
427       [AC_LANG_SOURCE(
428         [[
429           static _Thread_local int val;
430           int main(int argc, char **argv) {
431             val = 0;
432             return val;
433           }
434         ]])
435       ],[have_tls=yes],[have_tls=no],[have_tls=no])
436     AC_MSG_RESULT($have_tls)
437     if test "x$have_tls" = "xyes"; then
438       AC_DEFINE([TLS_STORAGE_CLASS],[__declspec(thread)],[Define if the compiler supports a thread local storage class])
439     fi
440   fi
441 dnl #
442 dnl #  _Thread_local does exactly the same thing as __thread, but it's standards compliant with C11.
443 dnl #  we, however, state we are only compliant with C99, so the compiler will probably emit warnings
444 dnl #  if we use it.  So save it as a last resort.
445 dnl #
446   if test "x$have_tls" = "xno"; then
447     AC_MSG_CHECKING(for _Thread_local support in compiler)
448     AC_RUN_IFELSE(
449       [AC_LANG_SOURCE(
450         [[
451           static _Thread_local int val;
452           int main(int argc, char **argv) {
453             val = 0;
454             return val;
455           }
456         ]])
457       ],[have_tls=yes],[have_tls=no],[have_tls=no])
458     AC_MSG_RESULT($have_tls)
459     if test "x$have_tls" = "xyes"; then
460       AC_DEFINE([TLS_STORAGE_CLASS],[_Thread_local],[Define if the compiler supports a thread local storage class])
461     fi
462   fi
463 ])
464
465 AC_DEFUN([VL_LIB_READLINE], [
466   AC_CACHE_CHECK([for a readline compatible library],
467                  vl_cv_lib_readline, [
468     ORIG_LIBS="$LIBS"
469     for readline_lib in readline edit editline; do
470       for termcap_lib in "" termcap curses ncurses; do
471         if test -z "$termcap_lib"; then
472           TRY_LIB="-l$readline_lib"
473         else
474           TRY_LIB="-l$readline_lib -l$termcap_lib"
475         fi
476         LIBS="$ORIG_LIBS $TRY_LIB"
477         AC_TRY_LINK_FUNC(readline, vl_cv_lib_readline="$TRY_LIB")
478         if test -n "$vl_cv_lib_readline"; then
479           break
480         fi
481       done
482       if test -n "$vl_cv_lib_readline"; then
483         break
484       fi
485     done
486     if test -z "$vl_cv_lib_readline"; then
487       vl_cv_lib_readline="no"
488       LIBS="$ORIG_LIBS"
489     fi
490   ])
491
492   if test "$vl_cv_lib_readline" != "no"; then
493     LIBREADLINE="$vl_cv_lib_readline"
494     AC_DEFINE(HAVE_LIBREADLINE, 1,
495               [Define if you have a readline compatible library])
496     AC_CHECK_HEADERS(readline.h readline/readline.h)
497     AC_CACHE_CHECK([whether readline supports history],
498                    [vl_cv_lib_readline_history], [
499       vl_cv_lib_readline_history="no"
500       AC_TRY_LINK_FUNC([add_history], [vl_cv_lib_readline_history="yes"])
501     ])
502     if test "$vl_cv_lib_readline_history" = "yes"; then
503       AC_DEFINE(HAVE_READLINE_HISTORY, 1,
504                 [Define if your readline library has \`add_history'])
505       AC_CHECK_HEADERS(history.h readline/history.h)
506     fi
507   fi
508   AC_SUBST(LIBREADLINE)
509 ])dnl
510
511 dnl #
512 dnl #  Check if we have the choose expr builtin
513 dnl #
514 AC_DEFUN([FR_HAVE_BUILTIN_CHOOSE_EXPR],
515 [
516   AC_CACHE_CHECK([for __builtin_choose_expr support in compiler], [ax_cv_cc_builtin_choose_expr],[
517     AC_RUN_IFELSE(
518       [
519         AC_LANG_SOURCE(
520         [
521           int main(int argc, char **argv) {
522             if ((argc < 0) || !argv) return 1; /* -Werror=unused-parameter */
523             return __builtin_choose_expr(0, 1, 0);
524           }
525         ])
526       ],
527       [ax_cv_cc_builtin_choose_expr=yes],
528       [ax_cv_cc_builtin_choose_expr=no]
529     )
530   ])
531   if test "x$ax_cv_cc_builtin_choose_expr" = "xyes"; then
532     AC_DEFINE([HAVE_BUILTIN_CHOOSE_EXPR],1,[Define if the compiler supports __builtin_choose_expr])
533   fi
534 ])
535
536 dnl #
537 dnl #  Check if we have the types compatible p builtin
538 dnl #
539 AC_DEFUN([FR_HAVE_BUILTIN_TYPES_COMPATIBLE_P],
540 [
541   AC_CACHE_CHECK([for __builtin_types_compatible_p support in compiler], [ax_cv_cc_builtin_types_compatible_p],[
542     AC_RUN_IFELSE(
543       [
544         AC_LANG_SOURCE(
545         [
546           int main(int argc, char **argv) {
547             if ((argc < 0) || !argv) return 1; /* -Werror=unused-parameter */
548             return !(__builtin_types_compatible_p(char *, char *));
549           }
550         ])
551       ],
552       [ax_cv_cc_builtin_types_compatible_p=yes],
553       [ax_cv_cc_builtin_types_compatible_p=no]
554     )
555   ])
556   if test "x$ax_cv_cc_builtin_types_compatible_p" = "xyes"; then
557     AC_DEFINE([HAVE_BUILTIN_TYPES_COMPATIBLE_P],1,[Define if the compiler supports __builtin_types_compatible_p])
558   fi
559 ])
560
561 dnl #
562 dnl #  Check if we have the bwsap64 builtin
563 dnl #
564 AC_DEFUN([FR_HAVE_BUILTIN_BSWAP64],
565 [
566   AC_CACHE_CHECK([for __builtin_bswap64 support in compiler], [ax_cv_cc_builtin_bswap64],[
567     AC_RUN_IFELSE(
568       [
569         AC_LANG_SOURCE([
570           int main(int argc, char **argv) {
571             if ((argc < 0) || !argv) return 1; /* -Werror=unused-parameter */
572             return (__builtin_bswap64(0));
573           }
574         ])
575       ],
576       [ax_cv_cc_builtin_bswap64=yes],
577       [ax_cv_cc_builtin_bswap64=no]
578     )
579   ])
580   if test "x$ax_cv_cc_builtin_bswap64" = "xyes"; then
581     AC_DEFINE([HAVE_BUILTIN_BSWAP_64],1,[Define if the compiler supports __builtin_bswap64])
582   fi
583 ])
584
585 dnl #
586 dnl #  Check if we have __attribute__((__bounded__)) (usually only OpenBSD with GCC)
587 dnl #
588 AC_DEFUN([FR_HAVE_BOUNDED_ATTRIBUTE],[
589   AC_CACHE_CHECK([for __attribute__((__bounded__)) support in compiler], [ax_cv_cc_bounded_attribute],[
590     CFLAGS_SAVED=$CFLAGS
591     CFLAGS="$CFLAGS -Werror"
592     AC_RUN_IFELSE(
593       [
594         AC_LANG_SOURCE([
595           void test(char *buff) __attribute__ ((__bounded__ (__string__, 1, 1)));
596           int main(int argc, char **argv) {
597             if ((argc < 0) || !argv) return 1; /* -Werror=unused-parameter */
598             return 0;
599           }
600         ])
601       ],
602       [ax_cv_cc_bounded_attribute=yes],
603       [ax_cv_cc_bounded_attribute=no]
604     )
605     CFLAGS="$CFLAGS_SAVED"
606   ])
607   if test "x$ax_cv_cc_bounded_attribute" = "xyes"; then
608     AC_DEFINE(HAVE_ATTRIBUTE_BOUNDED, 1, [Define if your compiler supports the __bounded__ attribute (usually OpenBSD gcc).])
609   fi
610 ])
611
612 AC_INCLUDE(aclocal.m4)