60a313d737c93a2e28b17118b7f03709a6cceb67
[platform/upstream/cmake.git] / Modules / FindHDF5.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindHDF5
6 --------
7
8 Find Hierarchical Data Format (HDF5), a library for reading and writing
9 self describing array data.
10
11
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.
17
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)``.
28
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.
34
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
39 ``TRUE``.
40
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
44 regression testing.
45
46 Result Variables
47 ^^^^^^^^^^^^^^^^
48
49 This module will set the following variables in your project:
50
51 ``HDF5_FOUND``
52   HDF5 was found on the system
53 ``HDF5_VERSION``
54   HDF5 library version
55 ``HDF5_INCLUDE_DIRS``
56   Location of the HDF5 header files
57 ``HDF5_DEFINITIONS``
58   Required compiler definitions for HDF5
59 ``HDF5_LIBRARIES``
60   Required libraries for all requested bindings
61 ``HDF5_HL_LIBRARIES``
62   Required libraries for the HDF5 high level API for all bindings,
63   if the ``HL`` component is enabled
64
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:
70
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
83 ``HDF5_C_LIBRARIES``
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.
95
96 ``HDF5_IS_PARALLEL``
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
112
113 Hints
114 ^^^^^
115
116 The following variables can be set to guide the search for HDF5 libraries and includes:
117
118 ``HDF5_PREFER_PARALLEL``
119   set ``true`` to prefer parallel HDF5 (by default, serial is preferred)
120
121 ``HDF5_FIND_DEBUG``
122   Set ``true`` to get extra debugging output.
123
124 ``HDF5_NO_FIND_PACKAGE_CONFIG_FILE``
125   Set ``true`` to skip trying to find ``hdf5-config.cmake``.
126 #]=======================================================================]
127
128 # This module is maintained by Will Dicharry <wdicharry@stellarscience.com>.
129
130 include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
131 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
132
133 # List of the valid HDF5 components
134 set(HDF5_VALID_LANGUAGE_BINDINGS C CXX Fortran)
135
136 # Validate the list of find components.
137 if(NOT HDF5_FIND_COMPONENTS)
138   set(HDF5_LANGUAGE_BINDINGS "C")
139 else()
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")
148       set(HDF5_FIND_HL ON)
149     elseif(_component STREQUAL "Fortran_HL") # only for compatibility
150       list(APPEND HDF5_LANGUAGE_BINDINGS Fortran)
151       set(HDF5_FIND_HL ON)
152       set(HDF5_FIND_REQUIRED_Fortran_HL FALSE)
153       set(HDF5_FIND_REQUIRED_Fortran TRUE)
154       set(HDF5_FIND_REQUIRED_HL TRUE)
155     else()
156       message(FATAL_ERROR "${_component} is not a valid HDF5 component.")
157     endif()
158   endforeach()
159   unset(_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})
166       endif()
167     endforeach()
168   endif()
169   list(REMOVE_ITEM HDF5_FIND_COMPONENTS Fortran_HL) # replaced by Fortran and HL
170   list(REMOVE_DUPLICATES HDF5_LANGUAGE_BINDINGS)
171 endif()
172
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)
178 else()
179   set(HDF5_C_COMPILER_NAMES h5cc h5pcc)
180   set(HDF5_CXX_COMPILER_NAMES h5c++ h5pc++)
181   set(HDF5_Fortran_COMPILER_NAMES h5fc h5pfc)
182 endif()
183
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
191 # the beginning.
192 macro(_HDF5_remove_duplicates_from_beginning _list_name)
193   if(${_list_name})
194     list(REVERSE ${_list_name})
195     list(REMOVE_DUPLICATES ${_list_name})
196     list(REVERSE ${_list_name})
197   endif()
198 endmacro()
199
200
201 # Test first if the current compilers automatically wrap HDF5
202
203 function(_HDF5_test_regular_compiler_C success version is_parallel)
204   set(scratch_directory
205     ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
206   if(NOT ${success} OR
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"
215       "#endif\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"
221       "#endif\n"
222       "  hid_t fid;\n"
223       "  fid = H5Fcreate(\"foo.h5\",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);\n"
224       "  return 0;\n"
225       "}")
226     try_compile(${success} ${scratch_directory} ${test_file}
227       COPY_FILE ${scratch_directory}/compiler_has_h5_c
228     )
229   endif()
230   if(${success})
231     file(STRINGS ${scratch_directory}/compiler_has_h5_c INFO_STRINGS
232       REGEX "^INFO:"
233     )
234     string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
235       INFO_VER "${INFO_STRINGS}"
236     )
237     set(${version} ${CMAKE_MATCH_1})
238     if(CMAKE_MATCH_3)
239       set(${version} ${HDF5_C_VERSION}.${CMAKE_MATCH_3})
240     endif()
241     set(${version} ${${version}} PARENT_SCOPE)
242
243     if(INFO_STRINGS MATCHES "INFO:PARALLEL")
244       set(${is_parallel} TRUE PARENT_SCOPE)
245     else()
246       set(${is_parallel} FALSE PARENT_SCOPE)
247     endif()
248   endif()
249 endfunction()
250
251 function(_HDF5_test_regular_compiler_CXX success version is_parallel)
252   set(scratch_directory ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hdf5)
253   if(NOT ${success} OR
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"
260       "#endif\n"
261       "const char* info_ver = \"INFO\" \":\" H5_VERSION;\n"
262       "#ifdef H5_HAVE_PARALLEL\n"
263       "const char* info_parallel = \"INFO\" \":\" \"PARALLEL\";\n"
264       "#endif\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"
270       "#endif\n"
271       "  H5File file(\"foo.h5\", H5F_ACC_TRUNC);\n"
272       "  return 0;\n"
273       "}")
274     try_compile(${success} ${scratch_directory} ${test_file}
275       COPY_FILE ${scratch_directory}/compiler_has_h5_cxx
276     )
277   endif()
278   if(${success})
279     file(STRINGS ${scratch_directory}/compiler_has_h5_cxx INFO_STRINGS
280       REGEX "^INFO:"
281     )
282     string(REGEX MATCH "^INFO:([0-9]+\\.[0-9]+\\.[0-9]+)(-patch([0-9]+))?"
283       INFO_VER "${INFO_STRINGS}"
284     )
285     set(${version} ${CMAKE_MATCH_1})
286     if(CMAKE_MATCH_3)
287       set(${version} ${HDF5_CXX_VERSION}.${CMAKE_MATCH_3})
288     endif()
289     set(${version} ${${version}} PARENT_SCOPE)
290
291     if(INFO_STRINGS MATCHES "INFO:PARALLEL")
292       set(${is_parallel} TRUE PARENT_SCOPE)
293     else()
294       set(${is_parallel} FALSE PARENT_SCOPE)
295     endif()
296   endif()
297 endfunction()
298
299 function(_HDF5_test_regular_compiler_Fortran success is_parallel)
300   if(NOT ${success})
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"
306       "  use hdf5\n"
307       "  use h5lt\n"
308       "  use h5ds\n"
309       "  integer error\n"
310       "  call h5open_f(error)\n"
311       "  call h5close_f(error)\n"
312       "end\n")
313     try_compile(${success} ${scratch_directory} ${test_file})
314     if(${success})
315       execute_process(COMMAND ${CMAKE_Fortran_COMPILER} -showconfig
316         OUTPUT_VARIABLE config_output
317         ERROR_VARIABLE config_error
318         RESULT_VARIABLE config_result
319         )
320       if(config_output MATCHES "Parallel HDF5: yes")
321         set(${is_parallel} TRUE PARENT_SCOPE)
322       else()
323         set(${is_parallel} FALSE PARENT_SCOPE)
324       endif()
325     endif()
326   endif()
327 endfunction()
328
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)
335   else()
336     set(lib_type_args -shlib)
337   endif()
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)
345   endif()
346   # Verify that the compiler wrapper can actually compile: sometimes the compiler
347   # wrapper exists, but not the compiler.  E.g. Miniconda / Anaconda Python
348   execute_process(
349     COMMAND ${HDF5_${language}_COMPILER_EXECUTABLE} ${test_file}
350     RESULT_VARIABLE return_value
351     )
352   if(return_value)
353     message(STATUS
354       "HDF5 ${language} compiler wrapper is unable to compile a minimal HDF5 program.")
355   else()
356     execute_process(
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
362       )
363     if(return_value)
364       message(STATUS
365         "Unable to determine HDF5 ${language} flags from HDF5 wrapper.")
366     endif()
367     execute_process(
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
373       )
374     if(return_value)
375       message(STATUS
376         "Unable to determine HDF5 ${language} version_var from HDF5 wrapper.")
377     endif()
378     string(REGEX MATCH "HDF5 Version: ([a-zA-Z0-9\\.\\-]*)" version "${config_output}")
379     if(version)
380       string(REPLACE "HDF5 Version: " "" version "${version}")
381       string(REPLACE "-patch" "." version "${version}")
382     endif()
383     if(config_output MATCHES "Parallel HDF5: yes")
384       set(is_parallel TRUE)
385     endif()
386   endif()
387   foreach(var output return_value version is_parallel)
388     set(${${var}_var} ${${var}} PARENT_SCOPE)
389   endforeach()
390 endfunction()
391
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)
395
396   separate_arguments(_compile_args NATIVE_COMMAND "${${compile_line_var}}")
397
398   foreach(_arg IN LISTS _compile_args)
399     if("${_arg}" MATCHES "^-I(.*)$")
400       # include directory
401       list(APPEND include_paths "${CMAKE_MATCH_1}")
402     elseif("${_arg}" MATCHES "^-D(.*)$")
403       # compile definition
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.*)$")
409       # library name (hl)
410       list(APPEND libraries_hl "${CMAKE_MATCH_1}")
411     elseif("${_arg}" MATCHES "^-l(.*)$")
412       # library name
413       list(APPEND libraries "${CMAKE_MATCH_1}")
414     elseif("${_arg}" MATCHES "^(.:)?[/\\].*\\.(a|so|dylib|sl|lib)$")
415       # library file
416       if(NOT EXISTS "${_arg}")
417         continue()
418       endif()
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}")
425       else()
426         list(APPEND libraries "${_lname}")
427       endif()
428     endif()
429   endforeach()
430   foreach(var include_paths definitions library_paths libraries libraries_hl)
431     set(${${var}_var} ${${var}} PARENT_SCOPE)
432   endforeach()
433 endfunction()
434
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)
443         if(HDF5_FIND_DEBUG)
444             message(STATUS "Found imported configurations: ${_imported_conf}")
445         endif()
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})
452         if(HDF5_FIND_DEBUG)
453             message(STATUS "Start search through imported configurations in the following order: ${_preferred_confs}")
454         endif()
455         # Now find the first of these that is present in imported_conf
456         cmake_policy(PUSH)
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})
461                break()
462             endif()
463         endforeach()
464         cmake_policy(POP)
465     endif()
466     if(HDF5_FIND_DEBUG)
467         message(STATUS "Selected imported configuration: ${_imported_conf}")
468     endif()
469     # assign value to function argument
470     set(${imported_conf} ${_imported_conf} PARENT_SCOPE)
471 endfunction()
472
473
474 if(NOT HDF5_ROOT)
475     set(HDF5_ROOT $ENV{HDF5_ROOT})
476 endif()
477 if(HDF5_ROOT)
478     set(_HDF5_SEARCH_OPTS NO_DEFAULT_PATH)
479 else()
480     set(_HDF5_SEARCH_OPTS)
481 endif()
482
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
486       HINTS ${HDF5_ROOT}
487       ${_HDF5_SEARCH_OPTS}
488       )
489     if( HDF5_FOUND)
490         if(HDF5_FIND_DEBUG)
491             message(STATUS "Found HDF5 at ${HDF5_DIR} via NO_MODULE. Now trying to extract locations etc.")
492         endif()
493         set(HDF5_IS_PARALLEL ${HDF5_ENABLE_PARALLEL})
494         set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR})
495         set(HDF5_LIBRARIES)
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::")
499         endif()
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")
509         else()
510             set(_suffix "-shared")
511         endif()
512         foreach(_lang ${HDF5_LANGUAGE_BINDINGS})
513
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)
521                 break()
522               endif()
523               set(_suffix "")
524             endif()
525
526             if(HDF5_FIND_DEBUG)
527                 message(STATUS "Trying to get properties of target ${HDF5_${_lang}_TARGET}${_suffix}")
528             endif()
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)
537                 endif()
538             endif()
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)
544             endif()
545             if(HDF5_FIND_HL)
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)
551                     endif()
552                 endif()
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)
558                 endif()
559                 unset(_hdf5_lang_hl_location)
560             endif()
561             unset(_hdf5_imported_conf)
562             unset(_hdf5_lang_location)
563         endforeach()
564     endif()
565 endif()
566
567 if(NOT HDF5_FOUND)
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)
587     else()
588       continue()
589     endif()
590     if(HDF5_${_lang}_COMPILER_NO_INTERROGATE)
591       if(HDF5_FIND_DEBUG)
592         message(STATUS "HDF5: Using hdf5 compiler wrapper for all ${_lang} compiling")
593       endif()
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)
602
603       mark_as_advanced(HDF5_${_lang}_COMPILER_EXECUTABLE_NO_INTERROGATE)
604
605       set(HDF5_${_lang}_FOUND TRUE)
606       set(HDF5_HL_FOUND TRUE)
607     else()
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
613         HINTS ${HDF5_ROOT}
614         PATH_SUFFIXES bin Bin
615         DOC "HDF5 ${_lang} Wrapper compiler.  Used only to detect HDF5 compile flags."
616         ${_HDF5_SEARCH_OPTS}
617       )
618       mark_as_advanced( HDF5_${_lang}_COMPILER_EXECUTABLE )
619       unset(HDF5_${_lang}_COMPILER_NAMES)
620
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)
625           if(HDF5_FIND_DEBUG)
626             message(STATUS "HDF5: Using hdf5 compiler wrapper to determine ${_lang} configuration")
627           endif()
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
634           )
635           set(HDF5_${_lang}_LIBRARIES)
636
637           foreach(_lib IN LISTS HDF5_${_lang}_LIBRARY_NAMES)
638             set(_HDF5_SEARCH_NAMES_LOCAL)
639             if("x${_lib}" MATCHES "hdf5")
640               # hdf5 library
641               set(_HDF5_SEARCH_OPTS_LOCAL ${_HDF5_SEARCH_OPTS})
642               if(HDF5_USE_STATIC_LIBRARIES)
643                 if(WIN32)
644                   set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib})
645                 else()
646                   set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib}.a)
647                 endif()
648               endif()
649             else()
650               # external library
651               set(_HDF5_SEARCH_OPTS_LOCAL)
652             endif()
653             find_library(HDF5_${_lang}_LIBRARY_${_lib}
654               NAMES ${_HDF5_SEARCH_NAMES_LOCAL} ${_lib} NAMES_PER_DIR
655               HINTS ${HDF5_${_lang}_LIBRARY_DIRS}
656                     ${HDF5_ROOT}
657               ${_HDF5_SEARCH_OPTS_LOCAL}
658               )
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}})
663             else()
664               list(APPEND HDF5_${_lang}_LIBRARIES ${_lib})
665             endif()
666           endforeach()
667           if(HDF5_FIND_HL)
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")
672                 # hdf5 library
673                 set(_HDF5_SEARCH_OPTS_LOCAL ${_HDF5_SEARCH_OPTS})
674                 if(HDF5_USE_STATIC_LIBRARIES)
675                   if(WIN32)
676                     set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib})
677                   else()
678                     set(_HDF5_SEARCH_NAMES_LOCAL lib${_lib}.a)
679                   endif()
680                 endif()
681               else()
682                 # external library
683                 set(_HDF5_SEARCH_OPTS_LOCAL)
684               endif()
685               find_library(HDF5_${_lang}_LIBRARY_${_lib}
686                 NAMES ${_HDF5_SEARCH_NAMES_LOCAL} ${_lib} NAMES_PER_DIR
687                 HINTS ${HDF5_${_lang}_LIBRARY_DIRS}
688                       ${HDF5_ROOT}
689                 ${_HDF5_SEARCH_OPTS_LOCAL}
690                 )
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}})
695               else()
696                 list(APPEND HDF5_${_lang}_HL_LIBRARIES ${_lib})
697               endif()
698             endforeach()
699             set(HDF5_HL_FOUND TRUE)
700           endif()
701
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)
707         else()
708           set(_HDF5_NEED_TO_SEARCH TRUE)
709         endif()
710       else()
711         set(_HDF5_NEED_TO_SEARCH TRUE)
712       endif()
713     endif()
714     if(HDF5_${_lang}_VERSION)
715       if(NOT HDF5_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}")
719       endif()
720     endif()
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.")
728       endif()
729     endif()
730   endforeach()
731   unset(_lib)
732 else()
733   set(_HDF5_NEED_TO_SEARCH TRUE)
734 endif()
735
736 if(NOT HDF5_FOUND AND HDF5_COMPILER_NO_INTERROGATE)
737   # No arguments necessary, all languages can use the compiler wrappers
738   set(HDF5_FOUND TRUE)
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)
745   set(HDF5_LIBRARIES)
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})
753         if(HDF5_FIND_HL)
754           list(APPEND HDF5_HL_LIBRARIES ${HDF5_${_lang}_HL_LIBRARIES})
755         endif()
756       endif()
757     endif()
758   endforeach()
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)
763   set(HDF5_FOUND TRUE)
764   set(HDF5_REQUIRED_VARS HDF5_LIBRARIES)
765   if(HDF5_FIND_HL)
766     list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
767   endif()
768 endif()
769
770 find_program( HDF5_DIFF_EXECUTABLE
771     NAMES h5diff
772     HINTS ${HDF5_ROOT}
773     PATH_SUFFIXES bin Bin
774     ${_HDF5_SEARCH_OPTS}
775     DOC "HDF5 file differencing tool." )
776 mark_as_advanced( HDF5_DIFF_EXECUTABLE )
777
778 if( NOT HDF5_FOUND )
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} )
782
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})
785
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})
788
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)
793     endif()
794     if(HDF5_USE_STATIC_LIBRARIES)
795       set(_inc_suffixes include/static)
796     else()
797       set(_inc_suffixes include/shared)
798     endif()
799
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)
806         else()
807             set(HDF5_INCLUDE_FILENAME hdf5.h)
808         endif()
809
810         find_path(HDF5_${_lang}_INCLUDE_DIR ${HDF5_INCLUDE_FILENAME}
811             HINTS ${HDF5_ROOT}
812             PATHS $ENV{HOME}/.local/include
813             PATH_SUFFIXES include Include ${_inc_suffixes} ${_lib_suffixes}
814             ${_HDF5_SEARCH_OPTS}
815         )
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})
820
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)
833             else()
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)
836                 if(WIN32)
837                   list(APPEND HDF5_DEFINITIONS "-DH5_BUILT_AS_DYNAMIC_LIB")
838                 endif()
839             endif()
840             find_library(HDF5_${LIB}_LIBRARY_DEBUG
841                 NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
842                 HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
843                 ${_HDF5_SEARCH_OPTS}
844             )
845             find_library(HDF5_${LIB}_LIBRARY_RELEASE
846                 NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
847                 HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
848                 ${_HDF5_SEARCH_OPTS}
849             )
850
851             select_library_configurations( HDF5_${LIB} )
852             list(APPEND HDF5_${_lang}_LIBRARIES ${HDF5_${LIB}_LIBRARY})
853         endforeach()
854         if(HDF5_${_lang}_LIBRARIES)
855             set(HDF5_${_lang}_FOUND TRUE)
856         endif()
857
858         # Append the libraries for this language binding to the list of all
859         # required libraries.
860         list(APPEND HDF5_LIBRARIES ${HDF5_${_lang}_LIBRARIES})
861
862         if(HDF5_FIND_HL)
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)
874                 else()
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)
877                 endif()
878                 find_library(HDF5_${LIB}_LIBRARY_DEBUG
879                     NAMES ${THIS_LIBRARY_SEARCH_DEBUG}
880                     HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
881                     ${_HDF5_SEARCH_OPTS}
882                 )
883                 find_library(HDF5_${LIB}_LIBRARY_RELEASE
884                     NAMES ${THIS_LIBRARY_SEARCH_RELEASE}
885                     HINTS ${HDF5_ROOT} PATH_SUFFIXES lib Lib ${_lib_suffixes}
886                     ${_HDF5_SEARCH_OPTS}
887                 )
888
889                 select_library_configurations( HDF5_${LIB} )
890                 list(APPEND HDF5_${_lang}_HL_LIBRARIES ${HDF5_${LIB}_LIBRARY})
891             endforeach()
892
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})
896         endif()
897     endforeach()
898     if(HDF5_FIND_HL AND HDF5_HL_LIBRARIES)
899         set(HDF5_HL_FOUND TRUE)
900     endif()
901
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)
906
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 )
919             endif()
920             unset(HDF5_HAVE_PARALLEL_DEFINE)
921
922             file( STRINGS "${_hdr}"
923                 HDF5_VERSION_DEFINE
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}" )
928                 if( CMAKE_MATCH_3 )
929                   set( HDF5_VERSION ${HDF5_VERSION}.${CMAKE_MATCH_3})
930                 endif()
931             endif()
932             unset(HDF5_VERSION_DEFINE)
933         endif()
934       endforeach()
935     endforeach()
936     unset(_hdr)
937     unset(_dir)
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 )
941
942     set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
943     if(HDF5_FIND_HL)
944         list(APPEND HDF5_REQUIRED_VARS HDF5_HL_LIBRARIES)
945     endif()
946 endif()
947
948 # For backwards compatibility we set HDF5_INCLUDE_DIR to the value of
949 # HDF5_INCLUDE_DIRS
950 if( HDF5_INCLUDE_DIRS )
951   set( HDF5_INCLUDE_DIR "${HDF5_INCLUDE_DIRS}" )
952 endif()
953
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
956 # locations
957 if(NOT HDF5_REQUIRED_VARS)
958     set(HDF5_REQUIRED_VARS HDF5_LIBRARIES HDF5_INCLUDE_DIRS)
959 endif()
960
961 find_package_handle_standard_args(HDF5
962     REQUIRED_VARS ${HDF5_REQUIRED_VARS}
963     VERSION_VAR   HDF5_VERSION
964     HANDLE_COMPONENTS
965 )
966
967 unset(_HDF5_SEARCH_OPTS)
968
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)
973 endif()
974
975 if (HDF5_FIND_DEBUG)
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}")
989   endforeach()
990 endif()
991 unset(_lang)
992 unset(_HDF5_NEED_TO_SEARCH)