FindPostgreSQL.cmake
Go to the documentation of this file.
1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3 
4 #[=======================================================================[.rst:
5 FindPostgreSQL
6 --------------
7 
8 Find the PostgreSQL installation.
9 
10 IMPORTED Targets
11 ^^^^^^^^^^^^^^^^
12 
13 This module defines :prop_tgt:`IMPORTED` target ``PostgreSQL::PostgreSQL``
14 if PostgreSQL has been found.
15 
16 Result Variables
17 ^^^^^^^^^^^^^^^^
18 
19 This module will set the following variables in your project:
20 
21 ``PostgreSQL_FOUND``
22  True if PostgreSQL is found.
23 ``PostgreSQL_LIBRARIES``
24  the PostgreSQL libraries needed for linking
25 ``PostgreSQL_INCLUDE_DIRS``
26  the directories of the PostgreSQL headers
27 ``PostgreSQL_LIBRARY_DIRS``
28  the link directories for PostgreSQL libraries
29 ``PostgreSQL_VERSION_STRING``
30  the version of PostgreSQL found
31 #]=======================================================================]
32 
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 number 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 # "11" "10" "9.6" "9.5" "9.4" "9.3" "9.2" "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 # The ``PostgreSQL::PostgreSQL`` imported target is also created.
66 #
67 # ----------------------------------------------------------------------------
68 # If you have installed PostgreSQL in a non-standard location.
69 # (Please note that in the following comments, it is assumed that <Your Path>
70 # points to the root directory of the include directory of PostgreSQL.)
71 # Then you have three options.
72 # 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and
73 # PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is
74 # 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path()
75 # to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file
76 # set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include")
77 # 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have
78 # installed PostgreSQL, e.g. <Your Path>.
79 #
80 # ----------------------------------------------------------------------------
81 
82 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")
83 set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}")
84 set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.")
85 set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}")
86 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")
87 
88 
89 set(PostgreSQL_KNOWN_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS}
90  "12" "11" "10" "9.6" "9.5" "9.4" "9.3" "9.2" "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0")
91 
92 # Define additional search paths for root directories.
93 set( PostgreSQL_ROOT_DIRECTORIES
94  ENV PostgreSQL_ROOT
95  ${PostgreSQL_ROOT}
96 )
97 foreach(suffix ${PostgreSQL_KNOWN_VERSIONS})
98  if(WIN32)
99  list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES
100  "PostgreSQL/${suffix}/lib")
101  list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
102  "PostgreSQL/${suffix}/include")
103  list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES
104  "PostgreSQL/${suffix}/include/server")
105  endif()
106  if(UNIX)
107  list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES
108  "postgresql${suffix}"
109  "pgsql-${suffix}/lib")
110  list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES
111  "postgresql${suffix}"
112  "postgresql/${suffix}"
113  "pgsql-${suffix}/include")
114  list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES
115  "postgresql${suffix}/server"
116  "postgresql/${suffix}/server"
117  "pgsql-${suffix}/include/server")
118  endif()
119 endforeach()
120 
121 #
122 # Look for an installation.
123 #
124 find_path(PostgreSQL_INCLUDE_DIR
125  NAMES libpq-fe.h
126  PATHS
127  # Look in other places.
128  ${PostgreSQL_ROOT_DIRECTORIES}
129  PATH_SUFFIXES
130  pgsql
131  postgresql
132  include
133  ${PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES}
134  # Help the user find it if we cannot.
135  DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
136 )
137 
138 find_path(PostgreSQL_TYPE_INCLUDE_DIR
139  NAMES catalog/pg_type.h
140  PATHS
141  # Look in other places.
142  ${PostgreSQL_ROOT_DIRECTORIES}
143  PATH_SUFFIXES
144  postgresql
145  pgsql/server
146  postgresql/server
147  include/server
148  ${PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES}
149  # Help the user find it if we cannot.
150  DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
151 )
152 
153 # The PostgreSQL library.
154 set (PostgreSQL_LIBRARY_TO_FIND pq)
155 # Setting some more prefixes for the library
156 set (PostgreSQL_LIB_PREFIX "")
157 if ( WIN32 )
158  set (PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib")
159  set (PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND})
160 endif()
161 
162 function(__postgresql_find_library _name)
163  find_library(${_name}
164  NAMES ${ARGN}
165  PATHS
166  ${PostgreSQL_ROOT_DIRECTORIES}
167  PATH_SUFFIXES
168  lib
169  ${PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES}
170  # Help the user find it if we cannot.
171  DOC "The ${PostgreSQL_LIBRARY_DIR_MESSAGE}"
172  )
173 endfunction()
174 
175 # For compatibility with versions prior to this multi-config search, honor
176 # any PostgreSQL_LIBRARY that is already specified and skip the search.
177 if(PostgreSQL_LIBRARY)
178  set(PostgreSQL_LIBRARIES "${PostgreSQL_LIBRARY}")
179  get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY}" PATH)
180 else()
181  __postgresql_find_library(PostgreSQL_LIBRARY_RELEASE ${PostgreSQL_LIBRARY_TO_FIND})
182  __postgresql_find_library(PostgreSQL_LIBRARY_DEBUG ${PostgreSQL_LIBRARY_TO_FIND}d)
183  include(SelectLibraryConfigurations)
184  select_library_configurations(PostgreSQL)
185  mark_as_advanced(PostgreSQL_LIBRARY_RELEASE PostgreSQL_LIBRARY_DEBUG)
186  if(PostgreSQL_LIBRARY_RELEASE)
187  get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY_RELEASE}" PATH)
188  elseif(PostgreSQL_LIBRARY_DEBUG)
189  get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY_DEBUG}" PATH)
190  else()
191  set(PostgreSQL_LIBRARY_DIR "")
192  endif()
193 endif()
194 
195 if (PostgreSQL_INCLUDE_DIR)
196  # Some platforms include multiple pg_config.hs for multi-lib configurations
197  # This is a temporary workaround. A better solution would be to compile
198  # a dummy c file and extract the value of the symbol.
199  file(GLOB _PG_CONFIG_HEADERS "${PostgreSQL_INCLUDE_DIR}/pg_config*.h")
200  foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS})
201  if(EXISTS "${_PG_CONFIG_HEADER}")
202  file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str
203  REGEX "^#define[\t ]+PG_VERSION_NUM[\t ]+.*")
204  if(pgsql_version_str)
205  string(REGEX REPLACE "^#define[\t ]+PG_VERSION_NUM[\t ]+([0-9]*).*"
206  "\\1" _PostgreSQL_VERSION_NUM "${pgsql_version_str}")
207  break()
208  endif()
209  endif()
210  endforeach()
211  if (_PostgreSQL_VERSION_NUM)
212  # 9.x and older encoding
213  if (_PostgreSQL_VERSION_NUM LESS 100000)
214  math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000")
215  math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000 / 100")
216  math(EXPR _PostgreSQL_patch_version "${_PostgreSQL_VERSION_NUM} % 100")
217  set(PostgreSQL_VERSION_STRING "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}.${_PostgreSQL_patch_version}")
218  unset(_PostgreSQL_major_version)
219  unset(_PostgreSQL_minor_version)
220  unset(_PostgreSQL_patch_version)
221  else ()
222  math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000")
223  math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000")
224  set(PostgreSQL_VERSION_STRING "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}")
225  unset(_PostgreSQL_major_version)
226  unset(_PostgreSQL_minor_version)
227  endif ()
228  else ()
229  foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS})
230  if(EXISTS "${_PG_CONFIG_HEADER}")
231  file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str
232  REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"")
233  if(pgsql_version_str)
234  string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*"
235  "\\1" PostgreSQL_VERSION_STRING "${pgsql_version_str}")
236  break()
237  endif()
238  endif()
239  endforeach()
240  endif ()
241  unset(_PostgreSQL_VERSION_NUM)
242  unset(pgsql_version_str)
243 endif()
244 
245 # Did we find anything?
246 include(FindPackageHandleStandardArgs)
247 find_package_handle_standard_args(PostgreSQL
248  REQUIRED_VARS PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR
249  VERSION_VAR PostgreSQL_VERSION_STRING)
250 set(PostgreSQL_FOUND ${POSTGRESQL_FOUND})
251 
252 function(__postgresql_import_library _target _var _config)
253  if(_config)
254  set(_config_suffix "_${_config}")
255  else()
256  set(_config_suffix "")
257  endif()
258 
259  set(_lib "${${_var}${_config_suffix}}")
260  if(EXISTS "${_lib}")
261  if(_config)
262  set_property(TARGET ${_target} APPEND PROPERTY
263  IMPORTED_CONFIGURATIONS ${_config})
264  endif()
265  set_target_properties(${_target} PROPERTIES
266  IMPORTED_LOCATION${_config_suffix} "${_lib}")
267  endif()
268 endfunction()
269 
270 # Now try to get the include and library path.
271 if(PostgreSQL_FOUND)
272  if (NOT TARGET PostgreSQL::PostgreSQL)
273  add_library(PostgreSQL::PostgreSQL UNKNOWN IMPORTED)
274  set_target_properties(PostgreSQL::PostgreSQL PROPERTIES
275  INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIR};${PostgreSQL_TYPE_INCLUDE_DIR}")
276  __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "")
277  __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "RELEASE")
278  __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "DEBUG")
279  endif ()
280  set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR} ${PostgreSQL_TYPE_INCLUDE_DIR} )
281  set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR} )
282 endif()
283 
284 mark_as_advanced(PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR)
boost::graph_traits< vtkGraph *>::vertex_descriptor target(boost::graph_traits< vtkGraph *>::edge_descriptor e, vtkGraph *)
version
function __postgresql_import_library(_target, _var, _config)
string