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