File: FindPostgreSQL.cmake

package info (click to toggle)
h3-pg 4.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: sql: 4,469; ansic: 3,497; python: 322; sh: 56; makefile: 18
file content (207 lines) | stat: -rw-r--r-- 8,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Based on:
# https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#find-modules
# ----------------------------------------------------------------------------

# https://www.postgresql.org/support/versioning/
set(PostgreSQL_SUPPORTED_VERSIONS ${PostgreSQL_ADDITIONAL_VERSIONS}
  "18" "17" "16" "15" "14" "13")

# Use `FIND_VERSION` to locate specific version of pg_config, or fallback to known versions
if(PostgreSQL_FIND_VERSION)
  list(PREPEND PostgreSQL_SUPPORTED_VERSIONS "${PostgreSQL_FIND_VERSION_MAJOR}")
endif()

foreach(suffix ${PostgreSQL_SUPPORTED_VERSIONS})
  if(WIN32)
    list(APPEND PostgreSQL_CONFIG_PATH_SUFFIXES
      "PostgreSQL/${suffix}/bin")
  endif()
  if(UNIX)
    list(APPEND PostgreSQL_CONFIG_HINTS
      "/usr/lib/postgresql/${suffix}/bin")
  endif()
endforeach()

# Configuration will be based on values gathered from `pg_config`
find_program(PostgreSQL_CONFIG pg_config REQUIRED
  HINTS ${PostgreSQL_CONFIG_HINTS}
  PATH_SUFFIXES ${PostgreSQL_CONFIG_PATH_SUFFIXES}
)

# Grab information about the installed version of PostgreSQL
execute_process(COMMAND ${PostgreSQL_CONFIG} --version           OUTPUT_VARIABLE PostgreSQL_VERSION            OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PostgreSQL_CONFIG} --bindir            OUTPUT_VARIABLE PostgreSQL_BIN_DIR            OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PostgreSQL_CONFIG} --sharedir          OUTPUT_VARIABLE PostgreSQL_SHARE_DIR          OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PostgreSQL_CONFIG} --includedir        OUTPUT_VARIABLE PostgreSQL_INCLUDE_DIR        OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PostgreSQL_CONFIG} --pkgincludedir     OUTPUT_VARIABLE PostgreSQL_PKG_INCLUDE_DIR    OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PostgreSQL_CONFIG} --includedir-server OUTPUT_VARIABLE PostgreSQL_SERVER_INCLUDE_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PostgreSQL_CONFIG} --libdir            OUTPUT_VARIABLE PostgreSQL_LIBRARY_DIR        OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PostgreSQL_CONFIG} --pkglibdir         OUTPUT_VARIABLE PostgreSQL_PKG_LIBRARY_DIR    OUTPUT_STRIP_TRAILING_WHITESPACE)

# @TODO: Figure out if we need _INCLUDE_DIR and/or _PKG_INCLUDE_DIR

# Create include dirs list
list(APPEND PostgreSQL_INCLUDE_DIRS "${PostgreSQL_INCLUDE_DIR}" "${PostgreSQL_PKG_INCLUDE_DIR}" "${PostgreSQL_SERVER_INCLUDE_DIR}")
list(APPEND PostgreSQL_LIBRARY_DIRS "${PostgreSQL_LIBRARY_DIR}")

# Set library to search for (which is different on WIN32)
set(FIND_LIBRARY pq)

# Platform fixes: Windows
# https://wiki.postgresql.org/wiki/Building_and_Installing_PostgreSQL_Extension_Modules
if(WIN32)
  set(FIND_LIBRARY postgres)
  list(APPEND PostgreSQL_INCLUDE_DIRS "${PostgreSQL_SERVER_INCLUDE_DIR}/port/win32")
  if(MSVC)
    list(APPEND PostgreSQL_INCLUDE_DIRS "${PostgreSQL_SERVER_INCLUDE_DIR}/port/win32_msvc")
  endif(MSVC)
endif(WIN32)

# Platform fixes: MacOS
# https://github.com/postgres/postgres/blob/master/src/makefiles/Makefile.darwin
set(PostgreSQL_LINK_FLAGS "")
# @TODO: Find out if this can be done in a more cmakey way
if(APPLE)
  find_program(PostgreSQL_EXECUTABLE postgres REQUIRED NO_DEFAULT_PATH PATHS ${PostgreSQL_BIN_DIR})
  set(PostgreSQL_LINK_FLAGS "-bundle_loader ${PostgreSQL_EXECUTABLE}")
endif()

# ----------------------------------------------------------------------------
# Now we need to find the libraries and include files
# if the library is available with multiple configurations, you can use SelectLibraryConfigurations
# to automatically set the _LIBRARY variable:

# @TODO: Find out if we can actually find any debug libraries on each platform

find_library(PostgreSQL_LIBRARY_RELEASE
  NAMES ${FIND_LIBRARY}
  PATHS ${PostgreSQL_LIBRARY_DIRS}
)

find_library(PostgreSQL_LIBRARY_DEBUG
  NAMES ${FIND_LIBRARY}d # <-- debug
  PATHS ${PostgreSQL_LIBRARY_DIRS}
)

include(SelectLibraryConfigurations)
select_library_configurations(PostgreSQL)

# ----------------------------------------------------------------------------
# If you have a good way of getting the version (from a header file, for example),
# you can use that information to set Foo_VERSION (although note that find modules have
# traditionally used Foo_VERSION_STRING, so you may want to set both).

string(REGEX MATCHALL "[0-9]+" PostgreSQL_VERSION_LIST "${PostgreSQL_VERSION}")
list(GET PostgreSQL_VERSION_LIST 0 PostgreSQL_VERSION_MAJOR)
list(GET PostgreSQL_VERSION_LIST 1 PostgreSQL_VERSION_MINOR)
set(PostgreSQL_VERSION "${PostgreSQL_VERSION_MAJOR}.${PostgreSQL_VERSION_MINOR}")

# ----------------------------------------------------------------------------
# Handle additional components

if("PostGIS" IN_LIST PostgreSQL_FIND_COMPONENTS)
  find_library(
    POSTGIS
    NAMES postgis postgis-3 postgis-3.so
    PATHS ${PostgreSQL_PKG_LIBRARY_DIR}
  )

  set(PostgreSQL_PostGIS_FOUND TRUE)
  if(NOT POSTGIS)
    set(PostgreSQL_PostGIS_FOUND FALSE)
  endif()
endif()

# ----------------------------------------------------------------------------
# Now we can use FindPackageHandleStandardArgs to do
# most of the rest of the work for us
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PostgreSQL
  REQUIRED_VARS
    PostgreSQL_LIBRARY
    PostgreSQL_INCLUDE_DIRS
  HANDLE_COMPONENTS
  VERSION_VAR PostgreSQL_VERSION
)

# ----------------------------------------------------------------------------
# At this point, we have to provide a way for users of the find module to link to the library or libraries that were found.
#
# If the library is available with multiple configurations, the IMPORTED_CONFIGURATIONS target property should also be populated:

if(PostgreSQL_FOUND)
  # When providing imported targets, these should be namespaced (hence the Foo:: prefix);
  # CMake will recognize that values passed to target_link_libraries() that contain :: in their name
  # are supposed to be imported targets (rather than just library names),
  if (NOT TARGET PostgreSQL::PostgreSQL)
    add_library(PostgreSQL::PostgreSQL UNKNOWN IMPORTED)
  endif()
  
  # The RELEASE variant should be listed first in the property so that the variant is chosen
  # if the user uses a configuration which is not an exact match for any listed IMPORTED_CONFIGURATIONS.
  if (PostgreSQL_LIBRARY_RELEASE)
    set_property(TARGET PostgreSQL::PostgreSQL APPEND PROPERTY
      IMPORTED_CONFIGURATIONS RELEASE
    )
    set_target_properties(PostgreSQL::PostgreSQL PROPERTIES
      IMPORTED_LOCATION_RELEASE "${PostgreSQL_LIBRARY_RELEASE}"
    )
  endif()

  if (PostgreSQL_LIBRARY_DEBUG)
    set_property(TARGET PostgreSQL::PostgreSQL APPEND PROPERTY
      IMPORTED_CONFIGURATIONS DEBUG
    )
    set_target_properties(PostgreSQL::PostgreSQL PROPERTIES
      IMPORTED_LOCATION_DEBUG "${PostgreSQL_LIBRARY_DEBUG}"
    )
  endif()

  set_target_properties(PostgreSQL::PostgreSQL PROPERTIES
    #INTERFACE_COMPILE_OPTIONS "${PostgreSQL_CFLAGS_OTHER}"
    INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIRS}"
  )
endif()

# Most of the cache variables should be hidden in the `ccmake` interface unless the user explicitly asks to edit them.
mark_as_advanced(
  PostgreSQL_INCLUDE_DIRS
  PostgreSQL_LIBRARY
)

# ----------------------------------------------------------------------------

# Traditional variables (from pkg-config):
#
# _FOUND            has the package been found
# _LIBRARIES        ?
# _LINK_LIBRARIES   ?
# _LIBRARY_DIRS     ?
# _LDFLAGS          ?
# _LDFLAGS_OTHER    ?
# _INCLUDE_DIRS     ?
# _CFLAGS           ?
# _CFLAGS_OTHER     required non-include-dir CFLAGS to stdout
# 
# _VERSION          full version of found package
# _PREFIX           ?
# _INCLUDEDIR       ?
# _LIBDIR           ?

# debug
message(STATUS "Find version:  ${PostgreSQL_FIND_VERSION}")
message(STATUS "Find major:    ${PostgreSQL_FIND_VERSION_MAJOR}")
message(STATUS "Find minor:    ${PostgreSQL_FIND_VERSION_MINOR}")

message(STATUS "Found version: ${PostgreSQL_VERSION}")
message(STATUS "Found major:   ${PostgreSQL_VERSION_MAJOR}")
message(STATUS "Found minor:   ${PostgreSQL_VERSION_MINOR}")

# Get all variables
GET_CMAKE_PROPERTY(vars VARIABLES)
FOREACH(var ${vars})
  STRING(REGEX MATCH "^PostgreSQL_" item ${var})
  IF(item)
    message(STATUS "${var} = ${${var}}")
  ENDIF(item)
ENDFOREACH(var)