xref: /freebsd/sys/contrib/openzfs/config/find_system_library.m4 (revision b985c9cafd2aedac5cf92428c0211485ea4ede24)
1eda14cbcSMatt Macy# find_system_lib.m4 - Macros to search for a system library.   -*- Autoconf -*-
2eda14cbcSMatt Macy
3eda14cbcSMatt Macydnl requires pkg.m4 from pkg-config
4eda14cbcSMatt Macydnl requires ax_save_flags.m4 from autoconf-archive
5eda14cbcSMatt Macydnl requires ax_restore_flags.m4 from autoconf-archive
6eda14cbcSMatt Macy
7eda14cbcSMatt Macydnl ZFS_AC_FIND_SYSTEM_LIBRARY(VARIABLE-PREFIX, MODULE, HEADER, HEADER-PREFIXES, LIBRARY, FUNCTIONS, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
8eda14cbcSMatt Macy
9eda14cbcSMatt MacyAC_DEFUN([ZFS_AC_FIND_SYSTEM_LIBRARY], [
10eda14cbcSMatt Macy    AC_REQUIRE([PKG_PROG_PKG_CONFIG])
11eda14cbcSMatt Macy
12eda14cbcSMatt Macy    _header_found=
13eda14cbcSMatt Macy    _library_found=
142c48331dSMatt Macy    _pc_found=
15eda14cbcSMatt Macy
16eda14cbcSMatt Macy    AS_IF([test -n "$2"], [PKG_CHECK_MODULES([$1], [$2], [
17eda14cbcSMatt Macy	_header_found=1
18eda14cbcSMatt Macy	_library_found=1
192c48331dSMatt Macy	_pc_found=1
20eda14cbcSMatt Macy    ], [:])])
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy    # set _header_found/_library_found if the user passed in CFLAGS/LIBS
23eda14cbcSMatt Macy    AS_IF([test "x$[$1][_CFLAGS]" != x], [_header_found=1])
24eda14cbcSMatt Macy    AS_IF([test "x$[$1][_LIBS]" != x], [_library_found=1])
25eda14cbcSMatt Macy
26eda14cbcSMatt Macy    AX_SAVE_FLAGS
27eda14cbcSMatt Macy
28eda14cbcSMatt Macy    orig_CFLAGS="$CFLAGS"
29eda14cbcSMatt Macy
30eda14cbcSMatt Macy    for _prefixdir in /usr /usr/local
31eda14cbcSMatt Macy    do
32eda14cbcSMatt Macy	AS_VAR_PUSHDEF([header_cache], [ac_cv_header_$3])
33eda14cbcSMatt Macy	AS_IF([test "x$_prefixdir" != "x/usr"], [
34eda14cbcSMatt Macy	    [$1][_CFLAGS]="-I$lt_sysroot$_prefixdir/include"
35eda14cbcSMatt Macy	    AS_IF([test "x$_library_found" = x], [
36eda14cbcSMatt Macy		[$1][_LIBS]="-L$lt_sysroot$_prefixdir/lib"
37eda14cbcSMatt Macy	    ])
38eda14cbcSMatt Macy	])
39eda14cbcSMatt Macy	CFLAGS="$orig_CFLAGS $[$1][_CFLAGS]"
40eda14cbcSMatt Macy	AS_UNSET([header_cache])
41eda14cbcSMatt Macy	AC_CHECK_HEADER([$3], [
42eda14cbcSMatt Macy	    _header_found=1
43eda14cbcSMatt Macy	    break
44eda14cbcSMatt Macy	], [AS_IF([test "x$_header_found" = "x1"], [
45eda14cbcSMatt Macy	    # if pkg-config or the user set CFLAGS, fail if the header is unusable
46eda14cbcSMatt Macy	    AC_MSG_FAILURE([header [$3] for library [$5] is not usable])
47eda14cbcSMatt Macy	])], [AC_INCLUDES_DEFAULT])
48eda14cbcSMatt Macy	# search for header under HEADER-PREFIXES
49eda14cbcSMatt Macy	m4_foreach_w([prefix], [$4], [
50eda14cbcSMatt Macy	    [$1][_CFLAGS]=["-I$lt_sysroot$_prefixdir/include/]prefix["]
51eda14cbcSMatt Macy	    CFLAGS="$orig_CFLAGS $[$1][_CFLAGS]"
52eda14cbcSMatt Macy	    AS_UNSET([header_cache])
53eda14cbcSMatt Macy	    AC_CHECK_HEADER([$3], [
54eda14cbcSMatt Macy		_header_found=1
55eda14cbcSMatt Macy		break
56eda14cbcSMatt Macy	    ], [], [AC_INCLUDES_DEFAULT])
57eda14cbcSMatt Macy	])
58eda14cbcSMatt Macy	AS_VAR_POPDEF([header_cache])
59eda14cbcSMatt Macy    done
60eda14cbcSMatt Macy
61eda14cbcSMatt Macy    AS_IF([test "x$_header_found" = "x1"], [
62eda14cbcSMatt Macy	AS_IF([test "x$_library_found" = x], [
63eda14cbcSMatt Macy	    [$1][_LIBS]="$[$1]_LIBS -l[$5]"
64eda14cbcSMatt Macy	])
65eda14cbcSMatt Macy	LDFLAGS="$LDFLAGS $[$1][_LIBS]"
66eda14cbcSMatt Macy
67eda14cbcSMatt Macy	_libcheck=1
68eda14cbcSMatt Macy	m4_ifval([$6],
69eda14cbcSMatt Macy	    [m4_foreach_w([func], [$6], [AC_CHECK_LIB([$5], func, [:], [_libcheck=])])],
70eda14cbcSMatt Macy	    [AC_CHECK_LIB([$5], [main], [:], [_libcheck=])])
71eda14cbcSMatt Macy
72eda14cbcSMatt Macy	AS_IF([test "x$_libcheck" = "x1"], [_library_found=1],
73eda14cbcSMatt Macy	    [test "x$_library_found" = "x1"], [
74eda14cbcSMatt Macy	    # if pkg-config or the user set LIBS, fail if the library is unusable
75eda14cbcSMatt Macy	    AC_MSG_FAILURE([library [$5] is not usable])
76eda14cbcSMatt Macy	])
77eda14cbcSMatt Macy    ], [test "x$_library_found" = "x1"], [
78eda14cbcSMatt Macy	# if the user set LIBS, fail if we didn't find the header
79eda14cbcSMatt Macy	AC_MSG_FAILURE([cannot find header [$3] for library [$5]])
80eda14cbcSMatt Macy    ])
81eda14cbcSMatt Macy
82eda14cbcSMatt Macy    AX_RESTORE_FLAGS
83eda14cbcSMatt Macy
84eda14cbcSMatt Macy    AS_IF([test "x$_header_found" = "x1" && test "x$_library_found" = "x1"], [
85eda14cbcSMatt Macy	AC_SUBST([$1]_CFLAGS)
86eda14cbcSMatt Macy	AC_SUBST([$1]_LIBS)
872c48331dSMatt Macy	AS_IF([test "x$_pc_found" = "x1"], [
882c48331dSMatt Macy	    AC_SUBST([$1]_PC, [$2])
892c48331dSMatt Macy	])
90eda14cbcSMatt Macy	AC_DEFINE([HAVE_][$1], [1], [Define if you have [$5]])
91eda14cbcSMatt Macy	$7
92eda14cbcSMatt Macy    ],[dnl ELSE
93*b985c9caSMartin Matuska	AC_SUBST([$1]_CFLAGS, [""])
94*b985c9caSMartin Matuska	AC_SUBST([$1]_LIBS, [""])
95eda14cbcSMatt Macy	AC_MSG_WARN([cannot find [$5] via pkg-config or in the standard locations])
96eda14cbcSMatt Macy	$8
97eda14cbcSMatt Macy    ])
98eda14cbcSMatt Macy])
99