How to check for available HDF5 options in CMake?

101 Views Asked by At

I'm using CMake to configure a project including HDF5 support. I can detect whether HDF5 is installed using find_package(HDF5 ...). According to the CMake manual entry, the find_package call will set a few more variables, e.g., HDF5_IS_PARALLEL.

However, when building HDF5, one can set a lot more build options. For my project, I need to have an HDF5 installation with both the HDF5_ENABLE_DIRECT_VFD and HDF5_ENABLE_THREADSAFE options enabled. Trying to run my project using an HDF5 installation without those options will either not compile or yield various runtime errors.

How can I detect if those options are present in the HDF5 installation during the CMake configuration of my project?

1

There are 1 best solutions below

0
On

I found a somewhat working approach: the HDF5 configuration is stored in a file libhdf5.settings within the library path. CMake is able to read and parse this file. I have not found a way to reliably get the correct path, though. Still, for the default install directory, this snippet is able to parse the options:

find_package(HDF5 COMPONENTS C)

if (HDF5_FOUND) 
    message("HDF5 was found, searching configuration...")
    # this search works for default install path of any version
    # change path if hdf5 was build into a non-default directory
    file(GLOB HDF5_SETTINGS "/usr/local/HDF_Group/HDF5/*/lib/libhdf5.settings") 

    if (HDF5_SETTINGS)
        message("Checking HDF5 settings in : ${HDF5_SETTINGS}")
        file(STRINGS ${HDF5_SETTINGS} HDF5_SETTINGS_CONTENT)
        string(REGEX MATCH "Threadsafety: ON" HDF5_HAS_THREADSAFE ${HDF5_SETTINGS_CONTENT})
        string(REGEX MATCH "Direct VFD: 1" HDF5_HAS_DIRECTIO ${HDF5_SETTINGS_CONTENT})
        if (HDF5_HAS_THREADSAFE AND HDF5_HAS_DIRECTIO)
            message("Correct HDF5 settings found.")
        else()
            message(WARNING "HDF5 Settings are incorrect. Please reinstall HDF5 with --threadsafe and --directvfd options enabled.")
            set(HDF5_FOUND false)
        endif()
    endif(HDF5_SETTINGS)
endif(HDF5_FOUND)