1 dnl Helper functions to manage compiler variables.
3 dnl These are a wide variety of helper macros to make it easier to construct
4 dnl standard macros to probe for a library and to set library-specific
5 dnl CPPFLAGS, LDFLAGS, and LIBS shell substitution variables. Most of them
6 dnl take as one of the arguments the prefix string to use for variables, which
7 dnl is usually something like "KRB5" or "GSSAPI".
9 dnl Depends on RRA_SET_LDFLAGS.
11 dnl The canonical version of this file is maintained in the rra-c-util
12 dnl package, available at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
14 dnl Written by Russ Allbery <eagle@eyrie.org>
15 dnl Copyright 2011, 2013
16 dnl The Board of Trustees of the Leland Stanford Junior University
18 dnl This file is free software; the authors give unlimited permission to copy
19 dnl and/or distribute it, with or without modifications, as long as this
20 dnl notice is preserved.
22 dnl Add the library flags to the default compiler flags and then remove them.
24 dnl To use these macros, pass the prefix string used for the variables as the
25 dnl only argument. For example, to use these for a library with KRB5 as a
26 dnl prefix, one would use:
28 dnl AC_DEFUN([RRA_LIB_KRB5_SWITCH], [RRA_LIB_HELPER_SWITCH([KRB5])])
29 dnl AC_DEFUN([RRA_LIB_KRB5_RESTORE], [RRA_LIB_HELPER_RESTORE([KRB5])])
31 dnl Then, wrap checks for library features with RRA_LIB_KRB5_SWITCH and
32 dnl RRA_LIB_KRB5_RESTORE.
33 AC_DEFUN([RRA_LIB_HELPER_SWITCH],
34 [rra_$1[]_save_CPPFLAGS="$CPPFLAGS"
35 rra_$1[]_save_LDFLAGS="$LDFLAGS"
36 rra_$1[]_save_LIBS="$LIBS"
37 CPPFLAGS="$$1[]_CPPFLAGS $CPPFLAGS"
38 LDFLAGS="$$1[]_LDFLAGS $LDFLAGS"
39 LIBS="$$1[]_LIBS $LIBS"])
41 AC_DEFUN([RRA_LIB_HELPER_RESTORE],
42 [CPPFLAGS="$rra_$1[]_save_CPPFLAGS"
43 LDFLAGS="$rra_$1[]_save_LDFLAGS"
44 LIBS="$rra_$1[]_save_LIBS"])
46 dnl Given _root, _libdir, and _includedir variables set for a library (set by
47 dnl RRA_LIB_HELPER_WITH*), set the LDFLAGS and CPPFLAGS variables for that
48 dnl library accordingly. Takes the variable prefix as the only argument.
49 AC_DEFUN([RRA_LIB_HELPER_PATHS],
50 [AS_IF([test x"$rra_$1[]_libdir" != x],
51 [$1[]_LDFLAGS="-L$rra_$1[]_libdir"],
52 [AS_IF([test x"$rra_$1[]_root" != x],
53 [RRA_SET_LDFLAGS([$1][_LDFLAGS], [${rra_$1[]_root}])])])
54 AS_IF([test x"$rra_$1[]_includedir" != x],
55 [$1[]_CPPFLAGS="-I$rra_$1[]_includedir"],
56 [AS_IF([test x"$rra_$1[]_root" != x],
57 [AS_IF([test x"$rra_$1[]_root" != x/usr],
58 [$1[]_CPPFLAGS="-I${rra_$1[]_root}/include"])])])])
60 dnl Check whether a library works. This is used as a sanity check on the
61 dnl results of *-config shell scripts. Takes four arguments; the first, if
62 dnl "true", says that a working library is mandatory and errors out if it
63 dnl doesn't. The second is the variable prefix. The third is a function to
64 dnl look for that should be in the libraries. The fourth is the
65 dnl human-readable name of the library for error messages.
66 AC_DEFUN([RRA_LIB_HELPER_CHECK],
67 [RRA_LIB_HELPER_SWITCH([$2])
68 AC_CHECK_FUNC([$3], [],
69 [AS_IF([test x"$1" = xtrue],
70 [AC_MSG_FAILURE([unable to link with $4 library])])
74 RRA_LIB_HELPER_RESTORE([$2])])
76 dnl Initialize the variables used by a library probe and set the appropriate
77 dnl ones as substitution variables. Takes the library variable prefix as its
79 AC_DEFUN([RRA_LIB_HELPER_VAR_INIT],
87 AC_SUBST([$1][_CPPFLAGS])
88 AC_SUBST([$1][_LDFLAGS])
89 AC_SUBST([$1][_LIBS])])
91 dnl Handles --with options for a non-optional library. First argument is the
92 dnl base for the switch names. Second argument is the short description.
93 dnl Third argument is the variable prefix. The variables set are used by
94 dnl RRA_LIB_HELPER_PATHS.
95 AC_DEFUN([RRA_LIB_HELPER_WITH],
97 [AS_HELP_STRING([--with-][$1][=DIR],
98 [Location of $2 headers and libraries])],
99 [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
100 [rra_$3[]_root="$withval"])])
101 AC_ARG_WITH([$1][-include],
102 [AS_HELP_STRING([--with-][$1][-include=DIR],
103 [Location of $2 headers])],
104 [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
105 [rra_$3[]_includedir="$withval"])])
106 AC_ARG_WITH([$1][-lib],
107 [AS_HELP_STRING([--with-][$1][-lib=DIR],
108 [Location of $2 libraries])],
109 [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
110 [rra_$3[]_libdir="$withval"])])])
112 dnl Handles --with options for an optional library, so --with-<library> can
113 dnl cause the checks to be skipped entirely or become mandatory. Sets an
114 dnl rra_use_PREFIX variable to true or false if the library is explicitly
115 dnl enabled or disabled.
117 dnl First argument is the base for the switch names. Second argument is the
118 dnl short description. Third argument is the variable prefix.
120 dnl The variables set are used by RRA_LIB_HELPER_PATHS.
121 AC_DEFUN([RRA_LIB_HELPER_WITH_OPTIONAL],
123 [AS_HELP_STRING([--with-][$1][@<:@=DIR@:>@],
124 [Location of $2 headers and libraries])],
125 [AS_IF([test x"$withval" = xno],
127 [AS_IF([test x"$withval" != xyes], [rra_$3[]_root="$withval"])
129 AC_ARG_WITH([$1][-include],
130 [AS_HELP_STRING([--with-][$1][-include=DIR],
131 [Location of $2 headers])],
132 [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
133 [rra_$3[]_includedir="$withval"])])
134 AC_ARG_WITH([$1][-lib],
135 [AS_HELP_STRING([--with-][$1][-lib=DIR],
136 [Location of $2 libraries])],
137 [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
138 [rra_$3[]_libdir="$withval"])])])