2006-02-22 Marcus Brinkmann <marcus@g10code.de>
[gpgme.git] / m4 / pth.m4
1 dnl ##
2 dnl ##  GNU Pth - The GNU Portable Threads
3 dnl ##  Copyright (c) 1999-2002 Ralf S. Engelschall <rse@engelschall.com>
4 dnl ##
5 dnl ##  This file is part of GNU Pth, a non-preemptive thread scheduling
6 dnl ##  library which can be found at http://www.gnu.org/software/pth/.
7 dnl ##
8 dnl ##  This library is free software; you can redistribute it and/or
9 dnl ##  modify it under the terms of the GNU Lesser General Public
10 dnl ##  License as published by the Free Software Foundation; either
11 dnl ##  version 2.1 of the License, or (at your option) any later version.
12 dnl ##
13 dnl ##  This library is distributed in the hope that it will be useful,
14 dnl ##  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 dnl ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 dnl ##  Lesser General Public License for more details.
17 dnl ##
18 dnl ##  You should have received a copy of the GNU Lesser General Public
19 dnl ##  License along with this library; if not, write to the Free Software
20 dnl ##  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 dnl ##  USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
22 dnl ##
23 dnl ##  pth.m4: Autoconf macro for locating GNU Pth from within
24 dnl ##          configure.in of third-party software packages
25 dnl ##
26
27 dnl ##
28 dnl ##  Synopsis:
29 dnl ##  AC_CHECK_PTH([MIN-VERSION [,          # minimum Pth version, e.g. 1.2.0
30 dnl ##                DEFAULT-WITH-PTH [,     # default value for --with-pth option
31 dnl ##                DEFAULT-WITH-PTH-TEST [,# default value for --with-pth-test option
32 dnl ##                EXTEND-VARS [,          # whether CFLAGS/LDFLAGS/etc are extended
33 dnl ##                ACTION-IF-FOUND [,      # action to perform if Pth was found
34 dnl ##                ACTION-IF-NOT-FOUND     # action to perform if Pth was not found
35 dnl ##                ]]]]]])
36 dnl ##  Examples:
37 dnl ##  AC_CHECK_PTH(1.2.0)
38 dnl ##  AC_CHECK_PTH(1.2.0,,,no,CFLAGS="$CFLAGS -DHAVE_PTH $PTH_CFLAGS")
39 dnl ##  AC_CHECK_PTH(1.2.0,yes,yes,yes,CFLAGS="$CFLAGS -DHAVE_PTH")
40 dnl ##
41 dnl
42 dnl #   auxilliary macros
43 AC_DEFUN([_AC_PTH_ERROR], [dnl
44 AC_MSG_RESULT([*FAILED*])
45 dnl define(_ac_pth_line,dnl
46 dnl "+------------------------------------------------------------------------+")
47 dnl echo " _ac_pth_line" 1>&2
48 cat <<EOT | sed -e 's/^[[       ]]*/ | /' -e 's/>>/  /' 1>&2
49 $1
50 EOT
51 dnl echo " _ac_pth_line" 1>&2
52 dnl undefine(_ac_pth_line)
53 exit 1
54 ])
55 AC_DEFUN([_AC_PTH_VERBOSE], [dnl
56 if test ".$verbose" = .yes; then
57     AC_MSG_RESULT([  $1])
58 fi
59 ])
60 dnl #   the user macro
61 AC_DEFUN([AC_CHECK_PTH], [dnl
62 dnl
63 dnl #   prerequisites
64 AC_REQUIRE([AC_PROG_CC])dnl
65 AC_REQUIRE([AC_PROG_CPP])dnl
66 dnl
67 PTH_CPPFLAGS=''
68 PTH_CFLAGS=''
69 PTH_LDFLAGS=''
70 PTH_LIBS=''
71 AC_SUBST(PTH_CPPFLAGS)
72 AC_SUBST(PTH_CFLAGS)
73 AC_SUBST(PTH_LDFLAGS)
74 AC_SUBST(PTH_LIBS)
75 dnl #   command line options
76 AC_MSG_CHECKING(for GNU Pth)
77 _AC_PTH_VERBOSE([])
78 AC_ARG_WITH(pth,dnl
79 [  --with-pth[=ARG]        Build with GNU Pth Library  (default=]ifelse([$2],,yes,$2)[)],dnl
80 ,dnl
81 with_pth="ifelse([$2],,yes,$2)"
82 )dnl
83 AC_ARG_WITH(pth-test,dnl
84 [  --with-pth-test         Perform GNU Pth Sanity Test (default=]ifelse([$3],,yes,$3)[)],dnl
85 ,dnl
86 with_pth_test="ifelse([$3],,yes,$3)"
87 )dnl
88 _AC_PTH_VERBOSE([+ Command Line Options:])
89 _AC_PTH_VERBOSE([    o --with-pth=$with_pth])
90 _AC_PTH_VERBOSE([    o --with-pth-test=$with_pth_test])
91 dnl
92 dnl #   configuration
93 if test ".$with_pth" != .no; then
94     _pth_subdir=no
95     _pth_subdir_opts=''
96     case "$with_pth" in
97         subdir:* )
98             _pth_subdir=yes
99             changequote(, )dnl
100             _pth_subdir_opts=`echo $with_pth | sed -e 's/^subdir:[^     ]*[     ]*//'`
101             with_pth=`echo $with_pth | sed -e 's/^subdir:\([^   ]*\).*$/\1/'`
102             changequote([, ])dnl
103             ;;
104     esac
105     _pth_version=""
106     _pth_location=""
107     _pth_type=""
108     _pth_cppflags=""
109     _pth_cflags=""
110     _pth_ldflags=""
111     _pth_libs=""
112     if test ".$with_pth" = .yes; then
113         #   via config script in $PATH
114         changequote(, )dnl
115         _pth_version=`(pth-config --version) 2>/dev/null |\
116                       sed -e 's/^.*\([0-9]\.[0-9]*[ab.][0-9]*\).*$/\1/'`
117         changequote([, ])dnl
118         if test ".$_pth_version" != .; then
119             _pth_location=`pth-config --prefix`
120             _pth_type='installed'
121             _pth_cppflags=`pth-config --cflags`
122             _pth_cflags=`pth-config --cflags`
123             _pth_ldflags=`pth-config --ldflags`
124             _pth_libs=`pth-config --libs`
125         fi
126     elif test -d "$with_pth"; then
127         with_pth=`echo $with_pth | sed -e 's;/*$;;'`
128         _pth_found=no
129         #   via locally included source tree
130         if test ".$_pth_subdir" = .yes; then
131             _pth_location="$with_pth"
132             _pth_type='local'
133             _pth_cppflags="-I$with_pth"
134             _pth_cflags="-I$with_pth"
135             if test -f "$with_pth/ltconfig"; then
136                 _pth_ldflags="-L$with_pth/.libs"
137             else
138                 _pth_ldflags="-L$with_pth"
139             fi
140             _pth_libs="-lpth"
141             changequote(, )dnl
142             _pth_version=`grep '^const char PTH_Hello' $with_pth/pth_vers.c |\
143                           sed -e 's;^.*Version[         ]*\([0-9]*\.[0-9]*[.ab][0-9]*\)[        ].*$;\1;'`
144             changequote([, ])dnl
145             _pth_found=yes
146             ac_configure_args="$ac_configure_args --enable-subdir $_pth_subdir_opts"
147             with_pth_test=no
148         fi
149         #   via config script under a specified directory
150         #   (a standard installation, but not a source tree)
151         if test ".$_pth_found" = .no; then
152             for _dir in $with_pth/bin $with_pth; do
153                 if test -f "$_dir/pth-config"; then
154                     test -f "$_dir/pth-config.in" && continue # pth-config in source tree!
155                     changequote(, )dnl
156                     _pth_version=`($_dir/pth-config --version) 2>/dev/null |\
157                                   sed -e 's/^.*\([0-9]\.[0-9]*[ab.][0-9]*\).*$/\1/'`
158                     changequote([, ])dnl
159                     if test ".$_pth_version" != .; then
160                         _pth_location=`$_dir/pth-config --prefix`
161                         _pth_type="installed"
162                         _pth_cppflags=`$_dir/pth-config --cflags`
163                         _pth_cflags=`$_dir/pth-config --cflags`
164                         _pth_ldflags=`$_dir/pth-config --ldflags`
165                         _pth_libs=`$_dir/pth-config --libs`
166                         _pth_found=yes
167                         break
168                     fi
169                 fi
170             done
171         fi
172         #   in any subarea under a specified directory
173         #   (either a special installation or a Pth source tree)
174         if test ".$_pth_found" = .no; then
175             changequote(, )dnl
176             _pth_found=0
177             for _file in x `find $with_pth -name "pth.h" -type f -print`; do
178                 test .$_file = .x && continue
179                 _dir=`echo $_file | sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;'`
180                 _pth_version=`($_dir/pth-config --version) 2>/dev/null |\
181                               sed -e 's/^.*\([0-9]\.[0-9]*[ab.][0-9]*\).*$/\1/'`
182                 if test ".$_pth_version" = .; then
183                     _pth_version=`grep '^#define PTH_VERSION_STR' $_file |\
184                                   sed -e 's;^#define[   ]*PTH_VERSION_STR[      ]*"\([0-9]*\.[0-9]*[.ab][0-9]*\)[       ].*$;\1;'`
185                 fi
186                 _pth_cppflags="-I$_dir"
187                 _pth_cflags="-I$_dir"
188                 _pth_found=`expr $_pth_found + 1`
189             done
190             for _file in x `find $with_pth -name "libpth.[aso]" -type f -print`; do
191                 test .$_file = .x && continue
192                 _dir=`echo $_file | sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;'`
193                 _pth_ldflags="-L$_dir"
194                 _pth_libs="-lpth"
195                 _pth_found=`expr $_pth_found + 1`
196             done
197             changequote([, ])dnl
198             if test ".$_pth_found" = .2; then
199                 _pth_location="$with_pth"
200                 _pth_type="uninstalled"
201             else
202                 _pth_version=''
203             fi
204         fi
205     fi
206     _AC_PTH_VERBOSE([+ Determined Location:])
207     _AC_PTH_VERBOSE([    o path: $_pth_location])
208     _AC_PTH_VERBOSE([    o type: $_pth_type])
209     if test ".$_pth_version" = .; then
210         with_pth=no
211     else
212 dnl        if test ".$with_pth" != .yes; then
213 dnl             _AC_PTH_ERROR([dnl
214 dnl             Unable to locate GNU Pth under $with_pth.
215 dnl             Please specify the correct path to either a GNU Pth installation tree
216 dnl             (use --with-pth=DIR if you used --prefix=DIR for installing GNU Pth in
217 dnl             the past) or to a GNU Pth source tree (use --with-pth=DIR if DIR is a
218 dnl             path to a pth-X.Y.Z/ directory; but make sure the package is already
219 dnl             built, i.e., the "configure; make" step was already performed there).])
220 dnl        else
221 dnl             _AC_PTH_ERROR([dnl
222 dnl             Unable to locate GNU Pth in any system-wide location (see \$PATH).
223 dnl             Please specify the correct path to either a GNU Pth installation tree
224 dnl             (use --with-pth=DIR if you used --prefix=DIR for installing GNU Pth in
225 dnl             the past) or to a GNU Pth source tree (use --with-pth=DIR if DIR is a
226 dnl             path to a pth-X.Y.Z/ directory; but make sure the package is already
227 dnl             built, i.e., the "configure; make" step was already performed there).])
228 dnl        fi
229 dnl    fi
230     dnl #
231     dnl #  Check whether the found version is sufficiently new
232     dnl #
233     _req_version="ifelse([$1],,1.0.0,$1)"
234     for _var in _pth_version _req_version; do
235         eval "_val=\"\$${_var}\""
236         _major=`echo $_val | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\([[ab.]]\)\([[0-9]]*\)/\1/'`
237         _minor=`echo $_val | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\([[ab.]]\)\([[0-9]]*\)/\2/'`
238         _rtype=`echo $_val | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\([[ab.]]\)\([[0-9]]*\)/\3/'`
239         _micro=`echo $_val | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\([[ab.]]\)\([[0-9]]*\)/\4/'`
240         case $_rtype in
241             "a" ) _rtype=0 ;;
242             "b" ) _rtype=1 ;;
243             "." ) _rtype=2 ;;
244         esac
245         _hex=`echo dummy | awk '{ printf("%d%02d%1d%02d", major, minor, rtype, micro); }' \
246               "major=$_major" "minor=$_minor" "rtype=$_rtype" "micro=$_micro"`
247         eval "${_var}_hex=\"\$_hex\""
248     done
249     _AC_PTH_VERBOSE([+ Determined Versions:])
250     _AC_PTH_VERBOSE([    o existing: $_pth_version -> 0x$_pth_version_hex])
251     _AC_PTH_VERBOSE([    o required: $_req_version -> 0x$_req_version_hex])
252     _ok=0
253     if test ".$_pth_version_hex" != .; then
254         if test ".$_req_version_hex" != .; then
255             if test $_pth_version_hex -ge $_req_version_hex; then
256                 _ok=1
257             fi
258         fi
259     fi
260     if test ".$_ok" = .0; then
261         _AC_PTH_ERROR([dnl
262         Found Pth version $_pth_version, but required at least version $_req_version.
263         Upgrade Pth under $_pth_location to $_req_version or higher first, please.])
264     fi
265     dnl #
266     dnl #   Perform Pth Sanity Compile Check
267     dnl #
268     if test ".$with_pth_test" = .yes; then
269         _ac_save_CPPFLAGS="$CPPFLAGS"
270         _ac_save_CFLAGS="$CFLAGS"
271         _ac_save_LDFLAGS="$LDFLAGS"
272         _ac_save_LIBS="$LIBS"
273         CPPFLAGS="$CPPFLAGS $_pth_cppflags"
274         CFLAGS="$CFLAGS $_pth_cflags"
275         LDFLAGS="$LDFLAGS $_pth_ldflags"
276         LIBS="$LIBS $_pth_libs"
277         _AC_PTH_VERBOSE([+ Test Build Environment:])
278         _AC_PTH_VERBOSE([    o CPPFLAGS=\"$CPPFLAGS\"])
279         _AC_PTH_VERBOSE([    o CFLAGS=\"$CFLAGS\"])
280         _AC_PTH_VERBOSE([    o LDFLAGS=\"$LDFLAGS\"])
281         _AC_PTH_VERBOSE([    o LIBS=\"$LIBS\"])
282         cross_compile=no
283         define(_code1, [dnl
284         #include <stdio.h>
285         #include <pth.h>
286         ])
287         define(_code2, [dnl
288             FILE *fp;
289             if (!(fp = fopen("conftestval", "w")))
290                 exit(1);
291             fprintf(fp, "hmm");
292             fclose(fp);
293             pth_init();
294             pth_kill();
295             if (!(fp = fopen("conftestval", "w")))
296                 exit(1);
297             fprintf(fp, "yes");
298             fclose(fp);
299             exit(0);
300         ])
301         _AC_PTH_VERBOSE([+ Performing Sanity Checks:])
302         _AC_PTH_VERBOSE([    o pre-processor test])
303         AC_TRY_CPP(_code1, _ok=yes, _ok=no)
304         if test ".$_ok" != .yes; then
305             _AC_PTH_ERROR([dnl
306             Found GNU Pth $_pth_version under $_pth_location, but
307             was unable to perform a sanity pre-processor check. This means
308             the GNU Pth header pth.h was not found.
309             We used the following build environment:
310             >> CPP="$CPP"
311             >> CPPFLAGS="$CPPFLAGS"
312             See config.log for possibly more details.])
313         fi
314         _AC_PTH_VERBOSE([    o link check])
315         AC_TRY_LINK(_code1, _code2, _ok=yes, _ok=no)
316         if test ".$_ok" != .yes; then
317             _AC_PTH_ERROR([dnl
318             Found GNU Pth $_pth_version under $_pth_location, but
319             was unable to perform a sanity linker check. This means
320             the GNU Pth library libpth.a was not found.
321             We used the following build environment:
322             >> CC="$CC"
323             >> CFLAGS="$CFLAGS"
324             >> LDFLAGS="$LDFLAGS"
325             >> LIBS="$LIBS"
326             See config.log for possibly more details.])
327         fi
328         _AC_PTH_VERBOSE([    o run-time check])
329         AC_TRY_RUN(AC_LANG_PROGRAM(_code1, _code2), _ok=`cat conftestval`, _ok=no, _ok=no)
330         if test ".$_ok" != .yes; then
331             if test ".$_ok" = .no; then
332                 _AC_PTH_ERROR([dnl
333                 Found GNU Pth $_pth_version under $_pth_location, but
334                 was unable to perform a sanity execution check. This usually
335                 means that the GNU Pth shared library libpth.so is present
336                 but \$LD_LIBRARY_PATH is incomplete to execute a Pth test.
337                 In this case either disable this test via --without-pth-test,
338                 or extend \$LD_LIBRARY_PATH, or build GNU Pth as a static
339                 library only via its --disable-shared Autoconf option.
340                 We used the following build environment:
341                 >> CC="$CC"
342                 >> CFLAGS="$CFLAGS"
343                 >> LDFLAGS="$LDFLAGS"
344                 >> LIBS="$LIBS"
345                 See config.log for possibly more details.])
346             else
347                 _AC_PTH_ERROR([dnl
348                 Found GNU Pth $_pth_version under $_pth_location, but
349                 was unable to perform a sanity run-time check. This usually
350                 means that the GNU Pth library failed to work and possibly
351                 caused a core dump in the test program. In this case it
352                 is strongly recommended that you re-install GNU Pth and this
353                 time make sure that it really passes its "make test" procedure.
354                 We used the following build environment:
355                 >> CC="$CC"
356                 >> CFLAGS="$CFLAGS"
357                 >> LDFLAGS="$LDFLAGS"
358                 >> LIBS="$LIBS"
359                 See config.log for possibly more details.])
360             fi
361         fi
362         _extendvars="ifelse([$4],,yes,$4)"
363         if test ".$_extendvars" != .yes; then
364             CPPFLAGS="$_ac_save_CPPFLAGS"
365             CFLAGS="$_ac_save_CFLAGS"
366             LDFLAGS="$_ac_save_LDFLAGS"
367             LIBS="$_ac_save_LIBS"
368         fi
369     else
370         _extendvars="ifelse([$4],,yes,$4)"
371         if test ".$_extendvars" = .yes; then
372             if test ".$_pth_subdir" = .yes; then
373                 CPPFLAGS="$CPPFLAGS $_pth_cppflags"
374                 CFLAGS="$CFLAGS $_pth_cflags"
375                 LDFLAGS="$LDFLAGS $_pth_ldflags"
376                 LIBS="$LIBS $_pth_libs"
377             fi
378         fi
379     fi
380     PTH_CPPFLAGS="$_pth_cppflags"
381     PTH_CFLAGS="$_pth_cflags"
382     PTH_LDFLAGS="$_pth_ldflags"
383     PTH_LIBS="$_pth_libs"
384     AC_SUBST(PTH_CPPFLAGS)
385     AC_SUBST(PTH_CFLAGS)
386     AC_SUBST(PTH_LDFLAGS)
387     AC_SUBST(PTH_LIBS)
388     _AC_PTH_VERBOSE([+ Final Results:])
389     _AC_PTH_VERBOSE([    o PTH_CPPFLAGS=\"$PTH_CPPFLAGS\"])
390     _AC_PTH_VERBOSE([    o PTH_CFLAGS=\"$PTH_CFLAGS\"])
391     _AC_PTH_VERBOSE([    o PTH_LDFLAGS=\"$PTH_LDFLAGS\"])
392     _AC_PTH_VERBOSE([    o PTH_LIBS=\"$PTH_LIBS\"])
393 fi
394 fi
395 if test ".$with_pth" != .no; then
396     AC_MSG_RESULT([version $_pth_version, $_pth_type under $_pth_location])
397     ifelse([$5], , :, [$5])
398 else
399     AC_MSG_RESULT([no])
400     ifelse([$6], , :, [$6])
401 fi
402 ])