netcdf-fortran icon indicating copy to clipboard operation
netcdf-fortran copied to clipboard

upgrade CMake build to use KitWare FindNetCDF?

Open edwardhartnett opened this issue 5 years ago • 3 comments

On another project it was pointed out to me that there is a FindNetCDF() CMake function from Kitware: https://github.com/Kitware/VTK/blob/master/CMake/FindNetCDF.cmake

It seems very comprehensive.

In netcdf-fortran there is no call to FindNetCDF, but there is a section of code which performs that function:

###
# Seek out netcdf library file and include path.
###

IF(NOT netCDF_LIBRARIES AND NOT netCDF_INCLUDE_DIR)
  FIND_PACKAGE(netCDF QUIET)
ELSE()
  SET(netCDF_FOUND TRUE)
ENDIF()

IF (netCDF_FOUND)
  SET(NETCDF_C_LIBRARY ${netCDF_LIBRARIES})
  SET(NETCDF_C_INCLUDE_DIR ${netCDF_INCLUDE_DIR})
  message(STATUS "Found netCDF CMake package: ${NETCDF_C_LIBRARY}")
ELSE()
  # netCDF not properly packaged. Try to find it manually.
  FIND_LIBRARY(NETCDF_C_LIBRARY NAMES netcdf libnetcdf)
  IF(NOT NETCDF_C_LIBRARY)
    MESSAGE(FATAL_ERROR "libnetcdf not found. Please reinstall and try again.")
  ELSE()
    FIND_PATH(NETCDF_C_INCLUDE_DIR "netcdf.h")
    MESSAGE(STATUS "Found netcdf: ${NETCDF_C_LIBRARY}")
    IF(NOT NETCDF_C_INCLUDE_DIR)
      MESSAGE(FATAL_ERROR "Directory containing netcdf.h cannot be found. Please reinstall and try again.")
    ELSE()
      GET_FILENAME_COMPONENT(netCDF_LIB_DIR "${NETCDF_C_LIBRARY}" REALPATH)
    ENDIF()
  ENDIF()
ENDIF()

@WardF I would be interested in your thoughts. We face this same issue in PIO CMake builds...

edwardhartnett avatar Aug 04 '20 15:08 edwardhartnett

I think this is worth pursuing; I don't recall why we didn't use it, either it didn't exist at the time, or we didn't know about it, but either way, adding this support makes sense.

WardF avatar Aug 06 '20 22:08 WardF

@WardF @edhartnett https://github.com/NOAA-EMC/CMakeModules/blob/develop/Modules/FindNetCDF.cmake Care to take a look and see if this works for the NetCDF community?

It works for C, Fortran and C++ NetCDF API's It also works when they are all installed in the same location or distributed.

aerorahul avatar Sep 02 '20 16:09 aerorahul

@aerorahul perhaps you could submit a PR with this change? That would be the logical next step...

edwardhartnett avatar Sep 02 '20 16:09 edwardhartnett