Skip to content

Commit 5dd2c19

Browse files
authored
CMake confcheck switch to try_* functions (#2090)
TYPE: enhancement KEYWORDS: cmake, configuration SOURCE: internal DESCRIPTION OF CHANGES: Problem: The configuration checks done by the CMake build use language-specific calls between various functions. This has lead to a rather complex signature that is also limited in scope. Solution: Simplify the `wrf_conf_check` calls to make use of CMake's built-in `try_compile()` and `try_run()` functions, forwarding argument to those as necessary. Aside from minor adjustments to the `wrf_conf_check` calls this should appear as a drop-in replacement that provides the same results as before, i.e. no change to the system configuration options detected.
1 parent 75a6ff2 commit 5dd2c19

File tree

2 files changed

+49
-113
lines changed

2 files changed

+49
-113
lines changed

cmake/confcheck.cmake

Lines changed: 38 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,60 @@
11
# WRF Macro for adding configuration checks from source file, default is fortran
22
# https://cmake.org/cmake/help/latest/module/CheckFortranSourceCompiles.html
33
# 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 )
4+
# include( CheckFortranSourceRuns )
5+
# include( CheckFortranSourceCompiles )
6+
# include( CheckCSourceRuns )
7+
# include( CheckCSourceCompiles )
8+
# include( CheckCXXSourceRuns )
9+
# include( CheckCXXSourceCompiles )
1010

11-
macro( wrf_conf_check )
11+
function( wrf_conf_check )
1212

1313
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 )
14+
set( oneValueArgs RESULT_VAR MESSAGE )
15+
set( multiValueArgs SOURCES OPTIONS )
1616

1717
cmake_parse_arguments(
1818
WRF_CFG
1919
"${options}" "${oneValueArgs}" "${multiValueArgs}"
2020
${ARGN}
2121
)
2222

23-
get_filename_component( WRF_CFG_SOURCE_FILE ${WRF_CFG_SOURCE} REALPATH )
24-
file( READ ${WRF_CFG_SOURCE_FILE} WRF_CFG_CODE )
25-
26-
# Santize for newlines
27-
string( REPLACE "\\n" "\\\\n" WRF_CFG_CODE "${WRF_CFG_CODE}" )
28-
29-
if ( NOT DEFINED WRF_CFG_SOURCE_TYPE )
30-
set( WRF_CFG_SOURCE_TYPE fortran )
31-
endif()
32-
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()
23+
if ( NOT DEFINED WRF_CFG_BINDIR )
24+
set( WRF_CFG_BINDIR ${CMAKE_CURRENT_BINARY_DIR}/confcheck/${WRF_CFG_RESULT_VAR}/ )
3925
endif()
4026

41-
if ( DEFINED WRF_CFG_EXTENSION )
42-
set( WRF_CFG_EXTENSION SRC_EXT ${WRF_CFG_EXTENSION} )
43-
endif()
44-
45-
# Additional options
46-
if ( DEFINED WRF_CFG_QUIET AND ${WRF_CFG_QUIET} )
47-
set( CMAKE_REQUIRED_QUIET ${WRF_CFG_QUIET} )
48-
endif()
27+
message( STATUS "Performing Check ${WRF_CFG_RESULT_VAR}" )
4928

50-
if ( DEFINED WRF_CFG_ADDITIONAL_FLAGS )
51-
set( CMAKE_REQUIRED_FLAGS ${WRF_CFG_ADDITIONAL_FLAGS} )
52-
endif()
53-
54-
if ( DEFINED WRF_CFG_ADDITIONAL_DEFINITIONS )
55-
set( CMAKE_REQUIRED_DEFINITIONS ${WRF_CFG_ADDITIONAL_DEFINITIONS} )
56-
endif()
57-
58-
if ( DEFINED WRF_CFG_ADDITIONAL_INCLUDES )
59-
set( CMAKE_REQUIRED_INCLUDES ${WRF_CFG_ADDITIONAL_INCLUDES} )
60-
endif()
61-
62-
if ( DEFINED WRF_CFG_ADDITIONAL_LINK_OPTIONS )
63-
set( CMAKE_REQUIRED_LINK_OPTIONS ${WRF_CFG_ADDITIONAL_LINK_OPTIONS} )
64-
endif()
65-
66-
if ( DEFINED WRF_CFG_ADDITIONAL_LIBRARIES )
67-
set( CMAKE_REQUIRED_LIBRARIES ${WRF_CFG_ADDITIONAL_LIBRARIES} )
68-
endif()
69-
70-
string( TOLOWER "${WRF_CFG_SOURCE_TYPE}" WRF_CFG_SOURCE_TYPE )
7129
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-
)
30+
try_run(
31+
${WRF_CFG_RESULT_VAR}
32+
WRF_CFG_COMPILE_RESULT_VAR
33+
${WRF_CFG_BINDIR}
34+
${WRF_CFG_SOURCES}
35+
${WRF_CFG_OPTIONS}
36+
)
37+
if ( ${WRF_CFG_COMPILE_RESULT_VAR} )
38+
# Did it run successfully
39+
if ( ${${WRF_CFG_RESULT_VAR}} EQUAL 0 )
40+
set( ${WRF_CFG_RESULT_VAR} TRUE )
41+
endif()
42+
else()
43+
set( ${WRF_CFG_RESULT_VAR} FALSE )
9344
endif()
9445
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()
46+
try_compile(
47+
${WRF_CFG_RESULT_VAR}
48+
${WRF_CFG_BINDIR}
49+
SOURCES ${WRF_CFG_SOURCES}
50+
${WRF_CFG_OPTIONS}
51+
)
11452
endif()
11553

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

131-
endmacro()
72+
set( ${WRF_CFG_RESULT_VAR} ${${WRF_CFG_RESULT_VAR}} PARENT_SCOPE )
73+
74+
endfunction()
13275

13376

confcheck/CMakeLists.txt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,58 @@
22
wrf_conf_check(
33
RUN
44
RESULT_VAR Fortran_2003_IEEE
5-
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2003_ieee_test.F
6-
EXTENSION .F
5+
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2003_ieee_test.F
76
MESSAGE "Some IEEE Fortran 2003 features missing, removing usage of these features"
87
)
98

109
wrf_conf_check(
1110
RUN
1211
RESULT_VAR Fortran_2003_ISO_C
13-
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2003_iso_c_test.F
14-
EXTENSION .F
12+
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2003_iso_c_test.F
1513
MESSAGE "Some ISO_C Fortran 2003 features missing, removing usage ISO_C and stubbing code dependent on it"
1614
)
1715

1816
wrf_conf_check(
1917
RUN
2018
RESULT_VAR Fortran_2003_FLUSH
21-
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2003_flush_test.F
22-
EXTENSION .F
19+
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2003_flush_test.F
2320
MESSAGE "Standard FLUSH routine Fortran 2003 features missing, checking for alternate Fortran_2003_FFLUSH"
2421
)
2522

2623
if ( NOT ${Fortran_2003_FLUSH} )
2724
wrf_conf_check(
2825
RUN
2926
RESULT_VAR Fortran_2003_FFLUSH
30-
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2003_fflush_test.F
31-
EXTENSION .F
27+
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2003_fflush_test.F
3228
MESSAGE "Standard FFLUSH routine Fortran 2003 features missing, no alternate to FLUSH found, feature stubbed out"
3329
)
3430
endif()
3531

3632
wrf_conf_check(
3733
RUN
3834
RESULT_VAR Fortran_2003_GAMMA
39-
SOURCE ${PROJECT_SOURCE_DIR}/tools/fortran_2008_gamma_test.F
40-
EXTENSION .F
35+
SOURCES ${PROJECT_SOURCE_DIR}/tools/fortran_2008_gamma_test.F
4136
MESSAGE "Some Fortran 2003 features missing, removing usage gamma function intrinsic and stubbing code dependent on it"
4237
)
4338

4439

4540

4641
wrf_conf_check(
4742
RUN
48-
SOURCE_TYPE C
4943
RESULT_VAR FSEEKO64
50-
SOURCE ${PROJECT_SOURCE_DIR}/tools/fseek_test.c
51-
EXTENSION .c
52-
ADDITIONAL_DEFINITIONS -DTEST_FSEEKO64 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -DFILE_TO_TEST="${PROJECT_SOURCE_DIR}/CMakeLists.txt"
44+
SOURCES ${PROJECT_SOURCE_DIR}/tools/fseek_test.c
45+
OPTIONS
46+
COMPILE_DEFINITIONS -DTEST_FSEEKO64 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -DFILE_TO_TEST="${PROJECT_SOURCE_DIR}/CMakeLists.txt"
5347
MESSAGE "fseeko64 not supported, checking alternate fseeko"
5448
)
5549

5650
if ( NOT "${FSEEKO64}" )
5751
wrf_conf_check(
5852
RUN
59-
SOURCE_TYPE C
6053
RESULT_VAR FSEEKO
61-
SOURCE ${PROJECT_SOURCE_DIR}/tools/fseek_test.c
62-
EXTENSION .c
63-
ADDITIONAL_DEFINITIONS -DTEST_FSEEKO -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -DFILE_TO_TEST="${PROJECT_SOURCE_DIR}/CMakeLists.txt"
54+
SOURCES ${PROJECT_SOURCE_DIR}/tools/fseek_test.c
55+
OPTIONS
56+
COMPILE_DEFINITINOS -DTEST_FSEEKO -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1 -DFILE_TO_TEST="${PROJECT_SOURCE_DIR}/CMakeLists.txt"
6457
MESSAGE "fseeko not supported, compiling with fseek (caution with large files)"
6558
)
6659
endif()

0 commit comments

Comments
 (0)