mirror_zfs/config/find_system_library.m4
Rob Norris 7ac00d3c26 find_system_library: fix var cleanup when library not found
The "not found" path is attempting to clear SOMELIB_CFLAGS and
SOMELIB_LIBS by resetting them in AC_SUBST(). However, the second arg to
AC_SUBST is expanded in autoconf with `m4_ifvaln([$2], [[$1]=$2])`,
which is defined as "if the first arg is non-empty". The m4 "empty"
construction is [], therefore, the existing AC_SUBST calls never modify
the variables at all.

The effect of this is that leftovers from the library test can leak out.
At least, if a library header is found in the first stage, but the
library itself is not, -lsomelib is added to SOMELIB_LIBS and further
tests done. If that library is not found, SOMELIB_LIBS will not be
cleared.

For most of our library tests this hasn't been a problem, as they're
either always found properly via pkg-config or set directly, or the
calling test immediately aborts configure. For an optional dependency
however, an apparent "partial" result where the header is found but no
corresponding library causes link errors later.

I think a complete fix should probably not be setting SOMELIB_xxx until
the final result is known, but for now, adjusting the AC_SUBST calls to
explictly set the empty shell string (which is not "empty" to m4) at
least restores the intent.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <robn@despairlabs.com>
Sponsored-by: https://despairlabs.com/sponsor/
Closes #16140
2024-05-01 10:51:14 -07:00

99 lines
2.9 KiB
Plaintext

# find_system_lib.m4 - Macros to search for a system library. -*- Autoconf -*-
dnl requires pkg.m4 from pkg-config
dnl requires ax_save_flags.m4 from autoconf-archive
dnl requires ax_restore_flags.m4 from autoconf-archive
dnl ZFS_AC_FIND_SYSTEM_LIBRARY(VARIABLE-PREFIX, MODULE, HEADER, HEADER-PREFIXES, LIBRARY, FUNCTIONS, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
AC_DEFUN([ZFS_AC_FIND_SYSTEM_LIBRARY], [
AC_REQUIRE([PKG_PROG_PKG_CONFIG])
_header_found=
_library_found=
_pc_found=
AS_IF([test -n "$2"], [PKG_CHECK_MODULES([$1], [$2], [
_header_found=1
_library_found=1
_pc_found=1
], [:])])
# set _header_found/_library_found if the user passed in CFLAGS/LIBS
AS_IF([test "x$[$1][_CFLAGS]" != x], [_header_found=1])
AS_IF([test "x$[$1][_LIBS]" != x], [_library_found=1])
AX_SAVE_FLAGS
orig_CFLAGS="$CFLAGS"
for _prefixdir in /usr /usr/local
do
AS_VAR_PUSHDEF([header_cache], [ac_cv_header_$3])
AS_IF([test "x$_prefixdir" != "x/usr"], [
[$1][_CFLAGS]="-I$lt_sysroot$_prefixdir/include"
AS_IF([test "x$_library_found" = x], [
[$1][_LIBS]="-L$lt_sysroot$_prefixdir/lib"
])
])
CFLAGS="$orig_CFLAGS $[$1][_CFLAGS]"
AS_UNSET([header_cache])
AC_CHECK_HEADER([$3], [
_header_found=1
break
], [AS_IF([test "x$_header_found" = "x1"], [
# if pkg-config or the user set CFLAGS, fail if the header is unusable
AC_MSG_FAILURE([header [$3] for library [$5] is not usable])
])], [AC_INCLUDES_DEFAULT])
# search for header under HEADER-PREFIXES
m4_foreach_w([prefix], [$4], [
[$1][_CFLAGS]=["-I$lt_sysroot$_prefixdir/include/]prefix["]
CFLAGS="$orig_CFLAGS $[$1][_CFLAGS]"
AS_UNSET([header_cache])
AC_CHECK_HEADER([$3], [
_header_found=1
break
], [], [AC_INCLUDES_DEFAULT])
])
AS_VAR_POPDEF([header_cache])
done
AS_IF([test "x$_header_found" = "x1"], [
AS_IF([test "x$_library_found" = x], [
[$1][_LIBS]="$[$1]_LIBS -l[$5]"
])
LDFLAGS="$LDFLAGS $[$1][_LIBS]"
_libcheck=1
m4_ifval([$6],
[m4_foreach_w([func], [$6], [AC_CHECK_LIB([$5], func, [:], [_libcheck=])])],
[AC_CHECK_LIB([$5], [main], [:], [_libcheck=])])
AS_IF([test "x$_libcheck" = "x1"], [_library_found=1],
[test "x$_library_found" = "x1"], [
# if pkg-config or the user set LIBS, fail if the library is unusable
AC_MSG_FAILURE([library [$5] is not usable])
])
], [test "x$_library_found" = "x1"], [
# if the user set LIBS, fail if we didn't find the header
AC_MSG_FAILURE([cannot find header [$3] for library [$5]])
])
AX_RESTORE_FLAGS
AS_IF([test "x$_header_found" = "x1" && test "x$_library_found" = "x1"], [
AC_SUBST([$1]_CFLAGS)
AC_SUBST([$1]_LIBS)
AS_IF([test "x$_pc_found" = "x1"], [
AC_SUBST([$1]_PC, [$2])
])
AC_DEFINE([HAVE_][$1], [1], [Define if you have [$5]])
$7
],[dnl ELSE
AC_SUBST([$1]_CFLAGS, [""])
AC_SUBST([$1]_LIBS, [""])
AC_MSG_WARN([cannot find [$5] via pkg-config or in the standard locations])
$8
])
])