1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find Hierarchical Data Format (HDF5), a library for reading and writing
9 self describing array data.
12 This module invokes the ``HDF5`` wrapper compiler that should be installed
13 alongside ``HDF5``. Depending upon the ``HDF5`` Configuration, the wrapper
14 compiler is called either ``h5cc`` or ``h5pcc``. If this succeeds, the module
15 will then call the compiler with the show argument to see what flags
16 are used when compiling an ``HDF5`` client application.
18 The module will optionally accept the ``COMPONENTS`` argument. If no
19 ``COMPONENTS`` are specified, then the find module will default to finding
20 only the ``HDF5`` C library. If one or more ``COMPONENTS`` are specified, the
21 module will attempt to find the language bindings for the specified
22 components. The valid components are ``C``, ``CXX``, ``Fortran``, ``HL``.
23 ``HL`` refers to the "high-level" HDF5 functions for C and Fortran.
24 If the ``COMPONENTS`` argument is not given, the module will
25 attempt to find only the C bindings.
26 For example, to use Fortran HDF5 and HDF5-HL functions, do:
27 ``find_package(HDF5 COMPONENTS Fortran HL)``.
29 This module will read the variable
30 ``HDF5_USE_STATIC_LIBRARIES`` to determine whether or not to prefer a
31 static link to a dynamic link for ``HDF5`` and all of it's dependencies.
32 To use this feature, make sure that the ``HDF5_USE_STATIC_LIBRARIES``
33 variable is set before the call to find_package.
35 Both the serial and parallel ``HDF5`` wrappers are considered and the first
36 directory to contain either one will be used. In the event that both appear
37 in the same directory the serial version is preferentially selected. This
38 behavior can be reversed by setting the variable ``HDF5_PREFER_PARALLEL`` to
41 In addition to finding the includes and libraries required to compile
42 an ``HDF5`` client application, this module also makes an effort to find
43 tools that come with the ``HDF5`` distribution that may be useful for
49 This module will set the following variables in your project:
52 HDF5 was found on the system
56 Location of the HDF5 header files
58 Required compiler definitions for HDF5
60 Required libraries for all requested bindings
62 Required libraries for the HDF5 high level API for all bindings,
63 if the ``HL`` component is enabled
65 Available components are: ``C`` ``CXX`` ``Fortran`` and ``HL``.
66 For each enabled language binding, a corresponding ``HDF5_${LANG}_LIBRARIES``
67 variable, and potentially ``HDF5_${LANG}_DEFINITIONS``, will be defined.
68 If the ``HL`` component is enabled, then an ``HDF5_${LANG}_HL_LIBRARIES`` will
69 also be defined. With all components enabled, the following variables will be defined:
71 ``HDF5_C_DEFINITIONS``
72 Required compiler definitions for HDF5 C bindings
73 ``HDF5_CXX_DEFINITIONS``
74 Required compiler definitions for HDF5 C++ bindings
75 ``HDF5_Fortran_DEFINITIONS``
76 Required compiler definitions for HDF5 Fortran bindings
77 ``HDF5_C_INCLUDE_DIRS``
78 Required include directories for HDF5 C bindings
79 ``HDF5_CXX_INCLUDE_DIRS``
80 Required include directories for HDF5 C++ bindings
81 ``HDF5_Fortran_INCLUDE_DIRS``
82 Required include directories for HDF5 Fortran bindings
84 Required libraries for the HDF5 C bindings
85 ``HDF5_CXX_LIBRARIES``
86 Required libraries for the HDF5 C++ bindings
87 ``HDF5_Fortran_LIBRARIES``
88 Required libraries for the HDF5 Fortran bindings
89 ``HDF5_C_HL_LIBRARIES``
90 Required libraries for the high level C bindings
91 ``HDF5_CXX_HL_LIBRARIES``
92 Required libraries for the high level C++ bindings
93 ``HDF5_Fortran_HL_LIBRARIES``
94 Required libraries for the high level Fortran bindings.
97 HDF5 library has parallel IO support
98 ``HDF5_C_COMPILER_EXECUTABLE``
99 path to the HDF5 C wrapper compiler
100 ``HDF5_CXX_COMPILER_EXECUTABLE``
101 path to the HDF5 C++ wrapper compiler
102 ``HDF5_Fortran_COMPILER_EXECUTABLE``
103 path to the HDF5 Fortran wrapper compiler
104 ``HDF5_C_COMPILER_EXECUTABLE_NO_INTERROGATE``
105 path to the primary C compiler which is also the HDF5 wrapper
106 ``HDF5_CXX_COMPILER_EXECUTABLE_NO_INTERROGATE``
107 path to the primary C++ compiler which is also the HDF5 wrapper
108 ``HDF5_Fortran_COMPILER_EXECUTABLE_NO_INTERROGATE``
109 path to the primary Fortran compiler which is also the HDF5 wrapper
110 ``HDF5_DIFF_EXECUTABLE``
111 path to the HDF5 dataset comparison tool
116 The following variables can be set to guide the search for HDF5 libraries and includes:
118 ``HDF5_PREFER_PARALLEL``
119 set ``true`` to prefer parallel HDF5 (by default, serial is preferred)
122 Set ``true`` to get extra debugging output.
124 ``HDF5_NO_FIND_PACKAGE_CONFIG_FILE``
125 Set ``true`` to skip trying to find ``hdf5-config.cmake``.
126 #]=======================================================================]
128 # This module is maintained by Will Dicharry <wdicharry@stellarscience.com>.
130 include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
131 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
133 # List of the valid HDF5 components
134 set(HDF5_VALID_LANGUAGE_BINDINGS C CXX Fortran)
136 # Validate the list of find components.
137 if(NOT HDF5_FIND_COMPONENTS)
138 set(HDF5_LANGUAGE_BINDINGS "C")
140 set(HDF5_LANGUAGE_BINDINGS)
141 # add the extra specified components, ensuring that they are valid.
142 set(HDF5_FIND_HL OFF)
143 foreach(_component IN LISTS HDF5_FIND_COMPONENTS)
144 list(FIND HDF5_VALID_LANGUAGE_BINDINGS ${_component} _component_location)
145 if(NOT _component_location EQUAL -1)
146 list(APPEND HDF5_LANGUAGE_BINDINGS ${_component})
147 elseif(_component STREQUAL "HL")
149 elseif(_component STREQUAL "Fortran_HL") # only for compatibility
150 list(APPEND HDF5_LANGUAGE_BINDINGS Fortran)
152 set(HDF5_FIND_REQUIRED_Fortran_HL FALSE)
153 set(HDF5_FIND_REQUIRED_Fortran TRUE)
154 set(HDF5_FIND_REQUIRED_HL TRUE)
156 message(FATAL_ERROR "${_component} is not a valid HDF5 component.")
160 unset(_component_location)
161 if(NOT HDF5_LANGUAGE_BINDINGS)
162 get_property(_langs GLOBAL PROPERTY ENABLED_LANGUAGES)
163 foreach(_lang IN LISTS _langs)
164 if(_lang MATCHES "^(C|CXX|Fortran)$")
165 list(APPEND HDF5_LANGUAGE_BINDINGS ${_lang})
169 list(REMOVE_ITEM HDF5_FIND_COMPONENTS Fortran_HL) # replaced by Fortran and HL
170 list(REMOVE_DUPLICATES HDF5_LANGUAGE_BINDINGS)
173 # Determine whether to search for serial or parallel executable first
174 if(HDF5_PREFER_PARALLEL)
175 set(HDF5_C_COMPILER_NAMES h5pcc h5cc)
176 set(HDF5_CXX_COMPILER_NAMES h5pc++ h5c++)
177 set(HDF5_Fortran_COMPILER_NAMES h5pfc h5fc)
179 set(HDF5_C_COMPILER_NAMES h5cc h5pcc)
180 set(HDF5_CXX_COMPILER_NAMES h5c++ h5pc++)
181 set(HDF5_Fortran_COMPILER_NAMES h5fc h5pfc)
184 # We may have picked up some duplicates in various lists during the above
185 # process for the language bindings (both the C and C++ bindings depend on
186 # libz for example). Remove the duplicates. It appears that the default
187 # CMake behavior is to remove duplicates from the end of a list. However,
188 # for link lines, this is incorrect since unresolved symbols are searched
189 # for down the link line. Therefore, we reverse the list, remove the
190 # duplicates, and then reverse it again to get the duplicates removed from
192 macro(_HDF5_remove_duplicates_from_beginning _list_name)
194 list(REVERSE ${_list_name})
195 list(REMOVE_DUPLICATES ${_list_name})
196 list(REVERSE ${_list_name})
201 # Test first if the current compilers automatically wrap HDF5
203 function(_HDF5_test_regular_compiler_C success version is_parallel)
204 set(scratch_directory
205 ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
207 NOT EXISTS ${scratch_directory}/compiler_has_h5_c)
208 set(test_file ${scratch_directory}/cmake_hdf5_test.c)
209 file(WRITE ${test_file}
210 "#include <hdf5.h>\n"
211 "#include <hdf5_hl.h>\n"
212 "const char* info_ver = \"INFO\" \":\" H5_VERSION;\n"
213 "#ifdef H5_HAVE_PARALLEL\n"
214 "const char* info_parallel = \"INFO\" \":\" \"PARALLEL\";\n"
216 "int main(int argc, char **argv) {\n"
217 " int require = 0;\n"
218 " require += info_ver[argc];\n"
219 "#ifdef H5_HAVE_PARALLEL\n"
220 " require += info_parallel[argc];\n"
223 " fid = H5Fcreate(\"foo.h5\",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);\n"
226 try_compile(${success} ${scratch_directory} ${test_file}
227 COPY_FILE ${scratch_directory}/compiler_has_h5_c
231 file(STRINGS ${scratch_directory}/compiler_has_h5_c INFO_STRINGS
234 string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
235 INFO_VER "${INFO_STRINGS}"
237 set(${version} ${CMAKE_MATCH_1})
239 set(${version} ${HDF5_C_VERSION}.${CMAKE_MATCH_3})
241 set(${version} ${${version}} PARENT_SCOPE)
243 if(INFO_STRINGS MATCHES "INFO:PARALLEL")
244 set(${is_parallel} TRUE PARENT_SCOPE)
246 set(${is_parallel} FALSE PARENT_SCOPE)
251 function(_HDF5_test_regular_compiler_CXX success version is_parallel)
252 set(scratch_directory ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
254 NOT EXISTS ${scratch_directory}/compiler_has_h5_cxx)
255 set(test_file ${scratch_directory}/cmake_hdf5_test.cxx)
256 file(WRITE ${test_file}
257 "#include <H5Cpp.h>\n"
258 "#ifndef H5_NO_NAMESPACE\n"
259 "using namespace H5;\n"
261 "const char* info_ver = \"INFO\" \":\" H5_VERSION;\n"
262 "#ifdef H5_HAVE_PARALLEL\n"
263 "const char* info_parallel = \"INFO\" \":\" \"PARALLEL\";\n"
265 "int main(int argc, char **argv) {\n"
266 " int require = 0;\n"
267 " require += info_ver[argc];\n"
268 "#ifdef H5_HAVE_PARALLEL\n"
269 " require += info_parallel[argc];\n"
271 " H5File file(\"foo.h5\", H5F_ACC_TRUNC);\n"
274 try_compile(${success} ${scratch_directory} ${test_file}
275 COPY_FILE ${scratch_directory}/compiler_has_h5_cxx
279 file(STRINGS ${scratch_directory}/compiler_has_h5_cxx INFO_STRINGS
282 string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
283 INFO_VER "${INFO_STRINGS}"
285 set(${version} ${CMAKE_MATCH_1})
287 set(${version} ${HDF5_CXX_VERSION}.${CMAKE_MATCH_3})
289 set(${version} ${${version}} PARENT_SCOPE)
291 if(INFO_STRINGS MATCHES "INFO:PARALLEL")
292 set(${is_parallel} TRUE PARENT_SCOPE)
294 set(${is_parallel} FALSE PARENT_SCOPE)
299 function(_HDF5_test_regular_compiler_Fortran success is_parallel)
301 set(scratch_directory
302 ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
303 set(test_file ${scratch_directory}/cmake_hdf5_test.f90)
304 file(WRITE ${test_file}
305 "program hdf5_hello\n"
310 " call h5open_f(error)\n"
311 " call h5close_f(error)\n"
313 try_compile(${success} ${scratch_directory} ${test_file})
315 execute_process(COMMAND ${CMAKE_Fortran_COMPILER} -showconfig
316 OUTPUT_VARIABLE config_output
317 ERROR_VARIABLE config_error
318 RESULT_VARIABLE config_result
320 if(config_output MATCHES "Parallel HDF5: yes")
321 set(${is_parallel} TRUE PARENT_SCOPE)
323 set(${is_parallel} FALSE PARENT_SCOPE)
329 # Invoke the HDF5 wrapper compiler. The compiler return value is stored to the
330 # return_value argument, the text output is stored to the output variable.
331 function( _HDF5_invoke_compiler language output_var return_value_var version_var is_parallel_var)
332 set(is_parallel FALSE)
333 if(HDF5_USE_STATIC_LIBRARIES)
334 set(lib_type_args -noshlib)
336 set(lib_type_args -shlib)
338 set(scratch_dir ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
339 if("${language}" STREQUAL "C")
340 set(test_file ${scratch_dir}/cmake_hdf5_test.c)
341 elseif("${language}" STREQUAL "CXX")
342 set(test_file ${scratch_dir}/cmake_hdf5_test.cxx)
343 elseif("${language}" STREQUAL "Fortran")
344 set(test_file ${scratch_dir}/cmake_hdf5_test.f90)
346 # Verify that the compiler wrapper can actually compile: sometimes the compiler
347 # wrapper exists, but not the compiler. E.g. Miniconda / Anaconda Python
349 COMMAND ${HDF5_${language}_COMPILER_EXECUTABLE} ${test_file}
350 RESULT_VARIABLE return_value
354 "HDF5 ${language} compiler wrapper is unable to compile a minimal HDF5 program.")
357 COMMAND ${HDF5_${language}_COMPILER_EXECUTABLE} -show ${lib_type_args} ${test_file}
358 OUTPUT_VARIABLE output
359 ERROR_VARIABLE output
360 RESULT_VARIABLE return_value
361 OUTPUT_STRIP_TRAILING_WHITESPACE
365 "Unable to determine HDF5 ${language} flags from HDF5 wrapper.")
368 COMMAND ${HDF5_${language}_COMPILER_EXECUTABLE} -showconfig
369 OUTPUT_VARIABLE config_output
370 ERROR_VARIABLE config_output
371 RESULT_VARIABLE return_value
372 OUTPUT_STRIP_TRAILING_WHITESPACE
376 "Unable to determine HDF5 ${language} version_var from HDF5 wrapper.")
378 string(REGEX MATCH "HDF5 Version: ([a-zA-Z0-9\\.\\-]*)" version "${config_output}")
380 string(REPLACE "HDF5 Version: " "" version "${version}")
381 string(REPLACE "-patch" "." version "${version}")
383 if(config_output MATCHES "Parallel HDF5: yes")
384 set(is_parallel TRUE)
387 foreach(var output return_value version is_parallel)
388 set(${${var}_var} ${${var}} PARENT_SCOPE)
392 # Parse a compile line for definitions, includes, library paths, and libraries.
393 function(_HDF5_parse_compile_line compile_line_var include_paths definitions
394 library_paths libraries libraries_hl)
396 separate_arguments(_compile_args NATIVE_COMMAND "${${compile_line_var}}")
398 foreach(_arg IN LISTS _compile_args)
399 if("${_arg}" MATCHES "^-I(.*)$")
401 list(APPEND include_paths "${CMAKE_MATCH_1}")
402 elseif("${_arg}" MATCHES "^-D(.*)$")
404 list(APPEND definitions "-D${CMAKE_MATCH_1}")
405 elseif("${_arg}" MATCHES "^-L(.*)$")
406 # library search path
407 list(APPEND library_paths "${CMAKE_MATCH_1}")
408 elseif("${_arg}" MATCHES "^-l(hdf5.*hl.*)$")
410 list(APPEND libraries_hl "${CMAKE_MATCH_1}")
411 elseif("${_arg}" MATCHES "^-l(.*)$")
413 list(APPEND libraries "${CMAKE_MATCH_1}")
414 elseif("${_arg}" MATCHES "^(.:)?[/\\].*\\.(a|so|dylib|sl|lib)$")
416 if(NOT EXISTS "${_arg}")
419 get_filename_component(_lpath "${_arg}" DIRECTORY)
420 get_filename_component(_lname "${_arg}" NAME_WE)
421 string(REGEX REPLACE "^lib" "" _lname "${_lname}")
422 list(APPEND library_paths "${_lpath}")
423 if(_lname MATCHES "hdf5.*hl")
424 list(APPEND libraries_hl "${_lname}")
426 list(APPEND libraries "${_lname}")
430 foreach(var include_paths definitions library_paths libraries libraries_hl)
431 set(${${var}_var} ${${var}} PARENT_SCOPE)
435 # Select a preferred imported configuration from a target
436 function(_HDF5_select_imported_config target imported_conf)
437 # We will first assign the value to a local variable _imported_conf, then assign
438 # it to the function argument at the end.
439 get_target_property(_imported_conf ${target} MAP_IMPORTED_CONFIG_${CMAKE_BUILD_TYPE})
440 if (NOT _imported_conf)
441 # Get available imported configurations by examining target properties
442 get_target_property(_imported_conf ${target} IMPORTED_CONFIGURATIONS)
444 message(STATUS "Found imported configurations: ${_imported_conf}")
446 # Find the imported configuration that we prefer.
447 # We do this by making list of configurations in order of preference,
448 # starting with ${CMAKE_BUILD_TYPE} and ending with the first imported_conf
449 set(_preferred_confs ${CMAKE_BUILD_TYPE})
450 list(GET _imported_conf 0 _fallback_conf)
451 list(APPEND _preferred_confs RELWITHDEBINFO RELEASE DEBUG ${_fallback_conf})
453 message(STATUS "Start search through imported configurations in the following order: ${_preferred_confs}")
455 # Now find the first of these that is present in imported_conf
457 cmake_policy(SET CMP0057 NEW) # support IN_LISTS
458 foreach (_conf IN LISTS _preferred_confs)
459 if (${_conf} IN_LIST _imported_conf)
460 set(_imported_conf ${_conf})
467 message(STATUS "Selected imported configuration: ${_imported_conf}")
469 # assign value to function argument
470 set(${imported_conf} ${_imported_conf} PARENT_SCOPE)
475 set(HDF5_ROOT $ENV{HDF5_ROOT})
478 set(_HDF5_SEARCH_OPTS NO_DEFAULT_PATH)
480 set(_HDF5_SEARCH_OPTS)
483 # Try to find HDF5 using an installed hdf5-config.cmake
484 if(NOT HDF5_FOUND AND NOT HDF5_NO_FIND_PACKAGE_CONFIG_FILE)
485 find_package(HDF5 QUIET NO_MODULE
491 message(STATUS "Found HDF5 at ${HDF5_DIR} via NO_MODULE. Now trying to extract locations etc.")
493 set(HDF5_IS_PARALLEL ${HDF5_ENABLE_PARALLEL})
494 set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR})
496 if (NOT TARGET hdf5 AND NOT TARGET hdf5-static AND NOT TARGET hdf5-shared)
497 # Some HDF5 versions (e.g. 1.8.18) used hdf5::hdf5 etc
498 set(_target_prefix "hdf5::")
500 set(HDF5_C_TARGET ${_target_prefix}hdf5)
501 set(HDF5_C_HL_TARGET ${_target_prefix}hdf5_hl)
502 set(HDF5_CXX_TARGET ${_target_prefix}hdf5_cpp)
503 set(HDF5_CXX_HL_TARGET ${_target_prefix}hdf5_hl_cpp)
504 set(HDF5_Fortran_TARGET ${_target_prefix}hdf5_fortran)
505 set(HDF5_Fortran_HL_TARGET ${_target_prefix}hdf5_hl_fortran)
506 set(HDF5_DEFINITIONS "")
507 if(HDF5_USE_STATIC_LIBRARIES)
508 set(_suffix "-static")
510 set(_suffix "-shared")
512 foreach(_lang ${HDF5_LANGUAGE_BINDINGS})
514 #Older versions of hdf5 don't have a static/shared suffix so
515 #if we detect that occurrence clear the suffix
516 if(_suffix AND NOT TARGET ${HDF5_${_lang}_TARGET}${_suffix})
517 if(NOT TARGET ${HDF5_${_lang}_TARGET})
518 #can't find this component with or without the suffix
519 #so bail out, and let the following locate HDF5
520 set(HDF5_FOUND FALSE)
527 message(STATUS "Trying to get properties of target ${HDF5_${_lang}_TARGET}${_suffix}")
529 # Find library for this target. Complicated as on Windows with a DLL, we need to search for the import-lib.
530 _HDF5_select_imported_config(${HDF5_${_lang}_TARGET}${_suffix} _hdf5_imported_conf)
531 get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
532 if (NOT _hdf5_lang_location)
533 # no import lib, just try LOCATION
534 get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION_${_hdf5_imported_conf})
535 if (NOT _hdf5_lang_location)
536 get_target_property(_hdf5_lang_location ${HDF5_${_lang}_TARGET}${_suffix} LOCATION)
539 if( _hdf5_lang_location )
540 set(HDF5_${_lang}_LIBRARY ${_hdf5_lang_location})
541 list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
542 set(HDF5_${_lang}_LIBRARIES ${HDF5_${_lang}_TARGET}${_suffix})
543 set(HDF5_${_lang}_FOUND TRUE)
546 get_target_property(_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} IMPORTED_IMPLIB_${_hdf5_imported_conf} )
547 if (NOT _hdf5_lang_hl_location)
548 get_target_property(_hdf5_lang_hl_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION_${_hdf5_imported_conf})
549 if (NOT _hdf5_hl_lang_location)
550 get_target_property(_hdf5_hl_lang_location ${HDF5_${_lang}_HL_TARGET}${_suffix} LOCATION)
553 if( _hdf5_lang_hl_location )
554 set(HDF5_${_lang}_HL_LIBRARY ${_hdf5_lang_hl_location})
555 list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_TARGET}${_suffix})
556 set(HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_HL_TARGET}${_suffix})
557 set(HDF5_HL_FOUND TRUE)
559 unset(_hdf5_lang_hl_location)
561 unset(_hdf5_imported_conf)
562 unset(_hdf5_lang_location)
568 set(_HDF5_NEED_TO_SEARCH FALSE)
569 set(HDF5_COMPILER_NO_INTERROGATE TRUE)
570 # Only search for languages we've enabled
571 foreach(_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
572 # First check to see if our regular compiler is one of wrappers
573 if(_lang STREQUAL "C")
574 _HDF5_test_regular_compiler_C(
575 HDF5_${_lang}_COMPILER_NO_INTERROGATE
576 HDF5_${_lang}_VERSION
577 HDF5_${_lang}_IS_PARALLEL)
578 elseif(_lang STREQUAL "CXX")
579 _HDF5_test_regular_compiler_CXX(
580 HDF5_${_lang}_COMPILER_NO_INTERROGATE
581 HDF5_${_lang}_VERSION
582 HDF5_${_lang}_IS_PARALLEL)
583 elseif(_lang STREQUAL "Fortran")
584 _HDF5_test_regular_compiler_Fortran(
585 HDF5_${_lang}_COMPILER_NO_INTERROGATE
586 HDF5_${_lang}_IS_PARALLEL)
590 if(HDF5_${_lang}_COMPILER_NO_INTERROGATE)
592 message(STATUS "HDF5: Using hdf5 compiler wrapper for all ${_lang} compiling")
594 set(HDF5_${_lang}_FOUND TRUE)
595 set(HDF5_${_lang}_COMPILER_EXECUTABLE_NO_INTERROGATE
596 "${CMAKE_${_lang}_COMPILER}"
597 CACHE FILEPATH "HDF5 ${_lang} compiler wrapper")
598 set(HDF5_${_lang}_DEFINITIONS)
599 set(HDF5_${_lang}_INCLUDE_DIRS)
600 set(HDF5_${_lang}_LIBRARIES)
601 set(HDF5_${_lang}_HL_LIBRARIES)
603 mark_as_advanced(HDF5_${_lang}_COMPILER_EXECUTABLE_NO_INTERROGATE)
605 set(HDF5_${_lang}_FOUND TRUE)
606 set(HDF5_HL_FOUND TRUE)
608 set(HDF5_COMPILER_NO_INTERROGATE FALSE)
609 # If this language isn't using the wrapper, then try to seed the
610 # search options with the wrapper
611 find_program(HDF5_${_lang}_COMPILER_EXECUTABLE
612 NAMES ${HDF5_${_lang}_COMPILER_NAMES} NAMES_PER_DIR
614 PATH_SUFFIXES bin Bin
615 DOC "HDF5 ${_lang} Wrapper compiler. Used only to detect HDF5 compile flags."
618 mark_as_advanced( HDF5_${_lang}_COMPILER_EXECUTABLE )
619 unset(HDF5_${_lang}_COMPILER_NAMES)
621 if(HDF5_${_lang}_COMPILER_EXECUTABLE)
622 _HDF5_invoke_compiler(${_lang} HDF5_${_lang}_COMPILE_LINE
623 HDF5_${_lang}_RETURN_VALUE HDF5_${_lang}_VERSION HDF5_${_lang}_IS_PARALLEL)
624 if(HDF5_${_lang}_RETURN_VALUE EQUAL 0)
626 message(STATUS "HDF5: Using hdf5 compiler wrapper to determine ${_lang} configuration")
628 _HDF5_parse_compile_line( HDF5_${_lang}_COMPILE_LINE
629 HDF5_${_lang}_INCLUDE_DIRS
630 HDF5_${_lang}_DEFINITIONS
631 HDF5_${_lang}_LIBRARY_DIRS
632 HDF5_${_lang}_LIBRARY_NAMES
633 HDF5_${_lang}_HL_LIBRARY_NAMES
635 set(HDF5_${_lang}_LIBRARIES)
637 foreach(_lib IN LISTS HDF5_${_lang}_LIBRARY_NAMES)
638 set(_HDF5_SEARCH_NAMES_LOCAL)
639 if("x${_lib}" MATCHES "hdf5")
641 set(_HDF5_SEARCH_OPTS_LOCAL ${_HDF5_SEARCH_OPTS})
642 if(HDF5_USE_STATIC_LIBRARIES)
644 set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib})
646 set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib}.a)
651 set(_HDF5_SEARCH_OPTS_LOCAL)
653 find_library(HDF5_${_lang}_LIBRARY_${_lib}
654 NAMES ${_HDF5_SEARCH_NAMES_LOCAL} ${_lib} NAMES_PER_DIR
655 HINTS ${HDF5_${_lang}_LIBRARY_DIRS}
657 ${_HDF5_SEARCH_OPTS_LOCAL}
659 unset(_HDF5_SEARCH_OPTS_LOCAL)
660 unset(_HDF5_SEARCH_NAMES_LOCAL)
661 if(HDF5_${_lang}_LIBRARY_${_lib})
662 list(APPEND HDF5_${_lang}_LIBRARIES ${HDF5_${_lang}_LIBRARY_${_lib}})
664 list(APPEND HDF5_${_lang}_LIBRARIES ${_lib})
668 set(HDF5_${_lang}_HL_LIBRARIES)
669 foreach(_lib IN LISTS HDF5_${_lang}_HL_LIBRARY_NAMES)
670 set(_HDF5_SEARCH_NAMES_LOCAL)
671 if("x${_lib}" MATCHES "hdf5")
673 set(_HDF5_SEARCH_OPTS_LOCAL ${_HDF5_SEARCH_OPTS})
674 if(HDF5_USE_STATIC_LIBRARIES)
676 set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib})
678 set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib}.a)
683 set(_HDF5_SEARCH_OPTS_LOCAL)
685 find_library(HDF5_${_lang}_LIBRARY_${_lib}
686 NAMES ${_HDF5_SEARCH_NAMES_LOCAL} ${_lib} NAMES_PER_DIR
687 HINTS ${HDF5_${_lang}_LIBRARY_DIRS}
689 ${_HDF5_SEARCH_OPTS_LOCAL}
691 unset(_HDF5_SEARCH_OPTS_LOCAL)
692 unset(_HDF5_SEARCH_NAMES_LOCAL)
693 if(HDF5_${_lang}_LIBRARY_${_lib})
694 list(APPEND HDF5_${_lang}_HL_LIBRARIES ${HDF5_${_lang}_LIBRARY_${_lib}})
696 list(APPEND HDF5_${_lang}_HL_LIBRARIES ${_lib})
699 set(HDF5_HL_FOUND TRUE)
702 set(HDF5_${_lang}_FOUND TRUE)
703 _HDF5_remove_duplicates_from_beginning(HDF5_${_lang}_DEFINITIONS)
704 _HDF5_remove_duplicates_from_beginning(HDF5_${_lang}_INCLUDE_DIRS)
705 _HDF5_remove_duplicates_from_beginning(HDF5_${_lang}_LIBRARIES)
706 _HDF5_remove_duplicates_from_beginning(HDF5_${_lang}_HL_LIBRARIES)
708 set(_HDF5_NEED_TO_SEARCH TRUE)
711 set(_HDF5_NEED_TO_SEARCH TRUE)
714 if(HDF5_${_lang}_VERSION)
716 set(HDF5_VERSION ${HDF5_${_lang}_VERSION})
717 elseif(NOT HDF5_VERSION VERSION_EQUAL HDF5_${_lang}_VERSION)
718 message(WARNING "HDF5 Version found for language ${_lang}, ${HDF5_${_lang}_VERSION} is different than previously found version ${HDF5_VERSION}")
721 if(DEFINED HDF5_${_lang}_IS_PARALLEL)
722 if(NOT DEFINED HDF5_IS_PARALLEL)
723 set(HDF5_IS_PARALLEL ${HDF5_${_lang}_IS_PARALLEL})
724 elseif(NOT HDF5_IS_PARALLEL AND HDF5_${_lang}_IS_PARALLEL)
725 message(WARNING "HDF5 found for language ${_lang} is parallel but previously found language is not parallel.")
726 elseif(HDF5_IS_PARALLEL AND NOT HDF5_${_lang}_IS_PARALLEL)
727 message(WARNING "HDF5 found for language ${_lang} is not parallel but previously found language is parallel.")
733 set(_HDF5_NEED_TO_SEARCH TRUE)
736 if(NOT HDF5_FOUND AND HDF5_COMPILER_NO_INTERROGATE)
737 # No arguments necessary, all languages can use the compiler wrappers
739 set(HDF5_METHOD "Included by compiler wrappers")
740 set(HDF5_REQUIRED_VARS HDF5_METHOD)
741 elseif(NOT HDF5_FOUND AND NOT _HDF5_NEED_TO_SEARCH)
742 # Compiler wrappers aren't being used by the build but were found and used
743 # to determine necessary include and library flags
744 set(HDF5_INCLUDE_DIRS)
746 set(HDF5_HL_LIBRARIES)
747 foreach(_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
748 if(HDF5_${_lang}_FOUND)
749 if(NOT HDF5_${_lang}_COMPILER_NO_INTERROGATE)
750 list(APPEND HDF5_DEFINITIONS ${HDF5_${_lang}_DEFINITIONS})
751 list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${_lang}_INCLUDE_DIRS})
752 list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_LIBRARIES})
754 list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARIES})
759 _HDF5_remove_duplicates_from_beginning(HDF5_DEFINITIONS)
760 _HDF5_remove_duplicates_from_beginning(HDF5_INCLUDE_DIRS)
761 _HDF5_remove_duplicates_from_beginning(HDF5_LIBRARIES)
762 _HDF5_remove_duplicates_from_beginning(HDF5_HL_LIBRARIES)
764 set(HDF5_REQUIRED_VARS HDF5_LIBRARIES)
766 list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
770 find_program( HDF5_DIFF_EXECUTABLE
773 PATH_SUFFIXES bin Bin
775 DOC "HDF5 file differencing tool." )
776 mark_as_advanced( HDF5_DIFF_EXECUTABLE )
779 # seed the initial lists of libraries to find with items we know we need
780 set(HDF5_C_LIBRARY_NAMES hdf5)
781 set(HDF5_C_HL_LIBRARY_NAMES hdf5_hl ${HDF5_C_LIBRARY_NAMES} )
783 set(HDF5_CXX_LIBRARY_NAMES hdf5_cpp ${HDF5_C_LIBRARY_NAMES})
784 set(HDF5_CXX_HL_LIBRARY_NAMES hdf5_hl_cpp ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_CXX_LIBRARY_NAMES})
786 set(HDF5_Fortran_LIBRARY_NAMES hdf5_fortran ${HDF5_C_LIBRARY_NAMES})
787 set(HDF5_Fortran_HL_LIBRARY_NAMES hdf5hl_fortran ${HDF5_C_HL_LIBRARY_NAMES} ${HDF5_Fortran_LIBRARY_NAMES})
789 # suffixes as seen on Linux, MSYS2, ...
790 set(_lib_suffixes hdf5)
791 if(NOT HDF5_PREFER_PARALLEL)
792 list(APPEND _lib_suffixes hdf5/serial)
794 if(HDF5_USE_STATIC_LIBRARIES)
795 set(_inc_suffixes include/static)
797 set(_inc_suffixes include/shared)
800 foreach(_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
801 # find the HDF5 include directories
802 if("${_lang}" STREQUAL "Fortran")
803 set(HDF5_INCLUDE_FILENAME hdf5.mod HDF5.mod)
804 elseif("${_lang}" STREQUAL "CXX")
805 set(HDF5_INCLUDE_FILENAME H5Cpp.h)
807 set(HDF5_INCLUDE_FILENAME hdf5.h)
810 find_path(HDF5_${_lang}_INCLUDE_DIR ${HDF5_INCLUDE_FILENAME}
812 PATHS $ENV{HOME}/.local/include
813 PATH_SUFFIXES include Include ${_inc_suffixes} ${_lib_suffixes}
816 mark_as_advanced(HDF5_${_lang}_INCLUDE_DIR)
817 # set the _DIRS variable as this is what the user will normally use
818 set(HDF5_${_lang}_INCLUDE_DIRS ${HDF5_${_lang}_INCLUDE_DIR})
819 list(APPEND HDF5_INCLUDE_DIRS ${HDF5_${_lang}_INCLUDE_DIR})
821 # find the HDF5 libraries
822 foreach(LIB IN LISTS HDF5_${_lang}_LIBRARY_NAMES)
823 if(HDF5_USE_STATIC_LIBRARIES)
824 # According to bug 1643 on the CMake bug tracker, this is the
825 # preferred method for searching for a static library.
826 # See https://gitlab.kitware.com/cmake/cmake/-/issues/1643. We search
827 # first for the full static library name, but fall back to a
828 # generic search on the name if the static search fails.
829 set( THIS_LIBRARY_SEARCH_DEBUG
830 lib${LIB}d.a lib${LIB}_debug.a lib${LIB}d lib${LIB}_D lib${LIB}_debug
831 lib${LIB}d-static.a lib${LIB}_debug-static.a ${LIB}d-static ${LIB}_D-static ${LIB}_debug-static )
832 set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a lib${LIB} lib${LIB}-static.a ${LIB}-static)
834 set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_D ${LIB}_debug ${LIB}d-shared ${LIB}_D-shared ${LIB}_debug-shared)
835 set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
837 list(APPEND HDF5_DEFINITIONS "-DH5_BUILT_AS_DYNAMIC_LIB")
840 find_library(HDF5_${LIB}_LIBRARY_DEBUG
841 NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
842 HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
845 find_library(HDF5_${LIB}_LIBRARY_RELEASE
846 NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
847 HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
851 select_library_configurations( HDF5_${LIB} )
852 list(APPEND HDF5_${_lang}_LIBRARIES ${HDF5_${LIB}_LIBRARY})
854 if(HDF5_${_lang}_LIBRARIES)
855 set(HDF5_${_lang}_FOUND TRUE)
858 # Append the libraries for this language binding to the list of all
859 # required libraries.
860 list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_LIBRARIES})
863 foreach(LIB IN LISTS HDF5_${_lang}_HL_LIBRARY_NAMES)
864 if(HDF5_USE_STATIC_LIBRARIES)
865 # According to bug 1643 on the CMake bug tracker, this is the
866 # preferred method for searching for a static library.
867 # See https://gitlab.kitware.com/cmake/cmake/-/issues/1643. We search
868 # first for the full static library name, but fall back to a
869 # generic search on the name if the static search fails.
870 set( THIS_LIBRARY_SEARCH_DEBUG
871 lib${LIB}d.a lib${LIB}_debug.a lib${LIB}d lib${LIB}_D lib${LIB}_debug
872 lib${LIB}d-static.a lib${LIB}_debug-static.a lib${LIB}d-static lib${LIB}_D-static lib${LIB}_debug-static )
873 set( THIS_LIBRARY_SEARCH_RELEASE lib${LIB}.a lib${LIB} lib${LIB}-static.a lib${LIB}-static)
875 set( THIS_LIBRARY_SEARCH_DEBUG ${LIB}d ${LIB}_D ${LIB}_debug ${LIB}d-shared ${LIB}_D-shared ${LIB}_debug-shared)
876 set( THIS_LIBRARY_SEARCH_RELEASE ${LIB} ${LIB}-shared)
878 find_library(HDF5_${LIB}_LIBRARY_DEBUG
879 NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
880 HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
883 find_library(HDF5_${LIB}_LIBRARY_RELEASE
884 NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
885 HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
889 select_library_configurations( HDF5_${LIB} )
890 list(APPEND HDF5_${_lang}_HL_LIBRARIES ${HDF5_${LIB}_LIBRARY})
893 # Append the libraries for this language binding to the list of all
894 # required libraries.
895 list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARIES})
898 if(HDF5_FIND_HL AND HDF5_HL_LIBRARIES)
899 set(HDF5_HL_FOUND TRUE)
902 _HDF5_remove_duplicates_from_beginning(HDF5_DEFINITIONS)
903 _HDF5_remove_duplicates_from_beginning(HDF5_INCLUDE_DIRS)
904 _HDF5_remove_duplicates_from_beginning(HDF5_LIBRARIES)
905 _HDF5_remove_duplicates_from_beginning(HDF5_HL_LIBRARIES)
907 # If the HDF5 include directory was found, open H5pubconf.h to determine if
908 # HDF5 was compiled with parallel IO support
909 set( HDF5_IS_PARALLEL FALSE )
910 set( HDF5_VERSION "" )
911 foreach( _dir IN LISTS HDF5_INCLUDE_DIRS )
912 foreach(_hdr "${_dir}/H5pubconf.h" "${_dir}/H5pubconf-64.h" "${_dir}/H5pubconf-32.h")
913 if( EXISTS "${_hdr}" )
914 file( STRINGS "${_hdr}"
915 HDF5_HAVE_PARALLEL_DEFINE
916 REGEX "HAVE_PARALLEL 1" )
917 if( HDF5_HAVE_PARALLEL_DEFINE )
918 set( HDF5_IS_PARALLEL TRUE )
920 unset(HDF5_HAVE_PARALLEL_DEFINE)
922 file( STRINGS "${_hdr}"
924 REGEX "^[ \t]*#[ \t]*define[ \t]+H5_VERSION[ \t]+" )
925 if( "${HDF5_VERSION_DEFINE}" MATCHES
926 "H5_VERSION[ \t]+\"([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?\"" )
927 set( HDF5_VERSION "${CMAKE_MATCH_1}" )
929 set( HDF5_VERSION ${HDF5_VERSION}.${CMAKE_MATCH_3})
932 unset(HDF5_VERSION_DEFINE)
938 set( HDF5_IS_PARALLEL ${HDF5_IS_PARALLEL} CACHE BOOL
939 "HDF5 library compiled with parallel IO support" )
940 mark_as_advanced( HDF5_IS_PARALLEL )
942 set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
944 list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
948 # For backwards compatibility we set HDF5_INCLUDE_DIR to the value of
950 if( HDF5_INCLUDE_DIRS )
951 set( HDF5_INCLUDE_DIR "${HDF5_INCLUDE_DIRS}" )
954 # If HDF5_REQUIRED_VARS is empty at this point, then it's likely that
955 # something external is trying to explicitly pass already found
957 if(NOT HDF5_REQUIRED_VARS)
958 set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
961 find_package_handle_standard_args(HDF5
962 REQUIRED_VARS ${HDF5_REQUIRED_VARS}
963 VERSION_VAR HDF5_VERSION
967 unset(_HDF5_SEARCH_OPTS)
969 if( HDF5_FOUND AND NOT HDF5_DIR)
970 # hide HDF5_DIR for the non-advanced user to avoid confusion with
971 # HDF5_DIR-NOT_FOUND while HDF5 was found.
972 mark_as_advanced(HDF5_DIR)
976 message(STATUS "HDF5_DIR: ${HDF5_DIR}")
977 message(STATUS "HDF5_DEFINITIONS: ${HDF5_DEFINITIONS}")
978 message(STATUS "HDF5_INCLUDE_DIRS: ${HDF5_INCLUDE_DIRS}")
979 message(STATUS "HDF5_LIBRARIES: ${HDF5_LIBRARIES}")
980 message(STATUS "HDF5_HL_LIBRARIES: ${HDF5_HL_LIBRARIES}")
981 foreach(_lang IN LISTS HDF5_LANGUAGE_BINDINGS)
982 message(STATUS "HDF5_${_lang}_DEFINITIONS: ${HDF5_${_lang}_DEFINITIONS}")
983 message(STATUS "HDF5_${_lang}_INCLUDE_DIR: ${HDF5_${_lang}_INCLUDE_DIR}")
984 message(STATUS "HDF5_${_lang}_INCLUDE_DIRS: ${HDF5_${_lang}_INCLUDE_DIRS}")
985 message(STATUS "HDF5_${_lang}_LIBRARY: ${HDF5_${_lang}_LIBRARY}")
986 message(STATUS "HDF5_${_lang}_LIBRARIES: ${HDF5_${_lang}_LIBRARIES}")
987 message(STATUS "HDF5_${_lang}_HL_LIBRARY: ${HDF5_${_lang}_HL_LIBRARY}")
988 message(STATUS "HDF5_${_lang}_HL_LIBRARIES: ${HDF5_${_lang}_HL_LIBRARIES}")
992 unset(_HDF5_NEED_TO_SEARCH)