FindHDF5.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. Currently, the only
  13. # valid components are C and CXX. The module does not yet support finding the
  14. # Fortran bindings. If the COMPONENTS argument is not given, the module will
  15. # attempt to find only the C bindings.
  16. #
  17. # On UNIX systems, this module will read the variable HDF5_USE_STATIC_LIBRARIES
  18. # to determine whether or not to prefer a static link to a dynamic link for HDF5
  19. # and all of it's dependencies. To use this feature, make sure that the
  20. # HDF5_USE_STATIC_LIBRARIES variable is set before the call to find_package.
  21. #
  22. # To provide the module with a hint about where to find your HDF5 installation,
  23. # you can set the environment variable HDF5_ROOT. The Find module will then
  24. # look in this path when searching for HDF5 executables, paths, and libraries.
  25. #
  26. # In addition to finding the includes and libraries required to compile an HDF5
  27. # client application, this module also makes an effort to find tools that come
  28. # with the HDF5 distribution that may be useful for regression testing.
  29. #
  30. # This module will define the following variables:
  31. # HDF5_INCLUDE_DIR - Location of the hdf5 includes
  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_LIBRARIES - Required libraries for all requested bindings
  36. # HDF5_FOUND - true if HDF5 was found on the system
  37. # HDF5_LIBRARY_DIRS - the full set of library directories
  38. # HDF5_IS_PARALLEL - Whether or not HDF5 was found with parallel IO support
  39. # HDF5_C_COMPILER_EXECUTABLE - the path to the HDF5 C wrapper compiler
  40. # HDF5_CXX_COMPILER_EXECUTABLE - the path to the HDF5 C++ wrapper compiler
  41. # HDF5_DIFF_EXECUTABLE - the path to the HDF5 dataset comparison tool
  42. # This module is maintained by Will Dicharry <[email protected]>.
  43. include(SelectLibraryConfigurations)
  44. include(FindPackageHandleStandardArgs)
  45. # List of the valid HDF5 components
  46. set( HDF5_VALID_COMPONENTS
  47. C
  48. CXX
  49. )
  50. # try to find the HDF5 wrapper compilers
  51. find_program( HDF5_C_COMPILER_EXECUTABLE
  52. NAMES h5cc h5pcc
  53. HINTS ENV HDF5_ROOT
  54. PATH_SUFFIXES bin Bin
  55. DOC "HDF5 Wrapper compiler. Used only to detect HDF5 compile flags." )
  56. mark_as_advanced( HDF5_C_COMPILER_EXECUTABLE )
  57. find_program( HDF5_CXX_COMPILER_EXECUTABLE
  58. NAMES h5c++ h5pc++
  59. HINTS ENV HDF5_ROOT
  60. PATH_SUFFIXES bin Bin
  61. DOC "HDF5 C++ Wrapper compiler. Used only to detect HDF5 compile flags." )
  62. mark_as_advanced( HDF5_CXX_COMPILER_EXECUTABLE )
  63. find_program( HDF5_DIFF_EXECUTABLE
  64. NAMES h5diff
  65. HINTS ENV HDF5_ROOT
  66. PATH_SUFFIXES bin Bin
  67. DOC "HDF5 file differencing tool." )
  68. mark_as_advanced( HDF5_DIFF_EXECUTABLE )
  69. # Invoke the HDF5 wrapper compiler. The compiler return value is stored to the
  70. # return_value argument, the text output is stored to the output variable.
  71. macro( _HDF5_invoke_compiler language output return_value )
  72. if( HDF5_${language}_COMPILER_EXECUTABLE )
  73. exec_program( ${HDF5_${language}_COMPILER_EXECUTABLE}
  74. ARGS -show
  75. OUTPUT_VARIABLE ${output}
  76. RETURN_VALUE ${return_value}
  77. )
  78. if( ${${return_value}} EQUAL 0 )
  79. # do nothing
  80. else()
  81. message( STATUS
  82. "Unable to determine HDF5 ${language} flags from HDF5 wrapper." )
  83. endif()
  84. endif()
  85. endmacro()
  86. # Parse a compile line for definitions, includes, library paths, and libraries.
  87. macro( _HDF5_parse_compile_line
  88. compile_line
  89. include_paths
  90. definitions
  91. library_paths
  92. libraries )
  93. # Match the include paths
  94. string( REGEX MATCHALL "-I([^\" ]+)" include_path_flags
  95. "${compile_line}"
  96. )
  97. foreach( IPATH ${include_path_flags} )
  98. string( REGEX REPLACE "^-I" "" IPATH ${IPATH} )
  99. string( REGEX REPLACE "//" "/" IPATH ${IPATH} )
  100. list( APPEND ${include_paths} ${IPATH} )
  101. endforeach()
  102. # Match the definitions
  103. string( REGEX MATCHALL "-D[^ ]*" definition_flags "${compile_line}" )
  104. foreach( DEF ${definition_flags} )
  105. list( APPEND ${definitions} ${DEF} )
  106. endforeach()
  107. # Match the library paths
  108. string( REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" library_path_flags
  109. "${compile_line}"
  110. )
  111. foreach( LPATH ${library_path_flags} )
  112. string( REGEX REPLACE "^-L" "" LPATH ${LPATH} )
  113. string( REGEX REPLACE "//" "/" LPATH ${LPATH} )
  114. list( APPEND ${library_paths} ${LPATH} )
  115. endforeach()
  116. # now search for the library names specified in the compile line (match -l...)
  117. # match only -l's preceded by a space or comma
  118. # this is to exclude directory names like xxx-linux/
  119. string( REGEX MATCHALL "[, ]-l([^\", ]+)" library_name_flags
  120. "${compile_line}" )
  121. # strip the -l from all of the library flags and add to the search list
  122. foreach( LIB ${library_name_flags} )
  123. string( REGEX REPLACE "^[, ]-l" "" LIB ${LIB} )
  124. list( APPEND ${libraries} ${LIB} )
  125. endforeach()
  126. endmacro()
  127. if( HDF5_INCLUDE_DIR AND HDF5_LIBRARIES )
  128. # Do nothing: we already have HDF5_INCLUDE_PATH and HDF5_LIBRARIES in the
  129. # cache, it would be a shame to override them
  130. else()
  131. _HDF5_invoke_compiler( C HDF5_C_COMPILE_LINE HDF5_C_RETURN_VALUE )
  132. _HDF5_invoke_compiler( CXX HDF5_CXX_COMPILE_LINE HDF5_CXX_RETURN_VALUE )
  133. if( NOT HDF5_FIND_COMPONENTS )
  134. set( HDF5_LANGUAGE_BINDINGS "C" )
  135. else()
  136. # add the extra specified components, ensuring that they are valid.
  137. foreach( component ${HDF5_FIND_COMPONENTS} )
  138. list( FIND HDF5_VALID_COMPONENTS ${component} component_location )
  139. if( ${component_location} EQUAL -1 )
  140. message( FATAL_ERROR
  141. "\"${component}\" is not a valid HDF5 component." )
  142. else()
  143. list( APPEND HDF5_LANGUAGE_BINDINGS ${component} )
  144. endif()
  145. endforeach()
  146. endif()
  147. # seed the initial lists of libraries to find with items we know we need
  148. set( HDF5_C_LIBRARY_NAMES_INIT hdf5 hdf5_hl )
  149. set( HDF5_CXX_LIBRARY_NAMES_INIT hdf5_cpp )
  150. foreach( LANGUAGE ${HDF5_LANGUAGE_BINDINGS} )
  151. if( HDF5_${LANGUAGE}_COMPILE_LINE )
  152. _HDF5_parse_compile_line( ${HDF5_${LANGUAGE}_COMPILE_LINE}
  153. HDF5_${LANGUAGE}_INCLUDE_FLAGS
  154. HDF5_${LANGUAGE}_DEFINITIONS
  155. HDF5_${LANGUAGE}_LIBRARY_DIRS
  156. HDF5_${LANGUAGE}_LIBRARY_NAMES
  157. )
  158. # take a guess that the includes may be in the 'include' sibling directory
  159. # of a library directory.
  160. foreach( dir ${HDF5_${LANGUAGE}_LIBRARY_DIRS} )
  161. list( APPEND HDF5_${LANGUAGE}_INCLUDE_FLAGS ${dir}/../include )
  162. endforeach()
  163. endif()
  164. # set the definitions for the language bindings.
  165. list( APPEND HDF5_DEFINITIONS ${HDF5_${LANGUAGE}_DEFINITIONS} )
  166. # find the HDF5 include directories
  167. find_path( HDF5_${LANGUAGE}_INCLUDE_DIR hdf5.h
  168. HINTS
  169. ${HDF5_${LANGUAGE}_INCLUDE_FLAGS}
  170. ENV
  171. HDF5_ROOT
  172. PATHS
  173. $ENV{HOME}/.local/include
  174. PATH_SUFFIXES
  175. include
  176. Include
  177. )
  178. mark_as_advanced( HDF5_${LANGUAGE}_INCLUDE_DIR )
  179. list( APPEND HDF5_INCLUDE_DIR ${HDF5_${LANGUAGE}_INCLUDE_DIR} )
  180. set( HDF5_${LANGUAGE}_LIBRARY_NAMES
  181. ${HDF5_${LANGUAGE}_LIBRARY_NAMES_INIT}
  182. ${HDF5_${LANGUAGE}_LIBRARY_NAMES} )
  183. # find the HDF5 libraries
  184. foreach( LIB ${HDF5_${LANGUAGE}_LIBRARY_NAMES} )
  185. if( UNIX AND HDF5_USE_STATIC_LIBRARIES )
  186. # According to bug 1643 on the CMake bug tracker, this is the
  187. # preferred method for searching for a static library.
  188. # See http://www.cmake.org/Bug/view.php?id=1643. We search
  189. # first for the full static library name, but fall back to a
  190. # generic search on the name if the static search fails.
  191. set( THIS_LIBRARY_SEARCH_DEBUG lib${LIB}d.a ${LIB}d )
  192. set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a ${LIB} )
  193. else()
  194. set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d )
  195. set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} )
  196. endif()
  197. find_library( HDF5_${LIB}_LIBRARY_DEBUG
  198. NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
  199. HINTS ${HDF5_${LANGUAGE}_LIBRARY_DIRS}
  200. ENV HDF5_ROOT
  201. PATH_SUFFIXES lib Lib )
  202. find_library( HDF5_${LIB}_LIBRARY_RELEASE
  203. NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
  204. HINTS ${HDF5_${LANGUAGE}_LIBRARY_DIRS}
  205. ENV HDF5_ROOT
  206. PATH_SUFFIXES lib Lib )
  207. select_library_configurations( HDF5_${LIB} )
  208. # even though we adjusted the individual library names in
  209. # select_library_configurations, we still need to distinguish
  210. # between debug and release variants because HDF5_LIBRARIES will
  211. # need to specify different lists for debug and optimized builds.
  212. # We can't just use the HDF5_${LIB}_LIBRARY variable (which was set
  213. # up by the selection macro above) because it may specify debug and
  214. # optimized variants for a particular library, but a list of
  215. # libraries is allowed to specify debug and optimized only once.
  216. list( APPEND HDF5_${LANGUAGE}_LIBRARIES_DEBUG
  217. ${HDF5_${LIB}_LIBRARY_DEBUG} )
  218. list( APPEND HDF5_${LANGUAGE}_LIBRARIES_RELEASE
  219. ${HDF5_${LIB}_LIBRARY_RELEASE} )
  220. endforeach()
  221. list( APPEND HDF5_LIBRARY_DIRS ${HDF5_${LANGUAGE}_LIBRARY_DIRS} )
  222. # Append the libraries for this language binding to the list of all
  223. # required libraries.
  224. list( APPEND HDF5_LIBRARIES_DEBUG
  225. ${HDF5_${LANGUAGE}_LIBRARIES_DEBUG} )
  226. list( APPEND HDF5_LIBRARIES_RELEASE
  227. ${HDF5_${LANGUAGE}_LIBRARIES_RELEASE} )
  228. endforeach()
  229. # We may have picked up some duplicates in various lists during the above
  230. # process for the language bindings (both the C and C++ bindings depend on
  231. # libz for example). Remove the duplicates.
  232. list( REMOVE_DUPLICATES HDF5_INCLUDE_DIR )
  233. list( REMOVE_DUPLICATES HDF5_LIBRARIES_DEBUG )
  234. list( REMOVE_DUPLICATES HDF5_LIBRARIES_RELEASE )
  235. list( REMOVE_DUPLICATES HDF5_LIBRARY_DIRS )
  236. # Construct the complete list of HDF5 libraries with debug and optimized
  237. # variants when the generator supports them.
  238. if( CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE )
  239. set( HDF5_LIBRARIES
  240. debug ${HDF5_LIBRARIES_DEBUG}
  241. optimized ${HDF5_LIBRARIES_RELEASE} )
  242. else()
  243. set( HDF5_LIBRARIES ${HDF5_LIBRARIES_RELEASE} )
  244. endif()
  245. # If the HDF5 include directory was found, open H5pubconf.h to determine if
  246. # HDF5 was compiled with parallel IO support
  247. if( HDF5_INCLUDE_DIR )
  248. file( STRINGS "${HDF5_INCLUDE_DIR}/H5pubconf.h"
  249. HDF5_HAVE_PARALLEL_DEFINE
  250. REGEX "HAVE_PARALLEL 1" )
  251. if( HDF5_HAVE_PARALLEL_DEFINE )
  252. set( HDF5_IS_PARALLEL TRUE )
  253. else()
  254. set( HDF5_IS_PARALLEL FALSE )
  255. endif()
  256. endif()
  257. set( HDF5_IS_PARALLEL ${HDF5_IS_PARALLEL} CACHE BOOL
  258. "HDF5 library compiled with parallel IO support" )
  259. mark_as_advanced( HDF5_IS_PARALLEL )
  260. endif()
  261. find_package_handle_standard_args( HDF5 DEFAULT_MSG
  262. HDF5_LIBRARIES
  263. HDF5_INCLUDE_DIR
  264. )
  265. mark_as_advanced(
  266. HDF5_INCLUDE_DIR
  267. HDF5_LIBRARIES
  268. HDF5_DEFINTIONS
  269. HDF5_LIBRARY_DIRS
  270. HDF5_C_COMPILER_EXECUTABLE
  271. HDF5_CXX_COMPILER_EXECUTABLE )