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