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