FindHDF5.cmake 13 KB

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