1
0

FindPostgreSQL.cmake 7.5 KB

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