From 293a7971dbe5e93ab12e319a0c6086c0e10ba94a Mon Sep 17 00:00:00 2001 From: James Foucar Date: Fri, 29 Sep 2023 10:16:04 -0600 Subject: [PATCH] Better way of handling the spio->netcdf dependency --- components/cmake/common_setup.cmake | 6 -- components/cmake/modules/FindNETCDF.cmake | 100 ++++++++++++---------- components/cmake/modules/FindPIO.cmake | 4 +- 3 files changed, 57 insertions(+), 53 deletions(-) diff --git a/components/cmake/common_setup.cmake b/components/cmake/common_setup.cmake index adc809293693..2290ec8c715d 100644 --- a/components/cmake/common_setup.cmake +++ b/components/cmake/common_setup.cmake @@ -107,12 +107,6 @@ else() endif() endif() -# Have to do this here for now because the macros must be loaded in order for -# the netcdf cmake variables to be set. Once scorpio has a good package-config, -# this won't be necessary -find_package(NETCDF REQUIRED) -target_link_libraries(spio INTERFACE netcdf) - # Set HAVE_SLASHPROC on LINUX systems which are not bluegene or Darwin (OSx) string(FIND "${CPPDEFS}" "-DLINUX" HAS_DLINUX) string(FIND "${CPPDEFS}" "DBG" HAS_DBG) diff --git a/components/cmake/modules/FindNETCDF.cmake b/components/cmake/modules/FindNETCDF.cmake index df7458f7f923..38284d15e492 100644 --- a/components/cmake/modules/FindNETCDF.cmake +++ b/components/cmake/modules/FindNETCDF.cmake @@ -11,57 +11,65 @@ if (TARGET netcdf) return() endif() -# Pnetcdf is optional, and only if not running serial -if (NOT MPILIB STREQUAL mpi-serial) - if (PNETCDF_PATH) - find_library(pnetcdf_lib pnetcdf REQUIRED PATHS ${PNETCDF_PATH}/lib) - find_path (pnetcdf_incdir pnetcdf.h REQUIRED PATHS ${PNETCDF_PATH}/include) - endif() -endif() +function(create_netcdf_target) + # Need to load macros to pick up netcdf vars. This should not impact + # the scope of the caller because we are doing this within a function + include(${CASEROOT}/Macros.cmake) -if (NETCDF_C_PATH) - # Sanity checks - if (NOT NETCDF_FORTRAN_PATH) - message(FATAL_ERROR "NETCDF_C_PATH specified without NETCDF_FORTRAN_PATH") + # Pnetcdf is optional, and only if not running serial + if (NOT MPILIB STREQUAL mpi-serial) + if (PNETCDF_PATH) + find_library(pnetcdf_lib pnetcdf REQUIRED PATHS ${PNETCDF_PATH}/lib) + find_path (pnetcdf_incdir pnetcdf.h REQUIRED PATHS ${PNETCDF_PATH}/include) + endif() endif() - if (NOT EXISTS ${NETCDF_C_PATH}/lib AND NOT EXISTS ${NETCDF_C_PATH}/lib64) - message(FATAL_ERROR "NETCDF_C_PATH does not contain a lib or lib64 directory") - endif () - if (NOT EXISTS ${NETCDF_FORTRAN_PATH}/lib AND NOT EXISTS ${NETCDF_FORTRAN_PATH}/lib64) - message(FATAL_ERROR "NETCDF_FORTRAN_PATH does not contain a lib or lib64 directory") - endif () - # Find the libraries - find_library(netcdf_c_lib netcdf REQUIRED PATHS ${NETCDF_C_PATH}/lib ${NETCDF_C_PATH}/lib64) - find_library(netcdf_f_lib netcdff REQUIRED PATHS ${NETCDF_FORTRAN_PATH}/lib ${NETCDF_FORTRAN_PATH}/lib64) - find_path (netcdf_c_incdir netcdf.h REQUIRED PATHS ${NETCDF_C_PATH}/include) - find_path (netcdf_f_incdir netcdf.inc REQUIRED PATHS ${NETCDF_FORTRAN_PATH}/include) + if (NETCDF_C_PATH) + # Sanity checks + if (NOT NETCDF_FORTRAN_PATH) + message(FATAL_ERROR "NETCDF_C_PATH specified without NETCDF_FORTRAN_PATH") + endif() + if (NOT EXISTS ${NETCDF_C_PATH}/lib AND NOT EXISTS ${NETCDF_C_PATH}/lib64) + message(FATAL_ERROR "NETCDF_C_PATH does not contain a lib or lib64 directory") + endif () + if (NOT EXISTS ${NETCDF_FORTRAN_PATH}/lib AND NOT EXISTS ${NETCDF_FORTRAN_PATH}/lib64) + message(FATAL_ERROR "NETCDF_FORTRAN_PATH does not contain a lib or lib64 directory") + endif () -elseif (NETCDF_FORTRAN_PATH) - message(FATAL_ERROR "NETCDF_FORTRAN_PATH specified without NETCDF_C_PATH") -elseif (NETCDF_PATH) + # Find the libraries + find_library(netcdf_c_lib netcdf REQUIRED PATHS ${NETCDF_C_PATH}/lib ${NETCDF_C_PATH}/lib64) + find_library(netcdf_f_lib netcdff REQUIRED PATHS ${NETCDF_FORTRAN_PATH}/lib ${NETCDF_FORTRAN_PATH}/lib64) + find_path (netcdf_c_incdir netcdf.h REQUIRED PATHS ${NETCDF_C_PATH}/include) + find_path (netcdf_f_incdir netcdf.inc REQUIRED PATHS ${NETCDF_FORTRAN_PATH}/include) - # Sanity checks - if (NOT EXISTS ${NETCDF_PATH}/lib AND NOT EXISTS ${NETCDF_PATH}/lib64) - message(FATAL_ERROR "NETCDF_PATH does not contain a lib or lib64 directory") - endif () + elseif (NETCDF_FORTRAN_PATH) + message(FATAL_ERROR "NETCDF_FORTRAN_PATH specified without NETCDF_C_PATH") + elseif (NETCDF_PATH) - find_library(netcdf_c_lib netcdf REQUIRED PATHS ${NETCDF_PATH}/lib ${NETCDF_PATH}/lib64) - find_library(netcdf_f_lib netcdff REQUIRED PATHS ${NETCDF_PATH}/lib ${NETCDF_PATH}/lib64) - find_path (netcdf_c_incdir netcdf.h REQUIRED PATHS ${NETCDF_PATH}/include) - find_path (netcdf_f_incdir netcdf.inc REQUIRED PATHS ${NETCDF_PATH}/include) -else() - message(FATAL_ERROR "NETCDF not found: Define NETCDF_PATH or NETCDF_C_PATH and NETCDF_FORTRAN_PATH in config_machines.xml or config_compilers.xml") -endif() + # Sanity checks + if (NOT EXISTS ${NETCDF_PATH}/lib AND NOT EXISTS ${NETCDF_PATH}/lib64) + message(FATAL_ERROR "NETCDF_PATH does not contain a lib or lib64 directory") + endif () + + find_library(netcdf_c_lib netcdf REQUIRED PATHS ${NETCDF_PATH}/lib ${NETCDF_PATH}/lib64) + find_library(netcdf_f_lib netcdff REQUIRED PATHS ${NETCDF_PATH}/lib ${NETCDF_PATH}/lib64) + find_path (netcdf_c_incdir netcdf.h REQUIRED PATHS ${NETCDF_PATH}/include) + find_path (netcdf_f_incdir netcdf.inc REQUIRED PATHS ${NETCDF_PATH}/include) + else() + message(FATAL_ERROR "NETCDF not found: Define NETCDF_PATH or NETCDF_C_PATH and NETCDF_FORTRAN_PATH in config_machines.xml or config_compilers.xml") + endif() + + set (pnetcdf_lib ${pnetcdf_lib}) + set (netcdf_c_lib ${netcdf_c_lib}) + set (netcdf_f_lib ${netcdf_f_lib}) + set (pnetcdf_incdir ${pnetcdf_incdir}) + set (netcdf_c_incdir ${netcdf_c_incdir}) + set (netcdf_f_incdir ${netcdf_f_incdir}) -set (pnetcdf_lib ${pnetcdf_lib}) -set (netcdf_c_lib ${netcdf_c_lib}) -set (netcdf_f_lib ${netcdf_f_lib}) -set (pnetcdf_incdir ${pnetcdf_incdir}) -set (netcdf_c_incdir ${netcdf_c_incdir}) -set (netcdf_f_incdir ${netcdf_f_incdir}) + # Create the interface library, and set target properties + add_library(netcdf INTERFACE) + target_link_libraries(netcdf INTERFACE ${pnetcdf_lib};${netcdf_c_lib};${netcdf_f_lib}) + target_include_directories(netcdf INTERFACE ${pnetcdf_incdir};${netcdf_c_incdir};${netcdf_f_incdir}) +endfunction() -# Create the interface library, and set target properties -add_library(netcdf INTERFACE) -target_link_libraries(netcdf INTERFACE ${pnetcdf_lib};${netcdf_c_lib};${netcdf_f_lib}) -target_include_directories(netcdf INTERFACE ${pnetcdf_incdir};${netcdf_c_incdir};${netcdf_f_incdir}) +create_netcdf_target() diff --git a/components/cmake/modules/FindPIO.cmake b/components/cmake/modules/FindPIO.cmake index e339576cb7fb..5693483a9633 100644 --- a/components/cmake/modules/FindPIO.cmake +++ b/components/cmake/modules/FindPIO.cmake @@ -23,6 +23,8 @@ else() set(PIOLIBS "${PIO_LIBDIR}/libpio.a") endif() +find_package(NETCDF REQUIRED) + # Create the interface library, and set target properties add_library(spio INTERFACE) -target_link_libraries(spio INTERFACE ${PIOLIBS}) +target_link_libraries(spio INTERFACE ${PIOLIBS};netcdf)