FindDCMTK.cmake 10 KB

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