Skip to content

Commit 122ad7a

Browse files
committed
Added cmake modules for NEtCDF and ESMF
1 parent 46738d9 commit 122ad7a

2 files changed

Lines changed: 305 additions & 0 deletions

File tree

cmake/FindESMF.cmake

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# - Try to find ESMF
2+
#
3+
# Uses ESMFMKFILE to find the filepath of esmf.mk. If this is NOT set, then this
4+
# module will attempt to find esmf.mk. If ESMFMKFILE exists, then
5+
# ESMF_FOUND=TRUE and all ESMF makefile variables will be set in the global
6+
# scope. Optionally, set ESMF_MKGLOBALS to a string list to filter makefile
7+
# variables. For example, to globally scope only ESMF_LIBSDIR and ESMF_APPSDIR
8+
# variables, use this CMake command in CMakeLists.txt:
9+
#
10+
# set(ESMF_MKGLOBALS "LIBSDIR" "APPSDIR")
11+
12+
# Set ESMFMKFILE as defined by system env variable. If it's not explicitly set
13+
# try to find esmf.mk file in default locations (ESMF_ROOT, CMAKE_PREFIX_PATH,
14+
# etc)
15+
16+
# - Common Usage
17+
#
18+
# Where to look for this FindESMF.cmake file
19+
# list(APPEND CMAKE_MODULE_PATH "<PATH_TO_THIS_FILE>")
20+
# <PATH_TO_THIS_FILE> is to be replaced with the directory for this file
21+
#
22+
# How to locate ESMF libraries and create target
23+
# find_package(ESMF <X.Y.Z> MODULE REQUIRED)
24+
# <X.Y.Z> is to be replaced with the minimum version required
25+
#
26+
# How to link targets
27+
# target_link_libraries(<CMAKE_TARGET> PUBLIC ESMF::ESMF)
28+
# <CMAKE_TARGET> is to be replaced with your CMake target
29+
30+
if(NOT DEFINED ESMFMKFILE)
31+
if(NOT DEFINED ENV{ESMFMKFILE})
32+
find_path(ESMFMKFILE_PATH esmf.mk PATH_SUFFIXES lib lib64)
33+
if(ESMFMKFILE_PATH)
34+
set(ESMFMKFILE ${ESMFMKFILE_PATH}/esmf.mk)
35+
message(STATUS "Found esmf.mk file ${ESMFMKFILE}")
36+
endif()
37+
else()
38+
set(ESMFMKFILE $ENV{ESMFMKFILE})
39+
endif()
40+
endif()
41+
42+
# Only parse the mk file if it is found
43+
if(EXISTS ${ESMFMKFILE})
44+
set(ESMFMKFILE ${ESMFMKFILE} CACHE FILEPATH "Path to esmf.mk file")
45+
set(ESMF_FOUND TRUE CACHE BOOL "esmf.mk file found" FORCE)
46+
47+
# Read the mk file
48+
file(STRINGS "${ESMFMKFILE}" esmfmkfile_contents)
49+
# Parse each line in the mk file
50+
foreach(str ${esmfmkfile_contents})
51+
# Only consider uncommented lines
52+
string(REGEX MATCH "^[^#]" def ${str})
53+
# Line is not commented
54+
if(def)
55+
# Extract the variable name
56+
string(REGEX MATCH "^[^=]+" esmf_varname ${str})
57+
# Extract the variable's value
58+
string(REGEX MATCH "=.+$" esmf_vardef ${str})
59+
# Only for variables with a defined value
60+
if(esmf_vardef)
61+
# Get rid of the assignment string
62+
string(SUBSTRING ${esmf_vardef} 1 -1 esmf_vardef)
63+
# Remove whitespace
64+
string(STRIP ${esmf_vardef} esmf_vardef)
65+
# A string or single-valued list
66+
if(NOT DEFINED ESMF_MKGLOBALS)
67+
# Set in global scope
68+
set(${esmf_varname} ${esmf_vardef})
69+
# Don't display by default in GUI
70+
mark_as_advanced(esmf_varname)
71+
else() # Need to filter global promotion
72+
foreach(m ${ESMF_MKGLOBALS})
73+
string(FIND ${esmf_varname} ${m} match)
74+
# Found the string
75+
if(NOT ${match} EQUAL -1)
76+
# Promote to global scope
77+
set(${esmf_varname} ${esmf_vardef})
78+
# Don't display by default in the GUI
79+
mark_as_advanced(esmf_varname)
80+
# No need to search for the current string filter
81+
break()
82+
endif()
83+
endforeach()
84+
endif()
85+
endif()
86+
endif()
87+
endforeach()
88+
89+
# Construct ESMF_VERSION from ESMF_VERSION_STRING_GIT
90+
# ESMF_VERSION_MAJOR and ESMF_VERSION_MINOR are defined in ESMFMKFILE
91+
set(ESMF_VERSION 0)
92+
set(ESMF_VERSION_PATCH ${ESMF_VERSION_REVISION})
93+
set(ESMF_BETA_RELEASE FALSE)
94+
if(ESMF_VERSION_BETASNAPSHOT MATCHES "^('T')$")
95+
set(ESMF_BETA_RELEASE TRUE)
96+
if(ESMF_VERSION_STRING_GIT MATCHES "^ESMF.*beta_snapshot")
97+
set(ESMF_BETA_SNAPSHOT ${ESMF_VERSION_STRING_GIT})
98+
elseif(ESMF_VERSION_STRING_GIT MATCHES "^v.\..\..b")
99+
set(ESMF_BETA_SNAPSHOT ${ESMF_VERSION_STRING_GIT})
100+
else()
101+
set(ESMF_BETA_SNAPSHOT 0)
102+
endif()
103+
message(STATUS "Detected ESMF Beta snapshot: ${ESMF_BETA_SNAPSHOT}")
104+
endif()
105+
set(ESMF_VERSION "${ESMF_VERSION_MAJOR}.${ESMF_VERSION_MINOR}.${ESMF_VERSION_PATCH}")
106+
107+
# Find the ESMF library
108+
if(USE_ESMF_STATIC_LIBS)
109+
find_library(ESMF_LIBRARY_LOCATION NAMES libesmf.a PATHS ${ESMF_LIBSDIR} NO_DEFAULT_PATH)
110+
if(ESMF_LIBRARY_LOCATION MATCHES "ESMF_LIBRARY_LOCATION-NOTFOUND")
111+
message(WARNING "Static ESMF library (libesmf.a) not found in \
112+
${ESMF_LIBSDIR}. Try setting USE_ESMF_STATIC_LIBS=OFF")
113+
endif()
114+
if(NOT TARGET ESMF::ESMF)
115+
add_library(ESMF::ESMF STATIC IMPORTED)
116+
endif()
117+
else()
118+
find_library(ESMF_LIBRARY_LOCATION NAMES esmf PATHS ${ESMF_LIBSDIR} NO_DEFAULT_PATH)
119+
if(ESMF_LIBRARY_LOCATION MATCHES "ESMF_LIBRARY_LOCATION-NOTFOUND")
120+
message(WARNING "ESMF library not found in ${ESMF_LIBSDIR}.")
121+
endif()
122+
if(NOT TARGET ESMF::ESMF)
123+
add_library(ESMF::ESMF UNKNOWN IMPORTED)
124+
endif()
125+
endif()
126+
127+
# Add ESMF as an alias to ESMF::ESMF for backward compatibility
128+
if(NOT TARGET ESMF)
129+
add_library(ESMF ALIAS ESMF::ESMF)
130+
endif()
131+
132+
# Add ESMF include directories
133+
set(ESMF_INCLUDE_DIRECTORIES "")
134+
separate_arguments(_ESMF_F90COMPILEPATHS UNIX_COMMAND ${ESMF_F90COMPILEPATHS})
135+
foreach(_ITEM ${_ESMF_F90COMPILEPATHS})
136+
string(REGEX REPLACE "^-I" "" _ITEM "${_ITEM}")
137+
list(APPEND ESMF_INCLUDE_DIRECTORIES ${_ITEM})
138+
endforeach()
139+
140+
# Add ESMF link libraries
141+
string(STRIP "${ESMF_F90LINKRPATHS} ${ESMF_F90ESMFLINKRPATHS} ${ESMF_F90ESMFLINKPATHS} ${ESMF_F90LINKPATHS} ${ESMF_F90LINKLIBS} ${ESMF_F90LINKOPTS}" ESMF_INTERFACE_LINK_LIBRARIES)
142+
143+
# Finalize find_package
144+
include(FindPackageHandleStandardArgs)
145+
146+
find_package_handle_standard_args(
147+
${CMAKE_FIND_PACKAGE_NAME}
148+
REQUIRED_VARS ESMF_LIBRARY_LOCATION
149+
ESMF_INTERFACE_LINK_LIBRARIES
150+
ESMF_F90COMPILEPATHS
151+
VERSION_VAR ESMF_VERSION)
152+
153+
set_target_properties(ESMF::ESMF PROPERTIES
154+
IMPORTED_LOCATION "${ESMF_LIBRARY_LOCATION}"
155+
INTERFACE_INCLUDE_DIRECTORIES "${ESMF_INCLUDE_DIRECTORIES}"
156+
INTERFACE_LINK_LIBRARIES "${ESMF_INTERFACE_LINK_LIBRARIES}")
157+
158+
else()
159+
set(ESMF_FOUND FALSE CACHE BOOL "esmf.mk file NOT found" FORCE)
160+
message(WARNING "ESMFMKFILE ${ESMFMKFILE} not found. Try setting ESMFMKFILE \
161+
to esmf.mk location.")
162+
endif()

cmake/FindNetCDF.cmake

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# - Try to find NetCDF
2+
#
3+
# This can be controlled by setting the NetCDF_PATH (or, equivalently, the
4+
# NETCDF environment variable), or NetCDF_<lang>_PATH CMake variables, where
5+
# <lang> is the COMPONENT language one needs.
6+
#
7+
# Once done, this will define:
8+
#
9+
# NetCDF_<lang>_FOUND (BOOL) - system has NetCDF
10+
# NetCDF_<lang>_IS_SHARED (BOOL) - whether library is shared/dynamic
11+
# NetCDF_<lang>_INCLUDE_DIR (PATH) - Location of the C header file
12+
# NetCDF_<lang>_INCLUDE_DIRS (LIST) - the NetCDF include directories
13+
# NetCDF_<lang>_LIBRARY (FILE) - Path to the C library file
14+
# NetCDF_<lang>_LIBRARIES (LIST) - link these to use NetCDF
15+
#
16+
# The available COMPONENTS are: C Fortran
17+
# If no components are specified, it assumes only C
18+
include (LibFind)
19+
include (LibCheck)
20+
21+
# Define NetCDF C Component
22+
define_package_component (NetCDF DEFAULT
23+
COMPONENT C
24+
INCLUDE_NAMES netcdf.h
25+
LIBRARY_NAMES netcdf)
26+
27+
# Define NetCDF Fortran Component
28+
define_package_component (NetCDF
29+
COMPONENT Fortran
30+
INCLUDE_NAMES netcdf.mod netcdf.inc
31+
LIBRARY_NAMES netcdff)
32+
33+
# Search for list of valid components requested
34+
find_valid_components (NetCDF)
35+
36+
#==============================================================================
37+
# SEARCH FOR VALIDATED COMPONENTS
38+
foreach (NCDFcomp IN LISTS NetCDF_FIND_VALID_COMPONENTS)
39+
40+
# If not found already, search...
41+
if (NOT NetCDF_${NCDFcomp}_FOUND)
42+
43+
# Manually add the MPI include and library dirs to search paths
44+
# and search for the package component
45+
if (MPI_${NCDFcomp}_FOUND)
46+
initialize_paths (NetCDF_${NCDFcomp}_PATHS
47+
INCLUDE_DIRECTORIES ${MPI_${NCDFcomp}_INCLUDE_PATH}
48+
LIBRARIES ${MPI_${NCDFcomp}_LIBRARIES})
49+
find_package_component(NetCDF COMPONENT ${NCDFcomp}
50+
PATHS ${NetCDF_${NCDFcomp}_PATHS})
51+
else ()
52+
find_package_component(NetCDF COMPONENT ${NCDFcomp})
53+
endif ()
54+
55+
# Continue only if component found
56+
if (NetCDF_${NCDFcomp}_FOUND)
57+
58+
# Checks
59+
if (NCDFcomp STREQUAL C)
60+
61+
# Check version
62+
check_version (NetCDF
63+
NAME "netcdf_meta.h"
64+
HINTS ${NetCDF_C_INCLUDE_DIRS}
65+
MACRO_REGEX "NC_VERSION_")
66+
67+
# Check for parallel support
68+
check_macro (NetCDF_C_HAS_PARALLEL
69+
NAME TryNetCDF_PARALLEL.c
70+
HINTS ${CMAKE_MODULE_PATH}
71+
DEFINITIONS -I${NetCDF_C_INCLUDE_DIR}
72+
COMMENT "whether NetCDF has parallel support")
73+
74+
# Check if logging enabled
75+
set(CMAKE_REQUIRED_INCLUDES ${NetCDF_C_INCLUDE_DIR})
76+
set(CMAKE_REQUIRED_LIBRARIES ${NetCDF_C_LIBRARIES})
77+
CHECK_FUNCTION_EXISTS(nc_set_log_level NetCDF_C_LOGGING_ENABLED)
78+
79+
endif ()
80+
81+
# Dependencies
82+
if (NCDFcomp STREQUAL C AND NOT NetCDF_C_IS_SHARED)
83+
84+
# DEPENDENCY: PnetCDF (if PnetCDF enabled)
85+
check_macro (NetCDF_C_HAS_PNETCDF
86+
NAME TryNetCDF_PNETCDF.c
87+
HINTS ${CMAKE_MODULE_PATH}
88+
DEFINITIONS -I${NetCDF_C_INCLUDE_DIR}
89+
COMMENT "whether NetCDF has PnetCDF support")
90+
if (NetCDF_C_HAS_PNETCDF)
91+
find_package (PnetCDF COMPONENTS C)
92+
if (CURL_FOUND)
93+
list (APPEND NetCDF_C_INCLUDE_DIRS ${PnetCDF_C_INCLUDE_DIRS})
94+
list (APPEND NetCDF_C_LIBRARIES ${PnetCDF_C_LIBRARIES})
95+
endif ()
96+
endif ()
97+
98+
# DEPENDENCY: CURL (If DAP enabled)
99+
check_macro (NetCDF_C_HAS_DAP
100+
NAME TryNetCDF_DAP.c
101+
HINTS ${CMAKE_MODULE_PATH}
102+
DEFINITIONS -I${NetCDF_C_INCLUDE_DIR}
103+
COMMENT "whether NetCDF has DAP support")
104+
if (NetCDF_C_HAS_DAP)
105+
find_package (CURL)
106+
if (CURL_FOUND)
107+
list (APPEND NetCDF_C_INCLUDE_DIRS ${CURL_INCLUDE_DIRS})
108+
list (APPEND NetCDF_C_LIBRARIES ${CURL_LIBRARIES})
109+
endif ()
110+
endif ()
111+
112+
# DEPENDENCY: HDF5
113+
find_package (HDF5 COMPONENTS HL C)
114+
if (HDF5_C_FOUND)
115+
list (APPEND NetCDF_C_INCLUDE_DIRS ${HDF5_C_INCLUDE_DIRS}
116+
${HDF5_HL_INCLUDE_DIRS})
117+
list (APPEND NetCDF_C_LIBRARIES ${HDF5_C_LIBRARIES}
118+
${HDF5_HL_LIBRARIES})
119+
endif ()
120+
121+
# DEPENDENCY: LIBDL Math
122+
list (APPEND NetCDF_C_LIBRARIES -ldl -lm)
123+
124+
elseif (NCDFcomp STREQUAL Fortran AND NOT NetCDF_Fortran_IS_SHARED)
125+
126+
# DEPENDENCY: NetCDF
127+
set (orig_comp ${NCDFcomp})
128+
set (orig_comps ${NetCDF_FIND_VALID_COMPONENTS})
129+
find_package (NetCDF COMPONENTS C)
130+
set (NetCDF_FIND_VALID_COMPONENTS ${orig_comps})
131+
set (NCDFcomp ${orig_comp})
132+
if (NetCDF_C_FOUND)
133+
list (APPEND NetCDF_Fortran_INCLUDE_DIRS ${NetCDF_C_INCLUDE_DIRS})
134+
list (APPEND NetCDF_Fortran_LIBRARIES ${NetCDF_C_LIBRARIES})
135+
endif ()
136+
137+
endif ()
138+
139+
endif ()
140+
141+
endif ()
142+
143+
endforeach ()

0 commit comments

Comments
 (0)