Remove generated files
[framework/connectivity/libgphoto2.git] / libgphoto2_port / m4 / gp-check-library.m4
1 dnl @synopsis GP_CHECK_LIBRARY([VARNAMEPART],[libname],[VERSION-REQUIREMENT],
2 dnl                            [headername],[functionname],
3 dnl                            [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND],
4 dnl                            [OPTIONAL-REQUIRED-ETC],[WHERE-TO-GET-IT])
5 dnl
6 dnl Checks for the presence of a certain library.
7 dnl
8 dnl Parameters:
9 dnl
10 dnl    VARNAMEPART            partial variable name for variable definitions
11 dnl    libname                name of library
12 dnl    VERSION-REQUIREMENT    check for the version using pkg-config.
13 dnl                           default: []
14 dnl    headername             name of header file
15 dnl                           default: []
16 dnl    functionname           name of function name in library
17 dnl                           default: []
18 dnl    ACTION-IF-FOUND        shell action to execute if found
19 dnl                           default: []
20 dnl    ACTION-IF-NOT-FOUND    shell action to execute if not found
21 dnl                           default: []
22 dnl    OPTIONAL-REQUIRED-ETC  one of "mandatory", "default-on", "default-off"
23 dnl                                  "disable-explicitly"
24 dnl                           default: [mandatory]
25 dnl    WHERE-TO-GET-IT        place where to find the library, e.g. a URL
26 dnl                           default: []
27 dnl
28 dnl What the ACTION-IFs can do:
29 dnl
30 dnl   * change the variable have_[$1] to "yes" or "no" and thus change
31 dnl     the outcome of the test
32 dnl   * execute additional checks to define more specific variables, e.g.
33 dnl     for different API versions
34 dnl
35 dnl What the OPTIONAL-REQUIRED-ETC options mean:
36 dnl
37 dnl   mandatory           Absolute requirement, cannot be disabled.
38 dnl   default-on          If found, it is used. If not found, it is not used.
39 dnl   default-off         In case of --with-libfoo, detect it. Without
40 dnl                       --with-libfoo, do not look for and use it.
41 dnl   disable-explicitly  Required by default, but can be disabled by
42 dnl                       explicitly giving --without-libfoo.
43 dnl
44 dnl These results have happened after calling GP_CHECK_LIBRARY:
45 dnl
46 dnl    AM_CONDITIONAL([HAVE_VARPREFIX],[ if found ])
47 dnl    AM_SUBST([have_VARPREFIX], [ "yes" if found, "no" if not found ])
48 dnl    AM_SUBST([VARPREFIX_CFLAGS],[ -I, -D and stuff ])
49 dnl    AM_SUBST([VARPREFIX_LIBS], [ /path/to/libname.la -L/path -lfoo ])
50 dnl
51 dnl Parameters to ./configure which influence the GP_CHECK_LIBRARY results:
52 dnl
53 dnl   * VARNAMEPART_LIBS=/foobar/arm-palmos/lib/libname.la
54 dnl     VARNAMEPART_CFLAGS=-I/foobar/include
55 dnl   * --without-libfoo
56 dnl   * --with-libfoo=/usr/local
57 dnl   * --with-libfoo-include-dir=/foobar/include
58 dnl   * --with-libfoo-lib=/foobar/arm-palmos/lib
59 dnl   * --with-libfoo=autodetect
60 dnl
61 dnl Examples:
62 dnl    GP_CHECK_LIBRARY([LIBEXIF], [libexif])dnl
63 dnl    GP_CHECK_LIBRARY([LIBEXIF], [libexif-gtk], [>= 0.3.3])dnl
64 dnl                                  note the space! ^
65 dnl
66 dnl Possible enhancements:
67 dnl
68 dnl   * Derive VAR_PREFIX directly from libname
69 dnl     This will change the calling conventions, so be aware of that.
70 dnl   * Give names of a header file and function name and to a test
71 dnl     compilation.
72 dnl
73 AC_DEFUN([_GP_CHECK_LIBRARY_SOEXT],[dnl
74 AC_MSG_CHECKING([for dynamic library extension])
75 soext=""
76 case "$host" in
77         *linux*)        soext=".so" ;;
78         *sunos*)        soext=".so" ;;
79         *solaris*)      soext=".so" ;;
80         *bsd*)          soext=".so" ;;
81         *darwin*)       soext=".dylib" ;;
82         *w32*)          soext=".dll" ;;
83 esac
84 if test "x$soext" = "x"; then
85         soext=".so"
86         AC_MSG_RESULT([${soext}])
87         AC_MSG_WARN([
88 Host system "${host}" not recognized, defaulting to "${soext}".
89 ])
90 else
91         AC_MSG_RESULT([${soext}])
92 fi
93 ])dnl
94 dnl
95 AC_DEFUN([_GP_CHECK_LIBRARY],[
96 # ----------------------------------------------------------------------
97 # [GP_CHECK_LIBRARY]([$1],[$2],[$3],
98 #                    [$4],[$5],
99 #                    [...],[...],[$8])
100 m4_ifval([$9],[dnl
101 # $9
102 ])dnl
103 # ----------------------------------------------------------------------
104 dnl
105 AC_REQUIRE([GP_CONFIG_MSG])dnl
106 AC_REQUIRE([GP_PKG_CONFIG])dnl
107 AC_REQUIRE([_GP_CHECK_LIBRARY_SOEXT])dnl
108 # Use _CFLAGS and _LIBS given to configure.
109 # This makes it possible to set these vars in a configure script
110 # and AC_CONFIG_SUBDIRS this configure.
111 AC_ARG_VAR([$1][_CFLAGS], [CFLAGS for compiling with ][$2])dnl
112 AC_ARG_VAR([$1][_LIBS],   [LIBS to add for linking against ][$2])dnl
113 dnl
114 AC_MSG_CHECKING([for ][$2][ to use])
115 m4_ifval([$3],[REQUIREMENTS_FOR_][$1][="][$2][ $3]["],
116               [REQUIREMENTS_FOR_][$1][="][$2]["])
117 userdef_[$1]=no
118 have_[$1]=no
119 if test "x${[$1][_LIBS]}" = "x" && test "x${[$1][_CFLAGS]}" = "x"; then
120         # define --with/--without argument
121         m4_if([$8], [default-off],
122                 [m4_pushdef([gp_lib_arg],[--without-][$2])dnl
123                         try_[$1]=no
124                 ],
125                 [m4_pushdef([gp_lib_arg],[--with-][$2])dnl
126                         try_[$1]=auto
127                 ])dnl
128         AC_ARG_WITH([$2],[AS_HELP_STRING([gp_lib_arg][=PREFIX],[where to find ][$2][, "no" or "auto"])],[try_][$1][="$withval"])
129         if test "x${[try_][$1]}" = "xno"; then
130               [REQUIREMENTS_FOR_][$1][=]
131         fi
132         if test "x${[try_][$1]}" = "xauto"; then [try_][$1]=autodetect; fi
133         AC_MSG_RESULT([${try_][$1][}])
134         m4_popdef([gp_lib_arg])dnl
135         if test "x${[try_][$1]}" = "xautodetect"; then
136                 # OK, we have to autodetect.
137                 # We start autodetection with the cleanest known method: pkg-config
138                 if test "x${[have_][$1]}" = "xno"; then
139                         # we need that line break after the PKG_CHECK_MODULES
140                         m4_ifval([$3],
141                                 [PKG_CHECK_MODULES([$1],[$2][ $3],[have_][$1][=yes],[:])],
142                                 [PKG_CHECK_MODULES([$1],[$2],     [have_][$1][=yes],[:])]
143                         )
144                 fi
145                 # If pkg-config didn't find anything, try the libfoo-config program
146                 # certain known libraries ship with.
147                 if test "x${[have_][$1]}" = "xno"; then
148                         AC_MSG_CHECKING([$2][ config program])
149                         m4_pushdef([gp_lib_config],[m4_if([$2],[libusb],[libusb-config],
150                                 [$2],[libgphoto2],[gphoto2-config],
151                                 [$2],[libgphoto2_port],[gphoto2-port-config],
152                                 [none])])dnl
153                         AC_MSG_RESULT([gp_lib_config])
154                         AC_PATH_PROG([$1][_CONFIG_PROG],[gp_lib_config])
155                         if test -n "${[$1][_CONFIG_PROG]}" &&
156                                 test "${[$1][_CONFIG_PROG]}" != "none"; then
157                                 m4_ifval([$3],[
158                                 AC_MSG_CHECKING([for ][$2][ version according to ][gp_lib_config])
159                                 m4_pushdef([gp_lib_compop],[regexp([$3], [\(>=\|>\|<\|<=\|=\)[ \t]*.*], [\1])])dnl comparison operator
160                                 m4_if(  gp_lib_compop,[>=],[_][$1][_COMPN="-lt"],
161                                         gp_lib_compop,[>], [_][$1][_COMPN="-le"],
162                                         gp_lib_compop,[<], [_][$1][_COMPN="-ge"],
163                                         gp_lib_compop,[<=],[_][$1][_COMPN="-gt"],
164                                         gp_lib_compop,[=], [_][$1][_COMPN="-ne"],
165                                         [m4_errprint(__file__:__line__:[ Error:
166 Illegal version comparison operator: `gp_lib_compop'
167 It must be one of ">=", ">", "<", "<=", "=".
168 ])m4_exit(1)])
169                                 m4_popdef([gp_lib_compop])dnl
170                                 # split requested version number using m4 regexps
171                                 _[$1]_REQ_1="regexp([$3], [\(>=\|>\|<\|<=\|=\)[ \t]*\([0-9]+\).*],                           [\2])"
172                                 _[$1]_REQ_2="regexp([$3], [\(>=\|>\|<\|<=\|=\)[ \t]*\([0-9]+\)\.\([0-9]+\).*],               [\3])"
173                                 _[$1]_REQ_3="regexp([$3], [\(>=\|>\|<\|<=\|=\)[ \t]*\([0-9]+\)\.\([0-9]+\)\.\([0-9]+\).*],   [\4])"
174                                 _[$1]_REQ_4="regexp([$3], [\(>=\|>\|<\|<=\|=\)[ \t]*\([0-9]+\)\.\([0-9]+\)\.\([0-9]+\)\(.*\)], [\5])"
175                                 # split installed version number via shell and sed
176                                 _[$1]_VERSION="$("${[$1][_CONFIG_PROG]}" --version | sed 's/^.* //')"
177                                 _[$1]_VER_1="$(echo "${_[$1]_VERSION}" | sed 's/\([[0-9]]*\).*/\1/g')"
178                                 _[$1]_VER_2="$(echo "${_[$1]_VERSION}" | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\2/g')"
179                                 _[$1]_VER_3="$(echo "${_[$1]_VERSION}" | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\3/g')"
180                                 _[$1]_VER_4="$(echo "${_[$1]_VERSION}" | sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)\(.*\)/\4/g')"
181                                 AC_MSG_RESULT([${_][$1][_VERSION}])
182                                 _tmp=false
183                                 if   test "${_[$1]_VER_1}" "${_[$1]_COMPN}" "${_[$1]_REQ_1}"; then _tmp=true;
184                                 elif test "${_[$1]_VER_2}" "${_[$1]_COMPN}" "${_[$1]_REQ_2}"; then _tmp=true;
185                                 elif test "${_[$1]_VER_3}" "${_[$1]_COMPN}" "${_[$1]_REQ_3}"; then _tmp=true;
186                                 elif test "x${_[$1]_VER_4}" = "x" && test "x${_[$1]_REQ_4}" != "x"; then _tmp=true;
187                                 elif test "${_[$1]_VER_4}" "${_[$1]_COMPN}" "${_[$1]_REQ_4}"; then _tmp=true;
188                                 fi
189                                 AC_MSG_CHECKING([if ][$2][ version is matching requirement ][$3])
190                                 if "${_tmp}"; then
191                                    AC_MSG_RESULT([no])
192                                    AC_MSG_ERROR([Version requirement ][$2][ ][$3][ not met. Found: ${_][$1][_VERSION}])
193                                 else
194                                    AC_MSG_RESULT([yes])
195                                 fi
196                                 ])dnl if version requirement given
197                                 AC_MSG_CHECKING([for ][$2][ parameters from ][gp_lib_config])
198                                 [$1]_LIBS="$(${[$1][_CONFIG_PROG]} --libs || echo "*error*")"
199                                 [$1]_CFLAGS="$(${[$1][_CONFIG_PROG]} --cflags || echo "*error*")"
200                                 if test "x${[$1]_LIBS}" = "*error*" || 
201                                         test "x${[$1]_CFLAGS}" = "*error*"; then
202                                         AC_MSG_RESULT([error])
203                                 else
204                                         have_[$1]=yes
205                                         AC_MSG_RESULT([ok])
206                                 fi
207                         fi
208                         m4_popdef([gp_lib_config])dnl
209                 fi
210                 m4_ifval([$3],[# Version requirement given, so we do not rely on probing.],[
211                 # Neither pkg-config, nor the libfoo-config program have found anything.
212                 # So let's just probe the system.
213                 AC_MSG_WARN([The `$2' library could not be found using pkg-config or its known config program.
214 No version checks will be performed if it is found using any other method.])
215                 if test "x${[have_][$1]}" = "xno"; then
216                         ifs="$IFS"
217                         IFS=":" # FIXME: for W32 and OS/2 we may need ";" here
218                         for _libdir_ in \
219                                 ${LD_LIBRARY_PATH} \
220                                 "${libdir}" \
221                                 "${prefix}/lib64" "${prefix}/lib" \
222                                 /usr/lib64 /usr/lib \
223                                 /usr/local/lib64 /usr/local/lib \
224                                 /opt/lib64 /opt/lib
225                         do
226                                 IFS="$ifs"
227                                 for _soext_ in .la ${soext} .a; do
228                                         if test -f "${_libdir_}/[$2]${_soext_}"
229                                         then
230                                                 if test "x${_soext_}" = "x.la" ||
231                                                    test "x${_soext_}" = "x.a"; then
232                                                         [$1]_LIBS="${_libdir_}/[$2]${_soext_}"
233                                                 else
234                                                         [$1]_LIBS="-L${_libdir_} -l$(echo "$2" | sed 's/^lib//')"
235                                                 fi
236                                                 break
237                                         fi
238                                 done
239                                 if test "x${[$1][_LIBS]}" != "x"; then
240                                         break
241                                 fi
242                         done
243                         IFS="$ifs"
244                         if test "x${[$1][_LIBS]}" != "x"; then
245                                 have_[$1]=yes
246                         fi
247                 fi
248                 ])
249         elif test "x${[try_][$1]}" = "xno"; then
250                 :
251         else
252                 # We've been given a prefix to look in for library $2.
253                 # We start looking for $2.la files first.
254                 AC_MSG_CHECKING([for ][$2][.la file in ${[try_][$1]}])
255                 if test -f "${[try_][$1]}/lib/[$2].la"; then
256                         [$1][_LIBS]="${[try_][$1]}/lib/[$2].la"
257                         [$1][_CFLAGS]="-I${[try_][$1]}/include"
258                         AC_MSG_RESULT([libtool file $][$1][_LIBS (good)])
259                         have_[$1]=yes
260                 elif test -f "${[try_][$1]}/lib64/[$2].la"; then # HACK
261                         [$1][_LIBS]="${[try_][$1]}/lib64/[$2].la"
262                         [$1][_CFLAGS]="-I${[try_][$1]}/include"
263                         AC_MSG_RESULT([libtool file $][$1][_LIBS (good)])
264                         have_[$1]=yes
265                 else
266                         AC_MSG_RESULT([wild guess that something is in $try_][$1])
267                         [$1][_LIBS]="-L${[try_][$1]}/lib -l$(echo "$2" | sed 's/^lib//')"
268                         [$1][_CFLAGS]="-I${[try_][$1]}/include"
269                         have_[$1]=yes
270                         AC_MSG_WARN([
271 * Warning:
272 *   libtool file $2.la could not be found.
273 *   We may be linking against the WRONG library.
274 ])
275                 fi
276         fi
277 elif test "x${[$1][_LIBS]}" != "x" && test "x${[$1][_CFLAGS]}" != "x"; then
278         AC_MSG_RESULT([user-defined])
279         userdef_[$1]=yes
280         have_[$1]=yes
281 else
282         AC_MSG_RESULT([broken call])
283         AC_MSG_ERROR([
284 * Fatal:
285 * When calling configure for ${PACKAGE_TARNAME}
286 *     ${PACKAGE_NAME}
287 * either set both [$1][_LIBS] *and* [$1][_CFLAGS]
288 * or neither.
289 ])
290 fi
291 AC_SUBST([REQUIREMENTS_FOR_][$1])
292 dnl
293 dnl ACTION-IF-FOUND
294 dnl
295 m4_ifval([$6],[dnl
296 if test "x${[have_][$1]}" = "xyes"; then
297 # ACTION-IF-FOUND
298 $6
299 fi
300 ])dnl
301 dnl
302 dnl ACTION-IF-NOT-FOUND
303 dnl
304 m4_ifval([$7],[dnl
305 if test "x${[have_][$1]}" = "xno"; then
306 # ACTION-IF-NOT-FOUND
307 $7
308 fi
309 ])dnl
310 dnl
311 dnl Run our own test compilation
312 dnl
313 m4_ifval([$4],[dnl
314 if test "x${[have_][$1]}" = "xyes"; then
315 dnl AC_MSG_CHECKING([whether ][$2][ test compile succeeds])
316 dnl AC_MSG_RESULT([${[have_][$1]}])
317 CPPFLAGS_save="$CPPFLAGS"
318 CPPFLAGS="${[$1]_CFLAGS}"
319 AC_CHECK_HEADER([$4],[have_][$1][=yes],[have_][$1][=no])
320 CPPFLAGS="$CPPFLAGS_save"
321 fi
322 ])dnl
323 dnl
324 dnl Run our own test link
325 dnl    Does not work for libraries which be built after configure time,
326 dnl    so we deactivate it for them (userdef_).
327 dnl
328 m4_ifval([$5],[dnl
329 if test "x${[userdef_][$1]}" = "xno" && test "x${[have_][$1]}" = "xyes"; then
330         AC_MSG_CHECKING([for function ][$5][ in ][$2])
331         LIBS_save="$LIBS"
332         LIBS="${[$1]_LIBS}"
333         AC_TRY_LINK_FUNC([$5],[],[have_][$1][=no])
334         LIBS="$LIBS_save"
335         AC_MSG_RESULT([${[have_][$1]}])
336 fi
337 ])dnl
338 dnl
339 dnl Abort configure script if mandatory, but not found
340 dnl
341 m4_if([$8],[mandatory],[
342 if test "x${[have_][$1]}" = "xno"; then
343         AC_MSG_ERROR([
344 PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
345 [$1][_LIBS]=${[$1][_LIBS]}
346 [$1][_CFLAGS]=${[$1][_CFLAGS]}
347
348 * Fatal: ${PACKAGE_NAME} requires $2 $3 to build.
349
350 * Possible solutions:
351 *   - set PKG_CONFIG_PATH to adequate value
352 *   - call configure with [$1][_LIBS]=.. and [$1][_CFLAGS]=..
353 *   - call configure with one of the --with-$2 parameters
354 ]m4_ifval([$9],[dnl
355 *   - get $2 and install it
356 ],[dnl
357 *   - get $2 and install it:
358       $9]))
359 fi
360 ])dnl
361 dnl
362 dnl Abort configure script if not found and not explicitly disabled
363 dnl
364 m4_if([$8],[disable-explicitly],[
365 if test "x${[try_][$1]}" != "xno" && test "x${[have_][$1]}" = "xno"; then
366         AC_MSG_ERROR([
367 PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
368 [$1][_LIBS]=${[$1][_LIBS]}
369 [$1][_CFLAGS]=${[$1][_CFLAGS]}
370
371 * Fatal: ${PACKAGE_NAME} by default requires $2 $3 to build.
372 *        You must explicitly disable $2 to build ${PACKAGE_TARNAME} without it.
373
374 * Possible solutions:
375 *   - call configure with --with-$2=no or --without-$2
376 *   - set PKG_CONFIG_PATH to adequate value
377 *   - call configure with [$1][_LIBS]=.. and [$1][_CFLAGS]=..
378 *   - call configure with one of the --with-$2 parameters
379 ]m4_ifval([$9],[dnl
380 *   - get $2 and install it
381 ],[dnl
382 *   - get $2 and install it:
383       $9]))
384 fi
385 ])dnl
386 AM_CONDITIONAL([HAVE_][$1], [test "x$have_[$1]" = "xyes"])
387 if test "x$have_[$1]" = "xyes"; then
388         AC_DEFINE([HAVE_][$1], 1, [whether we compile with ][$2][ support])
389         GP_CONFIG_MSG([$2],[yes])dnl
390         AC_MSG_CHECKING([$2][ library flags])
391         AC_MSG_RESULT(["${[$1][_LIBS]}"])
392         AC_MSG_CHECKING([$2][ cpp flags])
393         AC_MSG_RESULT(["${[$1][_CFLAGS]}"])
394 else
395         GP_CONFIG_MSG([$2],[no])dnl
396 fi
397 dnl AC_SUBST is done implicitly by AC_ARG_VAR above.
398 dnl AC_SUBST([$1][_LIBS])
399 dnl AC_SUBST([$1][_CFLAGS])
400 ])dnl
401 dnl
402 dnl ####################################################################
403 dnl
404 AC_DEFUN([_GP_CHECK_LIBRARY_SYNTAX_ERROR],[dnl
405 m4_errprint(__file__:__line__:[ Error:
406 *** Calling $0 macro with old syntax
407 *** Aborting.
408 ])dnl
409 m4_exit(1)dnl
410 ])dnl
411 dnl
412 dnl ####################################################################
413 dnl
414 AC_DEFUN([GP_CHECK_LIBRARY],[dnl
415 m4_if([$4], [mandatory],        [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)],
416       [$4], [default-enabled],  [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)],
417       [$4], [default-disabled], [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)])dnl
418 m4_if([$8], [], [dnl
419       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[mandatory],[$9])],
420       [$8], [default-on], [dnl
421       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
422       [$8], [disable-explicitly], [dnl
423       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
424       [$8], [default-off], [dnl
425       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
426       [$8], [mandatory], [dnl
427       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
428       [m4_errprint(__file__:__line__:[ Error:
429 Illegal argument 6 to $0: `$6'
430 It must be one of "default-on", "default-off", "mandatory".
431 ])m4_exit(1)])dnl
432 ])dnl
433 dnl
434 m4_pattern_disallow([GP_CHECK_LIBRARY])
435 m4_pattern_disallow([_GP_CHECK_LIBRARY])
436 m4_pattern_disallow([_GP_CHECK_LIBRARY_SYNTAX_ERROR])
437 m4_pattern_disallow([_GP_CHECK_LIBRARY_SOEXT])
438 dnl
439 dnl ####################################################################
440 dnl
441 dnl Please do not remove this:
442 dnl filetype: 6e60b4f0-acb2-4cd5-8258-42014f92bd2c
443 dnl I use this to find all the different instances of this file which 
444 dnl are supposed to be synchronized.
445 dnl
446 dnl Local Variables:
447 dnl mode: autoconf
448 dnl End: