comparison Resources/CMake/PostgreSQLConfiguration.cmake @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children 9e5e7a8314e0
comparison
equal deleted inserted replaced
-1:000000000000 0:7cea966b6829
1 # Orthanc - A Lightweight, RESTful DICOM Store
2 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
3 # Department, University Hospital of Liege, Belgium
4 # Copyright (C) 2017-2018 Osimis S.A., Belgium
5 #
6 # This program is free software: you can redistribute it and/or
7 # modify it under the terms of the GNU Affero General Public License
8 # as published by the Free Software Foundation, either version 3 of
9 # the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Affero General Public License for more details.
15 #
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 #####################################################################
21 ## PostgreSQL
22 #####################################################################
23
24 INCLUDE(CheckTypeSize)
25 INCLUDE(CheckCSourceCompiles)
26 INCLUDE(CheckFunctionExists)
27 INCLUDE(CheckStructHasMember)
28
29
30 if (STATIC_BUILD OR NOT USE_SYSTEM_LIBPQ)
31 add_definitions(-DORTHANC_POSTGRESQL_STATIC=1)
32
33 SET(LIBPQ_MAJOR 9)
34 SET(LIBPQ_MINOR 6)
35 SET(LIBPQ_REVISION 1)
36 SET(LIBPQ_VERSION ${LIBPQ_MAJOR}.${LIBPQ_MINOR}.${LIBPQ_REVISION})
37
38 SET(LIBPQ_SOURCES_DIR ${CMAKE_BINARY_DIR}/postgresql-${LIBPQ_VERSION})
39 DownloadPackage(
40 "eaa7e267e89ea1ed2693d2b88d3cd290"
41 "http://www.orthanc-server.com/downloads/third-party/postgresql-${LIBPQ_VERSION}.tar.gz"
42 "${LIBPQ_SOURCES_DIR}")
43
44
45 ##
46 ## Platform-specific configuration
47 ##
48
49 if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
50 add_definitions(
51 -DEXEC_BACKEND
52 )
53
54 configure_file(
55 ${LIBPQ_SOURCES_DIR}/src/include/port/win32.h
56 ${AUTOGENERATED_DIR}/pg_config_os.h
57 COPYONLY)
58
59 elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
60 add_definitions(
61 -D_GNU_SOURCE
62 -D_THREAD_SAFE
63 -D_POSIX_PTHREAD_SEMANTICS
64 )
65
66 configure_file(
67 ${LIBPQ_SOURCES_DIR}/src/include/port/linux.h
68 ${AUTOGENERATED_DIR}/pg_config_os.h
69 COPYONLY)
70
71 elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
72 add_definitions(
73 -D_GNU_SOURCE
74 -D_THREAD_SAFE
75 -D_POSIX_PTHREAD_SEMANTICS
76 )
77
78 configure_file(
79 ${LIBPQ_SOURCES_DIR}/src/include/port/darwin.h
80 ${AUTOGENERATED_DIR}/pg_config_os.h
81 COPYONLY)
82
83 elseif (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
84 configure_file(
85 ${LIBPQ_SOURCES_DIR}/src/include/port/openbsd.h
86 ${AUTOGENERATED_DIR}/pg_config_os.h
87 COPYONLY)
88
89 elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
90 configure_file(
91 ${LIBPQ_SOURCES_DIR}/src/include/port/freebsd.h
92 ${AUTOGENERATED_DIR}/pg_config_os.h
93 COPYONLY)
94
95 else()
96 message(FATAL_ERROR "Support your platform here")
97 endif()
98
99
100 ##
101 ## Generation of "pg_config.h"
102 ##
103
104 if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
105 configure_file(
106 ${LIBPQ_SOURCES_DIR}/src/include/pg_config_ext.h.win32
107 ${AUTOGENERATED_DIR}/pg_config_ext.h
108 COPYONLY)
109
110 configure_file(
111 ${LIBPQ_SOURCES_DIR}/src/include/pg_config.h.win32
112 ${AUTOGENERATED_DIR}/pg_config.h
113 COPYONLY)
114
115 if (CMAKE_COMPILER_IS_GNUCXX) # MinGW
116 add_definitions(
117 -DPG_PRINTF_ATTRIBUTE=gnu_printf
118 -DHAVE_GETTIMEOFDAY
119 -DHAVE_LONG_LONG_INT_64
120 -DHAVE_STRUCT_ADDRINFO
121 -DHAVE_STRUCT_SOCKADDR_STORAGE
122 -DHAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
123 )
124 endif()
125
126 elseif(CROSS_COMPILING)
127 message(FATAL_ERROR "Cannot auto-generate the configuration file cross-compiling")
128
129 else()
130 configure_file(
131 ${CMAKE_CURRENT_LIST_DIR}/../PostgreSQL/pg_config_ext.h
132 ${AUTOGENERATED_DIR}/pg_config_ext.h
133 COPYONLY
134 )
135
136 set(CMAKE_EXTRA_INCLUDE_FILES "sys/socket.h;netdb.h;sys/types.h")
137
138 include(${CMAKE_CURRENT_LIST_DIR}/../PostgreSQL/func_accept_args.cmake)
139 set(ACCEPT_TYPE_ARG3 ${ACCEPT_TYPE_ARG3})
140
141 check_type_size("long int" SIZE_LONG_INT)
142 if (SIZE_LONG_INT EQUAL 8)
143 set(HAVE_LONG_INT_64 1)
144 endif()
145
146 check_type_size("long long int" SIZE_LONG_LONG_INT)
147 if (SIZE_LONG_LONG_INT EQUAL 8)
148 set(HAVE_LONG_LONG_INT_64 1)
149 endif()
150
151 file(READ ${CMAKE_CURRENT_LIST_DIR}/../PostgreSQL/c_flexmember.c SOURCE)
152 check_c_source_compiles("${SOURCE}" c_flexmember)
153 if (c_flexmember)
154 set(FLEXIBLE_ARRAY_MEMBER "/**/")
155 endif()
156
157 if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
158 CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
159 CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
160 set(PG_PRINTF_ATTRIBUTE "printf")
161 else()
162 file(READ ${CMAKE_CURRENT_LIST_DIR}/../PostgreSQL/printf_archetype.c SOURCE)
163 check_c_source_compiles("${SOURCE}" printf_archetype)
164 if (printf_archetype)
165 set(PG_PRINTF_ATTRIBUTE "gnu_printf")
166 else()
167 set(PG_PRINTF_ATTRIBUTE "printf")
168 endif()
169 endif()
170
171 check_function_exists("isinf" HAVE_ISINF)
172 check_function_exists("getaddrinfo" HAVE_GETADDRINFO)
173 check_function_exists("gettimeofday" HAVE_GETTIMEOFDAY)
174 check_function_exists("snprintf" HAVE_DECL_SNPRINTF)
175 check_function_exists("srandom" HAVE_SRANDOM)
176 check_function_exists("strlcat" HAVE_DECL_STRLCAT)
177 check_function_exists("strlcpy" HAVE_DECL_STRLCPY)
178 check_function_exists("unsetenv" HAVE_UNSETENV)
179 check_function_exists("vsnprintf" HAVE_DECL_VSNPRINTF)
180
181 check_type_size("struct addrinfo" SIZE_STRUCT_ADDRINFO)
182 if (HAVE_SIZE_STRUCT_ADDRINFO)
183 set(HAVE_STRUCT_ADDRINFO 1)
184 endif()
185
186 check_type_size("struct sockaddr_storage" SIZE_STRUCT_SOCKADDR_STORAGE)
187 if (HAVE_SIZE_STRUCT_SOCKADDR_STORAGE)
188 set(HAVE_STRUCT_SOCKADDR_STORAGE 1)
189 endif()
190
191 set(MEMSET_LOOP_LIMIT 1024) # This is hardcoded in "postgresql-9.6.1/configure"
192 set(DEF_PGPORT 5432) # Default port number of PostgreSQL
193 set(DEF_PGPORT_STR "\"5432\"") # Same as above, as a string
194 set(PG_VERSION "\"${LIBPQ_VERSION}\"") # Version of PostgreSQL, as a string
195
196 # Version of PostgreSQL, as a number
197 math(EXPR PG_VERSION_NUM "${LIBPQ_MAJOR} * 10000 + ${LIBPQ_MINOR} * 100 + ${LIBPQ_REVISION}")
198
199 set(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY 1) # TODO Autodetection
200
201 # Compute maximum alignment of any basic type.
202 # We assume long's alignment is at least as strong as char, short, or int;
203 # but we must check long long (if it exists) and double.
204 check_type_size("long" SIZE_LONG)
205 check_type_size("long long" SIZE_LONG_LONG)
206 check_type_size("double" SIZE_DOUBLE)
207 set(MAXIMUM_ALIGNOF ${SIZE_LONG})
208 if(SIZE_LONG_LONG AND SIZE_LONG_LONG GREATER MAXIMUM_ALIGNOF)
209 set(MAXIMUM_ALIGNOF ${SIZE_LONG_LONG})
210 endif()
211 if(SIZE_DOUBLE GREATER MAXIMUM_ALIGNOF)
212 set(MAXIMUM_ALIGNOF ${SIZE_DOUBLE})
213 endif()
214
215 check_include_file("poll.h" HAVE_POLL_H)
216 check_include_file("net/if.h" HAVE_NET_IF_H)
217 check_include_file("netinet/in.h" HAVE_NETINET_IN_H)
218 check_include_file("netinet/tcp.h" HAVE_NETINET_TCP_H)
219 check_include_file("sys/ioctl.h" HAVE_SYS_IOCTL_H)
220 check_include_file("sys/un.h" HAVE_SYS_UN_H)
221
222 If (NOT HAVE_NET_IF_H) # This is the case of OpenBSD
223 unset(HAVE_NET_IF_H CACHE)
224 check_include_files("sys/socket.h;net/if.h" HAVE_NET_IF_H)
225 endif()
226
227 if (NOT HAVE_NETINET_TCP_H) # This is the case of OpenBSD
228 unset(HAVE_NETINET_TCP_H CACHE)
229 check_include_files("sys/socket.h;netinet/tcp.h" HAVE_NETINET_TCP_H)
230 endif()
231
232
233 execute_process(
234 COMMAND
235 ${PYTHON_EXECUTABLE}
236 "${CMAKE_CURRENT_LIST_DIR}/../PostgreSQL/PrepareCMakeConfigurationFile.py"
237 "${LIBPQ_SOURCES_DIR}/src/include/pg_config.h.in"
238 "${AUTOGENERATED_DIR}/pg_config.h.in"
239 ERROR_VARIABLE NO_PG_CONFIG
240 OUTPUT_VARIABLE out
241 )
242
243 if (NO_PG_CONFIG)
244 message(FATAL_ERROR "Cannot find pg_config.h.in")
245 endif()
246
247 configure_file(
248 ${AUTOGENERATED_DIR}/pg_config.h.in
249 ${AUTOGENERATED_DIR}/pg_config.h)
250 endif()
251
252
253
254 ##
255 ## Generic configuration
256 ##
257
258 file(WRITE
259 ${AUTOGENERATED_DIR}/pg_config_paths.h
260 "")
261
262 add_definitions(
263 -D_REENTRANT
264 -DFRONTEND
265 -DUNSAFE_STAT_OK
266 -DSYSCONFDIR=""
267 )
268
269 include_directories(
270 ${LIBPQ_SOURCES_DIR}/src/include
271 ${LIBPQ_SOURCES_DIR}/src/include/libpq
272 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq
273 )
274
275 set(LIBPQ_SOURCES
276 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-auth.c
277 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-connect.c
278 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-exec.c
279 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-lobj.c
280 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-misc.c
281 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-print.c
282 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-protocol2.c
283 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-protocol3.c
284 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-secure.c
285 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/libpq-events.c
286 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/pqexpbuffer.c
287
288 # libpgport C files we always use
289 ${LIBPQ_SOURCES_DIR}/src/port/chklocale.c
290 ${LIBPQ_SOURCES_DIR}/src/port/inet_net_ntop.c
291 ${LIBPQ_SOURCES_DIR}/src/port/noblock.c
292 ${LIBPQ_SOURCES_DIR}/src/port/pgstrcasecmp.c
293 ${LIBPQ_SOURCES_DIR}/src/port/pqsignal.c
294 ${LIBPQ_SOURCES_DIR}/src/port/thread.c
295
296 ${LIBPQ_SOURCES_DIR}/src/backend/libpq/ip.c
297 ${LIBPQ_SOURCES_DIR}/src/backend/libpq/md5.c
298 ${LIBPQ_SOURCES_DIR}/src/backend/utils/mb/encnames.c
299 ${LIBPQ_SOURCES_DIR}/src/backend/utils/mb/wchar.c
300 )
301
302
303 if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
304 LIST(APPEND LIBPQ_SOURCES
305 ${LIBPQ_SOURCES_DIR}/src/port/strlcpy.c
306 )
307
308 elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
309 include_directories(
310 ${LIBPQ_SOURCES_DIR}/src/include/port/win32
311 ${LIBPQ_SOURCES_DIR}/src/port
312 )
313
314 LIST(APPEND LIBPQ_SOURCES
315 # libpgport C files that are needed if identified by configure
316 ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/win32.c
317 ${LIBPQ_SOURCES_DIR}/src/port/crypt.c
318 ${LIBPQ_SOURCES_DIR}/src/port/inet_aton.c
319 ${LIBPQ_SOURCES_DIR}/src/port/open.c
320 ${LIBPQ_SOURCES_DIR}/src/port/pgsleep.c
321 ${LIBPQ_SOURCES_DIR}/src/port/snprintf.c
322 ${LIBPQ_SOURCES_DIR}/src/port/system.c
323 ${LIBPQ_SOURCES_DIR}/src/port/win32setlocale.c
324 ${LIBPQ_SOURCES_DIR}/src/port/getaddrinfo.c
325 ${LIBPQ_SOURCES_DIR}/src/port/strlcpy.c
326 )
327
328 if (CMAKE_COMPILER_IS_GNUCXX OR
329 (MSVC AND MSVC_VERSION GREATER 1800))
330 # Starting Visual Studio 2013 (version 1800), it is necessary to also add "win32error.c"
331 LIST(APPEND LIBPQ_SOURCES ${LIBPQ_SOURCES_DIR}/src/port/win32error.c)
332 endif()
333
334 if (MSVC)
335 LIST(APPEND LIBPQ_SOURCES ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/pthread-win32.c)
336 endif()
337 endif()
338
339 if (CMAKE_COMPILER_IS_GNUCXX AND
340 NOT CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
341 LIST(APPEND LIBPQ_SOURCES
342 ${LIBPQ_SOURCES_DIR}/src/port/getpeereid.c
343 )
344
345 elseif (MSVC)
346 include_directories(
347 ${LIBPQ_SOURCES_DIR}/src/include/port/win32_msvc
348 )
349
350 LIST(APPEND LIBPQ_SOURCES
351 ${LIBPQ_SOURCES_DIR}/src/port/dirent.c
352 ${LIBPQ_SOURCES_DIR}/src/port/dirmod.c
353 )
354 endif()
355
356 source_group(ThirdParty\\PostgreSQL REGULAR_EXPRESSION ${LIBPQ_SOURCES_DIR}/.*)
357
358 else()
359 include(${CMAKE_CURRENT_LIST_DIR}/FindPostgreSQL.cmake)
360 include_directories(
361 ${PostgreSQL_INCLUDE_DIR}
362 ${PostgreSQL_TYPE_INCLUDE_DIR}
363 )
364 link_libraries(${PostgreSQL_LIBRARY})
365 endif()