FindPostgreSQL.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 were 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. # "17"
  94. # "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"
  95. #
  96. # To use this variable just do something like this:
  97. # set(PostgreSQL_ADDITIONAL_VERSIONS "9.2" "8.4.4")
  98. # before calling find_package(PostgreSQL) in your CMakeLists.txt file.
  99. # This will mean that the versions you set here will be found first in the order
  100. # specified before the default ones are searched.
  101. #
  102. # ----------------------------------------------------------------------------
  103. # You may need to manually set:
  104. # PostgreSQL_INCLUDE_DIR - the path to where the PostgreSQL include files are.
  105. # PostgreSQL_LIBRARY_DIR - The path to where the PostgreSQL library files are.
  106. # If FindPostgreSQL.cmake cannot find the include files or the library files.
  107. #
  108. # ----------------------------------------------------------------------------
  109. # The following variables are set if PostgreSQL is found:
  110. # PostgreSQL_FOUND - Set to true when PostgreSQL is found.
  111. # PostgreSQL_INCLUDE_DIRS - Include directories for PostgreSQL
  112. # PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries
  113. # PostgreSQL_LIBRARIES - The PostgreSQL libraries.
  114. #
  115. # The ``PostgreSQL::PostgreSQL`` imported target is also created.
  116. #
  117. # ----------------------------------------------------------------------------
  118. # If you have installed PostgreSQL in a non-standard location.
  119. # (Please note that in the following comments, it is assumed that <Your Path>
  120. # points to the root directory of the include directory of PostgreSQL.)
  121. # Then you have three options.
  122. # 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and
  123. # PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is
  124. # 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path()
  125. # to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file
  126. # set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include")
  127. # 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have
  128. # installed PostgreSQL, e.g. <Your Path>.
  129. #
  130. # ----------------------------------------------------------------------------
  131. cmake_policy(PUSH)
  132. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  133. 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")
  134. set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}")
  135. set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.")
  136. set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}")
  137. 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")
  138. set(PostgreSQL_KNOWN_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS}
  139. "17"
  140. "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")
  141. # Define additional search paths for root directories.
  142. set( PostgreSQL_ROOT_DIRECTORIES
  143. ENV PostgreSQL_ROOT
  144. ${PostgreSQL_ROOT}
  145. )
  146. foreach(suffix ${PostgreSQL_KNOWN_VERSIONS})
  147. if(WIN32)
  148. list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES
  149. "PostgreSQL/${suffix}/lib")
  150. list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
  151. "PostgreSQL/${suffix}/include")
  152. list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES
  153. "PostgreSQL/${suffix}/include/server")
  154. endif()
  155. if(UNIX)
  156. list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES
  157. "postgresql${suffix}"
  158. "postgresql@${suffix}"
  159. "pgsql-${suffix}/lib")
  160. list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
  161. "postgresql${suffix}"
  162. "postgresql@${suffix}"
  163. "postgresql/${suffix}"
  164. "pgsql-${suffix}/include")
  165. list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES
  166. "postgresql${suffix}/server"
  167. "postgresql@${suffix}/server"
  168. "postgresql/${suffix}/server"
  169. "pgsql-${suffix}/include/server")
  170. endif()
  171. endforeach()
  172. #
  173. # Look for an installation.
  174. #
  175. find_path(PostgreSQL_INCLUDE_DIR
  176. NAMES libpq-fe.h
  177. PATHS
  178. # Look in other places.
  179. ${PostgreSQL_ROOT_DIRECTORIES}
  180. PATH_SUFFIXES
  181. pgsql
  182. postgresql
  183. include
  184. ${PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES}
  185. # Help the user find it if we cannot.
  186. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  187. )
  188. find_path(PostgreSQL_TYPE_INCLUDE_DIR
  189. NAMES catalog/pg_type.h
  190. PATHS
  191. # Look in other places.
  192. ${PostgreSQL_ROOT_DIRECTORIES}
  193. PATH_SUFFIXES
  194. postgresql
  195. pgsql/server
  196. postgresql/server
  197. include/server
  198. ${PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES}
  199. # Help the user find it if we cannot.
  200. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  201. )
  202. # The PostgreSQL library.
  203. set (PostgreSQL_LIBRARY_TO_FIND pq)
  204. # Setting some more prefixes for the library
  205. set (PostgreSQL_LIB_PREFIX "")
  206. if ( WIN32 )
  207. set (PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib")
  208. set (PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND})
  209. endif()
  210. function(__postgresql_find_library _name)
  211. find_library(${_name}
  212. NAMES ${ARGN}
  213. PATHS
  214. ${PostgreSQL_ROOT_DIRECTORIES}
  215. PATH_SUFFIXES
  216. lib
  217. ${PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES}
  218. # Help the user find it if we cannot.
  219. DOC "The ${PostgreSQL_LIBRARY_DIR_MESSAGE}"
  220. )
  221. endfunction()
  222. # For compatibility with versions prior to this multi-config search, honor
  223. # any PostgreSQL_LIBRARY that is already specified and skip the search.
  224. if(PostgreSQL_LIBRARY)
  225. set(PostgreSQL_LIBRARIES "${PostgreSQL_LIBRARY}")
  226. get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY}" PATH)
  227. else()
  228. __postgresql_find_library(PostgreSQL_LIBRARY_RELEASE ${PostgreSQL_LIBRARY_TO_FIND})
  229. __postgresql_find_library(PostgreSQL_LIBRARY_DEBUG ${PostgreSQL_LIBRARY_TO_FIND}d)
  230. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  231. select_library_configurations(PostgreSQL)
  232. mark_as_advanced(PostgreSQL_LIBRARY_RELEASE PostgreSQL_LIBRARY_DEBUG)
  233. if(PostgreSQL_LIBRARY_RELEASE)
  234. get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY_RELEASE}" PATH)
  235. elseif(PostgreSQL_LIBRARY_DEBUG)
  236. get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY_DEBUG}" PATH)
  237. else()
  238. set(PostgreSQL_LIBRARY_DIR "")
  239. endif()
  240. endif()
  241. if (PostgreSQL_INCLUDE_DIR)
  242. # Some platforms include multiple pg_config.hs for multi-lib configurations
  243. # This is a temporary workaround. A better solution would be to compile
  244. # a dummy c file and extract the value of the symbol.
  245. file(GLOB _PG_CONFIG_HEADERS "${PostgreSQL_INCLUDE_DIR}/pg_config*.h")
  246. foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS})
  247. if(EXISTS "${_PG_CONFIG_HEADER}")
  248. file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str
  249. REGEX "^#define[\t ]+PG_VERSION_NUM[\t ]+.*")
  250. if(pgsql_version_str)
  251. string(REGEX REPLACE "^#define[\t ]+PG_VERSION_NUM[\t ]+([0-9]*).*"
  252. "\\1" _PostgreSQL_VERSION_NUM "${pgsql_version_str}")
  253. break()
  254. endif()
  255. endif()
  256. endforeach()
  257. if (_PostgreSQL_VERSION_NUM)
  258. # 9.x and older encoding
  259. if (_PostgreSQL_VERSION_NUM LESS 100000)
  260. math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000")
  261. math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000 / 100")
  262. math(EXPR _PostgreSQL_patch_version "${_PostgreSQL_VERSION_NUM} % 100")
  263. set(PostgreSQL_VERSION "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}.${_PostgreSQL_patch_version}")
  264. set(PostgreSQL_VERSION_STRING "${PostgreSQL_VERSION}")
  265. unset(_PostgreSQL_major_version)
  266. unset(_PostgreSQL_minor_version)
  267. unset(_PostgreSQL_patch_version)
  268. else ()
  269. math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000")
  270. math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000")
  271. set(PostgreSQL_VERSION "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}")
  272. set(PostgreSQL_VERSION_STRING "${PostgreSQL_VERSION}")
  273. unset(_PostgreSQL_major_version)
  274. unset(_PostgreSQL_minor_version)
  275. endif ()
  276. else ()
  277. foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS})
  278. if(EXISTS "${_PG_CONFIG_HEADER}")
  279. file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str
  280. REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"")
  281. if(pgsql_version_str)
  282. string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*"
  283. "\\1" PostgreSQL_VERSION "${pgsql_version_str}")
  284. set(PostgreSQL_VERSION_STRING "${PostgreSQL_VERSION}")
  285. break()
  286. endif()
  287. endif()
  288. endforeach()
  289. endif ()
  290. unset(_PostgreSQL_VERSION_NUM)
  291. unset(pgsql_version_str)
  292. endif()
  293. if("Server" IN_LIST PostgreSQL_FIND_COMPONENTS)
  294. set(PostgreSQL_Server_FOUND TRUE)
  295. if(NOT PostgreSQL_TYPE_INCLUDE_DIR)
  296. set(PostgreSQL_Server_FOUND FALSE)
  297. endif()
  298. endif()
  299. # Did we find anything?
  300. include(FindPackageHandleStandardArgs)
  301. find_package_handle_standard_args(PostgreSQL
  302. REQUIRED_VARS PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR
  303. HANDLE_COMPONENTS
  304. VERSION_VAR PostgreSQL_VERSION)
  305. function(__postgresql_import_library _target _var _config)
  306. if(_config)
  307. set(_config_suffix "_${_config}")
  308. else()
  309. set(_config_suffix "")
  310. endif()
  311. set(_lib "${${_var}${_config_suffix}}")
  312. if(EXISTS "${_lib}")
  313. if(_config)
  314. set_property(TARGET ${_target} APPEND PROPERTY
  315. IMPORTED_CONFIGURATIONS ${_config})
  316. endif()
  317. set_target_properties(${_target} PROPERTIES
  318. IMPORTED_LOCATION${_config_suffix} "${_lib}")
  319. endif()
  320. endfunction()
  321. # Now try to get the include and library path.
  322. if(PostgreSQL_FOUND)
  323. set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR})
  324. if(PostgreSQL_TYPE_INCLUDE_DIR)
  325. list(APPEND PostgreSQL_INCLUDE_DIRS ${PostgreSQL_TYPE_INCLUDE_DIR})
  326. endif()
  327. set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR})
  328. if (NOT TARGET PostgreSQL::PostgreSQL)
  329. add_library(PostgreSQL::PostgreSQL UNKNOWN IMPORTED)
  330. set_target_properties(PostgreSQL::PostgreSQL PROPERTIES
  331. INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIRS}")
  332. __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "")
  333. __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "RELEASE")
  334. __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "DEBUG")
  335. endif ()
  336. endif()
  337. mark_as_advanced(PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR)
  338. cmake_policy(POP)