FindHDF5.cmake 13 KB

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