2005-11-17 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         int main(int argc, char *argv[])
289         {
290             FILE *fp;
291             if (!(fp = fopen("conftestval", "w")))
292                 exit(1);
293             fprintf(fp, "hmm");
294             fclose(fp);
295             pth_init();
296             pth_kill();
297             if (!(fp = fopen("conftestval", "w")))
298                 exit(1);
299             fprintf(fp, "yes");
300             fclose(fp);
301             exit(0);
302         }
303         ])
304         _AC_PTH_VERBOSE([+ Performing Sanity Checks:])
305         _AC_PTH_VERBOSE([    o pre-processor test])
306         AC_TRY_CPP(_code1, _ok=yes, _ok=no)
307         if test ".$_ok" != .yes; then
308             _AC_PTH_ERROR([dnl
309             Found GNU Pth $_pth_version under $_pth_location, but
310             was unable to perform a sanity pre-processor check. This means
311             the GNU Pth header pth.h was not found.
312             We used the following build environment:
313             >> CPP="$CPP"
314             >> CPPFLAGS="$CPPFLAGS"
315             See config.log for possibly more details.])
316         fi
317         _AC_PTH_VERBOSE([    o link check])
318         AC_TRY_LINK(_code1, _code2, _ok=yes, _ok=no)
319         if test ".$_ok" != .yes; then
320             _AC_PTH_ERROR([dnl
321             Found GNU Pth $_pth_version under $_pth_location, but
322             was unable to perform a sanity linker check. This means
323             the GNU Pth library libpth.a was not found.
324             We used the following build environment:
325             >> CC="$CC"
326             >> CFLAGS="$CFLAGS"
327             >> LDFLAGS="$LDFLAGS"
328             >> LIBS="$LIBS"
329             See config.log for possibly more details.])
330         fi
331         _AC_PTH_VERBOSE([    o run-time check])
332         AC_TRY_RUN(_code1 _code2, _ok=`cat conftestval`, _ok=no, _ok=no)
333         if test ".$_ok" != .yes; then
334             if test ".$_ok" = .no; then
335                 _AC_PTH_ERROR([dnl
336                 Found GNU Pth $_pth_version under $_pth_location, but
337                 was unable to perform a sanity execution check. This usually
338                 means that the GNU Pth shared library libpth.so is present
339                 but \$LD_LIBRARY_PATH is incomplete to execute a Pth test.
340                 In this case either disable this test via --without-pth-test,
341                 or extend \$LD_LIBRARY_PATH, or build GNU Pth as a static
342                 library only via its --disable-shared Autoconf option.
343                 We used the following build environment:
344                 >> CC="$CC"
345                 >> CFLAGS="$CFLAGS"
346                 >> LDFLAGS="$LDFLAGS"
347                 >> LIBS="$LIBS"
348                 See config.log for possibly more details.])
349             else
350                 _AC_PTH_ERROR([dnl
351                 Found GNU Pth $_pth_version under $_pth_location, but
352                 was unable to perform a sanity run-time check. This usually
353                 means that the GNU Pth library failed to work and possibly
354                 caused a core dump in the test program. In this case it
355                 is strongly recommended that you re-install GNU Pth and this
356                 time make sure that it really passes its "make test" procedure.
357                 We used the following build environment:
358                 >> CC="$CC"
359                 >> CFLAGS="$CFLAGS"
360                 >> LDFLAGS="$LDFLAGS"
361                 >> LIBS="$LIBS"
362                 See config.log for possibly more details.])
363             fi
364         fi
365         _extendvars="ifelse([$4],,yes,$4)"
366         if test ".$_extendvars" != .yes; then
367             CPPFLAGS="$_ac_save_CPPFLAGS"
368             CFLAGS="$_ac_save_CFLAGS"
369             LDFLAGS="$_ac_save_LDFLAGS"
370             LIBS="$_ac_save_LIBS"
371         fi
372     else
373         _extendvars="ifelse([$4],,yes,$4)"
374         if test ".$_extendvars" = .yes; then
375             if test ".$_pth_subdir" = .yes; then
376                 CPPFLAGS="$CPPFLAGS $_pth_cppflags"
377                 CFLAGS="$CFLAGS $_pth_cflags"
378                 LDFLAGS="$LDFLAGS $_pth_ldflags"
379                 LIBS="$LIBS $_pth_libs"
380             fi
381         fi
382     fi
383     PTH_CPPFLAGS="$_pth_cppflags"
384     PTH_CFLAGS="$_pth_cflags"
385     PTH_LDFLAGS="$_pth_ldflags"
386     PTH_LIBS="$_pth_libs"
387     AC_SUBST(PTH_CPPFLAGS)
388     AC_SUBST(PTH_CFLAGS)
389     AC_SUBST(PTH_LDFLAGS)
390     AC_SUBST(PTH_LIBS)
391     _AC_PTH_VERBOSE([+ Final Results:])
392     _AC_PTH_VERBOSE([    o PTH_CPPFLAGS=\"$PTH_CPPFLAGS\"])
393     _AC_PTH_VERBOSE([    o PTH_CFLAGS=\"$PTH_CFLAGS\"])
394     _AC_PTH_VERBOSE([    o PTH_LDFLAGS=\"$PTH_LDFLAGS\"])
395     _AC_PTH_VERBOSE([    o PTH_LIBS=\"$PTH_LIBS\"])
396 fi
397 fi
398 if test ".$with_pth" != .no; then
399     AC_MSG_RESULT([version $_pth_version, $_pth_type under $_pth_location])
400     ifelse([$5], , :, [$5])
401 else
402     AC_MSG_RESULT([no])
403     ifelse([$6], , :, [$6])
404 fi
405 ])