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-2021 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 Lesser 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# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this program. If not, see
18# <http://www.gnu.org/licenses/>.
19
20
21if (STATIC_BUILD OR NOT USE_SYSTEM_CURL)
22  SET(CURL_SOURCES_DIR ${CMAKE_BINARY_DIR}/curl-7.64.0)
23  SET(CURL_URL "http://orthanc.osimis.io/ThirdPartyDownloads/curl-7.64.0.tar.gz")
24  SET(CURL_MD5 "a026740d599a32bcbbe6e70679397899")
25
26  if (IS_DIRECTORY "${CURL_SOURCES_DIR}")
27    set(FirstRun OFF)
28  else()
29    set(FirstRun ON)
30  endif()
31
32  DownloadPackage(${CURL_MD5} ${CURL_URL} "${CURL_SOURCES_DIR}")
33
34  if (FirstRun)
35    execute_process(
36      COMMAND ${PATCH_EXECUTABLE} -p0 -N -i
37      ${CMAKE_CURRENT_LIST_DIR}/../Patches/curl-7.64.0-cmake.patch
38      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
39      RESULT_VARIABLE Failure
40      )
41
42    if (Failure)
43      message(FATAL_ERROR "Error while patching a file")
44    endif()
45  endif()
46
47  include_directories(
48    ${CURL_SOURCES_DIR}/include
49    )
50
51  AUX_SOURCE_DIRECTORY(${CURL_SOURCES_DIR}/lib CURL_SOURCES)
52  AUX_SOURCE_DIRECTORY(${CURL_SOURCES_DIR}/lib/vauth CURL_SOURCES)
53  AUX_SOURCE_DIRECTORY(${CURL_SOURCES_DIR}/lib/vtls CURL_SOURCES)
54  source_group(ThirdParty\\LibCurl REGULAR_EXPRESSION ${CURL_SOURCES_DIR}/.*)
55
56  add_definitions(
57    -DBUILDING_LIBCURL=1
58    -DCURL_STATICLIB=1
59    -DCURL_DISABLE_LDAPS=1
60    -DCURL_DISABLE_LDAP=1
61    -DCURL_DISABLE_DICT=1
62    -DCURL_DISABLE_FILE=1
63    -DCURL_DISABLE_FTP=1
64    -DCURL_DISABLE_GOPHER=1
65    -DCURL_DISABLE_LDAP=1
66    -DCURL_DISABLE_LDAPS=1
67    -DCURL_DISABLE_POP3=1
68    #-DCURL_DISABLE_PROXY=1
69    -DCURL_DISABLE_RTSP=1
70    -DCURL_DISABLE_TELNET=1
71    -DCURL_DISABLE_TFTP=1
72    )
73
74  if (ENABLE_SSL)
75    add_definitions(
76      #-DHAVE_LIBSSL=1
77      -DUSE_OPENSSL=1
78      -DHAVE_OPENSSL_ENGINE_H=1
79      -DUSE_SSLEAY=1
80      )
81  endif()
82
83  if (NOT EXISTS "${CURL_SOURCES_DIR}/lib/vauth/vauth/vauth.h")
84    #file(WRITE ${CURL_SOURCES_DIR}/lib/curl_config.h "")
85
86    file(WRITE ${CURL_SOURCES_DIR}/lib/vauth/vauth/vauth.h "#include \"../vauth.h\"\n")
87    file(WRITE ${CURL_SOURCES_DIR}/lib/vauth/vauth/digest.h "#include \"../digest.h\"\n")
88    file(WRITE ${CURL_SOURCES_DIR}/lib/vauth/vauth/ntlm.h "#include \"../ntlm.h\"\n")
89    file(WRITE ${CURL_SOURCES_DIR}/lib/vauth/vtls/vtls.h "#include \"../../vtls/vtls.h\"\n")
90
91    file(GLOB CURL_LIBS_HEADERS ${CURL_SOURCES_DIR}/lib/*.h)
92    foreach (header IN LISTS CURL_LIBS_HEADERS)
93      get_filename_component(filename ${header} NAME)
94      file(WRITE ${CURL_SOURCES_DIR}/lib/vauth/${filename} "#include \"../${filename}\"\n")
95      file(WRITE ${CURL_SOURCES_DIR}/lib/vtls/${filename} "#include \"../${filename}\"\n")
96    endforeach()
97  endif()
98
99  if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR
100      ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR
101      ${CMAKE_SYSTEM_NAME} STREQUAL "DragonFly" OR
102      ${CMAKE_SYSTEM_NAME} STREQUAL "kFreeBSD" OR
103      ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
104    if ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
105      SET(TMP_OS "x86_64")
106    else()
107      SET(TMP_OS "x86")
108    endif()
109
110    set_property(
111      SOURCE ${CURL_SOURCES}
112      PROPERTY COMPILE_DEFINITIONS "HAVE_CONFIG_H=1;OS=\"${TMP_OS}\""
113      )
114
115    include(${CURL_SOURCES_DIR}/CMake/Macros.cmake)
116
117    # WARNING: Do *not* reorder the "check_include_file_concat()" below!
118    check_include_file_concat("stdio.h"          HAVE_STDIO_H)
119    check_include_file_concat("inttypes.h"       HAVE_INTTYPES_H)
120    check_include_file_concat("sys/filio.h"      HAVE_SYS_FILIO_H)
121    check_include_file_concat("sys/ioctl.h"      HAVE_SYS_IOCTL_H)
122    check_include_file_concat("sys/param.h"      HAVE_SYS_PARAM_H)
123    check_include_file_concat("sys/poll.h"       HAVE_SYS_POLL_H)
124    check_include_file_concat("sys/resource.h"   HAVE_SYS_RESOURCE_H)
125    check_include_file_concat("sys/select.h"     HAVE_SYS_SELECT_H)
126    check_include_file_concat("sys/socket.h"     HAVE_SYS_SOCKET_H)
127    check_include_file_concat("sys/sockio.h"     HAVE_SYS_SOCKIO_H)
128    check_include_file_concat("sys/stat.h"       HAVE_SYS_STAT_H)
129    check_include_file_concat("sys/time.h"       HAVE_SYS_TIME_H)
130    check_include_file_concat("sys/types.h"      HAVE_SYS_TYPES_H)
131    check_include_file_concat("sys/uio.h"        HAVE_SYS_UIO_H)
132    check_include_file_concat("sys/un.h"         HAVE_SYS_UN_H)
133    check_include_file_concat("sys/utime.h"      HAVE_SYS_UTIME_H)
134    check_include_file_concat("sys/xattr.h"      HAVE_SYS_XATTR_H)
135    check_include_file_concat("alloca.h"         HAVE_ALLOCA_H)
136    check_include_file_concat("arpa/inet.h"      HAVE_ARPA_INET_H)
137    check_include_file_concat("arpa/tftp.h"      HAVE_ARPA_TFTP_H)
138    check_include_file_concat("assert.h"         HAVE_ASSERT_H)
139    check_include_file_concat("crypto.h"         HAVE_CRYPTO_H)
140    check_include_file_concat("des.h"            HAVE_DES_H)
141    check_include_file_concat("err.h"            HAVE_ERR_H)
142    check_include_file_concat("errno.h"          HAVE_ERRNO_H)
143    check_include_file_concat("fcntl.h"          HAVE_FCNTL_H)
144    check_include_file_concat("idn2.h"           HAVE_IDN2_H)
145    check_include_file_concat("ifaddrs.h"        HAVE_IFADDRS_H)
146    check_include_file_concat("io.h"             HAVE_IO_H)
147    check_include_file_concat("krb.h"            HAVE_KRB_H)
148    check_include_file_concat("libgen.h"         HAVE_LIBGEN_H)
149    check_include_file_concat("limits.h"         HAVE_LIMITS_H)
150    check_include_file_concat("locale.h"         HAVE_LOCALE_H)
151    check_include_file_concat("net/if.h"         HAVE_NET_IF_H)
152    check_include_file_concat("netdb.h"          HAVE_NETDB_H)
153    check_include_file_concat("netinet/in.h"     HAVE_NETINET_IN_H)
154    check_include_file_concat("netinet/tcp.h"    HAVE_NETINET_TCP_H)
155
156    check_include_file_concat("pem.h"            HAVE_PEM_H)
157    check_include_file_concat("poll.h"           HAVE_POLL_H)
158    check_include_file_concat("pwd.h"            HAVE_PWD_H)
159    check_include_file_concat("rsa.h"            HAVE_RSA_H)
160    check_include_file_concat("setjmp.h"         HAVE_SETJMP_H)
161    check_include_file_concat("sgtty.h"          HAVE_SGTTY_H)
162    check_include_file_concat("signal.h"         HAVE_SIGNAL_H)
163    check_include_file_concat("ssl.h"            HAVE_SSL_H)
164    check_include_file_concat("stdbool.h"        HAVE_STDBOOL_H)
165    check_include_file_concat("stdint.h"         HAVE_STDINT_H)
166    check_include_file_concat("stdio.h"          HAVE_STDIO_H)
167    check_include_file_concat("stdlib.h"         HAVE_STDLIB_H)
168    check_include_file_concat("string.h"         HAVE_STRING_H)
169    check_include_file_concat("strings.h"        HAVE_STRINGS_H)
170    check_include_file_concat("stropts.h"        HAVE_STROPTS_H)
171    check_include_file_concat("termio.h"         HAVE_TERMIO_H)
172    check_include_file_concat("termios.h"        HAVE_TERMIOS_H)
173    check_include_file_concat("time.h"           HAVE_TIME_H)
174    check_include_file_concat("unistd.h"         HAVE_UNISTD_H)
175    check_include_file_concat("utime.h"          HAVE_UTIME_H)
176    check_include_file_concat("x509.h"           HAVE_X509_H)
177
178    check_include_file_concat("process.h"        HAVE_PROCESS_H)
179    check_include_file_concat("stddef.h"         HAVE_STDDEF_H)
180    check_include_file_concat("dlfcn.h"          HAVE_DLFCN_H)
181    check_include_file_concat("malloc.h"         HAVE_MALLOC_H)
182    check_include_file_concat("memory.h"         HAVE_MEMORY_H)
183    check_include_file_concat("netinet/if_ether.h" HAVE_NETINET_IF_ETHER_H)
184    check_include_file_concat("stdint.h"        HAVE_STDINT_H)
185    check_include_file_concat("sockio.h"        HAVE_SOCKIO_H)
186    check_include_file_concat("sys/utsname.h"   HAVE_SYS_UTSNAME_H)
187
188    check_type_size("size_t"  SIZEOF_SIZE_T)
189    check_type_size("ssize_t"  SIZEOF_SSIZE_T)
190    check_type_size("long long"  SIZEOF_LONG_LONG)
191    check_type_size("long"  SIZEOF_LONG)
192    check_type_size("short"  SIZEOF_SHORT)
193    check_type_size("int"  SIZEOF_INT)
194    check_type_size("__int64"  SIZEOF___INT64)
195    check_type_size("long double"  SIZEOF_LONG_DOUBLE)
196    check_type_size("time_t"  SIZEOF_TIME_T)
197    check_type_size("off_t"  SIZEOF_OFF_T)
198    check_type_size("socklen_t" CURL_SIZEOF_CURL_SOCKLEN_T)
199
200    check_symbol_exists(basename      "${CURL_INCLUDES}" HAVE_BASENAME)
201    check_symbol_exists(socket        "${CURL_INCLUDES}" HAVE_SOCKET)
202    # poll on macOS is unreliable, it first did not exist, then was broken until
203    # fixed in 10.9 only to break again in 10.12.
204    if(NOT APPLE)
205      check_symbol_exists(poll        "${CURL_INCLUDES}" HAVE_POLL)
206    endif()
207    check_symbol_exists(select        "${CURL_INCLUDES}" HAVE_SELECT)
208    check_symbol_exists(strdup        "${CURL_INCLUDES}" HAVE_STRDUP)
209    check_symbol_exists(strstr        "${CURL_INCLUDES}" HAVE_STRSTR)
210    check_symbol_exists(strtok_r      "${CURL_INCLUDES}" HAVE_STRTOK_R)
211    check_symbol_exists(strftime      "${CURL_INCLUDES}" HAVE_STRFTIME)
212    check_symbol_exists(uname         "${CURL_INCLUDES}" HAVE_UNAME)
213    check_symbol_exists(strcasecmp    "${CURL_INCLUDES}" HAVE_STRCASECMP)
214    check_symbol_exists(stricmp       "${CURL_INCLUDES}" HAVE_STRICMP)
215    check_symbol_exists(strcmpi       "${CURL_INCLUDES}" HAVE_STRCMPI)
216    check_symbol_exists(strncmpi      "${CURL_INCLUDES}" HAVE_STRNCMPI)
217    check_symbol_exists(alarm         "${CURL_INCLUDES}" HAVE_ALARM)
218    if(NOT HAVE_STRNCMPI)
219      set(HAVE_STRCMPI)
220    endif(NOT HAVE_STRNCMPI)
221
222    check_symbol_exists(gethostbyaddr "${CURL_INCLUDES}" HAVE_GETHOSTBYADDR)
223    check_symbol_exists(gethostbyaddr_r "${CURL_INCLUDES}" HAVE_GETHOSTBYADDR_R)
224    check_symbol_exists(gettimeofday  "${CURL_INCLUDES}" HAVE_GETTIMEOFDAY)
225    check_symbol_exists(inet_addr     "${CURL_INCLUDES}" HAVE_INET_ADDR)
226    check_symbol_exists(inet_ntoa     "${CURL_INCLUDES}" HAVE_INET_NTOA)
227    check_symbol_exists(inet_ntoa_r   "${CURL_INCLUDES}" HAVE_INET_NTOA_R)
228    check_symbol_exists(tcsetattr     "${CURL_INCLUDES}" HAVE_TCSETATTR)
229    check_symbol_exists(tcgetattr     "${CURL_INCLUDES}" HAVE_TCGETATTR)
230    check_symbol_exists(perror        "${CURL_INCLUDES}" HAVE_PERROR)
231    check_symbol_exists(closesocket   "${CURL_INCLUDES}" HAVE_CLOSESOCKET)
232    check_symbol_exists(setvbuf       "${CURL_INCLUDES}" HAVE_SETVBUF)
233    check_symbol_exists(sigsetjmp     "${CURL_INCLUDES}" HAVE_SIGSETJMP)
234    check_symbol_exists(getpass_r     "${CURL_INCLUDES}" HAVE_GETPASS_R)
235    check_symbol_exists(strlcat       "${CURL_INCLUDES}" HAVE_STRLCAT)
236    check_symbol_exists(getpwuid      "${CURL_INCLUDES}" HAVE_GETPWUID)
237    check_symbol_exists(geteuid       "${CURL_INCLUDES}" HAVE_GETEUID)
238    check_symbol_exists(utime         "${CURL_INCLUDES}" HAVE_UTIME)
239    check_symbol_exists(gmtime_r      "${CURL_INCLUDES}" HAVE_GMTIME_R)
240    check_symbol_exists(localtime_r   "${CURL_INCLUDES}" HAVE_LOCALTIME_R)
241
242    check_symbol_exists(gethostbyname   "${CURL_INCLUDES}" HAVE_GETHOSTBYNAME)
243    check_symbol_exists(gethostbyname_r "${CURL_INCLUDES}" HAVE_GETHOSTBYNAME_R)
244
245    check_symbol_exists(signal        "${CURL_INCLUDES}" HAVE_SIGNAL_FUNC)
246    check_symbol_exists(SIGALRM       "${CURL_INCLUDES}" HAVE_SIGNAL_MACRO)
247    if(HAVE_SIGNAL_FUNC AND HAVE_SIGNAL_MACRO)
248      set(HAVE_SIGNAL 1)
249    endif(HAVE_SIGNAL_FUNC AND HAVE_SIGNAL_MACRO)
250    check_symbol_exists(uname          "${CURL_INCLUDES}" HAVE_UNAME)
251    check_symbol_exists(strtoll        "${CURL_INCLUDES}" HAVE_STRTOLL)
252    check_symbol_exists(_strtoi64      "${CURL_INCLUDES}" HAVE__STRTOI64)
253    check_symbol_exists(strerror_r     "${CURL_INCLUDES}" HAVE_STRERROR_R)
254    check_symbol_exists(siginterrupt   "${CURL_INCLUDES}" HAVE_SIGINTERRUPT)
255    check_symbol_exists(perror         "${CURL_INCLUDES}" HAVE_PERROR)
256    check_symbol_exists(fork           "${CURL_INCLUDES}" HAVE_FORK)
257    check_symbol_exists(getaddrinfo    "${CURL_INCLUDES}" HAVE_GETADDRINFO)
258    check_symbol_exists(freeaddrinfo   "${CURL_INCLUDES}" HAVE_FREEADDRINFO)
259    check_symbol_exists(freeifaddrs    "${CURL_INCLUDES}" HAVE_FREEIFADDRS)
260    check_symbol_exists(pipe           "${CURL_INCLUDES}" HAVE_PIPE)
261    check_symbol_exists(ftruncate      "${CURL_INCLUDES}" HAVE_FTRUNCATE)
262    check_symbol_exists(getprotobyname "${CURL_INCLUDES}" HAVE_GETPROTOBYNAME)
263    check_symbol_exists(getrlimit      "${CURL_INCLUDES}" HAVE_GETRLIMIT)
264    check_symbol_exists(setlocale      "${CURL_INCLUDES}" HAVE_SETLOCALE)
265    check_symbol_exists(setmode        "${CURL_INCLUDES}" HAVE_SETMODE)
266    check_symbol_exists(setrlimit      "${CURL_INCLUDES}" HAVE_SETRLIMIT)
267    check_symbol_exists(fcntl          "${CURL_INCLUDES}" HAVE_FCNTL)
268    check_symbol_exists(ioctl          "${CURL_INCLUDES}" HAVE_IOCTL)
269    check_symbol_exists(setsockopt     "${CURL_INCLUDES}" HAVE_SETSOCKOPT)
270
271    if(HAVE_SIZEOF_LONG_LONG)
272      set(HAVE_LONGLONG 1)
273      set(HAVE_LL 1)
274    endif(HAVE_SIZEOF_LONG_LONG)
275
276    check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
277    check_function_exists(gethostname HAVE_GETHOSTNAME)
278
279    check_include_file_concat("pthread.h" HAVE_PTHREAD_H)
280    check_symbol_exists(recv "sys/socket.h" HAVE_RECV)
281    check_symbol_exists(send "sys/socket.h" HAVE_SEND)
282
283    check_struct_has_member("struct sockaddr_un" sun_path "sys/un.h" USE_UNIX_SOCKETS)
284
285    list(APPEND CMAKE_REQUIRED_INCLUDES "${CURL_SOURCES_DIR}/include")
286    set(CMAKE_EXTRA_INCLUDE_FILES "curl/system.h")
287    check_type_size("curl_off_t"  SIZEOF_CURL_OFF_T)
288
289    add_definitions(-DHAVE_GLIBC_STRERROR_R=1)
290
291    include(${CURL_SOURCES_DIR}/CMake/OtherTests.cmake)
292
293    foreach(CURL_TEST
294        HAVE_FCNTL_O_NONBLOCK
295        HAVE_IOCTLSOCKET
296        HAVE_IOCTLSOCKET_CAMEL
297        HAVE_IOCTLSOCKET_CAMEL_FIONBIO
298        HAVE_IOCTLSOCKET_FIONBIO
299        HAVE_IOCTL_FIONBIO
300        HAVE_IOCTL_SIOCGIFADDR
301        HAVE_SETSOCKOPT_SO_NONBLOCK
302        HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
303        TIME_WITH_SYS_TIME
304        HAVE_O_NONBLOCK
305        HAVE_GETHOSTBYADDR_R_5
306        HAVE_GETHOSTBYADDR_R_7
307        HAVE_GETHOSTBYADDR_R_8
308        HAVE_GETHOSTBYADDR_R_5_REENTRANT
309        HAVE_GETHOSTBYADDR_R_7_REENTRANT
310        HAVE_GETHOSTBYADDR_R_8_REENTRANT
311        HAVE_GETHOSTBYNAME_R_3
312        HAVE_GETHOSTBYNAME_R_5
313        HAVE_GETHOSTBYNAME_R_6
314        HAVE_GETHOSTBYNAME_R_3_REENTRANT
315        HAVE_GETHOSTBYNAME_R_5_REENTRANT
316        HAVE_GETHOSTBYNAME_R_6_REENTRANT
317        HAVE_SOCKLEN_T
318        HAVE_IN_ADDR_T
319        HAVE_BOOL_T
320        STDC_HEADERS
321        RETSIGTYPE_TEST
322        HAVE_INET_NTOA_R_DECL
323        HAVE_INET_NTOA_R_DECL_REENTRANT
324        HAVE_GETADDRINFO
325        HAVE_FILE_OFFSET_BITS
326        )
327      curl_internal_test(${CURL_TEST})
328    endforeach(CURL_TEST)
329
330    configure_file(
331      ${CURL_SOURCES_DIR}/lib/curl_config.h.cmake
332      ${CURL_SOURCES_DIR}/lib/curl_config.h
333      )
334  endif()
335
336elseif (CMAKE_CROSSCOMPILING AND
337    "${CMAKE_SYSTEM_VERSION}" STREQUAL "CrossToolNg")
338
339  CHECK_INCLUDE_FILE_CXX(curl/curl.h HAVE_CURL_H)
340  if (NOT HAVE_CURL_H)
341    message(FATAL_ERROR "Please install the libcurl-dev package")
342  endif()
343
344  CHECK_LIBRARY_EXISTS(curl "curl_easy_init" "" HAVE_CURL_LIB)
345  if (NOT HAVE_CURL_LIB)
346    message(FATAL_ERROR "Please install the libcurl package")
347  endif()
348
349  link_libraries(curl)
350
351else()
352  include(FindCURL)
353  include_directories(${CURL_INCLUDE_DIRS})
354  link_libraries(${CURL_LIBRARIES})
355
356  if (NOT ${CURL_FOUND})
357    message(FATAL_ERROR "Unable to find LibCurl")
358  endif()
359endif()
360