1dnl Find the compiler and linker flags for cURL.
2dnl
3dnl Finds the compiler and linker flags for linking with the cURL library.
4dnl Provides the --with-curl, --with-curl-lib, and --with-curl-include
5dnl configure options to specify non-standard paths to the cURL libraries.
6dnl Uses curl-config where available.
7dnl
8dnl Provides the macro RRA_LIB_CURL and sets the substitution variables
9dnl CURL_CPPFLAGS, CURL_LDFLAGS, and CURL_LIBS.  Also provides
10dnl RRA_LIB_CURL_SWITCH to set CPPFLAGS, LDFLAGS, and LIBS to include the cURL
11dnl libraries, saving the current values first, and RRA_LIB_CURL_RESTORE to
12dnl restore those settings to before the last RRA_LIB_CURL_SWITCH.
13dnl
14dnl Depends on RRA_SET_LDFLAGS and RRA_ENABLE_REDUCED_DEPENDS and may depend
15dnl on RRA_LIB_OPENSSL.
16dnl
17dnl The canonical version of this file is maintained in the rra-c-util
18dnl package, available at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
19dnl
20dnl Written by Russ Allbery <eagle@eyrie.org>
21dnl Copyright 2010, 2013
22dnl     The Board of Trustees of the Leland Stanford Junior University
23dnl
24dnl This file is free software; the authors give unlimited permission to copy
25dnl and/or distribute it, with or without modifications, as long as this
26dnl notice is preserved.
27
28dnl Save the current CPPFLAGS, LDFLAGS, and LIBS settings and switch to
29dnl versions that include the cURL flags.  Used as a wrapper, with
30dnl RRA_LIB_CURL_RESTORE, around tests.
31AC_DEFUN([RRA_LIB_CURL_SWITCH],
32[rra_curl_save_CPPFLAGS="$CPPFLAGS"
33 rra_curl_save_LDFLAGS="$LDFLAGS"
34 rra_curl_save_LIBS="$LIBS"
35 CPPFLAGS="$CURL_CPPFLAGS $CPPFLAGS"
36 LDFLAGS="$CURL_LDFLAGS $LDFLAGS"
37 LIBS="$CURL_LIBS $LIBS"])
38
39dnl Restore CPPFLAGS, LDFLAGS, and LIBS to their previous values (before
40dnl RRA_LIB_CURL_SWITCH was called).
41AC_DEFUN([RRA_LIB_CURL_RESTORE],
42[CPPFLAGS="$rra_curl_save_CPPFLAGS"
43 LDFLAGS="$rra_curl_save_LDFLAGS"
44 LIBS="$rra_curl_save_LIBS"])
45
46dnl Set CURL_CPPFLAGS and CURL_LDFLAGS based on rra_curl_root,
47dnl rra_curl_libdir, and rra_curl_includedir.
48AC_DEFUN([_RRA_LIB_CURL_PATHS],
49[AS_IF([test x"$rra_curl_libdir" != x],
50    [CURL_LDFLAGS="-L$rra_curl_libdir"],
51    [AS_IF([test x"$rra_curl_root" != x],
52        [RRA_SET_LDFLAGS([CURL_LDFLAGS], [$rra_curl_root])])])
53 AS_IF([test x"$rra_curl_includedir" != x],
54    [CURL_CPPFLAGS="-I$rra_curl_includedir"],
55    [AS_IF([test x"$rra_curl_root" != x],
56        [AS_IF([test x"$rra_curl_root" != x/usr],
57            [CURL_CPPFLAGS="-I${rra_curl_root}/include"])])])])
58
59dnl Does the appropriate library checks for reduced-dependency cURL linkage.
60AC_DEFUN([_RRA_LIB_CURL_REDUCED],
61[RRA_LIB_CURL_SWITCH
62 AC_CHECK_LIB([curl], [curl_easy_init], [CURL_LIBS="-lcurl"],
63    [AC_MSG_ERROR([cannot find usable cURL library])])
64 RRA_LIB_CURL_RESTORE])
65
66dnl Does the appropriate library checks for cURL linkage without curl-config
67dnl or reduced dependencies.
68AC_DEFUN([_RRA_LIB_CURL_MANUAL],
69[AC_REQUIRE([RRA_LIB_OPENSSL])
70 RRA_LIB_CURL_SWITCH
71 AC_CHECK_LIB([z], [inflate], [CURL_LIBS=-lz])
72 AC_CHECK_LIB([curl], [curl_easy_init],
73    [CURL_LDFLAGS="$CURL_LDFLAGS OPENSSL_LDFLAGS"
74     CURL_LIBS="-lcurl $CURL_LIBS $OPENSSL_LIBS"],
75    [AC_MSG_ERROR([cannot find usable cURL library])],
76    [$CURL_LIBS $OPENSSL_LDFLAGS $OPENSSL_LIBS])
77 RRA_LIB_CURL_RESTORE])
78
79dnl Sanity-check the results of curl-config and be sure we can really link a
80dnl cURL program.  If that fails, clear CURL_CPPFLAGS and CURL_LIBS so that we
81dnl know we don't have usable flags and fall back on the manual check.
82AC_DEFUN([_RRA_LIB_CURL_CHECK],
83[RRA_LIB_CURL_SWITCH
84 AC_CHECK_FUNC([curl_easy_init],
85    [RRA_LIB_CURL_RESTORE],
86    [RRA_LIB_CURL_RESTORE
87     CURL_CPPFLAGS=
88     CURL_LIBS=
89     _RRA_LIB_CURL_PATHS
90     AS_IF([test x"$rra_reduced_depends" = xtrue],
91         [_RRA_LIB_CURL_REDUCED],
92         [_RRA_LIB_CURL_MANUAL])])])
93
94dnl The main macro.
95AC_DEFUN([RRA_LIB_CURL],
96[AC_REQUIRE([AC_CANONICAL_HOST])
97 AC_REQUIRE([RRA_ENABLE_REDUCED_DEPENDS])
98 rra_curl_root=
99 rra_curl_libdir=
100 rra_curl_includedir=
101 CURL_CPPFLAGS=
102 CURL_LDFLAGS=
103 CURL_LIBS=
104 AC_SUBST([CURL_CPPFLAGS])
105 AC_SUBST([CURL_LDFLAGS])
106 AC_SUBST([CURL_LIBS])
107
108 AC_ARG_WITH([curl],
109    [AS_HELP_STRING([--with-curl=DIR],
110        [Location of cURL headers and libraries])],
111    [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
112        [rra_curl_root="$withval"])])
113 AC_ARG_WITH([curl-include],
114    [AS_HELP_STRING([--with-curl-include=DIR],
115        [Location of cURL headers])],
116    [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
117        [rra_curl_includedir="$withval"])])
118 AC_ARG_WITH([curl-lib],
119    [AS_HELP_STRING([--with-curl-lib=DIR],
120        [Location of cURL libraries])],
121    [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
122        [rra_curl_libdir="$withval"])])
123
124 AC_ARG_VAR([CURL_CONFIG], [Path to curl-config])
125 AS_IF([test x"$rra_curl_root" != x && test -z "$CURL_CONFIG"],
126    [AS_IF([test -x "${rra_curl_root}/bin/curl-config"],
127        [CURL_CONFIG="${rra_curl_root}/bin/curl-config"])],
128    [AC_PATH_PROG([CURL_CONFIG], [curl-config])])
129 AS_IF([test x"$CURL_CONFIG" != x && test -x "$CURL_CONFIG"],
130    [CURL_CPPFLAGS=`"$CURL_CONFIG" --cflags 2>/dev/null`
131     CURL_LIBS=`"$CURL_CONFIG" --libs 2>/dev/null`
132     CURL_CPPFLAGS=`echo "$CURL_CPPFLAGS" | sed 's%-I/usr/include ?%%'`
133     # Work around a bug in the Mac OS X curl-config script fixed in Tiger.
134     AS_CASE([$host],
135         [powerpc-apple-darwin7*],
136         [CURL_LIBS=`echo "$CURL_LIBS" | sed 's/-arch i386//g'`])
137     _RRA_LIB_CURL_CHECK],
138    [_RRA_LIB_CURL_PATHS
139     AS_IF([test x"$rra_reduced_depends" = xtrue],
140        [_RRA_LIB_CURL_REDUCED],
141        [_RRA_LIB_CURL_MANUAL])])])
142