8bffbd3f65afa08a9e75168d54fd343dfdb0be41
[gpgme.git] / m4 / pth.m4
1 dnl ##
2 dnl ##  GNU Pth - The GNU Portable Threads
3 dnl ##  Copyright (c) 1999-2003 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.ac 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 define(_ac_pth_line,dnl
46 "+------------------------------------------------------------------------+")
47 echo " _ac_pth_line" 1>&2
48 cat <<EOT | sed -e 's/^[[       ]]*/ | /' -e 's/>>/  /' 1>&2
49 $1
50 EOT
51 echo " _ac_pth_line" 1>&2
52 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         if test ".$with_pth" != .yes; then
211              _AC_PTH_ERROR([dnl
212              Unable to locate GNU Pth under $with_pth.
213              Please specify the correct path to either a GNU Pth installation tree
214              (use --with-pth=DIR if you used --prefix=DIR for installing GNU Pth in
215              the past) or to a GNU Pth source tree (use --with-pth=DIR if DIR is a
216              path to a pth-X.Y.Z/ directory; but make sure the package is already
217              built, i.e., the "configure; make" step was already performed there).])
218         else
219              _AC_PTH_ERROR([dnl
220              Unable to locate GNU Pth in any system-wide location (see \$PATH).
221              Please specify the correct path to either a GNU Pth installation tree
222              (use --with-pth=DIR if you used --prefix=DIR for installing GNU Pth in
223              the past) or to a GNU Pth source tree (use --with-pth=DIR if DIR is a
224              path to a pth-X.Y.Z/ directory; but make sure the package is already
225              built, i.e., the "configure; make" step was already performed there).])
226         fi
227     fi
228     dnl #
229     dnl #  Check whether the found version is sufficiently new
230     dnl #
231     _req_version="ifelse([$1],,1.0.0,$1)"
232     for _var in _pth_version _req_version; do
233         eval "_val=\"\$${_var}\""
234         _major=`echo $_val | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\([[ab.]]\)\([[0-9]]*\)/\1/'`
235         _minor=`echo $_val | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\([[ab.]]\)\([[0-9]]*\)/\2/'`
236         _rtype=`echo $_val | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\([[ab.]]\)\([[0-9]]*\)/\3/'`
237         _micro=`echo $_val | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\([[ab.]]\)\([[0-9]]*\)/\4/'`
238         case $_rtype in
239             "a" ) _rtype=0 ;;
240             "b" ) _rtype=1 ;;
241             "." ) _rtype=2 ;;
242         esac
243         _hex=`echo dummy | awk '{ printf("%d%02d%1d%02d", major, minor, rtype, micro); }' \
244               "major=$_major" "minor=$_minor" "rtype=$_rtype" "micro=$_micro"`
245         eval "${_var}_hex=\"\$_hex\""
246     done
247     _AC_PTH_VERBOSE([+ Determined Versions:])
248     _AC_PTH_VERBOSE([    o existing: $_pth_version -> 0x$_pth_version_hex])
249     _AC_PTH_VERBOSE([    o required: $_req_version -> 0x$_req_version_hex])
250     _ok=0
251     if test ".$_pth_version_hex" != .; then
252         if test ".$_req_version_hex" != .; then
253             if test $_pth_version_hex -ge $_req_version_hex; then
254                 _ok=1
255             fi
256         fi
257     fi
258     if test ".$_ok" = .0; then
259         _AC_PTH_ERROR([dnl
260         Found Pth version $_pth_version, but required at least version $_req_version.
261         Upgrade Pth under $_pth_location to $_req_version or higher first, please.])
262     fi
263     dnl #
264     dnl #   Perform Pth Sanity Compile Check
265     dnl #
266     if test ".$with_pth_test" = .yes; then
267         _ac_save_CPPFLAGS="$CPPFLAGS"
268         _ac_save_CFLAGS="$CFLAGS"
269         _ac_save_LDFLAGS="$LDFLAGS"
270         _ac_save_LIBS="$LIBS"
271         CPPFLAGS="$CPPFLAGS $_pth_cppflags"
272         CFLAGS="$CFLAGS $_pth_cflags"
273         LDFLAGS="$LDFLAGS $_pth_ldflags"
274         LIBS="$LIBS $_pth_libs"
275         _AC_PTH_VERBOSE([+ Test Build Environment:])
276         _AC_PTH_VERBOSE([    o CPPFLAGS=\"$CPPFLAGS\"])
277         _AC_PTH_VERBOSE([    o CFLAGS=\"$CFLAGS\"])
278         _AC_PTH_VERBOSE([    o LDFLAGS=\"$LDFLAGS\"])
279         _AC_PTH_VERBOSE([    o LIBS=\"$LIBS\"])
280         cross_compile=no
281         define(_code1, [dnl
282         #include <stdio.h>
283         #include <pth.h>
284         ])
285         define(_code2, [dnl
286         int main(int argc, char *argv[])
287         {
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         ])
302         _AC_PTH_VERBOSE([+ Performing Sanity Checks:])
303         _AC_PTH_VERBOSE([    o pre-processor test])
304         AC_TRY_CPP(_code1, _ok=yes, _ok=no)
305         if test ".$_ok" != .yes; then
306             _AC_PTH_ERROR([dnl
307             Found GNU Pth $_pth_version under $_pth_location, but
308             was unable to perform a sanity pre-processor check. This means
309             the GNU Pth header pth.h was not found.
310             We used the following build environment:
311             >> CPP="$CPP"
312             >> CPPFLAGS="$CPPFLAGS"
313             See config.log for possibly more details.])
314         fi
315         _AC_PTH_VERBOSE([    o link check])
316         AC_TRY_LINK(_code1, _code2, _ok=yes, _ok=no)
317         if test ".$_ok" != .yes; then
318             _AC_PTH_ERROR([dnl
319             Found GNU Pth $_pth_version under $_pth_location, but
320             was unable to perform a sanity linker check. This means
321             the GNU Pth library libpth.a was not found.
322             We used the following build environment:
323             >> CC="$CC"
324             >> CFLAGS="$CFLAGS"
325             >> LDFLAGS="$LDFLAGS"
326             >> LIBS="$LIBS"
327             See config.log for possibly more details.])
328         fi
329         _AC_PTH_VERBOSE([    o run-time check])
330         AC_TRY_RUN(_code1 _code2, _ok=`cat conftestval`, _ok=no, _ok=no)
331         if test ".$_ok" != .yes; then
332             if test ".$_ok" = .no; then
333                 _AC_PTH_ERROR([dnl
334                 Found GNU Pth $_pth_version under $_pth_location, but
335                 was unable to perform a sanity execution check. This usually
336                 means that the GNU Pth shared library libpth.so is present
337                 but \$LD_LIBRARY_PATH is incomplete to execute a Pth test.
338                 In this case either disable this test via --without-pth-test,
339                 or extend \$LD_LIBRARY_PATH, or build GNU Pth as a static
340                 library only via its --disable-shared Autoconf option.
341                 We used the following build environment:
342                 >> CC="$CC"
343                 >> CFLAGS="$CFLAGS"
344                 >> LDFLAGS="$LDFLAGS"
345                 >> LIBS="$LIBS"
346                 See config.log for possibly more details.])
347             else
348                 _AC_PTH_ERROR([dnl
349                 Found GNU Pth $_pth_version under $_pth_location, but
350                 was unable to perform a sanity run-time check. This usually
351                 means that the GNU Pth library failed to work and possibly
352                 caused a core dump in the test program. In this case it
353                 is strongly recommended that you re-install GNU Pth and this
354                 time make sure that it really passes its "make test" procedure.
355                 We used the following build environment:
356                 >> CC="$CC"
357                 >> CFLAGS="$CFLAGS"
358                 >> LDFLAGS="$LDFLAGS"
359                 >> LIBS="$LIBS"
360                 See config.log for possibly more details.])
361             fi
362         fi
363         _extendvars="ifelse([$4],,yes,$4)"
364         if test ".$_extendvars" != .yes; then
365             CPPFLAGS="$_ac_save_CPPFLAGS"
366             CFLAGS="$_ac_save_CFLAGS"
367             LDFLAGS="$_ac_save_LDFLAGS"
368             LIBS="$_ac_save_LIBS"
369         fi
370     else
371         _extendvars="ifelse([$4],,yes,$4)"
372         if test ".$_extendvars" = .yes; then
373             if test ".$_pth_subdir" = .yes; then
374                 CPPFLAGS="$CPPFLAGS $_pth_cppflags"
375                 CFLAGS="$CFLAGS $_pth_cflags"
376                 LDFLAGS="$LDFLAGS $_pth_ldflags"
377                 LIBS="$LIBS $_pth_libs"
378             fi
379         fi
380     fi
381     PTH_CPPFLAGS="$_pth_cppflags"
382     PTH_CFLAGS="$_pth_cflags"
383     PTH_LDFLAGS="$_pth_ldflags"
384     PTH_LIBS="$_pth_libs"
385     AC_SUBST(PTH_CPPFLAGS)
386     AC_SUBST(PTH_CFLAGS)
387     AC_SUBST(PTH_LDFLAGS)
388     AC_SUBST(PTH_LIBS)
389     _AC_PTH_VERBOSE([+ Final Results:])
390     _AC_PTH_VERBOSE([    o PTH_CPPFLAGS=\"$PTH_CPPFLAGS\"])
391     _AC_PTH_VERBOSE([    o PTH_CFLAGS=\"$PTH_CFLAGS\"])
392     _AC_PTH_VERBOSE([    o PTH_LDFLAGS=\"$PTH_LDFLAGS\"])
393     _AC_PTH_VERBOSE([    o PTH_LIBS=\"$PTH_LIBS\"])
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 ])
403