FindDCMTK.cmake 11 KB

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