FindPostgreSQL.cmake 12 KB

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