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