cdf6a8b899478dbb81bdffe9c04eed0c15c6bd5d
[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                           default: [mandatory]
24 dnl    WHERE-TO-GET-IT        place where to find the library, e.g. a URL
25 dnl                           default: []
26 dnl
27 dnl What the ACTION-IFs can do:
28 dnl
29 dnl   * change the variable have_[$1] to "yes" or "no" and thus change
30 dnl     the outcome of the test
31 dnl   * execute additional checks to define more specific variables, e.g.
32 dnl     for different API versions
33 dnl
34 dnl These results have happened after calling GP_CHECK_LIBRARY:
35 dnl
36 dnl    AM_CONDITIONAL([HAVE_VARPREFIX],[ if found ])
37 dnl    AM_SUBST([have_VARPREFIX], [ "yes" if found, "no" if not found ])
38 dnl    AM_SUBST([VARPREFIX_CFLAGS],[ -I, -D and stuff ])
39 dnl    AM_SUBST([VARPREFIX_LIBS], [ /path/to/libname.la -L/path -lfoo ])
40 dnl
41 dnl Parameters to ./configure which influence the GP_CHECK_LIBRARY results:
42 dnl
43 dnl   * VARNAMEPART_LIBS=/foobar/arm-palmos/lib/libname.la
44 dnl     VARNAMEPART_CFLAGS=-I/foobar/include
45 dnl   * --without-libfoo
46 dnl   * --with-libfoo=/usr/local
47 dnl   * --with-libfoo-include-dir=/foobar/include
48 dnl   * --with-libfoo-lib=/foobar/arm-palmos/lib
49 dnl   * --with-libfoo=autodetect
50 dnl
51 dnl Examples:
52 dnl    GP_CHECK_LIBRARY([LIBEXIF], [libexif])dnl
53 dnl    GP_CHECK_LIBRARY([LIBEXIF], [libexif-gtk], [>= 0.3.3])dnl
54 dnl                                  note the space! ^
55 dnl
56 dnl Possible enhancements:
57 dnl
58 dnl   * Derive VAR_PREFIX directly from libname
59 dnl     This will change the calling conventions, so be aware of that.
60 dnl   * Give names of a header file and function name and to a test
61 dnl     compilation.
62 dnl
63 AC_DEFUN([_GP_CHECK_LIBRARY_SOEXT],[dnl
64 AC_MSG_CHECKING([for dynamic library extension])
65 soext=""
66 case "$host" in
67         *linux*)        soext=".so" ;;
68         *sunos*)        soext=".so" ;;
69         *solaris*)      soext=".so" ;;
70         *bsd*)          soext=".so" ;;
71         *darwin*)       soext=".dylib" ;;
72         *w32*)          soext=".dll" ;;
73 esac
74 if test "x$soext" = "x"; then
75         soext=".so"
76         AC_MSG_RESULT([${soext}])
77         AC_MSG_WARN([
78 Host system "${host}" not recognized, defaulting to "${soext}".
79 ])
80 else
81         AC_MSG_RESULT([${soext}])
82 fi
83 ])dnl
84 dnl
85 AC_DEFUN([_GP_CHECK_LIBRARY],[
86 # ----------------------------------------------------------------------
87 # [GP_CHECK_LIBRARY]([$1],[$2],[$3],
88 #                    [$4],[$5],
89 #                    [...],[...],[$8])
90 m4_ifval([$9],[dnl
91 # $9
92 ])dnl
93 # ----------------------------------------------------------------------
94 dnl
95 AC_REQUIRE([GP_CONFIG_MSG])dnl
96 AC_REQUIRE([GP_PKG_CONFIG])dnl
97 AC_REQUIRE([_GP_CHECK_LIBRARY_SOEXT])dnl
98 dnl Use _CFLAGS and _LIBS given to configure.
99 dnl This makes it possible to set these vars in a configure script
100 dnl and AC_CONFIG_SUBDIRS this configure.
101 AC_ARG_VAR([$1][_CFLAGS], [CFLAGS for compiling with ][$2])dnl
102 AC_ARG_VAR([$1][_LIBS],   [LIBS to add for linking against ][$2])dnl
103 dnl
104 AC_MSG_CHECKING([for ][$2][ to use])
105 userdef_[$1]=no
106 have_[$1]=no
107 if test "x${[$1][_LIBS]}" = "x" && test "x${[$1][_CFLAGS]}" = "x"; then
108         dnl define --with/--without argument
109         m4_if([$8], [default-off],
110                 [m4_pushdef([gp_lib_arg],[--without-][$2])dnl
111                         try_[$1]=no
112                 ],
113                 [m4_pushdef([gp_lib_arg],[--with-][$2])dnl
114                         try_[$1]=auto
115                 ])dnl
116         AC_ARG_WITH([$2],[AS_HELP_STRING([gp_lib_arg][=PREFIX],[where to find ][$2][, "no" or "auto"])],[try_][$1][="$withval"])
117         if test "x${[try_][$1]}" = "xauto"; then [try_][$1]=autodetect; fi
118         AC_MSG_RESULT([${try_][$1][}])
119         m4_popdef([gp_lib_arg])dnl
120         if test "x${[try_][$1]}" = "xautodetect"; then
121                 dnl OK, we have to autodetect.
122                 dnl We start autodetection with the cleanest known method: pkg-config
123                 if test "x${[have_][$1]}" = "xno"; then
124                         dnl we need that line break after the PKG_CHECK_MODULES
125                         m4_ifval([$3],
126                                 [PKG_CHECK_MODULES([$1],[$2][ $3],[have_][$1][=yes],[:])],
127                                 [PKG_CHECK_MODULES([$1],[$2],     [have_][$1][=yes],[:])]
128                         )
129                 fi
130                 dnl If pkg-config didn't find anything, try the libfoo-config program
131                 dnl certain known libraries ship with.
132                 if test "x${[have_][$1]}" = "xno"; then
133                         AC_MSG_WARN([The `$2' library could not be found using pkg-config.
134 No version checks will be performed if it is found using any other method.])
135                         AC_MSG_CHECKING([$2][ config program])
136                         m4_pushdef([gp_lib_config],[m4_if([$2],[libusb],[libusb-config],
137                                 [$2],[libgphoto2],[gphoto2-config],
138                                 [$2],[libgphoto2_port],[gphoto2-port-config],
139                                 [none])])dnl
140                         AC_MSG_RESULT([gp_lib_config])
141                         AC_PATH_PROG([$1][_CONFIG_PROG],[gp_lib_config])
142                         if test -n "${[$1][_CONFIG_PROG]}" &&
143                                 test "${[$1][_CONFIG_PROG]}" != "none"; then
144                                 AC_MSG_CHECKING([for ][$2][ parameters from ][gp_lib_config])
145                                 [$1]_LIBS="$(${[$1][_CONFIG_PROG]} --libs || echo "*error*")"
146                                 [$1]_CFLAGSS="$(${[$1][_CONFIG_PROG]} --cflags || echo "*error*")"
147                                 if test "x${[$1]_LIBS}" = "*error*" || 
148                                         test "x${[$1]_CFLAGS}" = "*error*"; then
149                                         AC_MSG_RESULT([error])
150                                 else
151                                         have_[$1]=yes
152                                         AC_MSG_RESULT([ok])
153                                 fi
154                         fi
155                         m4_popdef([gp_lib_config])dnl
156                 fi
157                 dnl Neither pkg-config, nor the libfoo-config program have found anything.
158                 dnl So let's just probe the system.
159                 if test "x${[have_][$1]}" = "xno"; then
160                         ifs="$IFS"
161                         IFS=":" # FIXME: for W32 and OS/2 we may need ";" here
162                         for _libdir_ in \
163                                 ${LD_LIBRARY_PATH} \
164                                 "${libdir}" \
165                                 "${prefix}/lib64" "${prefix}/lib" \
166                                 /usr/lib64 /usr/lib \
167                                 /usr/local/lib64 /usr/local/lib \
168                                 /opt/lib64 /opt/lib
169                         do
170                                 IFS="$ifs"
171                                 for _soext_ in .la ${soext} .a; do
172                                         if test -f "${_libdir_}/[$2]${_soext_}"
173                                         then
174                                                 if test "x${_soext_}" = "x.la" ||
175                                                    test "x${_soext_}" = "x.a"; then
176                                                         [$1]_LIBS="${_libdir_}/[$2]${_soext_}"
177                                                 else
178                                                         [$1]_LIBS="-L${_libdir_} -l$(echo "$2" | sed 's/^lib//')"
179                                                 fi
180                                                 break
181                                         fi
182                                 done
183                                 if test "x${[$1][_LIBS]}" != "x"; then
184                                         break
185                                 fi
186                         done
187                         IFS="$ifs"
188                         if test "x${[$1][_LIBS]}" != "x"; then
189                                 have_[$1]=yes
190                         fi
191                 fi
192         elif test "x${[try_][$1]}" = "xno"; then
193                 :
194         else
195                 [$1][_LIBS]="-L${[try_][$1]}/lib -l$(echo "$2" | sed 's/^lib//')"
196                 [$1][_CFLAGS]="-I${[try_][$1]}/include"
197         fi
198 elif test "x${[$1][_LIBS]}" != "x" && test "x${[$1][_CFLAGS]}" != "x"; then
199         AC_MSG_RESULT([user-defined])
200         userdef_[$1]=yes
201         have_[$1]=yes
202 else
203         AC_MSG_RESULT([broken call])
204         AC_MSG_ERROR([
205 * Fatal:
206 * When calling configure for ${PACKAGE_TARNAME}
207 *     ${PACKAGE_NAME}
208 * either set both [$1][_LIBS] *and* [$1][_CFLAGS]
209 * or neither.
210 ])
211 fi
212 dnl
213 dnl ACTION-IF-FOUND
214 dnl
215 m4_ifval([$6],[dnl
216 if test "x${[have_][$1]}" = "xyes"; then
217 # ACTION-IF-FOUND
218 $6
219 fi
220 ])dnl
221 dnl
222 dnl ACTION-IF-NOT-FOUND
223 dnl
224 m4_ifval([$7],[dnl
225 if test "x${[have_][$1]}" = "xno"; then
226 # ACTION-IF-NOT-FOUND
227 $7
228 fi
229 ])dnl
230 dnl
231 dnl Run our own test compilation
232 dnl
233 m4_ifval([$4],[dnl
234 if test "x${[have_][$1]}" = "xyes"; then
235 dnl AC_MSG_CHECKING([whether ][$2][ test compile succeeds])
236 dnl AC_MSG_RESULT([${[have_][$1]}])
237 CPPFLAGS_save="$CPPFLAGS"
238 CPPFLAGS="${[$1]_CFLAGS}"
239 AC_CHECK_HEADER([$4],[have_][$1][=yes],[have_][$1][=no])
240 CPPFLAGS="$CPPFLAGS_save"
241 fi
242 ])dnl
243 dnl
244 dnl Run our own test link
245 dnl    Does not work for libraries which be built after configure time,
246 dnl    so we deactivate it for them (userdef_).
247 dnl
248 m4_ifval([$5],[dnl
249 if test "x${[userdef_][$1]}" = "xno" && test "x${[have_][$1]}" = "xyes"; then
250         AC_MSG_CHECKING([for function ][$5][ in ][$2])
251         LIBS_save="$LIBS"
252         LIBS="${[$1]_LIBS}"
253         AC_TRY_LINK_FUNC([$5],[],[have_][$1][=no])
254         LIBS="$LIBS_save"
255         AC_MSG_RESULT([${[have_][$1]}])
256 fi
257 ])dnl
258 dnl
259 dnl Abort configure script if mandatory, but not found
260 dnl
261 m4_if([$8],[mandatory],[
262 if test "x${[have_][$1]}" = "xno"; then
263         AC_MSG_ERROR([
264 PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
265 [$1][_LIBS]=${[$1][_LIBS]}
266 [$1][_CFLAGS]=${[$1][_CFLAGS]}
267
268 * Fatal: ${PACKAGE_NAME} requires $2 to build.
269
270 * Possible solutions:
271 *   - set PKG_CONFIG_PATH to adequate value
272 *   - call configure with [$1][_LIBS]=.. and [$1][_CFLAGS]=..
273 *   - call configure with one of the --with-$2 parameters
274 ]m4_ifval([$9],[dnl
275 *   - get $2 and install it
276 ],[dnl
277 *   - get $2 and install it:
278       $9]))
279 fi
280 ])dnl
281 AM_CONDITIONAL([HAVE_][$1], [test "x$have_[$1]" = "xyes"])
282 if test "x$have_[$1]" = "xyes"; then
283         AC_DEFINE([HAVE_][$1], 1, [whether we compile with ][$2][ support])
284         GP_CONFIG_MSG([$2],[yes])dnl
285         AC_MSG_CHECKING([$2][ library flags])
286         AC_MSG_RESULT([${[$1][_LIBS]}])
287         AC_MSG_CHECKING([$2][ cpp flags])
288         AC_MSG_RESULT([${[$1][_CFLAGS]}])
289 else
290         GP_CONFIG_MSG([$2],[no])dnl
291 fi
292 dnl AC_SUBST is done implicitly by AC_ARG_VAR above.
293 dnl AC_SUBST([$1][_LIBS])
294 dnl AC_SUBST([$1][_CFLAGS])
295 ])dnl
296 dnl
297 dnl ####################################################################
298 dnl
299 AC_DEFUN([_GP_CHECK_LIBRARY_SYNTAX_ERROR],[dnl
300 m4_errprint(__file__:__line__:[ Error:
301 *** Calling $0 macro with old syntax
302 *** Aborting.
303 ])dnl
304 m4_exit(1)dnl
305 ])dnl
306 dnl
307 dnl ####################################################################
308 dnl
309 AC_DEFUN([GP_CHECK_LIBRARY],[dnl
310 m4_if([$4], [mandatory],        [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)],
311       [$4], [default-enabled],  [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)],
312       [$4], [default-disabled], [_GP_CHECK_LIBRARY_SYNTAX_ERROR($0)])dnl
313 m4_if([$8], [], [dnl
314       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[mandatory],[$9])],
315       [$8], [default-on], [dnl
316       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
317       [$8], [default-off], [dnl
318       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
319       [$8], [mandatory], [dnl
320       _GP_CHECK_LIBRARY([$1],[$2],[$3],[$4],[$5],[$6],[$7],[$8],[$9])],
321       [m4_errprint(__file__:__line__:[ Error:
322 Illegal argument 6 to $0: `$6'
323 It must be one of "default-on", "default-off", "mandatory".
324 ])m4_exit(1)])dnl
325 ])dnl
326 dnl
327 m4_pattern_disallow([GP_CHECK_LIBRARY])
328 m4_pattern_disallow([_GP_CHECK_LIBRARY])
329 m4_pattern_disallow([_GP_CHECK_LIBRARY_SYNTAX_ERROR])
330 m4_pattern_disallow([_GP_CHECK_LIBRARY_SOEXT])
331 dnl
332 dnl ####################################################################
333 dnl
334 dnl Please do not remove this:
335 dnl filetype: 6e60b4f0-acb2-4cd5-8258-42014f92bd2c
336 dnl I use this to find all the different instances of this file which 
337 dnl are supposed to be synchronized.
338 dnl
339 dnl Local Variables:
340 dnl mode: autoconf
341 dnl End: