opm-simulators/cmake/Modules/UseOptimization.cmake
Roland Kaufmann 7bf82f96b2 Disable link-time, whole program optimization
When using LTO, the linker calls back to the compiler to figure out
where a certain symbol is defined. However, in many versions there
is apparently a bug that is triggered if a template in inlined in
both a used library and in the main program, but with different
versions of the compiler. The Boost exception class is particular
prone for this.

Thus, we disable the -lto from the development builds by default;
if anyone wants to test of the behaviour of their own compiler, they
must now explicitly turn on this flag.
2013-08-14 22:59:55 +02:00

63 lines
2.1 KiB
CMake

# - Turn on optimizations based on build type
include(TestCXXAcceptsFlag)
include (AddOptions)
# mapping from profile name (in CMAKE_BUILD_TYPE) to variable part
set (_prof_DEBUG "Debug")
set (_prof_RELEASE "Release;RelWithDebInfo;MinSizeRel")
# if we are building a debug target, then disable all optimizations
# otherwise, turn them on. indicate to the code what we have done
# so it can turn on assertions etc.
if (CMAKE_COMPILER_IS_GNUCXX)
# extra flags passed for optimization
set (_opt_flags "")
# link-time (a.k.a. global) optimizations
# disabled due to widespread bugs in the linker plugin
option (WHOLE_PROG_OPTIM "Whole program optimization (lto)" OFF)
if (WHOLE_PROG_OPTIM)
check_cxx_accepts_flag ("-flto" HAVE_LINK_OPTS)
if (HAVE_LINK_OPTS)
list (APPEND _opt_flags "-flto")
endif (HAVE_LINK_OPTS)
endif (WHOLE_PROG_OPTIM)
# native instruction set tuning
option (WITH_NATIVE "Use native instruction set" ON)
if (WITH_NATIVE)
check_cxx_accepts_flag ("-mtune=native" HAVE_MTUNE)
if (HAVE_MTUNE)
list (APPEND _opt_flags "-mtune=native")
endif (HAVE_MTUNE)
endif (WITH_NATIVE)
# default optimization flags, if not set by user
set_default_option (_opt_dbg "-O0" "(^|\ )-O")
set_default_option (_opt_rel "-O3" "(^|\ )-O")
# use these options for debug builds - no optimizations
add_options (ALL_LANGUAGES "${_prof_DEBUG}" ${_opt_dbg} "-DDEBUG")
# use these options for release builds - full optimization
add_options (ALL_LANGUAGES "${_prof_RELEASE}" ${_opt_rel} "-DNDEBUG" ${_opt_flags})
else (CMAKE_COMPILER_IS_GNUCXX)
# default information from system
foreach (lang IN ITEMS C CXX Fortran)
if (lang STREQUAL "Fortran")
set (_lang F)
else (lang STREQUAL "Fortran")
set (_lang ${lang})
endif (lang STREQUAL "Fortran")
foreach (profile IN ITEMS DEBUG RELEASE)
if (NOT CMAKE_${lang}_FLAGS_${profile})
add_options (${lang} "${_prof_${profile}}"
"$ENV{${_lang}FLAGS} ${CMAKE_${lang}_FLAGS_${profile}_INIT}")
endif (NOT CMAKE_${lang}_FLAGS_${profile})
endforeach (profile)
endforeach (lang)
endif (CMAKE_COMPILER_IS_GNUCXX)