1dnl Copyright (c) 2005, Eric Crahen
2dnl
3dnl Permission is hereby granted, free of charge, to any person obtaining a copy
4dnl of this software and associated documentation files (the "Software"), to deal
5dnl in the Software without restriction, including without limitation the rights
6dnl to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7dnl copies of the Software, and to permit persons to whom the Software is furnished
8dnl to do so, subject to the following conditions:
9dnl
10dnl The above copyright notice and this permission notice shall be included in all
11dnl copies or substantial portions of the Software.
12dnl
13dnl THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14dnl IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15dnl FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16dnl AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17dnl WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18dnl CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20dnl Detect the library and include paths for ZThreads, perform some test
21dnl compilations.
22dnl
23dnl Should be used in AC_PROG_CC mode before the swtich to C++ if any is made
24dnl (eg before AC_LANG_CPLUSPLUS)
25dnl
26dnl --with-zthread-prefix : Skip detection, use this general path
27dnl --with-zthread-exec-prefix : Skip detecting the zthread-config tool
28dnl
29dnl Sets the following variables.
30dnl
31dnl ZTHREAD_CXXFLAGS
32dnl ZTHREAD_LIBS
33dnl
34AC_DEFUN([AM_PATH_ZTHREAD],
35[
36
37
38AC_ARG_WITH(zthread-prefix,[  --with-zthread-prefix=PFX   Prefix where ZTHREAD is installed (optional)],
39            zthread_prefix="$withval", zthread_prefix="")
40AC_ARG_WITH(zthread-exec-prefix,[  --with-zthread-exec-prefix=PFX Exec prefix where ZTHREAD is installed (optional)],
41            zthread_exec_prefix="$withval", zthread_exec_prefix="")
42AC_ARG_ENABLE(zthreadtest, [  --disable-zthreadtest       Do not try to compile and run a test ZTHREAD program],
43		    , enable_zthreadtest=yes)
44
45  if test x$zthread_exec_prefix != x ; then
46     zthread_args="$zthread_args --exec-prefix=$zthread_exec_prefix"
47     if test x${ZTHREAD_CONFIG+set} != xset ; then
48        ZTHREAD_CONFIG=$zthread_exec_prefix/bin/zthread-config
49     fi
50  fi
51  if test x$zthread_prefix != x ; then
52     zthread_args="$zthread_args --prefix=$zthread_prefix"
53     if test x${ZTHREAD_CONFIG+set} != xset ; then
54        ZTHREAD_CONFIG=$zthread_prefix/bin/zthread-config
55     fi
56  fi
57
58  AC_PATH_PROG(ZTHREAD_CONFIG, zthread-config, no)
59  min_zthread_version=ifelse([$1], ,0.11.0,$1)
60  AC_MSG_CHECKING(for ZTHREAD - version >= $min_zthread_version)
61  no_zthread=""
62  if test "$ZTHREAD_CONFIG" = "no" ; then
63    no_zthread=yes
64  else
65    ZTHREAD_CXXFLAGS=`$ZTHREAD_CONFIG $zthreadconf_args --cflags`
66    ZTHREAD_LIBS=`$ZTHREAD_CONFIG $zthreadconf_args --libs`
67
68    zthread_major_version=`$ZTHREAD_CONFIG $zthread_args --version | \
69           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'`
70    zthread_minor_version=`$ZTHREAD_CONFIG $zthread_args --version | \
71           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'`
72    zthread_micro_version=`$ZTHREAD_CONFIG $zthread_config_args --version | \
73           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'`
74    if test "x$enable_zthreadtest" = "xyes" ; then
75      ac_save_CXXFLAGS="$CXXFLAGS"
76      ac_save_LIBS="$LIBS"
77      CXXFLAGS="$CXXFLAGS $ZTHREAD_CXXFLAGS"
78      LIBS="$LIBS $ZTHREAD_LIBS"
79
80dnl
81dnl Now check if the installed ZTHREAD is sufficiently new. (Also sanity
82dnl checks the results of zthread-config to some extent
83dnl
84      rm -f conf.zthreadtest
85      AC_TRY_RUN([
86
87
88#include <stdio.h>
89#include <string.h>
90#include <unistd.h>
91#include "ZThread.h"
92
93int main (int argc, char *argv[]) {
94
95  int major, minor, micro;
96  char tmp_version[256];
97
98  { FILE *fp = fopen("conf.zthreadtest", "a"); if ( fp ) fclose(fp); }
99
100
101  /* HP/UX 9 (%@#!) writes to sscanf strings */
102  strcpy(tmp_version, "$min_zthread_version");
103
104  if (sscanf(tmp_version, "%d.%d.%d", &major, &minor, &micro) != 3) {
105
106    printf("%s, bad version string\n", "$min_zthread_version");
107    return 1;
108
109  }
110
111  if (($zthread_major_version > major) ||
112     (($zthread_major_version == major) && ($zthread_minor_version > minor)) ||
113     (($zthread_major_version == major) && ($zthread_minor_version == minor) &&
114     ($zthread_micro_version >= micro))) {
115      return 0;
116  } else {
117
118    printf("\n*** 'zthread-config --version' returned %d.%d.%d, but the minimum version\n", $zthread_major_version, $zthread_minor_version, $zthread_micro_version);
119    printf("*** of ZThread required is %d.%d.%d. If zthread-config is correct, then it is\n", major, minor, micro);
120    printf("*** best to upgrade to the required version.\n");
121    printf("*** If zthread-config was wrong, set the environment variable ZTHREAD_CONFIG\n");
122    printf("*** to point to the correct copy of zthread-config, and remove the file\n");
123    printf("*** config.cache before re-running configure\n");
124
125    return 1;
126  }
127
128}
129
130],, no_zthread=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"])
131
132    CXXFLAGS="$ac_save_CXXFLAGS"
133    LIBS="$ac_save_LIBS"
134    fi
135  fi
136
137  if test "x$no_zthread" = x ; then
138
139     AC_MSG_RESULT(yes)
140     ifelse([$2], , :, [$2])
141
142  else
143
144     AC_MSG_RESULT(no)
145
146     if test "$ZTHREAD_CONFIG" = "no" ; then
147
148       echo "*** The zthread-config script installed by ZThread could not be found"
149       echo "*** If ZThread was installed in PREFIX, make sure PREFIX/bin is in"
150       echo "*** your path, or set the ZTHREAD_CONFIG environment variable to the"
151       echo "*** full path to zthread-config."
152
153     else
154       if test -f conf.zthreadtest ; then
155        :
156       else
157
158          echo "*** Could not run ZThread test program, checking why..."
159          CXXFLAGS="$CXXFLAGS $ZTHREAD_CXXFLAGS"
160          LIBS="$LIBS $ZTHREAD_LIBS"
161
162          echo $LIBS;
163
164          AC_TRY_LINK([#include "ZThread.h"],
165                      [ return 0; ], [
166          echo "*** The test program compiled, but did not run. This usually means"
167          echo "*** that the run-time linker is not finding ZThread or finding the wrong"
168          echo "*** version of ZThread. If it is not finding ZThread, you'll need to set your"
169          echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
170          echo "*** to the installed location  Also, make sure you have run ldconfig if that"
171          echo "*** is required on your system"
172	        echo "***"
173          echo "*** If you have an old version installed, it is best to remove it, although"
174          echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH"],
175        [ echo "*** The test program failed to compile or link. See the file config.log for the"
176          echo "*** exact error that occured. This usually means ZThread was incorrectly installed"
177          echo "*** or that you have moved ZThread since it was installed. In the latter case, you"
178          echo "*** may want to edit the zthread-config script: $ZTHREAD_CONFIG" ])
179          CXXFLAGS="$ac_save_CXXFLAGS"
180          LIBS="$ac_save_LIBS"
181       fi
182     fi
183
184     ZTHREAD_CXXFLAGS=""
185     ZTHREAD_LIBS=""
186     ifelse([$3], , :, [$3])
187
188  fi
189
190  AC_SUBST(ZTHREAD_CXXFLAGS)
191  AC_SUBST(ZTHREAD_LIBS)
192
193  rm -f conf.zthreadtest
194
195])
196