1 dnl @synopsis AX_PATH_BDB([MINIMUM-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
3 dnl This macro finds the latest version of Berkeley DB on the system,
4 dnl and ensures that the header file and library versions match. If
5 dnl MINIMUM-VERSION is specified, it will ensure that the library
6 dnl found is at least that version.
8 dnl It determines the name of the library as well as the path to the
9 dnl header file and library. It will check both the default environment
10 dnl as well as the default Berkeley DB install location. When found, it
11 dnl sets BDB_LIBS, BDB_CPPFLAGS, and BDB_LDFLAGS to the necessary values
12 dnl to add to LIBS, CPPFLAGS, and LDFLAGS, as well as setting BDB_VERSION
13 dnl to the version found. HAVE_DB_H is defined also.
15 dnl The option --with-bdb-dir=DIR can be used to specify a specific
16 dnl Berkeley DB installation to use.
18 dnl An example of it's use is:
20 dnl LIBS="$BDB_LIBS $LIBS"
21 dnl LDFLAGS="$BDB_LDFLAGS $LDFLAGS"
22 dnl CPPFLAGS="$CPPFLAGS $BDB_CPPFLAGS"
24 dnl which will locate the latest version of Berkeley DB on the system,
25 dnl and ensure that it is version 3.0 or higher.
27 dnl Details: This macro does not use either AC_CHECK_HEADERS or
28 dnl AC_CHECK_LIB because, first, the functions inside the library are
29 dnl sometimes renamed to contain a version code that is only available
30 dnl from the db.h on the system, and second, because it is common to
31 dnl have multiple db.h and libdb files on a system it is important to
32 dnl make sure the ones being used correspond to the same version.
33 dnl Additionally, there are many different possible names for libdb
34 dnl when installed by an OS distribution, and these need to be checked
35 dnl if db.h does not correspond to libdb.
37 dnl When cross compiling, only header versions are verified since it
38 dnl would be difficult to check the library version. Additionally
39 dnl the default Berkeley DB installation locations /usr/local/BerkeleyDB*
40 dnl are not searched for higher versions of the library.
42 dnl The format for the list of library names to search came from the
43 dnl Cyrus IMAP distribution, although they are generated dynamically
44 dnl here, and only for the version found in db.h.
46 dnl The macro AX_COMPARE_VERSION is required to use this macro, and
47 dnl should be available from the Autoconf Macro Archive.
49 dnl The author would like to acknowledge the generous and valuable feedback
50 dnl from Guido Draheim, without which this macro would be far less robust,
51 dnl and have poor and inconsistent cross compilation support.
53 dnl @version $Id: ax_path_bdb.m4,v 1.1 2005/01/06 20:24:52 shadow Exp $
54 dnl @author Tim Toolan <toolan@ele.uri.edu>
57 dnl #########################################################################
58 AC_DEFUN([AX_PATH_BDB], [
59 dnl # Used to indicate success or failure of this function.
62 # Add --with-bdb-dir option to configure.
63 AC_ARG_WITH([bdb-dir],
64 [AC_HELP_STRING([--with-bdb-dir=DIR],
65 [Berkeley DB installation directory])])
67 # Check if --with-bdb-dir was specified.
68 if test "x$with_bdb_dir" = "x" ; then
69 # No option specified, so just search the system.
70 AX_PATH_BDB_NO_OPTIONS([$1], [HIGHEST], [
74 # Set --with-bdb-dir option.
75 ax_path_bdb_INC="$with_bdb_dir/include"
76 ax_path_bdb_LIB="$with_bdb_dir/lib"
78 dnl # Save previous environment, and modify with new stuff.
79 ax_path_bdb_save_CPPFLAGS="$CPPFLAGS"
80 CPPFLAGS="-I$ax_path_bdb_INC $CPPFLAGS"
82 ax_path_bdb_save_LDFLAGS=$LDFLAGS
83 LDFLAGS="-L$ax_path_bdb_LIB $LDFLAGS"
85 # Check for specific header file db.h
86 AC_MSG_CHECKING([db.h presence in $ax_path_bdb_INC])
87 if test -f "$ax_path_bdb_INC/db.h" ; then
90 AX_PATH_BDB_NO_OPTIONS([$1], [ENVONLY], [
92 BDB_CPPFLAGS="-I$ax_path_bdb_INC"
93 BDB_LDFLAGS="-L$ax_path_bdb_LIB"
97 AC_MSG_NOTICE([no usable Berkeley DB not found])
100 dnl # Restore the environment.
101 CPPFLAGS="$ax_path_bdb_save_CPPFLAGS"
102 LDFLAGS="$ax_path_bdb_save_LDFLAGS"
106 dnl # Execute ACTION-IF-FOUND / ACTION-IF-NOT-FOUND.
107 if test "$ax_path_bdb_ok" = "yes" ; then
108 m4_ifvaln([$2],[$2],[:])dnl
109 m4_ifvaln([$3],[else $3])dnl
114 dnl #########################################################################
115 dnl Check for berkeley DB of at least MINIMUM-VERSION on system.
117 dnl The OPTION argument determines how the checks occur, and can be one of:
119 dnl HIGHEST - Check both the environment and the default installation
120 dnl directories for Berkeley DB and choose the version that
121 dnl is highest. (default)
122 dnl ENVFIRST - Check the environment first, and if no satisfactory
123 dnl library is found there check the default installation
124 dnl directories for Berkeley DB which is /usr/local/BerkeleyDB*
125 dnl ENVONLY - Check the current environment only.
127 dnl Requires AX_PATH_BDB_PATH_GET_VERSION, AX_PATH_BDB_PATH_FIND_HIGHEST,
128 dnl AX_PATH_BDB_ENV_CONFIRM_LIB, AX_PATH_BDB_ENV_GET_VERSION, and
129 dnl AX_COMPARE_VERSION macros.
131 dnl Result: sets ax_path_bdb_no_options_ok to yes or no
132 dnl sets BDB_LIBS, BDB_CPPFLAGS, BDB_LDFLAGS, BDB_VERSION
134 dnl AX_PATH_BDB_NO_OPTIONS([MINIMUM-VERSION], [OPTION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
135 AC_DEFUN([AX_PATH_BDB_NO_OPTIONS], [
136 dnl # Used to indicate success or failure of this function.
137 ax_path_bdb_no_options_ok=no
139 # Values to add to environment to use Berkeley DB.
145 # Check cross compilation here.
146 if test "x$cross_compiling" = "xyes" ; then
147 # If cross compiling, can't use AC_RUN_IFELSE so do these tests.
148 # The AC_PREPROC_IFELSE confirms that db.h is preprocessable,
149 # and extracts the version number from it.
150 AC_MSG_CHECKING([for db.h])
152 AS_VAR_PUSHDEF([HEADER_VERSION],[ax_path_bdb_no_options_HEADER_VERSION])dnl
157 AX_PATH_BDB_STUFF DB_VERSION_MAJOR,DB_VERSION_MINOR,DB_VERSION_PATCH
160 # Extract version from preprocessor output.
161 HEADER_VERSION=`eval "$ac_cpp conftest.$ac_ext" 2> /dev/null \
162 | grep AX_PATH_BDB_STUFF | sed 's/[[^0-9,]]//g;s/,/./g;1q'`
165 if test "x$HEADER_VERSION" = "x" ; then
168 AC_MSG_RESULT([$HEADER_VERSION])
170 # Check that version is high enough.
171 AX_COMPARE_VERSION([$HEADER_VERSION],[ge],[$1],[
172 # get major and minor version numbers
173 AS_VAR_PUSHDEF([MAJ],[ax_path_bdb_no_options_MAJOR])dnl
174 MAJ=`echo $HEADER_VERSION | sed 's,\..*,,'`
175 AS_VAR_PUSHDEF([MIN],[ax_path_bdb_no_options_MINOR])dnl
176 MIN=`echo $HEADER_VERSION | sed 's,^[[0-9]]*\.,,;s,\.[[0-9]]*$,,'`
179 ax_path_bdb_no_options_save_LIBS="$LIBS"
181 # Check that we can link with the library.
182 AC_SEARCH_LIBS([db_version],
183 [db db-$MAJ.$MIN db$MAJ.$MIN db$MAJ$MIN db-$MAJ db$MAJ],[
184 # Sucessfully found library.
185 ax_path_bdb_no_options_ok=yes
186 BDB_VERSION=$HEADER_VERSION
188 # Extract library from LIBS
189 ax_path_bdb_no_options_LEN=` \
190 echo "x$ax_path_bdb_no_options_save_LIBS" \
191 | awk '{print(length)}'`
192 BDB_LIBS=`echo "x$LIBS " \
193 | sed "s/.\{$ax_path_bdb_no_options_LEN\}\$//;s/^x//;s/ //g"`
197 LIBS="$ax_path_bdb_no_options_save_LIBS"
199 AS_VAR_POPDEF([MAJ])dnl
200 AS_VAR_POPDEF([MIN])dnl
204 AS_VAR_POPDEF([HEADER_VERSION])dnl
206 # Not cross compiling.
207 # Check version of Berkeley DB in the current environment.
208 AX_PATH_BDB_ENV_GET_VERSION([
209 AX_COMPARE_VERSION([$ax_path_bdb_env_get_version_VERSION],[ge],[$1],[
210 # Found acceptable version in current environment.
211 ax_path_bdb_no_options_ok=yes
212 BDB_VERSION="$ax_path_bdb_env_get_version_VERSION"
213 BDB_LIBS="$ax_path_bdb_env_get_version_LIBS"
217 # Determine if we need to search /usr/local/BerkeleyDB*
218 ax_path_bdb_no_options_DONE=no
219 if test "x$2" = "xENVONLY" ; then
220 ax_path_bdb_no_options_DONE=yes
221 elif test "x$2" = "xENVFIRST" ; then
222 ax_path_bdb_no_options_DONE=$ax_path_bdb_no_options_ok
225 if test "$ax_path_bdb_no_options_DONE" = "no" ; then
226 # Check for highest in /usr/local/BerkeleyDB*
227 AX_PATH_BDB_PATH_FIND_HIGHEST([
228 if test "$ax_path_bdb_no_options_ok" = "yes" ; then
229 # If we already have an acceptable version use this if higher.
231 [$ax_path_bdb_path_find_highest_VERSION],[gt],[$BDB_VERSION])
233 # Since we didn't have an acceptable version check if this one is.
235 [$ax_path_bdb_path_find_highest_VERSION],[ge],[$1])
239 dnl # If result from _AX_COMPARE_VERSION is true we want this version.
240 if test "$ax_compare_version" = "true" ; then
241 ax_path_bdb_no_options_ok=yes
243 BDB_CPPFLAGS="-I$ax_path_bdb_path_find_highest_DIR/include"
244 BDB_LDFLAGS="-L$ax_path_bdb_path_find_highest_DIR/lib"
245 BDB_VERSION="$ax_path_bdb_path_find_highest_VERSION"
250 dnl # Execute ACTION-IF-FOUND / ACTION-IF-NOT-FOUND.
251 if test "$ax_path_bdb_no_options_ok" = "yes" ; then
252 AC_MSG_NOTICE([using Berkeley DB version $BDB_VERSION])
253 AC_DEFINE([HAVE_DB_H],[1],
254 [Define to 1 if you have the <db.h> header file.])
255 m4_ifvaln([$3],[$3])dnl
257 AC_MSG_NOTICE([no Berkeley DB version $1 or higher found])
258 m4_ifvaln([$4],[$4])dnl
260 ]) dnl AX_PATH_BDB_NO_OPTIONS
262 dnl #########################################################################
263 dnl Check the default installation directory for Berkeley DB which is
264 dnl of the form /usr/local/BerkeleyDB* for the highest version.
266 dnl Result: sets ax_path_bdb_path_find_highest_ok to yes or no,
267 dnl sets ax_path_bdb_path_find_highest_VERSION to version,
268 dnl sets ax_path_bdb_path_find_highest_DIR to directory.
270 dnl AX_PATH_BDB_PATH_FIND_HIGHEST([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
271 AC_DEFUN([AX_PATH_BDB_PATH_FIND_HIGHEST], [
272 dnl # Used to indicate success or failure of this function.
273 ax_path_bdb_path_find_highest_ok=no
275 AS_VAR_PUSHDEF([VERSION],[ax_path_bdb_path_find_highest_VERSION])dnl
278 ax_path_bdb_path_find_highest_DIR=''
280 # find highest verison in default install directory for Berkeley DB
281 AS_VAR_PUSHDEF([CURDIR],[ax_path_bdb_path_find_highest_CURDIR])dnl
282 AS_VAR_PUSHDEF([CUR_VERSION],[ax_path_bdb_path_get_version_VERSION])dnl
284 for CURDIR in `ls -d /usr/local/BerkeleyDB* 2> /dev/null`
286 AX_PATH_BDB_PATH_GET_VERSION([$CURDIR],[
287 AX_COMPARE_VERSION([$CUR_VERSION],[gt],[$VERSION],[
288 ax_path_bdb_path_find_highest_ok=yes
289 ax_path_bdb_path_find_highest_DIR="$CURDIR"
290 VERSION="$CUR_VERSION"
295 AS_VAR_POPDEF([VERSION])dnl
296 AS_VAR_POPDEF([CUR_VERSION])dnl
297 AS_VAR_POPDEF([CURDIR])dnl
299 dnl # Execute ACTION-IF-FOUND / ACTION-IF-NOT-FOUND.
300 if test "$ax_path_bdb_path_find_highest_ok" = "yes" ; then
301 m4_ifvaln([$1],[$1],[:])dnl
302 m4_ifvaln([$2],[else $2])dnl
305 ]) dnl AX_PATH_BDB_PATH_FIND_HIGHEST
307 dnl #########################################################################
308 dnl Checks for Berkeley DB in specified directory's lib and include
311 dnl Result: sets ax_path_bdb_path_get_version_ok to yes or no,
312 dnl sets ax_path_bdb_path_get_version_VERSION to version.
314 dnl AX_PATH_BDB_PATH_GET_VERSION(BDB-DIR, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
315 AC_DEFUN([AX_PATH_BDB_PATH_GET_VERSION], [
316 dnl # Used to indicate success or failure of this function.
317 ax_path_bdb_path_get_version_ok=no
319 # Indicate status of checking for Berkeley DB header.
320 AC_MSG_CHECKING([in $1/include for db.h])
321 ax_path_bdb_path_get_version_got_header=no
322 test -f "$1/include/db.h" && ax_path_bdb_path_get_version_got_header=yes
323 AC_MSG_RESULT([$ax_path_bdb_path_get_version_got_header])
325 # Indicate status of checking for Berkeley DB library.
326 AC_MSG_CHECKING([in $1/lib for library -ldb])
328 ax_path_bdb_path_get_version_VERSION=''
330 if test -d "$1/include" && test -d "$1/lib" &&
331 test "$ax_path_bdb_path_get_version_got_header" = "yes" ; then
332 dnl # save and modify environment
333 ax_path_bdb_path_get_version_save_CPPFLAGS="$CPPFLAGS"
334 CPPFLAGS="-I$1/include $CPPFLAGS"
336 ax_path_bdb_path_get_version_save_LIBS="$LIBS"
339 ax_path_bdb_path_get_version_save_LDFLAGS="$LDFLAGS"
340 LDFLAGS="-L$1/lib $LDFLAGS"
342 # Compile and run a program that compares the version defined in
343 # the header file with a version defined in the library function
349 int main(int argc,char **argv)
351 int major,minor,patch;
352 db_version(&major,&minor,&patch);
354 printf("%d.%d.%d\n",DB_VERSION_MAJOR,DB_VERSION_MINOR,DB_VERSION_PATCH);
355 if (DB_VERSION_MAJOR == major && DB_VERSION_MINOR == minor &&
356 DB_VERSION_PATCH == patch)
363 # Program compiled and ran, so get version by adding argument.
364 ax_path_bdb_path_get_version_VERSION=`./conftest$ac_exeext x`
365 ax_path_bdb_path_get_version_ok=yes
368 dnl # restore environment
369 CPPFLAGS="$ax_path_bdb_path_get_version_save_CPPFLAGS"
370 LIBS="$ax_path_bdb_path_get_version_save_LIBS"
371 LDFLAGS="$ax_path_bdb_path_get_version_save_LDFLAGS"
374 dnl # Finally, execute ACTION-IF-FOUND / ACTION-IF-NOT-FOUND.
375 if test "$ax_path_bdb_path_get_version_ok" = "yes" ; then
376 AC_MSG_RESULT([$ax_path_bdb_path_get_version_VERSION])
377 m4_ifvaln([$2],[$2])dnl
380 m4_ifvaln([$3],[$3])dnl
382 ]) dnl AX_PATH_BDB_PATH_GET_VERSION
384 #############################################################################
385 dnl Checks if version of library and header match specified version.
386 dnl Only meant to be used by AX_PATH_BDB_ENV_GET_VERSION macro.
388 dnl Requires AX_COMPARE_VERSION macro.
390 dnl Result: sets ax_path_bdb_env_confirm_lib_ok to yes or no.
392 dnl AX_PATH_BDB_ENV_CONFIRM_LIB(VERSION, [LIBNAME])
393 AC_DEFUN([AX_PATH_BDB_ENV_CONFIRM_LIB], [
394 dnl # Used to indicate success or failure of this function.
395 ax_path_bdb_env_confirm_lib_ok=no
397 dnl # save and modify environment to link with library LIBNAME
398 ax_path_bdb_env_confirm_lib_save_LIBS="$LIBS"
401 # Compile and run a program that compares the version defined in
402 # the header file with a version defined in the library function
408 int main(int argc,char **argv)
410 int major,minor,patch;
411 db_version(&major,&minor,&patch);
413 printf("%d.%d.%d\n",DB_VERSION_MAJOR,DB_VERSION_MINOR,DB_VERSION_PATCH);
414 if (DB_VERSION_MAJOR == major && DB_VERSION_MINOR == minor &&
415 DB_VERSION_PATCH == patch)
422 # Program compiled and ran, so get version by giving an argument,
423 # which will tell the program to print the output.
424 ax_path_bdb_env_confirm_lib_VERSION=`./conftest$ac_exeext x`
426 # If the versions all match up, indicate success.
427 AX_COMPARE_VERSION([$ax_path_bdb_env_confirm_lib_VERSION],[eq],[$1],[
428 ax_path_bdb_env_confirm_lib_ok=yes
432 dnl # restore environment
433 LIBS="$ax_path_bdb_env_confirm_lib_save_LIBS"
435 ]) dnl AX_PATH_BDB_ENV_CONFIRM_LIB
437 #############################################################################
438 dnl Finds the version and library name for Berkeley DB in the
439 dnl current environment. Tries many different names for library.
441 dnl Requires AX_PATH_BDB_ENV_CONFIRM_LIB macro.
443 dnl Result: set ax_path_bdb_env_get_version_ok to yes or no,
444 dnl set ax_path_bdb_env_get_version_VERSION to the version found,
445 dnl and ax_path_bdb_env_get_version_LIBNAME to the library name.
447 dnl AX_PATH_BDB_ENV_GET_VERSION([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
448 AC_DEFUN([AX_PATH_BDB_ENV_GET_VERSION], [
449 dnl # Used to indicate success or failure of this function.
450 ax_path_bdb_env_get_version_ok=no
452 ax_path_bdb_env_get_version_VERSION=''
453 ax_path_bdb_env_get_version_LIBS=''
455 AS_VAR_PUSHDEF([HEADER_VERSION],[ax_path_bdb_env_get_version_HEADER_VERSION])dnl
456 AS_VAR_PUSHDEF([TEST_LIBNAME],[ax_path_bdb_env_get_version_TEST_LIBNAME])dnl
458 # Indicate status of checking for Berkeley DB library.
459 AC_MSG_CHECKING([for db.h])
461 # Compile and run a program that determines the Berkeley DB version
462 # in the header file db.h.
468 int main(int argc,char **argv)
471 printf("%d.%d.%d\n",DB_VERSION_MAJOR,DB_VERSION_MINOR,DB_VERSION_PATCH);
476 # Program compiled and ran, so get version by adding an argument.
477 HEADER_VERSION=`./conftest$ac_exeext x`
478 AC_MSG_RESULT([$HEADER_VERSION])
479 ],[AC_MSG_RESULT([no])],[AC_MSG_RESULT([no])])
481 # Have header version, so try to find corresponding library.
482 # Looks for library names in the order:
483 # nothing, db, db-X.Y, dbX.Y, dbXY, db-X, dbX
484 # and stops when it finds the first one that matches the version
485 # of the header file.
486 if test "x$HEADER_VERSION" != "x" ; then
487 AC_MSG_CHECKING([for library containing Berkeley DB $HEADER_VERSION])
489 AS_VAR_PUSHDEF([MAJOR],[ax_path_bdb_env_get_version_MAJOR])dnl
490 AS_VAR_PUSHDEF([MINOR],[ax_path_bdb_env_get_version_MINOR])dnl
492 # get major and minor version numbers
493 MAJOR=`echo $HEADER_VERSION | sed 's,\..*,,'`
494 MINOR=`echo $HEADER_VERSION | sed 's,^[[0-9]]*\.,,;s,\.[[0-9]]*$,,'`
496 # see if it is already specified in LIBS
498 AX_PATH_BDB_ENV_CONFIRM_LIB([$HEADER_VERSION], [$TEST_LIBNAME])
500 if test "$ax_path_bdb_env_confirm_lib_ok" = "no" ; then
503 AX_PATH_BDB_ENV_CONFIRM_LIB([$HEADER_VERSION], [$TEST_LIBNAME])
506 if test "$ax_path_bdb_env_confirm_lib_ok" = "no" ; then
507 # try format "db-X.Y"
508 TEST_LIBNAME="-ldb-${MAJOR}.$MINOR"
509 AX_PATH_BDB_ENV_CONFIRM_LIB([$HEADER_VERSION], [$TEST_LIBNAME])
512 if test "$ax_path_bdb_env_confirm_lib_ok" = "no" ; then
514 TEST_LIBNAME="-ldb${MAJOR}.$MINOR"
515 AX_PATH_BDB_ENV_CONFIRM_LIB([$HEADER_VERSION], [$TEST_LIBNAME])
518 if test "$ax_path_bdb_env_confirm_lib_ok" = "no" ; then
520 TEST_LIBNAME="-ldb$MAJOR$MINOR"
521 AX_PATH_BDB_ENV_CONFIRM_LIB([$HEADER_VERSION], [$TEST_LIBNAME])
524 if test "$ax_path_bdb_env_confirm_lib_ok" = "no" ; then
526 TEST_LIBNAME="-ldb-$MAJOR"
527 AX_PATH_BDB_ENV_CONFIRM_LIB([$HEADER_VERSION], [$TEST_LIBNAME])
530 if test "$ax_path_bdb_env_confirm_lib_ok" = "no" ; then
532 TEST_LIBNAME="-ldb$MAJOR"
533 AX_PATH_BDB_ENV_CONFIRM_LIB([$HEADER_VERSION], [$TEST_LIBNAME])
536 dnl # Found a valid library.
537 if test "$ax_path_bdb_env_confirm_lib_ok" = "yes" ; then
538 if test "x$TEST_LIBNAME" = "x" ; then
539 AC_MSG_RESULT([none required])
541 AC_MSG_RESULT([$TEST_LIBNAME])
543 ax_path_bdb_env_get_version_VERSION="$HEADER_VERSION"
544 ax_path_bdb_env_get_version_LIBS="$TEST_LIBNAME"
545 ax_path_bdb_env_get_version_ok=yes
550 AS_VAR_POPDEF([MAJOR])dnl
551 AS_VAR_POPDEF([MINOR])dnl
554 AS_VAR_POPDEF([HEADER_VERSION])dnl
555 AS_VAR_POPDEF([TEST_LIBNAME])dnl
557 dnl # Execute ACTION-IF-FOUND / ACTION-IF-NOT-FOUND.
558 if test "$ax_path_bdb_env_confirm_lib_ok" = "yes" ; then
559 m4_ifvaln([$1],[$1],[:])dnl
560 m4_ifvaln([$2],[else $2])dnl
563 ]) dnl BDB_ENV_GET_VERSION
565 #############################################################################