@@ -27,8 +27,8 @@ project(
2727 LANGUAGES C
2828)
2929
30- include (CheckLibraryExists)
3130include (CheckSourceCompiles)
31+ include (CheckSymbolExists)
3232include (CMakeDependentOption)
3333include (CMakePushCheckState)
3434include (FeatureSummary)
@@ -85,70 +85,110 @@ set_package_properties(
8585
8686target_link_libraries (php_ext_pgsql PUBLIC PostgreSQL::PostgreSQL)
8787
88+ # Check features of PostgreSQL library (libpq). As of PostgreSQL (and libpq)
89+ # version 14 there are features macros available in the libpq-fe.h. Additional
90+ # checks for specific symbols below are done to avoid issues present on
91+ # platforms that might package libpq development headers and PostgreSQL server
92+ # development headers into a single all-in-one package (for example,
93+ # postgresql-devel on openSUSE Linux at least on version 15), where libpq
94+ # package contains the latest library, but the development package contains the
95+ # header files of different PostgreSQL version (for example, postgresql16-devel)
96+ # Which leads to unexpected behavior if libpq would be version 17, but
97+ # libpq-fe.h would belong to the package version 16. Otherwise, ideally only
98+ # feature macros checks (LIBPQ_HAS_CHUNK_MODE, etc.) should be sufficient for
99+ # most platforms.
100+
88101if (TARGET PostgreSQL::PostgreSQL)
89- # PostgreSQL library minimum version sanity check.
90- check_library_exists(
91- PostgreSQL::PostgreSQL
92- PQencryptPasswordConn
93- ""
94- _PHP_LIBPQ_CHECK
95- )
96- if (NOT _PHP_LIBPQ_CHECK)
97- message (
98- FATAL_ERROR
99- "PostgreSQL check failed: libpq ${PHP_POSTGRESQL_MIN_VERSION} or later "
100- "is required."
101- )
102- endif ()
103-
104- # Available since PostgreSQL 12.
105- if (PostgreSQL_VERSION_STRING VERSION_GREATER_EQUAL 12)
106- check_library_exists(
107- PostgreSQL::PostgreSQL
108- PQresultMemorySize
109- ""
110- HAVE_PG_RESULT_MEMORY_SIZE
111- )
112-
113- cmake_push_check_state(RESET)
114- set (CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL)
102+ cmake_push_check_state(RESET)
103+ set (CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL)
104+
105+ # PostgreSQL library minimum version sanity check.
106+ check_symbol_exists(PQencryptPasswordConn "libpq-fe.h" _PHP_LIBPQ_CHECK)
107+ if (NOT _PHP_LIBPQ_CHECK)
108+ message (
109+ FATAL_ERROR
110+ "PostgreSQL check failed: libpq ${PHP_POSTGRESQL_MIN_VERSION} or later "
111+ "is required."
112+ )
113+ endif ()
114+
115+ # Available since PostgreSQL 12.
116+ if (PostgreSQL_VERSION_STRING VERSION_GREATER_EQUAL 12)
117+ check_symbol_exists(
118+ PQresultMemorySize
119+ "libpq-fe.h"
120+ HAVE_PG_RESULT_MEMORY_SIZE
121+ )
122+
115123 check_source_compiles(C [[
116124 #include <libpq-fe.h>
117125 int main(void) { PGVerbosity e = PQERRORS_SQLSTATE; (void)e; return 0; }
118126 ]] HAVE_PQERRORS_SQLSTATE)
119- cmake_pop_check_state()
120- endif ()
121-
122- # Available since PostgreSQL 17.
123- if (PostgreSQL_VERSION_STRING VERSION_GREATER_EQUAL 17)
124- check_library_exists(
125- PostgreSQL::PostgreSQL
126- PQchangePassword
127- ""
128- HAVE_PG_CHANGE_PASSWORD
129- )
130-
131- check_library_exists(
132- PostgreSQL::PostgreSQL
133- PQsocketPoll
134- ""
135- HAVE_PG_SOCKET_POLL
136- )
137-
138- check_library_exists(
139- PostgreSQL::PostgreSQL
140- PQsetChunkedRowsMode
141- ""
142- HAVE_PG_SET_CHUNKED_ROWS_SIZE
143- )
144-
145- check_library_exists(
146- PostgreSQL::PostgreSQL
147- PQclosePrepared
148- ""
149- HAVE_PG_CLOSE_STMT
150- )
151- endif ()
127+ endif ()
128+
129+ # Available since PostgreSQL 17.
130+ if (PostgreSQL_VERSION_STRING VERSION_GREATER_EQUAL 17)
131+ # Indicates the presence of PQchangePassword.
132+ check_symbol_exists(
133+ LIBPQ_HAS_CHANGE_PASSWORD
134+ "libpq-fe.h"
135+ _PHP_HAVE_LIBPQ_HAS_CHANGE_PASSWORD
136+ )
137+ check_symbol_exists(
138+ PQchangePassword
139+ "libpq-fe.h"
140+ _PHP_HAVE_PQCHANGEPASSWORD
141+ )
142+ if (_PHP_HAVE_LIBPQ_HAS_CHANGE_PASSWORD AND _PHP_HAVE_PQCHANGEPASSWORD)
143+ set (HAVE_PG_CHANGE_PASSWORD TRUE )
144+ endif ()
145+
146+ # Indicates the presence of PQsocketPoll, PQgetCurrentTimeUSec.
147+ check_symbol_exists(
148+ LIBPQ_HAS_SOCKET_POLL
149+ "libpq-fe.h"
150+ _PHP_HAVE_LIBPQ_HAS_SOCKET_POLL
151+ )
152+ check_symbol_exists(
153+ PQsocketPoll
154+ "libpq-fe.h"
155+ _PHP_HAVE_PQSOCKETPOLL
156+ )
157+ if (_PHP_HAVE_LIBPQ_HAS_SOCKET_POLL AND _PHP_HAVE_PQSOCKETPOLL)
158+ set (HAVE_PG_SOCKET_POLL TRUE )
159+ endif ()
160+
161+ # Indicates the presence of PQsetChunkedRowsMode and PGRES_TUPLES_CHUNK.
162+ check_symbol_exists(
163+ LIBPQ_HAS_CHUNK_MODE
164+ "libpq-fe.h"
165+ _PHP_HAVE_LIBPQ_HAS_CHUNK_MODE
166+ )
167+ check_symbol_exists(
168+ PQsetChunkedRowsMode
169+ "libpq-fe.h"
170+ _PHP_HAVE_PQSETCHUNKEDROWSMODE
171+ )
172+ if (_PHP_HAVE_LIBPQ_HAS_CHUNK_MODE AND _PHP_HAVE_PQSETCHUNKEDROWSMODE)
173+ set (HAVE_PG_SET_CHUNKED_ROWS_SIZE TRUE )
174+ endif ()
175+
176+ # Indicates the presence of PQclosePrepared, PQclosePortal, etc.
177+ check_symbol_exists(
178+ LIBPQ_HAS_CLOSE_PREPARED
179+ "libpq-fe.h"
180+ _PHP_HAVE_LIBPQ_HAS_CLOSE_PREPARED
181+ )
182+ check_symbol_exists(
183+ PQclosePrepared
184+ "libpq-fe.h"
185+ _PHP_HAVE_PQCLOSEPREPARED
186+ )
187+ if (_PHP_HAVE_LIBPQ_HAS_CLOSE_PREPARED AND _PHP_HAVE_PQCLOSEPREPARED)
188+ set (HAVE_PG_CLOSE_STMT TRUE )
189+ endif ()
190+ endif ()
191+ cmake_pop_check_state()
152192endif ()
153193
154194set (HAVE_PGSQL TRUE )
0 commit comments