FindPostgreSQL.cmake 12 KB

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