NOAA-EMC / CMakeModules

Common cmake module files used by spack and cmake to find dependencies
Other
4 stars 14 forks source link

Unable to find NetCDF when building UFS_UTILS standalone #30

Closed kgerheiser closed 4 years ago

kgerheiser commented 4 years ago

@GeorgeGayno-NOAA was unable to build UFS_UTILS standalone on Hera, and I was able to reproduce.

It was unable to find NetCDF even though the NetCDF module is loaded with:

module load netcdf/4.7.0

And to build:

git clone -b ufs-v1.0.0 --recursive https://github.com/NOAA-EMC/UFS_UTILS.git
cd UFS_UTILS
mkdir build
cd buil
cmake ..
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.27.1")
-- Checking for module 'netcdf>=4'
--   No package 'netcdf' found
CMake Error at /apps/spack/linux-centos7-x86_64/gcc-4.8.5/cmake-3.16.1-pujkqsxrn5sipm422gxshrq27d3miagd/share/cmake-3.16/Modules/FindPkgConfig.cmake:463 (message):
  A required package was not found
Call Stack (most recent call first):
  /apps/spack/linux-centos7-x86_64/gcc-4.8.5/cmake-3.16.1-pujkqsxrn5sipm422gxshrq27d3miagd/share/cmake-3.16/Modules/FindPkgConfig.cmake:643 (_pkg_check_modules_internal)
  cmake/Modules/FindNetCDF.cmake:23 (pkg_check_modules)
  CMakeLists.txt:34 (find_package)

-- Configuring incomplete, errors occurred!

I don't quite understand how pkg_check_modules works, but that's where it seems to be failing. https://github.com/NOAA-EMC/CMakeModules/blob/b605db0df4f8fb1110aaba989dbffad5312752b9/Modules/FindNetCDF.cmake#L23

Any ideas @climbfuji?

climbfuji commented 4 years ago

I think cmake needs to know where to look for the pkg config file. The way this is done in the umbrella build is in lines 80-93 of the umbrella build CMakeLists.txt:

# If NETCDF is not set in the NCEPLIBS-external cmake configuration file,
# need to add environment variable NETCDF to CMAKE_PREFIX_PATH for PkgConfig
# and set cmake variable accordingly
if(NOT NETCDF)
  if(NOT DEFINED ENV{NETCDF})
    message(FATAL_ERROR "Environment variable NETCDF not set")
  else()
    list(APPEND CMAKE_PREFIX_PATH $ENV{NETCDF})
    set(NETCDF $ENV{NETCDF})
  endif()
  if(DEFINED ENV{NETCDF_FORTRAN})
    list(APPEND CMAKE_PREFIX_PATH $ENV{NETCDF_FORTRAN})
  endif()
endif()

There is also a way to set/append to an environment variable in Linux that tells pkgconfig where to look. The consistent rule we have used between NCEPLIBS-external and NCEPLIBS is to require the user/module to set the environment variable NETCDF and then use the above block of code to append to CMAKE_PREFIX_PATH.

climbfuji commented 4 years ago

Does this help you? I guess you only have to copy & paste the block above into the UFS_UTILS CMakeLists.txt. If UFS_UTILS is built as part of NCEPLIBS (umbrella), then NETCDF is already set (because of the identical block in the NCEPLIBS CMakeLists.txt), and nothing changes.

We probably need to apply the same bugfix to EMC_post.

@kgerheiser @mark-a-potts @GeorgeGayno-NOAA @WenMeng-NOAA

aerorahul commented 4 years ago

I think we need to fix the FindNetCDF.cmake to actually find the libraries given some hints.

climbfuji commented 4 years ago

I think one of the hints could be the environment variables NETCDF and NETCDF_FORTRAN?

kgerheiser commented 4 years ago

Fixed with Rahul's FindNetCDF