FindHDF5.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. # - Find HDF5, a library for reading and writing self describing array data.
  2. #
  3. # This module invokes the HDF5 wrapper compiler that should be installed
  4. # alongside HDF5. Depending upon the HDF5 Configuration, the wrapper compiler
  5. # is called either h5cc or h5pcc. If this succeeds, the module will then call
  6. # the compiler with the -show argument to see what flags are used when compiling
  7. # an HDF5 client application.
  8. #
  9. # The module will optionally accept the COMPONENTS argument. If no COMPONENTS
  10. # are specified, then the find module will default to finding only the HDF5 C
  11. # library. If one or more COMPONENTS are specified, the module will attempt to
  12. # find the language bindings for the specified components. The only valid
  13. # components are C, CXX, Fortran, HL, and Fortran_HL. If the COMPONENTS
  14. # argument is not given, the module will attempt to find only the C bindings.
  15. #
  16. # On UNIX systems, this module will read the variable HDF5_USE_STATIC_LIBRARIES
  17. # to determine whether or not to prefer a static link to a dynamic link for HDF5
  18. # and all of it's dependencies. To use this feature, make sure that the
  19. # HDF5_USE_STATIC_LIBRARIES variable is set before the call to find_package.
  20. #
  21. # To provide the module with a hint about where to find your HDF5 installation,
  22. # you can set the environment variable HDF5_ROOT. The Find module will then
  23. # look in this path when searching for HDF5 executables, paths, and libraries.
  24. #
  25. # In addition to finding the includes and libraries required to compile an HDF5
  26. # client application, this module also makes an effort to find tools that come
  27. # with the HDF5 distribution that may be useful for regression testing.
  28. #
  29. # This module will define the following variables:
  30. # HDF5_INCLUDE_DIRS - Location of the hdf5 includes
  31. # HDF5_INCLUDE_DIR - Location of the hdf5 includes (deprecated)
  32. # HDF5_DEFINITIONS - Required compiler definitions for HDF5
  33. # HDF5_C_LIBRARIES - Required libraries for the HDF5 C bindings.
  34. # HDF5_CXX_LIBRARIES - Required libraries for the HDF5 C++ bindings
  35. # HDF5_Fortran_LIBRARIES - Required libraries for the HDF5 Fortran bindings
  36. # HDF5_HL_LIBRARIES - Required libraries for the HDF5 high level API
  37. # HDF5_Fortran_HL_LIBRARIES - Required libraries for the high level Fortran
  38. # bindings.
  39. # HDF5_LIBRARIES - Required libraries for all requested bindings
  40. # HDF5_FOUND - true if HDF5 was found on the system
  41. # HDF5_LIBRARY_DIRS - the full set of library directories
  42. # HDF5_IS_PARALLEL - Whether or not HDF5 was found with parallel IO support
  43. # HDF5_C_COMPILER_EXECUTABLE - the path to the HDF5 C wrapper compiler
  44. # HDF5_CXX_COMPILER_EXECUTABLE - the path to the HDF5 C++ wrapper compiler
  45. # HDF5_Fortran_COMPILER_EXECUTABLE - the path to the HDF5 Fortran wrapper compiler
  46. # HDF5_DIFF_EXECUTABLE - the path to the HDF5 dataset comparison tool
  47. #=============================================================================
  48. # Copyright 2009 Kitware, Inc.
  49. #
  50. # Distributed under the OSI-approved BSD License (the "License");
  51. # see accompanying file Copyright.txt for details.
  52. #
  53. # This software is distributed WITHOUT ANY WARRANTY; without even the
  54. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  55. # See the License for more information.
  56. #=============================================================================
  57. # (To distribute this file outside of CMake, substitute the full
  58. # License text for the above reference.)
  59. # This module is maintained by Will Dicharry <[email protected]>.
  60. include(SelectLibraryConfigurations)
  61. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  62. # List of the valid HDF5 components
  63. set( HDF5_VALID_COMPONENTS
  64. C
  65. CXX
  66. Fortran
  67. HL
  68. Fortran_HL
  69. )
  70. # Validate the list of find components.
  71. if( NOT HDF5_FIND_COMPONENTS )
  72. set( HDF5_LANGUAGE_BINDINGS "C" )
  73. else()
  74. # add the extra specified components, ensuring that they are valid.
  75. foreach( component ${HDF5_FIND_COMPONENTS} )
  76. list( FIND HDF5_VALID_COMPONENTS ${component} component_location )
  77. if( ${component_location} EQUAL -1 )
  78. message( FATAL_ERROR
  79. "\"${component}\" is not a valid HDF5 component." )
  80. else()
  81. list( APPEND HDF5_LANGUAGE_BINDINGS ${component} )
  82. endif()
  83. endforeach()
  84. endif()
  85. # try to find the HDF5 wrapper compilers
  86. find_program( HDF5_C_COMPILER_EXECUTABLE
  87. NAMES h5cc h5pcc
  88. HINTS ENV HDF5_ROOT
  89. PATH_SUFFIXES bin Bin
  90. DOC "HDF5 Wrapper compiler. Used only to detect HDF5 compile flags." )
  91. mark_as_advanced( HDF5_C_COMPILER_EXECUTABLE )
  92. find_program( HDF5_CXX_COMPILER_EXECUTABLE
  93. NAMES h5c++ h5pc++
  94. HINTS ENV HDF5_ROOT
  95. PATH_SUFFIXES bin Bin
  96. DOC "HDF5 C++ Wrapper compiler. Used only to detect HDF5 compile flags." )
  97. mark_as_advanced( HDF5_CXX_COMPILER_EXECUTABLE )
  98. find_program( HDF5_Fortran_COMPILER_EXECUTABLE
  99. NAMES h5fc h5pfc
  100. HINTS ENV HDF5_ROOT
  101. PATH_SUFFIXES bin Bin
  102. DOC "HDF5 Fortran Wrapper compiler. Used only to detect HDF5 compile flags." )
  103. mark_as_advanced( HDF5_Fortran_COMPILER_EXECUTABLE )
  104. find_program( HDF5_DIFF_EXECUTABLE
  105. NAMES h5diff
  106. HINTS ENV HDF5_ROOT
  107. PATH_SUFFIXES bin Bin
  108. DOC "HDF5 file differencing tool." )
  109. mark_as_advanced( HDF5_DIFF_EXECUTABLE )
  110. # Invoke the HDF5 wrapper compiler. The compiler return value is stored to the
  111. # return_value argument, the text output is stored to the output variable.
  112. macro( _HDF5_invoke_compiler language output return_value )
  113. if( HDF5_${language}_COMPILER_EXECUTABLE )
  114. exec_program( ${HDF5_${language}_COMPILER_EXECUTABLE}
  115. ARGS -show
  116. OUTPUT_VARIABLE ${output}
  117. RETURN_VALUE ${return_value}
  118. )
  119. if( ${${return_value}} EQUAL 0 )
  120. # do nothing
  121. else()
  122. message( STATUS
  123. "Unable to determine HDF5 ${language} flags from HDF5 wrapper." )
  124. endif()
  125. endif()
  126. endmacro()
  127. # Parse a compile line for definitions, includes, library paths, and libraries.
  128. macro( _HDF5_parse_compile_line
  129. compile_line_var
  130. include_paths
  131. definitions
  132. library_paths
  133. libraries )
  134. # Match the include paths
  135. string( REGEX MATCHALL "-I([^\" ]+)" include_path_flags
  136. "${${compile_line_var}}"
  137. )
  138. foreach( IPATH ${include_path_flags} )
  139. string( REGEX REPLACE "^-I" "" IPATH ${IPATH} )
  140. string( REGEX REPLACE "//" "/" IPATH ${IPATH} )
  141. list( APPEND ${include_paths} ${IPATH} )
  142. endforeach()
  143. # Match the definitions
  144. string( REGEX MATCHALL "-D[^ ]*" definition_flags "${${compile_line_var}}" )
  145. foreach( DEF ${definition_flags} )
  146. list( APPEND ${definitions} ${DEF} )
  147. endforeach()
  148. # Match the library paths
  149. string( REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" library_path_flags
  150. "${${compile_line_var}}"
  151. )
  152. foreach( LPATH ${library_path_flags} )
  153. string( REGEX REPLACE "^-L" "" LPATH ${LPATH} )
  154. string( REGEX REPLACE "//" "/" LPATH ${LPATH} )
  155. list( APPEND ${library_paths} ${LPATH} )
  156. endforeach()
  157. # now search for the library names specified in the compile line (match -l...)
  158. # match only -l's preceded by a space or comma
  159. # this is to exclude directory names like xxx-linux/
  160. string( REGEX MATCHALL "[, ]-l([^\", ]+)" library_name_flags
  161. "${${compile_line_var}}" )
  162. # strip the -l from all of the library flags and add to the search list
  163. foreach( LIB ${library_name_flags} )
  164. string( REGEX REPLACE "^[, ]-l" "" LIB ${LIB} )
  165. list( APPEND ${libraries} ${LIB} )
  166. endforeach()
  167. endmacro()
  168. # Try to find HDF5 using an installed hdf5-config.cmake
  169. if( NOT HDF5_FOUND )
  170. find_package( HDF5 QUIET NO_MODULE )
  171. if( HDF5_FOUND )
  172. set( HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR} )
  173. set( HDF5_LIBRARIES )
  174. set( HDF5_C_TARGET hdf5 )
  175. set( HDF5_CXX_TARGET hdf5_cpp )
  176. set( HDF5_HL_TARGET hdf5_hl )
  177. set( HDF5_Fortran_TARGET hdf5_fortran )
  178. set( HDF5_Fortran_HL_TARGET hdf5_hl_fortran )
  179. foreach( _component ${HDF5_LANGUAGE_BINDINGS} )
  180. list( FIND HDF5_VALID_COMPONENTS ${_component} _component_location )
  181. get_target_property( _comp_location ${HDF5_${_component}_TARGET} LOCATION )
  182. if( _comp_location )
  183. set( HDF5_${_component}_LIBRARY ${_comp_location} CACHE PATH
  184. "HDF5 ${_component} library" )
  185. mark_as_advanced( HDF5_${_component}_LIBRARY )
  186. list( APPEND HDF5_LIBRARIES ${HDF5_${_component}_LIBRARY} )
  187. endif()
  188. endforeach()
  189. endif()
  190. endif()
  191. if( NOT HDF5_FOUND )
  192. _HDF5_invoke_compiler( C HDF5_C_COMPILE_LINE HDF5_C_RETURN_VALUE )
  193. _HDF5_invoke_compiler( CXX HDF5_CXX_COMPILE_LINE HDF5_CXX_RETURN_VALUE )
  194. _HDF5_invoke_compiler( Fortran HDF5_Fortran_COMPILE_LINE HDF5_Fortran_RETURN_VALUE )
  195. # seed the initial lists of libraries to find with items we know we need
  196. set( HDF5_C_LIBRARY_NAMES_INIT hdf5 )
  197. set( HDF5_HL_LIBRARY_NAMES_INIT hdf5_hl ${HDF5_C_LIBRARY_NAMES_INIT} )
  198. set( HDF5_CXX_LIBRARY_NAMES_INIT hdf5_cpp ${HDF5_C_LIBRARY_NAMES_INIT} )
  199. set( HDF5_Fortran_LIBRARY_NAMES_INIT hdf5_fortran
  200. ${HDF5_C_LIBRARY_NAMES_INIT} )
  201. set( HDF5_Fortran_HL_LIBRARY_NAMES_INIT hdf5hl_fortran
  202. ${HDF5_Fortran_LIBRARY_NAMES_INIT} )
  203. foreach( LANGUAGE ${HDF5_LANGUAGE_BINDINGS} )
  204. if( HDF5_${LANGUAGE}_COMPILE_LINE )
  205. _HDF5_parse_compile_line( HDF5_${LANGUAGE}_COMPILE_LINE
  206. HDF5_${LANGUAGE}_INCLUDE_FLAGS
  207. HDF5_${LANGUAGE}_DEFINITIONS
  208. HDF5_${LANGUAGE}_LIBRARY_DIRS
  209. HDF5_${LANGUAGE}_LIBRARY_NAMES
  210. )
  211. # take a guess that the includes may be in the 'include' sibling
  212. # directory of a library directory.
  213. foreach( dir ${HDF5_${LANGUAGE}_LIBRARY_DIRS} )
  214. list( APPEND HDF5_${LANGUAGE}_INCLUDE_FLAGS ${dir}/../include )
  215. endforeach()
  216. endif()
  217. # set the definitions for the language bindings.
  218. list( APPEND HDF5_DEFINITIONS ${HDF5_${LANGUAGE}_DEFINITIONS} )
  219. # find the HDF5 include directories
  220. if(${LANGUAGE} MATCHES "Fortran.*")
  221. set(HDF5_INCLUDE_FILENAME hdf5.mod)
  222. else()
  223. set(HDF5_INCLUDE_FILENAME hdf5.h)
  224. endif()
  225. find_path( HDF5_${LANGUAGE}_INCLUDE_DIR ${HDF5_INCLUDE_FILENAME}
  226. HINTS
  227. ${HDF5_${LANGUAGE}_INCLUDE_FLAGS}
  228. ENV
  229. HDF5_ROOT
  230. PATHS
  231. $ENV{HOME}/.local/include
  232. PATH_SUFFIXES
  233. include
  234. Include
  235. )
  236. mark_as_advanced( HDF5_${LANGUAGE}_INCLUDE_DIR )
  237. list( APPEND HDF5_INCLUDE_DIRS ${HDF5_${LANGUAGE}_INCLUDE_DIR} )
  238. set( HDF5_${LANGUAGE}_LIBRARY_NAMES
  239. ${HDF5_${LANGUAGE}_LIBRARY_NAMES_INIT}
  240. ${HDF5_${LANGUAGE}_LIBRARY_NAMES} )
  241. # find the HDF5 libraries
  242. foreach( LIB ${HDF5_${LANGUAGE}_LIBRARY_NAMES} )
  243. if( UNIX AND HDF5_USE_STATIC_LIBRARIES )
  244. # According to bug 1643 on the CMake bug tracker, this is the
  245. # preferred method for searching for a static library.
  246. # See http://www.cmake.org/Bug/view.php?id=1643. We search
  247. # first for the full static library name, but fall back to a
  248. # generic search on the name if the static search fails.
  249. set( THIS_LIBRARY_SEARCH_DEBUG lib${LIB}d.a ${LIB}d )
  250. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a ${LIB} )
  251. else()
  252. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d )
  253. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} )
  254. endif()
  255. find_library( HDF5_${LIB}_LIBRARY_DEBUG
  256. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  257. HINTS ${HDF5_${LANGUAGE}_LIBRARY_DIRS}
  258. ENV HDF5_ROOT
  259. PATH_SUFFIXES lib Lib )
  260. find_library( HDF5_${LIB}_LIBRARY_RELEASE
  261. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  262. HINTS ${HDF5_${LANGUAGE}_LIBRARY_DIRS}
  263. ENV HDF5_ROOT
  264. PATH_SUFFIXES lib Lib )
  265. select_library_configurations( HDF5_${LIB} )
  266. # even though we adjusted the individual library names in
  267. # select_library_configurations, we still need to distinguish
  268. # between debug and release variants because HDF5_LIBRARIES will
  269. # need to specify different lists for debug and optimized builds.
  270. # We can't just use the HDF5_${LIB}_LIBRARY variable (which was set
  271. # up by the selection macro above) because it may specify debug and
  272. # optimized variants for a particular library, but a list of
  273. # libraries is allowed to specify debug and optimized only once.
  274. list( APPEND HDF5_${LANGUAGE}_LIBRARIES_DEBUG
  275. ${HDF5_${LIB}_LIBRARY_DEBUG} )
  276. list( APPEND HDF5_${LANGUAGE}_LIBRARIES_RELEASE
  277. ${HDF5_${LIB}_LIBRARY_RELEASE} )
  278. endforeach()
  279. list( APPEND HDF5_LIBRARY_DIRS ${HDF5_${LANGUAGE}_LIBRARY_DIRS} )
  280. # Append the libraries for this language binding to the list of all
  281. # required libraries.
  282. list( APPEND HDF5_LIBRARIES_DEBUG
  283. ${HDF5_${LANGUAGE}_LIBRARIES_DEBUG} )
  284. list( APPEND HDF5_LIBRARIES_RELEASE
  285. ${HDF5_${LANGUAGE}_LIBRARIES_RELEASE} )
  286. endforeach()
  287. # We may have picked up some duplicates in various lists during the above
  288. # process for the language bindings (both the C and C++ bindings depend on
  289. # libz for example). Remove the duplicates. It appears that the default
  290. # CMake behavior is to remove duplicates from the end of a list. However,
  291. # for link lines, this is incorrect since unresolved symbols are searched
  292. # for down the link line. Therefore, we reverse the list, remove the
  293. # duplicates, and then reverse it again to get the duplicates removed from
  294. # the beginning.
  295. macro( _remove_duplicates_from_beginning _list_name )
  296. list( REVERSE ${_list_name} )
  297. list( REMOVE_DUPLICATES ${_list_name} )
  298. list( REVERSE ${_list_name} )
  299. endmacro()
  300. if( HDF5_INCLUDE_DIRS )
  301. _remove_duplicates_from_beginning( HDF5_INCLUDE_DIRS )
  302. endif()
  303. if( HDF5_LIBRARIES_DEBUG )
  304. _remove_duplicates_from_beginning( HDF5_LIBRARIES_DEBUG )
  305. endif()
  306. if( HDF5_LIBRARIES_RELEASE )
  307. _remove_duplicates_from_beginning( HDF5_LIBRARIES_RELEASE )
  308. endif()
  309. if( HDF5_LIBRARY_DIRS )
  310. _remove_duplicates_from_beginning( HDF5_LIBRARY_DIRS )
  311. endif()
  312. # Construct the complete list of HDF5 libraries with debug and optimized
  313. # variants when the generator supports them.
  314. if( CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE )
  315. set( HDF5_LIBRARIES )
  316. foreach( _lib ${HDF5_LIBRARIES_DEBUG} )
  317. list( APPEND HDF5_LIBRARIES debug ${_lib} )
  318. endforeach()
  319. foreach( _lib ${HDF5_LIBRARIES_RELEASE} )
  320. list( APPEND HDF5_LIBRARIES optimized ${_lib} )
  321. endforeach()
  322. else()
  323. set( HDF5_LIBRARIES ${HDF5_LIBRARIES_RELEASE} )
  324. endif()
  325. # If the HDF5 include directory was found, open H5pubconf.h to determine if
  326. # HDF5 was compiled with parallel IO support
  327. set( HDF5_IS_PARALLEL FALSE )
  328. foreach( _dir IN LISTS HDF5_INCLUDE_DIRS )
  329. if( EXISTS "${_dir}/H5pubconf.h" )
  330. file( STRINGS "${_dir}/H5pubconf.h"
  331. HDF5_HAVE_PARALLEL_DEFINE
  332. REGEX "HAVE_PARALLEL 1" )
  333. if( HDF5_HAVE_PARALLEL_DEFINE )
  334. set( HDF5_IS_PARALLEL TRUE )
  335. endif()
  336. endif()
  337. endforeach()
  338. set( HDF5_IS_PARALLEL ${HDF5_IS_PARALLEL} CACHE BOOL
  339. "HDF5 library compiled with parallel IO support" )
  340. mark_as_advanced( HDF5_IS_PARALLEL )
  341. # For backwards compatibility we set HDF5_INCLUDE_DIR to the value of
  342. # HDF5_INCLUDE_DIRS
  343. if( HDF5_INCLUDE_DIRS )
  344. set( HDF5_INCLUDE_DIR "${HDF5_INCLUDE_DIRS}" )
  345. endif()
  346. endif()
  347. find_package_handle_standard_args( HDF5 DEFAULT_MSG
  348. HDF5_LIBRARIES
  349. HDF5_INCLUDE_DIRS
  350. )