FindHDF5.cmake 16 KB

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