Skip to content

Commit 40973d6

Browse files
committed
fixup! cmake: Add TryAppendCXXFlags module
1 parent f4162d0 commit 40973d6

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed

cmake/module/TryAppendCXXFlags.cmake

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,28 @@ include(CheckCXXSourceCompiles)
88
#[=[
99
Usage examples:
1010
11-
try_append_cxx_flags(warn_cxx_flags "-Wformat -Wformat-security")
11+
try_append_cxx_flags("-Wformat -Wformat-security" VAR warn_cxx_flags)
1212
1313
14-
try_append_cxx_flags(warn_cxx_flags "-Wsuggest-override"
14+
try_append_cxx_flags("-Wsuggest-override" VAR warn_cxx_flags
1515
SOURCE "struct A { virtual void f(); }; struct B : A { void f() final; };"
1616
)
1717
1818
19-
try_append_cxx_flags(sanitizers_cxx_flags "-fsanitize=${SANITIZERS}" RESULT_VAR cxx_supports_sanitizers)
19+
try_append_cxx_flags("-fsanitize=${SANITIZERS}" TARGET core
20+
RESULT_VAR cxx_supports_sanitizers
21+
)
2022
if(NOT cxx_supports_sanitizers)
2123
message(FATAL_ERROR "Compiler did not accept requested flags.")
2224
endif()
2325
2426
25-
try_append_cxx_flags(warn_cxx_flags "-Wunused-parameter" IF_CHECK_PASSED "-Wno-unused-parameter")
27+
try_append_cxx_flags("-Wunused-parameter" TARGET core
28+
IF_CHECK_PASSED "-Wno-unused-parameter"
29+
)
2630
2731
28-
try_append_cxx_flags(error_cxx_flags "-Werror=return-type"
32+
try_append_cxx_flags("-Werror=return-type" TARGET core
2933
IF_CHECK_FAILED "-Wno-error=return-type"
3034
SOURCE "#include <cassert>\nint f(){ assert(false); }"
3135
)
@@ -37,49 +41,67 @@ In configuration output, this function prints a string by the following pattern:
3741
-- Performing Test CXX_SUPPORTS_[flags] - Success
3842
3943
]=]
40-
function(try_append_cxx_flags flags_var flags)
41-
cmake_parse_arguments(PARSE_ARGV 2
44+
function(try_append_cxx_flags flags)
45+
cmake_parse_arguments(PARSE_ARGV 1
4246
TACXXF # prefix
4347
"" # options
44-
"SOURCE;RESULT_VAR" # one_value_keywords
48+
"TARGET;VAR;SOURCE;RESULT_VAR" # one_value_keywords
4549
"IF_CHECK_PASSED;IF_CHECK_FAILED" # multi_value_keywords
4650
)
4751

4852
string(MAKE_C_IDENTIFIER "${flags}" result)
4953
string(TOUPPER "${result}" result)
50-
set(result "CXX_SUPPORTS_${result}")
51-
52-
# Every subsequent check_cxx_source_compiles((<code> <resultVar>) run will re-use
53-
# the cached result rather than performing the check again, even if the <code> changes.
54-
# Removing the cached result in order to force the check to be re-evaluated.
55-
unset(${result} CACHE)
56-
57-
if(NOT DEFINED TACXXF_SOURCE)
58-
set(TACXXF_SOURCE "int main() { return 0; }")
54+
string(PREPEND result CXX_SUPPORTS_)
55+
56+
set(source "int main() { return 0; }")
57+
if(DEFINED TACXXF_SOURCE AND NOT TACXXF_SOURCE STREQUAL source)
58+
set(source "${TACXXF_SOURCE}")
59+
string(SHA256 source_hash "${source}")
60+
string(SUBSTRING ${source_hash} 0 4 source_hash_head)
61+
string(APPEND result _${source_hash_head})
5962
endif()
6063

6164
# This avoids running a linker.
6265
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
6366
set(CMAKE_REQUIRED_FLAGS "${flags} ${working_compiler_werror_flag}")
64-
check_cxx_source_compiles("${TACXXF_SOURCE}" ${result})
67+
check_cxx_source_compiles("${source}" ${result})
6568

6669
if(${result})
6770
if(DEFINED TACXXF_IF_CHECK_PASSED)
68-
string(STRIP "${${flags_var}} ${TACXXF_IF_CHECK_PASSED}" ${flags_var})
71+
if(DEFINED TACXXF_TARGET)
72+
target_compile_options(${TACXXF_TARGET} INTERFACE ${TACXXF_IF_CHECK_PASSED})
73+
endif()
74+
if(DEFINED TACXXF_VAR)
75+
string(STRIP "${${TACXXF_VAR}} ${TACXXF_IF_CHECK_PASSED}" ${TACXXF_VAR})
76+
endif()
6977
else()
70-
string(STRIP "${${flags_var}} ${flags}" ${flags_var})
78+
if(DEFINED TACXXF_TARGET)
79+
target_compile_options(${TACXXF_TARGET} INTERFACE ${flags})
80+
endif()
81+
if(DEFINED TACXXF_VAR)
82+
string(STRIP "${${TACXXF_VAR}} ${flags}" ${TACXXF_VAR})
83+
endif()
7184
endif()
7285
elseif(DEFINED TACXXF_IF_CHECK_FAILED)
73-
string(STRIP "${${flags_var}} ${TACXXF_IF_CHECK_FAILED}" ${flags_var})
86+
if(DEFINED TACXXF_TARGET)
87+
target_compile_options(${TACXXF_TARGET} INTERFACE ${TACXXF_IF_CHECK_FAILED})
88+
endif()
89+
if(DEFINED TACXXF_VAR)
90+
string(STRIP "${${TACXXF_VAR}} ${TACXXF_IF_CHECK_FAILED}" ${TACXXF_VAR})
91+
endif()
92+
endif()
93+
94+
if(DEFINED TACXXF_VAR)
95+
set(${TACXXF_VAR} "${${TACXXF_VAR}}" PARENT_SCOPE)
96+
endif()
97+
98+
if(DEFINED TACXXF_RESULT_VAR)
99+
set(${TACXXF_RESULT_VAR} "${${result}}" PARENT_SCOPE)
74100
endif()
75-
set(${flags_var} "${${flags_var}}" PARENT_SCOPE)
76-
set(${TACXXF_RESULT_VAR} "${${result}}" PARENT_SCOPE)
77101
endfunction()
78102

79103
if(MSVC)
80-
set(warning_as_error_flag /WX)
104+
try_append_cxx_flags("/WX /options:strict" VAR working_compiler_werror_flag)
81105
else()
82-
set(warning_as_error_flag -Werror)
106+
try_append_cxx_flags("-Werror" VAR working_compiler_werror_flag)
83107
endif()
84-
try_append_cxx_flags(working_compiler_werror_flag ${warning_as_error_flag})
85-
unset(warning_as_error_flag)

0 commit comments

Comments
 (0)