CMake netCDF Compatibility with WPS (#2121)
[WRF.git] / cmake / confcheck.cmake
blob5db8469519da039966c2c6227b14f71ead375275
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(
18                         WRF_CFG
19                         "${options}"  "${oneValueArgs}"  "${multiValueArgs}"
20                         ${ARGN}
21                         )
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 )
31   endif()
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" )
36     else()
37       set( WRF_CFG_FAIL_REGEX FAIL_REGEX ${WRF_CFG_FAIL_REGEX} )
38     endif()
39   endif()
41   if ( DEFINED WRF_CFG_EXTENSION )
42     set( WRF_CFG_EXTENSION SRC_EXT ${WRF_CFG_EXTENSION} )
43   endif()
45   # Additional options
46   if ( DEFINED WRF_CFG_QUIET AND ${WRF_CFG_QUIET} )
47     set( CMAKE_REQUIRED_QUIET ${WRF_CFG_QUIET} )
48   endif()
50   if ( DEFINED WRF_CFG_ADDITIONAL_FLAGS )
51     set( CMAKE_REQUIRED_FLAGS ${WRF_CFG_ADDITIONAL_FLAGS} )
52   endif()
54   if ( DEFINED WRF_CFG_ADDITIONAL_DEFINITIONS )
55     set( CMAKE_REQUIRED_DEFINITIONS ${WRF_CFG_ADDITIONAL_DEFINITIONS} )
56   endif()
58   if ( DEFINED WRF_CFG_ADDITIONAL_INCLUDES )
59     set( CMAKE_REQUIRED_INCLUDES ${WRF_CFG_ADDITIONAL_INCLUDES} )
60   endif()
62   if ( DEFINED WRF_CFG_ADDITIONAL_LINK_OPTIONS )
63     set( CMAKE_REQUIRED_LINK_OPTIONS ${WRF_CFG_ADDITIONAL_LINK_OPTIONS} )
64   endif()
66   if ( DEFINED WRF_CFG_ADDITIONAL_LIBRARIES )
67     set( CMAKE_REQUIRED_LIBRARIES ${WRF_CFG_ADDITIONAL_LIBRARIES} )
68   endif()
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(
74                                 "${WRF_CFG_CODE}"
75                                 ${WRF_CFG_RESULT_VAR}
76                                 ${WRF_CFG_FAIL_REGEX}
77                                 ${WRF_CFG_EXTENSION}
78                                 )
79     elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "c" )
80       check_c_source_runs(
81                           "${WRF_CFG_CODE}"
82                           ${WRF_CFG_RESULT_VAR}
83                           ${WRF_CFG_FAIL_REGEX}
84                           ${WRF_CFG_EXTENSION}
85                           )
86     elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "cpp" )
87       check_cpp_source_runs(
88                             "${WRF_CFG_CODE}"
89                             ${WRF_CFG_RESULT_VAR}
90                             ${WRF_CFG_FAIL_REGEX}
91                             ${WRF_CFG_EXTENSION}
92                             )
93     endif()
94   else()
95     if ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "fortran" )
96       check_fortran_source_compiles(
97                                     "${WRF_CFG_CODE}"
98                                     ${WRF_CFG_RESULT_VAR}
99                                     ${WRF_CFG_EXTENSION}
100                                     )
101     elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "c" )
102       check_c_source_compiles(
103                               "${WRF_CFG_CODE}"
104                               ${WRF_CFG_RESULT_VAR}
105                               ${WRF_CFG_EXTENSION}
106                               )
107     elseif ( ${WRF_CFG_SOURCE_TYPE} STREQUAL "cpp" )
108       check_cpp_source_compiles(
109                                 "${WRF_CFG_CODE}"
110                                 ${WRF_CFG_RESULT_VAR}
111                                 ${WRF_CFG_EXTENSION}
112                                 )
113     endif()
114   endif()
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 )
122     endif()
123     
124     if ( DEFINED WRF_CFG_MESSAGE )
125       message( ${WRF_CFG_MSG_TYPE} "${WRF_CFG_MESSAGE}" )
126     else()
127       message( ${WRF_CFG_MSG_TYPE} "${WRF_CFG_RESULT_VAR} marked as required, check failed" )
128     endif()
129   endif()
131 endmacro()