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