Skip to content

Commit

Permalink
Make Netcdf depend on HDF5, fix TriBITS find modules (TriBITSPub/TriB…
Browse files Browse the repository at this point in the history
…ITS#533)

This makes Netcdf depend on HDF5 using modern CMake targets.

ToDo: Finish this commit message.
  • Loading branch information
bartlettroscoe committed Oct 24, 2022
1 parent dbc5f6b commit 4f18ccc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 109 deletions.
135 changes: 77 additions & 58 deletions cmake/tribits/common_tpls/FindTPLHDF5.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,97 @@
# See associated tribits/Copyright.txt file for copyright and license! #
########################################################################

#
# First, set up the variables for the (backward-compatible) TriBITS way of
# finding HDF5. These are used in case find_package(HDF5 ...) is not called
# or does not find HDF5. Also, these variables need to be non-null in order
# to trigger the right behavior in the function
# tribits_tpl_find_include_dirs_and_libraries().
#

set(REQUIRED_HEADERS hdf5.h)
set(REQUIRED_LIBS_NAMES hdf5)

if (HDF5_REQUIRE_FORTRAN)
set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} hdf5_fortran)
endif()
if (Netcdf_ALLOW_MODERN)

if (TPL_ENABLE_MPI)
set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} z)
endif()
#set(minimum_modern_HDF5_version 4.7.4)
print_var(Netcdf_ALLOW_MODERN)
message("-- Using find_package(HDF5 ${minimum_modern_HDF5_version} CONFIG) ...")
find_package(HDF5 ${minimum_modern_HDF5_version} CONFIG)
if (HDF5_FOUND)
message("-- Found HDF5_CONFIG=${HDF5_CONFIG}")
message("-- Generating Netcdf::all_libs and NetcdfConfig.cmake")
tribits_extpkg_create_imported_all_libs_target_and_config_file(
HDF5
INNER_FIND_PACKAGE_NAME HDF5
IMPORTED_TARGETS_FOR_ALL_LIBS HDF5::HDF5)
endif()

if (TPL_ENABLE_Netcdf)
set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} hdf5_hl)
endif()

#
# Second, search for HDF5 components (if allowed) using the standard
# find_package(HDF5 ...).
#
tribits_tpl_allow_pre_find_package(HDF5 HDF5_ALLOW_PREFIND)
if (HDF5_ALLOW_PREFIND)
if (NOT TARGET HDF5::all_libs)

message("-- Using find_package(HDF5 ...) ...")
# First, set up the variables for the (backward-compatible) TriBITS way of
# finding HDF5. These are used in case find_package(HDF5 ...) is not called
# or does not find HDF5. Also, these variables need to be non-null in order
# to trigger the right behavior in the function
# tribits_tpl_find_include_dirs_and_libraries().

set(REQUIRED_HEADERS hdf5.h)
set(REQUIRED_LIBS_NAMES hdf5)

set(HDF5_COMPONENTS C)
if (HDF5_REQUIRE_FORTRAN)
list(APPEND HDF5_COMPONENTS Fortran)
set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} hdf5_fortran)
endif()

if (TPL_ENABLE_MPI)
set(HDF5_PREFER_PARALLEL TRUE)
set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} z)
endif()

find_package(HDF5 COMPONENTS ${HDF5_COMPONENTS})

# Make sure that HDF5 is parallel.
if (TPL_ENABLE_MPI AND NOT HDF5_IS_PARALLEL)
message(FATAL_ERROR "Trilinos is configured for MPI, HDF5 is not.
Did CMake find the correct libraries?
Try setting HDF5_INCLUDE_DIRS and/or HDF5_LIBRARY_DIRS explicitly.
")
if (TPL_ENABLE_Netcdf)
set(REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES} hdf5_hl)
endif()

if (HDF5_FOUND)
# Tell TriBITS that we found HDF5 and there no need to look any further!
set(TPL_HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIRS} CACHE PATH
"HDF5 include dirs")
set(TPL_HDF5_LIBRARIES ${HDF5_LIBRARIES} CACHE FILEPATH
"HDF5 libraries")
set(TPL_HDF5_LIBRARY_DIRS ${HDF5_LIBRARY_DIRS} CACHE PATH
"HDF5 library dirs")
#
# Second, search for HDF5 components (if allowed) using the standard
# find_package(HDF5 ...).
#
tribits_tpl_allow_pre_find_package(HDF5 HDF5_ALLOW_PREFIND)
if (HDF5_ALLOW_PREFIND)

message("-- Using find_package(HDF5 ...) ...")

set(HDF5_COMPONENTS C)
if (HDF5_REQUIRE_FORTRAN)
list(APPEND HDF5_COMPONENTS Fortran)
endif()

if (TPL_ENABLE_MPI)
set(HDF5_PREFER_PARALLEL TRUE)
endif()

find_package(HDF5 COMPONENTS ${HDF5_COMPONENTS})

# Make sure that HDF5 is parallel.
if (TPL_ENABLE_MPI AND NOT HDF5_IS_PARALLEL)
message(FATAL_ERROR "Trilinos is configured for MPI, HDF5 is not.
Did CMake find the correct libraries?
Try setting HDF5_INCLUDE_DIRS and/or HDF5_LIBRARY_DIRS explicitly.
")
endif()

if (HDF5_FOUND)
# Tell TriBITS that we found HDF5 and there no need to look any further!
set(TPL_HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIRS} CACHE PATH
"HDF5 include dirs")
set(TPL_HDF5_LIBRARIES ${HDF5_LIBRARIES} CACHE FILEPATH
"HDF5 libraries")
set(TPL_HDF5_LIBRARY_DIRS ${HDF5_LIBRARY_DIRS} CACHE PATH
"HDF5 library dirs")
endif()

endif()

endif()
#
# Third, call tribits_tpl_find_include_dirs_and_libraries()
#
tribits_tpl_find_include_dirs_and_libraries( HDF5
REQUIRED_HEADERS ${REQUIRED_HEADERS}
REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES}
)
# NOTE: If find_package(HDF5 ...) was called and successfully found HDF5, then
# tribits_tpl_find_include_dirs_and_libraries() will use the already-set
# variables TPL_HDF5_INCLUDE_DIRS and TPL_HDF5_LIBRARIES and then print them
# out (and set some other standard variables as well). This is the final
# "hook" into the TriBITS TPL system.

#
# Third, call tribits_tpl_find_include_dirs_and_libraries()
#
tribits_tpl_find_include_dirs_and_libraries( HDF5
REQUIRED_HEADERS ${REQUIRED_HEADERS}
REQUIRED_LIBS_NAMES ${REQUIRED_LIBS_NAMES}
)
# NOTE: If find_package(HDF5 ...) was called and successfully found HDF5, then
# tribits_tpl_find_include_dirs_and_libraries() will use the already-set
# variables TPL_HDF5_INCLUDE_DIRS and TPL_HDF5_LIBRARIES and then print them
# out (and set some other standard variables as well). This is the final
# "hook" into the TriBITS TPL system.
endif()
66 changes: 15 additions & 51 deletions cmake/tribits/common_tpls/FindTPLNetcdf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,72 +37,36 @@
# ************************************************************************
# @HEADER

#
# First, set up the variables for the (backward-compatible) TriBITS way of
# finding Netcdf. These are used in case find_package(NetCDF ...) is not
# called or does not find NetCDF. Also, these variables need to be non-null
# in order to trigger the right behavior in the function
# tribits_tpl_find_include_dirs_and_libraries().
#
if (${CMAKE_VERSION} GREATER "3.13")
cmake_policy(SET CMP0074 NEW)
endif()

set(Netcdf_ALLOW_MODERN FALSE CACHE BOOL "Allow finding Netcdf as a modern CMake config file with exported targets (and only this way)")

if (Netcdf_ALLOW_MODERN)
if (Netcdf_ALLOW_MODERN AND TARGET HDF5::HDF5)

set(minimum_modern_netCDF_version 4.7.4)
message("-- Netcdf_ALLOW_MODERN=${Netcdf_ALLOW_MODERN}")
print_var(Netcdf_ALLOW_MODERN)
message("-- Using find_package(netCDF ${minimum_modern_netCDF_version} CONFIG) ...")
find_package(netCDF ${minimum_modern_netCDF_version} CONFIG)
if (netCDF_FOUND)
message("-- Found netCDF_CONFIG=${netCDF_CONFIG}")
message("-- Generating Netcdf::all_libs and NetcdfConfig.cmake")
# instead of using tribits_extpkg_create_imported_all_libs_target_and_config_file,
# we will do what it does ourselves because we need to bring in two
# inner find packages (netCDF and hdf5) because netCDF does not properly
# do find_dependency(hdf5)
add_library(Netcdf::all_libs INTERFACE IMPORTED GLOBAL)
target_link_libraries(Netcdf::all_libs INTERFACE netCDF::netcdf)
set(configFileStr "")
string(APPEND configFileStr
"include(CMakeFindDependencyMacro)\n" )
string(APPEND configFileStr
"set(netCDF_DIR \"${netCDF_DIR}\")\n" )
string(APPEND configFileStr
"find_dependency(netCDF ${minimum_modern_netCDF_version} CONFIG)\n"
)
if (netCDF_HAS_HDF5)
find_package(hdf5 CONFIG REQUIRED)
string(APPEND configFileStr
"set(hdf5_DIR \"${hdf5_DIR}\")\n" )
string(APPEND configFileStr
"find_dependency(hdf5 CONFIG)\n"
)
endif()
string(APPEND configFileStr
"add_library(Netcdf::all_libs INTERFACE IMPORTED GLOBAL)\n"
)
string(APPEND configFileStr
"target_link_libraries(Netcdf::all_libs INTERFACE netCDF::netcdf)\n")
set(buildDirExternalPkgsDir
"${${PROJECT_NAME}_BINARY_DIR}/${${PROJECT_NAME}_BUILD_DIR_EXTERNAL_PKGS_DIR}")
set(tplConfigFile
"${buildDirExternalPkgsDir}/Netcdf/NetcdfConfig.cmake")
file(WRITE "${tplConfigFile}" "${configFileStr}")
# the following gets set by tribits_tpl_find_include_dirs_and_libraries
# and it is not well-documented that you need to set it yourself if not using
# tribits_tpl_find_include_dirs_and_libraries
set(TPL_Netcdf_NOT_FOUND FALSE)
message("-- TPL_Netcdf_PARALLEL = netCDF_HAS_PARALLEL = ${netCDF_HAS_PARALLEL}")
set(TPL_Netcdf_PARALLEL ${netCDF_HAS_PARALLEL})
message("-- TPL_Netcdf_Enables_Netcdf4 = netCDF_HAS_NC4 = ${netCDF_HAS_NC4}")
set(TPL_Netcdf_Enables_Netcdf4 ${netCDF_HAS_NC4} CACHE BOOL
"True if netcdf enables netcdf-4")
tribits_extpkg_create_imported_all_libs_target_and_config_file(
Netcdf
INNER_FIND_PACKAGE_NAME netCDF
IMPORTED_TARGETS_FOR_ALL_LIBS netCDF::netcdf)
endif()

else()
endif()

if (NOT TARGET Netcdf::all_libs)

# First, set up the variables for the (backward-compatible) TriBITS way of
# finding Netcdf. These are used in case find_package(Netcdf ...) is not
# called or does not find HDF5. Also, these variables need to be non-null
# in order to trigger the right behavior in the function
# tribits_tpl_find_include_dirs_and_libraries().

set(REQUIRED_HEADERS netcdf.h)
set(REQUIRED_LIBS_NAMES netcdf)
Expand Down
2 changes: 2 additions & 0 deletions cmake/tribits/common_tpls/FindTPLNetcdfDependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tribits_extpkg_define_dependencies( Netcdf
DEPENDENCIES HDF5)

0 comments on commit 4f18ccc

Please sign in to comment.