FindHDF5.cmake 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 <wdicharry@stellarscience.com>.
  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. set(HDF5_FIND_REQUIRED_${__lang} True)
  116. endif()
  117. endforeach()
  118. set(FIND_HL ON)
  119. set(HDF5_FIND_REQUIRED_HL True)
  120. else()
  121. # add the extra specified components, ensuring that they are valid.
  122. set(FIND_HL OFF)
  123. foreach(component IN LISTS HDF5_FIND_COMPONENTS)
  124. list(FIND HDF5_VALID_LANGUAGE_BINDINGS ${component} component_location)
  125. if(NOT component_location EQUAL -1)
  126. list(APPEND HDF5_LANGUAGE_BINDINGS ${component})
  127. elseif(component STREQUAL "HL")
  128. set(FIND_HL ON)
  129. elseif(component STREQUAL "Fortran_HL") # only for compatibility
  130. list(APPEND HDF5_LANGUAGE_BINDINGS Fortran)
  131. set(FIND_HL ON)
  132. set(HDF5_FIND_REQUIRED_Fortran_HL False)
  133. set(HDF5_FIND_REQUIRED_Fortran True)
  134. set(HDF5_FIND_REQUIRED_HL True)
  135. else()
  136. message(FATAL_ERROR "${component} is not a valid HDF5 component.")
  137. endif()
  138. endforeach()
  139. if(NOT HDF5_LANGUAGE_BINDINGS)
  140. get_property(__langs GLOBAL PROPERTY ENABLED_LANGUAGES)
  141. foreach(__lang IN LISTS __langs)
  142. if(__lang MATCHES "^(C|CXX|Fortran)$")
  143. list(APPEND HDF5_LANGUAGE_BINDINGS ${__lang})
  144. endif()
  145. endforeach()
  146. endif()
  147. list(REMOVE_ITEM HDF5_FIND_COMPONENTS Fortran_HL) # replaced by Fortran and HL
  148. list(REMOVE_DUPLICATES HDF5_LANGUAGE_BINDINGS)
  149. endif()
  150. # Determine whether to search for serial or parallel executable first
  151. if(HDF5_PREFER_PARALLEL)
  152. set(HDF5_C_COMPILER_NAMES h5pcc h5cc)
  153. set(HDF5_CXX_COMPILER_NAMES h5pc++ h5c++)
  154. set(HDF5_Fortran_COMPILER_NAMES h5pfc h5fc)
  155. else()
  156. set(HDF5_C_COMPILER_NAMES h5cc h5pcc)
  157. set(HDF5_CXX_COMPILER_NAMES h5c++ h5pc++)
  158. set(HDF5_Fortran_COMPILER_NAMES h5fc h5pfc)
  159. endif()
  160. # We may have picked up some duplicates in various lists during the above
  161. # process for the language bindings (both the C and C++ bindings depend on
  162. # libz for example). Remove the duplicates. It appears that the default
  163. # CMake behavior is to remove duplicates from the end of a list. However,
  164. # for link lines, this is incorrect since unresolved symbols are searched
  165. # for down the link line. Therefore, we reverse the list, remove the
  166. # duplicates, and then reverse it again to get the duplicates removed from
  167. # the beginning.
  168. macro(_HDF5_remove_duplicates_from_beginning _list_name)
  169. if(${_list_name})
  170. list(REVERSE ${_list_name})
  171. list(REMOVE_DUPLICATES ${_list_name})
  172. list(REVERSE ${_list_name})
  173. endif()
  174. endmacro()
  175. # Test first if the current compilers automatically wrap HDF5
  176. function(_HDF5_test_regular_compiler_C success version)
  177. set(scratch_directory
  178. ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
  179. if(NOT ${success} OR
  180. NOT EXISTS ${scratch_directory}/compiler_has_h5_c)
  181. set(test_file ${scratch_directory}/cmake_hdf5_test.c)
  182. file(WRITE ${test_file}
  183. "#include <hdf5.h>\n"
  184. "#include <hdf5_hl.h>\n"
  185. "int main(void) {\n"
  186. " char const* info_ver = \"INFO\" \":\" H5_VERSION;\n"
  187. " hid_t fid;\n"
  188. " fid = H5Fcreate(\"foo.h5\",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);\n"
  189. " return 0;\n"
  190. "}")
  191. try_compile(${success} ${scratch_directory} ${test_file}
  192. COPY_FILE ${scratch_directory}/compiler_has_h5_c
  193. )
  194. endif()
  195. if(${success})
  196. file(STRINGS ${scratch_directory}/compiler_has_h5_c INFO_VER
  197. REGEX "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  198. )
  199. string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  200. INFO_VER "${INFO_VER}"
  201. )
  202. set(${version} ${CMAKE_MATCH_1})
  203. if(CMAKE_MATCH_3)
  204. set(${version} ${HDF5_CXX_VERSION}.${CMAKE_MATCH_3})
  205. endif()
  206. set(${version} ${${version}} PARENT_SCOPE)
  207. endif()
  208. endfunction()
  209. function(_HDF5_test_regular_compiler_CXX success version)
  210. set(scratch_directory ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
  211. if(NOT ${success} OR
  212. NOT EXISTS ${scratch_directory}/compiler_has_h5_cxx)
  213. set(test_file ${scratch_directory}/cmake_hdf5_test.cxx)
  214. file(WRITE ${test_file}
  215. "#include <H5Cpp.h>\n"
  216. "#ifndef H5_NO_NAMESPACE\n"
  217. "using namespace H5;\n"
  218. "#endif\n"
  219. "int main(int argc, char **argv) {\n"
  220. " char const* info_ver = \"INFO\" \":\" H5_VERSION;\n"
  221. " H5File file(\"foo.h5\", H5F_ACC_TRUNC);\n"
  222. " return 0;\n"
  223. "}")
  224. try_compile(${success} ${scratch_directory} ${test_file}
  225. COPY_FILE ${scratch_directory}/compiler_has_h5_cxx
  226. )
  227. endif()
  228. if(${success})
  229. file(STRINGS ${scratch_directory}/compiler_has_h5_cxx INFO_VER
  230. REGEX "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  231. )
  232. string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
  233. INFO_VER "${INFO_VER}"
  234. )
  235. set(${version} ${CMAKE_MATCH_1})
  236. if(CMAKE_MATCH_3)
  237. set(${version} ${HDF5_CXX_VERSION}.${CMAKE_MATCH_3})
  238. endif()
  239. set(${version} ${${version}} PARENT_SCOPE)
  240. endif()
  241. endfunction()
  242. function(_HDF5_test_regular_compiler_Fortran success)
  243. if(NOT ${success})
  244. set(scratch_directory
  245. ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
  246. set(test_file ${scratch_directory}/cmake_hdf5_test.f90)
  247. file(WRITE ${test_file}
  248. "program hdf5_hello\n"
  249. " use hdf5\n"
  250. " use h5lt\n"
  251. " use h5ds\n"
  252. " integer error\n"
  253. " call h5open_f(error)\n"
  254. " call h5close_f(error)\n"
  255. "end\n")
  256. try_compile(${success} ${scratch_directory} ${test_file})
  257. endif()
  258. endfunction()
  259. # Invoke the HDF5 wrapper compiler. The compiler return value is stored to the
  260. # return_value argument, the text output is stored to the output variable.
  261. macro( _HDF5_invoke_compiler language output return_value version)
  262. set(${version})
  263. if(HDF5_USE_STATIC_LIBRARIES)
  264. set(lib_type_args -noshlib)
  265. else()
  266. set(lib_type_args -shlib)
  267. endif()
  268. set(scratch_dir ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
  269. if("${language}" STREQUAL "C")
  270. set(test_file ${scratch_dir}/cmake_hdf5_test.c)
  271. elseif("${language}" STREQUAL "CXX")
  272. set(test_file ${scratch_dir}/cmake_hdf5_test.cxx)
  273. elseif("${language}" STREQUAL "Fortran")
  274. set(test_file ${scratch_dir}/cmake_hdf5_test.f90)
  275. endif()
  276. exec_program( ${HDF5_${language}_COMPILER_EXECUTABLE}
  277. ARGS -show ${lib_type_args} ${test_file}
  278. OUTPUT_VARIABLE ${output}
  279. RETURN_VALUE ${return_value}
  280. )
  281. if(NOT ${${return_value}} EQUAL 0)
  282. message(STATUS
  283. "Unable to determine HDF5 ${language} flags from HDF5 wrapper.")
  284. endif()
  285. exec_program( ${HDF5_${language}_COMPILER_EXECUTABLE}
  286. ARGS -showconfig
  287. OUTPUT_VARIABLE config_output
  288. RETURN_VALUE config_return
  289. )
  290. if(NOT ${return_value} EQUAL 0)
  291. message( STATUS
  292. "Unable to determine HDF5 ${language} version from HDF5 wrapper.")
  293. endif()
  294. string(REGEX MATCH "HDF5 Version: ([a-zA-Z0-9\\.\\-]*)" version_match "${config_output}")
  295. if(version_match)
  296. string(REPLACE "HDF5 Version: " "" ${version} "${version_match}")
  297. string(REPLACE "-patch" "." ${version} "${${version}}")
  298. endif()
  299. endmacro()
  300. # Parse a compile line for definitions, includes, library paths, and libraries.
  301. macro( _HDF5_parse_compile_line
  302. compile_line_var
  303. include_paths
  304. definitions
  305. library_paths
  306. libraries
  307. libraries_hl)
  308. # Match the include paths
  309. set( RE " -I *([^\" ]+|\"[^\"]+\")")
  310. string( REGEX MATCHALL "${RE}" include_path_flags "${${compile_line_var}}")
  311. foreach( IPATH IN LISTS include_path_flags )
  312. string( REGEX REPLACE "${RE}" "\\1" IPATH "${IPATH}" )
  313. list( APPEND ${include_paths} ${IPATH} )
  314. endforeach()
  315. # Match the definitions
  316. set( RE " -D([^ ]*)")
  317. string( REGEX MATCHALL "${RE}" definition_flags "${${compile_line_var}}" )
  318. foreach( DEF IN LISTS definition_flags )
  319. string( REGEX REPLACE "${RE}" "\\1" DEF "${DEF}" )
  320. list( APPEND ${definitions} ${DEF} )
  321. endforeach()
  322. # Match the library paths
  323. set( RE " -L *([^\" ]+|\"[^\"]+\")")
  324. string( REGEX MATCHALL "${RE}" library_path_flags "${${compile_line_var}}")
  325. foreach( LPATH IN LISTS library_path_flags )
  326. string( REGEX REPLACE "${RE}" "\\1" LPATH "${LPATH}" )
  327. list( APPEND ${library_paths} ${LPATH} )
  328. endforeach()
  329. # now search for the lib names specified in the compile line (match -l...)
  330. # match only -l's preceded by a space or comma
  331. set( RE " -l *([^\" ]+|\"[^\"]+\")")
  332. string( REGEX MATCHALL "${RE}" library_name_flags "${${compile_line_var}}")
  333. foreach( LNAME IN LISTS library_name_flags )
  334. string( REGEX REPLACE "${RE}" "\\1" LNAME "${LNAME}" )
  335. if(LNAME MATCHES ".*hl")
  336. list(APPEND ${libraries_hl} ${LNAME})
  337. else()
  338. list(APPEND ${libraries} ${LNAME})
  339. endif()
  340. endforeach()
  341. # now search for full library paths with no flags
  342. set( RE " ([^\" ]+|\"[^\"]+\")")
  343. string( REGEX MATCHALL "${RE}" library_name_noflags "${${compile_line_var}}")
  344. foreach( LIB IN LISTS library_name_noflags )
  345. string( REGEX REPLACE "${RE}" "\\1" LIB "${LIB}" )
  346. get_filename_component(LIB "${LIB}" ABSOLUTE)
  347. if(NOT EXISTS ${LIB} OR IS_DIRECTORY ${LIB})
  348. continue()
  349. endif()
  350. get_filename_component(LPATH ${LIB} DIRECTORY)
  351. get_filename_component(LNAME ${LIB} NAME_WE)
  352. string( REGEX REPLACE "^lib" "" LNAME ${LNAME} )
  353. list( APPEND ${library_paths} ${LPATH} )
  354. if(LNAME MATCHES ".*hl")
  355. list(APPEND ${libraries_hl} ${LNAME})
  356. else()
  357. list(APPEND ${libraries} ${LNAME})
  358. endif()
  359. endforeach()
  360. endmacro()
  361. if(NOT HDF5_ROOT)
  362. set(HDF5_ROOT $ENV{HDF5_ROOT})
  363. endif()
  364. # Try to find HDF5 using an installed hdf5-config.cmake
  365. if(NOT HDF5_FOUND AND NOT HDF5_ROOT)
  366. find_package(HDF5 QUIET NO_MODULE)
  367. if( HDF5_FOUND)
  368. set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR})
  369. set(HDF5_LIBRARIES)
  370. set(HDF5_C_TARGET hdf5)
  371. set(HDF5_C_HL_TARGET hdf5_hl)
  372. set(HDF5_CXX_TARGET hdf5_cpp)
  373. set(HDF5_CXX_HL_TARGET hdf5_hl_cpp)
  374. set(HDF5_Fortran_TARGET hdf5_fortran)
  375. set(HDF5_Fortran_HL_TARGET hdf5_hl_fortran)
  376. if(HDF5_USE_STATIC_LIBRARIES)
  377. set(_suffix "-static")
  378. else()
  379. set(_suffix "-shared")
  380. endif()
  381. foreach(_lang ${HDF5_LANGUAGE_BINDINGS})
  382. #Older versions of hdf5 don't have a static/shared suffix so
  383. #if we detect that occurrence clear the suffix
  384. if(_suffix AND NOT TARGET ${HDF5_${_lang}_TARGET}${_suffix})
  385. if(NOT TARGET ${HDF5_${_lang}_TARGET})
  386. #cant find this component with our without the suffix
  387. #so bail out, and let the following locate HDF5
  388. set(HDF5_FOUND FALSE)
  389. break()
  390. endif()
  391. set(_suffix "")
  392. endif()
  393. get_target_property(_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
  394. if( _lang_location )
  395. set(HDF5_${_lang}_LIBRARY ${_lang_location} CACHE PATH
  396. "HDF5 ${_lang} library" )
  397. mark_as_advanced(HDF5_${_lang}_LIBRARY)
  398. list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_LIBRARY})
  399. set(HDF5_${_lang}_LIBRARIES ${HDF5_${_lang}_LIBRARY})
  400. set(HDF5_${_lang}_FOUND True)
  401. endif()
  402. if(FIND_HL)
  403. get_target_property(_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
  404. if( _lang_hl_location )
  405. set(HDF5_${_lang}_HL_LIBRARY ${_lang_hl_location} CACHE PATH
  406. "HDF5 ${_lang} HL library" )
  407. mark_as_advanced(HDF5_${_lang}_HL_LIBRARY)
  408. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARY})
  409. set(HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARY})
  410. set(HDF5_HL_FOUND True)
  411. endif()
  412. endif()
  413. endforeach()
  414. endif()
  415. endif()
  416. if(NOT HDF5_FOUND AND NOT HDF5_ROOT)
  417. set(_HDF5_NEED_TO_SEARCH False)
  418. set(HDF5_COMPILER_NO_INTERROGATE True)
  419. # Only search for languages we've enabled
  420. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  421. # First check to see if our regular compiler is one of wrappers
  422. if(__lang STREQUAL "C")
  423. _HDF5_test_regular_compiler_C(
  424. HDF5_${__lang}_COMPILER_NO_INTERROGATE
  425. HDF5_${__lang}_VERSION)
  426. elseif(__lang STREQUAL "CXX")
  427. _HDF5_test_regular_compiler_CXX(
  428. HDF5_${__lang}_COMPILER_NO_INTERROGATE
  429. HDF5_${__lang}_VERSION)
  430. elseif(__lang STREQUAL "Fortran")
  431. _HDF5_test_regular_compiler_Fortran(
  432. HDF5_${__lang}_COMPILER_NO_INTERROGATE)
  433. else()
  434. continue()
  435. endif()
  436. if(HDF5_${__lang}_COMPILER_NO_INTERROGATE)
  437. message(STATUS "HDF5: Using hdf5 compiler wrapper for all ${__lang} compiling")
  438. set(HDF5_${__lang}_FOUND True)
  439. set(HDF5_${__lang}_COMPILER_EXECUTABLE_NO_INTERROGATE
  440. "${CMAKE_${__lang}_COMPILER}"
  441. CACHE FILEPATH "HDF5 ${__lang} compiler wrapper")
  442. set(HDF5_${__lang}_DEFINITIONS)
  443. set(HDF5_${__lang}_INCLUDE_DIRS)
  444. set(HDF5_${__lang}_LIBRARIES)
  445. set(HDF5_${__lang}_HL_LIBRARIES)
  446. mark_as_advanced(HDF5_${__lang}_COMPILER_EXECUTABLE_NO_INTERROGATE)
  447. mark_as_advanced(HDF5_${__lang}_DEFINITIONS)
  448. mark_as_advanced(HDF5_${__lang}_INCLUDE_DIRS)
  449. mark_as_advanced(HDF5_${__lang}_LIBRARIES)
  450. mark_as_advanced(HDF5_${__lang}_HL_LIBRARIES)
  451. set(HDF5_${__lang}_FOUND True)
  452. set(HDF5_HL_FOUND True)
  453. else()
  454. set(HDF5_COMPILER_NO_INTERROGATE False)
  455. # If this language isn't using the wrapper, then try to seed the
  456. # search options with the wrapper
  457. find_program(HDF5_${__lang}_COMPILER_EXECUTABLE
  458. NAMES ${HDF5_${__lang}_COMPILER_NAMES} NAMES_PER_DIR
  459. PATH_SUFFIXES bin Bin
  460. DOC "HDF5 ${__lang} Wrapper compiler. Used only to detect HDF5 compile flags."
  461. )
  462. mark_as_advanced( HDF5_${__lang}_COMPILER_EXECUTABLE )
  463. unset(HDF5_${__lang}_COMPILER_NAMES)
  464. if(HDF5_${__lang}_COMPILER_EXECUTABLE)
  465. _HDF5_invoke_compiler(${__lang} HDF5_${__lang}_COMPILE_LINE
  466. HDF5_${__lang}_RETURN_VALUE HDF5_${__lang}_VERSION)
  467. if(HDF5_${__lang}_RETURN_VALUE EQUAL 0)
  468. message(STATUS "HDF5: Using hdf5 compiler wrapper to determine ${__lang} configuration")
  469. _HDF5_parse_compile_line( HDF5_${__lang}_COMPILE_LINE
  470. HDF5_${__lang}_INCLUDE_DIRS
  471. HDF5_${__lang}_DEFINITIONS
  472. HDF5_${__lang}_LIBRARY_DIRS
  473. HDF5_${__lang}_LIBRARY_NAMES
  474. HDF5_${__lang}_HL_LIBRARY_NAMES
  475. )
  476. set(HDF5_${__lang}_LIBRARIES)
  477. set(_HDF5_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  478. if(HDF5_USE_STATIC_LIBRARIES)
  479. set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
  480. else()
  481. set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
  482. endif()
  483. foreach(L IN LISTS HDF5_${__lang}_LIBRARY_NAMES)
  484. find_library(HDF5_${__lang}_LIBRARY_${L} ${L} ${HDF5_${__lang}_LIBRARY_DIRS})
  485. if(HDF5_${__lang}_LIBRARY_${L})
  486. list(APPEND HDF5_${__lang}_LIBRARIES ${HDF5_${__lang}_LIBRARY_${L}})
  487. else()
  488. list(APPEND HDF5_${__lang}_LIBRARIES ${L})
  489. endif()
  490. endforeach()
  491. if(FIND_HL)
  492. set(HDF5_${__lang}_HL_LIBRARIES)
  493. foreach(L IN LISTS HDF5_${__lang}_HL_LIBRARY_NAMES)
  494. find_library(HDF5_${__lang}_LIBRARY_${L} ${L} ${HDF5_${__lang}_LIBRARY_DIRS})
  495. if(HDF5_${__lang}_LIBRARY_${L})
  496. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${HDF5_${__lang}_LIBRARY_${L}})
  497. else()
  498. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${L})
  499. endif()
  500. endforeach()
  501. set(HDF5_HL_FOUND True)
  502. endif()
  503. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_HDF5_CMAKE_FIND_LIBRARY_SUFFIXES})
  504. set(HDF5_${__lang}_FOUND True)
  505. mark_as_advanced(HDF5_${__lang}_DEFINITIONS)
  506. mark_as_advanced(HDF5_${__lang}_INCLUDE_DIRS)
  507. mark_as_advanced(HDF5_${__lang}_LIBRARIES)
  508. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_DEFINITIONS)
  509. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_INCLUDE_DIRS)
  510. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_LIBRARIES)
  511. _HDF5_remove_duplicates_from_beginning(HDF5_${__lang}_HL_LIBRARIES)
  512. else()
  513. set(_HDF5_NEED_TO_SEARCH True)
  514. endif()
  515. else()
  516. set(_HDF5_NEED_TO_SEARCH True)
  517. endif()
  518. endif()
  519. if(HDF5_${__lang}_VERSION)
  520. if(NOT HDF5_VERSION)
  521. set(HDF5_VERSION ${HDF5_${__lang}_VERSION})
  522. elseif(NOT HDF5_VERSION VERSION_EQUAL HDF5_${__lang}_VERSION)
  523. message(WARNING "HDF5 Version found for language ${__lang}, ${HDF5_${__lang}_VERSION} is different than previously found version ${HDF5_VERSION}")
  524. endif()
  525. endif()
  526. endforeach()
  527. else()
  528. set(_HDF5_NEED_TO_SEARCH True)
  529. endif()
  530. if(NOT HDF5_FOUND AND HDF5_COMPILER_NO_INTERROGATE)
  531. # No arguments necessary, all languages can use the compiler wrappers
  532. set(HDF5_FOUND True)
  533. set(HDF5_METHOD "Included by compiler wrappers")
  534. set(HDF5_REQUIRED_VARS HDF5_METHOD)
  535. elseif(NOT HDF5_FOUND AND NOT _HDF5_NEED_TO_SEARCH)
  536. # Compiler wrappers aren't being used by the build but were found and used
  537. # to determine necessary include and library flags
  538. set(HDF5_INCLUDE_DIRS)
  539. set(HDF5_LIBRARIES)
  540. set(HDF5_HL_LIBRARIES)
  541. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  542. if(HDF5_${__lang}_FOUND)
  543. if(NOT HDF5_${__lang}_COMPILER_NO_INTERROGATE)
  544. list(APPEND HDF5_DEFINITIONS ${HDF5_${__lang}_DEFINITIONS})
  545. list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${__lang}_INCLUDE_DIRS})
  546. list(APPEND HDF5_LIBRARIES ${HDF5_${__lang}_LIBRARIES})
  547. if(FIND_HL)
  548. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${__lang}_HL_LIBRARIES})
  549. endif()
  550. endif()
  551. endif()
  552. endforeach()
  553. _HDF5_remove_duplicates_from_beginning(HDF5_DEFINITIONS)
  554. _HDF5_remove_duplicates_from_beginning(HDF5_INCLUDE_DIRS)
  555. _HDF5_remove_duplicates_from_beginning(HDF5_LIBRARIES)
  556. _HDF5_remove_duplicates_from_beginning(HDF5_HL_LIBRARIES)
  557. set(HDF5_FOUND True)
  558. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES)
  559. if(FIND_HL)
  560. list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
  561. endif()
  562. endif()
  563. if(HDF5_ROOT)
  564. set(SEARCH_OPTS NO_DEFAULT_PATH)
  565. endif()
  566. find_program( HDF5_DIFF_EXECUTABLE
  567. NAMES h5diff
  568. HINTS ${HDF5_ROOT}
  569. PATH_SUFFIXES bin Bin
  570. ${SEARCH_OPTS}
  571. DOC "HDF5 file differencing tool." )
  572. mark_as_advanced( HDF5_DIFF_EXECUTABLE )
  573. if( NOT HDF5_FOUND )
  574. # seed the initial lists of libraries to find with items we know we need
  575. set(HDF5_C_LIBRARY_NAMES hdf5)
  576. set(HDF5_C_HL_LIBRARY_NAMES hdf5_hl)
  577. set(HDF5_CXX_LIBRARY_NAMES hdf5_cpp ${HDF5_C_LIBRARY_NAMES})
  578. set(HDF5_CXX_HL_LIBRARY_NAMES hdf5_hl_cpp ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_CXX_LIBRARY_NAMES})
  579. set(HDF5_Fortran_LIBRARY_NAMES hdf5_fortran ${HDF5_C_LIBRARY_NAMES})
  580. set(HDF5_Fortran_HL_LIBRARY_NAMES hdf5_hl_fortran ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_Fortran_LIBRARY_NAMES})
  581. foreach(__lang IN LISTS HDF5_LANGUAGE_BINDINGS)
  582. # find the HDF5 include directories
  583. if(LANGUAGE STREQUAL "Fortran")
  584. set(HDF5_INCLUDE_FILENAME hdf5.mod)
  585. elseif(LANGUAGE STREQUAL "CXX")
  586. set(HDF5_INCLUDE_FILENAME H5Cpp.h)
  587. else()
  588. set(HDF5_INCLUDE_FILENAME hdf5.h)
  589. endif()
  590. find_path(HDF5_${__lang}_INCLUDE_DIR ${HDF5_INCLUDE_FILENAME}
  591. HINTS ${HDF5_ROOT}
  592. PATHS $ENV{HOME}/.local/include
  593. PATH_SUFFIXES include Include
  594. ${SEARCH_OPTS}
  595. )
  596. mark_as_advanced(HDF5_${LANGUAGE}_INCLUDE_DIR)
  597. list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${__lang}_INCLUDE_DIR})
  598. # find the HDF5 libraries
  599. foreach(LIB IN LISTS HDF5_${__lang}_LIBRARY_NAMES)
  600. if(UNIX AND HDF5_USE_STATIC_LIBRARIES)
  601. # According to bug 1643 on the CMake bug tracker, this is the
  602. # preferred method for searching for a static library.
  603. # See https://cmake.org/Bug/view.php?id=1643. We search
  604. # first for the full static library name, but fall back to a
  605. # generic search on the name if the static search fails.
  606. set( THIS_LIBRARY_SEARCH_DEBUG
  607. lib${LIB}d.a lib${LIB}_debug.a ${LIB}d ${LIB}_debug
  608. lib${LIB}d-static.a lib${LIB}_debug-static.a ${LIB}d-static ${LIB}_debug-static )
  609. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a ${LIB} lib${LIB}-static.a ${LIB}-static)
  610. else()
  611. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_debug ${LIB}d-shared ${LIB}_debug-shared)
  612. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
  613. endif()
  614. find_library(HDF5_${LIB}_LIBRARY_DEBUG
  615. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  616. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib
  617. ${SEARCH_OPTS}
  618. )
  619. find_library( HDF5_${LIB}_LIBRARY_RELEASE
  620. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  621. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib
  622. ${SEARCH_OPTS}
  623. )
  624. select_library_configurations( HDF5_${LIB} )
  625. list(APPEND HDF5_${__lang}_LIBRARIES ${HDF5_${LIB}_LIBRARY})
  626. endforeach()
  627. if(HDF5_${__lang}_LIBRARIES)
  628. set(HDF5_${__lang}_FOUND True)
  629. endif()
  630. # Append the libraries for this language binding to the list of all
  631. # required libraries.
  632. list(APPEND HDF5_LIBRARIES ${HDF5_${__lang}_LIBRARIES})
  633. if(FIND_HL)
  634. foreach(LIB IN LISTS HDF5_${__lang}_HL_LIBRARY_NAMES)
  635. if(UNIX AND HDF5_USE_STATIC_LIBRARIES)
  636. # According to bug 1643 on the CMake bug tracker, this is the
  637. # preferred method for searching for a static library.
  638. # See https://cmake.org/Bug/view.php?id=1643. We search
  639. # first for the full static library name, but fall back to a
  640. # generic search on the name if the static search fails.
  641. set( THIS_LIBRARY_SEARCH_DEBUG
  642. lib${LIB}d.a lib${LIB}_debug.a ${LIB}d ${LIB}_debug
  643. lib${LIB}d-static.a lib${LIB}_debug-static.a ${LIB}d-static ${LIB}_debug-static )
  644. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a ${LIB} lib${LIB}-static.a ${LIB}-static)
  645. else()
  646. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_debug ${LIB}d-shared ${LIB}_debug-shared)
  647. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
  648. endif()
  649. find_library(HDF5_${LIB}_LIBRARY_DEBUG
  650. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  651. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib
  652. ${SEARCH_OPTS}
  653. )
  654. find_library( HDF5_${LIB}_LIBRARY_RELEASE
  655. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  656. HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib
  657. ${SEARCH_OPTS}
  658. )
  659. select_library_configurations( HDF5_${LIB} )
  660. list(APPEND HDF5_${__lang}_HL_LIBRARIES ${HDF5_${LIB}_LIBRARY})
  661. endforeach()
  662. # Append the libraries for this language binding to the list of all
  663. # required libraries.
  664. list(APPEND HDF5_HL_LIBRARIES ${HDF5_${__lang}_HL_LIBRARIES})
  665. endif()
  666. endforeach()
  667. if(FIND_HL AND HDF5_HL_LIBRARIES)
  668. set(HDF5_HL_FOUND True)
  669. endif()
  670. _HDF5_remove_duplicates_from_beginning(HDF5_INCLUDE_DIRS)
  671. _HDF5_remove_duplicates_from_beginning(HDF5_LIBRARIES)
  672. _HDF5_remove_duplicates_from_beginning(HDF5_HL_LIBRARIES)
  673. # If the HDF5 include directory was found, open H5pubconf.h to determine if
  674. # HDF5 was compiled with parallel IO support
  675. set( HDF5_IS_PARALLEL FALSE )
  676. set( HDF5_VERSION "" )
  677. foreach( _dir IN LISTS HDF5_INCLUDE_DIRS )
  678. foreach(_hdr "${_dir}/H5pubconf.h" "${_dir}/H5pubconf-64.h" "${_dir}/H5pubconf-32.h")
  679. if( EXISTS "${_hdr}" )
  680. file( STRINGS "${_hdr}"
  681. HDF5_HAVE_PARALLEL_DEFINE
  682. REGEX "HAVE_PARALLEL 1" )
  683. if( HDF5_HAVE_PARALLEL_DEFINE )
  684. set( HDF5_IS_PARALLEL TRUE )
  685. endif()
  686. unset(HDF5_HAVE_PARALLEL_DEFINE)
  687. file( STRINGS "${_hdr}"
  688. HDF5_VERSION_DEFINE
  689. REGEX "^[ \t]*#[ \t]*define[ \t]+H5_VERSION[ \t]+" )
  690. if( "${HDF5_VERSION_DEFINE}" MATCHES
  691. "H5_VERSION[ \t]+\"([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?\"" )
  692. set( HDF5_VERSION "${CMAKE_MATCH_1}" )
  693. if( CMAKE_MATCH_3 )
  694. set( HDF5_VERSION ${HDF5_VERSION}.${CMAKE_MATCH_3})
  695. endif()
  696. endif()
  697. unset(HDF5_VERSION_DEFINE)
  698. endif()
  699. endforeach()
  700. endforeach()
  701. set( HDF5_IS_PARALLEL ${HDF5_IS_PARALLEL} CACHE BOOL
  702. "HDF5 library compiled with parallel IO support" )
  703. mark_as_advanced( HDF5_IS_PARALLEL )
  704. # For backwards compatibility we set HDF5_INCLUDE_DIR to the value of
  705. # HDF5_INCLUDE_DIRS
  706. if( HDF5_INCLUDE_DIRS )
  707. set( HDF5_INCLUDE_DIR "${HDF5_INCLUDE_DIRS}" )
  708. endif()
  709. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
  710. if(FIND_HL)
  711. list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
  712. endif()
  713. endif()
  714. # If HDF5_REQUIRED_VARS is empty at this point, then it's likely that
  715. # something external is trying to explicitly pass already found
  716. # locations
  717. if(NOT HDF5_REQUIRED_VARS)
  718. set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
  719. endif()
  720. find_package_handle_standard_args(HDF5
  721. REQUIRED_VARS ${HDF5_REQUIRED_VARS}
  722. VERSION_VAR HDF5_VERSION
  723. HANDLE_COMPONENTS
  724. )