FindPostgreSQL.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #.rst:
  2. # FindPostgreSQL
  3. # --------------
  4. #
  5. # Find the PostgreSQL installation.
  6. #
  7. # In Windows, we make the assumption that, if the PostgreSQL files are
  8. # installed, the default directory will be C:\Program Files\PostgreSQL.
  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_VERSION_STRING - the version of PostgreSQL found (since CMake 2.8.8)
  17. #=============================================================================
  18. # Copyright 2004-2009 Kitware, Inc.
  19. #
  20. # Distributed under the OSI-approved BSD License (the "License");
  21. # see accompanying file Copyright.txt for details.
  22. #
  23. # This software is distributed WITHOUT ANY WARRANTY; without even the
  24. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  25. # See the License for more information.
  26. #=============================================================================
  27. # (To distribute this file outside of CMake, substitute the full
  28. # License text for the above reference.)
  29. # ----------------------------------------------------------------------------
  30. # History:
  31. # This module is derived from the module originally found in the VTK source tree.
  32. #
  33. # ----------------------------------------------------------------------------
  34. # Note:
  35. # PostgreSQL_ADDITIONAL_VERSIONS is a variable that can be used to set the
  36. # version mumber of the implementation of PostgreSQL.
  37. # In Windows the default installation of PostgreSQL uses that as part of the path.
  38. # E.g C:\Program Files\PostgreSQL\8.4.
  39. # Currently, the following version numbers are known to this module:
  40. # "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0"
  41. #
  42. # To use this variable just do something like this:
  43. # set(PostgreSQL_ADDITIONAL_VERSIONS "9.2" "8.4.4")
  44. # before calling find_package(PostgreSQL) in your CMakeLists.txt file.
  45. # This will mean that the versions you set here will be found first in the order
  46. # specified before the default ones are searched.
  47. #
  48. # ----------------------------------------------------------------------------
  49. # You may need to manually set:
  50. # PostgreSQL_INCLUDE_DIR - the path to where the PostgreSQL include files are.
  51. # PostgreSQL_LIBRARY_DIR - The path to where the PostgreSQL library files are.
  52. # If FindPostgreSQL.cmake cannot find the include files or the library files.
  53. #
  54. # ----------------------------------------------------------------------------
  55. # The following variables are set if PostgreSQL is found:
  56. # PostgreSQL_FOUND - Set to true when PostgreSQL is found.
  57. # PostgreSQL_INCLUDE_DIRS - Include directories for PostgreSQL
  58. # PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries
  59. # PostgreSQL_LIBRARIES - The PostgreSQL libraries.
  60. #
  61. # ----------------------------------------------------------------------------
  62. # If you have installed PostgreSQL in a non-standard location.
  63. # (Please note that in the following comments, it is assumed that <Your Path>
  64. # points to the root directory of the include directory of PostgreSQL.)
  65. # Then you have three options.
  66. # 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and
  67. # PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is
  68. # 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path()
  69. # to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file
  70. # set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include")
  71. # 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have
  72. # installed PostgreSQL, e.g. <Your Path>.
  73. #
  74. # ----------------------------------------------------------------------------
  75. 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")
  76. set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}")
  77. set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.")
  78. set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}")
  79. 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")
  80. set(PostgreSQL_KNOWN_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS}
  81. "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0")
  82. # Define additional search paths for root directories.
  83. if ( WIN32 )
  84. foreach (suffix ${PostgreSQL_KNOWN_VERSIONS} )
  85. set(PostgreSQL_ADDITIONAL_SEARCH_PATHS ${PostgreSQL_ADDITIONAL_SEARCH_PATHS} "C:/Program Files/PostgreSQL/${suffix}" )
  86. endforeach()
  87. endif()
  88. set( PostgreSQL_ROOT_DIRECTORIES
  89. ENV PostgreSQL_ROOT
  90. ${PostgreSQL_ROOT}
  91. ${PostgreSQL_ADDITIONAL_SEARCH_PATHS}
  92. )
  93. #
  94. # Look for an installation.
  95. #
  96. find_path(PostgreSQL_INCLUDE_DIR
  97. NAMES libpq-fe.h
  98. PATHS
  99. # Look in other places.
  100. ${PostgreSQL_ROOT_DIRECTORIES}
  101. PATH_SUFFIXES
  102. pgsql
  103. postgresql
  104. include
  105. # Help the user find it if we cannot.
  106. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  107. )
  108. find_path(PostgreSQL_TYPE_INCLUDE_DIR
  109. NAMES catalog/pg_type.h
  110. PATHS
  111. # Look in other places.
  112. ${PostgreSQL_ROOT_DIRECTORIES}
  113. PATH_SUFFIXES
  114. postgresql
  115. pgsql/server
  116. postgresql/server
  117. include/server
  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. )
  136. get_filename_component(PostgreSQL_LIBRARY_DIR ${PostgreSQL_LIBRARY} PATH)
  137. if (PostgreSQL_INCLUDE_DIR)
  138. # Some platforms include multiple pg_config.hs for multi-lib configurations
  139. # This is a temporary workaround. A better solution would be to compile
  140. # a dummy c file and extract the value of the symbol.
  141. file(GLOB _PG_CONFIG_HEADERS "${PostgreSQL_INCLUDE_DIR}/pg_config*.h")
  142. foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS})
  143. if(EXISTS "${_PG_CONFIG_HEADER}")
  144. file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str
  145. REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"")
  146. if(pgsql_version_str)
  147. string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*"
  148. "\\1" PostgreSQL_VERSION_STRING "${pgsql_version_str}")
  149. break()
  150. endif()
  151. endif()
  152. endforeach()
  153. unset(pgsql_version_str)
  154. endif()
  155. # Did we find anything?
  156. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  157. find_package_handle_standard_args(PostgreSQL
  158. REQUIRED_VARS PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR
  159. VERSION_VAR PostgreSQL_VERSION_STRING)
  160. set(PostgreSQL_FOUND ${POSTGRESQL_FOUND})
  161. # Now try to get the include and library path.
  162. if(PostgreSQL_FOUND)
  163. set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR} ${PostgreSQL_TYPE_INCLUDE_DIR} )
  164. set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR} )
  165. set(PostgreSQL_LIBRARIES ${PostgreSQL_LIBRARY_TO_FIND})
  166. endif()
  167. mark_as_advanced(PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR PostgreSQL_LIBRARY )