Skip to content

Commit

Permalink
Merge branch 'develop' into cmake-kpp
Browse files Browse the repository at this point in the history
  • Loading branch information
islas authored Dec 21, 2024
2 parents 80b44a8 + b6542b0 commit 7adbe6e
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 138 deletions.
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
cmake_minimum_required( VERSION 3.20 )
cmake_policy( SET CMP0118 NEW )
cmake_policy( SET CMP0098 NEW )
cmake_minimum_required( VERSION 3.19 )

project( WRF )

enable_language( C )
enable_language( CXX )
enable_language( Fortran )

project( WRF )
set( EXPORT_NAME ${PROJECT_NAME} )

if ( DEFINED CMAKE_TOOLCHAIN_FILE )
Expand All @@ -15,6 +14,11 @@ if ( DEFINED CMAKE_TOOLCHAIN_FILE )
# include( ${WRF_CONFIG} )
endif()

# Import default flags now, get rid of any imported release flag
# we will handle that ourselves with WRF_FCOPTIM/WRF_FCNOOPT
set( CMAKE_Fortran_FLAGS_RELEASE "" CACHE STRING "" FORCE )
set( CMAKE_C_FLAGS_RELEASE "" CACHE STRING "" FORCE )

# list( APPEND CMAKE_MODULE_PATH )
list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/ ${PROJECT_SOURCE_DIR}/cmake/modules )

Expand Down
2 changes: 2 additions & 0 deletions arch/configure_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
referenceVar = re.compile( r"[$]([(])?(\w+)(?(1)[)])", re.I )
compileObject = re.compile( r"(\W|^)-c(\W|$)" )
configureRepl = re.compile( r"(\W|^)CONFIGURE_\w+(\W|$)" )
defineRepl = re.compile( r"-D([^ ]+)" )

class Stanza():

Expand Down Expand Up @@ -164,6 +165,7 @@ def sanitize( self ) :
for keyToSan in self.kvPairs_.keys() :
self.kvPairs_[ keyToSan ] = configureRepl.sub( r"\1\2", self.kvPairs_[ keyToSan ] ).strip()
self.kvPairs_[ keyToSan ] = compileObject.sub( r"\1\2", self.kvPairs_[ keyToSan ] ).strip()
self.kvPairs_[ keyToSan ] = defineRepl.sub( r"\1", self.kvPairs_[ keyToSan ] ).strip()


# Now fix certain ones that are mixing programs with flags all mashed into one option
Expand Down
8 changes: 1 addition & 7 deletions cmake/c_preproc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,13 @@ macro( wrf_c_preproc_fortran )
# # It keeps getting better lol
# # https://gitlab.kitware.com/cmake/cmake/-/issues/18399
# # We could use cmake 3.20+ and CMP0118, but this allows usage from 3.18.6+
# TL;DR - This doesn't work despite all documentation stating otherwise, need to use CMP0118
# set_source_files_properties(
# ${WRF_PP_F_OUTPUT_FILE}
# ${WRF_PP_F_TARGET_DIRECTORY}
# PROPERTIES
# GENERATED TRUE
# )
set_source_files_properties(
${WRF_PP_F_OUTPUT_FILE}
DIRECTORY ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${WRF_PP_F_TARGET_DIRECTORY}
PROPERTIES
Fortran_PREPROCESS OFF
GENERATED TRUE
)
# message( STATUS "File ${WRF_PP_F_SOURCE_FILE} will be preprocessed into ${WRF_PP_F_OUTPUT_FILE}" )

Expand Down
133 changes: 38 additions & 95 deletions cmake/confcheck.cmake
Original file line number Diff line number Diff line change
@@ -1,121 +1,60 @@
# WRF Macro for adding configuration checks from source file, default is fortran
# https://cmake.org/cmake/help/latest/module/CheckFortranSourceCompiles.html
# https://github.com/ufs-community/ufs-weather-model/issues/132
include( CheckFortranSourceRuns )
include( CheckFortranSourceCompiles )
include( CheckCSourceRuns )
include( CheckCSourceCompiles )
include( CheckCXXSourceRuns )
include( CheckCXXSourceCompiles )
# include( CheckFortranSourceRuns )
# include( CheckFortranSourceCompiles )
# include( CheckCSourceRuns )
# include( CheckCSourceCompiles )
# include( CheckCXXSourceRuns )
# include( CheckCXXSourceCompiles )

macro( wrf_conf_check )
function( wrf_conf_check )

set( options QUIET RUN REQUIRED )
set( oneValueArgs RESULT_VAR EXTENSION FAIL_REGEX SOURCE MESSAGE SOURCE_TYPE )
set( multiValueArgs ADDITIONAL_FLAGS ADDITIONAL_DEFINITIONS ADDITIONAL_INCLUDES ADDITIONAL_LINK_OPTIONS ADDITIONAL_LIBRARIES )
set( oneValueArgs RESULT_VAR MESSAGE )
set( multiValueArgs SOURCES OPTIONS )

cmake_parse_arguments(
WRF_CFG
"${options}" "${oneValueArgs}" "${multiValueArgs}"
${ARGN}
)

get_filename_component( WRF_CFG_SOURCE_FILE ${WRF_CFG_SOURCE} REALPATH )
file( READ ${WRF_CFG_SOURCE_FILE} WRF_CFG_CODE )

# Santize for newlines
string( REPLACE "\\n" "\\\\n" WRF_CFG_CODE "${WRF_CFG_CODE}" )

if ( NOT DEFINED WRF_CFG_SOURCE_TYPE )
set( WRF_CFG_SOURCE_TYPE fortran )
endif()

if ( DEFINED WRF_CFG_FAIL_REGEX )
if ( DEFINED WRF_CFG_RUN )
message( WARNING "wrf_conf_check: FAIL_REGEX ignored when running check" )
else()
set( WRF_CFG_FAIL_REGEX FAIL_REGEX ${WRF_CFG_FAIL_REGEX} )
endif()
if ( NOT DEFINED WRF_CFG_BINDIR )
set( WRF_CFG_BINDIR ${CMAKE_CURRENT_BINARY_DIR}/confcheck/${WRF_CFG_RESULT_VAR}/ )
endif()

if ( DEFINED WRF_CFG_EXTENSION )
set( WRF_CFG_EXTENSION SRC_EXT ${WRF_CFG_EXTENSION} )
endif()

# Additional options
if ( DEFINED WRF_CFG_QUIET AND ${WRF_CFG_QUIET} )
set( CMAKE_REQUIRED_QUIET ${WRF_CFG_QUIET} )
endif()
message( STATUS "Performing Check ${WRF_CFG_RESULT_VAR}" )

if ( DEFINED WRF_CFG_ADDITIONAL_FLAGS )
set( CMAKE_REQUIRED_FLAGS ${WRF_CFG_ADDITIONAL_FLAGS} )
endif()

if ( DEFINED WRF_CFG_ADDITIONAL_DEFINITIONS )
set( CMAKE_REQUIRED_DEFINITIONS ${WRF_CFG_ADDITIONAL_DEFINITIONS} )
endif()

if ( DEFINED WRF_CFG_ADDITIONAL_INCLUDES )
set( CMAKE_REQUIRED_INCLUDES ${WRF_CFG_ADDITIONAL_INCLUDES} )
endif()

if ( DEFINED WRF_CFG_ADDITIONAL_LINK_OPTIONS )
set( CMAKE_REQUIRED_LINK_OPTIONS ${WRF_CFG_ADDITIONAL_LINK_OPTIONS} )
endif()

if ( DEFINED WRF_CFG_ADDITIONAL_LIBRARIES )
set( CMAKE_REQUIRED_LIBRARIES ${WRF_CFG_ADDITIONAL_LIBRARIES} )
endif()

string( TOLOWER "${WRF_CFG_SOURCE_TYPE}" WRF_CFG_SOURCE_TYPE )
if ( DEFINED WRF_CFG_RUN )
if ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "fortran" )
check_fortran_source_runs(
"${WRF_CFG_CODE}"
${WRF_CFG_RESULT_VAR}
${WRF_CFG_FAIL_REGEX}
${WRF_CFG_EXTENSION}
)
elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "c" )
check_c_source_runs(
"${WRF_CFG_CODE}"
${WRF_CFG_RESULT_VAR}
${WRF_CFG_FAIL_REGEX}
${WRF_CFG_EXTENSION}
)
elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "cpp" )
check_cpp_source_runs(
"${WRF_CFG_CODE}"
${WRF_CFG_RESULT_VAR}
${WRF_CFG_FAIL_REGEX}
${WRF_CFG_EXTENSION}
)
try_run(
${WRF_CFG_RESULT_VAR}
WRF_CFG_COMPILE_RESULT_VAR
${WRF_CFG_BINDIR}
${WRF_CFG_SOURCES}
${WRF_CFG_OPTIONS}
)
if ( ${WRF_CFG_COMPILE_RESULT_VAR} )
# Did it run successfully
if ( ${${WRF_CFG_RESULT_VAR}} EQUAL 0 )
set( ${WRF_CFG_RESULT_VAR} TRUE )
endif()
else()
set( ${WRF_CFG_RESULT_VAR} FALSE )
endif()
else()
if ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "fortran" )
check_fortran_source_compiles(
"${WRF_CFG_CODE}"
${WRF_CFG_RESULT_VAR}
${WRF_CFG_EXTENSION}
)
elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "c" )
check_c_source_compiles(
"${WRF_CFG_CODE}"
${WRF_CFG_RESULT_VAR}
${WRF_CFG_EXTENSION}
)
elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "cpp" )
check_cpp_source_compiles(
"${WRF_CFG_CODE}"
${WRF_CFG_RESULT_VAR}
${WRF_CFG_EXTENSION}
)
endif()
try_compile(
${WRF_CFG_RESULT_VAR}
${WRF_CFG_BINDIR}
SOURCES ${WRF_CFG_SOURCES}
${WRF_CFG_OPTIONS}
)
endif()

# If it failed - note that since this is a run/compile test we expect pass/true
# to just proceed as normal, but if failure we should do something about it
if ( NOT ( DEFINED ${WRF_CFG_RESULT_VAR} AND "${${WRF_CFG_RESULT_VAR}}" ) )
message( STATUS "Performing Check ${WRF_CFG_RESULT_VAR} - Failure" )
set( WRF_CFG_MSG_TYPE STATUS )
if ( DEFINED WRF_CFG_REQUIRED AND ${WRF_CFG_REQUIRED} )
set( WRF_CFG_MSG_TYPE FATAL_ERROR )
Expand All @@ -126,8 +65,12 @@ macro( wrf_conf_check )
else()
message( ${WRF_CFG_MSG_TYPE} "${WRF_CFG_RESULT_VAR} marked as required, check failed" )
endif()
else()
message( STATUS "Performing Check ${WRF_CFG_RESULT_VAR} - Success" )
endif()

endmacro()
set( ${WRF_CFG_RESULT_VAR} ${${WRF_CFG_RESULT_VAR}} PARENT_SCOPE )

endfunction()


8 changes: 1 addition & 7 deletions cmake/m4_preproc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,13 @@ macro( wrf_m4_preproc_fortran )
# # It keeps getting better lol
# # https://gitlab.kitware.com/cmake/cmake/-/issues/18399
# # We could use cmake 3.20+ and CMP0118, but this allows usage from 3.18.6+
# TL;DR - This doesn't work despite all documentation stating otherwise, need to use CMP0118
# set_source_files_properties(
# ${WRF_PP_M4_OUTPUT_FILE}
# ${WRF_PP_M4_TARGET_DIRECTORY}
# PROPERTIES
# GENERATED TRUE
# )
set_source_files_properties(
${WRF_PP_M4_OUTPUT_FILE}
DIRECTORY ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${WRF_PP_M4_TARGET_DIRECTORY}
PROPERTIES
Fortran_PREPROCESS OFF
GENERATED TRUE
)
# message( STATUS "File ${WRF_PP_M4_SOURCE_FILE} will be preprocessed into ${WRF_PP_M4_OUTPUT_FILE}" )

Expand Down
4 changes: 2 additions & 2 deletions cmake/modules/FindnetCDF.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ else()

foreach( NC_QUERY ${netCDF_QUERY_YES_OPTIONS} )
execute_process( COMMAND ${NETCDF_PROGRAM} --has-${NC_QUERY} OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE netCDF_${NC_QUERY}_LOWERCASE )
if ( NOT "${netCDF-Fortran_${NF_QUERY}_LOWERCASE}" )
if ( NOT "${netCDF_${NC_QUERY}_LOWERCASE}" )
# might be empty
set( netCDF-Fortran_${NF_QUERY}_LOWERCASE no )
set( netCDF_${NC_QUERY}_LOWERCASE no )
endif()
string( TOUPPER ${NC_QUERY} NC_QUERY_UPPERCASE )
string( TOUPPER ${netCDF_${NC_QUERY}_LOWERCASE} NC_ANSWER_UPPERCASE )
Expand Down
4 changes: 4 additions & 0 deletions cmake/target_source_properties.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ function( define_target_source_properties )
SOURCE
PROPERTY ${PROPERTY}
# INHERITED # they will be "inherited" via target to source
BRIEF_DOCS "Transitive property from target to file"
FULL_DOCS "This property can override an existing property derived from a target"
)

define_property(
TARGET
PROPERTY ${PROPERTY}
# INHERITED # they will be "inherited" via target to source
BRIEF_DOCS "Transitive property from target to file"
FULL_DOCS "This property can be overridden on a per file basis"
)
endforeach()
endfunction()
Expand Down
8 changes: 4 additions & 4 deletions cmake/template/arch_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ set( CMAKE_Fortran_FLAGS_INIT "{SFC_FLAGS} {FCBASEOPTS} {BYTESWAPIO}" )
set( CMAKE_C_FLAGS_INIT "{SCC_FLAGS} {CFLAGS_LOCAL}" )

# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_CONFIG_INIT.html
set( CMAKE_Fortran_FLAGS_Debug_INIT "{FCDEBUG}" )
set( CMAKE_Fortran_FLAGS_Release_INIT "" )
set( CMAKE_C_FLAGS_Debug_INIT "" )
set( CMAKE_C_FLAGS_Release_INIT "" )
set( CMAKE_Fortran_FLAGS_DEBUG_INIT "{FCDEBUG}" )
set( CMAKE_Fortran_FLAGS_RELEASE_INIT "" )
set( CMAKE_C_FLAGS_DEBUG_INIT "" )
set( CMAKE_C_FLAGS_RELEASE_INIT "" )

# Project specifics now
set( WRF_MPI_Fortran_FLAGS "{DM_FC_FLAGS}" )
Expand Down
29 changes: 11 additions & 18 deletions confcheck/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,58 @@
wrf_conf_check(
RUN
RESULT_VAR Fortran_2003_IEEE
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2003_ieee_test.F
EXTENSION .F
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2003_ieee_test.F
MESSAGE "Some IEEE Fortran 2003 features missing, removing usage of these features"
)

wrf_conf_check(
RUN
RESULT_VAR Fortran_2003_ISO_C
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2003_iso_c_test.F
EXTENSION .F
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2003_iso_c_test.F
MESSAGE "Some ISO_C Fortran 2003 features missing, removing usage ISO_C and stubbing code dependent on it"
)

wrf_conf_check(
RUN
RESULT_VAR Fortran_2003_FLUSH
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2003_flush_test.F
EXTENSION .F
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2003_flush_test.F
MESSAGE "Standard FLUSH routine Fortran 2003 features missing, checking for alternate Fortran_2003_FFLUSH"
)

if ( NOT ${Fortran_2003_FLUSH} )
wrf_conf_check(
RUN
RESULT_VAR Fortran_2003_FFLUSH
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2003_fflush_test.F
EXTENSION .F
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2003_fflush_test.F
MESSAGE "Standard FFLUSH routine Fortran 2003 features missing, no alternate to FLUSH found, feature stubbed out"
)
endif()

wrf_conf_check(
RUN
RESULT_VAR Fortran_2003_GAMMA
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2008_gamma_test.F
EXTENSION .F
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2008_gamma_test.F
MESSAGE "Some Fortran 2003 features missing, removing usage gamma function intrinsic and stubbing code dependent on it"
)



wrf_conf_check(
RUN
SOURCE_TYPE C
RESULT_VAR FSEEKO64
SOURCE ${PROJECT_SOURCE_DIR}/tools/fseek_test.c
EXTENSION .c
ADDITIONAL_DEFINITIONS -DTEST_FSEEKO64 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -DFILE_TO_TEST="${PROJECT_SOURCE_DIR}/CMakeLists.txt"
SOURCES ${PROJECT_SOURCE_DIR}/tools/fseek_test.c
OPTIONS
COMPILE_DEFINITIONS -DTEST_FSEEKO64 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -DFILE_TO_TEST="${PROJECT_SOURCE_DIR}/CMakeLists.txt"
MESSAGE "fseeko64 not supported, checking alternate fseeko"
)

if ( NOT "${FSEEKO64}" )
wrf_conf_check(
RUN
SOURCE_TYPE C
RESULT_VAR FSEEKO
SOURCE ${PROJECT_SOURCE_DIR}/tools/fseek_test.c
EXTENSION .c
ADDITIONAL_DEFINITIONS -DTEST_FSEEKO -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -DFILE_TO_TEST="${PROJECT_SOURCE_DIR}/CMakeLists.txt"
SOURCES ${PROJECT_SOURCE_DIR}/tools/fseek_test.c
OPTIONS
COMPILE_DEFINITINOS -DTEST_FSEEKO -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -DFILE_TO_TEST="${PROJECT_SOURCE_DIR}/CMakeLists.txt"
MESSAGE "fseeko not supported, compiling with fseek (caution with large files)"
)
endif()
Expand Down
5 changes: 4 additions & 1 deletion frame/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ endif()
target_sources(
${PROJECT_NAME}_Core
PRIVATE
${WRF_INCLUDE_FILES}
module_internal_header_util.F


module_configure.F
module_driver_constants.F
Expand Down Expand Up @@ -155,6 +156,8 @@ target_sources(
# Disable optimizations on these files always
set_source_files_properties(
${nl_dyn_source}
module_comm_nesting_dm.F
DIRECTORY ${PROJECT_SOURCE_DIR}
PROPERTIES
COMPILE_OPTIONS_OPTIMIZATION
$<$<COMPILE_LANGUAGE:Fortran>:${WRF_FCNOOPT}>
Expand Down
Loading

0 comments on commit 7adbe6e

Please sign in to comment.