LTO используется только для исполняемых файлов, но не библиотек

This commit is contained in:
Andrei Astafev 2019-10-09 17:10:17 +03:00
parent bf099295ab
commit e57792f316
2 changed files with 44 additions and 41 deletions

View File

@ -46,4 +46,41 @@ function(common_target_properties Name)
endif()
endif()
# LTO only for executables (not libraries) in Release build type
get_target_property(target_type ${Name} TYPE)
if(target_type STREQUAL "EXECUTABLE" AND CMAKE_BUILD_TYPE STREQUAL "Release")
check_cxx_compiler_flag(-flto CXX_HAS_LTO_FLAG)
check_cxx_compiler_flag(-fno-fat-lto-objects CXX_HAS_NO_FAT_LTO_FLAG)
if(CMAKE_CXX_COMPILER_IS_GCC AND CXX_HAS_LTO_FLAG)
find_program(CMAKE_GCC_AR
NAMES "${_CMAKE_TOOLCHAIN_PREFIX}gcc-ar${_CMAKE_TOOLCHAIN_SUFFIX}"
"${_CMAKE_TOOLCHAIN_PREFIX}gcc-ar"
HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
find_program(CMAKE_GCC_NM
NAMES "${_CMAKE_TOOLCHAIN_PREFIX}gcc-nm${_CMAKE_TOOLCHAIN_SUFFIX}"
"${_CMAKE_TOOLCHAIN_PREFIX}gcc-nm"
HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
find_program(CMAKE_GCC_RANLIB
NAMES "${_CMAKE_TOOLCHAIN_PREFIX}gcc-ranlib${_CMAKE_TOOLCHAIN_SUFFIX}"
"${_CMAKE_TOOLCHAIN_PREFIX}gcc-ranlib"
HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
if(CMAKE_GCC_AR AND CMAKE_GCC_NM AND CMAKE_GCC_RANLIB)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto")
if(CXX_HAS_NO_FAT_LTO_FLAG)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-fat-lto-objects")
endif()
set(CMAKE_AR "${CMAKE_GCC_AR}")
set(CMAKE_NM "${CMAKE_GCC_NM}")
set(CMAKE_RANLIB "${CMAKE_GCC_RANLIB}")
else()
message(
WARNING
"GCC indicates LTO support, but binutils wrappers could not be found. Disabling LTO.")
endif()
else()
check_enable_compiler_flag(-flto)
check_enable_compiler_flag(-fno-fat-lto-objects)
endif()
endif()
endfunction()

View File

@ -1,14 +1,19 @@
# based on https://github.com/bluescarni/yacma
include(CheckCXXCompilerFlag)
if(LSB_DISTRIBUTOR_ID STREQUAL "AstraLinuxSE"
AND LSB_CODENAME STREQUAL "smolensk"
AND LSB_RELEASE STREQUAL "1.5")
set(_CMAKE_TOOLCHAIN_PREFIX "x86_64-linux-gnu-")
set(_CMAKE_TOOLCHAIN_SUFFIX "-4.7")
set(_CMAKE_TOOLCHAIN_LOCATION} "/usr/bin")
else()
# -Wshadow gives a lot of false positives with GCC 4.7.2 in Astra Linux 1.5
if(CMAKE_CXX_COMPILER_IS_GCC)
check_enable_cxx_flag(-Wshadow)
endif()
endif()
include(CheckCXXCompilerFlag)
macro(CHECK_ENABLE_CXX_FLAG flag)
set(CMAKE_REQUIRED_QUIET TRUE)
@ -92,41 +97,6 @@ if(CMAKE_CXX_COMPILER_IS_CLANG OR CMAKE_CXX_COMPILER_IS_INTEL OR CMAKE_CXX_COMPI
check_enable_cxx_flag(-Wstrict-null-sentinel)
check_enable_cxx_flag(-Wshadow-all)
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Release")
check_cxx_compiler_flag(-flto CXX_HAS_LTO_FLAG)
check_cxx_compiler_flag(-fno-fat-lto-objects CXX_HAS_NO_FAT_LTO_FLAG)
if(CMAKE_CXX_COMPILER_IS_GCC AND CXX_HAS_LTO_FLAG)
find_program(CMAKE_GCC_AR
NAMES "${_CMAKE_TOOLCHAIN_PREFIX}gcc-ar${_CMAKE_TOOLCHAIN_SUFFIX}"
"${_CMAKE_TOOLCHAIN_PREFIX}gcc-ar"
HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
find_program(CMAKE_GCC_NM
NAMES "${_CMAKE_TOOLCHAIN_PREFIX}gcc-nm${_CMAKE_TOOLCHAIN_SUFFIX}"
"${_CMAKE_TOOLCHAIN_PREFIX}gcc-nm"
HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
find_program(CMAKE_GCC_RANLIB
NAMES "${_CMAKE_TOOLCHAIN_PREFIX}gcc-ranlib${_CMAKE_TOOLCHAIN_SUFFIX}"
"${_CMAKE_TOOLCHAIN_PREFIX}gcc-ranlib"
HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
if(CMAKE_GCC_AR AND CMAKE_GCC_NM AND CMAKE_GCC_RANLIB)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto")
if(CXX_HAS_NO_FAT_LTO_FLAG)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-fat-lto-objects")
endif()
set(CMAKE_AR "${CMAKE_GCC_AR}")
set(CMAKE_NM "${CMAKE_GCC_NM}")
set(CMAKE_RANLIB "${CMAKE_GCC_RANLIB}")
else()
message(
WARNING
"GCC indicates LTO support, but binutils wrappers could not be found. Disabling LTO.")
endif()
else()
check_enable_compiler_flag(-flto)
check_enable_compiler_flag(-fno-fat-lto-objects)
endif()
endif()
endif()
if(CMAKE_CXX_COMPILER_IS_GCC)
@ -142,10 +112,6 @@ if(CMAKE_CXX_COMPILER_IS_GCC)
# Let's just disable the warning for now.
check_enable_cxx_flag(-Wno-attributes)
endif()
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "4.799")
# -Wshadow gives a lot of false positives with GCC 4.7.2 in Astra Linux 1.5
check_enable_cxx_flag(-Wshadow)
endif()
endif()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "e2k")