dd3700a7d323a54c41faf49b4df21d8e0aa3b56c
[platform/upstream/libexif.git] / m4m / 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 dnl Use _CFLAGS and _LIBS given to configure.
109 dnl This makes it possible to set these vars in a configure script
110 dnl 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 userdef_[$1]=no
116 have_[$1]=no
117 if test "x${[$1][_LIBS]}" = "x" && test "x${[$1][_CFLAGS]}" = "x"; then
118         dnl define --with/--without argument
119         m4_if([$8], [default-off],
120                 [m4_pushdef([gp_lib_arg],[--without-][$2])dnl
121                         try_[$1]=no
122                 ],
123                 [m4_pushdef([gp_lib_arg],[--with-][$2])dnl
124                         try_[$1]=auto
125                 ])dnl
126         AC_ARG_WITH([$2],[AS_HELP_STRING([gp_lib_arg][=PREFIX],[where to find ][$2][, "no" or "auto"])],[try_][$1][="$withval"])
127         if test "x${[try_][$1]}" = "xauto"; then [try_][$1]=autodetect; fi
128         AC_MSG_RESULT([${try_][$1][}])
129         m4_popdef([gp_lib_arg])dnl
130         if test "x${[try_][$1]}" = "xautodetect"; then
131                 dnl OK, we have to autodetect.
132                 dnl We start autodetection with the cleanest known method: pkg-config
133                 if test "x${[have_][$1]}" = "xno"; then
134                         dnl we need that line break after the PKG_CHECK_MODULES
135                         m4_ifval([$3],
136                                 [PKG_CHECK_MODULES([$1],[$2][ $3],[have_][$1][=yes],[:])],
137                                 [PKG_CHECK_MODULES([$1],[$2],     [have_][$1][=yes],[:])]
138                         )
139                 fi
140                 dnl If pkg-config didn't find anything, try the libfoo-config program
141                 dnl certain known libraries ship with.
142                 if test "x${[have_][$1]}" = "xno"; then
143                         AC_MSG_WARN([The `$2' library could not be found using pkg-config.
144 No version checks will be performed if it is found using any other method.])
145                         AC_MSG_CHECKING([$2][ config program])
146                         m4_pushdef([gp_lib_config],[m4_if([$2],[libusb],[libusb-config],
147                                 [$2],[libgphoto2],[gphoto2-config],
148                                 [$2],[libgphoto2_port],[gphoto2-port-config],
149                                 [none])])dnl
150                         AC_MSG_RESULT([gp_lib_config])
151                         AC_PATH_PROG([$1][_CONFIG_PROG],[gp_lib_config])
152                         if test -n "${[$1][_CONFIG_PROG]}" &&
153                                 test "${[$1][_CONFIG_PROG]}" != "none"; then
154                                 AC_MSG_CHECKING([for ][$2][ parameters from ][gp_lib_config])
155                                 [$1]_LIBS="$(${[$1][_CONFIG_PROG]} --libs || echo "*error*")"
156                                 [$1]_CFLAGSS="$(${[$1][_CONFIG_PROG]} --cflags || echo "*error*")"
157                                 if test "x${[$1]_LIBS}" = "*error*" || 
158                                         test "x${[$1]_CFLAGS}" = "*error*"; then
159                                         AC_MSG_RESULT([error])
160                                 else
161                                         have_[$1]=yes
162                                         AC_MSG_RESULT([ok])
163                                 fi
164                         fi
165                         m4_popdef([gp_lib_config])dnl
166                 fi
167                 dnl Neither pkg-config, nor the libfoo-config program have found anything.
168                 dnl So let's just probe the system.
169                 if test "x${[have_][$1]}" = "xno"; then
170                         ifs="$IFS"
171                         IFS=":" # FIXME: for W32 and OS/2 we may need ";" here
172                         for _libdir_ in \
173                                 ${LD_LIBRARY_PATH} \
174                                 "${libdir}" \
175                                 "${prefix}/lib64" "${prefix}/lib" \
176                                 /usr/lib64 /usr/lib \
177                                 /usr/local/lib64 /usr/local/lib \
178                                 /opt/lib64 /opt/lib
179                         do
180                                 IFS="$ifs"
181                                 for _soext_ in .la ${soext} .a; do
182                                         if test -f "${_libdir_}/[$2]${_soext_}"
183                                         then
184                                                 if test "x${_soext_}" = "x.la" ||
185                                                    test "x${_soext_}" = "x.a"; then
186                                                         [$1]_LIBS="${_libdir_}/[$2]${_soext_}"
187                                                 else
188                                                         [$1]_LIBS="-L${_libdir_} -l$(echo "$2" | sed 's/^lib//')"
189                                                 fi
190                                                 break
191                                         fi
192                                 done
193                                 if test "x${[$1][_LIBS]}" != "x"; then
194                                         break
195                                 fi
196                         done
197                         IFS="$ifs"
198                         if test "x${[$1][_LIBS]}" != "x"; then
199                                 have_[$1]=yes
200                         fi
201                 fi
202         elif test "x${[try_][$1]}" = "xno"; then
203                 :
204         else
205                 [$1][_LIBS]="-L${[try_][$1]}/lib -l$(echo "$2" | sed 's/^lib//')"
206                 [$1][_CFLAGS]="-I${[try_][$1]}/include"
207         fi
208 elif test "x${[$1][_LIBS]}" != "x" && test "x${[$1][_CFLAGS]}" != "x"; then
209         AC_MSG_RESULT([user-defined])
210         userdef_[$1]=yes
211         have_[$1]=yes
212 else
213         AC_MSG_RESULT([broken call])
214         AC_MSG_ERROR([
215 * Fatal:
216 * When calling configure for ${PACKAGE_TARNAME}
217 *     ${PACKAGE_NAME}
218 * either set both [$1][_LIBS] *and* [$1][_CFLAGS]
219 * or neither.
220 ])
221 fi
222 dnl
223 dnl ACTION-IF-FOUND
224 dnl
225 m4_ifval([$6],[dnl
226 if test "x${[have_][$1]}" = "xyes"; then
227 # ACTION-IF-FOUND
228 $6
229 fi
230 ])dnl
231 dnl
232 dnl ACTION-IF-NOT-FOUND
233 dnl
234 m4_ifval([$7],[dnl
235 if test "x${[have_][$1]}" = "xno"; then
236 # ACTION-IF-NOT-FOUND
237 $7
238 fi
239 ])dnl
240 dnl
241 dnl Run our own test compilation
242 dnl
243 m4_ifval([$4],[dnl
244 if test "x${[have_][$1]}" = "xyes"; then
245 dnl AC_MSG_CHECKING([whether ][$2][ test compile succeeds])
246 dnl AC_MSG_RESULT([${[have_][$1]}])
247 CPPFLAGS_save="$CPPFLAGS"
248 CPPFLAGS="${[$1]_CFLAGS}"
249 AC_CHECK_HEADER([$4],[have_][$1][=yes],[have_][$1][=no])
250 CPPFLAGS="$CPPFLAGS_save"
251 fi
252 ])dnl
253 dnl
254 dnl Run our own test link
255 dnl    Does not work for libraries which be built after configure time,
256 dnl    so we deactivate it for them (userdef_).
257 dnl
258 m4_ifval([$5],[dnl
259 if test "x${[userdef_][$1]}" = "xno" && test "x${[have_][$1]}" = "xyes"; then
260         AC_MSG_CHECKING([for function ][$5][ in ][$2])
261         LIBS_save="$LIBS"
262         LIBS="${[$1]_LIBS}"
263         AC_TRY_LINK_FUNC([$5],[],[have_][$1][=no])
264         LIBS="$LIBS_save"
265         AC_MSG_RESULT([${[have_][$1]}])
266 fi
267 ])dnl
268 dnl
269 dnl Abort configure script if mandatory, but not found
270 dnl
271 m4_if([$8],[mandatory],[
272 if test "x${[have_][$1]}" = "xno"; then
273         AC_MSG_ERROR([
274 PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
275 [$1][_LIBS]=${[$1][_LIBS]}
276 [$1][_CFLAGS]=${[$1][_CFLAGS]}
277
278 * Fatal: ${PACKAGE_NAME} requires $2 to build.
279
280 * Possible solutions:
281 *   - set PKG_CONFIG_PATH to adequate value
282 *   - call configure with [$1][_LIBS]=.. and [$1][_CFLAGS]=..
283 *   - call configure with one of the --with-$2 parameters
284 ]m4_ifval([$9],[dnl
285 *   - get $2 and install it
286 ],[dnl
287 *   - get $2 and install it:
288       $9]))
289 fi
290 ])dnl
291 dnl
292 dnl Abort configure script if not found and not explicitly disabled
293 dnl
294 m4_if([$8],[disable-explicitly],[
295 if test "x${[try_][$1]}" != "xno" && test "x${[have_][$1]}" = "xno"; then
296         AC_MSG_ERROR([
297 PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
298 [$1][_LIBS]=${[$1][_LIBS]}
299 [$1][_CFLAGS]=${[$1][_CFLAGS]}
300
301 * Fatal: ${PACKAGE_NAME} by default requires $2 to build.
302 *        You must explicitly disable $2 to build ${PACKAGE_TARNAME} without it.
303
304 * Possible solutions:
305 *   - call configure with --with-$2=no or --without-$2
306 *   - set PKG_CONFIG_PATH to adequate value
307 *   - call configure with [$1][_LIBS]=.. and [$1][_CFLAGS]=..
308 *   - call configure with one of the --with-$2 parameters
309 ]m4_ifval([$9],[dnl
310 *   - get $2 and install it
311 ],[dnl
312 *   - get $2 and install it:
313       $9]))
314 fi
315 ])dnl
316 AM_CONDITIONAL([HAVE_][$1], [test "x$have_[$1]" = "xyes"])
317 if test "x$have_[$1]" = "xyes"; then
318         AC_DEFINE([HAVE_][$1], 1, [whether we compile with ][$2][ support])
319         GP_CONFIG_MSG([$2],[yes])dnl
320         AC_MSG_CHECKING([$2][ library flags])
321         AC_MSG_RESULT([${[$1][_LIBS]}])
322         AC_MSG_CHECKING([$2][ cpp flags])
323         AC_MSG_RESULT([${[$1][_CFLAGS]}])
324 else
325         GP_CONFIG_MSG([$2],[no])dnl
326 fi
327 dnl AC_SUBST is done implicitly by AC_ARG_VAR above.
328 dnl AC_SUBST([$1][_LIBS])
329 dnl AC_SUBST([$1][_CFLAGS])
330 ])dnl
331 dnl
332 dnl ####################################################################
333 dnl
334 AC_DEFUN([_GP_CHECK_LIBRARY_SYNTAX_ERROR],[dnl
335 m4_errprint(__file__:__line__:[ Error:
336 *** Calling $0 macro with old syntax
337 *** Aborting.
338 ])dnl
339 m4_exit(1)dnl
340 ])dnl
341 dnl
342 dnl ####################################################################
343 dnl
344 AC_DEFUN([GP_CHECK_LIBRARY],[dnl
345 m4_if([$4], [mandatory],        [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)],
346       [$4], [default-enabled],  [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)],
347       [$4], [default-disabled], [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)])dnl
348 m4_if([$8], [], [dnl
349       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[mandatory],[$9])],
350       [$8], [default-on], [dnl
351       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
352       [$8], [disable-explicitly], [dnl
353       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
354       [$8], [default-off], [dnl
355       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
356       [$8], [mandatory], [dnl
357       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
358       [m4_errprint(__file__:__line__:[ Error:
359 Illegal argument 6 to $0: `$6'
360 It must be one of "default-on", "default-off", "mandatory".
361 ])m4_exit(1)])dnl
362 ])dnl
363 dnl
364 m4_pattern_disallow([GP_CHECK_LIBRARY])
365 m4_pattern_disallow([_GP_CHECK_LIBRARY])
366 m4_pattern_disallow([_GP_CHECK_LIBRARY_SYNTAX_ERROR])
367 m4_pattern_disallow([_GP_CHECK_LIBRARY_SOEXT])
368 dnl
369 dnl ####################################################################
370 dnl
371 dnl Please do not remove this:
372 dnl filetype: 6e60b4f0-acb2-4cd5-8258-42014f92bd2c
373 dnl I use this to find all the different instances of this file which 
374 dnl are supposed to be synchronized.
375 dnl
376 dnl Local Variables:
377 dnl mode: autoconf
378 dnl End: