Skip to content

Commit

Permalink
Merge pull request #2841 from ZedThree/find_pnetcdf
Browse files Browse the repository at this point in the history
Pull out `FindPNETCDF` CMake module
  • Loading branch information
WardF authored Mar 12, 2024
2 parents 5973f3d + 83e184b commit 50a3ce0
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_tests_ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ jobs:

strategy:
matrix:
hdf5: [ 1.14.0 ]
hdf5: [ 1.14.3 ]
steps:

- uses: actions/checkout@v3
Expand Down
66 changes: 8 additions & 58 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -893,47 +893,16 @@ if(ENABLE_PARALLEL4 AND ENABLE_HDF5)
endif()

# Options to enable parallel IO for classic formats with PnetCDF library.
set(STATUS_PNETCDF "OFF")
set(STATUS_PNETCDF ${ENABLE_PNETCDF})
set(USE_PNETCDF ${ENABLE_PNETCDF})
if(ENABLE_PNETCDF)
# Check for ncmpi_create in libpnetcdf, define USE_PNETCDF
# Does the user want to turn on PnetCDF read ability?
set(USE_PNETCDF ON CACHE BOOL "")
if(NOT PNETCDF)
message(STATUS "Cannot find PnetCDF library. Disabling PnetCDF support.")
set(USE_PNETCDF OFF CACHE BOOL "")
else(NOT PNETCDF)
set(USE_PARALLEL ON CACHE BOOL "")

# Check PnetCDF version. Must be >= 1.6.0
set(pnetcdf_h "${PNETCDF_INCLUDE_DIR}/pnetcdf.h" )
message(STATUS "PnetCDF include file ${pnetcdf_h} will be searched for version")
file(STRINGS "${pnetcdf_h}" pnetcdf_major_string REGEX "^#define PNETCDF_VERSION_MAJOR")
string(REGEX REPLACE "[^0-9]" "" pnetcdf_major "${pnetcdf_major_string}")
file(STRINGS "${pnetcdf_h}" pnetcdf_minor_string REGEX "^#define PNETCDF_VERSION_MINOR")
string(REGEX REPLACE "[^0-9]" "" pnetcdf_minor "${pnetcdf_minor_string}")
file(STRINGS "${pnetcdf_h}" pnetcdf_sub_string REGEX "^#define PNETCDF_VERSION_SUB")
string(REGEX REPLACE "[^0-9]" "" pnetcdf_sub "${pnetcdf_sub_string}")
set(pnetcdf_version "${pnetcdf_major}.${pnetcdf_minor}.${pnetcdf_sub}")
message(STATUS "Found PnetCDF version ${pnetcdf_version}")

if(${pnetcdf_version} VERSION_GREATER "1.6.0")
set(STATUS_PNETCDF "ON")
include_directories(${PNETCDF_INCLUDE_DIR})
set(HAVE_LIBPNETCDF ON)
# PnetCDF => parallel
set(STATUS_PARALLEL ON)
set(USE_PARALLEL ON)
message(STATUS "Using PnetCDF Library: ${PNETCDF}")
else()
message(WARNING "ENABLE_PNETCDF requires version 1.6.1 or later; found version ${pnetcdf_version}. PnetCDF is disabled")
endif()

set(STATUS_PARALLEL ON)
set(USE_PARALLEL ON CACHE BOOL "")
###
# Generate pnetcdf test.
# Generate pnetcdf test
###
configure_file("${netCDF_SOURCE_DIR}/nc_test/run_pnetcdf_tests.sh.in"
"${netCDF_BINARY_DIR}/nc_test/run_pnetcdf_tests.sh")
endif(NOT PNETCDF)
endif()

# Options to enable use of fill values for elements causing NC_ERANGE
Expand All @@ -956,38 +925,19 @@ endif(ENABLE_ERANGE_FILL)
set(ENABLE_ZERO_LENGTH_COORD_BOUND ON)

# check and conform with PnetCDF settings on ERANGE_FILL and RELAX_COORD_BOUND
if(STATUS_PNETCDF)
file(STRINGS "${pnetcdf_h}" enable_erange_fill_pnetcdf REGEX "^#define PNETCDF_ERANGE_FILL")
string(REGEX REPLACE "[^0-9]" "" erange_fill_pnetcdf "${enable_erange_fill_pnetcdf}")
if("x${erange_fill_pnetcdf}" STREQUAL "x1")
set(erange_fill_pnetcdf "ON")
else()
set(erange_fill_pnetcdf "OFF")
endif()
if(USE_PNETCDF)
if(ENABLE_ERANGE_FILL STREQUAL "AUTO") # not set on command line
set(ENABLE_ERANGE_FILL "${erange_fill_pnetcdf}")
set(ENABLE_ERANGE_FILL "${PNETCDF_HAS_ERANGE_FILL}")
else()
# user explicitly set this option on command line
if(NOT ENABLE_ERANGE_FILL STREQUAL "${erange_fill_pnetcdf}")
if(NOT ENABLE_ERANGE_FILL STREQUAL "${PNETCDF_HAS_ERANGE_FILL}")
if(ENABLE_ERANGE_FILL)
message(FATAL_ERROR "Enabling erange-fill conflicts with PnetCDF setting")
else()
message(FATAL_ERROR "Disabling erange-fill conflicts with PnetCDF setting")
endif()
endif()
endif()

file(STRINGS "${pnetcdf_h}" relax_coord_bound_pnetcdf REGEX "^#define PNETCDF_RELAX_COORD_BOUND")
string(REGEX REPLACE "[^0-9]" "" relax_coord_bound "${relax_coord_bound_pnetcdf}")
if("x${relax_coord_bound}" STREQUAL "x1")
set(relax_coord_bound_pnetcdf "ON")
else()
set(relax_coord_bound_pnetcdf "OFF")
endif()
# pnetcdf must have relaxed coord bounds to build with netCDF-4
if(NOT ENABLE_ZERO_LENGTH_COORD_BOUND STREQUAL "${relax_coord_bound_pnetcdf}")
message(FATAL_ERROR "Pnetcdf must be built with relax-coord-bound enabled")
endif()
endif()

if(ENABLE_ERANGE_FILL)
Expand Down
12 changes: 5 additions & 7 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -446,20 +446,18 @@ endif(ENABLE_LIBXML2)

################################
# MPI
################################
################################
if(ENABLE_PARALLEL4 OR HDF5_PARALLEL)
find_package(MPI REQUIRED)
endif()

################################
# parallel IO
# Parallel IO
################################
if(ENABLE_PNETCDF)
find_library(PNETCDF NAMES pnetcdf)
find_path(PNETCDF_INCLUDE_DIR pnetcdf.h)
if(NOT PNETCDF)
message(STATUS "Cannot find PnetCDF library. Disabling PnetCDF support.")
set(USE_PNETCDF OFF CACHE BOOL "")
find_package(PNETCDF 1.6.0 REQUIRED)
if(NOT PNETCDF_HAS_RELAXED_COORD_BOUND)
message(FATAL_ERROR "Pnetcdf must be built with relax-coord-bound enabled")
endif()
endif()

Expand Down
80 changes: 80 additions & 0 deletions cmake/modules/FindPNETCDF.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# FindPNETCDF
# -----------
#
# Find parallel IO library for classic netCDF files
#
# This module will define the following variables:
#
# ::
#
# PNETCDF_FOUND
# PNETCDF_INCLUDE_DIRS
# PNETCDF_LIBRARIES
# PNETCDF_VERSION
# PNETCDF_HAS_ERANGE_FILL - True if PnetCDF was built with ``NC_ERANGE`` support`
# PNETCDF_HAS_RELAXED_COORD_BOUND - True if PnetCDF was built with relaxed coordinate bounds
#
# The ``PNETCDF::pnetcdf`` target will also be exported

include(FindPackageHandleStandardArgs)

find_path(PNETCDF_INCLUDE_DIR NAMES pnetcdf.h)
find_library(PNETCDF_LIBRARY NAMES pnetcdf)

if (PNETCDF_INCLUDE_DIR)
set(pnetcdf_h "${PNETCDF_INCLUDE_DIR}/pnetcdf.h" )
message(DEBUG "PnetCDF include file ${pnetcdf_h} will be searched for version")
file(STRINGS "${pnetcdf_h}" pnetcdf_major_string REGEX "^#define PNETCDF_VERSION_MAJOR")
string(REGEX REPLACE "[^0-9]" "" pnetcdf_major "${pnetcdf_major_string}")
file(STRINGS "${pnetcdf_h}" pnetcdf_minor_string REGEX "^#define PNETCDF_VERSION_MINOR")
string(REGEX REPLACE "[^0-9]" "" pnetcdf_minor "${pnetcdf_minor_string}")
file(STRINGS "${pnetcdf_h}" pnetcdf_sub_string REGEX "^#define PNETCDF_VERSION_SUB")
string(REGEX REPLACE "[^0-9]" "" pnetcdf_sub "${pnetcdf_sub_string}")
set(pnetcdf_version "${pnetcdf_major}.${pnetcdf_minor}.${pnetcdf_sub}")
message(DEBUG "Found PnetCDF version ${pnetcdf_version}")
endif()

message(DEBUG "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ]"
" PNETCDF_ROOT = ${PNETCDF_ROOT}"
" PNETCDF_INCLUDE_DIR = ${PNETCDF_INCLUDE_DIR}"
" PNETCDF_LIBRARY = ${PNETCDF_LIBRARY}"
" PNETCDF_VERSION = ${PNETCDF_VERSION}"
)

file(STRINGS "${pnetcdf_h}" enable_erange_fill_pnetcdf REGEX "^#define PNETCDF_ERANGE_FILL")
string(REGEX REPLACE "[^0-9]" "" erange_fill_pnetcdf "${enable_erange_fill_pnetcdf}")
if("${erange_fill_pnetcdf}" STREQUAL "1")
set(PNETCDF_HAS_ERANGE_FILL ON CACHE BOOL "")
else()
set(PNETCDF_HAS_ERANGE_FILL OFF CACHE BOOL "")
endif()

mark_as_advanced(PNETCDF_INCLUDE_DIR PNETCDF_LIBRARY PNETCDF_ERANGE_FILL)

file(STRINGS "${pnetcdf_h}" relax_coord_bound_pnetcdf REGEX "^#define PNETCDF_RELAX_COORD_BOUND")
string(REGEX REPLACE "[^0-9]" "" relax_coord_bound "${relax_coord_bound_pnetcdf}")
if ("${relax_coord_bound}" STREQUAL "1")
set(PNETCDF_HAS_RELAXED_COORD_BOUND ON CACHE BOOL "")
else()
set(PNETCDF_HAS_RELAXED_COORD_BOUND OFF CACHE BOOL "")
endif()

find_package_handle_standard_args(PNETCDF
REQUIRED_VARS PNETCDF_LIBRARY PNETCDF_INCLUDE_DIR
VERSION_VAR PNETCDF_VERSION
)

if (PNETCDF_FOUND AND NOT TARGET PNETCDF::PNETCDF)
find_package(MPI REQUIRED)
add_library(PNETCDF::PNETCDF UNKNOWN IMPORTED)
set_target_properties(PNETCDF::PNETCDF PROPERTIES
IMPORTED_LINK_INTERFACE_LIBRARIES MPI::MPI_C
IMPORTED_LOCATION "${PNETCDF_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${PNETCDF_INCLUDE_DIR}"
)
find_library(MATH_LIBRARY m)
if (MATH_LIBRARY)
set_target_properties(PNETCDF::PNETCDF PROPERTIES
IMPORTED_LINK_INTERFACE_LIBRARIES ${MATH_LIBRARY})
endif()
endif()
3 changes: 0 additions & 3 deletions config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,6 @@ are set when opening a binary file on Windows. */
/* Define to 1 if you have the `mfhdf' library (-lmfhdf). */
#cmakedefine HAVE_LIBMFHDF 1

/* Define to 1 if you have the `pnetcdf' library (-lpnetcdf). */
#cmakedefine HAVE_LIBPNETCDF 1

/* Define to 1 if you have the libxml2 library. */
#cmakedefine ENABLE_LIBXML2 1

Expand Down
6 changes: 5 additions & 1 deletion libdispatch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ if(STATUS_PARALLEL)
target_link_libraries(dispatch PUBLIC MPI::MPI_C)
endif(STATUS_PARALLEL)

if(ENABLE_NCZARR)
if (ENABLE_PNETCDF)
target_link_libraries(dispatch PUBLIC PNETCDF::PNETCDF)
endif()

IF(ENABLE_NCZARR)
target_include_directories(dispatch PUBLIC ../libnczarr)
endif(ENABLE_NCZARR)

Expand Down
5 changes: 3 additions & 2 deletions liblib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ if(USE_HDF4)
set(TLL_LIBS ${HDF4_LIBRARIES} ${TLL_LIBS})
endif()

if(ENABLE_PNETCDF AND PNETCDF)
set(TLL_LIBS ${TLL_LIBS} ${PNETCDF})
if(ENABLE_PNETCDF)
SET(TLL_LIBS ${TLL_LIBS} PNETCDF::PNETCDF)
endif()


Expand Down Expand Up @@ -188,6 +188,7 @@ target_link_libraries(netcdf PRIVATE ${TLL_LIBS})
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${TLL_LIBS})
if(MSVC)
set_target_properties(netcdf PROPERTIES

LINK_FLAGS_DEBUG " /NODEFAULTLIB:MSVCRT"
)
endif()
Expand Down
1 change: 1 addition & 0 deletions libsrcp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# See netcdf-c/COPYRIGHT file for more info.

add_library(netcdfp OBJECT ${libsrcp_SOURCES})
target_link_libraries(netcdfp PUBLIC PNETCDF::PNETCDF)

target_sources(netcdfp PRIVATE ncpdispatch.c)

Expand Down
1 change: 1 addition & 0 deletions nc_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ TARGET_INCLUDE_DIRECTORIES(nc_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
TARGET_LINK_LIBRARIES(nc_test
netcdf
${HAVE_LIBM}
${ALL_TLL_LIBS}
)

##
Expand Down

0 comments on commit 50a3ce0

Please sign in to comment.