Skip to content

Add optional pkg-config support for PostgreSQL #14540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ PHP 8.4 INTERNALS UPGRADE NOTES
- Symbol HAVE_MYSQL has been removed.
- Symbol HAVE_PDO_SQLITELIB has been removed.
- Symbol HAVE_WAITPID has been removed.
- Symbol HAVE_LIBPQ has been removed.
- M4 macro PHP_DEFINE (atomic includes) removed (use AC_DEFINE and config.h).
- M4 macro PHP_WITH_SHARED has been removed (use PHP_ARG_WITH).
- M4 macro PHP_STRUCT_FLOCK has been removed (use AC_CHECK_TYPES).
Expand All @@ -145,6 +146,11 @@ PHP 8.4 INTERNALS UPGRADE NOTES
- PDO extensions in php-src don't have the include flag -I$pdo_cv_inc_path
directory anymore.
- M4 macro PHP_SETUP_OPENSSL doesn't accept the 3rd argument anymore.
- Added pkg-config support to find libpq for the pdo_pgsql and pgsql
extensions. The libpq paths can be customized with the PGSQL_CFLAGS and
PGSQL_LIBS environment variables. When a directory argument is provided to
configure options (--with-pgsql=DIR or --with-pdo-pgsql=DIR), it will be
used instead of the pkg-config search.

c. Windows build system changes
- The configure options --with-oci8-11g, --with-oci8-12c, --with-oci8-19 have
Expand Down
70 changes: 70 additions & 0 deletions build/php.m4
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,76 @@ PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.11], [dnl
$2], [$3])dnl
])

dnl
dnl PHP_SETUP_PGSQL([shared-add [, action-found [, action-not-found [, pgsql-dir]]]])
dnl
dnl Common setup macro for PostgreSQL library (libpq). The optional "pgsql-dir"
dnl is the PostgreSQL base install directory or the path to pg_config. Support
dnl for pkg-config was introduced in PostgreSQL 9.3. If library can't be found
dnl with pkg-config, check falls back to pg_config. If libpq is not found, error
dnl is thrown, unless the "action-not-found" is given.
dnl
AC_DEFUN([PHP_SETUP_PGSQL], [dnl
found_pgsql=no
dnl Set PostgreSQL installation directory if given from the configure argument.
AS_CASE([$4], [yes], [pgsql_dir=""], [pgsql_dir=$4])
AS_VAR_IF([pgsql_dir],,
[PKG_CHECK_MODULES([PGSQL], [libpq >= 9.3],
[found_pgsql=yes],
[found_pgsql=no])])

AS_VAR_IF([found_pgsql], [no], [dnl
AC_MSG_CHECKING([for pg_config])
for i in $pgsql_dir $pgsql_dir/bin /usr/local/pgsql/bin /usr/local/bin /usr/bin ""; do
AS_IF([test -x $i/pg_config], [PG_CONFIG="$i/pg_config"; break;])
done

AS_VAR_IF([PG_CONFIG],, [dnl
AC_MSG_RESULT([not found])
AS_VAR_IF([pgsql_dir],,
[pgsql_search_paths="/usr /usr/local /usr/local/pgsql"],
[pgsql_search_paths=$pgsql_dir])

for i in $pgsql_search_paths; do
for j in include include/pgsql include/postgres include/postgresql ""; do
AS_IF([test -r "$i/$j/libpq-fe.h"], [PGSQL_INCLUDE=$i/$j])
done

for j in $PHP_LIBDIR lib $PHP_LIBDIR/pgsql $PHP_LIBDIR/postgres $PHP_LIBDIR/postgresql ""; do
AS_IF([test -f "$i/$j/libpq.so" || test -f "$i/$j/libpq.a"],
[PGSQL_LIBDIR=$i/$j])
done
done
], [dnl
AC_MSG_RESULT([$PG_CONFIG])
PGSQL_INCLUDE=$($PG_CONFIG --includedir)
PGSQL_LIBDIR=$($PG_CONFIG --libdir)
])

AS_IF([test -n "$PGSQL_INCLUDE" && test -n "PGSQL_LIBDIR"], [
found_pgsql=yes
PGSQL_CFLAGS="-I$PGSQL_INCLUDE"
PGSQL_LIBS="-L$PGSQL_LIBDIR -lpq"
])dnl
])

AS_VAR_IF([found_pgsql], [yes], [dnl
PHP_EVAL_INCLINE([$PGSQL_CFLAGS])
PHP_EVAL_LIBLINE([$PGSQL_LIBS], [$1])
dnl PostgreSQL minimum version sanity check.
PHP_CHECK_LIBRARY([pq], [PQlibVersion],, [AC_MSG_ERROR([m4_normalize([
PostgreSQL check failed: libpq 9.1 or later is required, please see
config.log for details.
])])],
[$PGSQL_LIBS])
$2],
[m4_default([$3], [AC_MSG_ERROR([m4_normalize([
Cannot find libpq-fe.h or pq library (libpq). Please specify the correct
PostgreSQL installation path with environment variables PGSQL_CFLAGS and
PGSQL_LIBS or provide the PostgreSQL installation directory.
])])])])
])

dnl ----------------------------------------------------------------------------
dnl Misc. macros
dnl ----------------------------------------------------------------------------
Expand Down
75 changes: 9 additions & 66 deletions ext/pdo_pgsql/config.m4
Original file line number Diff line number Diff line change
@@ -1,81 +1,24 @@
PHP_ARG_WITH([pdo-pgsql],
[for PostgreSQL support for PDO],
[AS_HELP_STRING([[--with-pdo-pgsql[=DIR]]],
[PDO: PostgreSQL support. DIR is the PostgreSQL base install directory or
the path to pg_config])])
[PDO: PostgreSQL support. Optional DIR is the PostgreSQL base install
directory or the path to pg_config. Also, the PGSQL_CFLAGS and PGSQL_LIBS
environment variables can be used instead of the DIR argument to customize
the libpq paths.])])

if test "$PHP_PDO_PGSQL" != "no"; then

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

PHP_EXPAND_PATH($PGSQL_INCLUDE, PGSQL_INCLUDE)

AC_MSG_CHECKING(for pg_config)
for i in $PHP_PDO_PGSQL $PHP_PDO_PGSQL/bin /usr/local/pgsql/bin /usr/local/bin /usr/bin ""; do
if test -x $i/pg_config; then
PG_CONFIG="$i/pg_config"
break;
fi
done

if test -n "$PG_CONFIG"; then
AC_MSG_RESULT([$PG_CONFIG])
PGSQL_INCLUDE=`$PG_CONFIG --includedir`
PGSQL_LIBDIR=`$PG_CONFIG --libdir`
else
AC_MSG_RESULT(not found)
if test "$PHP_PDO_PGSQL" = "yes"; then
PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql"
else
PGSQL_SEARCH_PATHS=$PHP_PDO_PGSQL
fi

for i in $PGSQL_SEARCH_PATHS; do
for j in include include/pgsql include/postgres include/postgresql ""; do
if test -r "$i/$j/libpq-fe.h"; then
PGSQL_INC_BASE=$i
PGSQL_INCLUDE=$i/$j
fi
done

for j in $PHP_LIBDIR $PHP_LIBDIR/pgsql $PHP_LIBDIR/postgres $PHP_LIBDIR/postgresql ""; do
if test -f "$i/$j/libpq.so" || test -f "$i/$j/libpq.a"; then
PGSQL_LIBDIR=$i/$j
fi
done
done
fi

if test -z "$PGSQL_INCLUDE"; then
AC_MSG_ERROR(Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path)
fi

if test -z "$PGSQL_LIBDIR"; then
AC_MSG_ERROR(Cannot find libpq.so. Please specify correct PostgreSQL installation path)
fi

if test -z "$PGSQL_INCLUDE" && test -z "$PGSQL_LIBDIR"; then
AC_MSG_ERROR([Unable to find libpq anywhere under $PGSQL_SEARCH_PATHS])
fi
PHP_SETUP_PGSQL([PDO_PGSQL_SHARED_LIBADD],,, [$PHP_PDO_PGSQL])
PHP_SUBST([PDO_PGSQL_SHARED_LIBADD])

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

old_LIBS=$LIBS
old_LDFLAGS=$LDFLAGS
LDFLAGS="-L$PGSQL_LIBDIR $LDFLAGS"

AC_CHECK_LIB(pq, PQlibVersion,, AC_MSG_ERROR([Unable to build the PDO PostgreSQL driver: at least libpq 9.1 is required]))
AC_CHECK_LIB(pq, PQresultMemorySize, AC_DEFINE(HAVE_PG_RESULT_MEMORY_SIZE,1,[PostgreSQL 12 or later]))

LIBS=$old_LIBS
LDFLAGS=$old_LDFLAGS

PHP_ADD_LIBRARY_WITH_PATH(pq, $PGSQL_LIBDIR, PDO_PGSQL_SHARED_LIBADD)
PHP_SUBST(PDO_PGSQL_SHARED_LIBADD)

PHP_ADD_INCLUDE($PGSQL_INCLUDE)
PHP_CHECK_LIBRARY([pq], [PQresultMemorySize],
[AC_DEFINE([HAVE_PG_RESULT_MEMORY_SIZE], [1], [PostgreSQL 12 or later])],,
[$PGSQL_LIBS])

PHP_CHECK_PDO_INCLUDES

Expand Down
97 changes: 26 additions & 71 deletions ext/pgsql/config.m4
Original file line number Diff line number Diff line change
@@ -1,76 +1,38 @@
PHP_ARG_WITH([pgsql],
[for PostgreSQL support],
[AS_HELP_STRING([[--with-pgsql[=DIR]]],
[Include PostgreSQL support. DIR is the PostgreSQL base install directory or
the path to pg_config])])
[Include PostgreSQL support. Optional DIR is the PostgreSQL base install
directory or the path to pg_config. Also, the PGSQL_CFLAGS and PGSQL_LIBS
environment variables can be used instead of the DIR argument to customize
the libpq paths.])])

if test "$PHP_PGSQL" != "no"; then
PHP_EXPAND_PATH($PGSQL_INCLUDE, PGSQL_INCLUDE)
PHP_SETUP_PGSQL([PGSQL_SHARED_LIBADD],,, [$PHP_PGSQL])
PHP_SUBST([PGSQL_SHARED_LIBADD])

dnl pg_config is still the default way to retrieve build options
dnl pkgconfig support was only introduced in 9.3

AC_MSG_CHECKING(for pg_config)
for i in $PHP_PGSQL $PHP_PGSQL/bin /usr/local/pgsql/bin /usr/local/bin /usr/bin ""; do
if test -x $i/pg_config; then
PG_CONFIG="$i/pg_config"
break;
fi
done

if test -n "$PG_CONFIG"; then
AC_MSG_RESULT([$PG_CONFIG])
PGSQL_INCLUDE=`$PG_CONFIG --includedir`
PGSQL_LIBDIR=`$PG_CONFIG --libdir`
else
AC_MSG_RESULT(not found)
if test "$PHP_PGSQL" = "yes"; then
PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql"
else
PGSQL_SEARCH_PATHS=$PHP_PGSQL
fi

for i in $PGSQL_SEARCH_PATHS; do
for j in include include/pgsql include/postgres include/postgresql ""; do
if test -r "$i/$j/libpq-fe.h"; then
PGSQL_INC_BASE=$i
PGSQL_INCLUDE=$i/$j
fi
done

for j in lib $PHP_LIBDIR/pgsql $PHP_LIBDIR/postgres $PHP_LIBDIR/postgresql ""; do
if test -f "$i/$j/libpq.so" || test -f "$i/$j/libpq.a"; then
PGSQL_LIBDIR=$i/$j
fi
done
done
fi

if test -z "$PGSQL_INCLUDE"; then
AC_MSG_ERROR(Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path)
fi

if test -z "$PGSQL_LIBDIR"; then
AC_MSG_ERROR(Cannot find libpq.so. Please specify correct PostgreSQL installation path)
fi
AC_DEFINE(HAVE_PGSQL,1,[Whether to build PostgreSQL support or not])

if test -z "$PGSQL_INCLUDE" && test -z "$PGSQL_LIBDIR"; then
AC_MSG_ERROR([Unable to find libpq anywhere under $PGSQL_SEARCH_PATHS])
fi
PHP_CHECK_LIBRARY([pq], [lo_truncate64],
[AC_DEFINE([HAVE_PG_LO64], [1], [PostgreSQL 9.3 or later])],,
[$PGSQL_LIBS])
PHP_CHECK_LIBRARY([pq], [PQsetErrorContextVisibility],
[AC_DEFINE([HAVE_PG_CONTEXT_VISIBILITY], [1], [PostgreSQL 9.6 or later])],,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you aim for 10.0, perhaps checks and ifdefs for HAVE_PG_LO64 and HAVE_PG_CONTEXT_VISIBILITY can be removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this will be cleaned up. In this PR I'd only refactor the check into a separate macro. In the follow up PR, this would be bumped to 10.0 and these conditions removed. And also the PQlibVersion check would be replaced there with PQencryptPasswordConn (added in 10.0).

[$PGSQL_LIBS])
PHP_CHECK_LIBRARY([pq], [PQresultMemorySize],
[AC_DEFINE([HAVE_PG_RESULT_MEMORY_SIZE], [1], [PostgreSQL 12 or later])],,
[$PGSQL_LIBS])
PHP_CHECK_LIBRARY([pq], [PQchangePassword],
[AC_DEFINE([HAVE_PG_CHANGE_PASSWORD], [1], [PostgreSQL 17 or later])],,
[$PGSQL_LIBS])
PHP_CHECK_LIBRARY([pq], [PQsocketPoll],
[AC_DEFINE([HAVE_PG_SOCKET_POLL], [1], [PostgreSQL 17 or later])],,
[$PGSQL_LIBS])
PHP_CHECK_LIBRARY([pq], [PQsetChunkedRowsMode],
[AC_DEFINE([HAVE_PG_SET_CHUNKED_ROWS_SIZE], [1], [PostgreSQL 17 or later])],,
[$PGSQL_LIBS])

AC_DEFINE(HAVE_PGSQL,1,[Whether to build PostgreSQL support or not])
old_LIBS=$LIBS
old_LDFLAGS=$LDFLAGS
LDFLAGS="-L$PGSQL_LIBDIR $LDFLAGS"
old_CFLAGS=$CFLAGS
CFLAGS="$CFLAGS -I$PGSQL_INCLUDE"
AC_CHECK_LIB(pq, PQlibVersion,, AC_MSG_ERROR([Unable to build the PostgreSQL extension: at least libpq 9.1 is required]))
AC_CHECK_LIB(pq, lo_truncate64, AC_DEFINE(HAVE_PG_LO64,1,[PostgreSQL 9.3 or later]))
AC_CHECK_LIB(pq, PQsetErrorContextVisibility, AC_DEFINE(HAVE_PG_CONTEXT_VISIBILITY,1,[PostgreSQL 9.6 or later]))
AC_CHECK_LIB(pq, PQresultMemorySize, AC_DEFINE(HAVE_PG_RESULT_MEMORY_SIZE,1,[PostgreSQL 12 or later]))
AC_CHECK_LIB(pq, PQchangePassword, AC_DEFINE(HAVE_PG_CHANGE_PASSWORD,1,[PostgreSQL 17 or later]))
AC_CHECK_LIB(pq, PQsocketPoll, AC_DEFINE(HAVE_PG_SOCKET_POLL,1,[PostgreSQL 17 or later]))
AC_CHECK_LIB(pq, PQsetChunkedRowsMode, AC_DEFINE(HAVE_PG_SET_CHUNKED_ROWS_SIZE,1,[PostgreSQL 17 or later]))
CFLAGS="$CFLAGS $PGSQL_CFLAGS"

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

LIBS=$old_LIBS
LDFLAGS=$old_LDFLAGS
CFLAGS=$old_CFLAGS

PHP_ADD_LIBRARY_WITH_PATH(pq, $PGSQL_LIBDIR, PGSQL_SHARED_LIBADD)
PHP_SUBST(PGSQL_SHARED_LIBADD)

PHP_ADD_INCLUDE($PGSQL_INCLUDE)

PHP_NEW_EXTENSION(pgsql, pgsql.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
PHP_ADD_EXTENSION_DEP(pgsql, pcre)
fi
Loading