diff --git a/FindNetCDF.cmake b/FindNetCDF.cmake index 05b4e84af44cc1600434680000caf21eac2d088e..a05d0e5c9e3a7377bf0790aa2d2de3cccd4c53a8 100644 --- a/FindNetCDF.cmake +++ b/FindNetCDF.cmake @@ -1,3 +1,55 @@ +# https://gitlab.kitware.com/vtk/vtk/blob/d42d8eb02bc965134eb92dde4aaab92223cb8a6a/CMake/FindNetCDF.cmake + +# I have had trouble with this version of FindNetCDF for ml2pl on +# Irene, but, on the other hand, I found this version of FindNetCDF +# useful for NetCDF95 on Ciclad, taking advantage of pkg-config. + +#[==[ +Provides the following variables: + + * `NetCDF_FOUND`: Whether NetCDF was found or not. + * `NetCDF_INCLUDE_DIRS`: Include directories necessary to use NetCDF. + * `NetCDF_LIBRARIES`: Libraries necessary to use NetCDF. + * `NetCDF_VERSION`: The version of NetCDF found. + * `NetCDF::NetCDF`: A target to use with `target_link_libraries`. +#]==] + +# Try to find a CMake-built NetCDF. +find_package(netCDF CONFIG QUIET) +if (netCDF_FOUND) + # Forward the variables in a consistent way. + set(NetCDF_FOUND "${netCDF_FOUND}") + set(NetCDF_INCLUDE_DIRS "${netCDF_INCLUDE_DIR}") + set(NetCDF_LIBRARIES "${netCDF_LIBRARIES}") + set(NetCDF_VERSION "${NetCDFVersion}") + if (NOT TARGET NetCDF::NetCDF) + add_library(NetCDF::NetCDF INTERFACE IMPORTED) + set_target_properties(NetCDF::NetCDF PROPERTIES + INTERFACE_LINK_LIBRARIES "${NetCDF_LIBRARIES}") + endif () + # Skip the rest of the logic in this file. + return () +endif () + +find_package(PkgConfig QUIET) +if (PkgConfig_FOUND) + pkg_check_modules(_NetCDF QUIET netcdf IMPORTED_TARGET) + if (_NetCDF_FOUND) + # Forward the variables in a consistent way. + set(NetCDF_FOUND "${_NetCDF_FOUND}") + set(NetCDF_INCLUDE_DIRS "${_NetCDF_INCLUDE_DIRS}") + set(NetCDF_LIBRARIES "${_NetCDF_LIBRARIES}") + set(NetCDF_VERSION "${_NetCDF_VERSION}") + if (NOT TARGET NetCDF::NetCDF) + add_library(NetCDF::NetCDF INTERFACE IMPORTED) + set_target_properties(NetCDF::NetCDF PROPERTIES + INTERFACE_LINK_LIBRARIES "PkgConfig::_NetCDF") + endif () + # Skip the rest of the logic in this file. + return () + endif () +endif () + find_path(NetCDF_INCLUDE_DIR NAMES netcdf.h DOC "netcdf include directories")