Debian Codegraph @

cmake/tribits/common_tpls/find_modules/FindHDF5.cmake

raw
1 # - FindHDF5.cmake
2 #
3 # The FindHDF5 module with the CMake distribution will not work if
4 # the HDF5 compilers are not installed or if more the one hdf5 is on the
5 # system. The search logic also depends on an environment variable
6 # HDF5_ROOT. This module removes both requirements and instead relies on the
7 # libhdf5.settings file found in the library installation directory
8 #
9 # This module will ONLY work for HDF5 configured through the GNU
10 # autoconf script configure. When built through CMake, the *config.cmake
11 # files should be used.
12 #
13 #
14 # Based on version from MSTK which has the following:
15 # ACKNOWLEDGEMENT:
16 # This file has been adapted from the build system of the Amanzi project
17 # under the DOE/ASCEM code project. It is used here based on the BSD open
18 # source licensing of the Amanzi code base. Many thanks to the Amanzi build
19 # system developers
20 #
21 # @HEADER
22 # *****************************************************************************
23 # TriBITS: Tribal Build, Integrate, and Test System
24 #
25 # Copyright 2013-2016 NTESS and the TriBITS contributors.
26 # SPDX-License-Identifier: BSD-3-Clause
27 # *****************************************************************************
28 # @HEADER
29
30 # CMake includes
31 include(FindPackageHandleStandardArgs)
32
33 include(AddImportedLibrary)
34
35 # ---------------------------------------------------------------------------- #
36 # Functions/Macros
37 #
38 #
39 macro(_hdf5_boolean_convert _var)
40 string(TOUPPER ${${_var}} _var_UC)
41 if(_var_UC)
42 set(${_var} TRUE)
43 else()
44 set(${_var} FALSE)
45 endif()
46 endmacro()
47
48
49
50
51 function(_hdf5_chomp_string old_str new_str_var)
52
53 string(REGEX REPLACE "[\t\r\n]" " " _tmp "${old_str}")
54 #string(REGEX REPLACE " " "S" _tmp "${_tmp}")
55 string(REGEX REPLACE "^ +" "" _tmp "${_tmp}")
56 string(REGEX REPLACE " +$" "" _tmp "${_tmp}")
57
58 set(${new_str_var} ${_tmp} PARENT_SCOPE)
59
60 endfunction()
61
62
63
64 function(_hdf5_parse_settings_file _file _key _value)
65
66 set(_tmp ${_value}-NOTFOUND)
67 file(STRINGS ${_file} _output
68 REGEX "^[ \t]*${_key}:|^${_key}")
69
70 if(_output)
71 # _HDF5_CHOMP_STRING will remove all tabs, newlines and returns
72 # It also removes leading and trailing whitespace
73 _hdf5_chomp_string(${_output} _output)
74 # Remove the key signature
75 string(REGEX REPLACE "${_key}:" "" _output "${_output}")
76 # CHOMP again to remove leading and trailing whitespace
77 if (_output)
78 _hdf5_chomp_string(${_output} _output)
79 endif()
80 # Entry is non-empty if ANY non-space character is left
81 if ( "${_output}" MATCHES "[^ ]" )
82 set(_tmp ${_output})
83 endif()
84 endif()
85
86 set(${_value} ${_tmp} PARENT_SCOPE)
87
88 endfunction()
89
90
91
92 function(_hdf5_define_version _file _var)
93
94 set(_search_key "HDF5 Version")
95 _hdf5_parse_settings_file(${_file} ${_search_key} _tmp)
96
97 set(${_var} ${_tmp} PARENT_SCOPE)
98
99 endfunction()
100
101
102
103 function(_hdf5_define_parallel_build _file _var)
104
105 set(_search_key "Parallel HDF5")
106 _hdf5_parse_settings_file(${_file} ${_search_key} _tmp)
107 _hdf5_boolean_convert(_tmp)
108
109 set(${_var} ${_tmp} PARENT_SCOPE)
110
111 endfunction()
112
113
114
115 function(_hdf5_extra_library_dirs _file _var)
116
117 # Settings file has several locations to list LDFLAGS
118 # We'll pick them all and sort out later.
119 set(_search_ldflags_keys "AM_LDFLAGS;H5_LDFLAGS;LDFLAGS;Extra libraries")
120 set(_ldflags "")
121 foreach ( _key ${_search_ldflags_keys})
122 _hdf5_parse_settings_file(${_file} ${_key} _tmp)
123 if ( _tmp )
124 set(_ldflags "${_ldflags} ${_tmp}")
125 endif()
126 endforeach()
127
128 # Now match all the -L flags
129 string(REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" _lib_path_flags ${_ldflags})
130
131 # Loop through each
132 set(_directories)
133 foreach(_dir ${_lib_path_flags})
134 string(REGEX REPLACE "^-L" "" _dir ${_dir})
135 string(REGEX REPLACE "//" "/" _dir ${_dir})
136 list(APPEND _directories ${_dir})
137 endforeach()
138
139 if(_directories)
140 list(REMOVE_DUPLICATES _directories)
141 endif()
142 set(${_var} ${_directories} PARENT_SCOPE)
143
144 endfunction()
145
146 function(_hdf5_library_path _file _var)
147
148 # Settings file has several locations to list LDFLAGS
149 # We'll pick them all and sort out later.
150 set(_search_key "Installation point")
151 _hdf5_parse_settings_file(${_file} ${_search_key} _tmp)
152
153 set(${_var} ${_tmp} PARENT_SCOPE)
154 endfunction()
155
156
157
158 function(_hdf5_extra_libraries _file _var)
159
160 # Find all the extra libraries defined in the file
161 set(_search_key "Extra libraries")
162 set(_libraries)
163 _hdf5_parse_settings_file(${_file} ${_search_key} _library_flags)
164 string( REGEX MATCHALL "[, ]-l([^\", ]+)|^-l([^\", ]+)" _library_name_flags ${_library_flags})
165 foreach ( _lib ${_library_name_flags} )
166 _hdf5_chomp_string(${_lib} _lib_chomp)
167 string( REGEX REPLACE "^[,]-l|^-l" "" _lib_chomp ${_lib_chomp})
168 list(APPEND _libraries ${_lib_chomp})
169 endforeach()
170
171 # Grab all the extra library paths to build a search list
172 _hdf5_extra_library_dirs(${_file} _search_list)
173
174 # Loop through each library
175 # (1) find_library with the search list for hints
176 # (2) Add library name if find succeeds, otherwise
177 # add the name to the list.
178 set(_return_list)
179 foreach( _lib ${_libraries})
180
181 # Search with hints
182 set(_lib_name _lib_name-NOTFOUND)
183 find_library(_lib_name
184 NAMES ${_lib}
185 HINTS ${_search_list}
186 )
187 # Search without hints if the first search fails
188 if ( NOT _lib_name )
189 find_library(_lib_name NAMES ${_lib})
190 endif()
191
192 # Add the full library name if either find succeeded
193 # otherwise add the library name.
194 if(_lib_name)
195 list(APPEND _return_list ${_lib_name})
196 else()
197 list(APPEND _return_list ${_lib})
198 endif()
199
200 endforeach()
201
202 set(${_var} ${_return_list} PARENT_SCOPE)
203
204 endfunction()
205
206
207
208 function(_hdf5_extra_include_dirs _file _var)
209
210 # Settings file has several locations to list LDFLAGS
211 # We'll pick them all and sort out later.
212
213 # Keys in the file changed around version 1.8.3
214 set(_search_cflags_keys "CFLAGS;H5_CFLAGS;AM_CFLAGS;CPPFLAGS;H5_CPPFLAGS;AM_CPPFLAGS")
215
216 if ( HDF5_VERSION_STRING )
217 if ( "${HDF5_VERSION_STRING}" VERSION_LESS "1.8.3")
218 set(_search_cflags_keys "CFLAGS/H5_CFLAGS;AM_CFLAGS;CPPFLAGS/H5_CPPFLAGS;AM_CPPFLAGS")
219 endif()
220 endif()
221
222 set(_cflags "")
223 foreach ( _key ${_search_cflags_keys})
224 _hdf5_parse_settings_file(${_file} ${_key} _tmp)
225 if ( _tmp )
226 set(_cflags "${_cflags} ${_tmp}")
227 endif()
228 endforeach()
229
230 # Now match all the -I flags
231 if (${_cflags})
232 string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _inc_path_flags ${_cflags})
233
234 # Loop through each
235 set(_directories)
236 foreach(_dir ${_inc_path_flags})
237 string(REGEX REPLACE "^-I" "" _dir ${_dir})
238 string(REGEX REPLACE "//" "/" _dir ${_dir})
239 list(APPEND _directories ${_dir})
240 endforeach()
241 endif()
242
243 if(_directories)
244 list(REMOVE_DUPLICATES _directories)
245 endif()
246 set(${_var} ${_directories} PARENT_SCOPE)
247
248 endfunction()
249
250 #
251 # End Functions/Macros
252 # ---------------------------------------------------------------------------- #
253
254 # ------------------------------------ #
255 # Initialize search paths and criteria #
256 # ------------------------------------ #
257
258 # If HDF5_ROOT was defined in the environment, use it.
259 # Definition from the command line will take precedence.
260 if (NOT HDF5_ROOT AND NOT $ENV{HDF5_ROOT} STREQUAL "")
261 set(HDF5_ROOT $ENV{HDF5_ROOT})
262 endif()
263
264 # HDF5_DIR is DEPRECATED WARN THE USER if it is set
265 if (NOT HDF5_ROOT AND HDF5_DIR )
266 message(WARNING "The configuration parameter HDF5_DIR is deprecated."
267 " Please use HDF5_ROOT instead to define the HDF5 installation")
268 set(HDF5_ROOT ${HDF5_DIR})
269 endif()
270
271 # Add the usual paths for searching using the HDF5_ROOT variable
272 if (HDF5_ROOT)
273 list(APPEND _hdf5_INCLUDE_SEARCH_DIRS
274 ${HDF5_ROOT}/include
275 ${HDF5_ROOT}/include/hdf5/serial
276 ${HDF5_ROOT}
277 )
278
279 list(APPEND _hdf5_LIBRARY_SEARCH_DIRS
280 ${HDF5_ROOT}/lib
281 ${HDF5_ROOT}
282 )
283
284 list(APPEND _hdf5_BINARY_SEARCH_DIRS
285 ${HDF5_ROOT}/bin
286 ${HDF5_ROOT}
287 )
288 endif()
289
290 # Restrict the search to HDF5_ROOT if user does not want other
291 # directories searched.
292 if ( HDF5_NO_SYSTEM_PATHS )
293 set(_hdf5_FIND_OPTIONS NO_CMAKE_SYSTEM_PATH)
294 endif()
295
296 # A list of valid components
297 set(HDF5_VALID_COMPONENTS HL C)
298
299 # A list of requested components, invalid components are ignored.
300 if ( NOT HDF5_FIND_COMPONENTS )
301 set(HDF5_SEARCH_COMPONENTS "C")
302 else()
303 foreach ( component ${HDF5_FIND_COMPONENTS} )
304 list(FIND HDF5_VALID_COMPONENTS ${component} component_idx)
305 if ( ${component_idx} EQUAL -1 )
306 message(SEND_ERROR "${component} is not a valid HDF5 component")
307 else()
308 list(APPEND HDF5_SEARCH_COMPONENTS ${component})
309 endif()
310 endforeach()
311 endif()
312
313
314 # ------------------------------------ #
315 # Perform CMake Search #
316 # ------------------------------------ #
317
318
319
320 if ( HDF5_INCLUDE_DIRS AND HDF5_LIBRARIES )
321 # Do nothing if the user has defined these on the command line
322 else()
323
324 # --- Target names used in both the CMake configure files
325 # and here to create targets
326 set( HDF5_C_TARGET hdf5 )
327 set( HDF5_HL_TARGET hdf5_hl )
328
329 # ------------------------------------------------------ #
330 # Search Logic
331 # (1) Look for a CMake configuration file(s)
332 # (2) If the above fails, search by name the include and
333 # library files.
334 # Step one will be bypassed if HDF5_NO_HDF5_CMAKE is set
335
336 # --- Search for a CMake Configuration
337 if ( NOT HDF5_NO_HDF5_CMAKE )
338
339 # Call find package only looking for CMake config files
340 find_package(HDF5
341 HINTS ${_hdf5_INCLUDE_SEARCH_DIRS} ${_hdf5_LIBRARY_SEARCH_DIRS}
342 QUIET
343 NO_MODULE)
344
345 # If located a HDF5 configuration file
346 if (HDF5_FOUND)
347
348 message(STATUS "Found CMake configuration file HDF5 ( directory ${HDF5_ROOT} )")
349
350 # Want consistency between this module and the CMake file
351 set(HDF5_VERSION ${HDF5_VERSION_STRING})
352 set(HDF5_IS_PARALLEL ${HDF5_ENABLE_PARALLEL})
353 set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR})
354
355 # Loop through each possible target and
356 # build the HDF5_LIBRARIES.
357 # Target names set by the HDF5 configuration file
358 set(HDF5_LIBRARIES)
359
360 foreach( _component ${HDF5_VALID_COMPONENTS} )
361 set(target ${HDF5_${_component}_TARGET})
362 if ( TARGET ${target} )
363 set(HDF5_${_component}_LIBRARY ${target})
364 list(APPEND HDF5_LIBRARIES ${HDF5_${_component}_LIBRARY})
365 endif()
366 endforeach()
367
368 # Define HDF5_C_LIBRARIES to contain hdf5 and hdf5_hl C libraries
369 set(HDF5_C_LIBRARIES ${HDF5_HL_LIBRARY} ${HDF5_CLIBRARY})
370
371
372 endif(HDF5_FOUND)
373
374 endif(NOT HDF5_NO_HDF5_CMAKE)
375
376 # --- If HDF5 is NOT found search for the settings file installed with the libraries
377 # Will allow the user to define the HDF5_SETTINGS_FILE before attempting a search.
378 if ( NOT HDF5_FOUND AND ( NOT HDF5_SETTINGS_FILE ) )
379 find_file(HDF5_SETTINGS_FILE
380 NAMES libhdf5.settings
381 HINTS ${_hdf5_LIBRARY_SEARCH_DIRS}
382 ${_hdf5_FIND_OPTIONS})
383 endif()
384
385
386 # --- Now search by file name. HDF5_INCLUDE_DIRS and HD5_LIBRARIES will
387 # not be set if the CMake configuration search was successful.
388
389 # --- Search for the include files
390 if ( NOT HDF5_INCLUDE_DIRS )
391 find_path(HDF5_INCLUDE_DIR
392 NAMES hdf5.h
393 PATHS ${_hdf5_INCLUDE_SEARCH_DIRS}
394 ${_hdf5_FIND_OPTIONS}
395 )
396
397 if ( NOT HDF5_INCLUDE_DIR )
398 message(FATAL_ERROR "Failed to locate HDF5 include file")
399 endif()
400
401 # Check the settings file for other include directories
402 if ( HDF5_SETTINGS_FILE )
403 _hdf5_extra_include_dirs(${HDF5_SETTINGS_FILE} extra_inc_dirs)
404 endif()
405
406 # Build HDF5_INCLUDE_DIRS
407 set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR} ${extra_inc_dirs})
408 list(REMOVE_DUPLICATES HDF5_INCLUDE_DIRS)
409
410 endif(NOT HDF5_INCLUDE_DIRS)
411
412 # Search for the libraries
413
414 if (HDF5_SETTINGS_FILE)
415 _hdf5_library_path(${HDF5_SETTINGS_FILE} _hdf5_path)
416 set(_hdf5_LIBRARY_SEARCH_DIRS ${_hdf5_path}/lib)
417 endif()
418
419 if ( NOT HDF5_LIBRARIES )
420
421 # --- Search for the C library
422 find_library(_HDF5_C_LIBRARY
423 NAMES hdf5 hdf5_serial
424 HINTS ${_hdf5_LIBRARY_SEARCH_DIRS}
425 ${_hdf5_FIND_OPTIONS}
426 )
427
428 # Since all other libraries need this core library, throw a
429 # fatal error her if it is not found.
430 if ( NOT _HDF5_C_LIBRARY )
431 message(FATAL_ERROR "Could not locate the C HDF5 library")
432 endif()
433
434 # Define the target for the C library
435 if (HDF5_SETTINGS_FILE)
436 _hdf5_extra_libraries(${HDF5_SETTINGS_FILE} HDF5_LINK_LIBRARIES)
437 endif()
438
439 add_imported_library(${HDF5_C_TARGET}
440 LOCATION ${_HDF5_C_LIBRARY}
441 LINK_LANGUAGES "C"
442 LINK_INTERFACE_LIBRARIES "${HDF5_LINK_LIBRARIES}")
443 set(HDF5_C_LIBRARY ${HDF5_C_TARGET})
444
445 # --- Search for the other possible component libraries
446
447 # Search for the high-level (HL) library
448 find_library(_HDF5_HL_LIBRARY
449 NAMES hdf5_hl
450 HINTS ${_hdf5_LIBRARY_SEARCH_DIRS}
451 ${_hdf5_FIND_OPTIONS})
452
453 # --- Create the imported targets for each of the components found
454 # and update HDF5_LIBRARIES.
455
456 # HL Library
457 if ( _HDF5_HL_LIBRARY )
458 add_imported_library(${HDF5_HL_TARGET}
459 LOCATION ${_HDF5_HL_LIBRARY}
460 LINK_LANGUAGES "C"
461 LINK_INTERFACE_LIBRARIES "${HDF5_C_TARGET}")
462 set(HDF5_HL_LIBRARY ${HDF5_HL_TARGET})
463 endif()
464
465 # Define the HDF5_<component>_LIBRARY to point to the target
466 foreach ( _component ${HDF5_VALID_COMPONENTS} )
467 if ( TARGET ${HDF5_${_component}_TARGET} )
468 set(HDF5_${_component}_LIBRARY ${HDF5_${_component}_TARGET})
469 endif()
470 endforeach()
471
472 # Define the HDF5_LIBRARIES variable
473 set(HDF5_LIBRARY_TARGETS
474 ${HDF5_HL_LIBRARY}
475 ${HDF5_C_LIBRARY})
476
477 # Define the HDF5_C_LIBRARIES variable
478 set(HDF5_C_LIBRARIES ${HDF5_HL_LIBRARY} ${HDF5_C_LIBRARY})
479
480 # HDF5 and extra libraries listed as full paths rather than
481 # libraries for the purposes of exporting
482
483 set(HDF5_LIBRARIES)
484 foreach (_component ${HDF5_VALID_COMPONENTS})
485 if ( TARGET ${HDF5_${_component}_TARGET} )
486 list(APPEND HDF5_LIBRARIES ${_HDF5_${_component}_LIBRARY})
487 endif()
488 endforeach()
489 list(APPEND HDF5_LIBRARIES ${HDF5_LINK_LIBRARIES})
490
491 endif(NOT HDF5_LIBRARIES)
492
493 endif()
494
495 set(HDF5_LIBRARIES_EXPORT ${HDF5_LIBRARIES})
496
497
498 # --- Define the version string from the settings file if not already set
499 if ( NOT HDF5_VERSION AND HDF5_SETTINGS_FILE )
500 _hdf5_define_version(${HDF5_SETTINGS_FILE} HDF5_VERSION)
501 endif()
502
503 if ( NOT HDF5_VERSION )
504 set(HDF5_VERSION "Unknown")
505 endif()
506
507 # --- Define HDF5_IS_PARALLEL from the settings file if not already set
508 if ( NOT HDF5_IS_PARALLEL AND HDF5_SETTINGS_FILE )
509 _hdf5_define_parallel_build(${HDF5_SETTINGS_FILE} HDF5_IS_PARALLEL)
510 endif()
511
512 # --- Search for HDF5 tools
513 set(_hdf5_TOOLS h52gif h5copy h5debug h5diff h5dump h5import h5jam h5ls h5mkgrp h5stat)
514 set(HDF5_TOOLS_FOUND)
515 foreach( tool ${_hdf5_TOOLS})
516 string(TOUPPER "${tool}" tool_uc)
517 set(_hdf5_VAR_NAME HDF5_${tool_uc}_BINARY)
518 find_program(${_hdf5_VAR_NAME}
519 ${tool}
520 HINTS ${_hdf5_BINARY_SEARCH_DIRS}
521 ${_hdf5_FIND_OPTIONS})
522 if (${_hdf5_VAR_NAME})
523 list(APPEND HDF5_TOOLS_FOUND ${tool})
524 endif()
525 endforeach()
526
527
528
529 # --- Set the variables HDF5_<COMPONENT>_FOUND FLAGS
530 foreach ( _component ${HDF5_VALID_COMPONENTS} )
531 if( HDF5_${_component}_LIBRARY )
532 set(HDF5_${_component}_FOUND TRUE)
533 else()
534 set(HDF5_${_component}_FOUND FALSE)
535 endif()
536 endforeach()
537
538 # --- Provide a summary of what the module found
539 if ( NOT HDF5_FIND_QUIETLY )
540
541 # Create a not found list
542
543 message(STATUS "HDF5 Version: ${HDF5_VERSION}")
544 message(STATUS "\tHDF5_INCLUDE_DIRS =${HDF5_INCLUDE_DIRS}")
545 message(STATUS "\tHDF5_LIBRARY_TARGETS =${HDF5_LIBRARY_TARGETS}")
546 message(STATUS "\tHDF5_LIBRARIES =${HDF5_LIBRARIES}")
547 message(STATUS "\tHDF5_LIBRARIES_EXPORT =${HDF5_LIBRARIES_EXPORT}")
548 message(STATUS "\tHDF5_LINK_LIBRARIES =${HDF5_LINK_LIBRARIES}")
549 message(STATUS "\tHDF5_IS_PARALLEL =${HDF5_IS_PARALLEL}")
550 message(STATUS "Found the following HDF5 component libraries")
551 set(HDF5_COMPONENTS_NOTFOUND)
552 foreach (_component ${HDF5_VALID_COMPONENTS} )
553 if ( HDF5_${_component}_FOUND )
554 #message(STATUS "\t HDF5_${_component}_LIBRARY\t\t=${HDF5_${_component}_LIBRARY}")
555 message(STATUS "\t${HDF5_${_component}_LIBRARY}")
556 else()
557 list(APPEND HDF5_COMPONENTS_NOTFOUND ${_component})
558 endif()
559 endforeach()
560 if ( HDF5_COMPONENTS_NOTFOUND )
561 message(STATUS "\tHDF5 Components not found: ${HDF5_COMPONENTS_NOTFOUND}")
562 endif()
563 message(STATUS "\tHDF5_TOOLS_FOUND: ${HDF5_TOOLS_FOUND}")
564
565 endif()
566 # For compatibility with TriBITS:
567 set(TPL_HDF5_LIBRARY_DIRS ${_hdf5_LIBRARY_SEARCH_DIRS})
568 set(TPL_HDF5_LIBRARIES ${HDF5_LIBRARIES})
569 set(TPL_HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIRS})
570
571 find_package_handle_standard_args( HDF5 DEFAULT_MESSAGE
572 HDF5_INCLUDE_DIRS
573 HDF5_LIBRARIES
574 HDF5_VERSION)