FindPostgreSQL.cmake 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # TODO(Bjoe) This is taken from cmake 3.10. For poco we need some changes here. Maybe we create an issue on cmake project
  2. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  3. # file Copyright.txt or https://cmake.org/licensing for details.
  4. #.rst:
  5. # FindPostgreSQL
  6. # --------------
  7. #
  8. # Find the PostgreSQL installation.
  9. #
  10. # This module defines
  11. #
  12. # ::
  13. #
  14. # PostgreSQL_LIBRARIES - the PostgreSQL libraries needed for linking
  15. # PostgreSQL_INCLUDE_DIRS - the directories of the PostgreSQL headers
  16. # PostgreSQL_LIBRARY_DIRS - the link directories for PostgreSQL libraries
  17. # PostgreSQL_VERSION - the version of PostgreSQL found (since CMake 2.8.8)
  18. # ----------------------------------------------------------------------------
  19. # History:
  20. # This module is derived from the module originally found in the VTK source tree.
  21. #
  22. # ----------------------------------------------------------------------------
  23. # Note:
  24. # PostgreSQL_ADDITIONAL_VERSIONS is a variable that can be used to set the
  25. # version mumber of the implementation of PostgreSQL.
  26. # In Windows the default installation of PostgreSQL uses that as part of the path.
  27. # E.g C:\Program Files\PostgreSQL\8.4.
  28. # Currently, the following version numbers are known to this module:
  29. # "9.6" "9.5" "9.4" "9.3" "9.2" "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0"
  30. #
  31. # To use this variable just do something like this:
  32. # set(PostgreSQL_ADDITIONAL_VERSIONS "9.2" "8.4.4")
  33. # before calling find_package(PostgreSQL) in your CMakeLists.txt file.
  34. # This will mean that the versions you set here will be found first in the order
  35. # specified before the default ones are searched.
  36. #
  37. # ----------------------------------------------------------------------------
  38. # You may need to manually set:
  39. # PostgreSQL_ROOT_DIR - that points to the root of where you have installed PostgreSQL
  40. # PostgreSQL_INCLUDE_DIR - the path to where the PostgreSQL include files are.
  41. # PostgreSQL_LIBRARY_DIR - The path to where the PostgreSQL library files are.
  42. # If FindPostgreSQL.cmake cannot find the include files or the library files.
  43. #
  44. # ----------------------------------------------------------------------------
  45. # The following variables are set if PostgreSQL is found:
  46. # PostgreSQL_FOUND - Set to true when PostgreSQL is found.
  47. # PostgreSQL_INCLUDE_DIRS - Include directories for PostgreSQL
  48. # PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries
  49. # PostgreSQL_LIBRARIES - The PostgreSQL libraries.
  50. #
  51. # ----------------------------------------------------------------------------
  52. # If you have installed PostgreSQL in a non-standard location.
  53. # (Please note that in the following comments, it is assumed that <Your Path>
  54. # points to the root directory of the include directory of PostgreSQL.)
  55. # Then you have three options.
  56. # 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and
  57. # PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is
  58. # 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path()
  59. # to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file
  60. # set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include")
  61. # 3) Set an environment variable called ${PostgreSQL_ROOT} / ${PostgreSQL_ROOT_DIR} that points to the root of where you have
  62. # installed PostgreSQL, e.g. <Your Path>.
  63. #
  64. # ----------------------------------------------------------------------------
  65. 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")
  66. set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}")
  67. set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.")
  68. set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}")
  69. 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")
  70. set(PostgreSQL_KNOWN_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS}
  71. "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")
  72. # Define additional search paths for root directories.
  73. set(PostgreSQL_ROOT_DIRECTORIES
  74. ENV PostgreSQL_ROOT
  75. ${PostgreSQL_ROOT}
  76. ${PostgreSQL_ROOT_DIR}
  77. )
  78. foreach(suffix ${PostgreSQL_KNOWN_VERSIONS})
  79. if(WIN32)
  80. list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES
  81. "PostgreSQL/${suffix}/lib")
  82. list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
  83. "PostgreSQL/${suffix}/include")
  84. list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES
  85. "PostgreSQL/${suffix}/include/server")
  86. endif()
  87. if(UNIX)
  88. list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES
  89. "pgsql-${suffix}/lib")
  90. list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
  91. "pgsql-${suffix}/include")
  92. list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES
  93. "postgresql/${suffix}/server"
  94. "pgsql-${suffix}/include/server")
  95. endif()
  96. endforeach()
  97. if(UNIX)
  98. list(APPEND PostgreSQL_ROOT_DIRECTORIES
  99. "/usr")
  100. list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
  101. "include/postgresql")
  102. endif()
  103. #
  104. # Look for an installation.
  105. #
  106. find_path(PostgreSQL_INCLUDE_DIR
  107. NAMES libpq-fe.h
  108. HINTS
  109. ${PostgreSQL_ROOT_INCLUDE_DIRS}
  110. PATHS
  111. # Look in other places.
  112. ${PostgreSQL_ROOT_DIRECTORIES}
  113. PATH_SUFFIXES
  114. pgsql
  115. postgresql
  116. include
  117. ${PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES}
  118. # Help the user find it if we cannot.
  119. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  120. )
  121. # TODO(Bjoe) It is not needed to build an PostgreSQL client. Maybe create an issue on cmake project
  122. # find_path(PostgreSQL_TYPE_INCLUDE_DIR
  123. # NAMES catalog/pg_type.h
  124. # PATHS
  125. # # Look in other places.
  126. # ${PostgreSQL_ROOT_DIRECTORIES}
  127. # PATH_SUFFIXES
  128. # postgresql
  129. # pgsql/server
  130. # postgresql/server
  131. # include/server
  132. # ${PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES}
  133. # # Help the user find it if we cannot.
  134. # DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  135. # )
  136. # The PostgreSQL library.
  137. set(PostgreSQL_LIBRARY_TO_FIND pq)
  138. # Setting some more prefixes for the library
  139. set(PostgreSQL_LIB_PREFIX "")
  140. if(WIN32)
  141. set(PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib")
  142. set(PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND})
  143. endif()
  144. find_library(PostgreSQL_LIBRARY
  145. NAMES ${PostgreSQL_LIBRARY_TO_FIND}
  146. HINTS
  147. ${PostgreSQL_ROOT_LIBRARY_DIRS}
  148. PATHS
  149. ${PostgreSQL_ROOT_DIRECTORIES}
  150. PATH_SUFFIXES
  151. lib
  152. ${PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES}
  153. # Help the user find it if we cannot.
  154. DOC "The ${PostgreSQL_LIBRARY_DIR_MESSAGE}"
  155. )
  156. get_filename_component(PostgreSQL_LIBRARY_DIR ${PostgreSQL_LIBRARY} PATH)
  157. if(PostgreSQL_INCLUDE_DIR)
  158. # Some platforms include multiple pg_config.hs for multi-lib configurations
  159. # This is a temporary workaround. A better solution would be to compile
  160. # a dummy c file and extract the value of the symbol.
  161. file(GLOB _PG_CONFIG_HEADERS "${PostgreSQL_INCLUDE_DIR}/pg_config*.h")
  162. foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS})
  163. if(EXISTS "${_PG_CONFIG_HEADER}")
  164. file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str
  165. REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"")
  166. if(pgsql_version_str)
  167. string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*"
  168. "\\1" PostgreSQL_VERSION "${pgsql_version_str}")
  169. break()
  170. endif()
  171. endif()
  172. endforeach()
  173. unset(pgsql_version_str)
  174. endif()
  175. # Did we find anything?
  176. include(FindPackageHandleStandardArgs)
  177. find_package_handle_standard_args(
  178. PostgreSQL
  179. REQUIRED_VARS PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR #PostgreSQL_TYPE_INCLUDE_DIR
  180. VERSION_VAR PostgreSQL_VERSION
  181. )
  182. set(PostgreSQL_FOUND ${POSTGRESQL_FOUND})
  183. # Now try to get the include and library path.
  184. if(PostgreSQL_FOUND)
  185. set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR} ) #${PostgreSQL_TYPE_INCLUDE_DIR} )
  186. set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR} )
  187. set(PostgreSQL_LIBRARIES ${PostgreSQL_LIBRARY})
  188. endif()
  189. if(PostgreSQL_FOUND AND NOT TARGET PostgreSQL::client)
  190. add_library(PostgreSQL::client UNKNOWN IMPORTED)
  191. set_target_properties(PostgreSQL::client PROPERTIES
  192. IMPORTED_LOCATION "${PostgreSQL_LIBRARY}"
  193. INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIR}"
  194. )
  195. endif()
  196. mark_as_advanced(PostgreSQL_INCLUDE_DIR PostgreSQL_LIBRARY ) #PostgreSQL_TYPE_INCLUDE_DIR