1 # WRF Macro for adding configuration checks from source file, default is fortran
2 # https://cmake.org/cmake/help/latest/module/CheckFortranSourceCompiles.html
3 # https://github.com/ufs-community/ufs-weather-model/issues/132
4 include( CheckFortranSourceRuns )
5 include( CheckFortranSourceCompiles )
6 include( CheckCSourceRuns )
7 include( CheckCSourceCompiles )
8 include( CheckCXXSourceRuns )
9 include( CheckCXXSourceCompiles )
11 macro( wrf_conf_check )
13 set( options QUIET RUN REQUIRED )
14 set( oneValueArgs RESULT_VAR EXTENSION FAIL_REGEX SOURCE MESSAGE SOURCE_TYPE )
15 set( multiValueArgs ADDITIONAL_FLAGS ADDITIONAL_DEFINITIONS ADDITIONAL_INCLUDES ADDITIONAL_LINK_OPTIONS ADDITIONAL_LIBRARIES )
17 cmake_parse_arguments(
19 "${options}" "${oneValueArgs}" "${multiValueArgs}"
23 get_filename_component( WRF_CFG_SOURCE_FILE ${WRF_CFG_SOURCE} REALPATH )
24 file( READ ${WRF_CFG_SOURCE_FILE} WRF_CFG_CODE )
26 # Santize for newlines
27 string( REPLACE "\\n" "\\\\n" WRF_CFG_CODE "${WRF_CFG_CODE}" )
29 if ( NOT DEFINED WRF_CFG_SOURCE_TYPE )
30 set( WRF_CFG_SOURCE_TYPE fortran )
33 if ( DEFINED WRF_CFG_FAIL_REGEX )
34 if ( DEFINED WRF_CFG_RUN )
35 message( WARNING "wrf_conf_check: FAIL_REGEX ignored when running check" )
37 set( WRF_CFG_FAIL_REGEX FAIL_REGEX ${WRF_CFG_FAIL_REGEX} )
41 if ( DEFINED WRF_CFG_EXTENSION )
42 set( WRF_CFG_EXTENSION SRC_EXT ${WRF_CFG_EXTENSION} )
46 if ( DEFINED WRF_CFG_QUIET AND ${WRF_CFG_QUIET} )
47 set( CMAKE_REQUIRED_QUIET ${WRF_CFG_QUIET} )
50 if ( DEFINED WRF_CFG_ADDITIONAL_FLAGS )
51 set( CMAKE_REQUIRED_FLAGS ${WRF_CFG_ADDITIONAL_FLAGS} )
54 if ( DEFINED WRF_CFG_ADDITIONAL_DEFINITIONS )
55 set( CMAKE_REQUIRED_DEFINITIONS ${WRF_CFG_ADDITIONAL_DEFINITIONS} )
58 if ( DEFINED WRF_CFG_ADDITIONAL_INCLUDES )
59 set( CMAKE_REQUIRED_INCLUDES ${WRF_CFG_ADDITIONAL_INCLUDES} )
62 if ( DEFINED WRF_CFG_ADDITIONAL_LINK_OPTIONS )
63 set( CMAKE_REQUIRED_LINK_OPTIONS ${WRF_CFG_ADDITIONAL_LINK_OPTIONS} )
66 if ( DEFINED WRF_CFG_ADDITIONAL_LIBRARIES )
67 set( CMAKE_REQUIRED_LIBRARIES ${WRF_CFG_ADDITIONAL_LIBRARIES} )
70 string( TOLOWER "${WRF_CFG_SOURCE_TYPE}" WRF_CFG_SOURCE_TYPE )
71 if ( DEFINED WRF_CFG_RUN )
72 if ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "fortran" )
73 check_fortran_source_runs(
79 elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "c" )
86 elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "cpp" )
87 check_cpp_source_runs(
95 if ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "fortran" )
96 check_fortran_source_compiles(
101 elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "c" )
102 check_c_source_compiles(
104 ${WRF_CFG_RESULT_VAR}
107 elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "cpp" )
108 check_cpp_source_compiles(
110 ${WRF_CFG_RESULT_VAR}
116 # If it failed - note that since this is a run/compile test we expect pass/true
117 # to just proceed as normal, but if failure we should do something about it
118 if ( NOT ( DEFINED ${WRF_CFG_RESULT_VAR} AND "${${WRF_CFG_RESULT_VAR}}" ) )
119 set( WRF_CFG_MSG_TYPE STATUS )
120 if ( DEFINED WRF_CFG_REQUIRED AND ${WRF_CFG_REQUIRED} )
121 set( WRF_CFG_MSG_TYPE FATAL_ERROR )
124 if ( DEFINED WRF_CFG_MESSAGE )
125 message( ${WRF_CFG_MSG_TYPE} "${WRF_CFG_MESSAGE}" )
127 message( ${WRF_CFG_MSG_TYPE} "${WRF_CFG_RESULT_VAR} marked as required, check failed" )