cmake: apply '-Wl,--as-needed' linker option

This commit is contained in:
Alexander Alekhin 2020-01-21 17:19:30 +03:00
parent 80ade96c8c
commit 8af87d5792
2 changed files with 16 additions and 1 deletions

View File

@ -316,6 +316,21 @@ if(PPC64LE)
endif()
endif()
# Apply "-Wl,--as-needed" linker flags: https://github.com/opencv/opencv/issues/7001
if(NOT OPENCV_SKIP_LINK_AS_NEEDED)
if(UNIX AND (NOT APPLE OR NOT CMAKE_VERSION VERSION_LESS "3.2"))
set(_option "-Wl,--as-needed")
set(_saved_CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${_option}") # requires CMake 3.2+ and CMP0056
ocv_check_compiler_flag(CXX "" HAVE_LINK_AS_NEEDED)
set(CMAKE_EXE_LINKER_FLAGS "${_saved_CMAKE_EXE_LINKER_FLAGS}")
if(HAVE_LINK_AS_NEEDED)
set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} ${_option}")
set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} ${_option}")
endif()
endif()
endif()
# combine all "extra" options
if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")

View File

@ -540,7 +540,7 @@ macro(ocv_check_flag_support lang flag varname base_options)
string(TOUPPER "${flag}" ${varname})
string(REGEX REPLACE "^(/|-)" "HAVE_${_lang}_" ${varname} "${${varname}}")
string(REGEX REPLACE " -|-|=| |\\." "_" ${varname} "${${varname}}")
string(REGEX REPLACE " -|-|=| |\\.|," "_" ${varname} "${${varname}}")
ocv_check_compiler_flag("${_lang}" "${base_options} ${flag}" ${${varname}} ${ARGN})
endmacro()