Skip to content

Commit f4ce50d

Browse files
authored
Add optional pkg-config support for PostgreSQL library (libpq) (#14540)
The pkg-config (libpq.pc file) was added in PostgreSQL 9.3. This adds a common setup M4 macro PHP_SETUP_PGSQL to find client PostgreSQL library libpq on the system with pkg-config. If not found, check falls back to pg_config to find the libpq and its headers in common locations as before. The PGSQL_CFLAGS and PGSQL_LIBS environment variables can override the libpq installation paths: ./configure --with-pgsql --with-pdo-pgsql \ PGSQL_CFLAGS=-I/path/to/libpq \ PGSQL_LIBS="-L/path/to/libpq -lpq" Passing manual, non-standard PostgreSQL installation path can be done with configure option arguments: ./configure \ --with-pgsql=/any/path/to/postgresql \ --with-pdo-postgresql=/any/path/to/postgresql If this DIR argument (PostgreSQL installation directory or path to the pg_config) is passed, it takes precedence over the pkg-config, when installed on the system. This also removes the unused HAVE_LIBPQ symbol and passing the PGSQL_INCLUDE and PGSQL_LIBDIR environment variable to configure in favor of PGSQL_CFLAGS and PGSQL_LIBS. Instead of the obsolete backticks the recommended $(...) is used when invoking the pg_config. Follow-up of GH-4235 (Use PKG_CHECK_MODULES to detect the pq library)
1 parent 5d359cd commit f4ce50d

File tree

4 files changed

+111
-137
lines changed

4 files changed

+111
-137
lines changed

UPGRADING.INTERNALS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ PHP 8.4 INTERNALS UPGRADE NOTES
134134
- Symbol HAVE_MYSQL has been removed.
135135
- Symbol HAVE_PDO_SQLITELIB has been removed.
136136
- Symbol HAVE_WAITPID has been removed.
137+
- Symbol HAVE_LIBPQ has been removed.
137138
- M4 macro PHP_DEFINE (atomic includes) removed (use AC_DEFINE and config.h).
138139
- M4 macro PHP_WITH_SHARED has been removed (use PHP_ARG_WITH).
139140
- M4 macro PHP_STRUCT_FLOCK has been removed (use AC_CHECK_TYPES).
@@ -145,6 +146,11 @@ PHP 8.4 INTERNALS UPGRADE NOTES
145146
- PDO extensions in php-src don't have the include flag -I$pdo_cv_inc_path
146147
directory anymore.
147148
- M4 macro PHP_SETUP_OPENSSL doesn't accept the 3rd argument anymore.
149+
- Added pkg-config support to find libpq for the pdo_pgsql and pgsql
150+
extensions. The libpq paths can be customized with the PGSQL_CFLAGS and
151+
PGSQL_LIBS environment variables. When a directory argument is provided to
152+
configure options (--with-pgsql=DIR or --with-pdo-pgsql=DIR), it will be
153+
used instead of the pkg-config search.
148154

149155
c. Windows build system changes
150156
- The configure options --with-oci8-11g, --with-oci8-12c, --with-oci8-19 have

build/php.m4

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,76 @@ PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.11], [dnl
19431943
$2], [$3])dnl
19441944
])
19451945

1946+
dnl
1947+
dnl PHP_SETUP_PGSQL([shared-add [, action-found [, action-not-found [, pgsql-dir]]]])
1948+
dnl
1949+
dnl Common setup macro for PostgreSQL library (libpq). The optional "pgsql-dir"
1950+
dnl is the PostgreSQL base install directory or the path to pg_config. Support
1951+
dnl for pkg-config was introduced in PostgreSQL 9.3. If library can't be found
1952+
dnl with pkg-config, check falls back to pg_config. If libpq is not found, error
1953+
dnl is thrown, unless the "action-not-found" is given.
1954+
dnl
1955+
AC_DEFUN([PHP_SETUP_PGSQL], [dnl
1956+
found_pgsql=no
1957+
dnl Set PostgreSQL installation directory if given from the configure argument.
1958+
AS_CASE([$4], [yes], [pgsql_dir=""], [pgsql_dir=$4])
1959+
AS_VAR_IF([pgsql_dir],,
1960+
[PKG_CHECK_MODULES([PGSQL], [libpq >= 9.3],
1961+
[found_pgsql=yes],
1962+
[found_pgsql=no])])
1963+
1964+
AS_VAR_IF([found_pgsql], [no], [dnl
1965+
AC_MSG_CHECKING([for pg_config])
1966+
for i in $pgsql_dir $pgsql_dir/bin /usr/local/pgsql/bin /usr/local/bin /usr/bin ""; do
1967+
AS_IF([test -x $i/pg_config], [PG_CONFIG="$i/pg_config"; break;])
1968+
done
1969+
1970+
AS_VAR_IF([PG_CONFIG],, [dnl
1971+
AC_MSG_RESULT([not found])
1972+
AS_VAR_IF([pgsql_dir],,
1973+
[pgsql_search_paths="/usr /usr/local /usr/local/pgsql"],
1974+
[pgsql_search_paths=$pgsql_dir])
1975+
1976+
for i in $pgsql_search_paths; do
1977+
for j in include include/pgsql include/postgres include/postgresql ""; do
1978+
AS_IF([test -r "$i/$j/libpq-fe.h"], [PGSQL_INCLUDE=$i/$j])
1979+
done
1980+
1981+
for j in $PHP_LIBDIR lib $PHP_LIBDIR/pgsql $PHP_LIBDIR/postgres $PHP_LIBDIR/postgresql ""; do
1982+
AS_IF([test -f "$i/$j/libpq.so" || test -f "$i/$j/libpq.a"],
1983+
[PGSQL_LIBDIR=$i/$j])
1984+
done
1985+
done
1986+
], [dnl
1987+
AC_MSG_RESULT([$PG_CONFIG])
1988+
PGSQL_INCLUDE=$($PG_CONFIG --includedir)
1989+
PGSQL_LIBDIR=$($PG_CONFIG --libdir)
1990+
])
1991+
1992+
AS_IF([test -n "$PGSQL_INCLUDE" && test -n "PGSQL_LIBDIR"], [
1993+
found_pgsql=yes
1994+
PGSQL_CFLAGS="-I$PGSQL_INCLUDE"
1995+
PGSQL_LIBS="-L$PGSQL_LIBDIR -lpq"
1996+
])dnl
1997+
])
1998+
1999+
AS_VAR_IF([found_pgsql], [yes], [dnl
2000+
PHP_EVAL_INCLINE([$PGSQL_CFLAGS])
2001+
PHP_EVAL_LIBLINE([$PGSQL_LIBS], [$1])
2002+
dnl PostgreSQL minimum version sanity check.
2003+
PHP_CHECK_LIBRARY([pq], [PQlibVersion],, [AC_MSG_ERROR([m4_normalize([
2004+
PostgreSQL check failed: libpq 9.1 or later is required, please see
2005+
config.log for details.
2006+
])])],
2007+
[$PGSQL_LIBS])
2008+
$2],
2009+
[m4_default([$3], [AC_MSG_ERROR([m4_normalize([
2010+
Cannot find libpq-fe.h or pq library (libpq). Please specify the correct
2011+
PostgreSQL installation path with environment variables PGSQL_CFLAGS and
2012+
PGSQL_LIBS or provide the PostgreSQL installation directory.
2013+
])])])])
2014+
])
2015+
19462016
dnl ----------------------------------------------------------------------------
19472017
dnl Misc. macros
19482018
dnl ----------------------------------------------------------------------------

ext/pdo_pgsql/config.m4

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,24 @@
11
PHP_ARG_WITH([pdo-pgsql],
22
[for PostgreSQL support for PDO],
33
[AS_HELP_STRING([[--with-pdo-pgsql[=DIR]]],
4-
[PDO: PostgreSQL support. DIR is the PostgreSQL base install directory or
5-
the path to pg_config])])
4+
[PDO: PostgreSQL support. Optional DIR is the PostgreSQL base install
5+
directory or the path to pg_config. Also, the PGSQL_CFLAGS and PGSQL_LIBS
6+
environment variables can be used instead of the DIR argument to customize
7+
the libpq paths.])])
68

79
if test "$PHP_PDO_PGSQL" != "no"; then
8-
910
if test "$PHP_PDO" = "no" && test "$ext_shared" = "no"; then
1011
AC_MSG_ERROR([PDO is not enabled! Add --enable-pdo to your configure line.])
1112
fi
1213

13-
PHP_EXPAND_PATH($PGSQL_INCLUDE, PGSQL_INCLUDE)
14-
15-
AC_MSG_CHECKING(for pg_config)
16-
for i in $PHP_PDO_PGSQL $PHP_PDO_PGSQL/bin /usr/local/pgsql/bin /usr/local/bin /usr/bin ""; do
17-
if test -x $i/pg_config; then
18-
PG_CONFIG="$i/pg_config"
19-
break;
20-
fi
21-
done
22-
23-
if test -n "$PG_CONFIG"; then
24-
AC_MSG_RESULT([$PG_CONFIG])
25-
PGSQL_INCLUDE=`$PG_CONFIG --includedir`
26-
PGSQL_LIBDIR=`$PG_CONFIG --libdir`
27-
else
28-
AC_MSG_RESULT(not found)
29-
if test "$PHP_PDO_PGSQL" = "yes"; then
30-
PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql"
31-
else
32-
PGSQL_SEARCH_PATHS=$PHP_PDO_PGSQL
33-
fi
34-
35-
for i in $PGSQL_SEARCH_PATHS; do
36-
for j in include include/pgsql include/postgres include/postgresql ""; do
37-
if test -r "$i/$j/libpq-fe.h"; then
38-
PGSQL_INC_BASE=$i
39-
PGSQL_INCLUDE=$i/$j
40-
fi
41-
done
42-
43-
for j in $PHP_LIBDIR $PHP_LIBDIR/pgsql $PHP_LIBDIR/postgres $PHP_LIBDIR/postgresql ""; do
44-
if test -f "$i/$j/libpq.so" || test -f "$i/$j/libpq.a"; then
45-
PGSQL_LIBDIR=$i/$j
46-
fi
47-
done
48-
done
49-
fi
50-
51-
if test -z "$PGSQL_INCLUDE"; then
52-
AC_MSG_ERROR(Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path)
53-
fi
54-
55-
if test -z "$PGSQL_LIBDIR"; then
56-
AC_MSG_ERROR(Cannot find libpq.so. Please specify correct PostgreSQL installation path)
57-
fi
58-
59-
if test -z "$PGSQL_INCLUDE" && test -z "$PGSQL_LIBDIR"; then
60-
AC_MSG_ERROR([Unable to find libpq anywhere under $PGSQL_SEARCH_PATHS])
61-
fi
14+
PHP_SETUP_PGSQL([PDO_PGSQL_SHARED_LIBADD],,, [$PHP_PDO_PGSQL])
15+
PHP_SUBST([PDO_PGSQL_SHARED_LIBADD])
6216

6317
AC_DEFINE(HAVE_PDO_PGSQL,1,[Whether to build PostgreSQL for PDO support or not])
6418

65-
old_LIBS=$LIBS
66-
old_LDFLAGS=$LDFLAGS
67-
LDFLAGS="-L$PGSQL_LIBDIR $LDFLAGS"
68-
69-
AC_CHECK_LIB(pq, PQlibVersion,, AC_MSG_ERROR([Unable to build the PDO PostgreSQL driver: at least libpq 9.1 is required]))
70-
AC_CHECK_LIB(pq, PQresultMemorySize, AC_DEFINE(HAVE_PG_RESULT_MEMORY_SIZE,1,[PostgreSQL 12 or later]))
71-
72-
LIBS=$old_LIBS
73-
LDFLAGS=$old_LDFLAGS
74-
75-
PHP_ADD_LIBRARY_WITH_PATH(pq, $PGSQL_LIBDIR, PDO_PGSQL_SHARED_LIBADD)
76-
PHP_SUBST(PDO_PGSQL_SHARED_LIBADD)
77-
78-
PHP_ADD_INCLUDE($PGSQL_INCLUDE)
19+
PHP_CHECK_LIBRARY([pq], [PQresultMemorySize],
20+
[AC_DEFINE([HAVE_PG_RESULT_MEMORY_SIZE], [1], [PostgreSQL 12 or later])],,
21+
[$PGSQL_LIBS])
7922

8023
PHP_CHECK_PDO_INCLUDES
8124

ext/pgsql/config.m4

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,38 @@
11
PHP_ARG_WITH([pgsql],
22
[for PostgreSQL support],
33
[AS_HELP_STRING([[--with-pgsql[=DIR]]],
4-
[Include PostgreSQL support. DIR is the PostgreSQL base install directory or
5-
the path to pg_config])])
4+
[Include PostgreSQL support. Optional DIR is the PostgreSQL base install
5+
directory or the path to pg_config. Also, the PGSQL_CFLAGS and PGSQL_LIBS
6+
environment variables can be used instead of the DIR argument to customize
7+
the libpq paths.])])
68

79
if test "$PHP_PGSQL" != "no"; then
8-
PHP_EXPAND_PATH($PGSQL_INCLUDE, PGSQL_INCLUDE)
10+
PHP_SETUP_PGSQL([PGSQL_SHARED_LIBADD],,, [$PHP_PGSQL])
11+
PHP_SUBST([PGSQL_SHARED_LIBADD])
912

10-
dnl pg_config is still the default way to retrieve build options
11-
dnl pkgconfig support was only introduced in 9.3
12-
13-
AC_MSG_CHECKING(for pg_config)
14-
for i in $PHP_PGSQL $PHP_PGSQL/bin /usr/local/pgsql/bin /usr/local/bin /usr/bin ""; do
15-
if test -x $i/pg_config; then
16-
PG_CONFIG="$i/pg_config"
17-
break;
18-
fi
19-
done
20-
21-
if test -n "$PG_CONFIG"; then
22-
AC_MSG_RESULT([$PG_CONFIG])
23-
PGSQL_INCLUDE=`$PG_CONFIG --includedir`
24-
PGSQL_LIBDIR=`$PG_CONFIG --libdir`
25-
else
26-
AC_MSG_RESULT(not found)
27-
if test "$PHP_PGSQL" = "yes"; then
28-
PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql"
29-
else
30-
PGSQL_SEARCH_PATHS=$PHP_PGSQL
31-
fi
32-
33-
for i in $PGSQL_SEARCH_PATHS; do
34-
for j in include include/pgsql include/postgres include/postgresql ""; do
35-
if test -r "$i/$j/libpq-fe.h"; then
36-
PGSQL_INC_BASE=$i
37-
PGSQL_INCLUDE=$i/$j
38-
fi
39-
done
40-
41-
for j in lib $PHP_LIBDIR/pgsql $PHP_LIBDIR/postgres $PHP_LIBDIR/postgresql ""; do
42-
if test -f "$i/$j/libpq.so" || test -f "$i/$j/libpq.a"; then
43-
PGSQL_LIBDIR=$i/$j
44-
fi
45-
done
46-
done
47-
fi
48-
49-
if test -z "$PGSQL_INCLUDE"; then
50-
AC_MSG_ERROR(Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path)
51-
fi
52-
53-
if test -z "$PGSQL_LIBDIR"; then
54-
AC_MSG_ERROR(Cannot find libpq.so. Please specify correct PostgreSQL installation path)
55-
fi
13+
AC_DEFINE(HAVE_PGSQL,1,[Whether to build PostgreSQL support or not])
5614

57-
if test -z "$PGSQL_INCLUDE" && test -z "$PGSQL_LIBDIR"; then
58-
AC_MSG_ERROR([Unable to find libpq anywhere under $PGSQL_SEARCH_PATHS])
59-
fi
15+
PHP_CHECK_LIBRARY([pq], [lo_truncate64],
16+
[AC_DEFINE([HAVE_PG_LO64], [1], [PostgreSQL 9.3 or later])],,
17+
[$PGSQL_LIBS])
18+
PHP_CHECK_LIBRARY([pq], [PQsetErrorContextVisibility],
19+
[AC_DEFINE([HAVE_PG_CONTEXT_VISIBILITY], [1], [PostgreSQL 9.6 or later])],,
20+
[$PGSQL_LIBS])
21+
PHP_CHECK_LIBRARY([pq], [PQresultMemorySize],
22+
[AC_DEFINE([HAVE_PG_RESULT_MEMORY_SIZE], [1], [PostgreSQL 12 or later])],,
23+
[$PGSQL_LIBS])
24+
PHP_CHECK_LIBRARY([pq], [PQchangePassword],
25+
[AC_DEFINE([HAVE_PG_CHANGE_PASSWORD], [1], [PostgreSQL 17 or later])],,
26+
[$PGSQL_LIBS])
27+
PHP_CHECK_LIBRARY([pq], [PQsocketPoll],
28+
[AC_DEFINE([HAVE_PG_SOCKET_POLL], [1], [PostgreSQL 17 or later])],,
29+
[$PGSQL_LIBS])
30+
PHP_CHECK_LIBRARY([pq], [PQsetChunkedRowsMode],
31+
[AC_DEFINE([HAVE_PG_SET_CHUNKED_ROWS_SIZE], [1], [PostgreSQL 17 or later])],,
32+
[$PGSQL_LIBS])
6033

61-
AC_DEFINE(HAVE_PGSQL,1,[Whether to build PostgreSQL support or not])
62-
old_LIBS=$LIBS
63-
old_LDFLAGS=$LDFLAGS
64-
LDFLAGS="-L$PGSQL_LIBDIR $LDFLAGS"
6534
old_CFLAGS=$CFLAGS
66-
CFLAGS="$CFLAGS -I$PGSQL_INCLUDE"
67-
AC_CHECK_LIB(pq, PQlibVersion,, AC_MSG_ERROR([Unable to build the PostgreSQL extension: at least libpq 9.1 is required]))
68-
AC_CHECK_LIB(pq, lo_truncate64, AC_DEFINE(HAVE_PG_LO64,1,[PostgreSQL 9.3 or later]))
69-
AC_CHECK_LIB(pq, PQsetErrorContextVisibility, AC_DEFINE(HAVE_PG_CONTEXT_VISIBILITY,1,[PostgreSQL 9.6 or later]))
70-
AC_CHECK_LIB(pq, PQresultMemorySize, AC_DEFINE(HAVE_PG_RESULT_MEMORY_SIZE,1,[PostgreSQL 12 or later]))
71-
AC_CHECK_LIB(pq, PQchangePassword, AC_DEFINE(HAVE_PG_CHANGE_PASSWORD,1,[PostgreSQL 17 or later]))
72-
AC_CHECK_LIB(pq, PQsocketPoll, AC_DEFINE(HAVE_PG_SOCKET_POLL,1,[PostgreSQL 17 or later]))
73-
AC_CHECK_LIB(pq, PQsetChunkedRowsMode, AC_DEFINE(HAVE_PG_SET_CHUNKED_ROWS_SIZE,1,[PostgreSQL 17 or later]))
35+
CFLAGS="$CFLAGS $PGSQL_CFLAGS"
7436

7537
dnl Available since PostgreSQL 12.
7638
AC_CACHE_CHECK([if PGVerbosity enum has PQERRORS_SQLSTATE],
@@ -83,15 +45,8 @@ if test "$PHP_PGSQL" != "no"; then
8345
[AC_DEFINE([HAVE_PQERRORS_SQLSTATE], [1],
8446
[Define to 1 if PGVerbosity enum has PQERRORS_SQLSTATE.])])
8547

86-
LIBS=$old_LIBS
87-
LDFLAGS=$old_LDFLAGS
8848
CFLAGS=$old_CFLAGS
8949

90-
PHP_ADD_LIBRARY_WITH_PATH(pq, $PGSQL_LIBDIR, PGSQL_SHARED_LIBADD)
91-
PHP_SUBST(PGSQL_SHARED_LIBADD)
92-
93-
PHP_ADD_INCLUDE($PGSQL_INCLUDE)
94-
9550
PHP_NEW_EXTENSION(pgsql, pgsql.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
9651
PHP_ADD_EXTENSION_DEP(pgsql, pcre)
9752
fi

0 commit comments

Comments
 (0)