1dnl Find the compiler and linker flags for the Berkeley DB library.
2dnl $Id: bdb.m4 10306 2018-12-02 14:26:13Z iulius $
3dnl
4dnl Finds the compiler and linker flags for linking with the Berkeley DB
5dnl library.  Provides the --with-bdb, --with-bdb-lib, and --with-bdb-include
6dnl configure options to specify non-standard paths to the Berkeley DB
7dnl library.
8dnl
9dnl Provides the macro INN_LIB_BDB and sets the substitution variables
10dnl BDB_CPPFLAGS, BDB_LDFLAGS, and BDB_LIBS.  Also provides INN_LIB_BDB_SWITCH
11dnl to set CPPFLAGS, LDFLAGS, and LIBS to include the Berkeley DB library,
12dnl saving the current values first, and INN_LIB_BDB_RESTORE to restore those
13dnl settings to before the last INN_LIB_BDB_SWITCH.  Defines HAVE_BDB and sets
14dnl inn_use_BDB to true if the library is found.
15dnl
16dnl Provides the INN_LIB_BDB_OPTIONAL macro, which should be used if Berkeley
17dnl DB support is optional.  This macro will still always set the substitution
18dnl variables, but they'll be empty unless --with-bdb is given.  Defines
19dnl HAVE_BDB and sets inn_use_BDB to true if the Berkeley DB library is found.
20dnl
21dnl This file also provides INN_LIB_BDB_NDBM, which checks whether the
22dnl Berkeley DB library has ndbm support.  It then defines HAVE_BDB_NDBM and
23dnl sets inn_cv_lib_bdb_ndbm to yes if ndbm compatibility layer for Berkely DB
24dnl is available.  Either INN_LIB_BDB or INN_LIB_BDB_OPTIONAL must be called
25dnl before calling this macro.
26dnl
27dnl Depends on the lib-helper.m4 framework.
28dnl
29dnl The canonical version of this file is maintained in the rra-c-util
30dnl package, available at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
31dnl
32dnl Written by Russ Allbery <eagle@eyrie.org>
33dnl Copyright 2018 Russ Allbery <eagle@eyrie.org>
34dnl Copyright 2013
35dnl     The Board of Trustees of the Leland Stanford Junior University
36dnl
37dnl This file is free software; the authors give unlimited permission to copy
38dnl and/or distribute it, with or without modifications, as long as this
39dnl notice is preserved.
40dnl
41dnl SPDX-License-Identifier: FSFULLR
42
43dnl Save the current CPPFLAGS, LDFLAGS, and LIBS settings and switch to
44dnl versions that include the Berkeley DB flags.  Used as a wrapper, with
45dnl INN_LIB_BDB_RESTORE, around tests.
46AC_DEFUN([INN_LIB_BDB_SWITCH], [INN_LIB_HELPER_SWITCH([BDB])])
47
48dnl Restore CPPFLAGS, LDFLAGS, and LIBS to their previous values (before
49dnl INN_LIB_BDB_SWITCH was called).
50AC_DEFUN([INN_LIB_BDB_RESTORE], [INN_LIB_HELPER_RESTORE([BDB])])
51
52dnl Checks if the Berkeley DB library is present.  The single argument,
53dnl if "true", says to fail if the Berkeley DB library could not be found.
54AC_DEFUN([_INN_LIB_BDB_INTERNAL],
55[INN_LIB_HELPER_PATHS([BDB])
56 INN_LIB_BDB_SWITCH
57 AC_CHECK_HEADER([db.h],
58    [AC_CHECK_LIB([db], [db_create],
59        [BDB_LIBS="-ldb"],
60        [AS_IF([test x"$1" = xtrue],
61            [AC_MSG_ERROR([cannot find usable Berkeley DB library])])])],
62    [AS_IF([test x"$1" = xtrue],
63        [AC_MSG_ERROR([cannot find usable Berkeley DB header])])])
64 INN_LIB_BDB_RESTORE])
65
66dnl The main macro for packages with mandatory Berkeley DB support.
67AC_DEFUN([INN_LIB_BDB],
68[INN_LIB_HELPER_VAR_INIT([BDB])
69 INN_LIB_HELPER_WITH([bdb], [Berkeley DB], [BDB])
70 _INN_LIB_BDB_INTERNAL([true])
71 inn_use_BDB=true
72 AC_DEFINE([HAVE_BDB], 1, [Define if libdb is available.])])
73
74dnl The main macro for packages with optional Berkeley DB support.
75AC_DEFUN([INN_LIB_BDB_OPTIONAL],
76[INN_LIB_HELPER_VAR_INIT([BDB])
77 INN_LIB_HELPER_WITH_OPTIONAL([bdb], [Berkeley DB], [BDB])
78 AS_IF([test x"$inn_use_BDB" != xfalse],
79    [AS_IF([test x"$inn_use_BDB" = xtrue],
80        [_INN_LIB_BDB_INTERNAL([true])],
81        [_INN_LIB_BDB_INTERNAL([false])])])
82 AS_IF([test x"$BDB_LIBS" = x],
83    [INN_LIB_HELPER_VAR_CLEAR([BDB])],
84    [inn_use_BDB=true
85     AC_DEFINE([HAVE_BDB], 1, [Define if libdb is available.])])])
86
87dnl Source used by INN_LIB_BDB_NDBM.
88AC_DEFUN([_INN_LIB_BDB_NDBM_SOURCE], [[
89#include <stdio.h>
90#define DB_DBM_HSEARCH 1
91#include <db.h>
92
93int
94main(void)
95{
96    DBM *database;
97    database = dbm_open("test", 0, 0600);
98    dbm_close(database);
99    return 0;
100}
101]])
102
103dnl Check whether Berkeley DB was compiled with ndbm compatibility layer.  If
104dnl so, set HAVE_BDB_NDBM.  Either INN_LIB_BDB or INN_LIB_BDB_OPTIONAL should
105dnl be called before calling this macro.
106AC_DEFUN([INN_LIB_BDB_NDBM],
107[INN_LIB_BDB_SWITCH
108 AC_CACHE_CHECK([for working ndbm compatibility layer with Berkeley DB],
109    [inn_cv_lib_bdb_ndbm],
110    [AC_LINK_IFELSE([AC_LANG_SOURCE([_INN_LIB_BDB_NDBM_SOURCE])],
111        [inn_cv_lib_bdb_ndbm=yes],
112        [inn_cv_lib_bdb_ndbm=no])])
113 AS_IF([test x"$inn_cv_lib_bdb_ndbm" = xyes],
114    [AC_DEFINE([HAVE_BDB_NDBM], 1,
115        [Define if the Berkeley DB ndbm compatibility layer is available.])])
116 INN_LIB_BDB_RESTORE])
117