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