First cut at a glib2 patch

Joe Shaw <[email protected]> 03 May 2002 16:14:34 -0400
Newsgroups gmane.comp.gnome.ximian.soup
Message-ID <1020456874.28767.14.camel@caedes>
Hi,

Attached is a patch against the soup-0-7 branch which will make it build
against glib 2.0.  This is just a first cut for now; open issues:

    * Very little testing.  I haven't tested it aside from some of the
    small test programs that are built.  I did test the SSL stuff
    though, and it seems to work fine.
    
    * The error reporting in soup-ssl-proxy is unfinished.  All these
    great new GError reporting mechanisms and I'm using none of them.
    
    * The libraries should probably be installed with different names so
    as to distinguish between a Soup built against glib 1.2 and one
    built against 2.0.  *It doesn't do this now*
    
    * Need to make a pkg-config file.

It can be turned on at configure time by passing in the --enable-glib2
flag.  Assuming everything looks okay, I'd like to commit it and then
finish up the loose ends.

Oh, one caveat: it uses both glib 1.2 and glib 2.0 m4 macros, so if you
don't have both of the devel packages installed, you'll need the
attached acinclude.m4 file in the toplevel source directory.  I'd commit
that as well.

Joe
soup-glib2.patch (text/plain, 9.4 KB)
? acinclude.m4
? configure.in.new
? soup-glib2.patch
Index: acconfig.h
===================================================================
RCS file: /cvs/gnome/soup/acconfig.h,v
retrieving revision 1.4.6.1
diff -u -p -u -r1.4.6.1 acconfig.h
--- acconfig.h	8 Jan 2002 05:16:06 -0000	1.4.6.1
+++ acconfig.h	3 May 2002 20:10:28 -0000
@@ -4,6 +4,8 @@
 #undef SOUP_INTERFACE_AGE
 #undef SOUP_BINARY_AGE
 
+#undef SOUP_GLIB2
+
 #undef PACKAGE_SOURCE_DIR
 
 #undef HAVE_OPENSSL
Index: configure.in
===================================================================
RCS file: /cvs/gnome/soup/configure.in,v
retrieving revision 1.55.2.5.2.5
diff -u -p -u -r1.55.2.5.2.5 configure.in
--- configure.in	29 Apr 2002 23:37:54 -0000	1.55.2.5.2.5
+++ configure.in	3 May 2002 20:10:29 -0000
@@ -66,17 +66,31 @@ AM_PROG_LIBTOOL
 # This isn't a program, but it doesn't fit anywhere else...
 AC_FUNC_ALLOCA
 
+dnl ***********************
+dnl *** Checks for glib ***
+dnl ***********************
 
-dnl ***************************
-dnl *** Checks for glib 1.2 ***
-dnl ***************************
-
-AM_PATH_GLIB(1.2.0,,
-             AC_MSG_ERROR([Cannot find GLIB: Is glib-config in path?]))
-
-GLIB_CFLAGS=`glib-config --cflags glib`
-GLIB_LIBS=`glib-config --libs glib`
-GMODULE_LIBS=`glib-config --libs gmodule`
+AC_MSG_CHECKING(whether to build against glib 2.0)
+AC_ARG_ENABLE(glib2, [\
+  --enable-glib2  Build Soup against glib 2.0], ,
+enable_glib2=no)
+AC_MSG_RESULT($enable_glib2)
+
+if test x"$enable_glib2" != xno; then
+	AM_PATH_GLIB_2_0(2.0.0,, AC_MSG_ERROR([Cannot find glib 2.0]))
+	GLIB_CFLAGS=`$PKG_CONFIG --cflags glib-2.0`
+	GLIB_LIBS=`$PKG_CONFIG --libs glib-2.0`
+	GMODULE_LIBS=`$PKG_CONFIG --libs gmodule-2.0`
+	FAVOUR_LIBXML=2
+	AC_DEFINE(SOUP_GLIB2)
+else
+	AM_PATH_GLIB(1.2.0,,
+		AC_MSG_ERROR([Cannot find GLIB: Is glib-config in path?]))
+	GLIB_CFLAGS=`glib-config --cflags glib`
+	GLIB_LIBS=`glib-config --libs glib`
+	GMODULE_LIBS=`glib-config --libs gmodule`
+	FAVOUR_LIBXML=1
+fi
 
 AC_SUBST(GLIB_CFLAGS)
 AC_SUBST(GLIB_LIBS)
@@ -87,7 +101,6 @@ dnl ****************************
 dnl *** Checks for gnome-xml ***
 dnl ****************************
 
-FAVOUR_LIBXML=1
 AC_ARG_WITH(libxml,
 	[  --with-libxml=[1/2] which version of libxml to use [default=$FAVOUR_LIBXML]],,
 	with_libxml=$FAVOUR_LIBXML)
@@ -567,6 +580,8 @@ Configuration:
   Source code location:		${srcdir}
   Compiler:			${CC}
   Build flags:			${CFLAGS} ${SOUP_DEBUG_FLAGS}
+
+  glib 2.0:                     ${enable_glib2}
 
   OpenSSL support:		${enable_openssl}
   Mozilla NSS support:		${enable_nss}
Index: src/soup-ssl-proxy/soup-openssl.c
===================================================================
RCS file: /cvs/gnome/soup/src/soup-ssl-proxy/soup-openssl.c,v
retrieving revision 1.5.6.4.2.4
diff -u -p -u -r1.5.6.4.2.4 soup-openssl.c
--- src/soup-ssl-proxy/soup-openssl.c	30 Apr 2002 16:24:22 -0000	1.5.6.4.2.4
+++ src/soup-ssl-proxy/soup-openssl.c	3 May 2002 20:10:29 -0000
@@ -34,6 +34,16 @@ typedef struct {
 	SSL         *ssl;
 } SoupOpenSSLChannel;
 
+static void
+soup_openssl_free (GIOChannel *channel)
+{
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
+	g_io_channel_unref (chan->real_sock);
+	SSL_free (chan->ssl);
+	g_free (chan);
+}
+
+#ifndef SOUP_GLIB2
 static GIOError
 soup_openssl_read (GIOChannel   *channel,
 		   gchar        *buf,
@@ -109,15 +119,6 @@ soup_openssl_close (GIOChannel   *channe
 	g_io_channel_close (chan->real_sock);
 }
 
-static void
-soup_openssl_free (GIOChannel   *channel)
-{
-	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
-	g_io_channel_unref (chan->real_sock);
-	SSL_free (chan->ssl);
-	g_free (chan);
-}
-
 typedef struct {
 	GIOFunc         func;
 	gpointer        user_data;
@@ -179,6 +180,248 @@ GIOFuncs soup_openssl_channel_funcs = {
 	soup_openssl_add_watch,
 	soup_openssl_free,
 };
+
+#else /* #ifndef SOUP_GLIB2 */
+
+static GIOStatus
+soup_openssl_read (GIOChannel   *channel,
+		   gchar        *buf,
+		   guint         count,
+		   guint        *bytes_read,
+		   GError      **err)
+{
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
+	gint result;
+
+	result = SSL_read (chan->ssl, buf, count);
+
+	if (result < 0) {
+		/* This occurs when a re-handshake is required */
+		*bytes_read = 0;
+		if (SSL_get_error (chan->ssl, result) == SSL_ERROR_WANT_READ)
+		  	return G_IO_STATUS_AGAIN;
+		switch (errno) {
+		case EINVAL:
+#if 0
+			return G_IO_ERROR_INVAL;
+#else
+			return G_IO_STATUS_ERROR;
+#endif
+		case EAGAIN:
+		case EINTR:
+			return G_IO_STATUS_AGAIN;
+		default:
+			return G_IO_STATUS_ERROR;
+		}
+	} else {
+		*bytes_read = result;
+		return G_IO_STATUS_NORMAL;
+	}
+}
+
+static GIOStatus
+soup_openssl_write (GIOChannel   *channel,
+		    const gchar  *buf,
+		    gsize         count,
+		    gsize        *bytes_written,
+		    GError      **err)
+{
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
+	gint result;
+
+	result = SSL_write (chan->ssl, buf, count);
+
+	if (result < 0) {
+		*bytes_written = 0;
+		if (SSL_get_error (chan->ssl, result) == SSL_ERROR_WANT_READ)
+			return G_IO_STATUS_AGAIN;
+		switch (errno) {
+		case EINVAL:
+#if 0
+			return G_IO_ERROR_INVAL;
+#else
+			return G_IO_STATUS_ERROR;
+#endif
+		case EAGAIN:
+		case EINTR:
+			return G_IO_STATUS_AGAIN;
+		default:
+			return G_IO_STATUS_ERROR;
+		}
+	} else {
+		*bytes_written = result;
+		return G_IO_STATUS_NORMAL;
+	}
+}
+
+static GIOStatus
+soup_openssl_seek (GIOChannel  *channel,
+		   gint64       offset,
+		   GSeekType    type,
+		   GError     **err)
+{
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
+	GIOError e;
+
+	e = g_io_channel_seek (chan->real_sock, offset, type);
+
+	if (e != G_IO_ERROR_NONE)
+		return G_IO_STATUS_ERROR;
+	else
+		return G_IO_STATUS_NORMAL;
+}
+
+static GIOStatus
+soup_openssl_close (GIOChannel  *channel,
+		    GError     **err)
+{
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
+	g_io_channel_close (chan->real_sock);
+
+	return G_IO_STATUS_NORMAL;
+}
+
+typedef struct {
+	GSource       source;
+	GPollFD       pollfd;
+	GIOChannel   *channel;
+	GIOCondition  condition;
+} SoupOpenSSLWatch;
+
+static gboolean
+soup_openssl_prepare (GSource *source,
+		      gint    *timeout)
+{
+	SoupOpenSSLWatch *watch = (SoupOpenSSLWatch *) source;
+	GIOCondition buffer_condition = g_io_channel_get_buffer_condition (
+		watch->channel);
+
+	*timeout = -1;
+
+	/* Only return TRUE here if _all_ bits in watch->condition will be set
+	 */
+	return ((watch->condition & buffer_condition) == watch->condition);
+}
+
+static gboolean
+soup_openssl_check (GSource *source)
+{
+	SoupOpenSSLWatch *watch = (SoupOpenSSLWatch *) source;
+	GIOCondition buffer_condition = g_io_channel_get_buffer_condition (
+		watch->channel);
+	GIOCondition poll_condition = watch->pollfd.revents;
+
+	return ((poll_condition | buffer_condition) & watch->condition);
+}
+
+static gboolean
+soup_openssl_dispatch (GSource     *source,
+		       GSourceFunc  callback,
+		       gpointer     user_data)
+{
+	GIOFunc func = (GIOFunc) callback;
+	SoupOpenSSLWatch *watch = (SoupOpenSSLWatch *) source;
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) watch->channel;
+	GIOCondition buffer_condition = g_io_channel_get_buffer_condition (
+		watch->channel);
+	GIOCondition cond;
+
+	if (!func) {
+		g_warning ("IO watch dispatched without callback\n"
+			   "You must call g_source_connect().");
+		return FALSE;
+	}
+	
+	cond = (watch->pollfd.revents | buffer_condition) & watch->condition;
+
+	if (cond & G_IO_IN) {
+		do {
+			if (!(*func) (watch->channel, cond, user_data))
+			return FALSE;
+		} while (SSL_pending (chan->ssl));
+
+		return TRUE;
+	} else
+		return (*func) (watch->channel, cond, user_data);
+}
+
+static void
+soup_openssl_finalize (GSource *source)
+{
+	SoupOpenSSLWatch *watch = (SoupOpenSSLWatch *) source;
+
+	g_io_channel_unref (watch->channel);
+}
+
+/* All of these functions were basically cut-and-pasted from glib */
+GSourceFuncs soup_openssl_watch_funcs = {
+	soup_openssl_prepare,
+	soup_openssl_check,
+	soup_openssl_dispatch,
+	soup_openssl_finalize
+};
+
+static GSource *
+soup_openssl_create_watch (GIOChannel   *channel,
+			   GIOCondition  condition)
+{
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
+
+	if (condition & G_IO_IN) {
+		GSource *source;
+		SoupOpenSSLWatch *watch;
+
+		source = g_source_new (&soup_openssl_watch_funcs,
+				       sizeof (SoupOpenSSLWatch));
+		watch = (SoupOpenSSLWatch *) source;
+
+		watch->channel = channel;
+		g_io_channel_ref (channel);
+
+		watch->condition = condition;
+		
+		watch->pollfd.fd = chan->fd;
+		watch->pollfd.events = condition;
+
+		g_source_add_poll (source, &watch->pollfd);
+
+		return source;
+	}
+	else {
+		return chan->real_sock->funcs->io_create_watch (channel,
+								condition);
+	}
+}
+
+static GIOStatus
+soup_openssl_set_flags (GIOChannel  *channel,
+			GIOFlags     flags,
+			GError     **err)
+{
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
+
+	return chan->real_sock->funcs->io_set_flags (channel, flags, err);
+}	
+
+static GIOFlags
+soup_openssl_get_flags (GIOChannel *channel)
+{
+	SoupOpenSSLChannel *chan = (SoupOpenSSLChannel *) channel;
+
+	return chan->real_sock->funcs->io_get_flags (channel);
+}
+
+GIOFuncs soup_openssl_channel_funcs = {
+	soup_openssl_read,
+	soup_openssl_write,
+	soup_openssl_seek,
+	soup_openssl_close,
+	soup_openssl_create_watch,
+	soup_openssl_free,
+	soup_openssl_set_flags,
+	soup_openssl_get_flags
+};
+#endif /* #ifndef SOUP_GLIB2 */
 
 static SSL_CTX *ssl_context = NULL;
acinclude.m4 (text/plain, 16.3 KB)
# Configure paths for GLIB
# Owen Taylor     97-11-3

dnl AM_PATH_GLIB([MINIMUM-VERSION, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND [, MODULES]]]])
dnl Test for GLIB, and define GLIB_CFLAGS and GLIB_LIBS, if "gmodule" or 
dnl gthread is specified in MODULES, pass to glib-config
dnl
AC_DEFUN(AM_PATH_GLIB,
[dnl 
dnl Get the cflags and libraries from the glib-config script
dnl
AC_ARG_WITH(glib-prefix,[  --with-glib-prefix=PFX   Prefix where GLIB is installed (optional)],
            glib_config_prefix="$withval", glib_config_prefix="")
AC_ARG_WITH(glib-exec-prefix,[  --with-glib-exec-prefix=PFX Exec prefix where GLIB is installed (optional)],
            glib_config_exec_prefix="$withval", glib_config_exec_prefix="")
AC_ARG_ENABLE(glibtest, [  --disable-glibtest       Do not try to compile and run a test GLIB program],
		    , enable_glibtest=yes)

  if test x$glib_config_exec_prefix != x ; then
     glib_config_args="$glib_config_args --exec-prefix=$glib_config_exec_prefix"
     if test x${GLIB_CONFIG+set} != xset ; then
        GLIB_CONFIG=$glib_config_exec_prefix/bin/glib-config
     fi
  fi
  if test x$glib_config_prefix != x ; then
     glib_config_args="$glib_config_args --prefix=$glib_config_prefix"
     if test x${GLIB_CONFIG+set} != xset ; then
        GLIB_CONFIG=$glib_config_prefix/bin/glib-config
     fi
  fi

  for module in . $4
  do
      case "$module" in
         gmodule) 
             glib_config_args="$glib_config_args gmodule"
         ;;
         gthread) 
             glib_config_args="$glib_config_args gthread"
         ;;
      esac
  done

  AC_PATH_PROG(GLIB_CONFIG, glib-config, no)
  min_glib_version=ifelse([$1], ,0.99.7,$1)
  AC_MSG_CHECKING(for GLIB - version >= $min_glib_version)
  no_glib=""
  if test "$GLIB_CONFIG" = "no" ; then
    no_glib=yes
  else
    GLIB_CFLAGS=`$GLIB_CONFIG $glib_config_args --cflags`
    GLIB_LIBS=`$GLIB_CONFIG $glib_config_args --libs`
    glib_config_major_version=`$GLIB_CONFIG $glib_config_args --version | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'`
    glib_config_minor_version=`$GLIB_CONFIG $glib_config_args --version | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'`
    glib_config_micro_version=`$GLIB_CONFIG $glib_config_args --version | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'`
    if test "x$enable_glibtest" = "xyes" ; then
      ac_save_CFLAGS="$CFLAGS"
      ac_save_LIBS="$LIBS"
      CFLAGS="$CFLAGS $GLIB_CFLAGS"
      LIBS="$GLIB_LIBS $LIBS"
dnl
dnl Now check if the installed GLIB is sufficiently new. (Also sanity
dnl checks the results of glib-config to some extent
dnl
      rm -f conf.glibtest
      AC_TRY_RUN([
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>

int 
main ()
{
  int major, minor, micro;
  char *tmp_version;

  system ("touch conf.glibtest");

  /* HP/UX 9 (%@#!) writes to sscanf strings */
  tmp_version = g_strdup("$min_glib_version");
  if (sscanf(tmp_version, "%d.%d.%d", &major, &minor, &micro) != 3) {
     printf("%s, bad version string\n", "$min_glib_version");
     exit(1);
   }

  if ((glib_major_version != $glib_config_major_version) ||
      (glib_minor_version != $glib_config_minor_version) ||
      (glib_micro_version != $glib_config_micro_version))
    {
      printf("\n*** 'glib-config --version' returned %d.%d.%d, but GLIB (%d.%d.%d)\n", 
             $glib_config_major_version, $glib_config_minor_version, $glib_config_micro_version,
             glib_major_version, glib_minor_version, glib_micro_version);
      printf ("*** was found! If glib-config was correct, then it is best\n");
      printf ("*** to remove the old version of GLIB. You may also be able to fix the error\n");
      printf("*** by modifying your LD_LIBRARY_PATH enviroment variable, or by editing\n");
      printf("*** /etc/ld.so.conf. Make sure you have run ldconfig if that is\n");
      printf("*** required on your system.\n");
      printf("*** If glib-config was wrong, set the environment variable GLIB_CONFIG\n");
      printf("*** to point to the correct copy of glib-config, and remove the file config.cache\n");
      printf("*** before re-running configure\n");
    } 
  else if ((glib_major_version != GLIB_MAJOR_VERSION) ||
	   (glib_minor_version != GLIB_MINOR_VERSION) ||
           (glib_micro_version != GLIB_MICRO_VERSION))
    {
      printf("*** GLIB header files (version %d.%d.%d) do not match\n",
	     GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
      printf("*** library (version %d.%d.%d)\n",
	     glib_major_version, glib_minor_version, glib_micro_version);
    }
  else
    {
      if ((glib_major_version > major) ||
        ((glib_major_version == major) && (glib_minor_version > minor)) ||
        ((glib_major_version == major) && (glib_minor_version == minor) && (glib_micro_version >= micro)))
      {
        return 0;
       }
     else
      {
        printf("\n*** An old version of GLIB (%d.%d.%d) was found.\n",
               glib_major_version, glib_minor_version, glib_micro_version);
        printf("*** You need a version of GLIB newer than %d.%d.%d. The latest version of\n",
	       major, minor, micro);
        printf("*** GLIB is always available from ftp://ftp.gtk.org.\n");
        printf("***\n");
        printf("*** If you have already installed a sufficiently new version, this error\n");
        printf("*** probably means that the wrong copy of the glib-config shell script is\n");
        printf("*** being found. The easiest way to fix this is to remove the old version\n");
        printf("*** of GLIB, but you can also set the GLIB_CONFIG environment to point to the\n");
        printf("*** correct copy of glib-config. (In this case, you will have to\n");
        printf("*** modify your LD_LIBRARY_PATH enviroment variable, or edit /etc/ld.so.conf\n");
        printf("*** so that the correct libraries are found at run-time))\n");
      }
    }
  return 1;
}
],, no_glib=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"])
       CFLAGS="$ac_save_CFLAGS"
       LIBS="$ac_save_LIBS"
     fi
  fi
  if test "x$no_glib" = x ; then
     AC_MSG_RESULT(yes)
     ifelse([$2], , :, [$2])     
  else
     AC_MSG_RESULT(no)
     if test "$GLIB_CONFIG" = "no" ; then
       echo "*** The glib-config script installed by GLIB could not be found"
       echo "*** If GLIB was installed in PREFIX, make sure PREFIX/bin is in"
       echo "*** your path, or set the GLIB_CONFIG environment variable to the"
       echo "*** full path to glib-config."
     else
       if test -f conf.glibtest ; then
        :
       else
          echo "*** Could not run GLIB test program, checking why..."
          CFLAGS="$CFLAGS $GLIB_CFLAGS"
          LIBS="$LIBS $GLIB_LIBS"
          AC_TRY_LINK([
#include <glib.h>
#include <stdio.h>
],      [ return ((glib_major_version) || (glib_minor_version) || (glib_micro_version)); ],
        [ echo "*** The test program compiled, but did not run. This usually means"
          echo "*** that the run-time linker is not finding GLIB or finding the wrong"
          echo "*** version of GLIB. If it is not finding GLIB, you'll need to set your"
          echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
          echo "*** to the installed location  Also, make sure you have run ldconfig if that"
          echo "*** is required on your system"
	  echo "***"
          echo "*** If you have an old version installed, it is best to remove it, although"
          echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH"
          echo "***"
          echo "*** If you have a RedHat 5.0 system, you should remove the GTK package that"
          echo "*** came with the system with the command"
          echo "***"
          echo "***    rpm --erase --nodeps gtk gtk-devel" ],
        [ echo "*** The test program failed to compile or link. See the file config.log for the"
          echo "*** exact error that occured. This usually means GLIB was incorrectly installed"
          echo "*** or that you have moved GLIB since it was installed. In the latter case, you"
          echo "*** may want to edit the glib-config script: $GLIB_CONFIG" ])
          CFLAGS="$ac_save_CFLAGS"
          LIBS="$ac_save_LIBS"
       fi
     fi
     GLIB_CFLAGS=""
     GLIB_LIBS=""
     ifelse([$3], , :, [$3])
  fi
  AC_SUBST(GLIB_CFLAGS)
  AC_SUBST(GLIB_LIBS)
  rm -f conf.glibtest
])
# Configure paths for GLIB
# Owen Taylor     1997-2001

dnl AM_PATH_GLIB_2_0([MINIMUM-VERSION, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND [, MODULES]]]])
dnl Test for GLIB, and define GLIB_CFLAGS and GLIB_LIBS, if gmodule, gobject or 
dnl gthread is specified in MODULES, pass to pkg-config
dnl
AC_DEFUN(AM_PATH_GLIB_2_0,
[dnl 
dnl Get the cflags and libraries from pkg-config
dnl
AC_ARG_ENABLE(glibtest, [  --disable-glibtest      do not try to compile and run a test GLIB program],
		    , enable_glibtest=yes)

  pkg_config_args=glib-2.0
  for module in . $4
  do
      case "$module" in
         gmodule) 
             pkg_config_args="$pkg_config_args gmodule-2.0"
         ;;
         gobject) 
             pkg_config_args="$pkg_config_args gobject-2.0"
         ;;
         gthread) 
             pkg_config_args="$pkg_config_args gthread-2.0"
         ;;
      esac
  done

  AC_PATH_PROG(PKG_CONFIG, pkg-config, no)

  no_glib=""

  if test x$PKG_CONFIG != xno ; then
    if $PKG_CONFIG --atleast-pkgconfig-version 0.7 ; then
      :
    else
      echo *** pkg-config too old; version 0.7 or better required.
      no_glib=yes
      PKG_CONFIG=no
    fi
  else
    no_glib=yes
  fi

  min_glib_version=ifelse([$1], ,2.0.0,$1)
  AC_MSG_CHECKING(for GLIB - version >= $min_glib_version)

  if test x$PKG_CONFIG != xno ; then
    ## don't try to run the test against uninstalled libtool libs
    if $PKG_CONFIG --uninstalled $pkg_config_args; then
	  echo "Will use uninstalled version of GLib found in PKG_CONFIG_PATH"
	  enable_glibtest=no
    fi

    if $PKG_CONFIG --atleast-version $min_glib_version $pkg_config_args; then
	  :
    else
	  no_glib=yes
    fi
  fi

  if test x"$no_glib" = x ; then
    GLIB_GENMARSHAL=`$PKG_CONFIG --variable=glib_genmarshal glib-2.0`
    GOBJECT_QUERY=`$PKG_CONFIG --variable=gobject_query glib-2.0`
    GLIB_MKENUMS=`$PKG_CONFIG --variable=glib_mkenums glib-2.0`

    GLIB_CFLAGS=`$PKG_CONFIG --cflags $pkg_config_args`
    GLIB_LIBS=`$PKG_CONFIG --libs $pkg_config_args`
    glib_config_major_version=`$PKG_CONFIG --modversion glib-2.0 | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'`
    glib_config_minor_version=`$PKG_CONFIG --modversion glib-2.0 | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'`
    glib_config_micro_version=`$PKG_CONFIG --modversion glib-2.0 | \
           sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'`
    if test "x$enable_glibtest" = "xyes" ; then
      ac_save_CFLAGS="$CFLAGS"
      ac_save_LIBS="$LIBS"
      CFLAGS="$CFLAGS $GLIB_CFLAGS"
      LIBS="$GLIB_LIBS $LIBS"
dnl
dnl Now check if the installed GLIB is sufficiently new. (Also sanity
dnl checks the results of pkg-config to some extent)
dnl
      rm -f conf.glibtest
      AC_TRY_RUN([
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>

int 
main ()
{
  int major, minor, micro;
  char *tmp_version;

  system ("touch conf.glibtest");

  /* HP/UX 9 (%@#!) writes to sscanf strings */
  tmp_version = g_strdup("$min_glib_version");
  if (sscanf(tmp_version, "%d.%d.%d", &major, &minor, &micro) != 3) {
     printf("%s, bad version string\n", "$min_glib_version");
     exit(1);
   }

  if ((glib_major_version != $glib_config_major_version) ||
      (glib_minor_version != $glib_config_minor_version) ||
      (glib_micro_version != $glib_config_micro_version))
    {
      printf("\n*** 'pkg-config --modversion glib-2.0' returned %d.%d.%d, but GLIB (%d.%d.%d)\n", 
             $glib_config_major_version, $glib_config_minor_version, $glib_config_micro_version,
             glib_major_version, glib_minor_version, glib_micro_version);
      printf ("*** was found! If pkg-config was correct, then it is best\n");
      printf ("*** to remove the old version of GLib. You may also be able to fix the error\n");
      printf("*** by modifying your LD_LIBRARY_PATH enviroment variable, or by editing\n");
      printf("*** /etc/ld.so.conf. Make sure you have run ldconfig if that is\n");
      printf("*** required on your system.\n");
      printf("*** If pkg-config was wrong, set the environment variable PKG_CONFIG_PATH\n");
      printf("*** to point to the correct configuration files\n");
    } 
  else if ((glib_major_version != GLIB_MAJOR_VERSION) ||
	   (glib_minor_version != GLIB_MINOR_VERSION) ||
           (glib_micro_version != GLIB_MICRO_VERSION))
    {
      printf("*** GLIB header files (version %d.%d.%d) do not match\n",
	     GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
      printf("*** library (version %d.%d.%d)\n",
	     glib_major_version, glib_minor_version, glib_micro_version);
    }
  else
    {
      if ((glib_major_version > major) ||
        ((glib_major_version == major) && (glib_minor_version > minor)) ||
        ((glib_major_version == major) && (glib_minor_version == minor) && (glib_micro_version >= micro)))
      {
        return 0;
       }
     else
      {
        printf("\n*** An old version of GLIB (%d.%d.%d) was found.\n",
               glib_major_version, glib_minor_version, glib_micro_version);
        printf("*** You need a version of GLIB newer than %d.%d.%d. The latest version of\n",
	       major, minor, micro);
        printf("*** GLIB is always available from ftp://ftp.gtk.org.\n");
        printf("***\n");
        printf("*** If you have already installed a sufficiently new version, this error\n");
        printf("*** probably means that the wrong copy of the pkg-config shell script is\n");
        printf("*** being found. The easiest way to fix this is to remove the old version\n");
        printf("*** of GLIB, but you can also set the PKG_CONFIG environment to point to the\n");
        printf("*** correct copy of pkg-config. (In this case, you will have to\n");
        printf("*** modify your LD_LIBRARY_PATH enviroment variable, or edit /etc/ld.so.conf\n");
        printf("*** so that the correct libraries are found at run-time))\n");
      }
    }
  return 1;
}
],, no_glib=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"])
       CFLAGS="$ac_save_CFLAGS"
       LIBS="$ac_save_LIBS"
     fi
  fi
  if test "x$no_glib" = x ; then
     AC_MSG_RESULT(yes (version $glib_config_major_version.$glib_config_minor_version.$glib_config_micro_version))
     ifelse([$2], , :, [$2])     
  else
     AC_MSG_RESULT(no)
     if test "$PKG_CONFIG" = "no" ; then
       echo "*** A new enough version of pkg-config was not found."
       echo "*** See http://www.freedesktop.org/software/pkgconfig/"
     else
       if test -f conf.glibtest ; then
        :
       else
          echo "*** Could not run GLIB test program, checking why..."
          CFLAGS="$CFLAGS $GLIB_CFLAGS"
          LIBS="$LIBS $GLIB_LIBS"
          AC_TRY_LINK([
#include <glib.h>
#include <stdio.h>
],      [ return ((glib_major_version) || (glib_minor_version) || (glib_micro_version)); ],
        [ echo "*** The test program compiled, but did not run. This usually means"
          echo "*** that the run-time linker is not finding GLIB or finding the wrong"
          echo "*** version of GLIB. If it is not finding GLIB, you'll need to set your"
          echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
          echo "*** to the installed location  Also, make sure you have run ldconfig if that"
          echo "*** is required on your system"
	  echo "***"
          echo "*** If you have an old version installed, it is best to remove it, although"
          echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH" ],
        [ echo "*** The test program failed to compile or link. See the file config.log for the"
          echo "*** exact error that occured. This usually means GLIB was incorrectly installed"
          echo "*** or that you have moved GLIB since it was installed. In the latter case, you"
          echo "*** may want to edit the pkg-config script: $PKG_CONFIG" ])
          CFLAGS="$ac_save_CFLAGS"
          LIBS="$ac_save_LIBS"
       fi
     fi
     GLIB_CFLAGS=""
     GLIB_LIBS=""
     GLIB_GENMARSHAL=""
     GOBJECT_QUERY=""
     GLIB_MKENUMS=""
     ifelse([$3], , :, [$3])
  fi
  AC_SUBST(GLIB_CFLAGS)
  AC_SUBST(GLIB_LIBS)
  AC_SUBST(GLIB_GENMARSHAL)
  AC_SUBST(GOBJECT_QUERY)
  AC_SUBST(GLIB_MKENUMS)
  rm -f conf.glibtest
])