Skip to content

Commit

Permalink
Improve error handling in FindESMF.cmake (#57)
Browse files Browse the repository at this point in the history
Better error handling in FindESMF.cmake
  • Loading branch information
DusanJovic-NOAA authored Sep 23, 2021
1 parent d39f199 commit cedeb2c
Showing 1 changed file with 50 additions and 45 deletions.
95 changes: 50 additions & 45 deletions Modules/FindESMF.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,38 @@


# Add the ESMFMKFILE path to the cache if defined as system env variable
if (DEFINED ENV{ESMFMKFILE} AND NOT DEFINED ESMFMKFILE)
if(DEFINED ENV{ESMFMKFILE} AND NOT DEFINED ESMFMKFILE)
set(ESMFMKFILE $ENV{ESMFMKFILE} CACHE FILEPATH "Path to ESMF mk file")
endif ()
endif()

# Found the mk file and ESMF exists on the system
if (EXISTS ${ESMFMKFILE})
set(ESMF_FOUND TRUE CACHE BOOL "ESMF mk file found" FORCE)
# Did not find the ESMF mk file
else()
set(ESMF_FOUND FALSE CACHE BOOL "ESMF mk file NOT found" FORCE)
# Best to warn users that without the mk file there is no way to find ESMF
if (NOT DEFINED ESMFMKFILE)
message(FATAL_ERROR "ESMFMKFILE not defined. This is the path to esmf.mk file. \
# If it's not explicitly set try to find esmf.mk file in default locations (ESMF_ROOT, CMAKE_PREFIX_PATH, etc)
if(NOT DEFINED ESMFMKFILE)
find_path(ESMFMKFILE_PATH esmf.mk PATH_SUFFIXES lib lib64)
if(ESMFMKFILE_PATH)
set(ESMFMKFILE ${ESMFMKFILE_PATH}/esmf.mk)
message(STATUS "Found esmf.mk file ${ESMFMKFILE}")
else()
message(STATUS "ESMFMKFILE not defined. This is the path to esmf.mk file. \
Without this filepath, ESMF_FOUND will always be FALSE.")
endif ()
endif()
endif()

# Only parse the mk file if it is found
if (ESMF_FOUND)
if(EXISTS ${ESMFMKFILE})
# Read the mk file
file(STRINGS "${ESMFMKFILE}" esmfmkfile_contents)
# Parse each line in the mk file
foreach(str ${esmfmkfile_contents})
# Only consider uncommented lines
string(REGEX MATCH "^[^#]" def ${str})
# Line is not commented
if (def)
if(def)
# Extract the variable name
string(REGEX MATCH "^[^=]+" esmf_varname ${str})
# Extract the variable's value
string(REGEX MATCH "=.+$" esmf_vardef ${str})
# Only for variables with a defined value
if (esmf_vardef)
if(esmf_vardef)
# Get rid of the assignment string
string(SUBSTRING ${esmf_vardef} 1 -1 esmf_vardef)
# Remove whitespace
Expand All @@ -62,7 +61,7 @@ if (ESMF_FOUND)
# Promote to global scope
set(${esmf_varname} ${esmf_vardef})
# Don't display by default in the GUI
mark_as_advanced (esmf_varname)
mark_as_advanced(esmf_varname)
# No need to search for the current string filter
break()
endif()
Expand All @@ -73,58 +72,64 @@ if (ESMF_FOUND)
endforeach()

# Construct ESMF_VERSION from ESMF_VERSION_STRING_GIT
if(ESMF_FOUND)
# ESMF_VERSION_MAJOR and ESMF_VERSION_MINOR are defined in ESMFMKFILE
set(ESMF_VERSION 0)
set(ESMF_VERSION_PATCH ${ESMF_VERSION_REVISION})
set(ESMF_BETA_RELEASE FALSE)
if(ESMF_VERSION_BETASNAPSHOT MATCHES "^('T')$")
set(ESMF_BETA_RELEASE TRUE)
string(REGEX REPLACE ".*beta_snapshot_*\([0-9]*\).*" "\\1" ESMF_BETA_SNAPSHOT "${ESMF_VERSION_STRING_GIT}")
endif()
set(ESMF_VERSION "${ESMF_VERSION_MAJOR}.${ESMF_VERSION_MINOR}.${ESMF_VERSION_PATCH}")
# ESMF_VERSION_MAJOR and ESMF_VERSION_MINOR are defined in ESMFMKFILE
set(ESMF_VERSION 0)
set(ESMF_VERSION_PATCH ${ESMF_VERSION_REVISION})
set(ESMF_BETA_RELEASE FALSE)
if(ESMF_VERSION_BETASNAPSHOT MATCHES "^('T')$")
set(ESMF_BETA_RELEASE TRUE)
string(REGEX REPLACE ".*beta_snapshot_*\([0-9]*\).*" "\\1" ESMF_BETA_SNAPSHOT "${ESMF_VERSION_STRING_GIT}")
message(STATUS "Detected ESMF Beta snapshot ${ESMF_BETA_SNAPSHOT}")
endif()
set(ESMF_VERSION "${ESMF_VERSION_MAJOR}.${ESMF_VERSION_MINOR}.${ESMF_VERSION_PATCH}")

separate_arguments(ESMF_F90COMPILEPATHS NATIVE_COMMAND ${ESMF_F90COMPILEPATHS})
foreach (ITEM ${ESMF_F90COMPILEPATHS})
foreach(ITEM ${ESMF_F90COMPILEPATHS})
string(REGEX REPLACE "^-I" "" ITEM "${ITEM}")
list(APPEND tmp ${ITEM})
endforeach()
set(ESMF_F90COMPILEPATHS ${tmp})

add_library(esmf UNKNOWN IMPORTED)
# Look for static library, if not found try dynamic library
find_library(esmf_lib NAMES libesmf.a PATHS ${ESMF_LIBSDIR})
if(esmf_lib MATCHES "esmf_lib-NOTFOUND")
unset(esmf_lib)
message(STATUS "Static ESMF library not found, searching for dynamic library instead")
find_library(esmf_lib NAMES esmf_fullylinked PATHS ${ESMF_LIBSDIR})
find_library(esmf_lib NAMES esmf_fullylinked libesmf.so PATHS ${ESMF_LIBSDIR})
if(esmf_lib MATCHES "esmf_lib-NOTFOUND")
message(FATAL_ERROR "Neither the dynamic nor the static ESMF library was found")
unset(esmf_lib)
message(STATUS "Neither the dynamic nor the static ESMF library was found")
else()
set(_library_type SHARED)
endif()
set(ESMF_INTERFACE_LINK_LIBRARIES "")
else()
# When linking the static library, also need the ESMF linker flags; strip any leading/trailing whitespaces
string(STRIP "${ESMF_F90ESMFLINKRPATHS} ${ESMF_F90ESMFLINKPATHS} ${ESMF_F90LINKPATHS} ${ESMF_F90LINKLIBS} ${ESMF_F90LINKOPTS}" ESMF_INTERFACE_LINK_LIBRARIES)
set(_library_type STATIC)
endif()

message(STATUS "Found ESMF library: ${esmf_lib}")
if(ESMF_BETA_RELEASE)
message(STATUS "Detected ESMF Beta snapshot ${ESMF_BETA_SNAPSHOT}")
endif()
string(STRIP "${ESMF_F90ESMFLINKRPATHS} ${ESMF_F90ESMFLINKPATHS} ${ESMF_F90LINKPATHS} ${ESMF_F90LINKLIBS} ${ESMF_F90LINKOPTS}" ESMF_INTERFACE_LINK_LIBRARIES)
set(ESMF_LIBRARY_LOCATION ${esmf_lib})

set_target_properties(esmf PROPERTIES
IMPORTED_LOCATION ${esmf_lib}
INTERFACE_INCLUDE_DIRECTORIES "${ESMF_F90COMPILEPATHS}"
INTERFACE_LINK_LIBRARIES "${ESMF_INTERFACE_LINK_LIBRARIES}")
else()

message(WARNING "ESMFMKFILE ${ESMFMKFILE} does not exist")

endif()

## Finalize find_package
include(FindPackageHandleStandardArgs)

find_package_handle_standard_args( ${CMAKE_FIND_PACKAGE_NAME}
REQUIRED_VARS ESMF_LIBSDIR
find_package_handle_standard_args(
${CMAKE_FIND_PACKAGE_NAME}
REQUIRED_VARS ESMF_LIBRARY_LOCATION
ESMF_INTERFACE_LINK_LIBRARIES
ESMF_F90COMPILEPATHS
VERSION_VAR ESMF_VERSION
HANDLE_COMPONENTS )
VERSION_VAR ESMF_VERSION)

## If ESMF is found create imported library target
if(ESMF_FOUND)
add_library(esmf ${_library_type} IMPORTED)
set_target_properties(esmf PROPERTIES
IMPORTED_LOCATION "${ESMF_LIBRARY_LOCATION}"
INTERFACE_INCLUDE_DIRECTORIES "${ESMF_F90COMPILEPATHS}"
INTERFACE_LINK_LIBRARIES "${ESMF_INTERFACE_LINK_LIBRARIES}")
endif()

0 comments on commit cedeb2c

Please sign in to comment.