Changeset View
Changeset View
Standalone View
Standalone View
cmake/modules/AddCompilerFlags.cmake
# Allow to easily add flags for C and C++ | # Allow to easily add flags for C and C++ | ||||
include(CheckCXXCompilerFlag) | include(CheckCXXCompilerFlag) | ||||
include(CheckCCompilerFlag) | include(CheckCCompilerFlag) | ||||
include(SanitizeHelper) | include(SanitizeHelper) | ||||
function(check_compiler_flag RESULT LANGUAGE FLAG) | function(check_compiler_flags RESULT LANGUAGE) | ||||
sanitize_c_cxx_definition("have_${LANGUAGE}_" ${FLAG} TEST_NAME) | sanitize_c_cxx_definition("have_${LANGUAGE}_" "${ARGN}" TEST_NAME) | ||||
if("${LANGUAGE}" STREQUAL "C") | if("${LANGUAGE}" STREQUAL "C") | ||||
CHECK_C_COMPILER_FLAG(${FLAG} ${TEST_NAME}) | CHECK_C_COMPILER_FLAG("${ARGN}" ${TEST_NAME}) | ||||
elseif("${LANGUAGE}" STREQUAL "CXX") | elseif("${LANGUAGE}" STREQUAL "CXX") | ||||
CHECK_CXX_COMPILER_FLAG(${FLAG} ${TEST_NAME}) | CHECK_CXX_COMPILER_FLAG("${ARGN}" ${TEST_NAME}) | ||||
else() | else() | ||||
message(FATAL_ERROR "check_compiler_flag LANGUAGE should be C or CXX") | message(FATAL_ERROR "check_compiler_flags LANGUAGE should be C or CXX") | ||||
endif() | endif() | ||||
set(${RESULT} ${${TEST_NAME}} PARENT_SCOPE) | set(${RESULT} ${${TEST_NAME}} PARENT_SCOPE) | ||||
endfunction() | endfunction() | ||||
function(add_compiler_flags_for_language LANGUAGE) | function(add_compiler_flags_for_language LANGUAGE) | ||||
foreach(f ${ARGN}) | foreach(f ${ARGN}) | ||||
check_compiler_flag(FLAG_IS_SUPPORTED ${LANGUAGE} ${f}) | check_compiler_flags(FLAG_IS_SUPPORTED ${LANGUAGE} ${f}) | ||||
if(${FLAG_IS_SUPPORTED}) | if(${FLAG_IS_SUPPORTED}) | ||||
add_compile_options($<$<COMPILE_LANGUAGE:${LANGUAGE}>:${f}>) | add_compile_options($<$<COMPILE_LANGUAGE:${LANGUAGE}>:${f}>) | ||||
endif() | endif() | ||||
endforeach() | endforeach() | ||||
endfunction() | endfunction() | ||||
macro(add_c_compiler_flags) | macro(add_c_compiler_flags) | ||||
add_compiler_flags_for_language(C ${ARGN}) | add_compiler_flags_for_language(C ${ARGN}) | ||||
Show All 36 Lines | |||||
macro(remove_compiler_flags) | macro(remove_compiler_flags) | ||||
remove_c_compiler_flags(${ARGN}) | remove_c_compiler_flags(${ARGN}) | ||||
remove_cxx_compiler_flags(${ARGN}) | remove_cxx_compiler_flags(${ARGN}) | ||||
endmacro() | endmacro() | ||||
function(add_compile_options_to_configuration_for_language CONFIGURATION LANGUAGE) | function(add_compile_options_to_configuration_for_language CONFIGURATION LANGUAGE) | ||||
foreach(f ${ARGN}) | foreach(f ${ARGN}) | ||||
check_compiler_flag(FLAG_IS_SUPPORTED ${LANGUAGE} ${f}) | check_compiler_flags(FLAG_IS_SUPPORTED ${LANGUAGE} ${f}) | ||||
if(${FLAG_IS_SUPPORTED}) | if(${FLAG_IS_SUPPORTED}) | ||||
add_compile_options($<$<AND:$<CONFIG:${CONFIGURATION}>,$<COMPILE_LANGUAGE:${LANGUAGE}>>:${f}>) | add_compile_options($<$<AND:$<CONFIG:${CONFIGURATION}>,$<COMPILE_LANGUAGE:${LANGUAGE}>>:${f}>) | ||||
endif() | endif() | ||||
endforeach() | endforeach() | ||||
endfunction() | endfunction() | ||||
macro(add_c_compile_options_to_configuration CONFIGURATION) | macro(add_c_compile_options_to_configuration CONFIGURATION) | ||||
add_compile_options_to_configuration_for_language(${CONFIGURATION} C ${ARGN}) | add_compile_options_to_configuration_for_language(${CONFIGURATION} C ${ARGN}) | ||||
Show All 23 Lines | function(check_linker_flag RESULT FLAG) | ||||
sanitize_c_cxx_definition("have_linker_" ${FLAG} FLAG_IS_SUPPORTED) | sanitize_c_cxx_definition("have_linker_" ${FLAG} FLAG_IS_SUPPORTED) | ||||
# Some linkers (e.g.: Clang) will issue a -Wunused-command-line-argument | # Some linkers (e.g.: Clang) will issue a -Wunused-command-line-argument | ||||
# warning when an unknown linker flag is set. | # warning when an unknown linker flag is set. | ||||
# Using -Werror will promote these warnings to errors so | # Using -Werror will promote these warnings to errors so | ||||
# CHECK_CXX_COMPILER_FLAG() will return false, preventing the flag from | # CHECK_CXX_COMPILER_FLAG() will return false, preventing the flag from | ||||
# being set. | # being set. | ||||
set(WERROR_UNUSED_ARG -Werror=unused-command-line-argument) | set(WERROR_UNUSED_ARG -Werror=unused-command-line-argument) | ||||
check_compiler_flag(IS_WERROR_SUPPORTED CXX ${WERROR_UNUSED_ARG}) | check_compiler_flags(IS_WERROR_SUPPORTED CXX ${WERROR_UNUSED_ARG}) | ||||
if(${IS_WERROR_SUPPORTED}) | if(${IS_WERROR_SUPPORTED}) | ||||
set(CMAKE_REQUIRED_FLAGS ${WERROR_UNUSED_ARG}) | set(CMAKE_REQUIRED_FLAGS ${WERROR_UNUSED_ARG}) | ||||
endif() | endif() | ||||
# Save the current linker flags | # Save the current linker flags | ||||
set(SAVE_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS}) | set(SAVE_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS}) | ||||
# Append the flag under test to the linker flags | # Append the flag under test to the linker flags | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |