FindPostgreSQL.cmake 12 KB

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