Unidata / netcdf-fortran

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

download, build, install netcdf and netcdf-fortran in an existing CMake project #442

Closed BenjaminTJohnson closed 5 months ago

BenjaminTJohnson commented 5 months ago

Background: linux environment (no existing netcdf/hdf libraries) CMake v3.23 latest releases of netcdf-c and netcdf-fortran.

In a CMake project (CRTM) that requires netCDF-Fortran, I'm attempting to give users the option to download and compile netcdf-fortran, all during the cmake configuration step.

In short (pseudo code):

cd crtm/build
cmake ..
[IF netCDF not found:]
   (clone and build netcdf-c)
   (clone and build netcdf-fortran)

I am able to successfully fetch, download, and cmake netcdf-c, and similarly for netcdf-fortran, but the latter fails during its cmake step with the error message:

CMake Error at build/_deps/netcdf-fortran-src/CMakeLists.txt:469 (MESSAGE):
  libnetcdf not found.  Please reinstall and try again.

So netcdf-fortran is not able to "see" that netcdf-c has been installed.

Is there an example of code where this is successfully accomplished that I can look at?
or, alternatively, is there a different method for accomplishing this. I'm completely open to any methods that can be run during the CRTM's cmake step.

I've attached my CMakeLists.txt file, in case you want to have a look. CMakeLists.txt

BenjaminTJohnson commented 5 months ago

@WardF @edhartnett tagging Ward / Ed for visibility. Thanks --

edwardhartnett commented 5 months ago

I don' t think this is a great idea.

Instead, take a look at spack, which is perfect for this sort of thing. I would suggest that netcdf-fortran does not need to know about the install details of netcdf-c, and vice-versa, because that clutters up the cmake files, and spack already knows this out of the box.

For example, you can say:

spack install netcdf-fortran

and it will install HDF5, zlib, netcdf-c, etc., and then netcdf-fortran. It's very clever and you can control all the options from the command line to get exactly what you need. See https://spack.io/

Spack is being used at a lot of HPC centers now to manage the build of complex scientific software...

BenjaminTJohnson commented 5 months ago

Yeah, personally I use spack/spack-stack on all of the HPC environments I'm involved with. I was hoping to avoid a multi-step process, but you're probably right in that it will be easier for me to support / debug if they're using spack.

Thanks for the advice.