FindPostgreSQL.cmake 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # - Find the PostgreSQL installation.
  2. # Usage:
  3. # In your CMakeLists.txt file do something like this:
  4. # ...
  5. # # PostgreSQL
  6. # FIND_PACKAGE(PostgreSQL)
  7. # ...
  8. # if( PostgreSQL_FOUND )
  9. # include_directories(${PostgreSQL_INCLUDE_DIRS})
  10. # link_directories(${PostgreSQL_LIBRARY_DIRS})
  11. # endif( PostgreSQL_FOUND )
  12. # ...
  13. # Remember to include ${PostgreSQL_LIBRARIES} in the target_link_libraries() statement.
  14. #
  15. #
  16. # In Windows, we make the assumption that, if the PostgreSQL files are installed, the default directory
  17. # will be C:\Program Files\PostgreSQL.
  18. #
  19. #=============================================================================
  20. # Copyright 2004-2009 Kitware, Inc.
  21. #
  22. # Distributed under the OSI-approved BSD License (the "License");
  23. # see accompanying file Copyright.txt for details.
  24. #
  25. # This software is distributed WITHOUT ANY WARRANTY; without even the
  26. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27. # See the License for more information.
  28. #=============================================================================
  29. # (To distribute this file outside of CMake, substitute the full
  30. # License text for the above reference.)
  31. # ----------------------------------------------------------------------------
  32. # History:
  33. # This module is derived from the module originally found in the VTK source tree.
  34. #
  35. # ----------------------------------------------------------------------------
  36. # Note:
  37. # PostgreSQL_ADDITIONAL_VERSIONS is a variable that can be used to set the
  38. # version mumber of the implementation of PostgreSQL.
  39. # In Windows the default installation of PostgreSQL uses that as part of the path.
  40. # E.g C:\Program Files\PostgreSQL\8.4.
  41. # Currently, the following version numbers are known to this module:
  42. # "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0"
  43. #
  44. # To use this variable just do something like this:
  45. # set(PostgreSQL_ADDITIONAL_VERSIONS "9.2" "8.4.4")
  46. # before calling FIND_PACKAGE(PostgreSQL) in your CMakeLists.txt file.
  47. # This will mean that the versions you set here will be found first in the order
  48. # specified before the default ones are searched.
  49. #
  50. # ----------------------------------------------------------------------------
  51. # You may need to manually set:
  52. # PostgreSQL_INCLUDE_DIR - the path to where the PostgreSQL include files are.
  53. # PostgreSQL_LIBRARY_DIR - The path to where the PostgreSQL library files are.
  54. # If FindPostgreSQL.cmake cannot find the include files or the library files.
  55. #
  56. # ----------------------------------------------------------------------------
  57. # The following variables are set if PostgreSQL is found:
  58. # PostgreSQL_FOUND - Set to true when PostgreSQL is found.
  59. # PostgreSQL_INCLUDE_DIRS - Include directories for PostgreSQL
  60. # PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries
  61. # PostgreSQL_LIBRARIES - The PostgreSQL libraries.
  62. #
  63. # ----------------------------------------------------------------------------
  64. # If you have installed PostgreSQL in a non-standard location.
  65. # (Please note that in the following comments, it is assumed that <Your Path>
  66. # points to the root directory of the include directory of PostgreSQL.)
  67. # Then you have three options.
  68. # 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and
  69. # PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is
  70. # 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path()
  71. # to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file
  72. # SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include")
  73. # 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have
  74. # installed PostgreSQL, e.g. <Your Path>.
  75. #
  76. # ----------------------------------------------------------------------------
  77. 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")
  78. set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}")
  79. set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.")
  80. set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}")
  81. 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")
  82. set(PostgreSQL_ROOT_DIRECTORIES $ENV{PostgreSQL_ROOT})
  83. if(PostgreSQL_ROOT_DIRECTORIES)
  84. file(TO_CMAKE_PATH ${PostgreSQL_ROOT_DIRECTORIES} PostgreSQL_ROOT_DIRECTORIES)
  85. endif(PostgreSQL_ROOT_DIRECTORIES)
  86. set(PostgreSQL_KNOWN_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS}
  87. "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0")
  88. # Define additional search paths for root directories.
  89. if ( WIN32 )
  90. foreach (suffix ${PostgreSQL_KNOWN_VERSIONS} )
  91. set(PostgreSQL_ADDITIONAL_SEARCH_PATHS ${PostgreSQL_ADDITIONAL_SEARCH_PATHS} "C:/Program Files/PostgreSQL/${suffix}" )
  92. endforeach(suffix)
  93. endif( WIN32 )
  94. set( PostgreSQL_ROOT_DIRECTORIES
  95. ${PostgreSQL_ROOT_DIRECTORIES}
  96. ${PostgreSQL_ROOT}
  97. ${PostgreSQL_ADDITIONAL_SEARCH_PATHS}
  98. )
  99. #
  100. # Look for an installation.
  101. #
  102. find_path(PostgreSQL_INCLUDE_DIR
  103. NAMES libpq-fe.h
  104. PATHS
  105. # Look in other places.
  106. ${PostgreSQL_ROOT_DIRECTORIES}
  107. PATH_SUFFIXES
  108. pgsql
  109. postgresql
  110. include
  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. server
  121. pgsql
  122. postgresql
  123. include
  124. # Help the user find it if we cannot.
  125. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  126. )
  127. # The PostgreSQL library.
  128. set (PostgreSQL_LIBRARY_TO_FIND pq)
  129. # Setting some more prefixes for the library
  130. set (PostgreSQL_LIB_PREFIX "")
  131. if ( WIN32 )
  132. set (PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib")
  133. set ( PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND})
  134. endif()
  135. find_library( PostgreSQL_LIBRARY
  136. NAMES ${PostgreSQL_LIBRARY_TO_FIND}
  137. PATHS
  138. ${PostgreSQL_ROOT_DIRECTORIES}
  139. PATH_SUFFIXES
  140. lib
  141. )
  142. get_filename_component(PostgreSQL_LIBRARY_DIR ${PostgreSQL_LIBRARY} PATH)
  143. # Did we find anything?
  144. set( PostgreSQL_FOUND 0 )
  145. if ( EXISTS "${PostgreSQL_INCLUDE_DIR}" AND EXISTS "${PostgreSQL_LIBRARY_DIR}" )
  146. set( PostgreSQL_FOUND 1 )
  147. else ( EXISTS "${PostgreSQL_INCLUDE_DIR}" AND EXISTS "${PostgreSQL_LIBRARY_DIR}" )
  148. if ( POSTGRES_REQUIRED )
  149. message( FATAL_ERROR "PostgreSQL is required. ${PostgreSQL_ROOT_DIR_MESSAGE}" )
  150. endif ( POSTGRES_REQUIRED )
  151. endif (EXISTS "${PostgreSQL_INCLUDE_DIR}" AND EXISTS "${PostgreSQL_LIBRARY_DIR}" )
  152. # Now try to get the include and library path.
  153. if(PostgreSQL_FOUND)
  154. if(EXISTS "${PostgreSQL_INCLUDE_DIR}")
  155. set(PostgreSQL_INCLUDE_DIRS
  156. ${PostgreSQL_INCLUDE_DIR}
  157. )
  158. endif(EXISTS "${PostgreSQL_INCLUDE_DIR}")
  159. if(EXISTS "${PostgreSQL_TYPE_INCLUDE_DIR}")
  160. list(APPEND PostgreSQL_INCLUDE_DIRS
  161. ${PostgreSQL_TYPE_INCLUDE_DIR}
  162. )
  163. endif(EXISTS "${PostgreSQL_TYPE_INCLUDE_DIR}")
  164. if(EXISTS "${PostgreSQL_LIBRARY_DIR}")
  165. set(PostgreSQL_LIBRARY_DIRS
  166. ${PostgreSQL_LIBRARY_DIR}
  167. )
  168. set(PostgreSQL_LIBRARIES ${PostgreSQL_LIBRARY_TO_FIND})
  169. endif(EXISTS "${PostgreSQL_LIBRARY_DIR}")
  170. #message("Final PostgreSQL include dir: ${PostgreSQL_INCLUDE_DIRS}")
  171. #message("Final PostgreSQL library dir: ${PostgreSQL_LIBRARY_DIRS}")
  172. #message("Final PostgreSQL libraries: ${PostgreSQL_LIBRARIES}")
  173. endif(PostgreSQL_FOUND)
  174. if(NOT PostgreSQL_FOUND)
  175. if(NOT PostgreSQL_FIND_QUIETLY)
  176. message(STATUS "PostgreSQL was not found. ${PostgreSQL_DIR_MESSAGE}")
  177. else(NOT PostgreSQL_FIND_QUIETLY)
  178. if(PostgreSQL_FIND_REQUIRED)
  179. message(FATAL_ERROR "PostgreSQL was not found. ${PostgreSQL_DIR_MESSAGE}")
  180. endif(PostgreSQL_FIND_REQUIRED)
  181. endif(NOT PostgreSQL_FIND_QUIETLY)
  182. endif(NOT PostgreSQL_FOUND)