Unidata / netcdf-fortran

Official GitHub repository for netCDF-Fortran libraries, which depend on the netCDF C library. Install the netCDF C library first.
Other
231 stars 95 forks source link

upgrade CMake build to use KitWare FindNetCDF? #276

Open edwardhartnett opened 4 years ago

edwardhartnett commented 4 years ago

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...

WardF commented 4 years ago

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.

aerorahul commented 3 years ago

@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.

edwardhartnett commented 3 years ago

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