From 8af87d5792f83e399272cecffc3d70cd6fec3109 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 21 Jan 2020 17:19:30 +0300 Subject: [PATCH] cmake: apply '-Wl,--as-needed' linker option --- cmake/OpenCVCompilerOptions.cmake | 15 +++++++++++++++ cmake/OpenCVUtils.cmake | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmake/OpenCVCompilerOptions.cmake b/cmake/OpenCVCompilerOptions.cmake index e5bcaf709a..dd4dcc2296 100644 --- a/cmake/OpenCVCompilerOptions.cmake +++ b/cmake/OpenCVCompilerOptions.cmake @@ -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}") diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake index f21a9acf14..2ea21bfcec 100644 --- a/cmake/OpenCVUtils.cmake +++ b/cmake/OpenCVUtils.cmake @@ -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()