FindPostgreSQL.cmake 7.7 KB

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