FindPostgreSQL.cmake 8.0 KB

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