FindDCMTK.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # - find DCMTK libraries and applications
  2. # The module defines the following variables:
  3. #
  4. # DCMTK_INCLUDE_DIRS - Directories to include to use DCMTK
  5. # DCMTK_LIBRARIES - Files to link against to use DCMTK
  6. # DCMTK_FOUND - If false, don't try to use DCMTK
  7. # DCMTK_DIR - (optional) Source directory for DCMTK
  8. #
  9. # DCMTK_DIR can be used to make it simpler to find the various include
  10. # directories and compiled libraries if you've just compiled it in the
  11. # source tree. Just set it to the root of the tree where you extracted
  12. # the source (default to /usr/include/dcmtk/)
  13. #
  14. #
  15. # This file is able to find version of DCMTK that do or do not export
  16. # a DCMTKConfig.cmake file.
  17. #
  18. # IMPORTANT: A set of patches has been contributed to DCMTK
  19. # maintainers, as soon as:
  20. # (1) it has been integrated upstream so that it is available in
  21. # an official release (for example X.Y.Z),
  22. # (2) code bases have been updated to build against X.Y.Z
  23. # This file could be removed.
  24. #
  25. # The set of patches is listed here: https://github.com/commontk/DCMTK/compare/79030ba...f461865
  26. #
  27. #
  28. # Waiting for this to happen, build systems will have to be able
  29. # to support different versions of DCMTK.
  30. #
  31. # On any given system, the following combination of DCMTK version could be
  32. # considered:
  33. #
  34. # | SYSTEM DCMTK + LOCAL DCMTK | Supported ? |
  35. # ---------------------------------------------------------------------------
  36. # Case A | NA + [ ] DCMTKConfig | YES |
  37. # ------------------------------------------------------|--------------------
  38. # Case B | NA + [X] DCMTKConfig | YES |
  39. # ------------------------------------------------------|--------------------
  40. # Case C | [ ] DCMTKConfig + NA | YES |
  41. # ------------------------------------------------------|--------------------
  42. # Case D | [X] DCMTKConfig + NA | YES |
  43. # ------------------------------------------------------|--------------------
  44. # Case E | [ ] DCMTKConfig + [ ] DCMTKConfig | YES (*) |
  45. # ------------------------------------------------------|--------------------
  46. # Case F | [X] DCMTKConfig + [ ] DCMTKConfig | NO |
  47. # ------------------------------------------------------|--------------------
  48. # Case G | [ ] DCMTKConfig + [X] DCMTKConfig | YES |
  49. # ------------------------------------------------------|--------------------
  50. # Case H | [X] DCMTKConfig + [X] DCMTKConfig | YES |
  51. # ---------------------------------------------------------------------------
  52. #
  53. # (*) See Troubleshooting section.
  54. #
  55. # Legend:
  56. #
  57. # NA ...............: Means that no System or Local DCMTK is available
  58. #
  59. # [ ] DCMTKConfig ..: Means that the version of DCMTK does NOT export a DCMTKConfig.cmake file.
  60. #
  61. # [X] DCMTKConfig ..: Means that the version of DCMTK exports a DCMTKConfig.cmake file.
  62. #
  63. #
  64. #
  65. # In CTK commits 52d953 and 74b4b07, the FindDCMTK.cmake module has been updated to be able
  66. # to find both version of DCMTK ([ ] DCMTKConfig and [X] DCMTKConfig).
  67. #
  68. # It is a two steps process:
  69. #
  70. # * Step 1: Attempt to find DCMTK version providing a DCMTKConfig.cmake file. This is done
  71. # with the help of ''
  72. #
  73. # * Step 2: If step 1 failed, rely on FindDCMTK.cmake to set DCMTK_* variables details below.
  74. #
  75. #
  76. #
  77. # Troubleshooting:
  78. #
  79. # What to do if my project find a different version of DCMTK ?
  80. #
  81. # Remove DCMTK entry from the CMake registry.
  82. # Search for "CMake user package registry" on http://www.cmake.org/cmake/help/v2.8.9/cmake.html#command:find_package
  83. #
  84. # Details: As some point, the DCMTK build tree was exported [1][2][3]. Indeed, before the set of
  85. # patches discussed above has been contributed to upstream DCMTK, an initial implementation of
  86. # a DCMTK build system exporting its build tree has been implemented and has been tested by some
  87. # folks. This had the effect of populating the CMake registry.
  88. #
  89. # Since Step1 does not exclude the CMake registry, when dealing with case E, the incorrect version of
  90. # DCMTK could be found.
  91. #
  92. # [1] http://slicer-devel.65872.n3.nabble.com/Packaging-seems-to-work-again-tp4028121p4028134.html
  93. # [2] https://www.assembla.com/spaces/slicerrt/tickets/244-dcmtk_dir-vs--dcmtkconfig-cmake?comment=267984263#comment:267984263
  94. # [3] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:export
  95. #
  96. #
  97. #=============================================================================
  98. # Copyright 2004-2009 Kitware, Inc.
  99. # Copyright 2009-2010 Mathieu Malaterre <mathieu.malaterre@gmail.com>
  100. # Copyright 2010 Thomas Sondergaard <ts@medical-insight.com>
  101. #
  102. # Distributed under the OSI-approved BSD License (the "License");
  103. # see accompanying file Copyright.txt for details.
  104. #
  105. # This software is distributed WITHOUT ANY WARRANTY; without even the
  106. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  107. # See the License for more information.
  108. #=============================================================================
  109. # (To distributed this file outside of CMake, substitute the full
  110. # License text for the above reference.)
  111. #
  112. # Written for VXL by Amitha Perera.
  113. # Upgraded for GDCM by Mathieu Malaterre.
  114. # Modified for EasyViz by Thomas Sondergaard.
  115. #
  116. set(_SAVED_DCMTK_DIR ${DCMTK_DIR})
  117. #
  118. # Step1: Attempt to find a version of DCMTK providing a DCMTKConfig.cmake file.
  119. #
  120. message(STATUS "Trying to find DCMTK expecting DCMTKConfig.cmake")
  121. find_package(DCMTK QUIET NO_MODULE)
  122. if(DCMTK_FOUND
  123. AND NOT "x" STREQUAL "x${DCMTK_LIBRARIES}"
  124. AND NOT "x" STREQUAL "x${DCMTK_INCLUDE_DIRS}")
  125. message(STATUS "Trying to find DCMTK expecting DCMTKConfig.cmake - ok")
  126. return()
  127. else()
  128. message(STATUS "Trying to find DCMTK expecting DCMTKConfig.cmake - failed")
  129. endif()
  130. message(STATUS "Trying to find DCMTK relying on FindDCMTK.cmake")
  131. # Restore the value reset by the previous call to 'find_package(DCMTK QUIET NO_MODULE)'
  132. set(DCMTK_DIR ${_SAVED_DCMTK_DIR} CACHE PATH "The directory containing a CMake configuration file for DCMTK." FORCE)
  133. #
  134. # Step2: Attempt to find a version of DCMTK that does NOT provide a DCMTKConfig.cmake file.
  135. #
  136. # prefer DCMTK_DIR over default system paths like /usr/lib
  137. if(DCMTK_DIR)
  138. set(CMAKE_PREFIX_PATH ${DCMTK_DIR}/lib ${CMAKE_PREFIX_PATH}) # this is given to FIND_LIBRARY or FIND_PATH
  139. 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. )
  158. # Find Release libraries
  159. find_library(DCMTK_${lib}_LIBRARY_RELEASE
  160. ${lib}
  161. PATHS
  162. ${DCMTK_DIR}/${lib}/libsrc
  163. ${DCMTK_DIR}/${lib}/libsrc/Release
  164. ${DCMTK_DIR}/${lib}/Release
  165. ${DCMTK_DIR}/lib
  166. ${DCMTK_DIR}/lib/Release
  167. ${DCMTK_DIR}/dcmjpeg/lib${lib}/Release
  168. NO_DEFAULT_PATH
  169. )
  170. # Find Debug libraries
  171. find_library(DCMTK_${lib}_LIBRARY_DEBUG
  172. ${lib}
  173. PATHS
  174. ${DCMTK_DIR}/${lib}/libsrc
  175. ${DCMTK_DIR}/${lib}/libsrc/Debug
  176. ${DCMTK_DIR}/${lib}/Debug
  177. ${DCMTK_DIR}/lib
  178. ${DCMTK_DIR}/lib/Debug
  179. ${DCMTK_DIR}/dcmjpeg/lib${lib}/Debug
  180. NO_DEFAULT_PATH
  181. )
  182. mark_as_advanced(DCMTK_${lib}_LIBRARY_RELEASE)
  183. mark_as_advanced(DCMTK_${lib}_LIBRARY_DEBUG)
  184. # Add libraries to variable according to build type
  185. if(DCMTK_${lib}_LIBRARY_RELEASE)
  186. list(APPEND DCMTK_LIBRARIES optimized ${DCMTK_${lib}_LIBRARY_RELEASE})
  187. endif()
  188. if(DCMTK_${lib}_LIBRARY_DEBUG)
  189. list(APPEND DCMTK_LIBRARIES debug ${DCMTK_${lib}_LIBRARY_DEBUG})
  190. endif()
  191. endforeach()
  192. set(CMAKE_THREAD_LIBS_INIT)
  193. if(DCMTK_oflog_LIBRARY_RELEASE OR DCMTK_oflog_LIBRARY_DEBUG)
  194. # Hack - Not having a DCMTKConfig.cmake file to read the settings from, we will attempt to
  195. # find the library in all cases.
  196. # Ideally, pthread library should be discovered only if DCMTK_WITH_THREADS is enabled.
  197. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  198. find_package(Threads)
  199. endif()
  200. if(CMAKE_THREAD_LIBS_INIT)
  201. list(APPEND DCMTK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  202. endif()
  203. #
  204. # SPECIFIC CASE FOR DCMTK BUILD DIR as DCMTK_DIR
  205. # (as opposed to a DCMTK install dir)
  206. # Have to find the source directory.
  207. if(EXISTS ${DCMTK_DIR}/CMakeCache.txt)
  208. load_cache(${DCMTK_DIR} READ_WITH_PREFIX "EXT"
  209. DCMTK_SOURCE_DIR)
  210. if(NOT EXISTS ${EXTDCMTK_SOURCE_DIR})
  211. message(FATAL_ERROR
  212. "DCMTK build directory references
  213. nonexistant DCMTK source directory ${EXTDCMTK_SOURCE_DIR}")
  214. endif()
  215. endif()
  216. set(DCMTK_config_TEST_HEADER osconfig.h)
  217. set(DCMTK_dcmdata_TEST_HEADER dctypes.h)
  218. set(DCMTK_dcmimage_TEST_HEADER dicoimg.h)
  219. set(DCMTK_dcmimgle_TEST_HEADER dcmimage.h)
  220. set(DCMTK_dcmjpeg_TEST_HEADER djdecode.h)
  221. set(DCMTK_dcmnet_TEST_HEADER assoc.h)
  222. set(DCMTK_dcmpstat_TEST_HEADER dcmpstat.h)
  223. set(DCMTK_dcmqrdb_TEST_HEADER dcmqrdba.h)
  224. set(DCMTK_dcmsign_TEST_HEADER sicert.h)
  225. set(DCMTK_dcmsr_TEST_HEADER dsrtree.h)
  226. set(DCMTK_dcmtls_TEST_HEADER tlslayer.h)
  227. set(DCMTK_ofstd_TEST_HEADER ofstdinc.h)
  228. set(DCMTK_oflog_TEST_HEADER oflog.h)
  229. set(DCMTK_dcmjpls_TEST_HEADER djlsutil.h)
  230. set(DCMTK_INCLUDE_DIR_NAMES)
  231. foreach(dir
  232. config
  233. dcmdata
  234. dcmimage
  235. dcmimgle
  236. dcmjpeg
  237. dcmjpls
  238. dcmnet
  239. dcmpstat
  240. dcmqrdb
  241. dcmsign
  242. dcmsr
  243. dcmtls
  244. ofstd
  245. oflog)
  246. if(EXTDCMTK_SOURCE_DIR)
  247. set(SOURCE_DIR_PATH
  248. ${EXTDCMTK_SOURCE_DIR}/${dir}/include/dcmtk/${dir})
  249. endif()
  250. find_path(DCMTK_${dir}_INCLUDE_DIR
  251. ${DCMTK_${dir}_TEST_HEADER}
  252. PATHS
  253. ${DCMTK_DIR}/${dir}/include
  254. ${DCMTK_DIR}/${dir}
  255. ${DCMTK_DIR}/include/dcmtk/${dir}
  256. ${DCMTK_DIR}/${dir}/include/dcmtk/${dir}
  257. ${DCMTK_DIR}/include/${dir}
  258. ${SOURCE_DIR_PATH}
  259. )
  260. mark_as_advanced(DCMTK_${dir}_INCLUDE_DIR)
  261. list(APPEND DCMTK_INCLUDE_DIR_NAMES DCMTK_${dir}_INCLUDE_DIR)
  262. if(DCMTK_${dir}_INCLUDE_DIR)
  263. # add the 'include' path so eg
  264. #include "dcmtk/dcmimgle/dcmimage.h"
  265. # works
  266. get_filename_component(_include ${DCMTK_${dir}_INCLUDE_DIR} PATH)
  267. get_filename_component(_include ${_include} PATH)
  268. list(APPEND
  269. DCMTK_INCLUDE_DIRS
  270. ${DCMTK_${dir}_INCLUDE_DIR}
  271. ${_include})
  272. endif()
  273. endforeach()
  274. list(APPEND DCMTK_INCLUDE_DIRS ${DCMTK_DIR}/include)
  275. if(WIN32)
  276. list(APPEND DCMTK_LIBRARIES netapi32 wsock32)
  277. endif()
  278. if(DCMTK_ofstd_INCLUDE_DIR)
  279. get_filename_component(DCMTK_dcmtk_INCLUDE_DIR
  280. ${DCMTK_ofstd_INCLUDE_DIR}
  281. PATH
  282. CACHE)
  283. list(APPEND DCMTK_INCLUDE_DIRS ${DCMTK_dcmtk_INCLUDE_DIR})
  284. mark_as_advanced(DCMTK_dcmtk_INCLUDE_DIR)
  285. endif()
  286. # Compatibility: This variable is deprecated
  287. set(DCMTK_INCLUDE_DIR ${DCMTK_INCLUDE_DIRS})
  288. find_package_handle_standard_args(DCMTK
  289. REQUIRED_VARS ${DCMTK_INCLUDE_DIR_NAMES} DCMTK_LIBRARIES
  290. FAIL_MESSAGE "Please set DCMTK_DIR and re-run configure")
  291. message(STATUS "Trying to find DCMTK relying on FindDCMTK.cmake - ok")