configure.ac: Check for unbound version >= 1.5
[asterisk/asterisk.git] / autoconf / ast_ext_lib.m4
1 # Helper function to setup variables for a package.
2 # $1 -> the package name. Used in configure.ac and also as a prefix
3 #       for the variables ($1_DIR, $1_INCLUDE, $1_LIB) in makeopts
4 # $3 -> option name, used in --with-$3 or --without-$3 when calling configure.
5 # $2 and $4 are just text describing the package (short and long form)
6
7 # AST_EXT_LIB_SETUP([package], [short description], [configure option name], [long description])
8
9 AC_DEFUN([AST_EXT_LIB_SETUP],
10 [
11     $1_DESCRIP="$2"
12     $1_OPTION="$3"
13     PBX_$1=0
14     AC_ARG_WITH([$3], AS_HELP_STRING([--with-$3=PATH],[use $2 files in PATH$4]),
15     [
16         case ${withval} in
17         n|no)
18         USE_$1=no
19         # -1 is a magic value used by menuselect to know that the package
20         # was disabled, other than 'not found'
21         PBX_$1=-1
22         ;;
23         y|ye|yes)
24         ac_mandatory_list="${ac_mandatory_list} $1"
25         ;;
26         *)
27         $1_DIR="${withval}"
28         ac_mandatory_list="${ac_mandatory_list} $1"
29         ;;
30         esac
31     ])
32     AH_TEMPLATE(m4_bpatsubst([[HAVE_$1]], [(.*)]), [Define to 1 if you have the $2 library.])
33     AC_SUBST([$1_LIB])
34     AC_SUBST([$1_INCLUDE])
35     AC_SUBST([$1_DIR])
36     AC_SUBST([PBX_$1])
37 ])
38
39 # AST_OPTION_ONLY([option name], [option variable], [option description], [default value])
40 AC_DEFUN([AST_OPTION_ONLY],
41 [
42 AC_ARG_WITH([$1], AS_HELP_STRING([--with-$1=PATH], [use $3 in PATH]),
43         [
44         case ${withval} in
45         n|no)
46                 unset $2
47                 ;;
48         *)
49                 if test "x${withval}" = "x"; then
50                         m4_ifval($4, [$2="$4"], [:])
51                 else
52                         $2="${withval}"
53                 fi
54                 ;;
55         esac
56         ],
57         [m4_ifval($4, [$2="$4"], [:])])
58 AC_SUBST($2)
59 ])
60
61 # Setup required dependent package
62 # AST_EXT_LIB_SETUP_DEPENDENT([dependent package symbol name], [dependent package friendly name], [master package symbol name], [master package name])
63
64 AC_DEFUN([AST_EXT_LIB_SETUP_DEPENDENT],
65 [
66 $1_DESCRIP="$2"
67 m4_ifval([$4], [$1_OPTION=$4])
68 m4_ifval([$3], [
69 for i in ${ac_mandatory_list}; do
70    if test "x$3" = "x$i"; then
71       ac_mandatory_list="${ac_mandatory_list} $1"
72       break
73    fi
74 done
75 $1_DIR=${$3_DIR}
76 ])
77 PBX_$1=0
78 AH_TEMPLATE(m4_bpatsubst([[HAVE_$1]], [(.*)]), [Define to 1 if you have the $2 library.])
79 AC_SUBST([$1_LIB])
80 AC_SUBST([$1_INCLUDE])
81 AC_SUBST([$1_DIR])
82 AC_SUBST([PBX_$1])
83 ])
84
85 # Setup optional dependent package
86 # AST_EXT_LIB_SETUP_OPTIONAL([optional package symbol name], [optional package friendly name], [master package symbol name], [master package name])
87
88 AC_DEFUN([AST_EXT_LIB_SETUP_OPTIONAL],
89 [
90 $1_DESCRIP="$2"
91 m4_ifval([$4], [$1_OPTION=$4])
92 m4_ifval([$3], [$1_DIR=${$3_DIR}
93 ])
94 PBX_$1=0
95 AH_TEMPLATE(m4_bpatsubst([[HAVE_$1]], [(.*)]), [Define to 1 if $3 has the $2 feature.])
96 AC_SUBST([$1_LIB])
97 AC_SUBST([$1_INCLUDE])
98 AC_SUBST([$1_DIR])
99 AC_SUBST([PBX_$1])
100 ])
101
102 # Check for existence of a given package ($1), either looking up a function
103 # in a library, or, if no function is supplied, only check for the
104 # existence of the header files.
105
106 # AST_EXT_LIB_CHECK([package], [library], [function], [header],
107 #        [extra libs], [extra cflags], [version])
108 AC_DEFUN([AST_EXT_LIB_CHECK],
109 [
110 if test "x${PBX_$1}" != "x1" -a "${USE_$1}" != "no"; then
111    pbxlibdir=""
112    # if --with-$1=DIR has been specified, use it.
113    if test "x${$1_DIR}" != "x"; then
114       if test -d ${$1_DIR}/lib; then
115          pbxlibdir="-L${$1_DIR}/lib"
116       else
117          pbxlibdir="-L${$1_DIR}"
118       fi
119    fi
120    m4_ifval([$3], [
121       ast_ext_lib_check_save_CFLAGS="${CFLAGS}"
122       CFLAGS="${CFLAGS} $6"
123       AC_CHECK_LIB([$2], [$3], [AST_$1_FOUND=yes], [AST_$1_FOUND=no], [${pbxlibdir} $5])
124       CFLAGS="${ast_ext_lib_check_save_CFLAGS}"
125    ], [
126       # empty lib, assume only headers
127       AST_$1_FOUND=yes
128    ])
129
130    # now check for the header.
131    if test "${AST_$1_FOUND}" = "yes"; then
132       $1_LIB="${pbxlibdir} -l$2 $5"
133       # if --with-$1=DIR has been specified, use it.
134       if test "x${$1_DIR}" != "x"; then
135          $1_INCLUDE="-I${$1_DIR}/include"
136       fi
137       $1_INCLUDE="${$1_INCLUDE} $6"
138       m4_ifval([$4], [
139          # check for the header
140          ast_ext_lib_check_saved_CPPFLAGS="${CPPFLAGS}"
141          CPPFLAGS="${CPPFLAGS} ${$1_INCLUDE}"
142          AC_CHECK_HEADER([$4], [$1_HEADER_FOUND=1], [$1_HEADER_FOUND=0])
143          CPPFLAGS="${ast_ext_lib_check_saved_CPPFLAGS}"
144       ], [
145          # no header, assume found
146          $1_HEADER_FOUND="1"
147       ])
148       if test "x${$1_HEADER_FOUND}" = "x0" ; then
149          $1_LIB=""
150          $1_INCLUDE=""
151       else
152          m4_ifval([$3], [], [
153             # only checking headers -> no library
154             $1_LIB=""
155          ])
156          PBX_$1=1
157          cat >>confdefs.h <<_ACEOF
158 [@%:@define] HAVE_$1 1
159 _ACEOF
160          m4_ifval([$7], [
161          cat >>confdefs.h <<_ACEOF
162 [@%:@define] HAVE_$1_VERSION $7
163 _ACEOF
164             ])
165       fi
166    fi
167 fi
168 m4_ifval([$7], [AH_TEMPLATE(m4_bpatsubst([[HAVE_$1_VERSION]], [(.*)]), [Define to the version of the $2 library.])])
169 ])
170
171 # Check if the previously discovered library can be dynamically linked.
172 #
173 # AST_EXT_LIB_CHECK_SHARED([package], [library], [function], [header],
174 #        [extra libs], [extra cflags], [action-if-true], [action-if-false])
175 AC_DEFUN([AST_EXT_LIB_CHECK_SHARED],
176 [
177 if test "x${PBX_$1}" = "x1"; then
178    ast_ext_lib_check_shared_saved_libs="${LIBS}"
179    ast_ext_lib_check_shared_saved_ldflags="${LDFLAGS}"
180    ast_ext_lib_check_shared_saved_cflags="${CFLAGS}"
181    LIBS="${LIBS} ${$1_LIB} $5"
182    LDFLAGS="${LDFLAGS} -shared -fPIC"
183    CFLAGS="${CFLAGS} ${$1_INCLUDE} $6"
184    AC_MSG_CHECKING(for the ability of -l$2 to be linked in a shared object)
185    AC_LINK_IFELSE(
186    [
187        AC_LANG_PROGRAM(
188            [#include <$4>],
189            [$3();]
190        )
191    ],
192    [
193       AC_MSG_RESULT(yes)
194       $7
195    ],
196    [
197       AC_MSG_RESULT(no)
198       $8
199    ]
200    )
201    CFLAGS="${ast_ext_lib_check_shared_saved_cflags}"
202    LDFLAGS="${ast_ext_lib_check_shared_saved_ldflags}"
203    LIBS="${ast_ext_lib_check_shared_saved_libs}"
204 fi
205 ])
206
207 # Check for existence of a given package ($1), either looking up a function
208 # in a library, or, if no function is supplied, only check for the
209 # existence of the header files.  Then compile, link and run the supplied
210 # code fragment to make the final determination.
211
212 # AST_EXT_LIB_EXTRA_CHECK([package], [library], [function], [header],
213 #        [extra libs], [extra cflags], [AC_LANG_PROGRAM(extra check code...)],
214 #    ["checking for" display string], ["HAVE_package_" extra variable to set])
215 AC_DEFUN([AST_EXT_LIB_EXTRA_CHECK],
216 [
217 if test "x${PBX_$1}" != "x1" -a "${USE_$1}" != "no"; then
218    pbxlibdir=""
219    # if --with-$1=DIR has been specified, use it.
220    if test "x${$1_DIR}" != "x"; then
221       if test -d ${$1_DIR}/lib; then
222          pbxlibdir="-L${$1_DIR}/lib"
223       else
224          pbxlibdir="-L${$1_DIR}"
225       fi
226    fi
227    m4_ifval([$3], [
228       ast_ext_lib_check_save_CFLAGS="${CFLAGS}"
229       CFLAGS="${CFLAGS} $6"
230       AC_CHECK_LIB([$2], [$3], [AST_$1_FOUND=yes], [AST_$1_FOUND=no], [${pbxlibdir} $5])
231       CFLAGS="${ast_ext_lib_check_save_CFLAGS}"
232    ], [
233       # empty lib, assume only headers
234       AST_$1_FOUND=yes
235    ])
236
237    # now check for the header.
238    if test "${AST_$1_FOUND}" = "yes"; then
239       $1_LIB="${pbxlibdir} -l$2 $5"
240       # if --with-$1=DIR has been specified, use it.
241       if test "x${$1_DIR}" != "x"; then
242          $1_INCLUDE="-I${$1_DIR}/include"
243       fi
244       $1_INCLUDE="${$1_INCLUDE} $6"
245       m4_ifval([$4], [
246          # check for the header
247          ast_ext_lib_check_saved_CPPFLAGS="${CPPFLAGS}"
248          CPPFLAGS="${CPPFLAGS} ${$1_INCLUDE}"
249          AC_CHECK_HEADER([$4], [$1_HEADER_FOUND=1], [$1_HEADER_FOUND=0])
250          CPPFLAGS="${ast_ext_lib_check_saved_CPPFLAGS}"
251       ], [
252          # no header, assume found
253          $1_HEADER_FOUND="1"
254       ])
255    fi
256    # Validate the package with the supplied code.
257    if test "x${$1_HEADER_FOUND}" = "x1" ; then
258       AC_MSG_CHECKING(for $8)
259       ast_ext_lib_check_saved_CPPFLAGS="${CPPFLAGS}"
260       CPPFLAGS="${CPPFLAGS} ${$1_INCLUDE}"
261       ast_ext_lib_check_saved_LIBS="${LIBS}"
262       LIBS="${$1_LIB}"
263       AC_LINK_IFELSE(
264          [$7],
265          [
266             if test "x${cross_compiling}" = "xyes" ; then
267                $1_VALIDATED="1"
268                AC_MSG_RESULT([yes (guessed for cross-compile)])
269             else
270                ./conftest$EXEEXT
271                if test $? -eq 0 ; then
272                   $1_VALIDATED="1"
273                   AC_MSG_RESULT(yes)
274                else
275                   AC_MSG_RESULT(no)
276                fi
277             fi
278          ],
279          [
280             AC_MSG_RESULT(no)
281          ]
282       )
283       CPPFLAGS="${ast_ext_lib_check_saved_CPPFLAGS}"
284       LIBS="${ast_ext_lib_check_saved_LIBS}"
285    fi
286
287    if test "x${$1_VALIDATED}" = "x1" ; then
288       m4_ifval([$3], [], [
289          # only checking headers -> no library
290          $1_LIB=""
291       ])
292       PBX_$1=1
293       cat >>confdefs.h <<_ACEOF
294 [@%:@define] HAVE_$1 1
295 _ACEOF
296       m4_ifval([$9], [
297       cat >>confdefs.h <<_ACEOF
298 [@%:@define] HAVE_$1_$9 1
299 _ACEOF
300             ])
301    else
302       $1_LIB=""
303       $1_INCLUDE=""
304    fi
305 fi
306 m4_ifval([$9], [AH_TEMPLATE(m4_bpatsubst([[HAVE_$1_$9]], [(.*)]), [Define if $8])])
307 ])