started updating for 1.2 release
[rrdtool.git] / acinclude.m4
1 dnl Helper Functions for the RRDtool configure.ac script
2 dnl 
3 dnl this file gets included into aclocal.m4 when runnning aclocal
4 dnl
5 dnl
6 dnl
7 dnl Check for the presence of a particular library and its header files
8 dnl if this check fails set the environment variable EX_CHECK_ALL_ERR to YES
9 dnl and prints out a helful message
10 dnl
11 dnl
12 dnl EX_CHECK_ALL(library, function, header, pkgconf name, tested-version, homepage, cppflags)
13 dnl              $1       $2        $3      $4            $5              $6        $7
14 dnl
15 dnl
16 AC_DEFUN([EX_CHECK_ALL],
17 [
18  AC_LANG_PUSH(C)
19  EX_CHECK_STATE=NO
20  ex_check_save_LIBS=${LIBS}
21  ex_check_save_CPPFLAGS=${CPPFLAGS}
22  ex_check_save_LDFLAGS=${LDPFLAGS}
23  if test "x$7" != "x"; then
24    CPPFLAGS="-I$7 $CPPFLAGS"
25  fi
26  dnl try compiling naked first
27  AC_CHECK_LIB($1,$2, [
28     AC_CHECK_HEADER($3,[LIBS="-l$1 ${LIBS}";EX_CHECK_STATE=YES],[])],[])
29  if test $EX_CHECK_STATE = NO; then
30     dnl now asking pkg-config for help
31     AC_CHECK_PROGS(PKGCONFIG,[pkg-config],no)
32     if test "$PKGCONFIG" != "no"; then
33           if $PKGCONFIG --exists $4; then
34              CPPFLAGS=${CPPFLAGS}" "`$PKGCONFIG --cflags $4`
35              LDFLAGS=${LDFLAGS}" "`$PKGCONFIG --libs-only-L $4`
36              LDFLAGS=${LDFLAGS}" "`$PKGCONFIG --libs-only-other $4`
37              LIBS=${LIBS}" "`$PKGCONFIG --libs-only-l $4`
38              dnl remove the cached value and test again
39              unset ac_cv_lib_$1_$2
40              AC_CHECK_LIB($1,$2,[ AC_CHECK_HEADER($3,[EX_CHECK_STATE=YES],[])],[])
41           else
42              AC_MSG_WARN([
43 ----------------------------------------------------------------------------
44 * I found a copy of pkgconfig, but there is no $4.pc file around.
45   You may want to set the PKG_CONFIG_PATH variable to point to its
46   location.
47 ----------------------------------------------------------------------------
48                         ])
49            fi
50      fi
51   fi  
52
53   if test ${EX_CHECK_STATE} = NO; then
54      AC_MSG_WARN([
55 ----------------------------------------------------------------------------
56 * I could not find a working copy of $4. Check config.log for hints on why
57   this is the case. Maybe you need to set LDFLAGS and CPPFLAGS appropriately
58   so that compiler and the linker can find lib$1 and its header files. If
59   you have not installed $4, you can get it either from its original home on
60
61      $6
62
63   You can find also find an archive copy on
64
65      http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/libs
66
67   The last tested version of $4 is $5.
68
69        LIBS=$LIBS
70    LDFLAGS=$LDFLAGS
71   CPPFLAGS=$CPPFLAGS
72
73 ----------------------------------------------------------------------------
74                 ])
75        EX_CHECK_ALL_ERR=YES
76        LIBS="${ex_check_save_LIBS}"
77        CPPFLAGS="${ex_check_save_CPPFLAGS}"
78        LDFLAGS="${ex_check_save_LDFLAGS}"
79     fi
80     AC_LANG_POP(C)
81 ]
82 )
83
84 dnl
85 dnl  Ptherad check from http://autoconf-archive.cryp.to/acx_pthread.m4
86 dnl
87 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
88 dnl
89 dnl This macro figures out how to build C programs using POSIX threads.
90 dnl It sets the PTHREAD_LIBS output variable to the threads library and
91 dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
92 dnl C compiler flags that are needed. (The user can also force certain
93 dnl compiler flags/libs to be tested by setting these environment
94 dnl variables.)
95 dnl
96 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
97 dnl multi-threaded programs (defaults to the value of CC otherwise).
98 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
99 dnl
100 dnl NOTE: You are assumed to not only compile your program with these
101 dnl flags, but also link it with them as well. e.g. you should link
102 dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
103 dnl $LIBS
104 dnl
105 dnl If you are only building threads programs, you may wish to use
106 dnl these variables in your default LIBS, CFLAGS, and CC:
107 dnl
108 dnl        LIBS="$PTHREAD_LIBS $LIBS"
109 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
110 dnl        CC="$PTHREAD_CC"
111 dnl
112 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
113 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
114 dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
115 dnl
116 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
117 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
118 dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
119 dnl default action will define HAVE_PTHREAD.
120 dnl
121 dnl Please let the authors know if this macro fails on any platform, or
122 dnl if you have any other suggestions or comments. This macro was based
123 dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
124 dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
125 dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
126 dnl We are also grateful for the helpful feedback of numerous users.
127 dnl
128 dnl @category InstalledPackages
129 dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
130 dnl @version 2005-01-14
131 dnl @license GPLWithACException
132
133 AC_DEFUN([ACX_PTHREAD], [
134 AC_REQUIRE([AC_CANONICAL_HOST])
135 AC_LANG_PUSH(C)
136 acx_pthread_ok=no
137
138 # We used to check for pthread.h first, but this fails if pthread.h
139 # requires special compiler flags (e.g. on True64 or Sequent).
140 # It gets checked for in the link test anyway.
141
142 # First of all, check if the user has set any of the PTHREAD_LIBS,
143 # etcetera environment variables, and if threads linking works using
144 # them:
145 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
146         save_CFLAGS="$CFLAGS"
147         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
148         save_LIBS="$LIBS"
149         LIBS="$PTHREAD_LIBS $LIBS"
150         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
151         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
152         AC_MSG_RESULT($acx_pthread_ok)
153         if test x"$acx_pthread_ok" = xno; then
154                 PTHREAD_LIBS=""
155                 PTHREAD_CFLAGS=""
156         fi
157         LIBS="$save_LIBS"
158         CFLAGS="$save_CFLAGS"
159 fi
160
161 # We must check for the threads library under a number of different
162 # names; the ordering is very important because some systems
163 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
164 # libraries is broken (non-POSIX).
165
166 # Create a list of thread flags to try.  Items starting with a "-" are
167 # C compiler flags, and other items are library names, except for "none"
168 # which indicates that we try without any flags at all, and "pthread-config"
169 # which is a program returning the flags for the Pth emulation library.
170
171 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
172
173 # The ordering *is* (sometimes) important.  Some notes on the
174 # individual items follow:
175
176 # pthreads: AIX (must check this before -lpthread)
177 # none: in case threads are in libc; should be tried before -Kthread and
178 #       other compiler flags to prevent continual compiler warnings
179 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
180 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
181 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
182 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
183 # -pthreads: Solaris/gcc
184 # -mthreads: Mingw32/gcc, Lynx/gcc
185 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
186 #      doesn't hurt to check since this sometimes defines pthreads too;
187 #      also defines -D_REENTRANT)
188 # pthread: Linux, etcetera
189 # --thread-safe: KAI C++
190 # pthread-config: use pthread-config program (for GNU Pth library)
191
192 case "${host_cpu}-${host_os}" in
193         *solaris*)
194
195         # On Solaris (at least, for some versions), libc contains stubbed
196         # (non-functional) versions of the pthreads routines, so link-based
197         # tests will erroneously succeed.  (We need to link with -pthread or
198         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
199         # a function called by this macro, so we could check for that, but
200         # who knows whether they'll stub that too in a future libc.)  So,
201         # we'll just look for -pthreads and -lpthread first:
202
203         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
204         ;;
205 esac
206
207 if test x"$acx_pthread_ok" = xno; then
208 for flag in $acx_pthread_flags; do
209
210         case $flag in
211                 none)
212                 AC_MSG_CHECKING([whether pthreads work without any flags])
213                 ;;
214
215                 -*)
216                 AC_MSG_CHECKING([whether pthreads work with $flag])
217                 PTHREAD_CFLAGS="$flag"
218                 ;;
219
220                 pthread-config)
221                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
222                 if test x"$acx_pthread_config" = xno; then continue; fi
223                 PTHREAD_CFLAGS="`pthread-config --cflags`"
224                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
225                 ;;
226
227                 *)
228                 AC_MSG_CHECKING([for the pthreads library -l$flag])
229                 PTHREAD_LIBS="-l$flag"
230                 ;;
231         esac
232
233         save_LIBS="$LIBS"
234         save_CFLAGS="$CFLAGS"
235         LIBS="$PTHREAD_LIBS $LIBS"
236         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
237
238         # Check for various functions.  We must include pthread.h,
239         # since some functions may be macros.  (On the Sequent, we
240         # need a special flag -Kthread to make this header compile.)
241         # We check for pthread_join because it is in -lpthread on IRIX
242         # while pthread_create is in libc.  We check for pthread_attr_init
243         # due to DEC craziness with -lpthreads.  We check for
244         # pthread_cleanup_push because it is one of the few pthread
245         # functions on Solaris that doesn't have a non-functional libc stub.
246         # We try pthread_create on general principles.
247         AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]], [[pthread_t th; pthread_join(th, 0);
248                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
249                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ]])],[acx_pthread_ok=yes],[])
250
251         LIBS="$save_LIBS"
252         CFLAGS="$save_CFLAGS"
253
254         AC_MSG_RESULT($acx_pthread_ok)
255         if test "x$acx_pthread_ok" = xyes; then
256                 break;
257         fi
258
259         PTHREAD_LIBS=""
260         PTHREAD_CFLAGS=""
261 done
262 fi
263
264 # Various other checks:
265 if test "x$acx_pthread_ok" = xyes; then
266         save_LIBS="$LIBS"
267         LIBS="$PTHREAD_LIBS $LIBS"
268         save_CFLAGS="$CFLAGS"
269         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
270
271         # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
272         AC_MSG_CHECKING([for joinable pthread attribute])
273         attr_name=unknown
274         for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
275             AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]], [[int attr=$attr;]])],[attr_name=$attr; break],[])
276         done
277         AC_MSG_RESULT($attr_name)
278         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
279             AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
280                                [Define to necessary symbol if this constant
281                                 uses a non-standard name on your system.])
282         fi
283
284         AC_MSG_CHECKING([if more special flags are required for pthreads])
285         x_rflag=no
286         case "${host_cpu}-${host_os}" in
287             *-aix* | *-freebsd* | *-darwin*) x_rflag="-D_THREAD_SAFE";;
288             *solaris* | *-osf* | *-hpux*) x_rflag="-D_REENTRANT";;
289         esac
290         AC_MSG_RESULT(${x_rflag})
291         if test "x$x_rflag" != xno; then
292             PTHREAD_CFLAGS="$x_rflag $PTHREAD_CFLAGS"
293         fi
294
295         LIBS="$save_LIBS"
296         CFLAGS="$save_CFLAGS"
297
298         # More AIX lossage: must compile with cc_r
299         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
300 else
301         PTHREAD_CC="$CC"
302 fi
303
304 AC_SUBST(PTHREAD_LIBS)
305 AC_SUBST(PTHREAD_CFLAGS)
306 AC_SUBST(PTHREAD_CC)
307
308 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
309 if test x"$acx_pthread_ok" = xyes; then
310         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
311         :
312 else
313         acx_pthread_ok=no
314         $2
315 fi
316 AC_LANG_POP(C)
317 ])dnl ACX_PTHREAD
318
319
320 dnl
321 dnl determine how to get IEEE math working
322 dnl AC_IEEE(MESSAGE, set rd_cv_ieee_[var] variable, INCLUDES,
323 dnl   FUNCTION-BODY, [ACTION-IF-FOUND [,ACTION-IF-NOT-FOUND]])
324 dnl
325
326 dnl substitute them in all the files listed in AC_OUTPUT
327 AC_SUBST(PERLFLAGS)
328
329 AC_DEFUN([AC_IEEE], [
330 AC_MSG_CHECKING([if IEEE math works $1])
331 AC_CACHE_VAL([rd_cv_ieee_$2],
332 [AC_RUN_IFELSE([AC_LANG_SOURCE([[$3
333
334 #if HAVE_MATH_H
335 #  include <math.h>
336 #endif
337
338 #if HAVE_FLOAT_H
339 #  include <float.h>  
340 #endif
341
342 #if HAVE_IEEEFP_H
343 #  include <ieeefp.h>
344 #endif
345
346 #if HAVE_FP_CLASS_H
347 #  include <fp_class.h>
348 #endif
349
350 /* Solaris */
351 #if (! defined(HAVE_ISINF) && defined(HAVE_FPCLASS))
352 #  define HAVE_ISINF 1
353 #  define isinf(a) (fpclass(a) == FP_NINF || fpclass(a) == FP_PINF)
354 #endif
355
356 /* Digital UNIX */
357 #if (! defined(HAVE_ISINF) && defined(HAVE_FP_CLASS) && defined(HAVE_FP_CLASS_H))
358 #  define HAVE_ISINF 1
359 #  define isinf(a) (fp_class(a) == FP_NEG_INF || fp_class(a) == FP_POS_INF)
360 #endif 
361
362 /* AIX */
363 #if (! defined(HAVE_ISINF) && defined(HAVE_CLASS))
364 #  define HAVE_ISINF 1
365 #  define isinf(a) (class(a) == FP_MINUS_INF || class(a) == FP_PLUS_INF)
366 #endif
367
368 #if (! defined(HAVE_ISINF) && defined(HAVE_FPCLASSIFY) && defined(FP_PLUS_INF) && defined(FP_MINUS_INF))
369 #  define HAVE_ISINF 1
370 #  define isinf(a) (fpclassify(a) == FP_MINUS_INF || fpclassify(a) == FP_PLUS_INF)
371 #endif
372
373 #if (! defined(HAVE_ISINF) && defined(HAVE_FPCLASSIFY) && defined(FP_INFINITE))
374 #  define HAVE_ISINF 1
375 #  define isinf(a) (fpclassify(a) == FP_INFINITE)
376 #endif
377
378 #include <stdio.h>
379 int main(void){
380     double nan,inf,c,zero;
381     $4;
382     /* some math to see if we get a floating point exception */
383     zero=sin(0.0); /* don't let the compiler optimize us away */
384     nan=0.0/zero; /* especially here */
385     inf=1.0/zero; /* and here. I want to know if it can do the magic */
386                   /* at run time without sig fpe */
387     c = inf + nan;
388     c = inf / nan;
389     if (! isnan(nan)) {printf ("not isnan(NaN) ... "); return 1;}
390     if (nan == nan) {printf ("nan == nan ... "); return 1;}
391     if (! isinf(inf)) {printf ("not isinf(oo) ... "); return 1;}
392     if (! isinf(-inf)) {printf ("not isinf(-oo) ... "); return 1;}
393     if (! inf > 0) {printf ("not inf > 0 ... "); return 1;}
394     if (! -inf < 0) {printf ("not -inf < 0 ... "); return 1;}
395     return 0;
396  }]])],[rd_cv_ieee_$2=yes],[rd_cv_ieee_$2=no],[:])])
397 dnl these we run regardles is cached or not
398 if test x${rd_cv_ieee_$2} = "xyes"; then
399  AC_MSG_RESULT(yes)
400  $5
401 else
402  AC_MSG_RESULT(no)
403  $6
404 fi
405
406 ])
407
408 AC_DEFUN([AC_FULL_IEEE],[
409 AC_LANG_PUSH(C)
410 _cflags=${CFLAGS}
411 AC_IEEE([out of the box], works, , , ,
412   [CFLAGS="$_cflags -ieee"
413   AC_IEEE([with the -ieee switch], switch, , , ,
414     [CFLAGS="$_cflags -qfloat=nofold"
415     AC_IEEE([with the -qfloat=nofold switch], nofold, , , ,
416       [CFLAGS="$_cflags -w -qflttrap=enable:zerodivide"
417       AC_IEEE([with the -w -qflttrap=enable:zerodivide], flttrap, , , ,
418        [CFLAGS="$_cflags -mieee"
419        AC_IEEE([with the -mieee switch], mswitch, , , ,
420          [CFLAGS="$_cflags -q float=rndsngl"
421          AC_IEEE([with the -q float=rndsngl switch], qswitch, , , ,
422            [CFLAGS="$_cflags -OPT:IEEE_NaN_inf=ON"
423            AC_IEEE([with the -OPT:IEEE_NaN_inf=ON switch], ieeenaninfswitch, , , ,
424              [CFLAGS="$_cflags -OPT:IEEE_comparisons=ON"
425              AC_IEEE([with the -OPT:IEEE_comparisons=ON switch], ieeecmpswitch, , , ,
426                [CFLAGS=$_cflags
427                AC_IEEE([with fpsetmask(0)], mask,
428                  [#include <floatingpoint.h>], [fpsetmask(0)],
429                  [AC_DEFINE(MUST_DISABLE_FPMASK)
430                  PERLFLAGS="CCFLAGS=-DMUST_DISABLE_FPMASK"],
431                  [AC_IEEE([with signal(SIGFPE,SIG_IGN)], sigfpe,
432                    [#include <signal.h>], [signal(SIGFPE,SIG_IGN)],
433                    [AC_DEFINE(MUST_DISABLE_SIGFPE)
434                    PERLFLAGS="CCFLAGS=-DMUST_DISABLE_SIGFPE"],          
435                    AC_MSG_ERROR([
436 Your Compiler does not do propper IEEE math ... Please find out how to
437 make IEEE math work with your compiler and let me know (oetiker@ee.ethz.ch).
438 Check config.log to see what went wrong ...
439 ]))])])])])])])])])])
440
441 AC_LANG_POP(C)
442
443 ])