Skip to content

Commit a7dc650

Browse files
committed
Enhance pgsql and pdo_pgsql checks
When checking for features of PostgreSQL library (libpq) there might happen that the libpq library is of certain version (for example, version 17), but the header file libpq-fe.h are of PostgreSQL 16 as they are bundled together in a single postgresql-devel package. This is how the PostgreSQL is packaged on openSUSE Linux, at the time of writing. This is considered an upstream bug because it can cause unexpected behavior when building software depending on libpq. Here, a simple solution is done to check symbols with the check_symbol_exists(), which checks if the linker sees the symbol and if it is also declared in the header. As of PostgreSQL version 14, there are also features macros available in the libpq-fe.h.
1 parent 9eded33 commit a7dc650

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

cmake/ext/pdo_pgsql/CMakeLists.txt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ project(
2727
LANGUAGES C
2828
)
2929

30-
include(CheckLibraryExists)
30+
include(CheckSymbolExists)
3131
include(CMakeDependentOption)
32+
include(CMakePushCheckState)
3233
include(FeatureSummary)
3334

3435
option(PHP_EXT_PDO_PGSQL "Enable the pdo_pgsql extension")
@@ -86,15 +87,19 @@ target_link_libraries(php_ext_pdo_pgsql PUBLIC PostgreSQL::PostgreSQL)
8687

8788
# Sanity check.
8889
if(TARGET PostgreSQL::PostgreSQL)
89-
# PostgreSQL library minimum version sanity check.
90-
check_library_exists(PostgreSQL::PostgreSQL PQlibVersion "" _PHP_LIBPQ_CHECK)
91-
if(NOT _PHP_LIBPQ_CHECK)
92-
message(
93-
FATAL_ERROR
94-
"PostgreSQL check failed: libpq ${PHP_POSTGRESQL_MIN_VERSION} or later "
95-
"is required."
96-
)
97-
endif()
90+
cmake_push_check_state(RESET)
91+
set(CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL)
92+
93+
# PostgreSQL library minimum version sanity check.
94+
check_symbol_exists(PQlibVersion "libpq-fe.h" _PHP_LIBPQ_CHECK)
95+
if(NOT _PHP_LIBPQ_CHECK)
96+
message(
97+
FATAL_ERROR
98+
"PostgreSQL check failed: libpq ${PHP_POSTGRESQL_MIN_VERSION} or later "
99+
"is required."
100+
)
101+
endif()
102+
cmake_pop_check_state()
98103
endif()
99104

100105
set(HAVE_PDO_PGSQL TRUE)

cmake/ext/pgsql/CMakeLists.txt

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ project(
2727
LANGUAGES C
2828
)
2929

30-
include(CheckLibraryExists)
30+
include(CheckSymbolExists)
3131
include(CMakeDependentOption)
32+
include(CMakePushCheckState)
3233
include(FeatureSummary)
3334

3435
option(PHP_EXT_PGSQL "Enable the pgsql extension")
@@ -84,36 +85,37 @@ set_package_properties(
8485
target_link_libraries(php_ext_pgsql PUBLIC PostgreSQL::PostgreSQL)
8586

8687
if(TARGET PostgreSQL::PostgreSQL)
87-
# PostgreSQL library minimum version sanity check.
88-
check_library_exists(PostgreSQL::PostgreSQL PQlibVersion "" _PHP_LIBPQ_CHECK)
89-
if(NOT _PHP_LIBPQ_CHECK)
90-
message(
91-
FATAL_ERROR
92-
"PostgreSQL check failed: libpq ${PHP_POSTGRESQL_MIN_VERSION} or later "
93-
"is required."
88+
cmake_push_check_state(RESET)
89+
set(CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL)
90+
91+
# PostgreSQL library minimum version sanity check.
92+
check_symbol_exists(PQlibVersion "libpq-fe.h" _PHP_LIBPQ_CHECK)
93+
if(NOT _PHP_LIBPQ_CHECK)
94+
message(
95+
FATAL_ERROR
96+
"PostgreSQL check failed: libpq ${PHP_POSTGRESQL_MIN_VERSION} or later "
97+
"is required."
98+
)
99+
endif()
100+
101+
check_symbol_exists(
102+
pg_encoding_to_char
103+
"libpq-fe.h"
104+
HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
94105
)
95-
endif()
96-
97-
check_library_exists(
98-
PostgreSQL::PostgreSQL
99-
pg_encoding_to_char
100-
""
101-
HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
102-
)
103-
104-
check_library_exists(
105-
PostgreSQL::PostgreSQL
106-
lo_truncate64
107-
""
108-
HAVE_PG_LO64
109-
)
110-
111-
check_library_exists(
112-
PostgreSQL::PostgreSQL
113-
PQsetErrorContextVisibility
114-
""
115-
HAVE_PG_CONTEXT_VISIBILITY
116-
)
106+
107+
check_symbol_exists(
108+
lo_truncate64
109+
"libpq-fe.h"
110+
HAVE_PG_LO64
111+
)
112+
113+
check_symbol_exists(
114+
PQsetErrorContextVisibility
115+
"libpq-fe.h"
116+
HAVE_PG_CONTEXT_VISIBILITY
117+
)
118+
cmake_pop_check_state()
117119
endif()
118120

119121
set(HAVE_PGSQL TRUE)

cmake/ext/pgsql/cmake/config.h.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
/* Define to 1 if libpq was compiled with the --enable-multibyte option. */
55
#cmakedefine HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT 1
66

7-
/* PostgreSQL 9.6 or later */
7+
/* Define to 1 if libpq has the 'PQsetErrorContextVisibility' function
8+
(PostgreSQL 9.6 or later). */
89
#cmakedefine HAVE_PG_CONTEXT_VISIBILITY 1
910

10-
/* PostgreSQL 9.3 or later */
11+
/* Define to 1 if libpq has the 'lo_truncate64' function (PostgreSQL 9.3 or
12+
later). */
1113
#cmakedefine HAVE_PG_LO64 1

0 commit comments

Comments
 (0)