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