FindHDF5.cmake 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. #.rst:
  2. # FindHDF5
  3. # --------
  4. #
  5. # Find HDF5, a library for reading and writing self describing array data.
  6. #
  7. #
  8. #
  9. # This module invokes the HDF5 wrapper compiler that should be installed
  10. # alongside HDF5. Depending upon the HDF5 Configuration, the wrapper
  11. # compiler is called either h5cc or h5pcc. If this succeeds, the module
  12. # will then call the compiler with the -show argument to see what flags
  13. # are used when compiling an HDF5 client application.
  14. #
  15. # The module will optionally accept the COMPONENTS argument. If no
  16. # COMPONENTS are specified, then the find module will default to finding
  17. # only the HDF5 C library. If one or more COMPONENTS are specified, the
  18. # module will attempt to find the language bindings for the specified
  19. # components. The only valid components are C, CXX, Fortran, HL, and
  20. # Fortran_HL. If the COMPONENTS argument is not given, the module will
  21. # attempt to find only the C bindings.
  22. #
  23. # On UNIX systems, 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. #
  29. # To provide the module with a hint about where to find your HDF5
  30. # installation, you can set the environment variable HDF5_ROOT. The
  31. # Find module will then look in this path when searching for HDF5
  32. # executables, paths, and libraries.
  33. #
  34. # Both the serial and parallel HDF5 wrappers are considered and the first
  35. # directory to contain either one will be used. In the event that both appear
  36. # in the same directory the serial version is preferentially selected. This
  37. # behavior can be reversed by setting the variable HDF5_PREFER_PARALLEL to
  38. # true.
  39. #
  40. # In addition to finding the includes and libraries required to compile
  41. # an HDF5 client application, this module also makes an effort to find
  42. # tools that come with the HDF5 distribution that may be useful for
  43. # regression testing.
  44. #
  45. # This module will define the following variables:
  46. #
  47. # ::
  48. #
  49. # HDF5_FOUND - true if HDF5 was found on the system
  50. # HDF5_VERSION - HDF5 version in format Major.Minor.Release
  51. # HDF5_INCLUDE_DIRS - Location of the hdf5 includes
  52. # HDF5_INCLUDE_DIR - Location of the hdf5 includes (deprecated)
  53. # HDF5_DEFINITIONS - Required compiler definitions for HDF5
  54. # HDF5_LIBRARIES - Required libraries for all requested bindings
  55. # HDF5_HL_LIBRARIES - Required libraries for the HDF5 high level API for all
  56. # bindings, if the HL component is enabled
  57. #
  58. # Available components are: C CXX Fortran and HL. For each enabled language
  59. # binding, a corresponding HDF5_${LANG}_LIBRARIES variable will be defined.
  60. # If the HL component is enabled, then an HDF5_${LANG}_HL_LIBRARIES will
  61. # also be defined. With all components enabled, the following variables will be defined:
  62. #
  63. # ::
  64. #
  65. # HDF5_C_LIBRARIES - Required libraries for the HDF5 C bindings
  66. # HDF5_CXX_LIBRARIES - Required libraries for the HDF5 C++ bindings
  67. # HDF5_Fortran_LIBRARIES - Required libraries for the HDF5 Fortran bindings
  68. # HDF5_C_HL_LIBRARIES - Required libraries for the high level C bindings
  69. # HDF5_CXX_HL_LIBRARIES - Required libraries for the high level C++ bindings
  70. # HDF5_Fortran_HL_LIBRARIES - Required libraries for the high level Fortran
  71. # bindings.
  72. #
  73. # HDF5_IS_PARALLEL - Whether or not HDF5 was found with parallel IO support
  74. # HDF5_C_COMPILER_EXECUTABLE - the path to the HDF5 C wrapper compiler
  75. # HDF5_CXX_COMPILER_EXECUTABLE - the path to the HDF5 C++ wrapper compiler
  76. # HDF5_Fortran_COMPILER_EXECUTABLE - the path to the HDF5 Fortran wrapper compiler
  77. # HDF5_C_COMPILER_EXECUTABLE_NO_INTERROGATE - path to the primary C compiler
  78. # which is also the HDF5 wrapper
  79. # HDF5_CXX_COMPILER_EXECUTABLE_NO_INTERROGATE - path to the primary C++
  80. # compiler which is also
  81. # the HDF5 wrapper
  82. # HDF5_Fortran_COMPILER_EXECUTABLE_NO_INTERROGATE - path to the primary
  83. # Fortran compiler which
  84. # is also the HDF5 wrapper
  85. # HDF5_DIFF_EXECUTABLE - the path to the HDF5 dataset comparison tool
  86. #
  87. # The following variable can be set to guide the search for HDF5 libraries and includes:
  88. #
  89. # HDF5_ROOT
  90. #=============================================================================
  91. # Copyright 2015 Axel Huebl, Helmholtz-Zentrum Dresden - Rossendorf
  92. # Copyright 2009 Kitware, Inc.
  93. #
  94. # Distributed under the OSI-approved BSD License (the "License");
  95. # see accompanying file Copyright.txt for details.
  96. #
  97. # This software is distributed WITHOUT ANY WARRANTY; without even the
  98. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  99. # See the License for more information.
  100. #=============================================================================
  101. # (To distribute this file outside of CMake, substitute the full
  102. # License text for the above reference.)
  103. # This module is maintained by Will Dicharry <[email protected]>.
  104. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  105. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  106. # List of the valid HDF5 components
  107. set(HDF5_VALID_LANGUAGE_BINDINGS C CXX Fortran)
  108. # Validate the list of find components.
  109. set(HDF5_LANGUAGE_BINDINGS)
  110. if(NOT HDF5_FIND_COMPONENTS)
  111. get_property(__langs GLOBAL PROPERTY ENABLED_LANGUAGES)
  112. foreach(__lang IN LISTS __langs)
  113. if(__lang MATCHES "^(C|CXX|Fortran)$")
  114. list(APPEND HDF5_LANGUAGE_BINDINGS ${__lang})
  115. endif()
  116. endforeach()
  117. set(FIND_HL ON)
  118. else()
  119. # add the extra specified components, ensuring that they are valid.
  120. set(FIND_HL OFF)
  121. foreach(component IN LISTS HDF5_FIND_COMPONENTS)
  122. list(FIND HDF5_VALID_LANGUAGE_BINDINGS ${component} component_location)
  123. if(NOT component_location EQUAL -1)
  124. list(APPEND HDF5_LANGUAGE_BINDINGS ${component})
  125. elseif(component STREQUAL "HL")
  126. set(FIND_HL ON)
  127. elseif(component STREQUAL "Fortran_HL") # only for compatibility
  128. list(APPEND HDF5_LANGUAGE_BINDINGS Fortran)
  129. set(FIND_HL ON)
  130. else()
  131. message(FATAL_ERROR "${component} is not a valid HDF5 component.")
  132. endif()
  133. endforeach()
  134. if(NOT HDF5_LANGUAGE_BINDINGS)
  135. get_property(__langs GLOBAL PROPERTY ENABLED_LANGUAGES)
  136. foreach(__lang IN LISTS __langs)
  137. if(__lang MATCHES "^(C|CXX|Fortran)$")
  138. list(APPEND HDF5_LANGUAGE_BINDINGS ${__lang})
  139. endif()
  140. endforeach()
  141. endif()
  142. list(REMOVE_DUPLICATES HDF5_LANGUAGE_BINDINGS)
  143. endif()
  144. # Determine whether to search for serial or parallel executable first
  145. if(HDF5_PREFER_PARALLEL)
  146. set(HDF5_C_COMPILER_NAMES h5pcc h5cc)
  147. set(HDF5_CXX_COMPILER_NAMES h5pc++ h5c++)
  148. set(HDF5_Fortran_COMPILER_NAMES h5pfc h5fc)
  149. else()
  150. set(HDF5_C_COMPILER_NAMES h5cc h5pcc)
  151. set(HDF5_CXX_COMPILER_NAMES h5c++ h5pc++)
  152. set(HDF5_Fortran_COMPILER_NAMES h5fc h5pfc)
  153. endif()
  154. # We may have picked up some duplicates in various lists during the above
  155. # process for the language bindings (both the C and C++ bindings depend on
  156. # libz for example). Remove the duplicates. It appears that the default
  157. # CMake behavior is to remove duplicates from the end of a list. However,
  158. # for link lines, this is incorrect since unresolved symbols are searched
  159. # for down the link line. Therefore, we reverse the list, remove the
  160. # duplicates, and then reverse it again to get the duplicates removed from
  161. # the beginning.
  162. macro(_HDF5_remove_duplicates_from_beginning _list_name)
  163. if(${_list_name})
  164. list(REVERSE ${_list_name})
  165. list(REMOVE_DUPLICATES ${_list_name})
  166. list(REVERSE ${_list_name})
  167. endif()
  168. endmacro()
  169. # Test first if the current compilers automatically wrap HDF5
  170. function(_HDF5_test_regular_compiler_C success version)
  171. set(scratch_directory
  172. ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
  173. if(NOT ${success} OR
  174. NOT EXISTS ${scratch_directory}/compiler_has_h5_c)
  175. set(test_file ${scratch_directory}/cmake_hdf5_test.c)
  176. file(WRITE ${test_file}
  177. "#include <hdf5.h>\n"
  178. "#include <hdf5_hl.h>\n"
  179. "int main(void) {\n"
  180. " char const* info_ver = \"INFO\" \":\" H5_VERSION;\n"
  181. " hid_t fid;\n"
  182. " fid = H5Fcreate(\"foo.h5\",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);\n"
  183. " return 0;\n"
  184. "}")
  185. try_compile(${success} ${scratch_directory} ${test_file}
  186. COPY_FILE ${scratch_directory}/compiler_has_h5_c
  187. )
  188. endif()
  189. if(${success})
  190. file(STRINGS ${scratch_directory}/compiler_has_h5_c INFO_VER
  191. REGEX "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  192. )
  193. string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  194. INFO_VER "${INFO_VER}"
  195. )
  196. set(${version} ${CMAKE_MATCH_1})
  197. if(CMAKE_MATCH_3)
  198. set(${version} ${HDF5_CXX_VERSION}.${CMAKE_MATCH_3})
  199. endif()
  200. set(${version} ${${version}} PARENT_SCOPE)
  201. endif()
  202. endfunction()
  203. function(_HDF5_test_regular_compiler_CXX success version)
  204. set(scratch_directory ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
  205. if(NOT ${success} OR
  206. NOT EXISTS ${scratch_directory}/compiler_has_h5_cxx)
  207. set(test_file ${scratch_directory}/cmake_hdf5_test.cxx)
  208. file(WRITE ${test_file}
  209. "#include <H5Cpp.h>\n"
  210. "#ifndef H5_NO_NAMESPACE\n"
  211. "using namespace H5;\n"
  212. "#endif\n"
  213. "int main(int argc, char **argv) {\n"
  214. " char const* info_ver = \"INFO\" \":\" H5_VERSION;\n"
  215. " H5File file(\"foo.h5\", H5F_ACC_TRUNC);\n"
  216. " return 0;\n"
  217. "}")
  218. try_compile(${success} ${scratch_directory} ${test_file}
  219. COPY_FILE ${scratch_directory}/compiler_has_h5_cxx
  220. )
  221. endif()
  222. if(${success})
  223. file(STRINGS ${scratch_directory}/compiler_has_h5_cxx INFO_VER
  224. REGEX "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  225. )
  226. string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  227. INFO_VER "${INFO_VER}"
  228. )
  229. set(${version} ${CMAKE_MATCH_1})
  230. if(CMAKE_MATCH_3)
  231. set(${version} ${HDF5_CXX_VERSION}.${CMAKE_MATCH_3})
  232. endif()
  233. set(${version} ${${version}} PARENT_SCOPE)
  234. endif()
  235. endfunction()
  236. function(_HDF5_test_regular_compiler_Fortran success)
  237. if(NOT ${success})
  238. set(scratch_directory
  239. ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
  240. set(test_file ${scratch_directory}/cmake_hdf5_test.f90)
  241. file(WRITE ${test_file}
  242. "program hdf5_hello\n"
  243. " use hdf5\n"
  244. " use h5lt\n"
  245. " use h5ds\n"
  246. " integer error\n"
  247. " call h5open_f(error)\n"
  248. " call h5close_f(error)\n"
  249. "end\n")
  250. try_compile(${success} ${scratch_directory} ${test_file})
  251. endif()
  252. endfunction()
  253. # Invoke the HDF5 wrapper compiler. The compiler return value is stored to the
  254. # return_value argument, the text output is stored to the output variable.
  255. macro( _HDF5_invoke_compiler language output return_value version)
  256. set(${version})
  257. exec_program( ${HDF5_${language}_COMPILER_EXECUTABLE}
  258. ARGS -show
  259. OUTPUT_VARIABLE ${output}
  260. RETURN_VALUE ${return_value}
  261. )
  262. if(NOT ${${return_value}} EQUAL 0)
  263. message(STATUS
  264. "Unable to determine HDF5 ${language} flags from HDF5 wrapper.")
  265. endif()
  266. exec_program( ${HDF5_${language}_COMPILER_EXECUTABLE}
  267. ARGS -showconfig
  268. OUTPUT_VARIABLE config_output
  269. RETURN_VALUE config_return
  270. )
  271. if(NOT ${return_value} EQUAL 0)
  272. message( STATUS
  273. "Unable to determine HDF5 ${language} version from HDF5 wrapper.")
  274. endif()
  275. string(REGEX MATCH "HDF5 Version: ([a-zA-Z0-9\\.\\-]*)" version_match "${config_output}")
  276. if(version_match)
  277. string(REPLACE "HDF5 Version: " "" ${version} "${version_match}")
  278. string(REPLACE "-patch" "." ${version} "${${version}}")
  279. endif()
  280. endmacro()
  281. # Parse a compile line for definitions, includes, library paths, and libraries.
  282. macro( _HDF5_parse_compile_line
  283. compile_line_var
  284. include_paths
  285. definitions
  286. library_paths
  287. libraries
  288. libraries_hl)
  289. # Match the include paths
  290. string( REGEX MATCHALL "-I([^\" ]+)" include_path_flags
  291. "${${compile_line_var}}"
  292. )
  293. foreach( IPATH ${include_path_flags} )
  294. string( REGEX REPLACE "^-I" "" IPATH ${IPATH} )
  295. string( REPLACE "//" "/" IPATH ${IPATH} )
  296. list( APPEND ${include_paths} ${IPATH} )
  297. endforeach()
  298. # Match the definitions
  299. string( REGEX MATCHALL "-D[^ ]*" definition_flags "${${compile_line_var}}" )
  300. foreach( DEF ${definition_flags} )
  301. list( APPEND ${definitions} ${DEF} )
  302. endforeach()
  303. # Match the library paths
  304. string( REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" library_path_flags
  305. "${${compile_line_var}}"
  306. )
  307. foreach( LPATH ${library_path_flags} )
  308. string( REGEX REPLACE "^-L" "" LPATH ${LPATH} )
  309. string( REPLACE "//" "/" LPATH ${LPATH} )
  310. list( APPEND ${library_paths} ${LPATH} )
  311. endforeach()
  312. # now search for the library names specified in the compile line (match -l...)
  313. # match only -l's preceded by a space or comma
  314. # this is to exclude directory names like xxx-linux/
  315. string( REGEX MATCHALL "[, ]-l([^\", ]+)" library_name_flags
  316. "${${compile_line_var}}" )
  317. # strip the -l from all of the library flags and add to the search list
  318. foreach( LIB ${library_name_flags} )
  319. string( REGEX REPLACE "^[, ]-l" "" LIB ${LIB} )
  320. if(LIB MATCHES ".*_hl")
  321. list(APPEND ${libraries_hl} ${LIB})
  322. else()
  323. list(APPEND ${libraries} ${LIB})
  324. endif()
  325. endforeach()
  326. endmacro()
  327. # Try to find HDF5 using an installed hdf5-config.cmake
  328. if(NOT HDF5_FOUND AND NOT HDF5_ROOT)
  329. find_package(HDF5 QUIET NO_MODULE)
  330. if( HDF5_FOUND)
  331. set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR})
  332. set(HDF5_LIBRARIES)
  333. set(HDF5_C_TARGET hdf5)
  334. set(HDF5_C_HL_TARGET hdf5_hl)
  335. set(HDF5_CXX_TARGET hdf5_cpp)
  336. set(HDF5_CXX_HL_TARGET hdf5_hl_cpp)
  337. set(HDF5_Fortran_TARGET hdf5_fortran)
  338. set(HDF5_Fortran_HL_TARGET hdf5_hl_fortran)
  339. if(HDF5_USE_STATIC_LIBRARIES)
  340. set(_suffix "-static")
  341. else()
  342. set(_suffix "-shared")
  343. endif()
  344. foreach(_lang ${HDF5_LANGUAGE_BINDINGS})
  345. get_target_property(_lang_location ${HDF5_${_component}_TARGET}${_suffix} LOCATION)
  346. if( _lang_location )
  347. set(HDF5_${_lang}_LIBRARY ${_lang_location} CACHE PATH
  348. "HDF5 ${_lang} library" )
  349. mark_as_advanced(HDF5_${_lang}_LIBRARY)
  350. list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_LIBRARY})
  351. endif()
  352. if(FIND_HL)
  353. get_target_property(_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
  354. if( _lang_location )
  355. set(HDF5_${_lang}_HL_LIBRARY ${_lang_location} CACHE PATH
  356. "HDF5 ${_lang} HL library" )
  357. mark_as_advanced(HDF5_${_lang}_LIBRARY)
  358. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARY})
  359. endif()
  360. endif()
  361. endforeach()
  362. endif()
  363. endif()
  364. if(NOT HDF5_FOUND AND NOT HDF5_ROOT)
  365. set(_HDF5_NEED_TO_SEARCH False)
  366. set(HDF5_COMPILER_NO_INTERROGATE True)
  367. # Only search for languages we've enabled
  368. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  369. # First check to see if our regular compiler is one of wrappers
  370. if(__lang STREQUAL "C")
  371. _HDF5_test_regular_compiler_C(
  372. HDF5_${__lang}_COMPILER_NO_INTERROGATE
  373. HDF5_${__lang}_VERSION)
  374. elseif(__lang STREQUAL "CXX")
  375. _HDF5_test_regular_compiler_CXX(
  376. HDF5_${__lang}_COMPILER_NO_INTERROGATE
  377. HDF5_${__lang}_VERSION)
  378. elseif(__lang STREQUAL "Fortran")
  379. _HDF5_test_regular_compiler_Fortran(
  380. HDF5_${__lang}_COMPILER_NO_INTERROGATE)
  381. else()
  382. continue()
  383. endif()
  384. if(HDF5_${__lang}_COMPILER_NO_INTERROGATE)
  385. message(STATUS "HDF5: Using hdf5 compiler wrapper for all ${__lang} compiling")
  386. set(HDF5_${__lang}_FOUND True)
  387. set(HDF5_${__lang}_COMPILER_EXECUTABLE_NO_INTERROGATE
  388. "${CMAKE_${__lang}_COMPILER}"
  389. CACHE FILEPATH "HDF5 ${__lang} compiler wrapper")
  390. set(HDF5_${__lang}_DEFINITIONS)
  391. set(HDF5_${__lang}_INCLUDE_DIRS)
  392. set(HDF5_${__lang}_LIBRARIES)
  393. mark_as_advanced(HDF5_${__lang}_COMPILER_EXECUTABLE_NO_INTERROGATE)
  394. mark_as_advanced(HDF5_${__lang}_DEFINITIONS)
  395. mark_as_advanced(HDF5_${__lang}_INCLUDE_DIRS)
  396. mark_as_advanced(HDF5_${__lang}_LIBRARIES)
  397. mark_as_advanced(HDF5_${__lang}_HL_LIBRARIES)
  398. else()
  399. set(HDF5_COMPILER_NO_INTERROGATE False)
  400. # If this language isn't using the wrapper, then try to seed the
  401. # search options with the wrapper
  402. find_program(HDF5_${__lang}_COMPILER_EXECUTABLE
  403. NAMES ${HDF5_${__lang}_COMPILER_NAMES} NAMES_PER_DIR
  404. HINTS ENV HDF5_ROOT
  405. PATH_SUFFIXES bin Bin
  406. DOC "HDF5 ${__lang} Wrapper compiler. Used only to detect HDF5 compile flags."
  407. )
  408. mark_as_advanced( HDF5_${__lang}_COMPILER_EXECUTABLE )
  409. unset(HDF5_${__lang}_COMPILER_NAMES)
  410. if(HDF5_${__lang}_COMPILER_EXECUTABLE)
  411. _HDF5_invoke_compiler(${__lang} HDF5_${__lang}_COMPILE_LINE
  412. HDF5_${__lang}_RETURN_VALUE HDF5_${__lang}_VERSION)
  413. if(HDF5_${__lang}_RETURN_VALUE EQUAL 0)
  414. message(STATUS "HDF5: Using hdf5 compiler wrapper to determine ${__lang} configuration")
  415. _HDF5_parse_compile_line( HDF5_${__lang}_COMPILE_LINE
  416. HDF5_${__lang}_INCLUDE_DIRS
  417. HDF5_${__lang}_DEFINITIONS
  418. HDF5_${__lang}_LIBRARY_DIRS
  419. HDF5_${__lang}_LIBRARY_NAMES
  420. HDF5_${__lang}_HL_LIBRARY_NAMES
  421. )
  422. set(HDF5_${__lang}_LIBRARIES)
  423. foreach(L IN LISTS HDF5_${__lang}_LIBRARY_NAMES)
  424. find_library(HDF5_${__lang}_LIBRARY_${L} ${L} ${HDF5_${__lang}_LIBRARY_DIRS})
  425. if(HDF5_${__lang}_LIBRARY_${L})
  426. list(APPEND HDF5_${__lang}_LIBRARIES ${HDF5_${__lang}_LIBRARY_${L}})
  427. else()
  428. list(APPEND HDF5_${__lang}_LIBRARIES ${L})
  429. endif()
  430. endforeach()
  431. if(FIND_HL)
  432. set(HDF5_${__lang}_HL_LIBRARIES)
  433. foreach(L IN LISTS HDF5_${__lang}_HL_LIBRARY_NAMES)
  434. find_library(HDF5_${__lang}_LIBRARY_${L} ${L} ${HDF5_${__lang}_LIBRARY_DIRS})
  435. if(HDF5_${__lang}_LIBRARY_${L})
  436. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${HDF5_${__lang}_LIBRARY_${L}})
  437. else()
  438. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${L})
  439. endif()
  440. endforeach()
  441. endif()
  442. set(HDF5_${__lang}_FOUND True)
  443. mark_as_advanced(HDF5_${__lang}_DEFINITIONS)
  444. mark_as_advanced(HDF5_${__lang}_INCLUDE_DIRS)
  445. mark_as_advanced(HDF5_${__lang}_LIBRARIES)
  446. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_DEFINITIONS)
  447. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_INCLUDE_DIRS)
  448. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_LIBRARIES)
  449. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_HL_LIBRARIES)
  450. else()
  451. set(_HDF5_NEED_TO_SEARCH True)
  452. endif()
  453. else()
  454. set(_HDF5_NEED_TO_SEARCH True)
  455. endif()
  456. endif()
  457. if(HDF5_${__lang}_VERSION)
  458. if(NOT HDF5_VERSION)
  459. set(HDF5_VERSION ${HDF5_${__lang}_VERSION})
  460. elseif(NOT HDF5_VERSION VERSION_EQUAL HDF5_${__lang}_VERSION)
  461. message(WARNING "HDF5 Version found for language ${__lang}, ${HDF5_${__lang}_VERSION} is different than previously found version ${HDF5_VERSION}")
  462. endif()
  463. endif()
  464. endforeach()
  465. else()
  466. set(_HDF5_NEED_TO_SEARCH True)
  467. endif()
  468. if(NOT HDF5_FOUND AND HDF5_COMPILER_NO_INTERROGATE)
  469. # No arguments necessary, all languages can use the compiler wrappers
  470. set(HDF5_FOUND True)
  471. set(HDF5_METHOD "Included by compiler wrappers")
  472. set(HDF5_REQUIRED_VARS HDF5_METHOD)
  473. elseif(NOT HDF5_FOUND AND NOT _HDF5_NEED_TO_SEARCH)
  474. # Compiler wrappers aren't being used by the build but were found and used
  475. # to determine necessary include and library flags
  476. set(HDF5_INCLUDE_DIRS)
  477. set(HDF5_LIBRARIES)
  478. set(HDF5_HL_LIBRARIES)
  479. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  480. if(HDF5_${__lang}_FOUND)
  481. if(NOT HDF5_${__lang}_COMPILER_NO_INTERROGATE)
  482. list(APPEND HDF5_DEFINITIONS ${HDF5_${__lang}_DEFINITIONS})
  483. list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${__lang}_INCLUDE_DIRS})
  484. list(APPEND HDF5_LIBRARIES ${HDF5_${__lang}_LIBRARIES})
  485. if(FIND_HL)
  486. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${__lang}_HL_LIBRARIES})
  487. endif()
  488. endif()
  489. endif()
  490. endforeach()
  491. _HDF5_remove_duplicates_from_beginning(HDF5_DEFINITIONS)
  492. _HDF5_remove_duplicates_from_beginning(HDF5_INCLUDE_DIRS)
  493. _HDF5_remove_duplicates_from_beginning(HDF5_LIBRARIES)
  494. _HDF5_remove_duplicates_from_beginning(HDF5_HL_LIBRARIES)
  495. set(HDF5_FOUND True)
  496. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES)
  497. if(FIND_HL)
  498. list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
  499. endif()
  500. endif()
  501. find_program( HDF5_DIFF_EXECUTABLE
  502. NAMES h5diff
  503. HINTS ENV HDF5_ROOT
  504. PATH_SUFFIXES bin Bin
  505. DOC "HDF5 file differencing tool." )
  506. mark_as_advanced( HDF5_DIFF_EXECUTABLE )
  507. if( NOT HDF5_FOUND )
  508. # seed the initial lists of libraries to find with items we know we need
  509. set(HDF5_C_LIBRARY_NAMES hdf5)
  510. set(HDF5_C_HL_LIBRARY_NAMES hdf5_hl)
  511. set(HDF5_CXX_LIBRARY_NAMES hdf5_cpp ${HDF5_C_LIBRARY_NAMES})
  512. set(HDF5_CXX_HL_LIBRARY_NAMES hdf5_hl_cpp ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_CXX_LIBRARY_NAMES})
  513. set(HDF5_Fortran_LIBRARY_NAMES hdf5_fortran ${HDF5_C_LIBRARY_NAMES})
  514. set(HDF5_Fortran_HL_LIBRARY_NAMES hdf5_hl_fortran ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_Fortran_LIBRARY_NAMES})
  515. if(HDF5_ROOT)
  516. set(SEARCH_OPTS NO_DEFAULT_PATH)
  517. endif()
  518. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  519. # find the HDF5 include directories
  520. if(LANGUAGE STREQUAL "Fortran")
  521. set(HDF5_INCLUDE_FILENAME hdf5.mod)
  522. elseif(LANGUAGE STREQUAL "CXX")
  523. set(HDF5_INCLUDE_FILENAME H5Cpp.h)
  524. else()
  525. set(HDF5_INCLUDE_FILENAME hdf5.h)
  526. endif()
  527. find_path(HDF5_${__lang}_INCLUDE_DIR ${HDF5_INCLUDE_FILENAME}
  528. HINTS ${HDF5_ROOT} ENV HDF5_ROOT
  529. PATHS $ENV{HOME}/.local/include
  530. PATH_SUFFIXES include Include
  531. ${SEARCH_OPTS}
  532. )
  533. mark_as_advanced(HDF5_${LANGUAGE}_INCLUDE_DIR)
  534. list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${__lang}_INCLUDE_DIR})
  535. # find the HDF5 libraries
  536. foreach(LIB IN LISTS HDF5_${__lang}_LIBRARY_NAMES)
  537. if(UNIX AND HDF5_USE_STATIC_LIBRARIES)
  538. # According to bug 1643 on the CMake bug tracker, this is the
  539. # preferred method for searching for a static library.
  540. # See https://cmake.org/Bug/view.php?id=1643. We search
  541. # first for the full static library name, but fall back to a
  542. # generic search on the name if the static search fails.
  543. set( THIS_LIBRARY_SEARCH_DEBUG
  544. lib${LIB}d.a lib${LIB}_debug.a ${LIB}d ${LIB}_debug
  545. lib${LIB}d-static.a lib${LIB}_debug-static.a ${LIB}d-static ${LIB}_debug-static )
  546. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a ${LIB} lib${LIB}-static.a ${LIB}-static)
  547. else()
  548. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_debug ${LIB}d-shared ${LIB}_debug-shared)
  549. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
  550. endif()
  551. find_library(HDF5_${LIB}_LIBRARY_DEBUG
  552. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  553. HINTS ${HDF5_ROOT} ENV HDF5_ROOT PATH_SUFFIXES lib Lib
  554. ${SEARCH_OPTS}
  555. )
  556. find_library( HDF5_${LIB}_LIBRARY_RELEASE
  557. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  558. HINTS ${HDF5_ROOT} ENV HDF5_ROOT PATH_SUFFIXES lib Lib
  559. ${SEARCH_OPTS}
  560. )
  561. select_library_configurations( HDF5_${LIB} )
  562. list(APPEND HDF5_${__lang}_LIBRARIES ${HDF5_${LIB}_LIBRARY})
  563. eNdforeach()
  564. # Append the libraries for this language binding to the list of all
  565. # required libraries.
  566. list(APPEND HDF5_LIBRARIES ${HDF5_${__lang}_LIBRARIES})
  567. if(FIND_HL)
  568. foreach(LIB IN LISTS HDF5_${__lang}_HL_LIBRARY_NAMES)
  569. if(UNIX AND HDF5_USE_STATIC_LIBRARIES)
  570. # According to bug 1643 on the CMake bug tracker, this is the
  571. # preferred method for searching for a static library.
  572. # See https://cmake.org/Bug/view.php?id=1643. We search
  573. # first for the full static library name, but fall back to a
  574. # generic search on the name if the static search fails.
  575. set( THIS_LIBRARY_SEARCH_DEBUG
  576. lib${LIB}d.a lib${LIB}_debug.a ${LIB}d ${LIB}_debug
  577. lib${LIB}d-static.a lib${LIB}_debug-static.a ${LIB}d-static ${LIB}_debug-static )
  578. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a ${LIB} lib${LIB}-static.a ${LIB}-static)
  579. else()
  580. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_debug ${LIB}d-shared ${LIB}_debug-shared)
  581. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
  582. endif()
  583. find_library(HDF5_${LIB}_LIBRARY_DEBUG
  584. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  585. HINTS ${HDF5_ROOT} ENV HDF5_ROOT PATH_SUFFIXES lib Lib
  586. ${SEARCH_OPTS}
  587. )
  588. find_library( HDF5_${LIB}_LIBRARY_RELEASE
  589. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  590. HINTS ${HDF5_ROOT} ENV HDF5_ROOT PATH_SUFFIXES lib Lib
  591. ${SEARCH_OPTS}
  592. )
  593. select_library_configurations( HDF5_${LIB} )
  594. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${HDF5_${LIB}_LIBRARY})
  595. endforeach()
  596. # Append the libraries for this language binding to the list of all
  597. # required libraries.
  598. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${__lang}_HL_LIBRARIES})
  599. endif()
  600. endforeach()
  601. _HDF5_remove_duplicates_from_beginning(HDF5_INCLUDE_DIRS)
  602. _HDF5_remove_duplicates_from_beginning(HDF5_LIBRARIES)
  603. _HDF5_remove_duplicates_from_beginning(HDF5_HL_LIBRARIES)
  604. # If the HDF5 include directory was found, open H5pubconf.h to determine if
  605. # HDF5 was compiled with parallel IO support
  606. set( HDF5_IS_PARALLEL FALSE )
  607. set( HDF5_VERSION "" )
  608. foreach( _dir IN LISTS HDF5_INCLUDE_DIRS )
  609. foreach(_hdr "${_dir}/H5pubconf.h" "${_dir}/H5pubconf-64.h" "${_dir}/H5pubconf-32.h")
  610. if( EXISTS "${_hdr}" )
  611. file( STRINGS "${_hdr}"
  612. HDF5_HAVE_PARALLEL_DEFINE
  613. REGEX "HAVE_PARALLEL 1" )
  614. if( HDF5_HAVE_PARALLEL_DEFINE )
  615. set( HDF5_IS_PARALLEL TRUE )
  616. endif()
  617. unset(HDF5_HAVE_PARALLEL_DEFINE)
  618. file( STRINGS "${_hdr}"
  619. HDF5_VERSION_DEFINE
  620. REGEX "^[ \t]*#[ \t]*define[ \t]+H5_VERSION[ \t]+" )
  621. if( "${HDF5_VERSION_DEFINE}" MATCHES
  622. "H5_VERSION[ \t]+\"([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?\"" )
  623. set( HDF5_VERSION "${CMAKE_MATCH_1}" )
  624. if( CMAKE_MATCH_3 )
  625. set( HDF5_VERSION ${HDF5_VERSION}.${CMAKE_MATCH_3})
  626. endif()
  627. endif()
  628. unset(HDF5_VERSION_DEFINE)
  629. endif()
  630. endforeach()
  631. endforeach()
  632. set( HDF5_IS_PARALLEL ${HDF5_IS_PARALLEL} CACHE BOOL
  633. "HDF5 library compiled with parallel IO support" )
  634. mark_as_advanced( HDF5_IS_PARALLEL )
  635. # For backwards compatibility we set HDF5_INCLUDE_DIR to the value of
  636. # HDF5_INCLUDE_DIRS
  637. if( HDF5_INCLUDE_DIRS )
  638. set( HDF5_INCLUDE_DIR "${HDF5_INCLUDE_DIRS}" )
  639. endif()
  640. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
  641. if(FIND_HL)
  642. list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
  643. endif()
  644. endif()
  645. # If HDF5_REQUIRED_VARS is empty at this point, then it's likely that
  646. # something external is trying to explicitly pass already found
  647. # locations
  648. if(NOT HDF5_REQUIRED_VARS)
  649. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
  650. endif()
  651. find_package_handle_standard_args(HDF5
  652. REQUIRED_VARS ${HDF5_REQUIRED_VARS}
  653. VERSION_VAR HDF5_VERSION
  654. )