1##### http://autoconf-archive.cryp.to/ax_lib_postgresql.html
2#
3# SYNOPSIS
4#
5#   AX_LIB_POSTGRESQL([MINIMUM-VERSION])
6#
7# DESCRIPTION
8#
9#   This macro provides tests of availability of PostgreSQL 'libpq'
10#   library of particular version or newer.
11#
12#   AX_LIB_POSTGRESQL macro takes only one argument which is optional.
13#   If there is no required version passed, then macro does not run
14#   version test.
15#
16#   The --with-postgresql option takes one of three possible values:
17#
18#   no - do not check for PostgreSQL client library
19#
20#   yes - do check for PostgreSQL library in standard locations
21#   (pg_config should be in the PATH)
22#
23#   path - complete path to pg_config utility, use this option if
24#   pg_config can't be found in the PATH
25#
26#   This macro calls:
27#
28#     AC_SUBST(POSTGRESQL_CFLAGS)
29#     AC_SUBST(POSTGRESQL_LDFLAGS)
30#     AC_SUBST(POSTGRESQL_VERSION)
31#
32#   And sets:
33#
34#     HAVE_POSTGRESQL
35#
36# LAST MODIFICATION
37#
38#   2006-07-16
39#
40# COPYLEFT
41#
42#   Copyright (c) 2006 Mateusz Loskot <mateusz@loskot.net>
43#
44#   Copying and distribution of this file, with or without
45#   modification, are permitted in any medium without royalty provided
46#   the copyright notice and this notice are preserved.
47
48AC_DEFUN([AX_LIB_POSTGRESQL],
49[
50    AC_ARG_WITH([postgresql],
51        AC_HELP_STRING([--with-postgresql=@<:@ARG@:>@],
52            [use PostgreSQL library @<:@default=yes@:>@, optionally specify path to pg_config]
53        ),
54        [
55        if test "$withval" = "no"; then
56            want_postgresql="no"
57        elif test "$withval" = "yes"; then
58            want_postgresql="yes"
59        else
60            want_postgresql="yes"
61            PG_CONFIG="$withval"
62        fi
63        ],
64        [want_postgresql="yes"]
65    )
66
67    POSTGRESQL_CFLAGS=""
68    POSTGRESQL_LDFLAGS=""
69    POSTGRESQL_POSTGRESQL=""
70
71    dnl
72    dnl Check PostgreSQL libraries (libpq)
73    dnl
74
75    if test "$want_postgresql" = "yes"; then
76
77        if test -z "$PG_CONFIG" -o test; then
78            AC_PATH_PROG([PG_CONFIG], [pg_config], [no])
79        fi
80
81        if test "$PG_CONFIG" != "no"; then
82            AC_MSG_CHECKING([for PostgreSQL libraries])
83
84            POSTGRESQL_CFLAGS="-I`$PG_CONFIG --includedir`"
85            POSTGRESQL_LDFLAGS="-L`$PG_CONFIG --libdir` -lpq"
86
87            POSTGRESQL_VERSION=`$PG_CONFIG --version | sed -e 's#PostgreSQL ##'`
88
89            AC_DEFINE([HAVE_POSTGRESQL], [1],
90                [Define to 1 if PostgreSQL libraries are available])
91
92            found_postgresql="yes"
93            AC_MSG_RESULT([yes])
94        else
95            found_postgresql="no"
96            AC_MSG_RESULT([no])
97        fi
98    fi
99
100    dnl
101    dnl Check if required version of PostgreSQL is available
102    dnl
103
104
105    postgresql_version_req=ifelse([$1], [], [], [$1])
106
107    if test "$found_postgresql" = "yes" -a -n "$postgresql_version_req"; then
108
109        AC_MSG_CHECKING([if PostgreSQL version is >= $postgresql_version_req])
110
111        dnl Decompose required version string of PostgreSQL
112        dnl and calculate its number representation
113        postgresql_version_req_major=`expr $postgresql_version_req : '\([[0-9]]*\)'`
114        postgresql_version_req_minor=`expr $postgresql_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
115        postgresql_version_req_micro=`expr $postgresql_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
116        if test "x$postgresql_version_req_micro" = "x"; then
117            postgresql_version_req_micro="0"
118        fi
119
120        postgresql_version_req_number=`expr $postgresql_version_req_major \* 1000000 \
121                                   \+ $postgresql_version_req_minor \* 1000 \
122                                   \+ $postgresql_version_req_micro`
123
124        dnl Decompose version string of installed PostgreSQL
125        dnl and calculate its number representation
126        postgresql_version_major=`expr $POSTGRESQL_VERSION : '\([[0-9]]*\)'`
127        postgresql_version_minor=`expr $POSTGRESQL_VERSION : '[[0-9]]*\.\([[0-9]]*\)'`
128        postgresql_version_micro=`expr $POSTGRESQL_VERSION : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
129        if test "x$postgresql_version_micro" = "x"; then
130            postgresql_version_micro="0"
131        fi
132
133        postgresql_version_number=`expr $postgresql_version_major \* 1000000 \
134                                   \+ $postgresql_version_minor \* 1000 \
135                                   \+ $postgresql_version_micro`
136
137        postgresql_version_check=`expr $postgresql_version_number \>\= $postgresql_version_req_number`
138        if test "$postgresql_version_check" = "1"; then
139            AC_MSG_RESULT([yes])
140        else
141            AC_MSG_RESULT([no])
142        fi
143    fi
144
145    AC_SUBST([POSTGRESQL_VERSION])
146    AC_SUBST([POSTGRESQL_CFLAGS])
147    AC_SUBST([POSTGRESQL_LDFLAGS])
148])
149