FindHDF5.cmake 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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. FindHDF5
  5. --------
  6. Find Hierarchical Data Format (HDF5), a library for reading and writing
  7. self describing array data.
  8. This module invokes the ``HDF5`` wrapper compiler that should be installed
  9. alongside ``HDF5``. Depending upon the ``HDF5`` Configuration, the wrapper
  10. compiler is called either ``h5cc`` or ``h5pcc``. If this succeeds, the module
  11. will then call the compiler with the show argument to see what flags
  12. are used when compiling an ``HDF5`` client application.
  13. The module will optionally accept the ``COMPONENTS`` argument. If no
  14. ``COMPONENTS`` are specified, then the find module will default to finding
  15. only the ``HDF5`` C library. If one or more ``COMPONENTS`` are specified, the
  16. module will attempt to find the language bindings for the specified
  17. components. The valid components are ``C``, ``CXX``, ``Fortran``, ``HL``.
  18. ``HL`` refers to the "high-level" HDF5 functions for C and Fortran.
  19. If the ``COMPONENTS`` argument is not given, the module will
  20. attempt to find only the C bindings.
  21. For example, to use Fortran HDF5 and HDF5-HL functions, do:
  22. ``find_package(HDF5 COMPONENTS Fortran HL)``.
  23. This module will read the variable
  24. ``HDF5_USE_STATIC_LIBRARIES`` to determine whether or not to prefer a
  25. static link to a dynamic link for ``HDF5`` and all of it's dependencies.
  26. To use this feature, make sure that the ``HDF5_USE_STATIC_LIBRARIES``
  27. variable is set before the call to find_package.
  28. .. versionadded:: 3.10
  29. Support for ``HDF5_USE_STATIC_LIBRARIES`` on Windows.
  30. Both the serial and parallel ``HDF5`` wrappers are considered and the first
  31. directory to contain either one will be used. In the event that both appear
  32. in the same directory the serial version is preferentially selected. This
  33. behavior can be reversed by setting the variable ``HDF5_PREFER_PARALLEL`` to
  34. ``TRUE``.
  35. In addition to finding the includes and libraries required to compile
  36. an ``HDF5`` client application, this module also makes an effort to find
  37. tools that come with the ``HDF5`` distribution that may be useful for
  38. regression testing.
  39. Result Variables
  40. ^^^^^^^^^^^^^^^^
  41. This module will set the following variables in your project:
  42. ``HDF5_FOUND``
  43. HDF5 was found on the system
  44. ``HDF5_VERSION``
  45. .. versionadded:: 3.3
  46. HDF5 library version
  47. ``HDF5_INCLUDE_DIRS``
  48. Location of the HDF5 header files
  49. ``HDF5_DEFINITIONS``
  50. Required compiler definitions for HDF5
  51. ``HDF5_LIBRARIES``
  52. Required libraries for all requested bindings
  53. ``HDF5_HL_LIBRARIES``
  54. Required libraries for the HDF5 high level API for all bindings,
  55. if the ``HL`` component is enabled
  56. Available components are: ``C`` ``CXX`` ``Fortran`` and ``HL``.
  57. For each enabled language binding, a corresponding ``HDF5_${LANG}_LIBRARIES``
  58. variable, and potentially ``HDF5_${LANG}_DEFINITIONS``, will be defined.
  59. If the ``HL`` component is enabled, then an ``HDF5_${LANG}_HL_LIBRARIES`` will
  60. also be defined. With all components enabled, the following variables will be defined:
  61. ``HDF5_C_DEFINITIONS``
  62. Required compiler definitions for HDF5 C bindings
  63. ``HDF5_CXX_DEFINITIONS``
  64. Required compiler definitions for HDF5 C++ bindings
  65. ``HDF5_Fortran_DEFINITIONS``
  66. Required compiler definitions for HDF5 Fortran bindings
  67. ``HDF5_C_INCLUDE_DIRS``
  68. Required include directories for HDF5 C bindings
  69. ``HDF5_CXX_INCLUDE_DIRS``
  70. Required include directories for HDF5 C++ bindings
  71. ``HDF5_Fortran_INCLUDE_DIRS``
  72. Required include directories for HDF5 Fortran bindings
  73. ``HDF5_C_LIBRARIES``
  74. Required libraries for the HDF5 C bindings
  75. ``HDF5_CXX_LIBRARIES``
  76. Required libraries for the HDF5 C++ bindings
  77. ``HDF5_Fortran_LIBRARIES``
  78. Required libraries for the HDF5 Fortran bindings
  79. ``HDF5_C_HL_LIBRARIES``
  80. Required libraries for the high level C bindings
  81. ``HDF5_CXX_HL_LIBRARIES``
  82. Required libraries for the high level C++ bindings
  83. ``HDF5_Fortran_HL_LIBRARIES``
  84. Required libraries for the high level Fortran bindings.
  85. ``HDF5_IS_PARALLEL``
  86. HDF5 library has parallel IO support
  87. ``HDF5_C_COMPILER_EXECUTABLE``
  88. path to the HDF5 C wrapper compiler
  89. ``HDF5_CXX_COMPILER_EXECUTABLE``
  90. path to the HDF5 C++ wrapper compiler
  91. ``HDF5_Fortran_COMPILER_EXECUTABLE``
  92. path to the HDF5 Fortran wrapper compiler
  93. ``HDF5_C_COMPILER_EXECUTABLE_NO_INTERROGATE``
  94. path to the primary C compiler which is also the HDF5 wrapper
  95. ``HDF5_CXX_COMPILER_EXECUTABLE_NO_INTERROGATE``
  96. path to the primary C++ compiler which is also the HDF5 wrapper
  97. ``HDF5_Fortran_COMPILER_EXECUTABLE_NO_INTERROGATE``
  98. path to the primary Fortran compiler which is also the HDF5 wrapper
  99. ``HDF5_DIFF_EXECUTABLE``
  100. path to the HDF5 dataset comparison tool
  101. With all components enabled, the following targets will be defined:
  102. ``HDF5::HDF5``
  103. All detected ``HDF5_LIBRARIES``.
  104. ``hdf5::hdf5``
  105. C library.
  106. ``hdf5::hdf5_cpp``
  107. C++ library.
  108. ``hdf5::hdf5_fortran``
  109. Fortran library.
  110. ``hdf5::hdf5_hl``
  111. High-level C library.
  112. ``hdf5::hdf5_hl_cpp``
  113. High-level C++ library.
  114. ``hdf5::hdf5_hl_fortran``
  115. High-level Fortran library.
  116. ``hdf5::h5diff``
  117. ``h5diff`` executable.
  118. Hints
  119. ^^^^^
  120. The following variables can be set to guide the search for HDF5 libraries and includes:
  121. ``HDF5_PREFER_PARALLEL``
  122. .. versionadded:: 3.4
  123. set ``true`` to prefer parallel HDF5 (by default, serial is preferred)
  124. ``HDF5_FIND_DEBUG``
  125. .. versionadded:: 3.9
  126. Set ``true`` to get extra debugging output.
  127. ``HDF5_NO_FIND_PACKAGE_CONFIG_FILE``
  128. .. versionadded:: 3.8
  129. Set ``true`` to skip trying to find ``hdf5-config.cmake``.
  130. #]=======================================================================]
  131. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  132. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  133. cmake_policy(PUSH)
  134. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  135. # We haven't found HDF5 yet. Clear its state in case it is set in the parent
  136. # scope somewhere else. We can't rely on it because different components may
  137. # have been requested for this call.
  138. set(HDF5_FOUND OFF)
  139. set(HDF5_LIBRARIES)
  140. set(HDF5_HL_LIBRARIES)
  141. # List of the valid HDF5 components
  142. set(HDF5_VALID_LANGUAGE_BINDINGS C CXX Fortran)
  143. # Validate the list of find components.
  144. if(NOT HDF5_FIND_COMPONENTS)
  145. set(HDF5_LANGUAGE_BINDINGS "C")
  146. else()
  147. set(HDF5_LANGUAGE_BINDINGS)
  148. # add the extra specified components, ensuring that they are valid.
  149. set(HDF5_FIND_HL OFF)
  150. foreach(_component IN LISTS HDF5_FIND_COMPONENTS)
  151. list(FIND HDF5_VALID_LANGUAGE_BINDINGS ${_component} _component_location)
  152. if(NOT _component_location EQUAL -1)
  153. list(APPEND HDF5_LANGUAGE_BINDINGS ${_component})
  154. elseif(_component STREQUAL "HL")
  155. set(HDF5_FIND_HL ON)
  156. elseif(_component STREQUAL "Fortran_HL") # only for compatibility
  157. list(APPEND HDF5_LANGUAGE_BINDINGS Fortran)
  158. set(HDF5_FIND_HL ON)
  159. set(HDF5_FIND_REQUIRED_Fortran_HL FALSE)
  160. set(HDF5_FIND_REQUIRED_Fortran TRUE)
  161. set(HDF5_FIND_REQUIRED_HL TRUE)
  162. else()
  163. message(FATAL_ERROR "${_component} is not a valid HDF5 component.")
  164. endif()
  165. endforeach()
  166. unset(_component)
  167. unset(_component_location)
  168. if(NOT HDF5_LANGUAGE_BINDINGS)
  169. get_property(_langs GLOBAL PROPERTY ENABLED_LANGUAGES)
  170. foreach(_lang IN LISTS _langs)
  171. if(_lang MATCHES "^(C|CXX|Fortran)$")
  172. list(APPEND HDF5_LANGUAGE_BINDINGS ${_lang})
  173. endif()
  174. endforeach()
  175. endif()
  176. list(REMOVE_ITEM HDF5_FIND_COMPONENTS Fortran_HL) # replaced by Fortran and HL
  177. list(REMOVE_DUPLICATES HDF5_LANGUAGE_BINDINGS)
  178. endif()
  179. # Determine whether to search for serial or parallel executable first
  180. if(HDF5_PREFER_PARALLEL)
  181. set(HDF5_C_COMPILER_NAMES h5pcc h5cc)
  182. set(HDF5_CXX_COMPILER_NAMES h5pc++ h5c++)
  183. set(HDF5_Fortran_COMPILER_NAMES h5pfc h5fc)
  184. else()
  185. set(HDF5_C_COMPILER_NAMES h5cc h5pcc)
  186. set(HDF5_CXX_COMPILER_NAMES h5c++ h5pc++)
  187. set(HDF5_Fortran_COMPILER_NAMES h5fc h5pfc)
  188. endif()
  189. # Test first if the current compilers automatically wrap HDF5
  190. function(_HDF5_test_regular_compiler_C success version is_parallel)
  191. if(NOT ${success} OR
  192. NOT EXISTS ${_HDF5_TEST_DIR}/compiler_has_h5_c)
  193. file(WRITE "${_HDF5_TEST_DIR}/${_HDF5_TEST_SRC}"
  194. "#include <hdf5.h>\n"
  195. "const char* info_ver = \"INFO\" \":\" H5_VERSION;\n"
  196. "#ifdef H5_HAVE_PARALLEL\n"
  197. "const char* info_parallel = \"INFO\" \":\" \"PARALLEL\";\n"
  198. "#endif\n"
  199. "int main(int argc, char **argv) {\n"
  200. " int require = 0;\n"
  201. " require += info_ver[argc];\n"
  202. "#ifdef H5_HAVE_PARALLEL\n"
  203. " require += info_parallel[argc];\n"
  204. "#endif\n"
  205. " hid_t fid;\n"
  206. " fid = H5Fcreate(\"foo.h5\",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);\n"
  207. " return 0;\n"
  208. "}")
  209. try_compile(${success} SOURCES "${_HDF5_TEST_DIR}/${_HDF5_TEST_SRC}"
  210. COPY_FILE ${_HDF5_TEST_DIR}/compiler_has_h5_c
  211. )
  212. endif()
  213. if(${success} AND EXISTS ${_HDF5_TEST_DIR}/compiler_has_h5_c)
  214. file(STRINGS ${_HDF5_TEST_DIR}/compiler_has_h5_c INFO_STRINGS
  215. REGEX "^INFO:"
  216. )
  217. string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  218. INFO_VER "${INFO_STRINGS}"
  219. )
  220. set(${version} ${CMAKE_MATCH_1})
  221. if(CMAKE_MATCH_3)
  222. set(${version} ${HDF5_C_VERSION}.${CMAKE_MATCH_3})
  223. endif()
  224. set(${version} ${${version}} PARENT_SCOPE)
  225. if(INFO_STRINGS MATCHES "INFO:PARALLEL")
  226. set(${is_parallel} TRUE PARENT_SCOPE)
  227. else()
  228. set(${is_parallel} FALSE PARENT_SCOPE)
  229. endif()
  230. endif()
  231. endfunction()
  232. function(_HDF5_test_regular_compiler_CXX success version is_parallel)
  233. if(NOT ${success} OR
  234. NOT EXISTS ${_HDF5_TEST_DIR}/compiler_has_h5_cxx)
  235. file(WRITE "${_HDF5_TEST_DIR}/${_HDF5_TEST_SRC}"
  236. "#include <H5Cpp.h>\n"
  237. "#ifndef H5_NO_NAMESPACE\n"
  238. "using namespace H5;\n"
  239. "#endif\n"
  240. "const char* info_ver = \"INFO\" \":\" H5_VERSION;\n"
  241. "#ifdef H5_HAVE_PARALLEL\n"
  242. "const char* info_parallel = \"INFO\" \":\" \"PARALLEL\";\n"
  243. "#endif\n"
  244. "int main(int argc, char **argv) {\n"
  245. " int require = 0;\n"
  246. " require += info_ver[argc];\n"
  247. "#ifdef H5_HAVE_PARALLEL\n"
  248. " require += info_parallel[argc];\n"
  249. "#endif\n"
  250. " H5File file(\"foo.h5\", H5F_ACC_TRUNC);\n"
  251. " return 0;\n"
  252. "}")
  253. try_compile(${success} SOURCES "${_HDF5_TEST_DIR}/${_HDF5_TEST_SRC}"
  254. COPY_FILE ${_HDF5_TEST_DIR}/compiler_has_h5_cxx
  255. )
  256. endif()
  257. if(${success} AND EXISTS ${_HDF5_TEST_DIR}/compiler_has_h5_cxx)
  258. file(STRINGS ${_HDF5_TEST_DIR}/compiler_has_h5_cxx INFO_STRINGS
  259. REGEX "^INFO:"
  260. )
  261. string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  262. INFO_VER "${INFO_STRINGS}"
  263. )
  264. set(${version} ${CMAKE_MATCH_1})
  265. if(CMAKE_MATCH_3)
  266. set(${version} ${HDF5_CXX_VERSION}.${CMAKE_MATCH_3})
  267. endif()
  268. set(${version} ${${version}} PARENT_SCOPE)
  269. if(INFO_STRINGS MATCHES "INFO:PARALLEL")
  270. set(${is_parallel} TRUE PARENT_SCOPE)
  271. else()
  272. set(${is_parallel} FALSE PARENT_SCOPE)
  273. endif()
  274. endif()
  275. endfunction()
  276. function(_HDF5_test_regular_compiler_Fortran success is_parallel)
  277. if(NOT ${success})
  278. file(WRITE "${_HDF5_TEST_DIR}/${_HDF5_TEST_SRC}"
  279. "program hdf5_hello\n"
  280. " use hdf5\n"
  281. " integer error\n"
  282. " call h5open_f(error)\n"
  283. " call h5close_f(error)\n"
  284. "end\n")
  285. try_compile(${success} SOURCES "${_HDF5_TEST_DIR}/${_HDF5_TEST_SRC}")
  286. if(${success})
  287. execute_process(COMMAND ${CMAKE_Fortran_COMPILER} -showconfig
  288. OUTPUT_VARIABLE config_output
  289. ERROR_VARIABLE config_error
  290. RESULT_VARIABLE config_result
  291. )
  292. if(config_output MATCHES "Parallel HDF5: ([A-Za-z0-9]+)")
  293. # The value may be anything used when HDF5 was configured,
  294. # so see if CMake interprets it as "true".
  295. set(parallelHDF5 "${CMAKE_MATCH_1}")
  296. if(parallelHDF5)
  297. set(${is_parallel} TRUE PARENT_SCOPE)
  298. else()
  299. set(${is_parallel} FALSE PARENT_SCOPE)
  300. endif()
  301. else()
  302. set(${is_parallel} FALSE PARENT_SCOPE)
  303. endif()
  304. endif()
  305. endif()
  306. endfunction()
  307. # Invoke the HDF5 wrapper compiler. The compiler return value is stored to the
  308. # return_value argument, the text output is stored to the output variable.
  309. function( _HDF5_invoke_compiler language output_var return_value_var version_var is_parallel_var)
  310. set(is_parallel FALSE)
  311. if(HDF5_USE_STATIC_LIBRARIES)
  312. set(lib_type_args -noshlib)
  313. else()
  314. set(lib_type_args -shlib)
  315. endif()
  316. # Verify that the compiler wrapper can actually compile: sometimes the compiler
  317. # wrapper exists, but not the compiler. E.g. Miniconda / Anaconda Python
  318. execute_process(
  319. COMMAND ${HDF5_${language}_COMPILER_EXECUTABLE} "${_HDF5_TEST_SRC}"
  320. WORKING_DIRECTORY ${_HDF5_TEST_DIR}
  321. OUTPUT_VARIABLE output
  322. ERROR_VARIABLE output
  323. RESULT_VARIABLE return_value
  324. )
  325. if(NOT return_value EQUAL 0)
  326. message(CONFIGURE_LOG
  327. "HDF5 ${language} compiler wrapper is unable to compile a minimal HDF5 program.\n\n${output}")
  328. if(NOT HDF5_FIND_QUIETLY)
  329. message(STATUS
  330. "HDF5 ${language} compiler wrapper is unable to compile a minimal HDF5 program.")
  331. endif()
  332. else()
  333. execute_process(
  334. COMMAND ${HDF5_${language}_COMPILER_EXECUTABLE} -show ${lib_type_args} "${_HDF5_TEST_SRC}"
  335. WORKING_DIRECTORY ${_HDF5_TEST_DIR}
  336. OUTPUT_VARIABLE output
  337. ERROR_VARIABLE output
  338. RESULT_VARIABLE return_value
  339. OUTPUT_STRIP_TRAILING_WHITESPACE
  340. )
  341. if(NOT return_value EQUAL 0)
  342. message(CONFIGURE_LOG
  343. "Unable to determine HDF5 ${language} flags from HDF5 wrapper.\n\n${output}")
  344. if(NOT HDF5_FIND_QUIETLY)
  345. message(STATUS
  346. "Unable to determine HDF5 ${language} flags from HDF5 wrapper.")
  347. endif()
  348. endif()
  349. execute_process(
  350. COMMAND ${HDF5_${language}_COMPILER_EXECUTABLE} -showconfig
  351. OUTPUT_VARIABLE config_output
  352. ERROR_VARIABLE config_output
  353. RESULT_VARIABLE return_value
  354. OUTPUT_STRIP_TRAILING_WHITESPACE
  355. )
  356. if(NOT return_value EQUAL 0)
  357. message(CONFIGURE_LOG
  358. "Unable to determine HDF5 ${language} version_var from HDF5 wrapper.\n\n${output}")
  359. if(NOT HDF5_FIND_QUIETLY)
  360. message(STATUS
  361. "Unable to determine HDF5 ${language} version_var from HDF5 wrapper.")
  362. endif()
  363. endif()
  364. string(REGEX MATCH "HDF5 Version: ([a-zA-Z0-9\\.\\-]*)" version "${config_output}")
  365. if(version)
  366. string(REPLACE "HDF5 Version: " "" version "${version}")
  367. string(REPLACE "-patch" "." version "${version}")
  368. endif()
  369. if(config_output MATCHES "Parallel HDF5: ([A-Za-z0-9]+)")
  370. # The value may be anything used when HDF5 was configured,
  371. # so see if CMake interprets it as "true".
  372. set(parallelHDF5 "${CMAKE_MATCH_1}")
  373. if(parallelHDF5)
  374. set(is_parallel TRUE)
  375. endif()
  376. endif()
  377. endif()
  378. foreach(var output return_value version is_parallel)
  379. set(${${var}_var} ${${var}} PARENT_SCOPE)
  380. endforeach()
  381. endfunction()
  382. # Parse a compile line for definitions, includes, library paths, and libraries.
  383. function(_HDF5_parse_compile_line compile_line_var include_paths definitions
  384. library_paths libraries libraries_hl)
  385. separate_arguments(_compile_args NATIVE_COMMAND "${${compile_line_var}}")
  386. foreach(_arg IN LISTS _compile_args)
  387. if("${_arg}" MATCHES "^-I(.*)$")
  388. # include directory
  389. list(APPEND include_paths "${CMAKE_MATCH_1}")
  390. elseif("${_arg}" MATCHES "^-D(.*)$")
  391. # compile definition
  392. list(APPEND definitions "-D${CMAKE_MATCH_1}")
  393. elseif("${_arg}" MATCHES "^-L(.*)$")
  394. # library search path
  395. list(APPEND library_paths "${CMAKE_MATCH_1}")
  396. elseif("${_arg}" MATCHES "^-l(hdf5.*hl.*)$")
  397. # library name (hl)
  398. list(APPEND libraries_hl "${CMAKE_MATCH_1}")
  399. elseif("${_arg}" MATCHES "^-l(.*)$")
  400. # library name
  401. list(APPEND libraries "${CMAKE_MATCH_1}")
  402. elseif("${_arg}" MATCHES "^(.:)?[/\\].*\\.(a|so|dylib|sl|lib)$")
  403. # library file
  404. if(NOT EXISTS "${_arg}")
  405. continue()
  406. endif()
  407. get_filename_component(_lpath "${_arg}" DIRECTORY)
  408. get_filename_component(_lname "${_arg}" NAME_WE)
  409. string(REGEX REPLACE "^lib" "" _lname "${_lname}")
  410. list(APPEND library_paths "${_lpath}")
  411. if(_lname MATCHES "hdf5.*hl")
  412. list(APPEND libraries_hl "${_lname}")
  413. else()
  414. list(APPEND libraries "${_lname}")
  415. endif()
  416. endif()
  417. endforeach()
  418. foreach(var include_paths definitions library_paths libraries libraries_hl)
  419. set(${${var}_var} ${${var}} PARENT_SCOPE)
  420. endforeach()
  421. endfunction()
  422. # Select a preferred imported configuration from a target
  423. function(_HDF5_select_imported_config target imported_conf)
  424. # We will first assign the value to a local variable _imported_conf, then assign
  425. # it to the function argument at the end.
  426. get_target_property(_imported_conf ${target} MAP_IMPORTED_CONFIG_${CMAKE_BUILD_TYPE})
  427. if (NOT _imported_conf)
  428. # Get available imported configurations by examining target properties
  429. get_target_property(_imported_conf ${target} IMPORTED_CONFIGURATIONS)
  430. if(HDF5_FIND_DEBUG)
  431. message(STATUS "Found imported configurations: ${_imported_conf}")
  432. endif()
  433. # Find the imported configuration that we prefer.
  434. # We do this by making list of configurations in order of preference,
  435. # starting with ${CMAKE_BUILD_TYPE} and ending with the first imported_conf
  436. set(_preferred_confs ${CMAKE_BUILD_TYPE})
  437. list(GET _imported_conf 0 _fallback_conf)
  438. list(APPEND _preferred_confs RELWITHDEBINFO RELEASE DEBUG ${_fallback_conf})
  439. if(HDF5_FIND_DEBUG)
  440. message(STATUS "Start search through imported configurations in the following order: ${_preferred_confs}")
  441. endif()
  442. # Now find the first of these that is present in imported_conf
  443. foreach (_conf IN LISTS _preferred_confs)
  444. if (${_conf} IN_LIST _imported_conf)
  445. set(_imported_conf ${_conf})
  446. break()
  447. endif()
  448. endforeach()
  449. endif()
  450. if(HDF5_FIND_DEBUG)
  451. message(STATUS "Selected imported configuration: ${_imported_conf}")
  452. endif()
  453. # assign value to function argument
  454. set(${imported_conf} ${_imported_conf} PARENT_SCOPE)
  455. endfunction()
  456. if(NOT HDF5_ROOT)
  457. set(HDF5_ROOT $ENV{HDF5_ROOT})
  458. endif()
  459. if(HDF5_ROOT)
  460. set(_HDF5_SEARCH_OPTS NO_DEFAULT_PATH)
  461. else()
  462. set(_HDF5_SEARCH_OPTS)
  463. endif()
  464. # Try to find HDF5 using an installed hdf5-config.cmake
  465. if(NOT HDF5_FOUND AND NOT HDF5_NO_FIND_PACKAGE_CONFIG_FILE)
  466. find_package(HDF5 QUIET NO_MODULE
  467. HINTS ${HDF5_ROOT}
  468. ${_HDF5_SEARCH_OPTS}
  469. )
  470. if( HDF5_FOUND)
  471. if(HDF5_FIND_DEBUG)
  472. message(STATUS "Found HDF5 at ${HDF5_DIR} via NO_MODULE. Now trying to extract locations etc.")
  473. endif()
  474. set(HDF5_IS_PARALLEL ${HDF5_ENABLE_PARALLEL})
  475. set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR})
  476. set(HDF5_LIBRARIES)
  477. if (NOT TARGET hdf5 AND NOT TARGET hdf5-static AND NOT TARGET hdf5-shared)
  478. # Some HDF5 versions (e.g. 1.8.18) used hdf5::hdf5 etc
  479. set(_target_prefix "hdf5::")
  480. endif()
  481. set(HDF5_C_TARGET ${_target_prefix}hdf5)
  482. set(HDF5_C_HL_TARGET ${_target_prefix}hdf5_hl)
  483. set(HDF5_CXX_TARGET ${_target_prefix}hdf5_cpp)
  484. set(HDF5_CXX_HL_TARGET ${_target_prefix}hdf5_hl_cpp)
  485. set(HDF5_Fortran_TARGET ${_target_prefix}hdf5_fortran)
  486. set(HDF5_Fortran_HL_TARGET ${_target_prefix}hdf5_hl_fortran)
  487. set(HDF5_DEFINITIONS "")
  488. if(HDF5_USE_STATIC_LIBRARIES)
  489. set(_suffix "-static")
  490. else()
  491. set(_suffix "-shared")
  492. endif()
  493. foreach(_lang ${HDF5_LANGUAGE_BINDINGS})
  494. #Older versions of hdf5 don't have a static/shared suffix so
  495. #if we detect that occurrence clear the suffix
  496. if(_suffix AND NOT TARGET ${HDF5_${_lang}_TARGET}${_suffix})
  497. if(NOT TARGET ${HDF5_${_lang}_TARGET})
  498. #can't find this component with or without the suffix
  499. #so bail out, and let the following locate HDF5
  500. set(HDF5_FOUND FALSE)
  501. break()
  502. endif()
  503. set(_suffix "")
  504. endif()
  505. if(HDF5_FIND_DEBUG)
  506. message(STATUS "Trying to get properties of target ${HDF5_${_lang}_TARGET}${_suffix}")
  507. endif()
  508. # Find library for this target. Complicated as on Windows with a DLL, we need to search for the import-lib.
  509. _HDF5_select_imported_config(${HDF5_${_lang}_TARGET}${_suffix} _hdf5_imported_conf)
  510. get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
  511. if (NOT _hdf5_lang_location)
  512. # no import lib, just try LOCATION
  513. get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION_${_hdf5_imported_conf})
  514. if (NOT _hdf5_lang_location)
  515. get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
  516. endif()
  517. endif()
  518. if( _hdf5_lang_location )
  519. set(HDF5_${_lang}_LIBRARY ${_hdf5_lang_location})
  520. list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
  521. set(HDF5_${_lang}_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
  522. set(HDF5_${_lang}_FOUND TRUE)
  523. endif()
  524. if(HDF5_FIND_HL)
  525. get_target_property(_hdf5_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
  526. if (NOT _hdf5_lang_hl_location)
  527. get_target_property(_hdf5_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION_${_hdf5_imported_conf})
  528. if (NOT _hdf5_hl_lang_location)
  529. get_target_property(_hdf5_hl_lang_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
  530. endif()
  531. endif()
  532. if( _hdf5_lang_hl_location )
  533. set(HDF5_${_lang}_HL_LIBRARY ${_hdf5_lang_hl_location})
  534. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_TARGET}${_suffix})
  535. set(HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_HL_TARGET}${_suffix})
  536. set(HDF5_HL_FOUND TRUE)
  537. endif()
  538. unset(_hdf5_lang_hl_location)
  539. endif()
  540. unset(_hdf5_imported_conf)
  541. unset(_hdf5_lang_location)
  542. endforeach()
  543. endif()
  544. endif()
  545. if(NOT HDF5_FOUND)
  546. set(_HDF5_NEED_TO_SEARCH FALSE)
  547. set(_HDF5_TEST_DIR ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
  548. set(HDF5_COMPILER_NO_INTERROGATE TRUE)
  549. # Only search for languages we've enabled
  550. foreach(_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  551. set(HDF5_${_lang}_LIBRARIES)
  552. set(HDF5_${_lang}_HL_LIBRARIES)
  553. # First check to see if our regular compiler is one of wrappers
  554. if(_lang STREQUAL "C")
  555. set(_HDF5_TEST_SRC cmake_hdf5_test.c)
  556. if(CMAKE_CXX_COMPILER_LOADED AND NOT CMAKE_C_COMPILER_LOADED)
  557. # CXX project without C enabled
  558. set(_HDF5_TEST_SRC cmake_hdf5_test.cxx)
  559. endif()
  560. _HDF5_test_regular_compiler_C(
  561. HDF5_${_lang}_COMPILER_NO_INTERROGATE
  562. HDF5_${_lang}_VERSION
  563. HDF5_${_lang}_IS_PARALLEL)
  564. elseif(_lang STREQUAL "CXX")
  565. set(_HDF5_TEST_SRC cmake_hdf5_test.cxx)
  566. _HDF5_test_regular_compiler_CXX(
  567. HDF5_${_lang}_COMPILER_NO_INTERROGATE
  568. HDF5_${_lang}_VERSION
  569. HDF5_${_lang}_IS_PARALLEL)
  570. elseif(_lang STREQUAL "Fortran")
  571. set(_HDF5_TEST_SRC cmake_hdf5_test.f90)
  572. _HDF5_test_regular_compiler_Fortran(
  573. HDF5_${_lang}_COMPILER_NO_INTERROGATE
  574. HDF5_${_lang}_IS_PARALLEL)
  575. else()
  576. continue()
  577. endif()
  578. if(HDF5_${_lang}_COMPILER_NO_INTERROGATE)
  579. if(HDF5_FIND_DEBUG)
  580. message(STATUS "HDF5: Using hdf5 compiler wrapper for all ${_lang} compiling")
  581. endif()
  582. set(HDF5_${_lang}_FOUND TRUE)
  583. set(HDF5_${_lang}_COMPILER_EXECUTABLE_NO_INTERROGATE
  584. "${CMAKE_${_lang}_COMPILER}"
  585. CACHE FILEPATH "HDF5 ${_lang} compiler wrapper")
  586. set(HDF5_${_lang}_DEFINITIONS)
  587. set(HDF5_${_lang}_INCLUDE_DIRS)
  588. set(HDF5_${_lang}_LIBRARIES)
  589. set(HDF5_${_lang}_HL_LIBRARIES)
  590. mark_as_advanced(HDF5_${_lang}_COMPILER_EXECUTABLE_NO_INTERROGATE)
  591. set(HDF5_${_lang}_FOUND TRUE)
  592. set(HDF5_HL_FOUND TRUE)
  593. else()
  594. set(HDF5_COMPILER_NO_INTERROGATE FALSE)
  595. # If this language isn't using the wrapper, then try to seed the
  596. # search options with the wrapper
  597. find_program(HDF5_${_lang}_COMPILER_EXECUTABLE
  598. NAMES ${HDF5_${_lang}_COMPILER_NAMES} NAMES_PER_DIR
  599. HINTS ${HDF5_ROOT}
  600. PATH_SUFFIXES bin Bin
  601. DOC "HDF5 ${_lang} Wrapper compiler. Used only to detect HDF5 compile flags."
  602. ${_HDF5_SEARCH_OPTS}
  603. )
  604. mark_as_advanced( HDF5_${_lang}_COMPILER_EXECUTABLE )
  605. unset(HDF5_${_lang}_COMPILER_NAMES)
  606. if(HDF5_${_lang}_COMPILER_EXECUTABLE)
  607. _HDF5_invoke_compiler(${_lang} HDF5_${_lang}_COMPILE_LINE
  608. HDF5_${_lang}_RETURN_VALUE HDF5_${_lang}_VERSION HDF5_${_lang}_IS_PARALLEL)
  609. if(HDF5_${_lang}_RETURN_VALUE EQUAL 0)
  610. if(HDF5_FIND_DEBUG)
  611. message(STATUS "HDF5: Using hdf5 compiler wrapper to determine ${_lang} configuration")
  612. endif()
  613. _HDF5_parse_compile_line( HDF5_${_lang}_COMPILE_LINE
  614. HDF5_${_lang}_INCLUDE_DIRS
  615. HDF5_${_lang}_DEFINITIONS
  616. HDF5_${_lang}_LIBRARY_DIRS
  617. HDF5_${_lang}_LIBRARY_NAMES
  618. HDF5_${_lang}_HL_LIBRARY_NAMES
  619. )
  620. set(HDF5_${_lang}_LIBRARIES)
  621. foreach(_lib IN LISTS HDF5_${_lang}_LIBRARY_NAMES)
  622. set(_HDF5_SEARCH_NAMES_LOCAL)
  623. if("x${_lib}" MATCHES "hdf5")
  624. # hdf5 library
  625. set(_HDF5_SEARCH_OPTS_LOCAL ${_HDF5_SEARCH_OPTS})
  626. if(HDF5_USE_STATIC_LIBRARIES)
  627. if(WIN32)
  628. set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib})
  629. else()
  630. set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib}.a)
  631. endif()
  632. endif()
  633. else()
  634. # external library
  635. set(_HDF5_SEARCH_OPTS_LOCAL)
  636. endif()
  637. find_library(HDF5_${_lang}_LIBRARY_${_lib}
  638. NAMES ${_HDF5_SEARCH_NAMES_LOCAL} ${_lib} NAMES_PER_DIR
  639. HINTS ${HDF5_${_lang}_LIBRARY_DIRS}
  640. ${HDF5_ROOT}
  641. ${_HDF5_SEARCH_OPTS_LOCAL}
  642. )
  643. unset(_HDF5_SEARCH_OPTS_LOCAL)
  644. unset(_HDF5_SEARCH_NAMES_LOCAL)
  645. if(HDF5_${_lang}_LIBRARY_${_lib})
  646. list(APPEND HDF5_${_lang}_LIBRARIES ${HDF5_${_lang}_LIBRARY_${_lib}})
  647. else()
  648. list(APPEND HDF5_${_lang}_LIBRARIES ${_lib})
  649. endif()
  650. endforeach()
  651. if(HDF5_FIND_HL)
  652. set(HDF5_${_lang}_HL_LIBRARIES)
  653. foreach(_lib IN LISTS HDF5_${_lang}_HL_LIBRARY_NAMES)
  654. set(_HDF5_SEARCH_NAMES_LOCAL)
  655. if("x${_lib}" MATCHES "hdf5")
  656. # hdf5 library
  657. set(_HDF5_SEARCH_OPTS_LOCAL ${_HDF5_SEARCH_OPTS})
  658. if(HDF5_USE_STATIC_LIBRARIES)
  659. if(WIN32)
  660. set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib})
  661. else()
  662. set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib}.a)
  663. endif()
  664. endif()
  665. else()
  666. # external library
  667. set(_HDF5_SEARCH_OPTS_LOCAL)
  668. endif()
  669. find_library(HDF5_${_lang}_LIBRARY_${_lib}
  670. NAMES ${_HDF5_SEARCH_NAMES_LOCAL} ${_lib} NAMES_PER_DIR
  671. HINTS ${HDF5_${_lang}_LIBRARY_DIRS}
  672. ${HDF5_ROOT}
  673. ${_HDF5_SEARCH_OPTS_LOCAL}
  674. )
  675. unset(_HDF5_SEARCH_OPTS_LOCAL)
  676. unset(_HDF5_SEARCH_NAMES_LOCAL)
  677. if(HDF5_${_lang}_LIBRARY_${_lib})
  678. list(APPEND HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_LIBRARY_${_lib}})
  679. else()
  680. list(APPEND HDF5_${_lang}_HL_LIBRARIES ${_lib})
  681. endif()
  682. endforeach()
  683. set(HDF5_HL_FOUND TRUE)
  684. endif()
  685. set(HDF5_${_lang}_FOUND TRUE)
  686. list(REMOVE_DUPLICATES HDF5_${_lang}_DEFINITIONS)
  687. list(REMOVE_DUPLICATES HDF5_${_lang}_INCLUDE_DIRS)
  688. else()
  689. set(_HDF5_NEED_TO_SEARCH TRUE)
  690. endif()
  691. else()
  692. set(_HDF5_NEED_TO_SEARCH TRUE)
  693. endif()
  694. endif()
  695. if(HDF5_${_lang}_VERSION)
  696. if(NOT HDF5_VERSION)
  697. set(HDF5_VERSION ${HDF5_${_lang}_VERSION})
  698. elseif(NOT HDF5_VERSION VERSION_EQUAL HDF5_${_lang}_VERSION)
  699. message(WARNING "HDF5 Version found for language ${_lang}, ${HDF5_${_lang}_VERSION} is different than previously found version ${HDF5_VERSION}")
  700. endif()
  701. endif()
  702. if(DEFINED HDF5_${_lang}_IS_PARALLEL)
  703. if(NOT DEFINED HDF5_IS_PARALLEL)
  704. set(HDF5_IS_PARALLEL ${HDF5_${_lang}_IS_PARALLEL})
  705. elseif(NOT HDF5_IS_PARALLEL AND HDF5_${_lang}_IS_PARALLEL)
  706. message(WARNING "HDF5 found for language ${_lang} is parallel but previously found language is not parallel.")
  707. elseif(HDF5_IS_PARALLEL AND NOT HDF5_${_lang}_IS_PARALLEL)
  708. message(WARNING "HDF5 found for language ${_lang} is not parallel but previously found language is parallel.")
  709. endif()
  710. endif()
  711. endforeach()
  712. unset(_HDF5_TEST_DIR)
  713. unset(_HDF5_TEST_SRC)
  714. unset(_lib)
  715. else()
  716. set(_HDF5_NEED_TO_SEARCH TRUE)
  717. endif()
  718. if(NOT HDF5_FOUND AND HDF5_COMPILER_NO_INTERROGATE)
  719. # No arguments necessary, all languages can use the compiler wrappers
  720. set(HDF5_FOUND TRUE)
  721. set(HDF5_METHOD "Included by compiler wrappers")
  722. set(HDF5_REQUIRED_VARS HDF5_METHOD)
  723. elseif(NOT HDF5_FOUND AND NOT _HDF5_NEED_TO_SEARCH)
  724. # Compiler wrappers aren't being used by the build but were found and used
  725. # to determine necessary include and library flags
  726. set(HDF5_INCLUDE_DIRS)
  727. set(HDF5_LIBRARIES)
  728. set(HDF5_HL_LIBRARIES)
  729. foreach(_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  730. if(HDF5_${_lang}_FOUND)
  731. if(NOT HDF5_${_lang}_COMPILER_NO_INTERROGATE)
  732. list(APPEND HDF5_DEFINITIONS ${HDF5_${_lang}_DEFINITIONS})
  733. list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${_lang}_INCLUDE_DIRS})
  734. list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_LIBRARIES})
  735. if(HDF5_FIND_HL)
  736. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARIES})
  737. endif()
  738. endif()
  739. endif()
  740. endforeach()
  741. list(REMOVE_DUPLICATES HDF5_DEFINITIONS)
  742. list(REMOVE_DUPLICATES HDF5_INCLUDE_DIRS)
  743. set(HDF5_FOUND TRUE)
  744. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES)
  745. if(HDF5_FIND_HL)
  746. list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
  747. endif()
  748. endif()
  749. find_program( HDF5_DIFF_EXECUTABLE
  750. NAMES h5diff
  751. HINTS ${HDF5_ROOT}
  752. PATH_SUFFIXES bin Bin
  753. ${_HDF5_SEARCH_OPTS}
  754. DOC "HDF5 file differencing tool." )
  755. mark_as_advanced( HDF5_DIFF_EXECUTABLE )
  756. if( NOT HDF5_FOUND )
  757. # seed the initial lists of libraries to find with items we know we need
  758. set(HDF5_C_LIBRARY_NAMES hdf5)
  759. set(HDF5_C_HL_LIBRARY_NAMES hdf5_hl ${HDF5_C_LIBRARY_NAMES} )
  760. set(HDF5_CXX_LIBRARY_NAMES hdf5_cpp ${HDF5_C_LIBRARY_NAMES})
  761. set(HDF5_CXX_HL_LIBRARY_NAMES hdf5_hl_cpp ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_CXX_LIBRARY_NAMES})
  762. set(HDF5_Fortran_LIBRARY_NAMES hdf5_fortran ${HDF5_C_LIBRARY_NAMES})
  763. set(HDF5_Fortran_HL_LIBRARY_NAMES hdf5_hl_fortran hdf5hl_fortran ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_Fortran_LIBRARY_NAMES})
  764. # suffixes as seen on Linux, MSYS2, ...
  765. set(_lib_suffixes hdf5)
  766. if(NOT HDF5_PREFER_PARALLEL)
  767. list(APPEND _lib_suffixes hdf5/serial)
  768. endif()
  769. if(HDF5_USE_STATIC_LIBRARIES)
  770. set(_inc_suffixes include/static)
  771. else()
  772. set(_inc_suffixes include/shared)
  773. endif()
  774. foreach(_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  775. set(HDF5_${_lang}_LIBRARIES)
  776. set(HDF5_${_lang}_HL_LIBRARIES)
  777. # The "main" library.
  778. set(_hdf5_main_library "")
  779. # find the HDF5 libraries
  780. foreach(LIB IN LISTS HDF5_${_lang}_LIBRARY_NAMES)
  781. if(HDF5_USE_STATIC_LIBRARIES)
  782. # According to bug 1643 on the CMake bug tracker, this is the
  783. # preferred method for searching for a static library.
  784. # See https://gitlab.kitware.com/cmake/cmake/-/issues/1643. We search
  785. # first for the full static library name, but fall back to a
  786. # generic search on the name if the static search fails.
  787. set( THIS_LIBRARY_SEARCH_DEBUG
  788. lib${LIB}d.a lib${LIB}_debug.a lib${LIB}d lib${LIB}_D lib${LIB}_debug
  789. lib${LIB}d-static.a lib${LIB}_debug-static.a ${LIB}d-static ${LIB}_D-static ${LIB}_debug-static )
  790. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a lib${LIB} lib${LIB}-static.a ${LIB}-static)
  791. else()
  792. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_D ${LIB}_debug ${LIB}d-shared ${LIB}_D-shared ${LIB}_debug-shared)
  793. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
  794. if(WIN32)
  795. list(APPEND HDF5_DEFINITIONS "-DH5_BUILT_AS_DYNAMIC_LIB")
  796. endif()
  797. endif()
  798. find_library(HDF5_${LIB}_LIBRARY_DEBUG
  799. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  800. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
  801. ${_HDF5_SEARCH_OPTS}
  802. )
  803. find_library(HDF5_${LIB}_LIBRARY_RELEASE
  804. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  805. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
  806. ${_HDF5_SEARCH_OPTS}
  807. )
  808. # Set the "main" library if not already set.
  809. if (NOT _hdf5_main_library)
  810. if (HDF5_${LIB}_LIBRARY_RELEASE)
  811. set(_hdf5_main_library "${HDF5_${LIB}_LIBRARY_RELEASE}")
  812. elseif (HDF5_${LIB}_LIBRARY_DEBUG)
  813. set(_hdf5_main_library "${HDF5_${LIB}_LIBRARY_DEBUG}")
  814. endif ()
  815. endif ()
  816. select_library_configurations( HDF5_${LIB} )
  817. list(APPEND HDF5_${_lang}_LIBRARIES ${HDF5_${LIB}_LIBRARY})
  818. endforeach()
  819. if(HDF5_${_lang}_LIBRARIES)
  820. set(HDF5_${_lang}_FOUND TRUE)
  821. endif()
  822. # Append the libraries for this language binding to the list of all
  823. # required libraries.
  824. list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_LIBRARIES})
  825. # find the HDF5 include directories
  826. set(_hdf5_inc_extra_paths)
  827. set(_hdf5_inc_extra_suffixes)
  828. if("${_lang}" STREQUAL "Fortran")
  829. set(HDF5_INCLUDE_FILENAME hdf5.mod HDF5.mod)
  830. # Add library-based search paths for Fortran modules.
  831. if (NOT _hdf5_main_library STREQUAL "")
  832. # gfortran module directory
  833. if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" OR CMAKE_Fortran_COMPILER_ID STREQUAL "LCC")
  834. get_filename_component(_hdf5_library_dir "${_hdf5_main_library}" DIRECTORY)
  835. list(APPEND _hdf5_inc_extra_paths "${_hdf5_library_dir}")
  836. unset(_hdf5_library_dir)
  837. list(APPEND _hdf5_inc_extra_suffixes gfortran/modules)
  838. endif ()
  839. endif ()
  840. elseif("${_lang}" STREQUAL "CXX")
  841. set(HDF5_INCLUDE_FILENAME H5Cpp.h)
  842. else()
  843. set(HDF5_INCLUDE_FILENAME hdf5.h)
  844. endif()
  845. unset(_hdf5_main_library)
  846. find_path(HDF5_${_lang}_INCLUDE_DIR ${HDF5_INCLUDE_FILENAME}
  847. HINTS ${HDF5_ROOT}
  848. PATHS $ENV{HOME}/.local/include ${_hdf5_inc_extra_paths}
  849. PATH_SUFFIXES include Include ${_inc_suffixes} ${_lib_suffixes} ${_hdf5_inc_extra_suffixes}
  850. ${_HDF5_SEARCH_OPTS}
  851. )
  852. mark_as_advanced(HDF5_${_lang}_INCLUDE_DIR)
  853. unset(_hdf5_inc_extra_paths)
  854. unset(_hdf5_inc_extra_suffixes)
  855. # set the _DIRS variable as this is what the user will normally use
  856. set(HDF5_${_lang}_INCLUDE_DIRS ${HDF5_${_lang}_INCLUDE_DIR})
  857. list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${_lang}_INCLUDE_DIR})
  858. if(HDF5_FIND_HL)
  859. foreach(LIB IN LISTS HDF5_${_lang}_HL_LIBRARY_NAMES)
  860. if(HDF5_USE_STATIC_LIBRARIES)
  861. # According to bug 1643 on the CMake bug tracker, this is the
  862. # preferred method for searching for a static library.
  863. # See https://gitlab.kitware.com/cmake/cmake/-/issues/1643. We search
  864. # first for the full static library name, but fall back to a
  865. # generic search on the name if the static search fails.
  866. set( THIS_LIBRARY_SEARCH_DEBUG
  867. lib${LIB}d.a lib${LIB}_debug.a lib${LIB}d lib${LIB}_D lib${LIB}_debug
  868. lib${LIB}d-static.a lib${LIB}_debug-static.a lib${LIB}d-static lib${LIB}_D-static lib${LIB}_debug-static )
  869. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a lib${LIB} lib${LIB}-static.a lib${LIB}-static)
  870. else()
  871. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_D ${LIB}_debug ${LIB}d-shared ${LIB}_D-shared ${LIB}_debug-shared)
  872. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
  873. endif()
  874. find_library(HDF5_${LIB}_LIBRARY_DEBUG
  875. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  876. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
  877. ${_HDF5_SEARCH_OPTS}
  878. )
  879. find_library(HDF5_${LIB}_LIBRARY_RELEASE
  880. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  881. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
  882. ${_HDF5_SEARCH_OPTS}
  883. )
  884. select_library_configurations( HDF5_${LIB} )
  885. list(APPEND HDF5_${_lang}_HL_LIBRARIES ${HDF5_${LIB}_LIBRARY})
  886. endforeach()
  887. # Append the libraries for this language binding to the list of all
  888. # required libraries.
  889. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARIES})
  890. endif()
  891. endforeach()
  892. if(HDF5_FIND_HL AND HDF5_HL_LIBRARIES)
  893. set(HDF5_HL_FOUND TRUE)
  894. endif()
  895. list(REMOVE_DUPLICATES HDF5_DEFINITIONS)
  896. list(REMOVE_DUPLICATES HDF5_INCLUDE_DIRS)
  897. # If the HDF5 include directory was found, open H5pubconf.h to determine if
  898. # HDF5 was compiled with parallel IO support
  899. set( HDF5_IS_PARALLEL FALSE )
  900. foreach( _dir IN LISTS HDF5_INCLUDE_DIRS )
  901. foreach(_hdr "${_dir}/H5pubconf.h" "${_dir}/H5pubconf-64.h" "${_dir}/H5pubconf-32.h")
  902. if( EXISTS "${_hdr}" )
  903. file( STRINGS "${_hdr}"
  904. HDF5_HAVE_PARALLEL_DEFINE
  905. REGEX "HAVE_PARALLEL 1" )
  906. if( HDF5_HAVE_PARALLEL_DEFINE )
  907. set( HDF5_IS_PARALLEL TRUE )
  908. endif()
  909. unset(HDF5_HAVE_PARALLEL_DEFINE)
  910. file( STRINGS "${_hdr}"
  911. HDF5_VERSION_DEFINE
  912. REGEX "^[ \t]*#[ \t]*define[ \t]+H5_VERSION[ \t]+" )
  913. if( "${HDF5_VERSION_DEFINE}" MATCHES
  914. "H5_VERSION[ \t]+\"([0-9\\.]+)(-patch([0-9]+))?\"" )
  915. set( HDF5_VERSION "${CMAKE_MATCH_1}" )
  916. if( CMAKE_MATCH_3 )
  917. set( HDF5_VERSION ${HDF5_VERSION}.${CMAKE_MATCH_3})
  918. endif()
  919. endif()
  920. unset(HDF5_VERSION_DEFINE)
  921. endif()
  922. endforeach()
  923. endforeach()
  924. unset(_hdr)
  925. unset(_dir)
  926. set( HDF5_IS_PARALLEL ${HDF5_IS_PARALLEL} CACHE BOOL
  927. "HDF5 library compiled with parallel IO support" )
  928. mark_as_advanced( HDF5_IS_PARALLEL )
  929. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
  930. if(HDF5_FIND_HL)
  931. list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
  932. endif()
  933. endif()
  934. # For backwards compatibility we set HDF5_INCLUDE_DIR to the value of
  935. # HDF5_INCLUDE_DIRS
  936. if( HDF5_INCLUDE_DIRS )
  937. set( HDF5_INCLUDE_DIR "${HDF5_INCLUDE_DIRS}" )
  938. endif()
  939. # If HDF5_REQUIRED_VARS is empty at this point, then it's likely that
  940. # something external is trying to explicitly pass already found
  941. # locations
  942. if(NOT HDF5_REQUIRED_VARS)
  943. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
  944. endif()
  945. find_package_handle_standard_args(HDF5
  946. REQUIRED_VARS ${HDF5_REQUIRED_VARS}
  947. VERSION_VAR HDF5_VERSION
  948. HANDLE_COMPONENTS
  949. )
  950. unset(_HDF5_SEARCH_OPTS)
  951. if( HDF5_FOUND AND NOT HDF5_DIR)
  952. # hide HDF5_DIR for the non-advanced user to avoid confusion with
  953. # HDF5_DIR-NOT_FOUND while HDF5 was found.
  954. mark_as_advanced(HDF5_DIR)
  955. endif()
  956. if (HDF5_FOUND)
  957. if (NOT TARGET HDF5::HDF5)
  958. add_library(HDF5::HDF5 INTERFACE IMPORTED)
  959. string(REPLACE "-D" "" _hdf5_definitions "${HDF5_DEFINITIONS}")
  960. set_target_properties(HDF5::HDF5 PROPERTIES
  961. INTERFACE_INCLUDE_DIRECTORIES "${HDF5_INCLUDE_DIRS}"
  962. INTERFACE_COMPILE_DEFINITIONS "${_hdf5_definitions}")
  963. unset(_hdf5_definitions)
  964. target_link_libraries(HDF5::HDF5 INTERFACE ${HDF5_LIBRARIES})
  965. endif ()
  966. foreach (hdf5_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  967. if (hdf5_lang STREQUAL "C")
  968. set(hdf5_target_name "hdf5")
  969. elseif (hdf5_lang STREQUAL "CXX")
  970. set(hdf5_target_name "hdf5_cpp")
  971. elseif (hdf5_lang STREQUAL "Fortran")
  972. set(hdf5_target_name "hdf5_fortran")
  973. else ()
  974. continue ()
  975. endif ()
  976. if (NOT TARGET "hdf5::${hdf5_target_name}")
  977. if (HDF5_COMPILER_NO_INTERROGATE)
  978. add_library("hdf5::${hdf5_target_name}" INTERFACE IMPORTED)
  979. string(REPLACE "-D" "" _hdf5_definitions "${HDF5_${hdf5_lang}_DEFINITIONS}")
  980. set_target_properties("hdf5::${hdf5_target_name}" PROPERTIES
  981. INTERFACE_INCLUDE_DIRECTORIES "${HDF5_${hdf5_lang}_INCLUDE_DIRS}"
  982. INTERFACE_COMPILE_DEFINITIONS "${_hdf5_definitions}")
  983. else()
  984. if (DEFINED "HDF5_${hdf5_target_name}_LIBRARY")
  985. set(_hdf5_location "${HDF5_${hdf5_target_name}_LIBRARY}")
  986. set(_hdf5_location_release "${HDF5_${hdf5_target_name}_LIBRARY_RELEASE}")
  987. set(_hdf5_location_debug "${HDF5_${hdf5_target_name}_LIBRARY_DEBUG}")
  988. elseif (DEFINED "HDF5_${hdf5_lang}_LIBRARY")
  989. set(_hdf5_location "${HDF5_${hdf5_lang}_LIBRARY}")
  990. set(_hdf5_location_release "${HDF5_${hdf5_lang}_LIBRARY_RELEASE}")
  991. set(_hdf5_location_debug "${HDF5_${hdf5_lang}_LIBRARY_DEBUG}")
  992. elseif (DEFINED "HDF5_${hdf5_lang}_LIBRARY_${hdf5_target_name}")
  993. set(_hdf5_location "${HDF5_${hdf5_lang}_LIBRARY_${hdf5_target_name}}")
  994. else ()
  995. # Error if we still don't have the location.
  996. message(SEND_ERROR
  997. "HDF5 was found, but a different variable was set which contains "
  998. "the location of the `hdf5::${hdf5_target_name}` library.")
  999. endif ()
  1000. add_library("hdf5::${hdf5_target_name}" UNKNOWN IMPORTED)
  1001. string(REPLACE "-D" "" _hdf5_definitions "${HDF5_${hdf5_lang}_DEFINITIONS}")
  1002. if (NOT HDF5_${hdf5_lang}_INCLUDE_DIRS)
  1003. set(HDF5_${hdf5_lang}_INCLUDE_DIRS ${HDF5_INCLUDE_DIRS})
  1004. endif ()
  1005. set_target_properties("hdf5::${hdf5_target_name}" PROPERTIES
  1006. INTERFACE_INCLUDE_DIRECTORIES "${HDF5_${hdf5_lang}_INCLUDE_DIRS}"
  1007. INTERFACE_COMPILE_DEFINITIONS "${_hdf5_definitions}")
  1008. if (_hdf5_location_release)
  1009. set_property(TARGET "hdf5::${hdf5_target_name}" APPEND PROPERTY
  1010. IMPORTED_CONFIGURATIONS RELEASE)
  1011. set_property(TARGET "hdf5::${hdf5_target_name}" PROPERTY
  1012. IMPORTED_LOCATION_RELEASE "${_hdf5_location_release}")
  1013. endif()
  1014. if (_hdf5_location_debug)
  1015. set_property(TARGET "hdf5::${hdf5_target_name}" APPEND PROPERTY
  1016. IMPORTED_CONFIGURATIONS DEBUG)
  1017. set_property(TARGET "hdf5::${hdf5_target_name}" PROPERTY
  1018. IMPORTED_LOCATION_DEBUG "${_hdf5_location_debug}")
  1019. endif()
  1020. if (NOT _hdf5_location_release AND NOT _hdf5_location_debug)
  1021. set_property(TARGET "hdf5::${hdf5_target_name}" PROPERTY
  1022. IMPORTED_LOCATION "${_hdf5_location}")
  1023. endif()
  1024. if (_hdf5_libtype STREQUAL "SHARED")
  1025. set_property(TARGET "hdf5::${hdf5_target_name}" APPEND
  1026. PROPERTY
  1027. INTERFACE_COMPILE_DEFINITIONS H5_BUILT_AS_DYNAMIC_LIB)
  1028. elseif (_hdf5_libtype STREQUAL "STATIC")
  1029. set_property(TARGET "hdf5::${hdf5_target_name}" APPEND
  1030. PROPERTY
  1031. INTERFACE_COMPILE_DEFINITIONS H5_BUILT_AS_STATIC_LIB)
  1032. endif ()
  1033. unset(_hdf5_definitions)
  1034. unset(_hdf5_libtype)
  1035. unset(_hdf5_location)
  1036. unset(_hdf5_location_release)
  1037. unset(_hdf5_location_debug)
  1038. endif ()
  1039. endif ()
  1040. if (NOT HDF5_FIND_HL)
  1041. continue ()
  1042. endif ()
  1043. set(hdf5_alt_target_name "")
  1044. if (hdf5_lang STREQUAL "C")
  1045. set(hdf5_target_name "hdf5_hl")
  1046. elseif (hdf5_lang STREQUAL "CXX")
  1047. set(hdf5_target_name "hdf5_hl_cpp")
  1048. elseif (hdf5_lang STREQUAL "Fortran")
  1049. set(hdf5_target_name "hdf5_hl_fortran")
  1050. set(hdf5_alt_target_name "hdf5hl_fortran")
  1051. else ()
  1052. continue ()
  1053. endif ()
  1054. if (NOT TARGET "hdf5::${hdf5_target_name}")
  1055. if (HDF5_COMPILER_NO_INTERROGATE)
  1056. add_library("hdf5::${hdf5_target_name}" INTERFACE IMPORTED)
  1057. string(REPLACE "-D" "" _hdf5_definitions "${HDF5_${hdf5_lang}_HL_DEFINITIONS}")
  1058. set_target_properties("hdf5::${hdf5_target_name}" PROPERTIES
  1059. INTERFACE_INCLUDE_DIRECTORIES "${HDF5_${hdf5_lang}_HL_INCLUDE_DIRS}"
  1060. INTERFACE_COMPILE_DEFINITIONS "${_hdf5_definitions}")
  1061. else()
  1062. if (DEFINED "HDF5_${hdf5_target_name}_LIBRARY")
  1063. set(_hdf5_location "${HDF5_${hdf5_target_name}_LIBRARY}")
  1064. set(_hdf5_location_release "${HDF5_${hdf5_target_name}_LIBRARY_RELEASE}")
  1065. set(_hdf5_location_debug "${HDF5_${hdf5_target_name}_LIBRARY_DEBUG}")
  1066. elseif (DEFINED "HDF5_${hdf5_lang}_HL_LIBRARY")
  1067. set(_hdf5_location "${HDF5_${hdf5_lang}_HL_LIBRARY}")
  1068. set(_hdf5_location_release "${HDF5_${hdf5_lang}_HL_LIBRARY_RELEASE}")
  1069. set(_hdf5_location_debug "${HDF5_${hdf5_lang}_HL_LIBRARY_DEBUG}")
  1070. elseif (DEFINED "HDF5_${hdf5_lang}_LIBRARY_${hdf5_target_name}")
  1071. set(_hdf5_location "${HDF5_${hdf5_lang}_LIBRARY_${hdf5_target_name}}")
  1072. elseif (hdf5_alt_target_name AND DEFINED "HDF5_${hdf5_lang}_LIBRARY_${hdf5_alt_target_name}")
  1073. set(_hdf5_location "${HDF5_${hdf5_lang}_LIBRARY_${hdf5_alt_target_name}}")
  1074. else ()
  1075. # Error if we still don't have the location.
  1076. message(SEND_ERROR
  1077. "HDF5 was found, but a different variable was set which contains "
  1078. "the location of the `hdf5::${hdf5_target_name}` library.")
  1079. endif ()
  1080. add_library("hdf5::${hdf5_target_name}" UNKNOWN IMPORTED)
  1081. string(REPLACE "-D" "" _hdf5_definitions "${HDF5_${hdf5_lang}_HL_DEFINITIONS}")
  1082. set_target_properties("hdf5::${hdf5_target_name}" PROPERTIES
  1083. INTERFACE_INCLUDE_DIRECTORIES "${HDF5_${hdf5_lang}_HL_INCLUDE_DIRS}"
  1084. INTERFACE_COMPILE_DEFINITIONS "${_hdf5_definitions}")
  1085. if (_hdf5_location_release)
  1086. set_property(TARGET "hdf5::${hdf5_target_name}" APPEND PROPERTY
  1087. IMPORTED_CONFIGURATIONS RELEASE)
  1088. set_property(TARGET "hdf5::${hdf5_target_name}" PROPERTY
  1089. IMPORTED_LOCATION_RELEASE "${_hdf5_location_release}")
  1090. endif()
  1091. if (_hdf5_location_debug)
  1092. set_property(TARGET "hdf5::${hdf5_target_name}" APPEND PROPERTY
  1093. IMPORTED_CONFIGURATIONS DEBUG)
  1094. set_property(TARGET "hdf5::${hdf5_target_name}" PROPERTY
  1095. IMPORTED_LOCATION_DEBUG "${_hdf5_location_debug}")
  1096. endif()
  1097. if (NOT _hdf5_location_release AND NOT _hdf5_location_debug)
  1098. set_property(TARGET "hdf5::${hdf5_target_name}" PROPERTY
  1099. IMPORTED_LOCATION "${_hdf5_location}")
  1100. endif()
  1101. if (_hdf5_libtype STREQUAL "SHARED")
  1102. set_property(TARGET "hdf5::${hdf5_target_name}" APPEND
  1103. PROPERTY
  1104. INTERFACE_COMPILE_DEFINITIONS H5_BUILT_AS_DYNAMIC_LIB)
  1105. elseif (_hdf5_libtype STREQUAL "STATIC")
  1106. set_property(TARGET "hdf5::${hdf5_target_name}" APPEND
  1107. PROPERTY
  1108. INTERFACE_COMPILE_DEFINITIONS H5_BUILT_AS_STATIC_LIB)
  1109. endif ()
  1110. unset(_hdf5_definitions)
  1111. unset(_hdf5_libtype)
  1112. unset(_hdf5_location)
  1113. endif ()
  1114. endif ()
  1115. endforeach ()
  1116. unset(hdf5_lang)
  1117. if (HDF5_DIFF_EXECUTABLE AND NOT TARGET hdf5::h5diff)
  1118. add_executable(hdf5::h5diff IMPORTED)
  1119. set_target_properties(hdf5::h5diff PROPERTIES
  1120. IMPORTED_LOCATION "${HDF5_DIFF_EXECUTABLE}")
  1121. endif ()
  1122. endif ()
  1123. if (HDF5_FIND_DEBUG)
  1124. message(STATUS "HDF5_DIR: ${HDF5_DIR}")
  1125. message(STATUS "HDF5_DEFINITIONS: ${HDF5_DEFINITIONS}")
  1126. message(STATUS "HDF5_INCLUDE_DIRS: ${HDF5_INCLUDE_DIRS}")
  1127. message(STATUS "HDF5_LIBRARIES: ${HDF5_LIBRARIES}")
  1128. message(STATUS "HDF5_HL_LIBRARIES: ${HDF5_HL_LIBRARIES}")
  1129. foreach(_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  1130. message(STATUS "HDF5_${_lang}_DEFINITIONS: ${HDF5_${_lang}_DEFINITIONS}")
  1131. message(STATUS "HDF5_${_lang}_INCLUDE_DIR: ${HDF5_${_lang}_INCLUDE_DIR}")
  1132. message(STATUS "HDF5_${_lang}_INCLUDE_DIRS: ${HDF5_${_lang}_INCLUDE_DIRS}")
  1133. message(STATUS "HDF5_${_lang}_LIBRARY: ${HDF5_${_lang}_LIBRARY}")
  1134. message(STATUS "HDF5_${_lang}_LIBRARIES: ${HDF5_${_lang}_LIBRARIES}")
  1135. message(STATUS "HDF5_${_lang}_HL_LIBRARY: ${HDF5_${_lang}_HL_LIBRARY}")
  1136. message(STATUS "HDF5_${_lang}_HL_LIBRARIES: ${HDF5_${_lang}_HL_LIBRARIES}")
  1137. endforeach()
  1138. message(STATUS "Defined targets (if any):")
  1139. foreach(_lang IN ITEMS "" "_cpp" "_fortran")
  1140. foreach(_hl IN ITEMS "" "_hl")
  1141. foreach(_prefix IN ITEMS "hdf5::" "")
  1142. foreach(_suffix IN ITEMS "-static" "-shared" "")
  1143. set (_target ${_prefix}hdf5${_hl}${_lang}${_suffix})
  1144. if (TARGET ${_target})
  1145. message(STATUS "... ${_target}")
  1146. else()
  1147. #message(STATUS "... ${_target} does not exist")
  1148. endif()
  1149. endforeach()
  1150. endforeach()
  1151. endforeach()
  1152. endforeach()
  1153. endif()
  1154. unset(_lang)
  1155. unset(_HDF5_NEED_TO_SEARCH)
  1156. cmake_policy(POP)