FindPostgreSQL.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindPostgreSQL
  5. --------------
  6. Finds the PostgreSQL installation - the client library (``libpq``) and
  7. optionally the server:
  8. .. code-block:: cmake
  9. find_package(PostgreSQL [<version>] [...])
  10. Imported Targets
  11. ^^^^^^^^^^^^^^^^
  12. This module provides the following :ref:`Imported Targets`:
  13. ``PostgreSQL::PostgreSQL``
  14. .. versionadded:: 3.14
  15. Target encapsulating all usage requirements of the required ``libpq`` client
  16. library and the optionally requested PostgreSQL server component. This target
  17. is available only if PostgreSQL is found.
  18. Result Variables
  19. ^^^^^^^^^^^^^^^^
  20. This module defines the following variables:
  21. ``PostgreSQL_FOUND``
  22. Boolean indicating whether the minimum required version and components of
  23. PostgreSQL have been found.
  24. ``PostgreSQL_VERSION``
  25. .. versionadded:: 4.2
  26. The version of PostgreSQL found.
  27. ``PostgreSQL_LIBRARIES``
  28. The PostgreSQL libraries needed for linking.
  29. ``PostgreSQL_INCLUDE_DIRS``
  30. The include directories containing PostgreSQL headers.
  31. ``PostgreSQL_LIBRARY_DIRS``
  32. The directories containing PostgreSQL libraries.
  33. ``PostgreSQL_TYPE_INCLUDE_DIR``
  34. The include directory containing PostgreSQL server headers.
  35. Components
  36. ^^^^^^^^^^
  37. This module supports the following additional components:
  38. ``Server``
  39. .. versionadded:: 3.20
  40. Ensures that server headers are also found. Note that
  41. ``PostgreSQL_TYPE_INCLUDE_DIR`` variable is set regardless of whether this
  42. component is specified in the ``find_package()`` call.
  43. Deprecated Variables
  44. ^^^^^^^^^^^^^^^^^^^^
  45. The following variables are provided for backward compatibility:
  46. ``PostgreSQL_VERSION_STRING``
  47. .. deprecated:: 4.2
  48. Superseded by the ``PostgreSQL_VERSION``.
  49. The version of PostgreSQL found.
  50. Examples
  51. ^^^^^^^^
  52. Finding the PostgreSQL client library and linking it to a project target:
  53. .. code-block:: cmake
  54. find_package(PostgreSQL)
  55. target_link_libraries(project_target PRIVATE PostgreSQL::PostgreSQL)
  56. Specifying a minimum required PostgreSQL version:
  57. .. code-block:: cmake
  58. find_package(PostgreSQL 11)
  59. Finding the PostgreSQL client library and requiring server headers using the
  60. ``Server`` component provides an imported target with all usage requirements,
  61. which can then be linked to a project target:
  62. .. code-block:: cmake
  63. find_package(PostgreSQL COMPONENTS Server)
  64. target_link_libraries(project_target PRIVATE PostgreSQL::PostgreSQL)
  65. When checking for PostgreSQL client library features, some capabilities are
  66. indicated by preprocessor macros in the ``libpq-fe.h`` header (e.g.
  67. ``LIBPQ_HAS_PIPELINING``). Others may require using the
  68. :command:`check_symbol_exists` command:
  69. .. code-block:: cmake
  70. find_package(PostgreSQL)
  71. target_link_libraries(project_target PRIVATE PostgreSQL::PostgreSQL)
  72. # The PQservice() function is available as of PostgreSQL 18.
  73. if(TARGET PostgreSQL::PostgreSQL)
  74. include(CheckSymbolExists)
  75. include(CMakePushCheckState)
  76. cmake_push_check_state(RESET)
  77. set(CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL)
  78. check_symbol_exists(PQservice "libpq-fe.h" PROJECT_HAS_PQSERVICE)
  79. cmake_pop_check_state()
  80. endif()
  81. #]=======================================================================]
  82. # ----------------------------------------------------------------------------
  83. # History:
  84. # This module is derived from the module originally found in the VTK source tree.
  85. #
  86. # ----------------------------------------------------------------------------
  87. # Note:
  88. # PostgreSQL_ADDITIONAL_VERSIONS is a variable that can be used to set the
  89. # version number of the implementation of PostgreSQL.
  90. # In Windows the default installation of PostgreSQL uses that as part of the path.
  91. # E.g C:\Program Files\PostgreSQL\8.4.
  92. # Currently, the following version numbers are known to this module:
  93. # "16" "15" "14" "13" "12" "11" "10" "9.6" "9.5" "9.4" "9.3" "9.2" "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0"
  94. #
  95. # To use this variable just do something like this:
  96. # set(PostgreSQL_ADDITIONAL_VERSIONS "9.2" "8.4.4")
  97. # before calling find_package(PostgreSQL) in your CMakeLists.txt file.
  98. # This will mean that the versions you set here will be found first in the order
  99. # specified before the default ones are searched.
  100. #
  101. # ----------------------------------------------------------------------------
  102. # You may need to manually set:
  103. # PostgreSQL_INCLUDE_DIR - the path to where the PostgreSQL include files are.
  104. # PostgreSQL_LIBRARY_DIR - The path to where the PostgreSQL library files are.
  105. # If FindPostgreSQL.cmake cannot find the include files or the library files.
  106. #
  107. # ----------------------------------------------------------------------------
  108. # The following variables are set if PostgreSQL is found:
  109. # PostgreSQL_FOUND - Set to true when PostgreSQL is found.
  110. # PostgreSQL_INCLUDE_DIRS - Include directories for PostgreSQL
  111. # PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries
  112. # PostgreSQL_LIBRARIES - The PostgreSQL libraries.
  113. #
  114. # The ``PostgreSQL::PostgreSQL`` imported target is also created.
  115. #
  116. # ----------------------------------------------------------------------------
  117. # If you have installed PostgreSQL in a non-standard location.
  118. # (Please note that in the following comments, it is assumed that <Your Path>
  119. # points to the root directory of the include directory of PostgreSQL.)
  120. # Then you have three options.
  121. # 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and
  122. # PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is
  123. # 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path()
  124. # to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file
  125. # set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include")
  126. # 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have
  127. # installed PostgreSQL, e.g. <Your Path>.
  128. #
  129. # ----------------------------------------------------------------------------
  130. cmake_policy(PUSH)
  131. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  132. set(PostgreSQL_INCLUDE_PATH_DESCRIPTION "top-level directory containing the PostgreSQL include directories. E.g /usr/local/include/PostgreSQL/8.4 or C:/Program Files/PostgreSQL/8.4/include")
  133. set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}")
  134. set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.")
  135. set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}")
  136. set(PostgreSQL_ROOT_DIR_MESSAGE "Set the PostgreSQL_ROOT system variable to where PostgreSQL is found on the machine E.g C:/Program Files/PostgreSQL/8.4")
  137. set(PostgreSQL_KNOWN_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS}
  138. "16" "15" "14" "13" "12" "11" "10" "9.6" "9.5" "9.4" "9.3" "9.2" "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0")
  139. # Define additional search paths for root directories.
  140. set( PostgreSQL_ROOT_DIRECTORIES
  141. ENV PostgreSQL_ROOT
  142. ${PostgreSQL_ROOT}
  143. )
  144. foreach(suffix ${PostgreSQL_KNOWN_VERSIONS})
  145. if(WIN32)
  146. list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES
  147. "PostgreSQL/${suffix}/lib")
  148. list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
  149. "PostgreSQL/${suffix}/include")
  150. list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES
  151. "PostgreSQL/${suffix}/include/server")
  152. endif()
  153. if(UNIX)
  154. list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES
  155. "postgresql${suffix}"
  156. "postgresql@${suffix}"
  157. "pgsql-${suffix}/lib")
  158. list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
  159. "postgresql${suffix}"
  160. "postgresql@${suffix}"
  161. "postgresql/${suffix}"
  162. "pgsql-${suffix}/include")
  163. list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES
  164. "postgresql${suffix}/server"
  165. "postgresql@${suffix}/server"
  166. "postgresql/${suffix}/server"
  167. "pgsql-${suffix}/include/server")
  168. endif()
  169. endforeach()
  170. #
  171. # Look for an installation.
  172. #
  173. find_path(PostgreSQL_INCLUDE_DIR
  174. NAMES libpq-fe.h
  175. PATHS
  176. # Look in other places.
  177. ${PostgreSQL_ROOT_DIRECTORIES}
  178. PATH_SUFFIXES
  179. pgsql
  180. postgresql
  181. include
  182. ${PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES}
  183. # Help the user find it if we cannot.
  184. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  185. )
  186. find_path(PostgreSQL_TYPE_INCLUDE_DIR
  187. NAMES catalog/pg_type.h
  188. PATHS
  189. # Look in other places.
  190. ${PostgreSQL_ROOT_DIRECTORIES}
  191. PATH_SUFFIXES
  192. postgresql
  193. pgsql/server
  194. postgresql/server
  195. include/server
  196. ${PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES}
  197. # Help the user find it if we cannot.
  198. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  199. )
  200. # The PostgreSQL library.
  201. set (PostgreSQL_LIBRARY_TO_FIND pq)
  202. # Setting some more prefixes for the library
  203. set (PostgreSQL_LIB_PREFIX "")
  204. if ( WIN32 )
  205. set (PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib")
  206. set (PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND})
  207. endif()
  208. function(__postgresql_find_library _name)
  209. find_library(${_name}
  210. NAMES ${ARGN}
  211. PATHS
  212. ${PostgreSQL_ROOT_DIRECTORIES}
  213. PATH_SUFFIXES
  214. lib
  215. ${PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES}
  216. # Help the user find it if we cannot.
  217. DOC "The ${PostgreSQL_LIBRARY_DIR_MESSAGE}"
  218. )
  219. endfunction()
  220. # For compatibility with versions prior to this multi-config search, honor
  221. # any PostgreSQL_LIBRARY that is already specified and skip the search.
  222. if(PostgreSQL_LIBRARY)
  223. set(PostgreSQL_LIBRARIES "${PostgreSQL_LIBRARY}")
  224. get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY}" PATH)
  225. else()
  226. __postgresql_find_library(PostgreSQL_LIBRARY_RELEASE ${PostgreSQL_LIBRARY_TO_FIND})
  227. __postgresql_find_library(PostgreSQL_LIBRARY_DEBUG ${PostgreSQL_LIBRARY_TO_FIND}d)
  228. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  229. select_library_configurations(PostgreSQL)
  230. mark_as_advanced(PostgreSQL_LIBRARY_RELEASE PostgreSQL_LIBRARY_DEBUG)
  231. if(PostgreSQL_LIBRARY_RELEASE)
  232. get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY_RELEASE}" PATH)
  233. elseif(PostgreSQL_LIBRARY_DEBUG)
  234. get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY_DEBUG}" PATH)
  235. else()
  236. set(PostgreSQL_LIBRARY_DIR "")
  237. endif()
  238. endif()
  239. if (PostgreSQL_INCLUDE_DIR)
  240. # Some platforms include multiple pg_config.hs for multi-lib configurations
  241. # This is a temporary workaround. A better solution would be to compile
  242. # a dummy c file and extract the value of the symbol.
  243. file(GLOB _PG_CONFIG_HEADERS "${PostgreSQL_INCLUDE_DIR}/pg_config*.h")
  244. foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS})
  245. if(EXISTS "${_PG_CONFIG_HEADER}")
  246. file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str
  247. REGEX "^#define[\t ]+PG_VERSION_NUM[\t ]+.*")
  248. if(pgsql_version_str)
  249. string(REGEX REPLACE "^#define[\t ]+PG_VERSION_NUM[\t ]+([0-9]*).*"
  250. "\\1" _PostgreSQL_VERSION_NUM "${pgsql_version_str}")
  251. break()
  252. endif()
  253. endif()
  254. endforeach()
  255. if (_PostgreSQL_VERSION_NUM)
  256. # 9.x and older encoding
  257. if (_PostgreSQL_VERSION_NUM LESS 100000)
  258. math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000")
  259. math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000 / 100")
  260. math(EXPR _PostgreSQL_patch_version "${_PostgreSQL_VERSION_NUM} % 100")
  261. set(PostgreSQL_VERSION "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}.${_PostgreSQL_patch_version}")
  262. set(PostgreSQL_VERSION_STRING "${PostgreSQL_VERSION}")
  263. unset(_PostgreSQL_major_version)
  264. unset(_PostgreSQL_minor_version)
  265. unset(_PostgreSQL_patch_version)
  266. else ()
  267. math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000")
  268. math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000")
  269. set(PostgreSQL_VERSION "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}")
  270. set(PostgreSQL_VERSION_STRING "${PostgreSQL_VERSION}")
  271. unset(_PostgreSQL_major_version)
  272. unset(_PostgreSQL_minor_version)
  273. endif ()
  274. else ()
  275. foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS})
  276. if(EXISTS "${_PG_CONFIG_HEADER}")
  277. file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str
  278. REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"")
  279. if(pgsql_version_str)
  280. string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*"
  281. "\\1" PostgreSQL_VERSION "${pgsql_version_str}")
  282. set(PostgreSQL_VERSION_STRING "${PostgreSQL_VERSION}")
  283. break()
  284. endif()
  285. endif()
  286. endforeach()
  287. endif ()
  288. unset(_PostgreSQL_VERSION_NUM)
  289. unset(pgsql_version_str)
  290. endif()
  291. if("Server" IN_LIST PostgreSQL_FIND_COMPONENTS)
  292. set(PostgreSQL_Server_FOUND TRUE)
  293. if(NOT PostgreSQL_TYPE_INCLUDE_DIR)
  294. set(PostgreSQL_Server_FOUND FALSE)
  295. endif()
  296. endif()
  297. # Did we find anything?
  298. include(FindPackageHandleStandardArgs)
  299. find_package_handle_standard_args(PostgreSQL
  300. REQUIRED_VARS PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR
  301. HANDLE_COMPONENTS
  302. VERSION_VAR PostgreSQL_VERSION)
  303. function(__postgresql_import_library _target _var _config)
  304. if(_config)
  305. set(_config_suffix "_${_config}")
  306. else()
  307. set(_config_suffix "")
  308. endif()
  309. set(_lib "${${_var}${_config_suffix}}")
  310. if(EXISTS "${_lib}")
  311. if(_config)
  312. set_property(TARGET ${_target} APPEND PROPERTY
  313. IMPORTED_CONFIGURATIONS ${_config})
  314. endif()
  315. set_target_properties(${_target} PROPERTIES
  316. IMPORTED_LOCATION${_config_suffix} "${_lib}")
  317. endif()
  318. endfunction()
  319. # Now try to get the include and library path.
  320. if(PostgreSQL_FOUND)
  321. set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR})
  322. if(PostgreSQL_TYPE_INCLUDE_DIR)
  323. list(APPEND PostgreSQL_INCLUDE_DIRS ${PostgreSQL_TYPE_INCLUDE_DIR})
  324. endif()
  325. set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR})
  326. if (NOT TARGET PostgreSQL::PostgreSQL)
  327. add_library(PostgreSQL::PostgreSQL UNKNOWN IMPORTED)
  328. set_target_properties(PostgreSQL::PostgreSQL PROPERTIES
  329. INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIRS}")
  330. __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "")
  331. __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "RELEASE")
  332. __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "DEBUG")
  333. endif ()
  334. endif()
  335. mark_as_advanced(PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR)
  336. cmake_policy(POP)