1
0

FindHDF5.cmake 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. # Match the include paths
  327. set( RE " -I *([^\" ]+|\"[^\"]+\")")
  328. string( REGEX MATCHALL "${RE}" include_path_flags "${${compile_line_var}}")
  329. foreach( IPATH IN LISTS include_path_flags )
  330. string( REGEX REPLACE "${RE}" "\\1" IPATH "${IPATH}" )
  331. list( APPEND ${include_paths} ${IPATH} )
  332. endforeach()
  333. # Match the definitions
  334. set( RE " -D([^ ]*)")
  335. string( REGEX MATCHALL "${RE}" definition_flags "${${compile_line_var}}" )
  336. foreach( DEF IN LISTS definition_flags )
  337. string( STRIP "${DEF}" DEF )
  338. list( APPEND ${definitions} ${DEF} )
  339. endforeach()
  340. # Match the library paths
  341. set( RE " -L *([^\" ]+|\"[^\"]+\")")
  342. string( REGEX MATCHALL "${RE}" library_path_flags "${${compile_line_var}}")
  343. foreach( LPATH IN LISTS library_path_flags )
  344. string( REGEX REPLACE "${RE}" "\\1" LPATH "${LPATH}" )
  345. list( APPEND ${library_paths} ${LPATH} )
  346. endforeach()
  347. # now search for the lib names specified in the compile line (match -l...)
  348. # match only -l's preceded by a space or comma
  349. set( RE " -l *([^\" ]+|\"[^\"]+\")")
  350. string( REGEX MATCHALL "${RE}" library_name_flags "${${compile_line_var}}")
  351. foreach( LNAME IN LISTS library_name_flags )
  352. string( REGEX REPLACE "${RE}" "\\1" LNAME "${LNAME}" )
  353. if(LNAME MATCHES ".*hl")
  354. list(APPEND ${libraries_hl} ${LNAME})
  355. else()
  356. list(APPEND ${libraries} ${LNAME})
  357. endif()
  358. endforeach()
  359. # now search for full library paths with no flags
  360. set( RE " ([^\" ]+|\"[^\"]+\")")
  361. string( REGEX MATCHALL "${RE}" library_name_noflags "${${compile_line_var}}")
  362. foreach( LIB IN LISTS library_name_noflags )
  363. string( REGEX REPLACE "${RE}" "\\1" LIB "${LIB}" )
  364. get_filename_component(LIB "${LIB}" ABSOLUTE)
  365. if(NOT EXISTS ${LIB} OR IS_DIRECTORY ${LIB})
  366. continue()
  367. endif()
  368. get_filename_component(LPATH ${LIB} DIRECTORY)
  369. get_filename_component(LNAME ${LIB} NAME_WE)
  370. string( REGEX REPLACE "^lib" "" LNAME ${LNAME} )
  371. list( APPEND ${library_paths} ${LPATH} )
  372. if(LNAME MATCHES ".*hl")
  373. list(APPEND ${libraries_hl} ${LNAME})
  374. else()
  375. list(APPEND ${libraries} ${LNAME})
  376. endif()
  377. endforeach()
  378. endmacro()
  379. if(NOT HDF5_ROOT)
  380. set(HDF5_ROOT $ENV{HDF5_ROOT})
  381. endif()
  382. # Try to find HDF5 using an installed hdf5-config.cmake
  383. if(NOT HDF5_FOUND AND NOT HDF5_ROOT)
  384. find_package(HDF5 QUIET NO_MODULE)
  385. if( HDF5_FOUND)
  386. set(HDF5_IS_PARALLEL ${HDF5_ENABLE_PARALLEL})
  387. set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR})
  388. set(HDF5_LIBRARIES)
  389. set(HDF5_C_TARGET hdf5)
  390. set(HDF5_C_HL_TARGET hdf5_hl)
  391. set(HDF5_CXX_TARGET hdf5_cpp)
  392. set(HDF5_CXX_HL_TARGET hdf5_hl_cpp)
  393. set(HDF5_Fortran_TARGET hdf5_fortran)
  394. set(HDF5_Fortran_HL_TARGET hdf5_hl_fortran)
  395. if(HDF5_USE_STATIC_LIBRARIES)
  396. set(_suffix "-static")
  397. else()
  398. set(_suffix "-shared")
  399. endif()
  400. foreach(_lang ${HDF5_LANGUAGE_BINDINGS})
  401. #Older versions of hdf5 don't have a static/shared suffix so
  402. #if we detect that occurrence clear the suffix
  403. if(_suffix AND NOT TARGET ${HDF5_${_lang}_TARGET}${_suffix})
  404. if(NOT TARGET ${HDF5_${_lang}_TARGET})
  405. #cant find this component with our without the suffix
  406. #so bail out, and let the following locate HDF5
  407. set(HDF5_FOUND FALSE)
  408. break()
  409. endif()
  410. set(_suffix "")
  411. endif()
  412. get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
  413. if( _lang_location )
  414. set(HDF5_${_lang}_LIBRARY ${_lang_location} CACHE PATH
  415. "HDF5 ${_lang} library" )
  416. mark_as_advanced(HDF5_${_lang}_LIBRARY)
  417. list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_LIBRARY})
  418. set(HDF5_${_lang}_LIBRARIES ${HDF5_${_lang}_LIBRARY})
  419. set(HDF5_${_lang}_FOUND True)
  420. endif()
  421. if(FIND_HL)
  422. get_target_property(_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
  423. if( _lang_hl_location )
  424. set(HDF5_${_lang}_HL_LIBRARY ${_lang_hl_location} CACHE PATH
  425. "HDF5 ${_lang} HL library" )
  426. mark_as_advanced(HDF5_${_lang}_HL_LIBRARY)
  427. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARY})
  428. set(HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARY})
  429. set(HDF5_HL_FOUND True)
  430. endif()
  431. endif()
  432. endforeach()
  433. endif()
  434. endif()
  435. if(NOT HDF5_FOUND AND NOT HDF5_ROOT)
  436. set(_HDF5_NEED_TO_SEARCH False)
  437. set(HDF5_COMPILER_NO_INTERROGATE True)
  438. # Only search for languages we've enabled
  439. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  440. # First check to see if our regular compiler is one of wrappers
  441. if(__lang STREQUAL "C")
  442. _HDF5_test_regular_compiler_C(
  443. HDF5_${__lang}_COMPILER_NO_INTERROGATE
  444. HDF5_${__lang}_VERSION
  445. HDF5_${__lang}_IS_PARALLEL)
  446. elseif(__lang STREQUAL "CXX")
  447. _HDF5_test_regular_compiler_CXX(
  448. HDF5_${__lang}_COMPILER_NO_INTERROGATE
  449. HDF5_${__lang}_VERSION
  450. HDF5_${__lang}_IS_PARALLEL)
  451. elseif(__lang STREQUAL "Fortran")
  452. _HDF5_test_regular_compiler_Fortran(
  453. HDF5_${__lang}_COMPILER_NO_INTERROGATE
  454. HDF5_${__lang}_IS_PARALLEL)
  455. else()
  456. continue()
  457. endif()
  458. if(HDF5_${__lang}_COMPILER_NO_INTERROGATE)
  459. message(STATUS "HDF5: Using hdf5 compiler wrapper for all ${__lang} compiling")
  460. set(HDF5_${__lang}_FOUND True)
  461. set(HDF5_${__lang}_COMPILER_EXECUTABLE_NO_INTERROGATE
  462. "${CMAKE_${__lang}_COMPILER}"
  463. CACHE FILEPATH "HDF5 ${__lang} compiler wrapper")
  464. set(HDF5_${__lang}_DEFINITIONS)
  465. set(HDF5_${__lang}_INCLUDE_DIRS)
  466. set(HDF5_${__lang}_LIBRARIES)
  467. set(HDF5_${__lang}_HL_LIBRARIES)
  468. mark_as_advanced(HDF5_${__lang}_COMPILER_EXECUTABLE_NO_INTERROGATE)
  469. mark_as_advanced(HDF5_${__lang}_DEFINITIONS)
  470. mark_as_advanced(HDF5_${__lang}_INCLUDE_DIRS)
  471. mark_as_advanced(HDF5_${__lang}_LIBRARIES)
  472. mark_as_advanced(HDF5_${__lang}_HL_LIBRARIES)
  473. set(HDF5_${__lang}_FOUND True)
  474. set(HDF5_HL_FOUND True)
  475. else()
  476. set(HDF5_COMPILER_NO_INTERROGATE False)
  477. # If this language isn't using the wrapper, then try to seed the
  478. # search options with the wrapper
  479. find_program(HDF5_${__lang}_COMPILER_EXECUTABLE
  480. NAMES ${HDF5_${__lang}_COMPILER_NAMES} NAMES_PER_DIR
  481. PATH_SUFFIXES bin Bin
  482. DOC "HDF5 ${__lang} Wrapper compiler. Used only to detect HDF5 compile flags."
  483. )
  484. mark_as_advanced( HDF5_${__lang}_COMPILER_EXECUTABLE )
  485. unset(HDF5_${__lang}_COMPILER_NAMES)
  486. if(HDF5_${__lang}_COMPILER_EXECUTABLE)
  487. _HDF5_invoke_compiler(${__lang} HDF5_${__lang}_COMPILE_LINE
  488. HDF5_${__lang}_RETURN_VALUE HDF5_${__lang}_VERSION HDF5_${__lang}_IS_PARALLEL)
  489. if(HDF5_${__lang}_RETURN_VALUE EQUAL 0)
  490. message(STATUS "HDF5: Using hdf5 compiler wrapper to determine ${__lang} configuration")
  491. _HDF5_parse_compile_line( HDF5_${__lang}_COMPILE_LINE
  492. HDF5_${__lang}_INCLUDE_DIRS
  493. HDF5_${__lang}_DEFINITIONS
  494. HDF5_${__lang}_LIBRARY_DIRS
  495. HDF5_${__lang}_LIBRARY_NAMES
  496. HDF5_${__lang}_HL_LIBRARY_NAMES
  497. )
  498. set(HDF5_${__lang}_LIBRARIES)
  499. set(_HDF5_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  500. if(HDF5_USE_STATIC_LIBRARIES)
  501. set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
  502. else()
  503. set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
  504. endif()
  505. foreach(L IN LISTS HDF5_${__lang}_LIBRARY_NAMES)
  506. find_library(HDF5_${__lang}_LIBRARY_${L} ${L} ${HDF5_${__lang}_LIBRARY_DIRS})
  507. if(HDF5_${__lang}_LIBRARY_${L})
  508. list(APPEND HDF5_${__lang}_LIBRARIES ${HDF5_${__lang}_LIBRARY_${L}})
  509. else()
  510. list(APPEND HDF5_${__lang}_LIBRARIES ${L})
  511. endif()
  512. endforeach()
  513. if(FIND_HL)
  514. set(HDF5_${__lang}_HL_LIBRARIES)
  515. foreach(L IN LISTS HDF5_${__lang}_HL_LIBRARY_NAMES)
  516. find_library(HDF5_${__lang}_LIBRARY_${L} ${L} ${HDF5_${__lang}_LIBRARY_DIRS})
  517. if(HDF5_${__lang}_LIBRARY_${L})
  518. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${HDF5_${__lang}_LIBRARY_${L}})
  519. else()
  520. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${L})
  521. endif()
  522. endforeach()
  523. set(HDF5_HL_FOUND True)
  524. endif()
  525. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_HDF5_CMAKE_FIND_LIBRARY_SUFFIXES})
  526. set(HDF5_${__lang}_FOUND True)
  527. mark_as_advanced(HDF5_${__lang}_DEFINITIONS)
  528. mark_as_advanced(HDF5_${__lang}_INCLUDE_DIRS)
  529. mark_as_advanced(HDF5_${__lang}_LIBRARIES)
  530. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_DEFINITIONS)
  531. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_INCLUDE_DIRS)
  532. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_LIBRARIES)
  533. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_HL_LIBRARIES)
  534. else()
  535. set(_HDF5_NEED_TO_SEARCH True)
  536. endif()
  537. else()
  538. set(_HDF5_NEED_TO_SEARCH True)
  539. endif()
  540. endif()
  541. if(HDF5_${__lang}_VERSION)
  542. if(NOT HDF5_VERSION)
  543. set(HDF5_VERSION ${HDF5_${__lang}_VERSION})
  544. elseif(NOT HDF5_VERSION VERSION_EQUAL HDF5_${__lang}_VERSION)
  545. message(WARNING "HDF5 Version found for language ${__lang}, ${HDF5_${__lang}_VERSION} is different than previously found version ${HDF5_VERSION}")
  546. endif()
  547. endif()
  548. if(DEFINED HDF5_${__lang}_IS_PARALLEL)
  549. if(NOT DEFINED HDF5_IS_PARALLEL)
  550. set(HDF5_IS_PARALLEL ${HDF5_${__lang}_IS_PARALLEL})
  551. elseif(NOT HDF5_IS_PARALLEL AND HDF5_${__lang}_IS_PARALLEL)
  552. message(WARNING "HDF5 found for language ${__lang} is parallel but previously found language is not parallel.")
  553. elseif(HDF5_IS_PARALLEL AND NOT HDF5_${__lang}_IS_PARALLEL)
  554. message(WARNING "HDF5 found for language ${__lang} is not parallel but previously found language is parallel.")
  555. endif()
  556. endif()
  557. endforeach()
  558. else()
  559. set(_HDF5_NEED_TO_SEARCH True)
  560. endif()
  561. if(NOT HDF5_FOUND AND HDF5_COMPILER_NO_INTERROGATE)
  562. # No arguments necessary, all languages can use the compiler wrappers
  563. set(HDF5_FOUND True)
  564. set(HDF5_METHOD "Included by compiler wrappers")
  565. set(HDF5_REQUIRED_VARS HDF5_METHOD)
  566. elseif(NOT HDF5_FOUND AND NOT _HDF5_NEED_TO_SEARCH)
  567. # Compiler wrappers aren't being used by the build but were found and used
  568. # to determine necessary include and library flags
  569. set(HDF5_INCLUDE_DIRS)
  570. set(HDF5_LIBRARIES)
  571. set(HDF5_HL_LIBRARIES)
  572. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  573. if(HDF5_${__lang}_FOUND)
  574. if(NOT HDF5_${__lang}_COMPILER_NO_INTERROGATE)
  575. list(APPEND HDF5_DEFINITIONS ${HDF5_${__lang}_DEFINITIONS})
  576. list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${__lang}_INCLUDE_DIRS})
  577. list(APPEND HDF5_LIBRARIES ${HDF5_${__lang}_LIBRARIES})
  578. if(FIND_HL)
  579. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${__lang}_HL_LIBRARIES})
  580. endif()
  581. endif()
  582. endif()
  583. endforeach()
  584. _HDF5_remove_duplicates_from_beginning(HDF5_DEFINITIONS)
  585. _HDF5_remove_duplicates_from_beginning(HDF5_INCLUDE_DIRS)
  586. _HDF5_remove_duplicates_from_beginning(HDF5_LIBRARIES)
  587. _HDF5_remove_duplicates_from_beginning(HDF5_HL_LIBRARIES)
  588. set(HDF5_FOUND True)
  589. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES)
  590. if(FIND_HL)
  591. list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
  592. endif()
  593. endif()
  594. if(HDF5_ROOT)
  595. set(SEARCH_OPTS NO_DEFAULT_PATH)
  596. endif()
  597. find_program( HDF5_DIFF_EXECUTABLE
  598. NAMES h5diff
  599. HINTS ${HDF5_ROOT}
  600. PATH_SUFFIXES bin Bin
  601. ${SEARCH_OPTS}
  602. DOC "HDF5 file differencing tool." )
  603. mark_as_advanced( HDF5_DIFF_EXECUTABLE )
  604. if( NOT HDF5_FOUND )
  605. # seed the initial lists of libraries to find with items we know we need
  606. set(HDF5_C_LIBRARY_NAMES hdf5)
  607. set(HDF5_C_HL_LIBRARY_NAMES hdf5_hl)
  608. set(HDF5_CXX_LIBRARY_NAMES hdf5_cpp ${HDF5_C_LIBRARY_NAMES})
  609. set(HDF5_CXX_HL_LIBRARY_NAMES hdf5_hl_cpp ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_CXX_LIBRARY_NAMES})
  610. set(HDF5_Fortran_LIBRARY_NAMES hdf5_fortran ${HDF5_C_LIBRARY_NAMES})
  611. set(HDF5_Fortran_HL_LIBRARY_NAMES hdf5hl_fortran ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_Fortran_LIBRARY_NAMES})
  612. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  613. # find the HDF5 include directories
  614. if(LANGUAGE STREQUAL "Fortran")
  615. set(HDF5_INCLUDE_FILENAME hdf5.mod)
  616. elseif(LANGUAGE STREQUAL "CXX")
  617. set(HDF5_INCLUDE_FILENAME H5Cpp.h)
  618. else()
  619. set(HDF5_INCLUDE_FILENAME hdf5.h)
  620. endif()
  621. find_path(HDF5_${__lang}_INCLUDE_DIR ${HDF5_INCLUDE_FILENAME}
  622. HINTS ${HDF5_ROOT}
  623. PATHS $ENV{HOME}/.local/include
  624. PATH_SUFFIXES include Include
  625. ${SEARCH_OPTS}
  626. )
  627. mark_as_advanced(HDF5_${LANGUAGE}_INCLUDE_DIR)
  628. list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${__lang}_INCLUDE_DIR})
  629. # find the HDF5 libraries
  630. foreach(LIB IN LISTS HDF5_${__lang}_LIBRARY_NAMES)
  631. if(UNIX AND HDF5_USE_STATIC_LIBRARIES)
  632. # According to bug 1643 on the CMake bug tracker, this is the
  633. # preferred method for searching for a static library.
  634. # See https://gitlab.kitware.com/cmake/cmake/issues/1643. We search
  635. # first for the full static library name, but fall back to a
  636. # generic search on the name if the static search fails.
  637. set( THIS_LIBRARY_SEARCH_DEBUG
  638. lib${LIB}d.a lib${LIB}_debug.a ${LIB}d ${LIB}_debug
  639. lib${LIB}d-static.a lib${LIB}_debug-static.a ${LIB}d-static ${LIB}_debug-static )
  640. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a ${LIB} lib${LIB}-static.a ${LIB}-static)
  641. else()
  642. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_debug ${LIB}d-shared ${LIB}_debug-shared)
  643. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
  644. endif()
  645. find_library(HDF5_${LIB}_LIBRARY_DEBUG
  646. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  647. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib
  648. ${SEARCH_OPTS}
  649. )
  650. find_library( HDF5_${LIB}_LIBRARY_RELEASE
  651. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  652. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib
  653. ${SEARCH_OPTS}
  654. )
  655. select_library_configurations( HDF5_${LIB} )
  656. list(APPEND HDF5_${__lang}_LIBRARIES ${HDF5_${LIB}_LIBRARY})
  657. endforeach()
  658. if(HDF5_${__lang}_LIBRARIES)
  659. set(HDF5_${__lang}_FOUND True)
  660. endif()
  661. # Append the libraries for this language binding to the list of all
  662. # required libraries.
  663. list(APPEND HDF5_LIBRARIES ${HDF5_${__lang}_LIBRARIES})
  664. if(FIND_HL)
  665. foreach(LIB IN LISTS HDF5_${__lang}_HL_LIBRARY_NAMES)
  666. if(UNIX AND HDF5_USE_STATIC_LIBRARIES)
  667. # According to bug 1643 on the CMake bug tracker, this is the
  668. # preferred method for searching for a static library.
  669. # See https://gitlab.kitware.com/cmake/cmake/issues/1643. We search
  670. # first for the full static library name, but fall back to a
  671. # generic search on the name if the static search fails.
  672. set( THIS_LIBRARY_SEARCH_DEBUG
  673. lib${LIB}d.a lib${LIB}_debug.a ${LIB}d ${LIB}_debug
  674. lib${LIB}d-static.a lib${LIB}_debug-static.a ${LIB}d-static ${LIB}_debug-static )
  675. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a ${LIB} lib${LIB}-static.a ${LIB}-static)
  676. else()
  677. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_debug ${LIB}d-shared ${LIB}_debug-shared)
  678. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
  679. endif()
  680. find_library(HDF5_${LIB}_LIBRARY_DEBUG
  681. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  682. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib
  683. ${SEARCH_OPTS}
  684. )
  685. find_library( HDF5_${LIB}_LIBRARY_RELEASE
  686. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  687. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib
  688. ${SEARCH_OPTS}
  689. )
  690. select_library_configurations( HDF5_${LIB} )
  691. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${HDF5_${LIB}_LIBRARY})
  692. endforeach()
  693. # Append the libraries for this language binding to the list of all
  694. # required libraries.
  695. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${__lang}_HL_LIBRARIES})
  696. endif()
  697. endforeach()
  698. if(FIND_HL AND HDF5_HL_LIBRARIES)
  699. set(HDF5_HL_FOUND True)
  700. endif()
  701. _HDF5_remove_duplicates_from_beginning(HDF5_INCLUDE_DIRS)
  702. _HDF5_remove_duplicates_from_beginning(HDF5_LIBRARIES)
  703. _HDF5_remove_duplicates_from_beginning(HDF5_HL_LIBRARIES)
  704. # If the HDF5 include directory was found, open H5pubconf.h to determine if
  705. # HDF5 was compiled with parallel IO support
  706. set( HDF5_IS_PARALLEL FALSE )
  707. set( HDF5_VERSION "" )
  708. foreach( _dir IN LISTS HDF5_INCLUDE_DIRS )
  709. foreach(_hdr "${_dir}/H5pubconf.h" "${_dir}/H5pubconf-64.h" "${_dir}/H5pubconf-32.h")
  710. if( EXISTS "${_hdr}" )
  711. file( STRINGS "${_hdr}"
  712. HDF5_HAVE_PARALLEL_DEFINE
  713. REGEX "HAVE_PARALLEL 1" )
  714. if( HDF5_HAVE_PARALLEL_DEFINE )
  715. set( HDF5_IS_PARALLEL TRUE )
  716. endif()
  717. unset(HDF5_HAVE_PARALLEL_DEFINE)
  718. file( STRINGS "${_hdr}"
  719. HDF5_VERSION_DEFINE
  720. REGEX "^[ \t]*#[ \t]*define[ \t]+H5_VERSION[ \t]+" )
  721. if( "${HDF5_VERSION_DEFINE}" MATCHES
  722. "H5_VERSION[ \t]+\"([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?\"" )
  723. set( HDF5_VERSION "${CMAKE_MATCH_1}" )
  724. if( CMAKE_MATCH_3 )
  725. set( HDF5_VERSION ${HDF5_VERSION}.${CMAKE_MATCH_3})
  726. endif()
  727. endif()
  728. unset(HDF5_VERSION_DEFINE)
  729. endif()
  730. endforeach()
  731. endforeach()
  732. set( HDF5_IS_PARALLEL ${HDF5_IS_PARALLEL} CACHE BOOL
  733. "HDF5 library compiled with parallel IO support" )
  734. mark_as_advanced( HDF5_IS_PARALLEL )
  735. # For backwards compatibility we set HDF5_INCLUDE_DIR to the value of
  736. # HDF5_INCLUDE_DIRS
  737. if( HDF5_INCLUDE_DIRS )
  738. set( HDF5_INCLUDE_DIR "${HDF5_INCLUDE_DIRS}" )
  739. endif()
  740. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
  741. if(FIND_HL)
  742. list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
  743. endif()
  744. endif()
  745. # If HDF5_REQUIRED_VARS is empty at this point, then it's likely that
  746. # something external is trying to explicitly pass already found
  747. # locations
  748. if(NOT HDF5_REQUIRED_VARS)
  749. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
  750. endif()
  751. find_package_handle_standard_args(HDF5
  752. REQUIRED_VARS ${HDF5_REQUIRED_VARS}
  753. VERSION_VAR HDF5_VERSION
  754. HANDLE_COMPONENTS
  755. )