opm-core/cmake/Modules/UseOnlyNeeded.cmake
Roland Kaufmann 9328cb896e Protect against underlinking of UMFPACK library
If libumfpack.so does not declare an explicit dependency on libamd.so,
the linker option -Wl,--as-needed will cause an undefined reference
since we don't use libamd.so ourself. If the configuration determines
that this may be the case, a linker option that forces linking the the
AMD library is added for the SuiteSparse library, and the general
configuration then gets out of the way if this is set.
2013-02-19 13:15:00 +01:00

37 lines
1.4 KiB
CMake

# - Use only needed imports from libraries
#
# Add the -Wl,--as-needed flag to the default linker flags on Linux
# in order to get only the minimal set of dependencies.
function (prepend var_name value)
if (${var_name})
set (${var_name} "${value} ${${var_name}}" PARENT_SCOPE)
else (${var_name})
set (${var_name} "${value}")
endif (${var_name})
endfunction (prepend var_name value)
# only ELF shared objects can be underlinked, and only GNU will accept
# these parameters; otherwise just leave it to the defaults
if ((CMAKE_CXX_PLATFORM_ID STREQUAL "Linux") AND CMAKE_COMPILER_IS_GNUCC)
# these are the modules whose probes will turn up incompatible
# flags on some systems
set (_maybe_underlinked
SuiteSparse
)
# check if any modules actually reported problems (by setting an
# appropriate linker flag)
set (_underlinked FALSE)
foreach (_module IN LISTS _maybe_underlinked)
if ("${${_module}_LINKER_FLAGS}" MATCHES "-Wl,--no-as-needed")
set (_underlinked TRUE)
endif ("${${_module}_LINKER_FLAGS}" MATCHES "-Wl,--no-as-needed")
endforeach (_module)
# if we didn't have any problems, then go ahead and add
if (NOT _underlinked)
prepend (CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
prepend (CMAKE_MODULE_LINKER_FLAGS "-Wl,--as-needed")
prepend (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
endif (NOT _underlinked)
endif ((CMAKE_CXX_PLATFORM_ID STREQUAL "Linux") AND CMAKE_COMPILER_IS_GNUCC)