1# LIBCURL_CHECK_CONFIG ([DEFAULT-ACTION], [MINIMUM-VERSION],
2#                       [ACTION-IF-YES], [ACTION-IF-NO])
3# ----------------------------------------------------------
4#      David Shaw <dshaw@jabberwocky.com>   May-09-2006
5#
6# Checks for libcurl.  DEFAULT-ACTION is the string yes or no to
7# specify whether to default to --with-libcurl or --without-libcurl.
8# If not supplied, DEFAULT-ACTION is yes.  MINIMUM-VERSION is the
9# minimum version of libcurl to accept.  Pass the version as a regular
10# version number like 7.10.1. If not supplied, any version is
11# accepted.  ACTION-IF-YES is a list of shell commands to run if
12# libcurl was successfully found and passed the various tests.
13# ACTION-IF-NO is a list of shell commands that are run otherwise.
14# Note that using --without-libcurl does run ACTION-IF-NO.
15#
16# This macro #defines HAVE_LIBCURL if a working libcurl setup is
17# found, and sets @LIBCURL_LIBS@ and @LIBCURL_CFLAGS@ to the necessary
18# values.  Other useful defines are LIBCURL_FEATURE_xxx where xxx are
19# the various features supported by libcurl, and LIBCURL_PROTOCOL_yyy
20# where yyy are the various protocols supported by libcurl.  Both xxx
21# and yyy are capitalized.  See the list of AH_TEMPLATEs at the top of
22# the macro for the complete list of possible defines.  Shell
23# variables $libcurl_feature_xxx and $libcurl_protocol_yyy are also
24# defined to 'yes' for those features and protocols that were found.
25# Note that xxx and yyy keep the same capitalization as in the
26# curl-config list (e.g. it's "HTTP" and not "http").
27#
28# Users may override the detected values by doing something like:
29# LIBCURL_LIBS="-lcurl" LIBCURL_CFLAGS="-I/usr/myinclude" ./configure
30#
31# For the sake of sanity, this macro assumes that any libcurl that is
32# found is after version 7.7.2, the first version that included the
33# curl-config script.  Note that it is very important for people
34# packaging binary versions of libcurl to include this script!
35# Without curl-config, we can only guess what protocols are available,
36# or use curl_version_info to figure it out at runtime.
37
38AC_DEFUN([LIBCURL_CHECK_CONFIG],
39[
40  AH_TEMPLATE([LIBCURL_FEATURE_SSL],[Defined if libcurl supports SSL])
41  AH_TEMPLATE([LIBCURL_FEATURE_KRB4],[Defined if libcurl supports KRB4])
42  AH_TEMPLATE([LIBCURL_FEATURE_IPV6],[Defined if libcurl supports IPv6])
43  AH_TEMPLATE([LIBCURL_FEATURE_LIBZ],[Defined if libcurl supports libz])
44  AH_TEMPLATE([LIBCURL_FEATURE_ASYNCHDNS],[Defined if libcurl supports AsynchDNS])
45  AH_TEMPLATE([LIBCURL_FEATURE_IDN],[Defined if libcurl supports IDN])
46  AH_TEMPLATE([LIBCURL_FEATURE_SSPI],[Defined if libcurl supports SSPI])
47  AH_TEMPLATE([LIBCURL_FEATURE_NTLM],[Defined if libcurl supports NTLM])
48
49  AH_TEMPLATE([LIBCURL_PROTOCOL_HTTP],[Defined if libcurl supports HTTP])
50  AH_TEMPLATE([LIBCURL_PROTOCOL_HTTPS],[Defined if libcurl supports HTTPS])
51  AH_TEMPLATE([LIBCURL_PROTOCOL_FTP],[Defined if libcurl supports FTP])
52  AH_TEMPLATE([LIBCURL_PROTOCOL_FTPS],[Defined if libcurl supports FTPS])
53  AH_TEMPLATE([LIBCURL_PROTOCOL_FILE],[Defined if libcurl supports FILE])
54  AH_TEMPLATE([LIBCURL_PROTOCOL_TELNET],[Defined if libcurl supports TELNET])
55  AH_TEMPLATE([LIBCURL_PROTOCOL_LDAP],[Defined if libcurl supports LDAP])
56  AH_TEMPLATE([LIBCURL_PROTOCOL_DICT],[Defined if libcurl supports DICT])
57  AH_TEMPLATE([LIBCURL_PROTOCOL_TFTP],[Defined if libcurl supports TFTP])
58
59  _libcurl_config="no"
60
61  AC_ARG_WITH(libcurl,
62     [
63If you want to use cURL library:
64AC_HELP_STRING([--with-libcurl@<:@=DIR@:>@],[use cURL package @<:@default=no@:>@, optionally specify path to curl-config])],
65        [
66        if test "x$withval" = "xno"; then
67            want_curl="no"
68        elif test "x$withval" = "xyes"; then
69            want_curl="yes"
70        else
71            want_curl="yes"
72            _libcurl_config=$withval
73        fi
74        ],
75        [want_curl=ifelse([$1],,[no],[$1])])
76
77	if test "x$want_curl" != "xno"; then
78
79		AC_PROG_AWK
80
81		_libcurl_version_parse="eval $AWK '{split(\$NF,A,\".\"); X=256*256*A[[1]]+256*A[[2]]+A[[3]]; print X;}'"
82
83		_libcurl_try_link=no
84
85		AC_PATH_PROG([_libcurl_config], [curl-config], [])
86
87		if test -x "$_libcurl_config"; then
88			AC_CACHE_CHECK([for the version of libcurl],
89				[libcurl_cv_lib_curl_version],
90				[libcurl_cv_lib_curl_version=`$_libcurl_config --version | $AWK '{print $[]2}'`]
91			)
92
93			_libcurl_version=`echo $libcurl_cv_lib_curl_version | $_libcurl_version_parse`
94			_libcurl_wanted=`echo ifelse([$2],,[0],[$2]) | $_libcurl_version_parse`
95
96			if test $_libcurl_wanted -gt 0; then
97				AC_CACHE_CHECK([for libcurl >= version $2],
98					[libcurl_cv_lib_version_ok],[
99						if test $_libcurl_version -ge $_libcurl_wanted; then
100							libcurl_cv_lib_version_ok=yes
101							else
102							libcurl_cv_lib_version_ok=no
103						fi
104					]
105				)
106			fi
107
108			if test $_libcurl_wanted -eq 0 || test "x$libcurl_cv_lib_version_ok" = "xyes"; then
109				if test "x$LIBCURL_CFLAGS" = "x"; then
110					LIBCURL_CFLAGS=`$_libcurl_config --cflags`
111				fi
112
113				if test "x$LIBCURL_LIBS" = "x"; then
114					_curl_dir_lib=`$_libcurl_config --prefix`
115					_curl_dir_lib="$_curl_dir_lib/lib"
116					_full_libcurl_libs=`$_libcurl_config --libs`
117					for i in $_full_libcurl_libs; do
118						case $i in
119							-L*)
120								LIBCURL_LDFLAGS="$LIBCURL_LDFLAGS $i"
121						;;
122							-R*)
123								LIBCURL_LDFLAGS="$LIBCURL_LDFLAGS -Wl,$i"
124						;;
125							-lcurl)
126								if test "x$enable_static_libs" = "xyes" -a "x$static_linking_support" = "xno"; then
127									i="$_curl_dir_lib/libcurl.a"
128								elif test "x$enable_static_libs" = "xyes"; then
129									i="${static_linking_support}static $i ${static_linking_support}dynamic"
130								fi
131								LIBCURL_LIBS="$LIBCURL_LIBS $i"
132						;;
133							-l*)
134								if test "x$enable_static_libs" = "xyes"; then
135									_lib_name=`echo "$i" | cut -b3-`
136									test -f "$_curl_dir_lib/lib$_lib_name.a" && i="$_curl_dir_lib/lib$_lib_name.a"
137								fi
138								LIBCURL_LIBS="$LIBCURL_LIBS $i"
139						;;
140						esac
141					done
142
143					_save_curl_cflags="$CFLAGS"
144					_save_curl_ldflags="$LDFLAGS"
145					_save_curl_libs="$LIBS"
146					CFLAGS="$CFLAGS $LIBCURL_CFLAGS"
147					LDFLAGS="$LDFLAGS $LIBCURL_LDFLAGS"
148					if test "x$enable_static_libs" = "xyes"; then
149						test "x$want_openssl" = "xyes" && CFLAGS="$OPENSSL_CFLAGS $CFLAGS"
150						test "x$want_openssl" = "xyes" && LDFLAGS="$OPENSSL_LDFLAGS $LDFLAGS"
151						test "x$want_ldap" = "xyes" && CFLAGS="$LDAP_CPPFLAGS $CFLAGS"
152						test "x$want_ldap" = "xyes" && LDFLAGS="$LDAP_LDFLAGS $LDFLAGS"
153					fi
154
155					if test "x$enable_static" = "xyes" -o "x$enable_static_libs" = "xyes"; then
156						_full_libcurl_libs=`$_libcurl_config --static-libs`
157
158						if test "x$enable_static_libs" = "xyes" -a -z "$LIBPTHREAD_LIBS"; then
159							LIBPTHREAD_CHECK_CONFIG([no])
160							if test "x$found_libpthread" != "xyes"; then
161								AC_MSG_ERROR([Unable to use libpthread (libpthread check failed)])
162							fi
163							_full_libcurl_libs="$LIBPTHREAD_LIBS $_full_libcurl_libs"
164						fi
165
166						for i in $_full_libcurl_libs; do
167							case $i in
168								-lcurl)
169							;;
170								-l*)
171									_lib_i=$i
172									_lib_name=`echo "$i" | cut -b3-`
173									AC_CHECK_LIB($_lib_name , main,[
174										if test "x$enable_static_libs" = "xyes"; then
175											case $i in
176												-lssl|-lcrypto)
177													test "x$want_openssl" = "xyes" && i="$OPENSSL_LIBS"
178											;;
179												-lldap|-lldap_r|-llber)
180													test "x$want_ldap" = "xyes" && i="$LDAP_LIBS"
181											;;
182												-l*)
183													test -f "$_curl_dir_lib/lib$_lib_name.a" && i="$_curl_dir_lib/lib$_lib_name.a"
184											;;
185											esac
186										fi
187										test -z "${LIBCURL_LIBS##*$_lib_i*}" && LIBCURL_LIBS=`echo "$LIBCURL_LIBS"|sed "s|$_lib_i||g"`
188										test -z "${LIBCURL_LIBS##*$i*}" || LIBCURL_LIBS="$LIBCURL_LIBS $i"
189									],[
190										AC_MSG_ERROR([static library $_lib_name required for linking libcurl not found])
191									])
192							;;
193								-framework|CoreFoundation|Security)
194									LIBCURL_LIBS="$LIBCURL_LIBS $i"
195							;;
196							esac
197						done
198					fi # "x$enable_static" or "x$enable_static_libs"
199
200					LIBS="$LIBS $LIBCURL_LIBS"
201
202					AC_CHECK_LIB(curl, main, , [AC_MSG_ERROR([libcurl library not found])])
203
204					CFLAGS="$_save_curl_cflags"
205					LDFLAGS="$_save_curl_ldflags"
206					LIBS="$_save_curl_libs"
207					unset _save_curl_cflags
208					unset _save_curl_ldflags
209					unset _save_curl_libs
210
211					# This is so silly, but Apple actually has a bug in their
212					# curl-config script.  Fixed in Tiger, but there are still
213					# lots of Panther installs around.
214					case "${host}" in
215						powerpc-apple-darwin7*)
216							LIBCURL_LIBS=`echo $LIBCURL_LIBS | sed -e 's|-arch i386||g'`
217						;;
218					esac
219				fi # "x$LIBCURL_LIBS" = "x"
220
221				# All curl-config scripts support --feature
222				_libcurl_features=`$_libcurl_config --feature`
223
224				# Is it modern enough to have --protocols? (7.12.4)
225				if test $_libcurl_version -ge 461828; then
226					_libcurl_protocols=`$_libcurl_config --protocols`
227				fi
228
229				_libcurl_try_link=yes
230			fi # $_libcurl_wanted -eq 0 || "x$libcurl_cv_lib_version_ok" = "xyes"
231
232			unset _libcurl_wanted
233		fi # -x "$_libcurl_config"
234
235		if test "x$_libcurl_try_link" = "xyes"; then
236			# we didn't find curl-config, so let's see if the user-supplied
237			# link line (or failing that, "-lcurl") is enough.
238
239			LIBCURL_LIBS=${LIBCURL_LIBS-"$_libcurl_libs -lcurl"}
240
241			AC_CACHE_CHECK([whether libcurl is usable],
242				[libcurl_cv_lib_curl_usable],[
243				_save_curl_libs="${LIBS}"
244				_save_curl_ldflags="${LDFLAGS}"
245				_save_curl_cflags="${CFLAGS}"
246				LIBS="${LIBS} ${LIBCURL_LIBS}"
247				LDFLAGS="${LDFLAGS} ${LIBCURL_LDFLAGS}"
248				CFLAGS="${CFLAGS} ${LIBCURL_CFLAGS}"
249				if test "x$enable_static_libs" = "xyes"; then
250					test "x$want_openssl" = "xyes" && CFLAGS=" $OPENSSL_CFLAGS $CFLAGS"
251					test "x$want_openssl" = "xyes" && LDFLAGS="$OPENSSL_LDFLAGS $LDFLAGS"
252					test "x$want_ldap" = "xyes" && CFLAGS="$LDAP_CPPFLAGS $CFLAGS"
253					test "x$want_ldap" = "xyes" && LDFLAGS="$LDAP_LDFLAGS $LDFLAGS"
254				fi
255
256				AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <curl/curl.h>
257#ifndef NULL
258#define NULL (void *)0
259#endif],[
260/* Try and use a few common options to force a failure if we are
261   missing symbols or can't link. */
262int x;
263curl_easy_setopt(NULL,CURLOPT_URL,NULL);
264x=CURL_ERROR_SIZE;
265x=CURLOPT_WRITEFUNCTION;
266x=CURLOPT_FILE;
267x=CURLOPT_ERRORBUFFER;
268x=CURLOPT_STDERR;
269x=CURLOPT_VERBOSE;
270])],libcurl_cv_lib_curl_usable=yes,libcurl_cv_lib_curl_usable=no)
271
272				LIBS="${_save_curl_libs}"
273				LDFLAGS="${_save_curl_ldflags}"
274				CFLAGS="${_save_curl_cflags}"
275				unset _save_curl_libs
276				unset _save_curl_ldflags
277				unset _save_curl_cflags
278			])
279
280			if test "x$libcurl_cv_lib_curl_usable" = "xno"; then
281				link_mode="dynamic"
282				if test "x$enable_static" = "xyes"; then
283					link_mode="static"
284				fi
285				AC_MSG_ERROR([libcurl is not available for ${link_mode} linking])
286			fi
287
288			# Does curl_free() exist in this version of libcurl?
289			# If not, fake it with free()
290
291			_save_curl_libs="${LIBS}"
292			_save_curl_ldflags="${LDFLAGS}"
293			_save_curl_cflags="${CFLAGS}"
294			LIBS="${LIBS} ${LIBCURL_LIBS}"
295			LDFLAGS="${LDFLAGS} ${LIBCURL_LDFLAGS}"
296			CFLAGS="${CFLAGS} ${LIBCURL_CFLAGS}"
297
298			AC_CHECK_FUNC(curl_free,,
299				AC_DEFINE(curl_free,free,
300					[Define curl_free() as free() if our version of curl lacks curl_free.]))
301
302			AC_CHECK_FUNC(curl_easy_escape,
303				AC_DEFINE(HAVE_FUNCTION_CURL_EASY_ESCAPE,1,
304					[Define to 1 if function 'curl_easy_escape' exists.]))
305
306			LIBS="${_save_curl_libs}"
307			LDFLAGS="${_save_curl_ldflags}"
308			CFLAGS="${_save_curl_cflags}"
309			unset _save_curl_libs
310			unset _save_curl_ldflags
311			unset _save_curl_cflags
312
313			AC_DEFINE(HAVE_LIBCURL,1,
314				[Define to 1 if you have a functional curl library.])
315			AC_SUBST(LIBCURL_CFLAGS)
316			AC_SUBST(LIBCURL_LDFLAGS)
317			AC_SUBST(LIBCURL_LIBS)
318			found_curl="yes"
319
320			for _libcurl_feature in $_libcurl_features ; do
321				AC_DEFINE_UNQUOTED(AS_TR_CPP(libcurl_feature_$_libcurl_feature),[1])
322				eval AS_TR_SH(libcurl_feature_$_libcurl_feature)=yes
323			done
324
325			if test "x$_libcurl_protocols" = "x"; then
326				# We don't have --protocols, so just assume that all
327				# protocols are available
328				_libcurl_protocols="HTTP FTP FILE TELNET LDAP DICT"
329
330				if test "x$libcurl_feature_SSL" = "xyes"; then
331					_libcurl_protocols="$_libcurl_protocols HTTPS"
332
333					# FTPS wasn't standards-compliant until version
334					# 7.11.0
335					if test $_libcurl_version -ge 461568; then
336						_libcurl_protocols="$_libcurl_protocols FTPS"
337					fi
338				fi
339			fi
340
341			for _libcurl_protocol in $_libcurl_protocols ; do
342				AC_DEFINE_UNQUOTED(AS_TR_CPP(libcurl_protocol_$_libcurl_protocol),[1])
343				eval AS_TR_SH(libcurl_protocol_$_libcurl_protocol)=yes
344			done
345		else
346			unset LIBCURL_LIBS
347			unset LIBCURL_CFLAGS
348		fi # "x$_libcurl_try_link" = "xyes"
349
350		unset _libcurl_try_link
351		unset _libcurl_version_parse
352		unset _libcurl_config
353		unset _libcurl_feature
354		unset _libcurl_features
355		unset _libcurl_protocol
356		unset _libcurl_protocols
357		unset _libcurl_version
358		unset _libcurl_libs
359	fi # "x$want_curl" != "xno"
360
361  if test "x$want_curl" = "xno" || test "x$libcurl_cv_lib_curl_usable" != "xyes"; then
362     # This is the IF-NO path
363     ifelse([$4],,:,[$4])
364  else
365     # This is the IF-YES path
366     ifelse([$3],,:,[$3])
367  fi
368
369])dnl
370