Big autotool update:
[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  EX_CHECK_STATE=NO
19  dnl try compiling naked first
20  AC_CHECK_LIB($1,$2, [
21     AC_CHECK_HEADER($3,[LIBS="-l$1 ${LIBS}";EX_CHECK_STATE=YES],[
22        dnl now asking pkg-config for help
23        AC_PATH_PROG(PKGCONFIG,pkg-config,no)
24        if test "$PKGCONFIG" != "no"; then
25           if $PKGCONFIG --exists $4; then
26              CPPFLAGS=${CPPFLAGS}" "`$PKGCONFIG --cflags $4`
27              LDFLAGS=${LDFLAGS}" "`$PKGCONFIG --libs-only-l $4`
28              LIBS=${LIBS}" " `$PKGCONFIG --libs-only-other $4`
29              AC_CHECK_LIB($1,$2,[ AC_CHECK_HEADER($3,[LIBS="-l$1 ${LIBS};EX_CHECK_STATE=YES"],[])],[])
30           else
31              AC_MSG_WARN([
32 ----------------------------------------------------------------------------
33 * I found a copy of pkgconfig, but there is no $4.pc file around.
34   You may want to set the PKG_CONFIG_PATH variable to point to its
35   location.
36                         ])
37            fi
38        fi
39      ])],[])
40
41
42   if test ${EX_CHECK_STATE} = NO; then
43      AC_MSG_WARN([
44 ----------------------------------------------------------------------------
45 * I could not find a working copy of $4. Check config.log for hints on why
46   this is the case. Maybe you need to set LDFLAGS appropriately so that the
47   linker can find lib$1. If you have not installed $4, you can get it
48   either from its original home on
49
50      $6
51
52   You can find also find an archive copy on
53
54      http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/libs
55
56   The last tested version of $4 is $5.
57 ----------------------------------------------------------------------------
58                 ])
59        EX_CHECK_ALL_ERR=YES
60     fi
61 ]
62 )
63 dnl
64 dnl  Ptherad check from http://autoconf-archive.cryp.to/acx_pthread.m4
65 dnl
66 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
67 dnl
68 dnl This macro figures out how to build C programs using POSIX threads.
69 dnl It sets the PTHREAD_LIBS output variable to the threads library and
70 dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
71 dnl C compiler flags that are needed. (The user can also force certain
72 dnl compiler flags/libs to be tested by setting these environment
73 dnl variables.)
74 dnl
75 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
76 dnl multi-threaded programs (defaults to the value of CC otherwise).
77 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
78 dnl
79 dnl NOTE: You are assumed to not only compile your program with these
80 dnl flags, but also link it with them as well. e.g. you should link
81 dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
82 dnl $LIBS
83 dnl
84 dnl If you are only building threads programs, you may wish to use
85 dnl these variables in your default LIBS, CFLAGS, and CC:
86 dnl
87 dnl        LIBS="$PTHREAD_LIBS $LIBS"
88 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
89 dnl        CC="$PTHREAD_CC"
90 dnl
91 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
92 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
93 dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
94 dnl
95 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
96 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
97 dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
98 dnl default action will define HAVE_PTHREAD.
99 dnl
100 dnl Please let the authors know if this macro fails on any platform, or
101 dnl if you have any other suggestions or comments. This macro was based
102 dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
103 dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
104 dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
105 dnl We are also grateful for the helpful feedback of numerous users.
106 dnl
107 dnl @category InstalledPackages
108 dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
109 dnl @version 2005-01-14
110 dnl @license GPLWithACException
111
112 AC_DEFUN([ACX_PTHREAD], [
113 AC_REQUIRE([AC_CANONICAL_HOST])
114 AC_LANG_SAVE
115 AC_LANG_C
116 acx_pthread_ok=no
117
118 # We used to check for pthread.h first, but this fails if pthread.h
119 # requires special compiler flags (e.g. on True64 or Sequent).
120 # It gets checked for in the link test anyway.
121
122 # First of all, check if the user has set any of the PTHREAD_LIBS,
123 # etcetera environment variables, and if threads linking works using
124 # them:
125 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
126         save_CFLAGS="$CFLAGS"
127         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
128         save_LIBS="$LIBS"
129         LIBS="$PTHREAD_LIBS $LIBS"
130         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
131         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
132         AC_MSG_RESULT($acx_pthread_ok)
133         if test x"$acx_pthread_ok" = xno; then
134                 PTHREAD_LIBS=""
135                 PTHREAD_CFLAGS=""
136         fi
137         LIBS="$save_LIBS"
138         CFLAGS="$save_CFLAGS"
139 fi
140
141 # We must check for the threads library under a number of different
142 # names; the ordering is very important because some systems
143 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
144 # libraries is broken (non-POSIX).
145
146 # Create a list of thread flags to try.  Items starting with a "-" are
147 # C compiler flags, and other items are library names, except for "none"
148 # which indicates that we try without any flags at all, and "pthread-config"
149 # which is a program returning the flags for the Pth emulation library.
150
151 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
152
153 # The ordering *is* (sometimes) important.  Some notes on the
154 # individual items follow:
155
156 # pthreads: AIX (must check this before -lpthread)
157 # none: in case threads are in libc; should be tried before -Kthread and
158 #       other compiler flags to prevent continual compiler warnings
159 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
160 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
161 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
162 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
163 # -pthreads: Solaris/gcc
164 # -mthreads: Mingw32/gcc, Lynx/gcc
165 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
166 #      doesn't hurt to check since this sometimes defines pthreads too;
167 #      also defines -D_REENTRANT)
168 # pthread: Linux, etcetera
169 # --thread-safe: KAI C++
170 # pthread-config: use pthread-config program (for GNU Pth library)
171
172 case "${host_cpu}-${host_os}" in
173         *solaris*)
174
175         # On Solaris (at least, for some versions), libc contains stubbed
176         # (non-functional) versions of the pthreads routines, so link-based
177         # tests will erroneously succeed.  (We need to link with -pthread or
178         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
179         # a function called by this macro, so we could check for that, but
180         # who knows whether they'll stub that too in a future libc.)  So,
181         # we'll just look for -pthreads and -lpthread first:
182
183         acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags"
184         ;;
185 esac
186
187 if test x"$acx_pthread_ok" = xno; then
188 for flag in $acx_pthread_flags; do
189
190         case $flag in
191                 none)
192                 AC_MSG_CHECKING([whether pthreads work without any flags])
193                 ;;
194
195                 -*)
196                 AC_MSG_CHECKING([whether pthreads work with $flag])
197                 PTHREAD_CFLAGS="$flag"
198                 ;;
199
200                 pthread-config)
201                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
202                 if test x"$acx_pthread_config" = xno; then continue; fi
203                 PTHREAD_CFLAGS="`pthread-config --cflags`"
204                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
205                 ;;
206
207                 *)
208                 AC_MSG_CHECKING([for the pthreads library -l$flag])
209                 PTHREAD_LIBS="-l$flag"
210                 ;;
211         esac
212
213         save_LIBS="$LIBS"
214         save_CFLAGS="$CFLAGS"
215         LIBS="$PTHREAD_LIBS $LIBS"
216         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
217
218         # Check for various functions.  We must include pthread.h,
219         # since some functions may be macros.  (On the Sequent, we
220         # need a special flag -Kthread to make this header compile.)
221         # We check for pthread_join because it is in -lpthread on IRIX
222         # while pthread_create is in libc.  We check for pthread_attr_init
223         # due to DEC craziness with -lpthreads.  We check for
224         # pthread_cleanup_push because it is one of the few pthread
225         # functions on Solaris that doesn't have a non-functional libc stub.
226         # We try pthread_create on general principles.
227         AC_TRY_LINK([#include <pthread.h>],
228                     [pthread_t th; pthread_join(th, 0);
229                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
230                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
231                     [acx_pthread_ok=yes])
232
233         LIBS="$save_LIBS"
234         CFLAGS="$save_CFLAGS"
235
236         AC_MSG_RESULT($acx_pthread_ok)
237         if test "x$acx_pthread_ok" = xyes; then
238                 break;
239         fi
240
241         PTHREAD_LIBS=""
242         PTHREAD_CFLAGS=""
243 done
244 fi
245
246 # Various other checks:
247 if test "x$acx_pthread_ok" = xyes; then
248         save_LIBS="$LIBS"
249         LIBS="$PTHREAD_LIBS $LIBS"
250         save_CFLAGS="$CFLAGS"
251         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
252
253         # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
254         AC_MSG_CHECKING([for joinable pthread attribute])
255         attr_name=unknown
256         for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
257             AC_TRY_LINK([#include <pthread.h>], [int attr=$attr;],
258                         [attr_name=$attr; break])
259         done
260         AC_MSG_RESULT($attr_name)
261         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
262             AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
263                                [Define to necessary symbol if this constant
264                                 uses a non-standard name on your system.])
265         fi
266
267         AC_MSG_CHECKING([if more special flags are required for pthreads])
268         flag=no
269         case "${host_cpu}-${host_os}" in
270             *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
271             *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
272         esac
273         AC_MSG_RESULT(${flag})
274         if test "x$flag" != xno; then
275             PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
276         fi
277
278         LIBS="$save_LIBS"
279         CFLAGS="$save_CFLAGS"
280
281         # More AIX lossage: must compile with cc_r
282         AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
283 else
284         PTHREAD_CC="$CC"
285 fi
286
287 AC_SUBST(PTHREAD_LIBS)
288 AC_SUBST(PTHREAD_CFLAGS)
289 AC_SUBST(PTHREAD_CC)
290
291 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
292 if test x"$acx_pthread_ok" = xyes; then
293         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
294         :
295 else
296         acx_pthread_ok=no
297         $2
298 fi
299 AC_LANG_RESTORE
300 ])dnl ACX_PTHREAD