blob: be4b6bbf2431853630a10dcc38df4d32c1b9646d [file] [log] [blame]
David Reissfaebedd2007-09-17 23:20:38 +00001dnl @synopsis AX_LIB_ZLIB([MINIMUM-VERSION])
2dnl
3dnl Test for the libz library of a particular version (or newer).
4dnl
5dnl If no path to the installed zlib is given, the macro will first try
6dnl using no -I or -L flags, then searches under /usr, /usr/local, /opt,
7dnl and /opt/zlib.
8dnl If these all fail, it will try the $ZLIB_ROOT environment variable.
9dnl
10dnl This macro calls:
11dnl AC_SUBST(ZLIB_CPPFLAGS)
12dnl AC_SUBST(ZLIB_LDFLAGS)
13dnl AC_SUBST(ZLIB_LIBS)
14dnl
15dnl And (if zlib is found):
16dnl AC_DEFINE(HAVE_ZLIB)
17dnl
18dnl It also leaves the shell variables "success" and "ax_have_zlib"
19dnl set to "yes" or "no".
20dnl
21dnl NOTE: This macro does not currently work for cross-compiling,
22dnl but it can be easily modified to allow it. (grep "cross").
23dnl
24dnl @category InstalledPackages
25dnl @category C
David Reissfaebedd2007-09-17 23:20:38 +000026dnl @version 2007-09-12
27dnl @license AllPermissive
28
29dnl Input: ax_zlib_path, WANT_ZLIB_VERSION
30dnl Output: success=yes/no
31AC_DEFUN([AX_LIB_ZLIB_DO_CHECK],
32 [
33 # Save our flags.
34 CPPFLAGS_SAVED="$CPPFLAGS"
35 LDFLAGS_SAVED="$LDFLAGS"
36 LIBS_SAVED="$LIBS"
37 LD_LIBRARY_PATH_SAVED="$LD_LIBRARY_PATH"
38
39 # Set our flags if we are checking a specific directory.
40 if test -n "$ax_zlib_path" ; then
41 ZLIB_CPPFLAGS="-I$ax_zlib_path/include"
42 ZLIB_LDFLAGS="-L$ax_zlib_path/lib"
43 LD_LIBRARY_PATH="$ax_zlib_path/lib:$LD_LIBRARY_PATH"
44 else
45 ZLIB_CPPFLAGS=""
46 ZLIB_LDFLAGS=""
47 fi
48
49 # Required flag for zlib.
50 ZLIB_LIBS="-lz"
51
52 # Prepare the environment for compilation.
53 CPPFLAGS="$CPPFLAGS $ZLIB_CPPFLAGS"
54 LDFLAGS="$LDFLAGS $ZLIB_LDFLAGS"
55 LIBS="$LIBS $ZLIB_LIBS"
56 export CPPFLAGS
57 export LDFLAGS
58 export LIBS
59 export LD_LIBRARY_PATH
60
61 success=no
62
63 # Compile, link, and run the program. This checks:
64 # - zlib.h is available for including.
65 # - zlibVersion() is available for linking.
66 # - ZLIB_VERNUM is greater than or equal to the desired version.
67 # - ZLIB_VERSION (defined in zlib.h) matches zlibVersion()
68 # (defined in the library).
69 AC_LANG_PUSH([C])
70 dnl This can be changed to AC_LINK_IFELSE if you are cross-compiling.
71 AC_RUN_IFELSE([AC_LANG_PROGRAM([[
72 #include <zlib.h>
73 #if ZLIB_VERNUM >= 0x$WANT_ZLIB_VERSION
74 #else
75 # error zlib is too old
76 #endif
77 ]], [[
78 const char* lib_version = zlibVersion();
79 const char* hdr_version = ZLIB_VERSION;
80 for (;;) {
81 if (*lib_version != *hdr_version) {
82 /* If this happens, your zlib header doesn't match your zlib */
83 /* library. That is really bad. */
84 return 1;
85 }
86 if (*lib_version == '\0') {
87 break;
88 }
89 lib_version++;
90 hdr_version++;
91 }
92 return 0;
93 ]])], [
94 success=yes
95 ])
96 AC_LANG_POP([C])
97
98 # Restore flags.
99 CPPFLAGS="$CPPFLAGS_SAVED"
100 LDFLAGS="$LDFLAGS_SAVED"
101 LIBS="$LIBS_SAVED"
102 LD_LIBRARY_PATH="$LD_LIBRARY_PATH_SAVED"
103 ])
104
105
106AC_DEFUN([AX_LIB_ZLIB],
107 [
108
109 dnl Allow search path to be overridden on the command line.
110 AC_ARG_WITH([zlib],
111 AS_HELP_STRING([--with-zlib@<:@=DIR@:>@], [use zlib (default is yes) - it is possible to specify an alternate root directory for zlib]),
112 [
David Reiss19d3d0a2009-03-13 21:25:31 +0000113 if test "x$withval" = "xno"; then
David Reissfaebedd2007-09-17 23:20:38 +0000114 want_zlib="no"
David Reiss19d3d0a2009-03-13 21:25:31 +0000115 elif test "x$withval" = "xyes"; then
David Reissfaebedd2007-09-17 23:20:38 +0000116 want_zlib="yes"
117 ax_zlib_path=""
118 else
119 want_zlib="yes"
120 ax_zlib_path="$withval"
121 fi
122 ],
123 [want_zlib="yes" ; ax_zlib_path="" ])
124
125
126 if test "$want_zlib" = "yes"; then
127 # Parse out the version.
128 zlib_version_req=ifelse([$1], ,1.2.3,$1)
129 zlib_version_req_major=`expr $zlib_version_req : '\([[0-9]]*\)'`
130 zlib_version_req_minor=`expr $zlib_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
131 zlib_version_req_patch=`expr $zlib_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
132 if test -z "$zlib_version_req_patch" ; then
133 zlib_version_req_patch="0"
134 fi
135 WANT_ZLIB_VERSION=`expr $zlib_version_req_major \* 1000 \+ $zlib_version_req_minor \* 100 \+ $zlib_version_req_patch \* 10`
136
137 AC_MSG_CHECKING(for zlib >= $zlib_version_req)
138
139 # Run tests.
140 if test -n "$ax_zlib_path"; then
141 AX_LIB_ZLIB_DO_CHECK
142 else
143 for ax_zlib_path in "" /usr /usr/local /opt /opt/zlib "$ZLIB_ROOT" ; do
144 AX_LIB_ZLIB_DO_CHECK
145 if test "$success" = "yes"; then
146 break;
147 fi
148 done
149 fi
150
151 if test "$success" != "yes" ; then
152 AC_MSG_RESULT(no)
153 ZLIB_CPPFLAGS=""
154 ZLIB_LDFLAGS=""
155 ZLIB_LIBS=""
156 else
157 AC_MSG_RESULT(yes)
158 AC_DEFINE(HAVE_ZLIB,,[define if zlib is available])
159 fi
160
161 ax_have_zlib="$success"
162
163 AC_SUBST(ZLIB_CPPFLAGS)
164 AC_SUBST(ZLIB_LDFLAGS)
165 AC_SUBST(ZLIB_LIBS)
166 fi
167
168 ])