Changeset View
Changeset View
Standalone View
Standalone View
cmake/modules/AddCompilerFlags.cmake
Show All 10 Lines | function(check_compiler_flag RESULT LANGUAGE FLAG) | ||||
elseif("${LANGUAGE}" STREQUAL "CXX") | elseif("${LANGUAGE}" STREQUAL "CXX") | ||||
CHECK_CXX_COMPILER_FLAG(${FLAG} ${TEST_NAME}) | CHECK_CXX_COMPILER_FLAG(${FLAG} ${TEST_NAME}) | ||||
else() | else() | ||||
message(FATAL_ERROR "check_compiler_flag LANGUAGE should be C or CXX") | message(FATAL_ERROR "check_compiler_flag 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_to_var TARGET LANGUAGE) | |||||
foreach(f ${ARGN}) | |||||
# If the flag is already set, avoid duplicating it | |||||
string(FIND "${${TARGET}}" "${f}" FLAG_POSITION) | |||||
if(${FLAG_POSITION} LESS 0) | |||||
check_compiler_flag(FLAG_IS_SUPPORTED ${LANGUAGE} ${f}) | |||||
if(${FLAG_IS_SUPPORTED}) | |||||
string(APPEND ${TARGET} " ${f}") | |||||
endif() | |||||
endif() | |||||
endforeach() | |||||
set(${TARGET} ${${TARGET}} PARENT_SCOPE) | |||||
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_flag(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() | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | |||||
# CMAKE_EXE_LINKER_FLAGS variable is used by the try_compile function, so there | # CMAKE_EXE_LINKER_FLAGS variable is used by the try_compile function, so there | ||||
# is a workaround that allow for testing the linker flags. | # is a workaround that allow for testing the linker flags. | ||||
function(check_linker_flag RESULT FLAG) | function(check_linker_flag RESULT FLAG) | ||||
# 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. | ||||
add_compiler_flags_to_var( | set(WERROR_UNUSED_ARG -Werror=unused-command-line-argument) | ||||
CMAKE_REQUIRED_FLAGS | check_compiler_flag(IS_WERROR_SUPPORTED CXX ${WERROR_UNUSED_ARG}) | ||||
CXX | if(${IS_WERROR_SUPPORTED}) | ||||
"-Werror=unused-command-line-argument" | set(CMAKE_REQUIRED_FLAGS ${WERROR_UNUSED_ARG}) | ||||
) | 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}) | ||||
# If the flag is already set, avoid duplicating it | # If the flag is already set, avoid duplicating it | ||||
string(FIND "${CMAKE_EXE_LINKER_FLAGS}" "${FLAG}" FLAG_POSITION) | string(FIND "${CMAKE_EXE_LINKER_FLAGS}" "${FLAG}" FLAG_POSITION) | ||||
if(${FLAG_POSITION} LESS 0) | if(${FLAG_POSITION} LESS 0) | ||||
string(APPEND CMAKE_EXE_LINKER_FLAGS " ${FLAG}") | string(APPEND CMAKE_EXE_LINKER_FLAGS " ${FLAG}") | ||||
Show All 25 Lines |