FindHDF5.cmake 46 KB

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