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