FindHDF5.cmake 49 KB

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