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_LIBS)
31#     AC_SUBST(POSTGRESQL_VERSION)
32#
33#   And sets:
34#
35#     HAVE_POSTGRESQL
36#
37# LAST MODIFICATION
38#
39#   2017-09-26   modified version detection to detect PostgreSQL 10 (Zabbix)
40#   2006-07-16
41#
42# COPYLEFT
43#
44#   Copyright (c) 2006 Mateusz Loskot <mateusz@loskot.net>
45#
46#   Copying and distribution of this file, with or without
47#   modification, are permitted in any medium without royalty provided
48#   the copyright notice and this notice are preserved.
49
50AC_DEFUN([AX_LIB_POSTGRESQL],
51[
52    PG_CONFIG="no"
53
54    AC_ARG_WITH([postgresql],
55        AC_HELP_STRING([--with-postgresql@<:@=ARG@:>@],
56            [use PostgreSQL library @<:@default=no@:>@, optionally specify path to pg_config]
57        ),
58        [
59        if test "x$withval" = "xno"; then
60            want_postgresql="no"
61        elif test "x$withval" = "xyes"; then
62            want_postgresql="yes"
63        else
64            want_postgresql="yes"
65            PG_CONFIG="$withval"
66        fi
67        ],
68        [want_postgresql="no"]
69    )
70
71    POSTGRESQL_CFLAGS=""
72    POSTGRESQL_LDFLAGS=""
73    POSTGRESQL_LIBS=""
74    POSTGRESQL_VERSION=""
75
76    dnl
77    dnl Check PostgreSQL libraries (libpq)
78    dnl
79
80    if test "x$want_postgresql" = "xyes"; then
81        AC_PATH_PROG([PG_CONFIG], [pg_config], [])
82
83        if test -x "$PG_CONFIG"; then
84            POSTGRESQL_CFLAGS="`$PG_CONFIG --includedir`"
85            if test -n "$POSTGRESQL_CFLAGS"; then
86                POSTGRESQL_CFLAGS="-I$POSTGRESQL_CFLAGS";
87            fi
88            POSTGRESQL_LDFLAGS="`$PG_CONFIG --libdir`"
89            if test -n "$POSTGRESQL_LDFLAGS"; then
90                POSTGRESQL_LDFLAGS="-L$POSTGRESQL_LDFLAGS";
91            fi
92            POSTGRESQL_LIBS="-lpq"
93
94            _save_postgresql_cflags="${CFLAGS}"
95            _save_postgresql_ldflags="${LDFLAGS}"
96            _save_postgresql_libs="${LIBS}"
97            CFLAGS="${CFLAGS} ${POSTGRESQL_CFLAGS}"
98            LDFLAGS="${LDFLAGS} ${POSTGRESQL_LDFLAGS}"
99            LIBS="${LIBS} ${POSTGRESQL_LIBS}"
100
101            AC_MSG_CHECKING([for PostgreSQL libraries])
102            AC_TRY_LINK(
103[
104#include <libpq-fe.h>
105],
106[
107PGconn	*conn;
108conn = PQsetdbLogin(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
109],
110            AC_DEFINE([HAVE_POSTGRESQL], [1], [Define to 1 if PostgreSQL libraries are available])
111            found_postgresql="yes"
112            AC_MSG_RESULT(yes),
113            AC_MSG_RESULT(no))
114
115            CFLAGS="${_save_postgresql_cflags}"
116            LDFLAGS="${_save_postgresql_ldflags}"
117            LIBS="${_save_postgresql_libs}"
118            unset _save_postgresql_cflags
119            unset _save_postgresql_ldflags
120            unset _save_postgresql_libs
121
122            if test "x$found_postgresql" = "xyes"; then
123                POSTGRESQL_VERSION=`$PG_CONFIG --version | cut -d' ' -f2`
124            fi
125        fi
126    fi
127
128    dnl
129    dnl Check if required version of PostgreSQL is available
130    dnl
131
132    postgresql_version_req=ifelse([$1], [], [], [$1])
133
134    if test "x$found_postgresql" = "xyes" -a -n "$postgresql_version_req"; then
135
136        dnl Decompose version string of installed PostgreSQL
137        dnl and calculate its number representation
138        postgresql_version_major=`expr $POSTGRESQL_VERSION : '\([[0-9]]*\)'`
139        postgresql_version_minor=`expr $POSTGRESQL_VERSION : '[[0-9]]*\.\([[0-9]]*\)'`
140        postgresql_version_micro=`expr $POSTGRESQL_VERSION : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
141        if test "x$postgresql_version_minor" = "x"; then
142            postgresql_version_minor="0"
143        fi
144        if test "x$postgresql_version_micro" = "x"; then
145            postgresql_version_micro="0"
146        fi
147
148        postgresql_version_number=`expr $postgresql_version_major \* 1000000 \
149                                   \+ $postgresql_version_minor \* 1000 \
150                                   \+ $postgresql_version_micro`
151
152        if test -n "$postgresql_version_req"; then
153
154            AC_MSG_CHECKING([if PostgreSQL version is >= $postgresql_version_req])
155
156            dnl Decompose required version string of PostgreSQL
157            dnl and calculate its number representation
158            postgresql_version_req_major=`expr $postgresql_version_req : '\([[0-9]]*\)'`
159            postgresql_version_req_minor=`expr $postgresql_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
160            postgresql_version_req_micro=`expr $postgresql_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
161            if test "x$postgresql_version_req_micro" = "x"; then
162                postgresql_version_req_micro="0"
163            fi
164
165            postgresql_version_req_number=`expr $postgresql_version_req_major \* 1000000 \
166                                       \+ $postgresql_version_req_minor \* 1000 \
167                                       \+ $postgresql_version_req_micro`
168
169            postgresql_version_check=`expr $postgresql_version_number \>\= $postgresql_version_req_number`
170            if test "$postgresql_version_check" = "1"; then
171                AC_MSG_RESULT([yes])
172            else
173                AC_MSG_RESULT([no])
174            fi
175
176	fi
177
178    fi
179
180    AC_SUBST([POSTGRESQL_CFLAGS])
181    AC_SUBST([POSTGRESQL_LDFLAGS])
182    AC_SUBST([POSTGRESQL_LIBS])
183    AC_SUBST([POSTGRESQL_VERSION])
184])
185