Skip to content

Commit 6b18540

Browse files
committed
Add optional pkg-config support for PostgreSQL
The pkg-config (libpq.pc file) was added in PostgreSQL 9.3. This adds a common setup M4 macro PHP_SETUP_PGSQL to find libpq on the system with pkg-config. If not found, it falls back to pg_config to find the libpq library and its headers in common locations as before. When using pkg-config, the PGSQL_CFLAGS and PGSQL_LIBS environment variables can override the paths to libpq installation: ./configure --with-pgsql --with-pdo-pgsql \ PGSQL_CFLAGS=-I/path/to/libpq \ PGSQL_LIBS="-L/path/to/libpq -lpq" Passing manual, non-standard installation paths can be done with option arguments: ./configure \ --with-pgsql=/any/path/to/postgresql \ --with-pdo-postgresql=/any/path/to/postgresql Follow-up of GH-4235 (Use PKG_CHECK_MODULES to detect the pq library) This also removes the unused HAVE_LIBPQ symbol
1 parent 2969889 commit 6b18540

File tree

4 files changed

+100
-137
lines changed

4 files changed

+100
-137
lines changed

UPGRADING.INTERNALS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ PHP 8.4 INTERNALS UPGRADE NOTES
131131
- Symbol HAVE_MYSQL has been removed.
132132
- Symbol HAVE_PDO_SQLITELIB has been removed.
133133
- Symbol HAVE_WAITPID has been removed.
134+
- Symbol HAVE_LIBPQ has been removed.
134135
- M4 macro PHP_DEFINE (atomic includes) removed (use AC_DEFINE and config.h).
135136
- M4 macro PHP_WITH_SHARED has been removed (use PHP_ARG_WITH).
136137
- M4 macro PHP_STRUCT_FLOCK has been removed (use AC_CHECK_TYPES).
@@ -142,6 +143,8 @@ PHP 8.4 INTERNALS UPGRADE NOTES
142143
- PDO extensions in php-src don't have the include flag -I$pdo_cv_inc_path
143144
directory anymore.
144145
- M4 macro PHP_SETUP_OPENSSL doesn't accept the 3rd argument anymore.
146+
- Added optional support for pkg-config in ext/pgsql and ext/pdo_pgsql for
147+
finding libpq >= 9.3.
145148

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

build/php.m4

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,71 @@ PKG_CHECK_MODULES([SQLITE], [sqlite3 >= 3.7.7], [
19291929
])
19301930
])
19311931

1932+
dnl
1933+
dnl PHP_SETUP_PGSQL([shared-add [, action-found [, action-not-found [, pgsql-dir]]]])
1934+
dnl
1935+
dnl Common setup macro for PostgreSQL library (libpq). Support for pkg-config
1936+
dnl was introduced in PostgreSQL 9.3 and minimum supported libpq in PHP is 9.1.
1937+
dnl If library can't be found with pkg-config, check falls back to pg_config.
1938+
dnl When not using pkg-config, the optional pgsql-dir is the PostgreSQL base
1939+
dnl install directory or the path to pg_config.
1940+
dnl
1941+
AC_DEFUN([PHP_SETUP_PGSQL],
1942+
[PKG_CHECK_MODULES([PGSQL], [libpq >= 9.3], [found_pgsql=yes], [
1943+
dnl Undocumented BC feature: The PGSQL_INCLUDE variable can be also passed to
1944+
dnl configure script to override the PostgreSQL library include directory.
1945+
PHP_EXPAND_PATH([$PGSQL_INCLUDE], [PGSQL_INCLUDE])
1946+
dnl Set installation PostgreSQL directory if given from configure argument.
1947+
AS_CASE([$4], [yes], [pgsql_dir=""], [pgsql_dir=$4])
1948+
AC_MSG_CHECKING([for pg_config])
1949+
for i in $pgsql_dir $pgsql_dir/bin /usr/local/pgsql/bin /usr/local/bin /usr/bin ""; do
1950+
AS_IF([test -x $i/pg_config], [PG_CONFIG="$i/pg_config"; break;])
1951+
done
1952+
1953+
AS_VAR_IF([PG_CONFIG], [], [
1954+
AC_MSG_RESULT([not found])
1955+
AS_VAR_IF([pgsql_dir], [],
1956+
[PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql"],
1957+
[PGSQL_SEARCH_PATHS=$pgsql_dir])
1958+
1959+
for i in $PGSQL_SEARCH_PATHS; do
1960+
for j in include include/pgsql include/postgres include/postgresql ""; do
1961+
AS_IF([test -r "$i/$j/libpq-fe.h"], [PGSQL_INCLUDE=$i/$j])
1962+
done
1963+
1964+
for j in $PHP_LIBDIR lib $PHP_LIBDIR/pgsql $PHP_LIBDIR/postgres $PHP_LIBDIR/postgresql ""; do
1965+
AS_IF([test -f "$i/$j/libpq.so" || test -f "$i/$j/libpq.a"],
1966+
[PGSQL_LIBDIR=$i/$j])
1967+
done
1968+
done
1969+
], [
1970+
AC_MSG_RESULT([$PG_CONFIG])
1971+
PGSQL_INCLUDE=`$PG_CONFIG --includedir`
1972+
PGSQL_LIBDIR=`$PG_CONFIG --libdir`
1973+
])
1974+
1975+
AS_IF([test -z "$PGSQL_INCLUDE" || test -z "PGSQL_LIBDIR"], [found_pgsql=no], [
1976+
found_pgsql=yes
1977+
PGSQL_CFLAGS="-I$PGSQL_INCLUDE"
1978+
PGSQL_LIBS="-L$PGSQL_LIBDIR -lpq"
1979+
])
1980+
])
1981+
1982+
AS_VAR_IF([found_pgsql], [yes], [
1983+
PHP_EVAL_INCLINE([$PGSQL_CFLAGS])
1984+
PHP_EVAL_LIBLINE([$PGSQL_LIBS], [$1])
1985+
PHP_CHECK_LIBRARY([pq], [PQlibVersion],,
1986+
[AC_MSG_ERROR([PostgreSQL check failed: libpq 9.1 or later is required.])],
1987+
[$PGSQL_LIBS])
1988+
$2],
1989+
[m4_default([$3], [AC_MSG_ERROR(m4_normalize([
1990+
Cannot find libpq-fe.h or libpq.so anywhere under $PGSQL_SEARCH_PATHS. Please
1991+
specify correct PostgreSQL installation path with pkg-config environment
1992+
variables PGSQL_CFLAGS and PGSQL_LIBS or pass the libpq installation directory
1993+
argument.
1994+
]))])])
1995+
])
1996+
19321997
dnl ----------------------------------------------------------------------------
19331998
dnl Misc. macros
19341999
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. When using pkg-config, the PGSQL_CFLAGS
6+
and PGSQL_LIBS environment variables can be used instead of the DIR argument
7+
to customize 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: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,35 @@
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. When using pkg-config, the PGSQL_CFLAGS
6+
and PGSQL_LIBS environment variables can be used instead of the DIR argument
7+
to customize 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])
6030

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"
6531
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]))
32+
CFLAGS="$CFLAGS $PGSQL_CFLAGS"
7333

7434
dnl Available since PostgreSQL 12.
7535
AC_CACHE_CHECK([if PGVerbosity enum has PQERRORS_SQLSTATE],
@@ -82,15 +42,7 @@ if test "$PHP_PGSQL" != "no"; then
8242
[AC_DEFINE([HAVE_PQERRORS_SQLSTATE], [1],
8343
[Define to 1 if PGVerbosity enum has PQERRORS_SQLSTATE.])])
8444

85-
LIBS=$old_LIBS
86-
LDFLAGS=$old_LDFLAGS
8745
CFLAGS=$old_CFLAGS
8846

89-
PHP_ADD_LIBRARY_WITH_PATH(pq, $PGSQL_LIBDIR, PGSQL_SHARED_LIBADD)
90-
PHP_SUBST(PGSQL_SHARED_LIBADD)
91-
92-
PHP_ADD_INCLUDE($PGSQL_INCLUDE)
93-
9447
PHP_NEW_EXTENSION(pgsql, pgsql.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
95-
PHP_ADD_EXTENSION_DEP(pgsql, pcre)
9648
fi

0 commit comments

Comments
 (0)