From 508badb42c22433aa51634607a85d60e09bb494c Mon Sep 17 00:00:00 2001
From: Olivier Mehani <olivier.mehani@nicta.com.au>
Date: Thu, 16 Aug 2012 17:28:35 +1000
Subject: [PATCH 2/2] Substantial rewrite of AX_CHECK_POSTGRES_DB.

It still behaves the same by default, AC_DEFINing HAVE_LIBPQ and setting
variables PQINCPATH and PQLIBPATH, while erroring when either header or
library is found in the default or specified paths.

The logic is however different, where the specified prefixes and/or
incdir and libdir are temporarily added to CFLAGS and LDFLAGS. The
normal detection logic (AC_CHECK_HEADER and AC_CHECK_LIB) is used next,
only, and once.

In addition, two optional arguments to the macro, ACTION-IF-FOUND and
ACTION-IF-NOT-FOUND are now available to extend the logic. The default
ACTION-IF-FOUND is to do nothing (apart from defining the variables and
substitutions, which is always done). The default ACTION-IF-NOT-FOUND is
to error out; it is replaced by the ACTION-IF-NOT-FOUND if specified
to, e.g., gracefully remove configure PGSQL out when it's only optional.

Bump serial from 5 to 6.

Signed-off-by: Olivier Mehani <olivier.mehani@nicta.com.au>
---
 m4/ax_check_postgres_db.m4 | 112 +++++++++++++++++++++++++--------------------
 1 file changed, 63 insertions(+), 49 deletions(-)

diff --git a/m4/ax_check_postgres_db.m4 b/m4/ax_check_postgres_db.m4
index 9e21c8f..0d8aef6 100644
--- a/m4/ax_check_postgres_db.m4
+++ b/m4/ax_check_postgres_db.m4
@@ -4,31 +4,47 @@
 #
 # SYNOPSIS
 #
-#   AX_CHECK_POSTGRES_DB
+#   AX_CHECK_POSTGRES_DB([ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
 #
 # DESCRIPTION
 #
-#   This macro tries to find the headers and librarys for the PostgreSQL
+#   This macro tries to find the headers and libraries for the PostgreSQL
 #   database to build client applications.
 #
-#   If includes are found, the variable PQINCPATH will be set. If librarys
-#   are found, the variable PQLIBPATH will be set. if no check was
-#   successful, the script exits with a error message.
+#   If includes are found, the variable PQINCPATH will be set, and have_pqinc
+#   will be 'yes'. If libraries are found, the variable PQLIBPATH will be set,
+#   and have_pqlib will be 'yes'. If both PQINCPATH and PQLIBPATH are found,
+#   HAVE_LIBPQ will be set to 'yes' (and AC_DEFINEd), and -lpq will be added to
+#   the beginning of LIBS. If their respective conditions are not met,
+#   have_pqinc, have_pqlib, HAVE_LIBPQ, will be 'no'.
+#
+#   This macro does fails if either the headers or the library are
+#   not found, unless ACTION-IF-NOT-FOUND is not empty. When both are found,
+#   ACTION-IF-FOUND, if defined, is exectued after the variables have been
+#   defined. If --with-pgsql=no (or equivalents) is given, nothing happens.
+#
+#   An automake conditional can be subsequently defined as
+#	AM_CONDITIONAL([HAVE_LIBPQ], [test x$HAVE_LIBPQ = x$yes])
+#   in configure.ac.
 #
 # LICENSE
 #
 #   Copyright (c) 2008 Christian Toepp <c.toepp@gmail.com>
+#   Copyright (c) 2012 Olivier Mehani <shtrom-ax@ssji.net>
 #
 #   Copying and distribution of this file, with or without modification, are
 #   permitted in any medium without royalty provided the copyright notice
 #   and this notice are preserved. This file is offered as-is, without any
 #   warranty.
 
-#serial 5
+#serial 6
 
 AU_ALIAS([CT_CHECK_POSTGRES_DB], [AX_CHECK_POSTGRES_DB])
 AC_DEFUN([AX_CHECK_POSTGRES_DB], [
 
+pushdef([ACTION_IF_FOUND],$1)
+pushdef([ACTION_IF_NOT_FOUND],$2)
+
 AC_ARG_WITH(pgsql,
 	[AS_HELP_STRING([--with-pgsql[[=PREFIX]]],
 			[Prefix of your PostgreSQL installation @<:@PREFIX@:>@])],
@@ -42,51 +58,49 @@ AC_ARG_WITH(pgsql-lib,
 			[Path to the libraries of PostgreSQL @<:@LIBDIR@:>@])],
 	[pg_lib=$withval], [pg_lib=])
 
+have_pqinc=no
+have_pqlib=no
+HAVE_LIBPQ=no
+
+AS_IF([test "$pg_prefix" != "no"],[
+       AS_IF([test "$pg_prefix" != "yes" && test "$pg_prefix" != ""], [
+	      PQINCPATH="-I$pg_prefix/include $PQINCPATH"
+	      PQLIBPATH="-L$pg_prefix/lib $PQLIBPATH"
+	      ])
+       AS_IF([test "$pg_inc" != ""], [PQINCPATH="-I$pg_inc $PQINCPATH"])
+       AS_IF([test "$pg_lib" != ""], [PQLIBPATH="-L$pg_lib $PQLIBPATH"])
+
+       oldCFLAGS=$CFLAGS
+       oldLDFLAGS=$LDFLAGS
 
-AC_SUBST(PQINCPATH)
-AC_SUBST(PQLIBPATH)
+       CFLAGS="$PQINCPATH $CFLAGS"
+       LDFLAGS="$PQLIBPATH $LDFLAGS"
 
-if test "$pg_prefix" != ""; then
-   AC_MSG_CHECKING([for PostgreSQL includes in $pg_prefix/include])
-   if test -f "$pg_prefix/include/libpq-fe.h" ; then
-      PQINCPATH="-I$pg_prefix/include"
-      AC_MSG_RESULT([yes])
-   else
-      AC_MSG_ERROR(libpq-fe.h not found)
-   fi
-   AC_MSG_CHECKING([for PostgreSQL librarys in $pg_prefix/lib])
-   if test -f "$pg_prefix/lib/libpq.so" ; then
-      PQLIBPATH="-L$pg_prefix/lib"
-      AC_MSG_RESULT([yes])
-   else
-      AC_MSG_ERROR(libpq.so not found)
-   fi
-else
-  if test "$pg_inc" != ""; then
-    AC_MSG_CHECKING([for PostgreSQL includes in $pg_inc])
-    if test -f "$pg_inc/libpq-fe.h" ; then
-      PQINCPATH="-I$pg_inc"
-      AC_MSG_RESULT([yes])
-    else
-      AC_MSG_ERROR(libpq-fe.h not found)
-    fi
-  fi
-  if test "$pg_lib" != ""; then
-    AC_MSG_CHECKING([for PostgreSQL librarys in $pg_lib])
-    if test -f "$pg_lib/libpq.so" ; then
-      PQLIBPATH="-L$pg_lib"
-      AC_MSG_RESULT([yes])
-    else
-      AC_MSG_ERROR(libpq.so not found)
-    fi
-  fi
-fi
+       AC_CHECK_HEADER([libpq-fe.h], [have_pqinc=yes])
+       AC_CHECK_LIB([pq], [PQconnectdb], [
+		     dnl We only search for libpq, so we know why we're here
+		     LIBPQ_LIBS=-lpq
+		     have_pqlib=yes
+		     ])
+
+       HAVE_LIBPQ=`(test x$have_pqinc = xyes && test x$have_pqlib = xyes && echo yes) || echo no`
+
+       CFLAGS=$oldCFLAGS
+       LDFLAGS=$oldLDFLAGS
+
+       AC_SUBST(PQINCPATH)
+       AC_SUBST(PQLIBPATH)
+       AC_SUBST(LIBPQ_LIBS)
+
+       AS_IF([test x$HAVE_LIBPQ = xyes],[
+	      AC_DEFINE([HAVE_LIBPQ], [1], [Define if libpq is installed])
+	      ACTION_IF_FOUND
+	      ],
+	      [m4_ifset([ACTION_IF_NOT_FOUND],[ACTION_IF_NOT_FOUND],
+			  [AC_MSG_ERROR(some support files for PostgreSQL were missing)])])
+])
 
-if test "$PQINCPATH" = "" ; then
-  AC_CHECK_HEADER([libpq-fe.h], [], AC_MSG_ERROR(libpq-fe.h not found))
-fi
-if test "$PQLIBPATH" = "" ; then
-  AC_CHECK_LIB(pq, PQconnectdb, [], AC_MSG_ERROR(libpq.so not found))
-fi
+popdef([ACTION_IF_FOUND])
+popdef([ACTION_IF_NOT_FOUND])
 
 ])
-- 
1.7.11.4

