FindHDF5.cmake 50 KB

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