FindPostgreSQL.cmake 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # - Find the PostgreSQL installation.
  2. # In Windows, we make the assumption that, if the PostgreSQL files are installed, the default directory
  3. # will be C:\Program Files\PostgreSQL.
  4. #
  5. # This module defines
  6. # PostgreSQL_LIBRARIES - the PostgreSQL libraries needed for linking
  7. # PostgreSQL_INCLUDE_DIRS - the directories of the PostgreSQL headers
  8. # PostgreSQL_VERSION_STRING - the version of PostgreSQL found (since CMake 2.8.8)
  9. #=============================================================================
  10. # Copyright 2004-2009 Kitware, Inc.
  11. #
  12. # Distributed under the OSI-approved BSD License (the "License");
  13. # see accompanying file Copyright.txt for details.
  14. #
  15. # This software is distributed WITHOUT ANY WARRANTY; without even the
  16. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. # See the License for more information.
  18. #=============================================================================
  19. # (To distribute this file outside of CMake, substitute the full
  20. # License text for the above reference.)
  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()
  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(PostgreSQL_ADDITIONAL_SEARCH_PATHS ${PostgreSQL_ADDITIONAL_SEARCH_PATHS} "C:/Program Files/PostgreSQL/${suffix}" )
  82. endforeach()
  83. endif()
  84. set( PostgreSQL_ROOT_DIRECTORIES
  85. ${PostgreSQL_ROOT_DIRECTORIES}
  86. ${PostgreSQL_ROOT}
  87. ${PostgreSQL_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. pgsql
  99. postgresql
  100. include
  101. # Help the user find it if we cannot.
  102. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  103. )
  104. find_path(PostgreSQL_TYPE_INCLUDE_DIR
  105. NAMES catalog/pg_type.h
  106. PATHS
  107. # Look in other places.
  108. ${PostgreSQL_ROOT_DIRECTORIES}
  109. PATH_SUFFIXES
  110. pgsql/server
  111. postgresql/server
  112. include/server
  113. # Help the user find it if we cannot.
  114. DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
  115. )
  116. # The PostgreSQL library.
  117. set (PostgreSQL_LIBRARY_TO_FIND pq)
  118. # Setting some more prefixes for the library
  119. set (PostgreSQL_LIB_PREFIX "")
  120. if ( WIN32 )
  121. set (PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib")
  122. set ( PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND})
  123. endif()
  124. find_library( PostgreSQL_LIBRARY
  125. NAMES ${PostgreSQL_LIBRARY_TO_FIND}
  126. PATHS
  127. ${PostgreSQL_ROOT_DIRECTORIES}
  128. PATH_SUFFIXES
  129. lib
  130. )
  131. get_filename_component(PostgreSQL_LIBRARY_DIR ${PostgreSQL_LIBRARY} PATH)
  132. if (PostgreSQL_INCLUDE_DIR AND EXISTS "${PostgreSQL_INCLUDE_DIR}/pg_config.h")
  133. file(STRINGS "${PostgreSQL_INCLUDE_DIR}/pg_config.h" pgsql_version_str
  134. REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"")
  135. string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*" "\\1"
  136. PostgreSQL_VERSION_STRING "${pgsql_version_str}")
  137. unset(pgsql_version_str)
  138. endif()
  139. # Did we find anything?
  140. include(FindPackageHandleStandardArgs)
  141. find_package_handle_standard_args(PostgreSQL
  142. REQUIRED_VARS PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR
  143. VERSION_VAR PostgreSQL_VERSION_STRING)
  144. set( PostgreSQL_FOUND ${POSTGRESQL_FOUND})
  145. # Now try to get the include and library path.
  146. if(PostgreSQL_FOUND)
  147. set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR} ${PostgreSQL_TYPE_INCLUDE_DIR} )
  148. set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR} )
  149. set(PostgreSQL_LIBRARIES ${PostgreSQL_LIBRARY_TO_FIND})
  150. #message("Final PostgreSQL include dir: ${PostgreSQL_INCLUDE_DIRS}")
  151. #message("Final PostgreSQL library dir: ${PostgreSQL_LIBRARY_DIRS}")
  152. #message("Final PostgreSQL libraries: ${PostgreSQL_LIBRARIES}")
  153. endif()
  154. mark_as_advanced(PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR PostgreSQL_LIBRARY )