FindHDF5.cmake 33 KB

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