Skip to content

Commit 6e4c97f

Browse files
committed
functionalize check_link for cleaner logic
1 parent 3e1a336 commit 6e4c97f

File tree

1 file changed

+76
-65
lines changed

1 file changed

+76
-65
lines changed

cmake/Modules/FindHDF5.cmake

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -309,72 +309,24 @@ set(HDF5_HL_FOUND true PARENT_SCOPE)
309309

310310
endfunction(find_hdf5_c)
311311

312-
# === main program
313-
314-
set(CMAKE_REQUIRED_LIBRARIES)
315312

316-
# we don't use pkg-config names because some distros pkg-config for HDF5 is broken
317-
# however at least the paths are often correct
318-
find_package(PkgConfig)
319-
if(NOT HDF5_FOUND)
320-
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
321-
pkg_search_module(pc_hdf5 hdf5-openmpi hdf5-mpich hdf5)
322-
else()
323-
pkg_search_module(pc_hdf5 hdf5-serial hdf5)
324-
endif()
325-
endif()
313+
function(check_hdf5_link)
326314

327-
set(_lsuf hdf5)
328-
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
329-
list(PREPEND _lsuf openmpi/lib mpich/lib hdf5/openmpi hdf5/mpich)
330-
else()
331-
list(PREPEND _lsuf hdf5/serial)
332-
endif()
333-
334-
set(_psuf static ${_lsuf})
335-
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
336-
list(PREPEND _psuf openmpi-x86_64 mpich-x86_64)
337-
endif()
338-
339-
set(_msuf ${_psuf})
340-
if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
341-
list(APPEND _msuf gfortran/modules)
342-
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
343-
list(PREPEND _msuf gfortran/modules/openmpi gfortran/modules/mpich)
344-
endif()
345-
endif()
346-
# Not immediately clear the benefits of this, as we'd have to foreach()
347-
# a priori names, kind of like we already do with find_library()
348-
# find_package(hdf5 CONFIG)
349-
# message(STATUS "hdf5 found ${hdf5_FOUND}")
350-
351-
if(Fortran IN_LIST HDF5_FIND_COMPONENTS)
352-
find_hdf5_fortran()
353-
endif()
354-
355-
if(CXX IN_LIST HDF5_FIND_COMPONENTS)
356-
find_hdf5_cxx()
315+
if(NOT HDF5_C_FOUND)
316+
return()
357317
endif()
358318

359-
# C is always needed
360-
find_hdf5_c()
361-
362-
# required libraries
363-
if(HDF5_C_FOUND)
364-
detect_config()
365-
endif(HDF5_C_FOUND)
366-
367-
# --- configure time checks
368-
# these checks avoid messy, confusing errors at build time
369-
370-
if(HDF5_C_FOUND)
371-
372319
list(PREPEND CMAKE_REQUIRED_LIBRARIES ${HDF5_C_LIBRARIES})
373320
set(CMAKE_REQUIRED_INCLUDES ${HDF5_C_INCLUDE_DIR})
374321

375322
if(HDF5_parallel_FOUND)
376323
list(APPEND CMAKE_REQUIRED_LIBRARIES MPI::MPI_C)
377324

325+
check_symbol_exists(H5Pset_fapl_mpio hdf5.h HAVE_H5Pset_fapl_mpio)
326+
if(NOT HAVE_H5Pset_fapl_mpio)
327+
return()
328+
endif()
329+
378330
set(src [=[
379331
#include "hdf5.h"
380332
#include "mpi.h"
@@ -398,20 +350,21 @@ else()
398350
#include "hdf5.h"
399351

400352
int main(void){
401-
hid_t f = H5Fcreate ("junk.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
353+
hid_t f = H5Fcreate("junk.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
402354
herr_t status = H5Fclose (f);
403355
return 0;}
404356
]=])
405357
endif(HDF5_parallel_FOUND)
406358

407359
check_c_source_compiles("${src}" HDF5_C_links)
408360

409-
set(HDF5_links ${HDF5_C_links})
361+
if(NOT HDF5_C_links)
362+
return()
363+
endif()
410364

411-
endif(HDF5_C_FOUND)
412365

413366

414-
if(HDF5_Fortran_FOUND AND HDF5_links)
367+
if(HDF5_Fortran_FOUND)
415368

416369
list(PREPEND CMAKE_REQUIRED_LIBRARIES ${HDF5_Fortran_LIBRARIES})
417370
set(CMAKE_REQUIRED_INCLUDES ${HDF5_Fortran_INCLUDE_DIR} ${HDF5_C_INCLUDE_DIR})
@@ -451,18 +404,76 @@ endif()
451404

452405
check_fortran_source_compiles(${src} HDF5_Fortran_links SRC_EXT f90)
453406

454-
455407
if(NOT HDF5_Fortran_links)
456-
set(HDF5_Fortran_links false)
408+
return()
457409
endif()
458410

459-
endif(HDF5_Fortran_FOUND AND HDF5_links)
411+
endif(HDF5_Fortran_FOUND)
412+
413+
414+
set(HDF5_links true PARENT_SCOPE)
415+
416+
endfunction(check_hdf5_link)
417+
418+
# === main program
460419

420+
set(CMAKE_REQUIRED_LIBRARIES)
461421

462-
if(HDF5_Fortran_FOUND AND NOT HDF5_Fortran_links)
463-
set(HDF5_links false)
422+
# we don't use pkg-config names because some distros pkg-config for HDF5 is broken
423+
# however at least the paths are often correct
424+
find_package(PkgConfig)
425+
if(NOT HDF5_FOUND)
426+
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
427+
pkg_search_module(pc_hdf5 hdf5-openmpi hdf5-mpich hdf5)
428+
else()
429+
pkg_search_module(pc_hdf5 hdf5-serial hdf5)
430+
endif()
464431
endif()
465432

433+
set(_lsuf hdf5)
434+
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
435+
list(PREPEND _lsuf openmpi/lib mpich/lib hdf5/openmpi hdf5/mpich)
436+
else()
437+
list(PREPEND _lsuf hdf5/serial)
438+
endif()
439+
440+
set(_psuf static ${_lsuf})
441+
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
442+
list(PREPEND _psuf openmpi-x86_64 mpich-x86_64)
443+
endif()
444+
445+
set(_msuf ${_psuf})
446+
if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
447+
list(APPEND _msuf gfortran/modules)
448+
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
449+
list(PREPEND _msuf gfortran/modules/openmpi gfortran/modules/mpich)
450+
endif()
451+
endif()
452+
# Not immediately clear the benefits of this, as we'd have to foreach()
453+
# a priori names, kind of like we already do with find_library()
454+
# find_package(hdf5 CONFIG)
455+
# message(STATUS "hdf5 found ${hdf5_FOUND}")
456+
457+
if(Fortran IN_LIST HDF5_FIND_COMPONENTS)
458+
find_hdf5_fortran()
459+
endif()
460+
461+
if(CXX IN_LIST HDF5_FIND_COMPONENTS)
462+
find_hdf5_cxx()
463+
endif()
464+
465+
# C is always needed
466+
find_hdf5_c()
467+
468+
# required libraries
469+
if(HDF5_C_FOUND)
470+
detect_config()
471+
endif(HDF5_C_FOUND)
472+
473+
# --- configure time checks
474+
# these checks avoid messy, confusing errors at build time
475+
check_hdf5_link()
476+
466477
set(CMAKE_REQUIRED_LIBRARIES)
467478
set(CMAKE_REQUIRED_INCLUDES)
468479

0 commit comments

Comments
 (0)