Changeset View
Changeset View
Standalone View
Standalone View
cmake/modules/AddCompilerFlags.cmake
Show First 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | function(add_compile_definitions_to_configuration CONFIGURATION) | ||||
endforeach() | endforeach() | ||||
endfunction() | endfunction() | ||||
# Note that CMake does not provide any facility to check that a linker flag is | # Note that CMake does not provide any facility to check that a linker flag is | ||||
# supported by the compiler. | # supported by the compiler. | ||||
# However since CMake 3.2 introduced the CMP0056 policy, the | # However since CMake 3.2 introduced the CMP0056 policy, the | ||||
# 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(add_linker_flags) | function(check_linker_flag RESULT FLAG) | ||||
foreach(f ${ARGN}) | |||||
sanitize_c_cxx_definition("have_linker_" ${f} 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. | ||||
add_compiler_flags_to_var( | add_compiler_flags_to_var( | ||||
CMAKE_REQUIRED_FLAGS | CMAKE_REQUIRED_FLAGS | ||||
CXX | CXX | ||||
"-Werror=unused-command-line-argument" | "-Werror=unused-command-line-argument" | ||||
) | ) | ||||
# Save the current linker flags | # Save the current linker flags | ||||
set(SAVE_CMAKE_EXE_LINKERFLAGS ${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}" "${f}" 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 " ${f}") | string(APPEND CMAKE_EXE_LINKER_FLAGS " ${FLAG}") | ||||
endif() | endif() | ||||
# CHECK_CXX_COMPILER_FLAG calls CHECK_CXX_SOURCE_COMPILES which in turn | # CHECK_CXX_COMPILER_FLAG calls CHECK_CXX_SOURCE_COMPILES which in turn | ||||
# calls try_compile, so it will check our flag | # calls try_compile, so it will check our flag | ||||
CHECK_CXX_COMPILER_FLAG("" ${FLAG_IS_SUPPORTED}) | CHECK_CXX_COMPILER_FLAG("" ${RESULT}) | ||||
# Unset the -Werror=unused-command-line-argument flag if it is set. | # Restore CMAKE_EXE_LINKER_FLAGS | ||||
remove_compiler_flags_from_var( | set(CMAKE_EXE_LINKER_FLAGS ${SAVE_CMAKE_EXE_LINKER_FLAGS}) | ||||
CMAKE_REQUIRED_FLAGS | |||||
"-Werror=unused-command-line-argument" | |||||
) | |||||
# If the flag is not supported restore CMAKE_EXE_LINKER_FLAGS | set(${RESULT} ${${RESULT}} PARENT_SCOPE) | ||||
if(NOT ${FLAG_IS_SUPPORTED}) | endfunction() | ||||
set(CMAKE_EXE_LINKER_FLAGS ${SAVE_CMAKE_EXE_LINKERFLAGS}) | |||||
function(add_linker_flags) | |||||
foreach(f ${ARGN}) | |||||
sanitize_c_cxx_definition("have_linker_" ${f} FLAG_IS_SUPPORTED) | |||||
check_linker_flag(${FLAG_IS_SUPPORTED} ${f}) | |||||
# If the flag is supported, add it to CMAKE_EXE_LINKER_FLAGS | |||||
if(${FLAG_IS_SUPPORTED}) | |||||
string(APPEND CMAKE_EXE_LINKER_FLAGS " ${f}") | |||||
endif() | endif() | ||||
endforeach() | endforeach() | ||||
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} PARENT_SCOPE) | set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} PARENT_SCOPE) | ||||
endfunction() | endfunction() |