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