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