FindDCMTK.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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)
  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(_dcmtk_dir_description "The directory of DCMTK build or install tree.")
  117. # Ensure that DCMTK_DIR is set to a reasonable default value
  118. # so that DCMTK libraries can be found on a standard Unix distribution.
  119. # It also overwrite the value of DCMTK_DIR after this one has been
  120. # set by a successful discovery of DCMTK by the unpatched FindDCMTK.cmake module
  121. # distributed with CMake (as of 0167cea)
  122. if(NOT DCMTK_DIR OR DCMTK_DIR STREQUAL "/usr/include/dcmtk")
  123. set(DCMTK_DIR "/usr" CACHE PATH ${_dcmtk_dir_description} FORCE)
  124. endif()
  125. set(_SAVED_DCMTK_DIR ${DCMTK_DIR})
  126. #
  127. # Step1: Attempt to find a version of DCMTK providing a DCMTKConfig.cmake file.
  128. #
  129. message(STATUS "Trying to find DCMTK expecting DCMTKConfig.cmake")
  130. find_package(DCMTK QUIET NO_MODULE)
  131. if(DCMTK_FOUND
  132. AND NOT "x" STREQUAL "x${DCMTK_LIBRARIES}"
  133. AND NOT "x" STREQUAL "x${DCMTK_INCLUDE_DIRS}")
  134. message(STATUS "Trying to find DCMTK expecting DCMTKConfig.cmake - ok")
  135. return()
  136. else()
  137. message(STATUS "Trying to find DCMTK expecting DCMTKConfig.cmake - failed")
  138. endif()
  139. message(STATUS "Trying to find DCMTK relying on FindDCMTK.cmake")
  140. # Restore the value reset by the previous call to 'find_package(DCMTK QUIET NO_MODULE)'
  141. set(DCMTK_DIR ${_SAVED_DCMTK_DIR} CACHE PATH ${_dcmtk_dir_description} FORCE)
  142. #
  143. # Step2: Attempt to find a version of DCMTK that does NOT provide a DCMTKConfig.cmake file.
  144. #
  145. # prefer DCMTK_DIR over default system paths like /usr/lib
  146. if(DCMTK_DIR)
  147. set(CMAKE_PREFIX_PATH ${DCMTK_DIR}/lib ${CMAKE_PREFIX_PATH}) # this is given to FIND_LIBRARY or FIND_PATH
  148. endif()
  149. # Find all libraries, store debug and release separately
  150. foreach(lib
  151. dcmpstat
  152. dcmsr
  153. dcmsign
  154. dcmtls
  155. dcmqrdb
  156. dcmnet
  157. dcmjpeg
  158. dcmimage
  159. dcmimgle
  160. dcmdata
  161. oflog
  162. ofstd
  163. ijg12
  164. ijg16
  165. ijg8
  166. )
  167. # Find Release libraries
  168. find_library(DCMTK_${lib}_LIBRARY_RELEASE
  169. ${lib}
  170. PATHS
  171. ${DCMTK_DIR}/${lib}/libsrc
  172. ${DCMTK_DIR}/${lib}/libsrc/Release
  173. ${DCMTK_DIR}/${lib}/Release
  174. ${DCMTK_DIR}/lib
  175. ${DCMTK_DIR}/lib/Release
  176. ${DCMTK_DIR}/dcmjpeg/lib${lib}/Release
  177. NO_DEFAULT_PATH
  178. )
  179. # Find Debug libraries
  180. find_library(DCMTK_${lib}_LIBRARY_DEBUG
  181. ${lib}${DCMTK_CMAKE_DEBUG_POSTFIX}
  182. PATHS
  183. ${DCMTK_DIR}/${lib}/libsrc
  184. ${DCMTK_DIR}/${lib}/libsrc/Debug
  185. ${DCMTK_DIR}/${lib}/Debug
  186. ${DCMTK_DIR}/lib
  187. ${DCMTK_DIR}/lib/Debug
  188. ${DCMTK_DIR}/dcmjpeg/lib${lib}/Debug
  189. NO_DEFAULT_PATH
  190. )
  191. mark_as_advanced(DCMTK_${lib}_LIBRARY_RELEASE)
  192. mark_as_advanced(DCMTK_${lib}_LIBRARY_DEBUG)
  193. # Add libraries to variable according to build type
  194. if(DCMTK_${lib}_LIBRARY_RELEASE)
  195. list(APPEND DCMTK_LIBRARIES optimized ${DCMTK_${lib}_LIBRARY_RELEASE})
  196. endif()
  197. if(DCMTK_${lib}_LIBRARY_DEBUG)
  198. list(APPEND DCMTK_LIBRARIES debug ${DCMTK_${lib}_LIBRARY_DEBUG})
  199. endif()
  200. endforeach()
  201. set(CMAKE_THREAD_LIBS_INIT)
  202. if(DCMTK_oflog_LIBRARY_RELEASE OR DCMTK_oflog_LIBRARY_DEBUG)
  203. # Hack - Not having a DCMTKConfig.cmake file to read the settings from, we will attempt to
  204. # find the library in all cases.
  205. # Ideally, pthread library should be discovered only if DCMTK_WITH_THREADS is enabled.
  206. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  207. find_package(Threads)
  208. endif()
  209. if(CMAKE_THREAD_LIBS_INIT)
  210. list(APPEND DCMTK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  211. endif()
  212. #
  213. # SPECIFIC CASE FOR DCMTK BUILD DIR as DCMTK_DIR
  214. # (as opposed to a DCMTK install dir)
  215. # Have to find the source directory.
  216. if(EXISTS ${DCMTK_DIR}/CMakeCache.txt)
  217. load_cache(${DCMTK_DIR} READ_WITH_PREFIX "EXT"
  218. DCMTK_SOURCE_DIR)
  219. if(NOT EXISTS ${EXTDCMTK_SOURCE_DIR})
  220. message(FATAL_ERROR
  221. "DCMTK build directory references
  222. nonexistant DCMTK source directory ${EXTDCMTK_SOURCE_DIR}")
  223. endif()
  224. 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)
  239. set(DCMTK_INCLUDE_DIR_NAMES)
  240. foreach(dir
  241. config
  242. dcmdata
  243. dcmimage
  244. dcmimgle
  245. dcmjpeg
  246. dcmjpls
  247. dcmnet
  248. dcmpstat
  249. dcmqrdb
  250. dcmsign
  251. dcmsr
  252. dcmtls
  253. ofstd
  254. oflog)
  255. if(EXTDCMTK_SOURCE_DIR)
  256. set(SOURCE_DIR_PATH
  257. ${EXTDCMTK_SOURCE_DIR}/${dir}/include/dcmtk/${dir})
  258. endif()
  259. find_path(DCMTK_${dir}_INCLUDE_DIR
  260. ${DCMTK_${dir}_TEST_HEADER}
  261. PATHS
  262. ${DCMTK_DIR}/${dir}/include
  263. ${DCMTK_DIR}/${dir}
  264. ${DCMTK_DIR}/include/dcmtk/${dir}
  265. ${DCMTK_DIR}/${dir}/include/dcmtk/${dir}
  266. ${DCMTK_DIR}/include/${dir}
  267. ${SOURCE_DIR_PATH}
  268. )
  269. mark_as_advanced(DCMTK_${dir}_INCLUDE_DIR)
  270. list(APPEND DCMTK_INCLUDE_DIR_NAMES DCMTK_${dir}_INCLUDE_DIR)
  271. if(DCMTK_${dir}_INCLUDE_DIR)
  272. # add the 'include' path so eg
  273. #include "dcmtk/dcmimgle/dcmimage.h"
  274. # works
  275. get_filename_component(_include ${DCMTK_${dir}_INCLUDE_DIR} PATH)
  276. get_filename_component(_include ${_include} PATH)
  277. list(APPEND
  278. DCMTK_INCLUDE_DIRS
  279. ${DCMTK_${dir}_INCLUDE_DIR}
  280. ${_include})
  281. endif()
  282. endforeach()
  283. list(APPEND DCMTK_INCLUDE_DIRS ${DCMTK_DIR}/include)
  284. if(WIN32)
  285. list(APPEND DCMTK_LIBRARIES netapi32 wsock32)
  286. endif()
  287. if(DCMTK_ofstd_INCLUDE_DIR)
  288. get_filename_component(DCMTK_dcmtk_INCLUDE_DIR
  289. ${DCMTK_ofstd_INCLUDE_DIR}
  290. PATH
  291. CACHE)
  292. list(APPEND DCMTK_INCLUDE_DIRS ${DCMTK_dcmtk_INCLUDE_DIR})
  293. mark_as_advanced(DCMTK_dcmtk_INCLUDE_DIR)
  294. endif()
  295. # Compatibility: This variable is deprecated
  296. set(DCMTK_INCLUDE_DIR ${DCMTK_INCLUDE_DIRS})
  297. include(FindPackageHandleStandardArgs)
  298. find_package_handle_standard_args(DCMTK
  299. REQUIRED_VARS ${DCMTK_INCLUDE_DIR_NAMES} DCMTK_LIBRARIES
  300. FAIL_MESSAGE "Please set DCMTK_DIR and re-run configure")
  301. # Workaround bug in packaging of DCMTK 3.6.0 on Debian.
  302. # See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=637687
  303. if(DCMTK_FOUND AND UNIX AND NOT APPLE)
  304. include(CheckCXXSourceCompiles)
  305. set(CMAKE_REQUIRED_FLAGS )
  306. set(CMAKE_REQUIRED_DEFINITIONS )
  307. set(CMAKE_REQUIRED_INCLUDES ${DCMTK_INCLUDE_DIRS})
  308. set(CMAKE_REQUIRED_LIBRARIES ${DCMTK_LIBRARIES})
  309. check_cxx_source_compiles("#include <dcmtk/config/osconfig.h>\n#include <dcmtk/ofstd/ofstream.h>\nint main(int,char*[]){return 0;}"
  310. DCMTK_HAVE_CONFIG_H_OPTIONAL
  311. )
  312. if(NOT DCMTK_HAVE_CONFIG_H_OPTIONAL)
  313. set(DCMTK_DEFINITIONS "HAVE_CONFIG_H")
  314. endif()
  315. endif()
  316. message(STATUS "Trying to find DCMTK relying on FindDCMTK.cmake - ok")