1dnl Helper functions to manage compiler variables. 2dnl 3dnl These are a wide variety of helper macros to make it easier to construct 4dnl standard macros to probe for a library and to set library-specific 5dnl CPPFLAGS, LDFLAGS, and LIBS shell substitution variables. Most of them 6dnl take as one of the arguments the prefix string to use for variables, which 7dnl is usually something like "KRB5" or "GSSAPI". 8dnl 9dnl Depends on RRA_SET_LDFLAGS. 10dnl 11dnl The canonical version of this file is maintained in the rra-c-util 12dnl package, available at <https://www.eyrie.org/~eagle/software/rra-c-util/>. 13dnl 14dnl Written by Russ Allbery <eagle@eyrie.org> 15dnl Copyright 2018 Russ Allbery <eagle@eyrie.org> 16dnl Copyright 2011, 2013 17dnl The Board of Trustees of the Leland Stanford Junior University 18dnl 19dnl This file is free software; the authors give unlimited permission to copy 20dnl and/or distribute it, with or without modifications, as long as this 21dnl notice is preserved. 22dnl 23dnl SPDX-License-Identifier: FSFULLR 24 25dnl Add the library flags to the default compiler flags and then remove them. 26dnl 27dnl To use these macros, pass the prefix string used for the variables as the 28dnl only argument. For example, to use these for a library with KRB5 as a 29dnl prefix, one would use: 30dnl 31dnl AC_DEFUN([RRA_LIB_KRB5_SWITCH], [RRA_LIB_HELPER_SWITCH([KRB5])]) 32dnl AC_DEFUN([RRA_LIB_KRB5_RESTORE], [RRA_LIB_HELPER_RESTORE([KRB5])]) 33dnl 34dnl Then, wrap checks for library features with RRA_LIB_KRB5_SWITCH and 35dnl RRA_LIB_KRB5_RESTORE. 36AC_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 44AC_DEFUN([RRA_LIB_HELPER_RESTORE], 45[CPPFLAGS="$rra_$1[]_save_CPPFLAGS" 46 LDFLAGS="$rra_$1[]_save_LDFLAGS" 47 LIBS="$rra_$1[]_save_LIBS"]) 48 49dnl Given _root, _libdir, and _includedir variables set for a library (set by 50dnl RRA_LIB_HELPER_WITH*), set the LDFLAGS and CPPFLAGS variables for that 51dnl library accordingly. Takes the variable prefix as the only argument. 52AC_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 63dnl Check whether a library works. This is used as a sanity check on the 64dnl results of *-config shell scripts. Takes four arguments; the first, if 65dnl "true", says that a working library is mandatory and errors out if it 66dnl doesn't. The second is the variable prefix. The third is a function to 67dnl look for that should be in the libraries. The fourth is the 68dnl human-readable name of the library for error messages. 69AC_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 79dnl Initialize the variables used by a library probe and set the appropriate 80dnl ones as substitution variables. Takes the library variable prefix as its 81dnl only argument. 82AC_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 94dnl Unset all of the variables used by a library probe. Used with the 95dnl _OPTIONAL versions of header probes when a header or library wasn't found 96dnl and therefore the library isn't usable. 97AC_DEFUN([RRA_LIB_HELPER_VAR_CLEAR], 98[$1[]_CPPFLAGS= 99 $1[]_LDFLAGS= 100 $1[]_LIBS=]) 101 102dnl Handles --with options for a non-optional library. First argument is the 103dnl base for the switch names. Second argument is the short description. 104dnl Third argument is the variable prefix. The variables set are used by 105dnl RRA_LIB_HELPER_PATHS. 106AC_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 123dnl Handles --with options for an optional library, so --with-<library> can 124dnl cause the checks to be skipped entirely or become mandatory. Sets an 125dnl rra_use_PREFIX variable to true or false if the library is explicitly 126dnl enabled or disabled. 127dnl 128dnl First argument is the base for the switch names. Second argument is the 129dnl short description. Third argument is the variable prefix. 130dnl 131dnl The variables set are used by RRA_LIB_HELPER_PATHS. 132AC_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"])])]) 150