Detect libpng.
[shapes.git] / m4 / libpng.m4
blob17d1bba29e7f3b9fa70578b8539fc760b9157d0b
1 dnl Check to find the zlib headers/libraries
3 AC_DEFUN([AM_PATH_LIBPNG],
5   AC_ARG_WITH(libpng-exec-prefix,[  --with-libpng-exec-prefix=PFX Exec prefix where libpng-config is installed (optional)],
6               libpng_exec_prefix="$withval", libpng_exec_prefix="")
8   if test "x${LIBPNG_CONFIG+set}" != xset ; then
9      if test "x$libpng_exec_prefix" != x ; then
10         LIBPNG_CONFIG="$libpng_exec_prefix/bin/libpng-config"
11      fi
12   fi
14   AC_PATH_PROG(LIBPNG_CONFIG, libpng-config, no)
15   min_libpng_version=ifelse([$1], ,1.2.12,$1)
16   AC_MSG_CHECKING(for libpng - version >= $min_libpng_version)
17   no_libpng=""
18   if test "$LIBPNG_CONFIG" = "no" ; then
19     no_libpng=yes
20   else
21     LIBPNG_CFLAGS=`$LIBPNG_CONFIG --cflags`
22     LIBPNG_LIBS=`$LIBPNG_CONFIG --libs`
24     libpng_major_version=`$LIBPNG_CONFIG --version | \
25            sed 's/^\([[0-9]]*\).*/\1/'`
26     if test "x${libpng_major_version}" = "x" ; then
27        libpng_major_version=0
28     fi
30     libpng_minor_version=`$LIBPNG_CONFIG --version | \
31            sed 's/^\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\).*/\2/'`
32     if test "x${libpng_minor_version}" = "x" ; then
33        libpng_minor_version=0
34     fi
36     libpng_micro_version=`$LIBPNG_CONFIG --version | \
37            sed 's/^\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\).*/\3/'`
38     if test "x${libpng_micro_version}" = "x" ; then
39        libpng_micro_version=0
40     fi
42     ac_save_CFLAGS="$CFLAGS"
43     ac_save_LIBS="$LIBS"
44     CFLAGS="$CFLAGS $LIBPNG_CFLAGS"
45     LIBS="$LIBS $LIBPNG_LIBS"
47     rm -f conf.libpngtest
48     AC_TRY_RUN([
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
53 char* my_strdup (const char *str);
55 char*
56 my_strdup (const char *str)
58   char *new_str;
60   if (str)
61     {
62       new_str = (char *)malloc ((strlen (str) + 1) * sizeof(char));
63       strcpy (new_str, str);
64     }
65   else
66     new_str = NULL;
68   return new_str;
71 int main (void)
73   int major = 0, minor = 0, micro = 0;
74   int n;
75   char *tmp_version;
77   system ("touch conf.libpngtest");
79   /* HP/UX 9 (%@#!) writes to sscanf strings */
80   tmp_version = my_strdup("$min_libpng_version");
82   n = sscanf(tmp_version, "%d.%d.%d", &major, &minor, &micro) ;
84   if (n != 2 && n != 3) {
85      printf("%s, bad version string\n", "$min_libpng_version");
86      exit(1);
87    }
89    if (($libpng_major_version > major) ||
90       (($libpng_major_version == major) && ($libpng_minor_version > minor)) ||
91       (($libpng_major_version == major) && ($libpng_minor_version == minor) && ($libpng_micro_version >= micro)))
92     {
93       exit(0);
94     }
95   else
96     {
97       printf("\n*** 'libpng-config --version' returned %d.%d.%d, but the minimum version\n", $libpng_major_version, $libpng_minor_version, $libpng_micro_version);
98       printf("*** of libpng required is %d.%d.%d. If libpng-config is correct, then it is\n", major, minor, micro);
99       printf("*** best to upgrade to the required version.\n");
100       printf("*** If libpng-config was wrong, set the environment variable LIBPNG_CONFIG\n");
101       printf("*** to point to the correct copy of libpng-config, and remove the file\n");
102       printf("*** config.cache before re-running configure\n");
103       exit(1);
104     }
106 ],, no_libpng=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"])
107        CFLAGS="$ac_save_CFLAGS"
108        LIBS="$ac_save_LIBS"
109   fi
110   if test "x$no_libpng" = x ; then
111      AC_DEFINE_UNQUOTED(HAVE_LIBPNG, true, [Define to true if libpng is installed])
112                  HAVE_LIBPNG=true
113      AC_MSG_RESULT(yes)
114      ifelse([$2], , :, [$2])
115   else
116      AC_MSG_RESULT(no)
117                  HAVE_LIBPNG=false
118      if test "$LIBPNG_CONFIG" = "no" ; then
119        echo "*** The libpng-config script installed with libpng could not be found"
120        echo "*** If libpng was installed in PREFIX, make sure PREFIX/bin is in your path,"
121        echo "*** or use the --with-libpng-exec-prefix=PFX option to configure."
122        echo "*** You may also set the LIBPNG_CONFIG environment variable to the full path"
123        echo "*** to libpng-config."
124      else
125        if test -f conf.libpngtest ; then
126         :
127        else
128           echo "*** Could not run libpng test program, checking why..."
129           CFLAGS="$CFLAGS $LIBPNG_CFLAGS"
130           LIBS="$LIBS $LIBPNG_LIBS"
131           AC_TRY_LINK([
132 #include <stdio.h>
133 ],      [ return 0; ],
134         [ echo "*** The test program compiled, but did not run. This usually means"
135           echo "*** that the run-time linker is not finding libpng or finding the wrong"
136           echo "*** version of libpng. If it is not finding libpng, you'll need to set your"
137           echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
138           echo "*** to the installed location  Also, make sure you have run ldconfig if that"
139           echo "*** is required on your system"
140           echo "***"
141           echo "*** If you have an old version installed, it is best to remove it, although"
142           echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH"],
143         [ echo "*** The test program failed to compile or link. See the file config.log for the"
144           echo "*** exact error that occured. This usually means libpng was incorrectly installed"
145           echo "*** or that you have moved libpng since it was installed. In the latter case, you"
146           echo "*** may want to edit the libpng-config script: $LIBPNG_CONFIG" ])
147           CFLAGS="$ac_save_CFLAGS"
148           LIBS="$ac_save_LIBS"
149        fi
150      fi
151 #     LIBPNG_CFLAGS=""
152 #     LIBPNG_LIBS=""
153      ifelse([$3], , :, [$3])
154   fi
155   AC_SUBST(HAVE_LIBPNG)
156   AC_SUBST(LIBPNG_CFLAGS)
157   AC_SUBST(LIBPNG_LIBS)
158   rm -f conf.libpngtest