Skip to content

Commit 9f1f6de

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" Follow-up of phpGH-4235 (Use PKG_CHECK_MODULES to detect the pq library)
1 parent bfc9885 commit 9f1f6de

File tree

4 files changed

+84
-131
lines changed

4 files changed

+84
-131
lines changed

UPGRADING.INTERNALS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ PHP 8.4 INTERNALS UPGRADE NOTES
142142
- PDO extensions in php-src don't have the include flag -I$pdo_cv_inc_path
143143
directory anymore.
144144
- M4 macro PHP_SETUP_OPENSSL doesn't accept the 3rd argument anymore.
145+
- Added optional support for pkg-config in ext/pgsql and ext/pdo_pgsql for
146+
finding libpq >= 9.3.
145147

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

build/php.m4

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,73 @@ 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+
[found_pgsql=no
1943+
PKG_CHECK_MODULES([PGSQL], [libpq >= 9.3], [
1944+
PHP_EVAL_INCLINE([$PGSQL_CFLAGS])
1945+
PHP_EVAL_LIBLINE([$PGSQL_LIBS], [$1])
1946+
found_pgsql=yes
1947+
], [
1948+
dnl Undocumented BC feature: The PGSQL_INCLUDE variable can be also passed to
1949+
dnl configure script to override the PostgreSQL library include directory.
1950+
PHP_EXPAND_PATH([$PGSQL_INCLUDE], [PGSQL_INCLUDE])
1951+
dnl Set installation PostgreSQL directory if given from configure argument.
1952+
AS_CASE([$4], [yes], [pgsql_dir=""], [pgsql_dir=$4])
1953+
AC_MSG_CHECKING([for pg_config])
1954+
for i in $pgsql_dir $pgsql_dir/bin /usr/local/pgsql/bin /usr/local/bin /usr/bin ""; do
1955+
AS_IF([test -x $i/pg_config], [PG_CONFIG="$i/pg_config"; break;])
1956+
done
1957+
1958+
AS_VAR_IF([PG_CONFIG], [], [
1959+
AC_MSG_RESULT([not found])
1960+
AS_VAR_IF([pgsql_dir], [],
1961+
[PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql"],
1962+
[PGSQL_SEARCH_PATHS=$pgsql_dir])
1963+
1964+
for i in $PGSQL_SEARCH_PATHS; do
1965+
for j in include include/pgsql include/postgres include/postgresql ""; do
1966+
AS_IF([test -r "$i/$j/libpq-fe.h"], [PGSQL_INCLUDE=$i/$j])
1967+
done
1968+
1969+
for j in $PHP_LIBDIR lib $PHP_LIBDIR/pgsql $PHP_LIBDIR/postgres $PHP_LIBDIR/postgresql ""; do
1970+
AS_IF([test -f "$i/$j/libpq.so" || test -f "$i/$j/libpq.a"],
1971+
[PGSQL_LIBDIR=$i/$j])
1972+
done
1973+
done
1974+
], [
1975+
AC_MSG_RESULT([$PG_CONFIG])
1976+
PGSQL_INCLUDE=`$PG_CONFIG --includedir`
1977+
PGSQL_LIBDIR=`$PG_CONFIG --libdir`
1978+
])
1979+
1980+
AS_IF([test -z "$PGSQL_INCLUDE" || test -z "PGSQL_LIBDIR"], [found_pgsql=no], [
1981+
found_pgsql=yes
1982+
PHP_ADD_LIBRARY_WITH_PATH([pq], [$PGSQL_LIBDIR], [$1])
1983+
PHP_ADD_INCLUDE([$PGSQL_INCLUDE])
1984+
])
1985+
])
1986+
1987+
AS_VAR_IF([found_pgsql], [yes],
1988+
[AC_CHECK_LIB([pq], [PQlibVersion], [:],
1989+
[AC_MSG_ERROR([PostgreSQL check failed: libpq 9.1 or later is required.])])
1990+
$2],
1991+
[m4_default([$3], [AC_MSG_ERROR(m4_normalize([
1992+
Cannot find libpq-fe.h or libpq.so anywhere under $PGSQL_SEARCH_PATHS. Please
1993+
specify correct PostgreSQL installation path with pkg-config environment
1994+
variables PGSQL_CFLAGS and PGSQL_LIBS or pass the libpq installation directory
1995+
argument.
1996+
]))])])
1997+
])
1998+
19321999
dnl ----------------------------------------------------------------------------
19332000
dnl Misc. macros
19342001
dnl ----------------------------------------------------------------------------

ext/pdo_pgsql/config.m4

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,23 @@
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]))
7019
AC_CHECK_LIB(pq, PQresultMemorySize, AC_DEFINE(HAVE_PG_RESULT_MEMORY_SIZE,1,[PostgreSQL 12 or later]))
7120

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)
79-
8021
PHP_CHECK_PDO_INCLUDES
8122

8223
PHP_NEW_EXTENSION(pdo_pgsql, pdo_pgsql.c pgsql_driver.c pgsql_statement.c, $ext_shared)

ext/pgsql/config.m4

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,20 @@
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)
9-
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
56-
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
10+
PHP_SETUP_PGSQL([PGSQL_SHARED_LIBADD],,, [$PHP_PGSQL])
11+
PHP_SUBST([PGSQL_SHARED_LIBADD])
6012

6113
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"
14+
6515
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]))
16+
CFLAGS="$CFLAGS -I$INCLUDES"
17+
6818
AC_CHECK_LIB(pq, pg_encoding_to_char,AC_DEFINE(HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT,1,[Whether libpq is compiled with --enable-multibyte]))
6919
AC_CHECK_LIB(pq, lo_truncate64, AC_DEFINE(HAVE_PG_LO64,1,[PostgreSQL 9.3 or later]))
7020
AC_CHECK_LIB(pq, PQsetErrorContextVisibility, AC_DEFINE(HAVE_PG_CONTEXT_VISIBILITY,1,[PostgreSQL 9.6 or later]))
@@ -83,14 +33,7 @@ if test "$PHP_PGSQL" != "no"; then
8333
[AC_DEFINE([HAVE_PQERRORS_SQLSTATE], [1],
8434
[Define to 1 if PGVerbosity enum has PQERRORS_SQLSTATE.])])
8535

86-
LIBS=$old_LIBS
87-
LDFLAGS=$old_LDFLAGS
8836
CFLAGS=$old_CFLAGS
8937

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-
9538
PHP_NEW_EXTENSION(pgsql, pgsql.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
9639
fi

0 commit comments

Comments
 (0)