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