Changeset View
Changeset View
Standalone View
Standalone View
cmake/modules/AddCompilerFlags.cmake
Show First 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | |||||
endmacro() | endmacro() | ||||
function(add_compile_definitions_to_configuration CONFIGURATION) | function(add_compile_definitions_to_configuration CONFIGURATION) | ||||
foreach(f ${ARGN}) | foreach(f ${ARGN}) | ||||
add_compile_definitions($<$<CONFIG:${CONFIGURATION}>:${f}>) | add_compile_definitions($<$<CONFIG:${CONFIGURATION}>:${f}>) | ||||
endforeach() | endforeach() | ||||
endfunction() | endfunction() | ||||
# Note that CMake does not provide any facility to check that a linker flag is | # Note that CMake provides a facility to check that a linker flag is supported | ||||
# supported by the compiler. | # by the compiler starting with 3.18 but we require 3.16 so we have our own | ||||
# However since CMake 3.2 introduced the CMP0056 policy, the | # function. | ||||
# CMAKE_EXE_LINKER_FLAGS variable is used by the try_compile function, so there | # ***WARNING***: it should not collide with any CMake name (including internal | ||||
# is a workaround that allow for testing the linker flags. | # names!), or it can cause infinite recursion, so we use some boring one here. | ||||
function(_check_linker_flag RESULT FLAG) | # | ||||
# 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 from versions before 3.18. | |||||
function(_internal_custom_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) | ||||
Show All 13 Lines | function(_internal_custom_check_linker_flag RESULT FLAG) | ||||
CHECK_CXX_COMPILER_FLAG("" ${FLAG_IS_SUPPORTED}) | CHECK_CXX_COMPILER_FLAG("" ${FLAG_IS_SUPPORTED}) | ||||
# Restore CMAKE_EXE_LINKER_FLAGS | # Restore CMAKE_EXE_LINKER_FLAGS | ||||
set(CMAKE_EXE_LINKER_FLAGS ${SAVED_CMAKE_EXE_LINKER_FLAGS}) | set(CMAKE_EXE_LINKER_FLAGS ${SAVED_CMAKE_EXE_LINKER_FLAGS}) | ||||
set(${RESULT} ${${FLAG_IS_SUPPORTED}} PARENT_SCOPE) | set(${RESULT} ${${FLAG_IS_SUPPORTED}} PARENT_SCOPE) | ||||
endfunction() | endfunction() | ||||
function(check_linker_flag RESULT FLAG) | function(custom_check_linker_flag RESULT FLAG) | ||||
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") | if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") | ||||
# Include -Wl,--disable-reloc-section so work around a bug from ld.bfd, | # Include -Wl,--disable-reloc-section so work around a bug from ld.bfd, | ||||
# the MinGw linker used to cross compile for Windows. | # the MinGw linker used to cross compile for Windows. | ||||
# CMake will always attempt to create an import library, despite this | # CMake will always attempt to create an import library, despite this | ||||
# try_compile is building an executable and not a library, by adding a | # try_compile is building an executable and not a library, by adding a | ||||
# --out-implib linker flag. For executables with no exported symbols | # --out-implib linker flag. For executables with no exported symbols | ||||
# this causes ld to segfault when section relocations is enabled, which | # this causes ld to segfault when section relocations is enabled, which | ||||
# is the default for recent MinGw versions (and it is a good thing). | # is the default for recent MinGw versions (and it is a good thing). | ||||
# But since here we're building for the sole purpose of detecting if a | # But since here we're building for the sole purpose of detecting if a | ||||
# flag is supported or not it's totally fine to manually disable it. | # flag is supported or not it's totally fine to manually disable it. | ||||
# See https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5194 | # See https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5194 | ||||
set(_LD_DISABLE_RELOC_SECTION "-Wl,--disable-reloc-section") | set(_LD_DISABLE_RELOC_SECTION "-Wl,--disable-reloc-section") | ||||
_check_linker_flag(_LD_DISABLE_RELOC_SECTION_SUPPORTED ${_LD_DISABLE_RELOC_SECTION}) | _internal_custom_check_linker_flag(_LD_DISABLE_RELOC_SECTION_SUPPORTED ${_LD_DISABLE_RELOC_SECTION}) | ||||
if(_LD_DISABLE_RELOC_SECTION_SUPPORTED) | if(_LD_DISABLE_RELOC_SECTION_SUPPORTED) | ||||
set(EXTRA_LD_FLAGS ${_LD_DISABLE_RELOC_SECTION}) | set(EXTRA_LD_FLAGS ${_LD_DISABLE_RELOC_SECTION}) | ||||
endif() | endif() | ||||
endif() | endif() | ||||
_check_linker_flag(_RESULT ${FLAG} ${EXTRA_LD_FLAGS}) | _internal_custom_check_linker_flag(_RESULT ${FLAG} ${EXTRA_LD_FLAGS}) | ||||
set(${RESULT} ${_RESULT} PARENT_SCOPE) | set(${RESULT} ${_RESULT} PARENT_SCOPE) | ||||
endfunction() | endfunction() | ||||
function(add_linker_flags) | function(add_linker_flags) | ||||
foreach(f ${ARGN}) | foreach(f ${ARGN}) | ||||
check_linker_flag(FLAG_IS_SUPPORTED ${f}) | custom_check_linker_flag(FLAG_IS_SUPPORTED ${f}) | ||||
if(${FLAG_IS_SUPPORTED}) | if(${FLAG_IS_SUPPORTED}) | ||||
add_link_options(${f}) | add_link_options(${f}) | ||||
endif() | endif() | ||||
endforeach() | endforeach() | ||||
endfunction() | endfunction() | ||||
macro(remove_optimization_level_from_var VAR) | macro(remove_optimization_level_from_var VAR) | ||||
Show All 21 Lines |