Merge topic 'cxx-checks-tolerate-unused-arguments'
[kiteware-cmake.git] / Modules / FindDCMTK.cmake
blob0154a059a46d6de0e4baa8547642947b7e8dec2e
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
5 FindDCMTK
6 ---------
8 Find DICOM ToolKit (DCMTK) libraries and applications
10 The module defines the following variables::
12  DCMTK_INCLUDE_DIRS  - Directories to include to use DCMTK
13  DCMTK_LIBRARIES     - Files to link against to use DCMTK
14  DCMTK_FOUND         - If false, don't try to use DCMTK
15  DCMTK_DIR           - (optional) Source directory for DCMTK
17 Compatibility
18 ^^^^^^^^^^^^^
20 This module is able to find a version of DCMTK that does or does not export
21 a ``DCMTKConfig.cmake`` file. It applies a two step process:
23 * Step 1:  Attempt to find DCMTK version providing a ``DCMTKConfig.cmake`` file.
24 * Step 2:  If step 1 failed, rely on ``FindDCMTK.cmake`` to set ``DCMTK_*``
25   variables details below.
28 `Recent DCMTK
29 <https://git.dcmtk.org/?p=dcmtk.git;a=commit;h=662ae187c493c6b9a73dd5e3875372cebd0c11fe>`_
30 provides a ``DCMTKConfig.cmake`` :manual:`package configuration file
31 <cmake-packages(7)>`. To exclusively use the package configuration file
32 (recommended when possible), pass the `NO_MODULE` option to
33 :command:`find_package`. For example, `find_package(DCMTK NO_MODULE)`.
34 This requires official DCMTK snapshot *3.6.1_20140617* or newer.
37 Until all clients update to the more recent DCMTK, build systems will need
38 to support different versions of DCMTK.
40 On any given system, the following combinations of DCMTK versions could be
41 considered:
43 +--------+---------------------+-----------------------+-------------------+
44 |        |   SYSTEM DCMTK      |      LOCAL DCMTK      |     Supported ?   |
45 +--------+---------------------+-----------------------+-------------------+
46 | Case A |   NA                |      [ ] DCMTKConfig  |         YES       |
47 +--------+---------------------+-----------------------+-------------------+
48 | Case B |   NA                |      [X] DCMTKConfig  |         YES       |
49 +--------+---------------------+-----------------------+-------------------+
50 | Case C |   [ ] DCMTKConfig   |      NA               |         YES       |
51 +--------+---------------------+-----------------------+-------------------+
52 | Case D |   [X] DCMTKConfig   |      NA               |         YES       |
53 +--------+---------------------+-----------------------+-------------------+
54 | Case E |   [ ] DCMTKConfig   |      [ ] DCMTKConfig  |         YES (*)   |
55 +--------+---------------------+-----------------------+-------------------+
56 | Case F |   [X] DCMTKConfig   |      [ ] DCMTKConfig  |         NO        |
57 +--------+---------------------+-----------------------+-------------------+
58 | Case G |   [ ] DCMTKConfig   |      [X] DCMTKConfig  |         YES       |
59 +--------+---------------------+-----------------------+-------------------+
60 | Case H |   [X] DCMTKConfig   |      [X] DCMTKConfig  |         YES       |
61 +--------+---------------------+-----------------------+-------------------+
63  (*) See Troubleshooting section.
65 Legend:
67   NA ...............: Means that no System or Local DCMTK is available
69   [ ] DCMTKConfig ..: Means that the version of DCMTK does NOT export a DCMTKConfig.cmake file.
71   [X] DCMTKConfig ..: Means that the version of DCMTK exports a DCMTKConfig.cmake file.
74 Troubleshooting
75 ^^^^^^^^^^^^^^^
77 What to do if my project finds a different version of DCMTK?
79 Remove DCMTK entry from the CMake cache per :command:`find_package`
80 documentation.
81 #]=======================================================================]
84 # Written for VXL by Amitha Perera.
85 # Upgraded for GDCM by Mathieu Malaterre.
86 # Modified for EasyViz by Thomas Sondergaard.
89 set(_dcmtk_dir_description "The directory of DCMTK build or install tree.")
91 # Ensure that DCMTK_DIR is set to a reasonable default value
92 # so that DCMTK libraries can be found on a standard Unix distribution.
93 # It also overwrite the value of DCMTK_DIR after this one has been
94 # set by a successful discovery of DCMTK by the unpatched FindDCMTK.cmake module
95 # distributed with CMake (as of 0167cea)
96 if(NOT DCMTK_DIR OR DCMTK_DIR STREQUAL "/usr/include/dcmtk")
97   set(DCMTK_DIR "/usr" CACHE PATH ${_dcmtk_dir_description} FORCE)
98 endif()
100 set(_SAVED_DCMTK_DIR ${DCMTK_DIR})
103 # Step1: Attempt to find a version of DCMTK providing a DCMTKConfig.cmake file.
105 if(NOT DCMTK_FIND_QUIETLY)
106   message(CHECK_START "Trying to find DCMTK expecting DCMTKConfig.cmake")
107 endif()
108 find_package(DCMTK QUIET NO_MODULE)
109 if(DCMTK_FOUND
110     AND NOT "x" STREQUAL "x${DCMTK_LIBRARIES}"
111     AND NOT "x" STREQUAL "x${DCMTK_INCLUDE_DIRS}")
113   if(NOT DCMTK_FIND_QUIETLY)
114     message(CHECK_PASS "ok")
115   endif()
116   return()
117 else()
118   if(NOT DCMTK_FIND_QUIETLY)
119     message(CHECK_FAIL "failed")
120   endif()
121 endif()
123 if(NOT DCMTK_FIND_QUIETLY)
124   message(STATUS "Trying to find DCMTK relying on FindDCMTK.cmake")
125 endif()
127 # Restore the value reset by the previous call to 'find_package(DCMTK QUIET NO_MODULE)'
128 set(DCMTK_DIR ${_SAVED_DCMTK_DIR} CACHE PATH ${_dcmtk_dir_description} FORCE)
132 # Step2: Attempt to find a version of DCMTK that does NOT provide a DCMTKConfig.cmake file.
135 # prefer DCMTK_DIR over default system paths like /usr/lib
136 if(DCMTK_DIR)
137   set(CMAKE_PREFIX_PATH ${DCMTK_DIR}/lib ${CMAKE_PREFIX_PATH}) # this is given to FIND_LIBRARY or FIND_PATH
138 endif()
140 # Find all libraries, store debug and release separately
141 foreach(lib
142     dcmpstat
143     dcmsr
144     dcmsign
145     dcmtls
146     dcmqrdb
147     dcmnet
148     dcmjpeg
149     dcmimage
150     dcmimgle
151     dcmdata
152     oflog
153     ofstd
154     ijg12
155     ijg16
156     ijg8
157     )
159   # Find Release libraries
160   find_library(DCMTK_${lib}_LIBRARY_RELEASE
161     ${lib}
162     PATHS
163     ${DCMTK_DIR}/${lib}/libsrc
164     ${DCMTK_DIR}/${lib}/libsrc/Release
165     ${DCMTK_DIR}/${lib}/Release
166     ${DCMTK_DIR}/lib
167     ${DCMTK_DIR}/lib/Release
168     ${DCMTK_DIR}/dcmjpeg/lib${lib}/Release
169     NO_DEFAULT_PATH
170     )
172   # Find Debug libraries
173   find_library(DCMTK_${lib}_LIBRARY_DEBUG
174     ${lib}${DCMTK_CMAKE_DEBUG_POSTFIX}
175     PATHS
176     ${DCMTK_DIR}/${lib}/libsrc
177     ${DCMTK_DIR}/${lib}/libsrc/Debug
178     ${DCMTK_DIR}/${lib}/Debug
179     ${DCMTK_DIR}/lib
180     ${DCMTK_DIR}/lib/Debug
181     ${DCMTK_DIR}/dcmjpeg/lib${lib}/Debug
182     NO_DEFAULT_PATH
183     )
185   mark_as_advanced(DCMTK_${lib}_LIBRARY_RELEASE)
186   mark_as_advanced(DCMTK_${lib}_LIBRARY_DEBUG)
188   # Add libraries to variable according to build type
189   if(DCMTK_${lib}_LIBRARY_RELEASE)
190     list(APPEND DCMTK_LIBRARIES optimized ${DCMTK_${lib}_LIBRARY_RELEASE})
191   endif()
193   if(DCMTK_${lib}_LIBRARY_DEBUG)
194     list(APPEND DCMTK_LIBRARIES debug ${DCMTK_${lib}_LIBRARY_DEBUG})
195   endif()
197 endforeach()
199 set(CMAKE_THREAD_LIBS_INIT)
200 if(DCMTK_oflog_LIBRARY_RELEASE OR DCMTK_oflog_LIBRARY_DEBUG)
201   # Hack - Not having a DCMTKConfig.cmake file to read the settings from, we will attempt to
202   # find the library in all cases.
203   # Ideally, pthread library should be discovered only if DCMTK_WITH_THREADS is enabled.
204   find_package(Threads)
205 endif()
207 if(CMAKE_THREAD_LIBS_INIT)
208   list(APPEND DCMTK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
209 endif()
212 # SPECIFIC CASE FOR DCMTK BUILD DIR as DCMTK_DIR
213 # (as opposed to a DCMTK install dir)
214 # Have to find the source directory.
215 if(EXISTS ${DCMTK_DIR}/CMakeCache.txt)
216           load_cache(${DCMTK_DIR} READ_WITH_PREFIX "EXT"
217           DCMTK_SOURCE_DIR)
218   if(NOT EXISTS ${EXTDCMTK_SOURCE_DIR})
219     message(FATAL_ERROR
220       "DCMTK build directory references
221 nonexistent DCMTK source directory ${EXTDCMTK_SOURCE_DIR}")
222   endif()
223 endif()
225 set(DCMTK_config_TEST_HEADER osconfig.h)
226 set(DCMTK_dcmdata_TEST_HEADER dctypes.h)
227 set(DCMTK_dcmimage_TEST_HEADER dicoimg.h)
228 set(DCMTK_dcmimgle_TEST_HEADER dcmimage.h)
229 set(DCMTK_dcmjpeg_TEST_HEADER djdecode.h)
230 set(DCMTK_dcmnet_TEST_HEADER assoc.h)
231 set(DCMTK_dcmpstat_TEST_HEADER dcmpstat.h)
232 set(DCMTK_dcmqrdb_TEST_HEADER dcmqrdba.h)
233 set(DCMTK_dcmsign_TEST_HEADER sicert.h)
234 set(DCMTK_dcmsr_TEST_HEADER dsrtree.h)
235 set(DCMTK_dcmtls_TEST_HEADER tlslayer.h)
236 set(DCMTK_ofstd_TEST_HEADER ofstdinc.h)
237 set(DCMTK_oflog_TEST_HEADER oflog.h)
238 set(DCMTK_dcmjpls_TEST_HEADER djlsutil.h)
240 set(DCMTK_INCLUDE_DIR_NAMES)
242 foreach(dir
243     config
244     dcmdata
245     dcmimage
246     dcmimgle
247     dcmjpeg
248     dcmjpls
249     dcmnet
250     dcmpstat
251     dcmqrdb
252     dcmsign
253     dcmsr
254     dcmtls
255     ofstd
256     oflog)
257   if(EXTDCMTK_SOURCE_DIR)
258     set(SOURCE_DIR_PATH
259       ${EXTDCMTK_SOURCE_DIR}/${dir}/include/dcmtk/${dir})
260   endif()
261   find_path(DCMTK_${dir}_INCLUDE_DIR
262     ${DCMTK_${dir}_TEST_HEADER}
263     PATHS
264     ${DCMTK_DIR}/${dir}/include
265     ${DCMTK_DIR}/${dir}
266     ${DCMTK_DIR}/include/dcmtk/${dir}
267     ${DCMTK_DIR}/${dir}/include/dcmtk/${dir}
268     ${DCMTK_DIR}/include/${dir}
269     ${SOURCE_DIR_PATH}
270     )
271   mark_as_advanced(DCMTK_${dir}_INCLUDE_DIR)
272   list(APPEND DCMTK_INCLUDE_DIR_NAMES DCMTK_${dir}_INCLUDE_DIR)
274   if(DCMTK_${dir}_INCLUDE_DIR)
275     # add the 'include' path so eg
276     #include "dcmtk/dcmimgle/dcmimage.h"
277     # works
278     get_filename_component(_include ${DCMTK_${dir}_INCLUDE_DIR} PATH)
279     get_filename_component(_include ${_include} PATH)
280     list(APPEND
281       DCMTK_INCLUDE_DIRS
282       ${DCMTK_${dir}_INCLUDE_DIR}
283       ${_include})
284   endif()
285 endforeach()
287 list(APPEND DCMTK_INCLUDE_DIRS ${DCMTK_DIR}/include)
289 if(WIN32)
290   list(APPEND DCMTK_LIBRARIES netapi32 wsock32)
291 endif()
293 if(DCMTK_ofstd_INCLUDE_DIR)
294   get_filename_component(DCMTK_dcmtk_INCLUDE_DIR
295     ${DCMTK_ofstd_INCLUDE_DIR}
296     PATH
297     CACHE)
298   list(APPEND DCMTK_INCLUDE_DIRS ${DCMTK_dcmtk_INCLUDE_DIR})
299   mark_as_advanced(DCMTK_dcmtk_INCLUDE_DIR)
300 endif()
302 # Compatibility: This variable is deprecated
303 set(DCMTK_INCLUDE_DIR ${DCMTK_INCLUDE_DIRS})
305 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
306 find_package_handle_standard_args(DCMTK
307   REQUIRED_VARS ${DCMTK_INCLUDE_DIR_NAMES} DCMTK_LIBRARIES
308   FAIL_MESSAGE "Please set DCMTK_DIR and re-run configure")
310 # Workaround bug in packaging of DCMTK 3.6.0 on Debian.
311 # See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=637687
312 if(DCMTK_FOUND AND UNIX AND NOT APPLE)
313   include(${CMAKE_CURRENT_LIST_DIR}/CheckIncludeFiles.cmake)
314   set(CMAKE_REQUIRED_FLAGS )
315   set(CMAKE_REQUIRED_DEFINITIONS )
316   set(CMAKE_REQUIRED_INCLUDES ${DCMTK_INCLUDE_DIRS})
317   set(CMAKE_REQUIRED_LIBRARIES ${DCMTK_LIBRARIES})
318   set(CMAKE_REQUIRED_QUIET ${DCMTK_FIND_QUIETLY})
319   check_include_files("dcmtk/config/osconfig.h;dcmtk/ofstd/ofstream.h" DCMTK_HAVE_CONFIG_H_OPTIONAL LANGUAGE CXX)
320   if(NOT DCMTK_HAVE_CONFIG_H_OPTIONAL)
321     set(DCMTK_DEFINITIONS "HAVE_CONFIG_H")
322   endif()
323 endif()