FindPostgreSQL.cmake 7.8 KB

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