1 # Copyright © 2014, 2019, 2024 Nick Bowler
3 # Helper macros for implementing library tests.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 # DX_LIB_SETUP(env-base, [human-name])
20 # Helper to setup a library test. Defines two user variables FOO_CFLAGS
21 # and FOO_LIBS, where FOO is replaced with env-base in uppercase. The
22 # human-name is the name of the library used in help text, by default the
24 AC_DEFUN([DX_LIB_SETUP],
25 [AC_ARG_VAR(AS_TR_SH([m4_toupper([$1_CFLAGS])]),
26 [C compiler flags for ]m4_default([$2], [$1]))dnl
27 AC_ARG_VAR(AS_TR_SH([m4_toupper([$1_LIBS])]),
28 [Linker flags for ]m4_default([$2], [$1]))])
30 # DX_LIB_PKGCONFIG_FLAGS(env-base, [library-name])
32 # Helper for querying a library's CFLAGS and LIBS values from the
33 # pkg-config database. The results are stored in the cache variables
34 # dx_cv_foo_pkg_cflags and dx_cv_foo_pkg_libs, where foo is replaced with
35 # env-base in lowercase. If not specified, the library name defaults to
37 AC_DEFUN([DX_LIB_PKGCONFIG_FLAGS],
38 [_DX_LIB_PKGCONFIG_FLAGS(AS_TR_SH([m4_tolower([$1])]),
39 m4_default_quoted([$2], [$1]))])
41 AC_DEFUN([_DX_LIB_PKGCONFIG_FLAGS],
42 [AS_IF([test ${PKG_CONFIG:+y}],
43 [AC_CACHE_CHECK([pkg-config database for $2], [dx_cv_$1_pkg_found],
44 [DX_PKG_CONFIG([dx_cv_$1_pkg_cflags], [--cflags "$2"],
45 [DX_PKG_CONFIG([dx_cv_$1_pkg_libs], [--libs "$2"],
46 [dx_cv_$1_pkg_found=yes], [dx_cv_$1_pkg_found=no])],
47 [dx_cv_$1_pkg_found=no])])])])
49 # DX_LIB_SEARCH(env-base, test, test-matrix)
51 # Helper to search for the correct compiler/linker flags for a particular
52 # library. The test-matrix is an ordered, quoted list of quoted lists.
53 # Each entry in the test matrix has the following form:
55 # [cflags, libs, shell-condition]
57 # For each item, the specified cflags and libs are temporarily appended to
58 # CFLAGS and LIBS, respectively, then the given test is run. The test
59 # argument must be an m4 macro which takes one argument (action-if-ok).
60 # This macro shall expand to shell code which executes action-if-ok if and
61 # only if the test was successful. The test macro is expanded only once.
63 # If a test is successful, dx_cv_foo_lib_found is set to "yes", where foo is
64 # replaced with env-base in lowercase. Subsequent tests are not tried.
65 # The cflags and libs values from a successful test are stored in FOO_CFLAGS
66 # and FOO_LIBS, respectively, where FOO is replaced with env-base in
67 # uppercase. These values are AC_SUBST-ed. If no test was successful,
68 # dx_cv_foo_lib_found is set to "no".
70 # The result ("yes" or "no") is printed so this macro should be preceded
71 # by a call to AC_MSG_CHECKING.
72 AC_DEFUN([DX_LIB_SEARCH],
73 [_DX_LIB_SEARCH(m4_toupper([$1]),
74 AS_TR_SH([m4_tolower([dx_cv_$1_lib])]),
75 AS_TR_SH([m4_tolower([dx_fn_$1_lib])]),
78 # _DX_LIB_SEARCH(cache-base, function, env-base, test, test-matrix)
79 m4_define([_DX_LIB_SEARCH], [AC_CACHE_VAL([$2_found],
80 [m4_divert_push([INIT_PREPARE])dnl
82 CFLAGS="$_dx_save_cflags $[1]" LIBS="$_dx_save_libs $[2]"
83 $4([$2_found=yes $2_cflags=$][1 $2_libs=$][2])
84 test x"$$2_found" = x"yes"
86 m4_divert_pop([INIT_PREPARE])dnl
87 _dx_save_cflags=$CFLAGS _dx_save_libs=$LIBS
89 m4_map_args_sep([_DX_LIB_SEARCH_TEST([$3],m4_unquote(], [))
91 DX_VAR_NORMALIZE_SPACE([$2_cflags])
92 DX_VAR_NORMALIZE_SPACE([$2_libs])
93 CFLAGS=$_dx_save_cflags LIBS=$_dx_save_LIBS
95 AC_MSG_RESULT([$$2_found])
96 AS_CASE([$$2_found], [yes],
97 [AC_SUBST([$1_CFLAGS], [$$2_cflags]) AC_SUBST([$1_LIBS], [$$2_libs])])])
99 m4_define([_DX_LIB_SEARCH_TEST],
100 [ m4_ifnblank([$4], [$4 &&
101 ])$1 "$2" "$3" && break])
103 # DX_LIB_SEARCH_LINK(env-base, test-program, test-matrix)
105 # Convenience wrapper around DX_LIB_SEARCH which implements the test
106 # function by attempting to linking the provided test program.
107 AC_DEFUN([DX_LIB_SEARCH_LINK],
108 [DX_LIB_SEARCH([$1], [m4_curry([AC_LINK_IFELSE], [$2])], [$3])])
110 # DX_LIB_USERFLAG_BLURB(env-base, [human-name])
112 # Expand to text explaining the FOO_CFLAGS and FOO_LIBS environment
113 # variables, used in error messages, where FOO is replaced with env-base
114 # in uppercase. The human-name is the name of the library, by default the
116 AC_DEFUN([DX_LIB_USERFLAG_BLURB],
117 [_DX_LIB_USERFLAG_BLURB(m4_toupper([$1]), m4_default_quoted([$2], [$1]))])
119 # Internal helper macro for the above.
120 AC_DEFUN([_DX_LIB_USERFLAG_BLURB],
121 [If $2 is installed but was not detected by this configure script,
122 consider adjusting $1_CFLAGS and/or $1_LIBS as necessary.])
124 # DX_LIB_USERFLAG_BLURB(env-base, [human-name])
126 # Expand to text explaining the PKG_CONFIG_PATH environment variable,
127 # used in error messages. The human-name is the name of the library, by
128 # default the same as env-base.
129 AC_DEFUN([DX_LIB_PKGCONFIG_BLURB],
130 [_DX_LIB_PKGCONFIG_BLURB([$1], m4_default_quoted([$2], [$1]))])
132 dnl Internal helper macro for the above.
133 AC_DEFUN([_DX_LIB_PKGCONFIG_BLURB],
134 [If pkg-config is installed, it may help to adjust PKG_CONFIG_PATH if
135 $2 is installed in a non-standard prefix.])
137 # DX_LIB_COMPILE_ERROR([text])
139 # Expand to C code which should fail compilation. The given text should
140 # appear in any error message from the compiler, and should not contain
142 AC_DEFUN([DX_LIB_COMPILE_ERROR],
144 static int AS_TR_SH([$1])[[-1]]])