1dnl @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
26dnl @version 2007-09-12
27dnl @license AllPermissive
28dnl
29dnl Copyright (C) 2009 David Reiss
30dnl Copying and distribution of this file, with or without modification,
31dnl are permitted in any medium without royalty provided the copyright
32dnl notice and this notice are preserved.
33
34dnl Input: ax_zlib_path, WANT_ZLIB_VERSION
35dnl Output: success=yes/no
36AC_DEFUN([AX_LIB_ZLIB_DO_CHECK],
37         [
38          # Save our flags.
39          CPPFLAGS_SAVED="$CPPFLAGS"
40          LDFLAGS_SAVED="$LDFLAGS"
41          LIBS_SAVED="$LIBS"
42          LD_LIBRARY_PATH_SAVED="$LD_LIBRARY_PATH"
43
44          # Set our flags if we are checking a specific directory.
45          if test -n "$ax_zlib_path" ; then
46            ZLIB_CPPFLAGS="-I$ax_zlib_path/include"
47            ZLIB_LDFLAGS="-L$ax_zlib_path/lib"
48            LD_LIBRARY_PATH="$ax_zlib_path/lib:$LD_LIBRARY_PATH"
49          else
50            ZLIB_CPPFLAGS=""
51            ZLIB_LDFLAGS=""
52          fi
53
54          # Required flag for zlib.
55          ZLIB_LIBS="-lz"
56
57          # Prepare the environment for compilation.
58          CPPFLAGS="$CPPFLAGS $ZLIB_CPPFLAGS"
59          LDFLAGS="$LDFLAGS $ZLIB_LDFLAGS"
60          LIBS="$LIBS $ZLIB_LIBS"
61          export CPPFLAGS
62          export LDFLAGS
63          export LIBS
64          export LD_LIBRARY_PATH
65
66          success=no
67
68          # Compile, link, and run the program.  This checks:
69          # - zlib.h is available for including.
70          # - zlibVersion() is available for linking.
71          # - ZLIB_VERNUM is greater than or equal to the desired version.
72          # - ZLIB_VERSION (defined in zlib.h) matches zlibVersion()
73          #   (defined in the library).
74          AC_LANG_PUSH([C])
75          dnl This can be changed to AC_LINK_IFELSE if you are cross-compiling.
76          AC_LINK_IFELSE([AC_LANG_PROGRAM([[
77          #include <zlib.h>
78          #if ZLIB_VERNUM >= 0x$WANT_ZLIB_VERSION
79          #else
80          # error zlib is too old
81          #endif
82          ]], [[
83          const char* lib_version = zlibVersion();
84          const char* hdr_version = ZLIB_VERSION;
85          for (;;) {
86            if (*lib_version != *hdr_version) {
87              /* If this happens, your zlib header doesn't match your zlib */
88              /* library.  That is really bad. */
89              return 1;
90            }
91            if (*lib_version == '\0') {
92              break;
93            }
94            lib_version++;
95            hdr_version++;
96          }
97          return 0;
98          ]])], [
99          success=yes
100          ])
101          AC_LANG_POP([C])
102
103          # Restore flags.
104          CPPFLAGS="$CPPFLAGS_SAVED"
105          LDFLAGS="$LDFLAGS_SAVED"
106          LIBS="$LIBS_SAVED"
107          LD_LIBRARY_PATH="$LD_LIBRARY_PATH_SAVED"
108         ])
109
110
111AC_DEFUN([AX_LIB_ZLIB],
112         [
113
114          dnl Allow search path to be overridden on the command line.
115          AC_ARG_WITH([zlib],
116                      AS_HELP_STRING([--with-zlib@<:@=DIR@:>@], [use zlib (default is yes) - it is possible to specify an alternate root directory for zlib]),
117                      [
118                       if test "x$withval" = "xno"; then
119                         want_zlib="no"
120                       elif test "x$withval" = "xyes"; then
121                         want_zlib="yes"
122                         ax_zlib_path=""
123                       else
124                         want_zlib="yes"
125                         ax_zlib_path="$withval"
126                       fi
127                       ],
128                       [want_zlib="yes" ; ax_zlib_path="" ])
129
130
131          if test "$want_zlib" = "yes"; then
132            # Parse out the version.
133            zlib_version_req=ifelse([$1], ,1.2.3,$1)
134            zlib_version_req_major=`expr $zlib_version_req : '\([[0-9]]*\)'`
135            zlib_version_req_minor=`expr $zlib_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
136            zlib_version_req_patch=`expr $zlib_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
137            if test -z "$zlib_version_req_patch" ; then
138              zlib_version_req_patch="0"
139            fi
140            WANT_ZLIB_VERSION=`expr $zlib_version_req_major \* 1000 \+  $zlib_version_req_minor \* 100 \+ $zlib_version_req_patch \* 10`
141
142            AC_MSG_CHECKING(for zlib >= $zlib_version_req)
143
144            # Run tests.
145            if test -n "$ax_zlib_path"; then
146              AX_LIB_ZLIB_DO_CHECK
147            else
148              for ax_zlib_path in "" /usr /usr/local /opt /opt/zlib "$ZLIB_ROOT" ; do
149                AX_LIB_ZLIB_DO_CHECK
150                if test "$success" = "yes"; then
151                  break;
152                fi
153              done
154            fi
155
156            if test "$success" != "yes" ; then
157              AC_MSG_RESULT(no)
158              ZLIB_CPPFLAGS=""
159              ZLIB_LDFLAGS=""
160              ZLIB_LIBS=""
161            else
162              AC_MSG_RESULT(yes)
163              AC_DEFINE(HAVE_ZLIB,,[define if zlib is available])
164            fi
165
166            ax_have_zlib="$success"
167
168            AC_SUBST(ZLIB_CPPFLAGS)
169            AC_SUBST(ZLIB_LDFLAGS)
170            AC_SUBST(ZLIB_LIBS)
171          fi
172
173          ])
174