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