1dnl                                            -*- Autoconf -*-
2##### http://autoconf-archive.cryp.to/ax_lib_postgresql.html
3#
4# SYNOPSIS
5#
6#   AX_LIB_POSTGRESQL([MINIMUM-VERSION])
7#
8# DESCRIPTION
9#
10#   This macro provides tests of availability of PostgreSQL 'libpq'
11#   library of particular version or newer.
12#
13#   AX_LIB_POSTGRESQL macro takes only one argument which is optional.
14#   If there is no required version passed, then macro does not run
15#   version test.
16#
17#   The --with-postgresql option takes one of three possible values:
18#
19#   no - do not check for PostgreSQL client library
20#
21#   yes - do check for PostgreSQL library in standard locations
22#   (pg_config should be in the PATH)
23#
24#   path - complete path to pg_config utility, use this option if
25#   pg_config can't be found in the PATH
26#
27#   This macro calls:
28#
29#     AC_SUBST([POSTGRESQL_CFLAGS])
30#     AC_SUBST([POSTGRESQL_CPPFLAGS])
31#     AC_SUBST([POSTGRESQL_LDFLAGS])
32#     AC_SUBST([POSTGRESQL_VERSION])
33#
34#   And sets:
35#
36#     HAVE_POSTGRESQL
37#
38# LAST MODIFICATION
39#
40#   2006-07-16
41#   2010-05-14 MKS: Added POSTGRESQL_CPPFLAGS
42#   2011-06-21 AJB: Added workaround for Fedora pg_config oddity
43#   2011-08-01 MKS: Changed PG_CONFIG to POSTGRESQL_CONFIG
44#                   Made test constructs more portable
45#
46# COPYLEFT
47#
48#   Copyright (c) 2006 Mateusz Loskot <mateusz@loskot.net>
49#
50#   Copying and distribution of this file, with or without
51#   modification, are permitted in any medium without royalty provided
52#   the copyright notice and this notice are preserved.
53
54AC_DEFUN([AX_LIB_POSTGRESQL],
55[
56  POSTGRESQL_CFLAGS=""
57  POSTGRESQL_CPPFLAGS=""
58  POSTGRESQL_LDFLAGS=""
59  POSTGRESQL_CONFIG=""
60  POSTGRESQL_VERSION=""
61
62  AC_ARG_WITH([postgresql],
63  [AS_HELP_STRING([--with-postgresql@<:=@ARG@:>@],
64  [use PostgreSQL library @<:@default=yes@:>@, optionally specify path to pg_config])],
65  [
66    AS_IF([test "x${withval}" = "xno"],
67    [want_postgresql="no"],
68    [test "x${withval}" = "xyes"],
69    [want_postgresql="yes"],
70    [
71      want_postgresql="yes"
72      POSTGRESQL_CONFIG="${withval}"
73    ])
74  ],
75  [want_postgresql="yes"])
76
77  dnl
78  dnl Check PostgreSQL libraries (libpq)
79  dnl
80
81  AS_IF([test "x${want_postgresql}" = "xyes"],
82  [
83    AS_IF([test -z "${POSTGRESQL_CONFIG}" -o test],
84    [AC_PATH_PROG([POSTGRESQL_CONFIG], [pg_config], [no])])
85
86    AS_IF([test "x${POSTGRESQL_CONFIG}" != "xno"],
87    [
88      AC_MSG_CHECKING([for PostgreSQL libraries])
89
90      POSTGRESQL_CFLAGS="-I`${POSTGRESQL_CONFIG} --includedir`"
91      POSTGRESQL_CPPFLAGS="-I`${POSTGRESQL_CONFIG} --includedir`"
92      POSTGRESQL_LDFLAGS="-L`${POSTGRESQL_CONFIG} --libdir` -lpq"
93
94      POSTGRESQL_VERSION=`${POSTGRESQL_CONFIG} --version | sed -e 's#PostgreSQL ##'`
95
96      dnl It isn't enough to just test for pg_config as Fedora
97      dnl provides it in the postgresql RPM even though postgresql-devel may
98      dnl not be installed
99
100      EMBCPPFLAGS="${CPPFLAGS}"
101      EMBLDFLAGS="${LDFLAGS}"
102
103      CPPFLAGS="${POSTGRESQL_CPPFLAGS} ${EMBCPPFLAGS}"
104      LDFLAGS="${POSTGRESQL_LDFLAGS} ${EMBLDFLAGS}"
105
106      AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>
107                                        #include "libpq-fe.h"]],
108                                      [[PQconnectdb(NULL)]])],
109        [havepostgresql="yes"],
110        [havepostgresql="no"])
111
112      CPPFLAGS="${EMBCPPFLAGS}"
113      LDFLAGS="${EMBLDFLAGS}"
114
115      AS_IF([test "x${havepostgresql}" = "xyes"],
116      [
117        AC_DEFINE([HAVE_POSTGRESQL], [1],
118        [Define to 1 if PostgreSQL libraries are available.])
119        found_postgresql="yes"
120        AC_MSG_RESULT([yes])
121      ],
122      [
123        POSTGRESQL_CFLAGS=""
124        POSTGRESQL_CPPFLAGS=""
125        POSTGRESQL_LDFLAGS=""
126        found_postgresql="no"
127        AC_MSG_RESULT([no])
128      ])
129    ],
130    [
131      found_postgresql="no"
132    ])
133  ])
134
135  dnl
136  dnl Check if required version of PostgreSQL is available
137  dnl
138
139  postgresql_version_req=ifelse([$1], [], [], [$1])
140
141  AS_IF([test "x${found_postgresql}" = "xyes" -a -n "${postgresql_version_req}"],
142  [
143    AC_MSG_CHECKING([if PostgreSQL version is >= ${postgresql_version_req}])
144
145    dnl Decompose required version string of PostgreSQL
146    dnl and calculate its number representation
147
148    postgresql_version_req_major=`expr ${postgresql_version_req} : '\([[0-9]]*\)'`
149    postgresql_version_req_minor=`expr ${postgresql_version_req} : '[[0-9]]*\.\([[0-9]]*\)'`
150    postgresql_version_req_micro=`expr ${postgresql_version_req} : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
151
152    AS_IF([test "x${postgresql_version_req_micro}" = "x"],
153    [postgresql_version_req_micro="0"])
154
155    postgresql_version_req_number=`expr ${postgresql_version_req_major} \* 1000000 \
156                                  \+ ${postgresql_version_req_minor} \* 1000 \
157                                  \+ ${postgresql_version_req_micro}`
158
159    dnl Decompose version string of installed PostgreSQL
160    dnl and calculate its number representation
161
162    postgresql_version_major=`expr ${POSTGRESQL_VERSION} : '\([[0-9]]*\)'`
163    postgresql_version_minor=`expr ${POSTGRESQL_VERSION} : '[[0-9]]*\.\([[0-9]]*\)'`
164    postgresql_version_micro=`expr ${POSTGRESQL_VERSION} : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
165
166    AS_IF([test "x${postgresql_version_micro}" = "x"],
167      [postgresql_version_micro="0"])
168
169    postgresql_version_number=`expr ${postgresql_version_major} \* 1000000 \
170                              \+ ${postgresql_version_minor} \* 1000 \
171                              \+ ${postgresql_version_micro}`
172
173    postgresql_version_check=`expr ${postgresql_version_number} \>\= ${postgresql_version_req_number}`
174
175    AS_IF([test "x${postgresql_version_check}" = "x1"],
176    [AC_MSG_RESULT([yes])],
177    [AC_MSG_RESULT([no])])
178  ])
179
180  AC_SUBST([POSTGRESQL_CFLAGS])
181  AC_SUBST([POSTGRESQL_CPPFLAGS])
182  AC_SUBST([POSTGRESQL_LDFLAGS])
183  AC_SUBST([POSTGRESQL_VERSION])
184])
185