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