FindPostgreSQL.cmake 6.9 KB

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