Skip to content

Commit

Permalink
Better way of handling the spio->netcdf dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jgfouca committed Sep 29, 2023
1 parent 3fcd94e commit 293a797
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 53 deletions.
6 changes: 0 additions & 6 deletions components/cmake/common_setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
100 changes: 54 additions & 46 deletions components/cmake/modules/FindNETCDF.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
4 changes: 3 additions & 1 deletion components/cmake/modules/FindPIO.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 293a797

Please sign in to comment.