blob: 08043b4c61af48b91edaafeec018350cf17e06fd [file] [log] [blame]
David Reiss79ae0f82007-09-17 21:15:47 +00001dnl @synopsis AX_LIB_EVENT([MINIMUM-VERSION])
2dnl
David Reissfaebedd2007-09-17 23:20:38 +00003dnl Test for the libevent library of a particular version (or newer).
David Reiss79ae0f82007-09-17 21:15:47 +00004dnl
5dnl If no path to the installed libevent is given, the macro will first try
6dnl using no -I or -L flags, then searches under /usr, /usr/local, /opt,
7dnl and /opt/libevent.
8dnl If these all fail, it will try the $LIBEVENT_ROOT environment variable.
9dnl
10dnl This macro requires that #include <sys/types.h> works and defines u_char.
11dnl
12dnl This macro calls:
13dnl AC_SUBST(LIBEVENT_CPPFLAGS)
14dnl AC_SUBST(LIBEVENT_LDFLAGS)
15dnl AC_SUBST(LIBEVENT_LIBS)
16dnl
17dnl And (if libevent is found):
18dnl AC_DEFINE(HAVE_LIBEVENT)
19dnl
20dnl It also leaves the shell variables "success" and "ax_have_libevent"
21dnl set to "yes" or "no".
22dnl
23dnl NOTE: This macro does not currently work for cross-compiling,
24dnl but it can be easily modified to allow it. (grep "cross").
25dnl
26dnl @category InstalledPackages
27dnl @category C
David Reiss79ae0f82007-09-17 21:15:47 +000028dnl @version 2007-09-12
29dnl @license AllPermissive
David Reissf82aee52009-03-30 22:52:29 +000030dnl
31dnl Copyright (C) 2009 David Reiss
32dnl Copying and distribution of this file, with or without modification,
33dnl are permitted in any medium without royalty provided the copyright
34dnl notice and this notice are preserved.
David Reiss79ae0f82007-09-17 21:15:47 +000035
36dnl Input: ax_libevent_path, WANT_LIBEVENT_VERSION
37dnl Output: success=yes/no
38AC_DEFUN([AX_LIB_EVENT_DO_CHECK],
39 [
40 # Save our flags.
41 CPPFLAGS_SAVED="$CPPFLAGS"
42 LDFLAGS_SAVED="$LDFLAGS"
43 LIBS_SAVED="$LIBS"
44 LD_LIBRARY_PATH_SAVED="$LD_LIBRARY_PATH"
45
46 # Set our flags if we are checking a specific directory.
47 if test -n "$ax_libevent_path" ; then
48 LIBEVENT_CPPFLAGS="-I$ax_libevent_path/include"
49 LIBEVENT_LDFLAGS="-L$ax_libevent_path/lib"
50 LD_LIBRARY_PATH="$ax_libevent_path/lib:$LD_LIBRARY_PATH"
51 else
52 LIBEVENT_CPPFLAGS=""
53 LIBEVENT_LDFLAGS=""
54 fi
55
56 # Required flag for libevent.
57 LIBEVENT_LIBS="-levent"
58
59 # Prepare the environment for compilation.
60 CPPFLAGS="$CPPFLAGS $LIBEVENT_CPPFLAGS"
61 LDFLAGS="$LDFLAGS $LIBEVENT_LDFLAGS"
62 LIBS="$LIBS $LIBEVENT_LIBS"
63 export CPPFLAGS
64 export LDFLAGS
65 export LIBS
66 export LD_LIBRARY_PATH
67
68 success=no
69
70 # Compile, link, and run the program. This checks:
71 # - event.h is available for including.
72 # - event_get_version() is available for linking.
73 # - The event version string is lexicographically greater
74 # than the required version.
75 AC_LANG_PUSH([C])
76 dnl This can be changed to AC_LINK_IFELSE if you are cross-compiling,
77 dnl but then the version cannot be checked.
78 AC_RUN_IFELSE([AC_LANG_PROGRAM([[
79 #include <sys/types.h>
80 #include <event.h>
81 ]], [[
82 const char* lib_version = event_get_version();
83 const char* wnt_version = "$WANT_LIBEVENT_VERSION";
84 for (;;) {
85 /* If we reached the end of the want version. We have it. */
David Reisse657eb42009-07-29 19:07:27 +000086 if (*wnt_version == '\0' || *wnt_version == '-') {
David Reiss79ae0f82007-09-17 21:15:47 +000087 return 0;
88 }
89 /* If the want version continues but the lib version does not, */
90 /* we are missing a letter. We don't have it. */
David Reisse657eb42009-07-29 19:07:27 +000091 if (*lib_version == '\0' || *lib_version == '-') {
92 return 1;
93 }
94 /* In the 1.4 version numbering style, if there are more digits */
95 /* in one version than the other, that one is higher. */
96 int lib_digits;
97 for (lib_digits = 0;
98 lib_version[lib_digits] >= '0' &&
99 lib_version[lib_digits] <= '9';
100 lib_digits++)
101 ;
102 int wnt_digits;
103 for (wnt_digits = 0;
104 wnt_version[wnt_digits] >= '0' &&
105 wnt_version[wnt_digits] <= '9';
106 wnt_digits++)
107 ;
108 if (lib_digits > wnt_digits) {
109 return 0;
110 }
111 if (lib_digits < wnt_digits) {
David Reiss79ae0f82007-09-17 21:15:47 +0000112 return 1;
113 }
114 /* If we have greater than what we want. We have it. */
115 if (*lib_version > *wnt_version) {
116 return 0;
117 }
118 /* If we have less, we don't. */
119 if (*lib_version < *wnt_version) {
120 return 1;
121 }
122 lib_version++;
123 wnt_version++;
124 }
125 return 0;
126 ]])], [
127 success=yes
128 ])
129 AC_LANG_POP([C])
130
131 # Restore flags.
132 CPPFLAGS="$CPPFLAGS_SAVED"
133 LDFLAGS="$LDFLAGS_SAVED"
134 LIBS="$LIBS_SAVED"
135 LD_LIBRARY_PATH="$LD_LIBRARY_PATH_SAVED"
136 ])
137
138
139AC_DEFUN([AX_LIB_EVENT],
140 [
141
142 dnl Allow search path to be overridden on the command line.
143 AC_ARG_WITH([libevent],
144 AS_HELP_STRING([--with-libevent@<:@=DIR@:>@], [use libevent (default is yes) - it is possible to specify an alternate root directory for libevent]),
145 [
146 if test "x$withval" = "xno"; then
147 want_libevent="no"
148 elif test "x$withval" = "xyes"; then
149 want_libevent="yes"
150 ax_libevent_path=""
151 else
152 want_libevent="yes"
153 ax_libevent_path="$withval"
154 fi
155 ],
156 [ want_libevent="yes" ; ax_libevent_path="" ])
157
158
159 if test "$want_libevent" = "yes"; then
160 WANT_LIBEVENT_VERSION=ifelse([$1], ,1.2,$1)
161
162 AC_MSG_CHECKING(for libevent >= $WANT_LIBEVENT_VERSION)
163
164 # Run tests.
165 if test -n "$ax_libevent_path"; then
166 AX_LIB_EVENT_DO_CHECK
167 else
T Jake Luciani9c983c22009-01-16 01:04:27 +0000168 for ax_libevent_path in "" /usr /usr/local /opt /opt/local /opt/libevent "$LIBEVENT_ROOT" ; do
David Reiss79ae0f82007-09-17 21:15:47 +0000169 AX_LIB_EVENT_DO_CHECK
170 if test "$success" = "yes"; then
171 break;
172 fi
173 done
174 fi
175
176 if test "$success" != "yes" ; then
177 AC_MSG_RESULT(no)
178 LIBEVENT_CPPFLAGS=""
179 LIBEVENT_LDFLAGS=""
180 LIBEVENT_LIBS=""
181 else
182 AC_MSG_RESULT(yes)
183 AC_DEFINE(HAVE_LIBEVENT,,[define if libevent is available])
184 fi
185
186 ax_have_libevent="$success"
187
188 AC_SUBST(LIBEVENT_CPPFLAGS)
189 AC_SUBST(LIBEVENT_LDFLAGS)
190 AC_SUBST(LIBEVENT_LIBS)
191 fi
192
193 ])