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
21SET(DCMTK_VERSION_NUMBER 366)
22SET(DCMTK_PACKAGE_VERSION "3.6.6")
23SET(DCMTK_SOURCES_DIR ${CMAKE_BINARY_DIR}/dcmtk-3.6.6)
24SET(DCMTK_URL "http://orthanc.osimis.io/ThirdPartyDownloads/dcmtk-3.6.6.tar.gz")
25SET(DCMTK_MD5 "f815879d315b916366a9da71339c7575")
26
27macro(DCMTK_UNSET)
28endmacro()
29
30macro(DCMTK_UNSET_CACHE)
31endmacro()
32
33set(DCMTK_BINARY_DIR ${DCMTK_SOURCES_DIR}/)
34set(DCMTK_CMAKE_INCLUDE ${DCMTK_SOURCES_DIR}/)
35
36if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
37  set(DCMTK_WITH_THREADS OFF)  # Disable thread support in wasm/asm.js
38else()
39  set(DCMTK_WITH_THREADS ON)
40endif()
41
42add_definitions(-DDCMTK_INSIDE_LOG4CPLUS=1)
43
44if (IS_DIRECTORY "${DCMTK_SOURCES_DIR}")
45  set(FirstRun OFF)
46else()
47  set(FirstRun ON)
48endif()
49
50DownloadPackage(${DCMTK_MD5} ${DCMTK_URL} "${DCMTK_SOURCES_DIR}")
51
52
53if (FirstRun)
54  # Apply the patches
55  execute_process(
56    COMMAND ${PATCH_EXECUTABLE} -p0 -N -i
57    ${CMAKE_CURRENT_LIST_DIR}/../Patches/dcmtk-3.6.6.patch
58    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
59    RESULT_VARIABLE Failure
60    )
61
62  if (Failure)
63    message(FATAL_ERROR "Error while patching a file")
64  endif()
65
66  configure_file(
67    ${CMAKE_CURRENT_LIST_DIR}/../Patches/dcmtk-dcdict_orthanc.cc
68    ${DCMTK_SOURCES_DIR}/dcmdata/libsrc/dcdict_orthanc.cc
69    COPYONLY)
70else()
71  message("The patches for DCMTK have already been applied")
72endif()
73
74
75include_directories(
76  ${DCMTK_SOURCES_DIR}/dcmiod/include
77  )
78
79
80# C_CHAR_UNSIGNED *must* be set before calling "GenerateDCMTKConfigure.cmake"
81IF (CMAKE_CROSSCOMPILING)
82  if (CMAKE_COMPILER_IS_GNUCXX AND
83      CMAKE_SYSTEM_NAME STREQUAL "Windows")  # MinGW
84    SET(C_CHAR_UNSIGNED 1 CACHE INTERNAL "Whether char is unsigned.")
85
86  elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")  # WebAssembly or asm.js
87
88    # Check out "../WebAssembly/ArithmeticTests/" to regenerate the
89    # "arith.h" file
90    configure_file(
91      ${CMAKE_CURRENT_LIST_DIR}/WebAssembly/arith.h
92      ${DCMTK_SOURCES_DIR}/config/include/dcmtk/config/arith.h
93      COPYONLY)
94
95    UNSET(C_CHAR_UNSIGNED CACHE)
96    SET(C_CHAR_UNSIGNED 0 CACHE INTERNAL "")
97
98  else()
99    message(FATAL_ERROR "Support your platform here")
100  endif()
101ENDIF()
102
103
104if ("${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase")
105  SET(DCMTK_ENABLE_CHARSET_CONVERSION "iconv" CACHE STRING "")
106  SET(HAVE_SYS_GETTID 0 CACHE INTERNAL "")
107endif()
108
109
110SET(DCMTK_SOURCE_DIR ${DCMTK_SOURCES_DIR})
111include(${DCMTK_SOURCES_DIR}/CMake/CheckFunctionWithHeaderExists.cmake)
112include(${DCMTK_SOURCES_DIR}/CMake/GenerateDCMTKConfigure.cmake)
113
114
115if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")  # WebAssembly or
116  # asm.js The macros below are not properly discovered by DCMTK
117  # when using WebAssembly. Check out "../WebAssembly/arith.h" for
118  # how we produced these values. This step MUST be after
119  # "GenerateDCMTKConfigure" and before the generation of
120  # "osconfig.h".
121  UNSET(SIZEOF_VOID_P   CACHE)
122  UNSET(SIZEOF_CHAR     CACHE)
123  UNSET(SIZEOF_DOUBLE   CACHE)
124  UNSET(SIZEOF_FLOAT    CACHE)
125  UNSET(SIZEOF_INT      CACHE)
126  UNSET(SIZEOF_LONG     CACHE)
127  UNSET(SIZEOF_SHORT    CACHE)
128  UNSET(SIZEOF_VOID_P   CACHE)
129
130  SET(SIZEOF_VOID_P 4   CACHE INTERNAL "")
131  SET(SIZEOF_CHAR 1     CACHE INTERNAL "")
132  SET(SIZEOF_DOUBLE 8   CACHE INTERNAL "")
133  SET(SIZEOF_FLOAT 4    CACHE INTERNAL "")
134  SET(SIZEOF_INT 4      CACHE INTERNAL "")
135  SET(SIZEOF_LONG 4     CACHE INTERNAL "")
136  SET(SIZEOF_SHORT 2    CACHE INTERNAL "")
137  SET(SIZEOF_VOID_P 4   CACHE INTERNAL "")
138endif()
139
140
141set(DCMTK_PACKAGE_VERSION_SUFFIX "")
142set(DCMTK_PACKAGE_VERSION_NUMBER ${DCMTK_VERSION_NUMBER})
143
144CONFIGURE_FILE(
145  ${DCMTK_SOURCES_DIR}/CMake/osconfig.h.in
146  ${DCMTK_SOURCES_DIR}/config/include/dcmtk/config/osconfig.h)
147
148if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
149  link_libraries(netapi32)  # For NetWkstaUserGetInfo@12
150  link_libraries(iphlpapi)  # For GetAdaptersInfo@8
151
152  # Configure Wine if cross-compiling for Windows
153  if (CMAKE_COMPILER_IS_GNUCXX)
154    include(${DCMTK_SOURCES_DIR}/CMake/dcmtkUseWine.cmake)
155    FIND_PROGRAM(WINE_WINE_PROGRAM wine)
156    FIND_PROGRAM(WINE_WINEPATH_PROGRAM winepath)
157    list(APPEND DCMTK_TRY_COMPILE_REQUIRED_CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=-static")
158  endif()
159endif()
160
161# This step must be after the generation of "osconfig.h"
162if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
163  INSPECT_FUNDAMENTAL_ARITHMETIC_TYPES()
164endif()
165
166
167# Source for the logging facility of DCMTK
168AUX_SOURCE_DIRECTORY(${DCMTK_SOURCES_DIR}/oflog/libsrc DCMTK_SOURCES)
169if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR
170    ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR
171    ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" OR
172    ${CMAKE_SYSTEM_NAME} STREQUAL "kFreeBSD" OR
173    ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD" OR
174    ${CMAKE_SYSTEM_NAME} STREQUAL "Emscripten")
175  list(REMOVE_ITEM DCMTK_SOURCES
176    ${DCMTK_SOURCES_DIR}/oflog/libsrc/clfsap.cc
177    ${DCMTK_SOURCES_DIR}/oflog/libsrc/windebap.cc
178    ${DCMTK_SOURCES_DIR}/oflog/libsrc/winsock.cc
179    )
180
181elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
182  list(REMOVE_ITEM DCMTK_SOURCES
183    ${DCMTK_SOURCES_DIR}/oflog/libsrc/unixsock.cc
184    ${DCMTK_SOURCES_DIR}/oflog/libsrc/clfsap.cc
185    )
186endif()
187
188
189# Starting with DCMTK 3.6.2, the Nagle algorithm is not disabled by
190# default since this does not seem to be appropriate (anymore) for
191# most modern operating systems. In order to change this default, the
192# environment variable NO_TCPDELAY can be set to "1" (see envvars.txt
193# for details). Alternatively, the macro DISABLE_NAGLE_ALGORITHM can
194# be defined to change this setting at compilation time (see
195# macros.txt for details).
196# https://forum.dcmtk.org/viewtopic.php?t=4632
197add_definitions(
198  -DDISABLE_NAGLE_ALGORITHM=1
199  )
200
201
202if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
203  # For compatibility with Windows XP, avoid using fiber-local-storage
204  # in log4cplus, but use thread-local-storage instead. Otherwise,
205  # Windows XP complains about missing "FlsGetValue()" in KERNEL32.dll
206  add_definitions(
207    -DDCMTK_LOG4CPLUS_AVOID_WIN32_FLS
208    )
209
210  if (CMAKE_COMPILER_IS_GNUCXX OR             # MinGW
211      "${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")  # MSVC for 32bit (*)
212
213    # (*) With multithreaded logging enabled, Visual Studio 2008 fails
214    # with error: ".\dcmtk-3.6.6\oflog\libsrc\globinit.cc(422) : error
215    # C2664: 'dcmtk::log4cplus::thread::impl::tls_init' : cannot
216    # convert parameter 1 from 'void (__stdcall *)(void *)' to
217    # 'dcmtk::log4cplus::thread::impl::tls_init_cleanup_func_type'"
218    #   None of the functions with this name in scope match the target type
219
220    add_definitions(
221      -DDCMTK_LOG4CPLUS_SINGLE_THREADED
222      )
223  endif()
224endif()
225