diff --git a/cmake/modules/AddCompilerFlags.cmake b/cmake/modules/AddCompilerFlags.cmake --- a/cmake/modules/AddCompilerFlags.cmake +++ b/cmake/modules/AddCompilerFlags.cmake @@ -63,11 +63,28 @@ endmacro() # Note that CMake does not provide any facility to check that a linker flag is -# supported by the compiler, but most linker will just drop any unsupported flag -# (eventually with a warning). +# supported by the compiler. +# However since CMake 3.2 introduced the CMP0056 policy, the +# CMAKE_EXE_LINKER_FLAGS variable is used by the try_compile function, so there +# is a workaround that allow for testing the linker flags. +macro(check_linker_flag FLAG RESULT) + # Save the current linker flags + set(SAVE_CMAKE_EXE_LINKERFLAGS ${CMAKE_EXE_LINKER_FLAGS}) + append_flag_to_variable(CMAKE_EXE_LINKER_FLAGS ${FLAG}) + # CHECK_CXX_COMPILER_FLAG calls CHECK_CXX_SOURCE_COMPILES which in turn + # calls try_compile, so it will check our flag + CHECK_CXX_COMPILER_FLAG("" ${RESULT}) + # Restore the current linker flags + set(CMAKE_EXE_LINKER_FLAGS ${SAVE_CMAKE_EXE_LINKERFLAGS}) +endmacro() + function(add_linker_flag) foreach(f ${ARGN}) - append_flag_to_variable(CMAKE_EXE_LINKER_FLAGS ${f}) + build_test_name_from_flag(FLAG_IS_SUPPORTED ${f}) + check_linker_flag(${f} ${FLAG_IS_SUPPORTED}) + if(${FLAG_IS_SUPPORTED}) + append_flag_to_variable(CMAKE_EXE_LINKER_FLAGS ${f}) + endif() endforeach() set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} PARENT_SCOPE) endfunction()