Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: Fix TryAppendCXXFlags module #287

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ else()
try_append_cxx_flags("-Wextra" TARGET warn_interface SKIP_LINK)
try_append_cxx_flags("-Wgnu" TARGET warn_interface SKIP_LINK)
# Some compilers will ignore -Wformat-security without -Wformat, so just combine the two here.
try_append_cxx_flags("-Wformat;-Wformat-security" TARGET warn_interface SKIP_LINK)
try_append_cxx_flags("-Wformat -Wformat-security" TARGET warn_interface SKIP_LINK)
try_append_cxx_flags("-Wvla" TARGET warn_interface SKIP_LINK)
try_append_cxx_flags("-Wshadow-field" TARGET warn_interface SKIP_LINK)
try_append_cxx_flags("-Wthread-safety" TARGET warn_interface SKIP_LINK)
Expand Down
32 changes: 18 additions & 14 deletions cmake/module/TryAppendCXXFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,27 @@ function(try_append_cxx_flags flags)
"IF_CHECK_PASSED;IF_CHECK_FAILED" # multi_value_keywords
)

string(MAKE_C_IDENTIFIER "${flags}" result)
string(TOUPPER "${result}" result)
string(PREPEND result CXX_SUPPORTS_)
set(flags_as_string "${flags}")
separate_arguments(flags)

string(MAKE_C_IDENTIFIER "${flags_as_string}" id_string)
string(TOUPPER "${id_string}" id_string)

set(source "int main() { return 0; }")
if(DEFINED TACXXF_SOURCE AND NOT TACXXF_SOURCE STREQUAL source)
set(source "${TACXXF_SOURCE}")
string(SHA256 source_hash "${source}")
string(SUBSTRING ${source_hash} 0 4 source_hash_head)
string(APPEND result _${source_hash_head})
string(APPEND id_string _${source_hash_head})
endif()

# This avoids running a linker.
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_REQUIRED_FLAGS "${flags} ${working_compiler_werror_flag}")
check_cxx_source_compiles("${source}" ${result})
set(CMAKE_REQUIRED_FLAGS "${flags_as_string} ${working_compiler_werror_flag}")
set(compiler_result CXX_SUPPORTS_${id_string})
check_cxx_source_compiles("${source}" ${compiler_result})

if(${result})
if(${compiler_result})
if(DEFINED TACXXF_IF_CHECK_PASSED)
if(DEFINED TACXXF_TARGET)
target_compile_options(${TACXXF_TARGET} INTERFACE ${TACXXF_IF_CHECK_PASSED})
Expand All @@ -82,7 +85,7 @@ function(try_append_cxx_flags flags)
target_compile_options(${TACXXF_TARGET} INTERFACE ${flags})
endif()
if(DEFINED TACXXF_VAR)
string(STRIP "${${TACXXF_VAR}} ${flags}" ${TACXXF_VAR})
string(STRIP "${${TACXXF_VAR}} ${flags_as_string}" ${TACXXF_VAR})
endif()
endif()
elseif(DEFINED TACXXF_IF_CHECK_FAILED)
Expand All @@ -99,19 +102,20 @@ function(try_append_cxx_flags flags)
endif()

if(DEFINED TACXXF_RESULT_VAR)
set(${TACXXF_RESULT_VAR} "${${result}}" PARENT_SCOPE)
set(${TACXXF_RESULT_VAR} "${${compiler_result}}" PARENT_SCOPE)
endif()

if(NOT ${result} OR TACXXF_SKIP_LINK)
if(NOT ${compiler_result} OR TACXXF_SKIP_LINK)
return()
endif()

# This forces running a linker.
set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
set(CMAKE_REQUIRED_FLAGS "${flags} ${working_linker_werror_flag}")
check_cxx_source_compiles("${source}" ${result})
set(CMAKE_REQUIRED_FLAGS "${flags_as_string} ${working_linker_werror_flag}")
set(linker_result LINKER_SUPPORTS_${id_string})
check_cxx_source_compiles("${source}" ${linker_result})

if(${result})
if(${linker_result})
if(DEFINED TACXXF_IF_CHECK_PASSED)
if(DEFINED TACXXF_TARGET)
target_link_options(${TACXXF_TARGET} INTERFACE ${TACXXF_IF_CHECK_PASSED})
Expand All @@ -122,7 +126,7 @@ function(try_append_cxx_flags flags)
endif()
endif()
else()
message(WARNING "The ${flags} fail(s) to link.")
message(WARNING "'${flags_as_string}' fail(s) to link.")
endif()
endfunction()

Expand Down
Loading