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: 5FindPostgreSQL 6-------------- 7 8Find the PostgreSQL installation. 9 10IMPORTED Targets 11^^^^^^^^^^^^^^^^ 12 13.. versionadded:: 3.14 14 15This module defines :prop_tgt:`IMPORTED` target ``PostgreSQL::PostgreSQL`` 16if PostgreSQL has been found. 17 18Result Variables 19^^^^^^^^^^^^^^^^ 20 21This module will set the following variables in your project: 22 23``PostgreSQL_FOUND`` 24 True if PostgreSQL is found. 25``PostgreSQL_LIBRARIES`` 26 the PostgreSQL libraries needed for linking 27``PostgreSQL_INCLUDE_DIRS`` 28 the directories of the PostgreSQL headers 29``PostgreSQL_LIBRARY_DIRS`` 30 the link directories for PostgreSQL libraries 31``PostgreSQL_VERSION_STRING`` 32 the version of PostgreSQL found 33``PostgreSQL_TYPE_INCLUDE_DIR`` 34 the directories of the PostgreSQL server headers 35 36Components 37^^^^^^^^^^ 38 39This module contains additional ``Server`` component, that forcibly checks 40for the presence of server headers. Note that ``PostgreSQL_TYPE_INCLUDE_DIR`` 41is set regardless of the presence of the ``Server`` component in find_package call. 42 43#]=======================================================================] 44 45# ---------------------------------------------------------------------------- 46# History: 47# This module is derived from the module originally found in the VTK source tree. 48# 49# ---------------------------------------------------------------------------- 50# Note: 51# PostgreSQL_ADDITIONAL_VERSIONS is a variable that can be used to set the 52# version number of the implementation of PostgreSQL. 53# In Windows the default installation of PostgreSQL uses that as part of the path. 54# E.g C:\Program Files\PostgreSQL\8.4. 55# Currently, the following version numbers are known to this module: 56# "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" 57# 58# To use this variable just do something like this: 59# set(PostgreSQL_ADDITIONAL_VERSIONS "9.2" "8.4.4") 60# before calling find_package(PostgreSQL) in your CMakeLists.txt file. 61# This will mean that the versions you set here will be found first in the order 62# specified before the default ones are searched. 63# 64# ---------------------------------------------------------------------------- 65# You may need to manually set: 66# PostgreSQL_INCLUDE_DIR - the path to where the PostgreSQL include files are. 67# PostgreSQL_LIBRARY_DIR - The path to where the PostgreSQL library files are. 68# If FindPostgreSQL.cmake cannot find the include files or the library files. 69# 70# ---------------------------------------------------------------------------- 71# The following variables are set if PostgreSQL is found: 72# PostgreSQL_FOUND - Set to true when PostgreSQL is found. 73# PostgreSQL_INCLUDE_DIRS - Include directories for PostgreSQL 74# PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries 75# PostgreSQL_LIBRARIES - The PostgreSQL libraries. 76# 77# The ``PostgreSQL::PostgreSQL`` imported target is also created. 78# 79# ---------------------------------------------------------------------------- 80# If you have installed PostgreSQL in a non-standard location. 81# (Please note that in the following comments, it is assumed that <Your Path> 82# points to the root directory of the include directory of PostgreSQL.) 83# Then you have three options. 84# 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and 85# PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is 86# 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path() 87# to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file 88# set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include") 89# 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have 90# installed PostgreSQL, e.g. <Your Path>. 91# 92# ---------------------------------------------------------------------------- 93 94cmake_policy(PUSH) 95cmake_policy(SET CMP0057 NEW) # if IN_LIST 96 97set(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") 98set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}") 99set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.") 100set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}") 101set(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") 102 103 104set(PostgreSQL_KNOWN_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS} 105 "13" "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") 106 107# Define additional search paths for root directories. 108set( PostgreSQL_ROOT_DIRECTORIES 109 ENV PostgreSQL_ROOT 110 ${PostgreSQL_ROOT} 111) 112foreach(suffix ${PostgreSQL_KNOWN_VERSIONS}) 113 if(WIN32) 114 list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES 115 "PostgreSQL/${suffix}/lib") 116 list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES 117 "PostgreSQL/${suffix}/include") 118 list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES 119 "PostgreSQL/${suffix}/include/server") 120 endif() 121 if(UNIX) 122 list(APPEND PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES 123 "postgresql${suffix}" 124 "pgsql-${suffix}/lib") 125 list(APPEND PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES 126 "postgresql${suffix}" 127 "postgresql/${suffix}" 128 "pgsql-${suffix}/include") 129 list(APPEND PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES 130 "postgresql${suffix}/server" 131 "postgresql/${suffix}/server" 132 "pgsql-${suffix}/include/server") 133 endif() 134endforeach() 135 136# 137# Look for an installation. 138# 139find_path(PostgreSQL_INCLUDE_DIR 140 NAMES libpq-fe.h 141 PATHS 142 # Look in other places. 143 ${PostgreSQL_ROOT_DIRECTORIES} 144 PATH_SUFFIXES 145 pgsql 146 postgresql 147 include 148 ${PostgreSQL_INCLUDE_ADDITIONAL_SEARCH_SUFFIXES} 149 # Help the user find it if we cannot. 150 DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}" 151) 152 153find_path(PostgreSQL_TYPE_INCLUDE_DIR 154 NAMES catalog/pg_type.h 155 PATHS 156 # Look in other places. 157 ${PostgreSQL_ROOT_DIRECTORIES} 158 PATH_SUFFIXES 159 postgresql 160 pgsql/server 161 postgresql/server 162 include/server 163 ${PostgreSQL_TYPE_ADDITIONAL_SEARCH_SUFFIXES} 164 # Help the user find it if we cannot. 165 DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}" 166) 167 168# The PostgreSQL library. 169set (PostgreSQL_LIBRARY_TO_FIND pq) 170# Setting some more prefixes for the library 171set (PostgreSQL_LIB_PREFIX "") 172if ( WIN32 ) 173 set (PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib") 174 set (PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND}) 175endif() 176 177function(__postgresql_find_library _name) 178 find_library(${_name} 179 NAMES ${ARGN} 180 PATHS 181 ${PostgreSQL_ROOT_DIRECTORIES} 182 PATH_SUFFIXES 183 lib 184 ${PostgreSQL_LIBRARY_ADDITIONAL_SEARCH_SUFFIXES} 185 # Help the user find it if we cannot. 186 DOC "The ${PostgreSQL_LIBRARY_DIR_MESSAGE}" 187 ) 188endfunction() 189 190# For compatibility with versions prior to this multi-config search, honor 191# any PostgreSQL_LIBRARY that is already specified and skip the search. 192if(PostgreSQL_LIBRARY) 193 set(PostgreSQL_LIBRARIES "${PostgreSQL_LIBRARY}") 194 get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY}" PATH) 195else() 196 __postgresql_find_library(PostgreSQL_LIBRARY_RELEASE ${PostgreSQL_LIBRARY_TO_FIND}) 197 __postgresql_find_library(PostgreSQL_LIBRARY_DEBUG ${PostgreSQL_LIBRARY_TO_FIND}d) 198 include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake) 199 select_library_configurations(PostgreSQL) 200 mark_as_advanced(PostgreSQL_LIBRARY_RELEASE PostgreSQL_LIBRARY_DEBUG) 201 if(PostgreSQL_LIBRARY_RELEASE) 202 get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY_RELEASE}" PATH) 203 elseif(PostgreSQL_LIBRARY_DEBUG) 204 get_filename_component(PostgreSQL_LIBRARY_DIR "${PostgreSQL_LIBRARY_DEBUG}" PATH) 205 else() 206 set(PostgreSQL_LIBRARY_DIR "") 207 endif() 208endif() 209 210if (PostgreSQL_INCLUDE_DIR) 211 # Some platforms include multiple pg_config.hs for multi-lib configurations 212 # This is a temporary workaround. A better solution would be to compile 213 # a dummy c file and extract the value of the symbol. 214 file(GLOB _PG_CONFIG_HEADERS "${PostgreSQL_INCLUDE_DIR}/pg_config*.h") 215 foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS}) 216 if(EXISTS "${_PG_CONFIG_HEADER}") 217 file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str 218 REGEX "^#define[\t ]+PG_VERSION_NUM[\t ]+.*") 219 if(pgsql_version_str) 220 string(REGEX REPLACE "^#define[\t ]+PG_VERSION_NUM[\t ]+([0-9]*).*" 221 "\\1" _PostgreSQL_VERSION_NUM "${pgsql_version_str}") 222 break() 223 endif() 224 endif() 225 endforeach() 226 if (_PostgreSQL_VERSION_NUM) 227 # 9.x and older encoding 228 if (_PostgreSQL_VERSION_NUM LESS 100000) 229 math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000") 230 math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000 / 100") 231 math(EXPR _PostgreSQL_patch_version "${_PostgreSQL_VERSION_NUM} % 100") 232 set(PostgreSQL_VERSION_STRING "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}.${_PostgreSQL_patch_version}") 233 unset(_PostgreSQL_major_version) 234 unset(_PostgreSQL_minor_version) 235 unset(_PostgreSQL_patch_version) 236 else () 237 math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000") 238 math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000") 239 set(PostgreSQL_VERSION_STRING "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}") 240 unset(_PostgreSQL_major_version) 241 unset(_PostgreSQL_minor_version) 242 endif () 243 else () 244 foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS}) 245 if(EXISTS "${_PG_CONFIG_HEADER}") 246 file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str 247 REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"") 248 if(pgsql_version_str) 249 string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*" 250 "\\1" PostgreSQL_VERSION_STRING "${pgsql_version_str}") 251 break() 252 endif() 253 endif() 254 endforeach() 255 endif () 256 unset(_PostgreSQL_VERSION_NUM) 257 unset(pgsql_version_str) 258endif() 259 260if("Server" IN_LIST PostgreSQL_FIND_COMPONENTS) 261 set(PostgreSQL_Server_FOUND TRUE) 262 if(NOT PostgreSQL_TYPE_INCLUDE_DIR) 263 set(PostgreSQL_Server_FOUND FALSE) 264 endif() 265endif() 266 267# Did we find anything? 268include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) 269find_package_handle_standard_args(PostgreSQL 270 REQUIRED_VARS PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR 271 HANDLE_COMPONENTS 272 VERSION_VAR PostgreSQL_VERSION_STRING) 273set(PostgreSQL_FOUND ${POSTGRESQL_FOUND}) 274 275function(__postgresql_import_library _target _var _config) 276 if(_config) 277 set(_config_suffix "_${_config}") 278 else() 279 set(_config_suffix "") 280 endif() 281 282 set(_lib "${${_var}${_config_suffix}}") 283 if(EXISTS "${_lib}") 284 if(_config) 285 set_property(TARGET ${_target} APPEND PROPERTY 286 IMPORTED_CONFIGURATIONS ${_config}) 287 endif() 288 set_target_properties(${_target} PROPERTIES 289 IMPORTED_LOCATION${_config_suffix} "${_lib}") 290 endif() 291endfunction() 292 293# Now try to get the include and library path. 294if(PostgreSQL_FOUND) 295 set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR}) 296 if(PostgreSQL_TYPE_INCLUDE_DIR) 297 list(APPEND PostgreSQL_INCLUDE_DIRS ${PostgreSQL_TYPE_INCLUDE_DIR}) 298 endif() 299 set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR}) 300 if (NOT TARGET PostgreSQL::PostgreSQL) 301 add_library(PostgreSQL::PostgreSQL UNKNOWN IMPORTED) 302 set_target_properties(PostgreSQL::PostgreSQL PROPERTIES 303 INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIRS}") 304 __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "") 305 __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "RELEASE") 306 __postgresql_import_library(PostgreSQL::PostgreSQL PostgreSQL_LIBRARY "DEBUG") 307 endif () 308endif() 309 310mark_as_advanced(PostgreSQL_INCLUDE_DIR PostgreSQL_TYPE_INCLUDE_DIR) 311 312cmake_policy(POP) 313