Upstream tarball 20080512
[amule.git] / m4 / zlib.m4
blob1da61a18574d188ed71b34401b1697ac98cd4abc
1 dnl ----------------------------------------------------
2 dnl AC_OPTIONS_ZLIB
3 dnl
4 dnl Add support for --with-zlib command-line parameter.
5 dnl PREFIX may be a directory prefix where zlib is installed, e.g. /usr/local
6 dnl or may be one of the following special keywords:
7 dnl    sys - use system zlib
8 dnl ----------------------------------------------------
9 AC_DEFUN([AC_OPTIONS_ZLIB],
11         AC_ARG_WITH(
12                 [zlib],
13                 AS_HELP_STRING(
14                         [--with-zlib=PREFIX],
15                         [use zlib in PREFIX]),
16                 [ac_zlib="$withval"],
17                 [ac_zlib=yes])
21 dnl ----------------------------------------------------
22 dnl AC_CHECK_ZLIB([MIN_ZLIB_VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
23 dnl check if zlib is on the system and is at least MIN_ZLIB_VERSION
24 dnl ----------------------------------------------------
25 AC_DEFUN([AC_CHECK_ZLIB],
26 [AC_REQUIRE([AC_OPTIONS_ZLIB])dnl
27 m4_define([ac_zver_max], [m4_bregexp([$1], [\([0-9]+\)\.\([0-9]+\)\.\([0-9]+\)], [\1])])dnl
28 m4_define([ac_zver_mid], [m4_bregexp([$1], [\([0-9]+\)\.\([0-9]+\)\.\([0-9]+\)], [\2])])dnl
29 m4_define([ac_zver_min], [m4_bregexp([$1], [\([0-9]+\)\.\([0-9]+\)\.\([0-9]+\)], [\3])])dnl
31         case "$ac_zlib" in
32         no)
33                 ifelse([$3], , :, [$3])
34                 ;;
35         yes | sys)
36                 ;;
37         *)
38                 ac_zlib_prefix="$ac_zlib"
39         esac
41         if test -n "$ac_zlib_prefix"; then
42                 ac_tmp_CPPFLAGS="$CPPFLAGS"
43                 ac_tmp_LDFLAGS="$LDFLAGS"
44                 CPPFLAGS="$CPPFLAGS -I$ac_zlib_prefix/include"
45                 LDFLAGS="-L$ac_zlib_prefix/lib"
46         fi
47         ac_tmp_LIBS="$LIBS"
48         LIBS="-lz $LIBS"
49         AC_MSG_CHECKING([for zlib >= $1])
50         AC_RUN_IFELSE([
51                 AC_LANG_PROGRAM([[
52                         #include <zlib.h>
53                         #include <stdio.h>
54                 ]], [dnl Do not use double-quoting here!
55                         char *zver = zlibVersion();
56                         FILE *f=fopen("conftestval", "w");
57                         if (!f) return 1;
58                         fprintf(f, "%s",
59                                 zver[[0]] > 'ac_zver_max' ||
60                                 (zver[[0]] == 'ac_zver_max' &&
61                                 (zver[[2]] > 'ac_zver_mid' ||
62                                 (zver[[2]] == 'ac_zver_mid' &&
63                                 zver[[4]] >= 'ac_zver_min'))) ? "yes" : "no");
64                         fclose(f);
65                         f=fopen("conftestver", "w");
66                         if (f) {
67                                 fprintf(f, "%s", ZLIB_VERSION);
68                                 fclose(f);
69                         }
70                 ])
71         ], [
72                 if test -f conftestval; then
73                         result=`cat conftestval`
74                 else
75                         result=no
76                 fi
77                 if test x$result = xyes; then
78                         if test -f conftestver; then
79                                 ZLIB_VERSION=`cat conftestver`
80                                 z_version=" (version $ZLIB_VERSION)"
81                         else
82                                 z_version=""
83                         fi
84                 fi
85                 AC_MSG_RESULT([$result$z_version])
86         ], [
87                 result=no
88                 AC_MSG_RESULT([$result])
89         ], [
90                 result=no
91                 z_version=''
92                 AC_LINK_IFELSE([
93                         AC_LANG_PROGRAM([[
94                                 #include <zlib.h>
95                                 #include <stdio.h>
96                         ]], [[
97                                 printf("\nZLIB_VERSION_START" ZLIB_VERSION "ZLIB_VERSION_END\n\n");
98                                 zlibVersion();
99                         ]])
100                 ], [
101                         ZLIB_VERSION=`grep -a '^ZLIB_VERSION_START.*ZLIB_VERSION_END$' conftest$ac_exeext | sed 's/^ZLIB_VERSION_START\(.*\)ZLIB_VERSION_END$/\1/'`
102                         ac_cross_zver_max="`echo $ZLIB_VERSION | cut -d. -f1`"
103                         ac_cross_zver_mid="`echo $ZLIB_VERSION | cut -d. -f2`"
104                         ac_cross_zver_min="`echo $ZLIB_VERSION | cut -d. -f3`"
105                         if test "$ac_cross_zver_max" -gt "ac_zver_max"; then
106                                 result=yes
107                         elif test "$ac_cross_zver_max" -eq "ac_zver_max"; then
108                                 if test "$ac_cross_zver_mid" -gt "ac_zver_mid"; then
109                                         result=yes
110                                 elif "$ac_cross_zver_mid" -eq "ac_zver_mid"; then
111                                         if test "$ac_cross_zver_min" -ge "ac_zver_min"; then
112                                                 result=yes
113                                         fi
114                                 fi
115                         fi
116                         if test x$result = xyes; then
117                                 z_version=" (version $ZLIB_VERSION)"
118                         fi
119                 ])
120                 AC_MSG_RESULT([$result$z_version])
121         ])
122         AS_IF([test x$result = xno], [
123                 if test "${ac_tmp_CPPFLAGS+set}" = set; then
124                         CPPFLAGS="$ac_tmp_CPPFLAGS"
125                 fi
126                 if test "${ac_tmp_LDFLAGS+set}" = set; then
127                         LDFLAGS="$ac_tmp_LDFLAGS"
128                 fi
129                 LIBS="$ac_tmp_LIBS"
130                 $3
131         ], [$2])
133 m4_undefine([ac_zver_max])dnl
134 m4_undefine([ac_zver_mid])dnl
135 m4_undefine([ac_zver_min])dnl