From e57792f316e0383d4238f030aaebaad0f84161aa Mon Sep 17 00:00:00 2001 From: Andrey Astafyev Date: Wed, 9 Oct 2019 17:10:17 +0300 Subject: [PATCH] =?UTF-8?q?LTO=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=B7=D1=83=D0=B5=D1=82=D1=81=D1=8F=20=D1=82=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=BA=D0=BE=20=D0=B4=D0=BB=D1=8F=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D1=8F=D0=B5=D0=BC=D1=8B=D1=85=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2,=20=D0=BD=D0=BE=20=D0=BD=D0=B5=20=D0=B1?= =?UTF-8?q?=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMLibCommonTargetProperties.cmake | 37 ++++++++++++++++++++++++ CMLibCompilerFlags.cmake | 48 +++++-------------------------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/CMLibCommonTargetProperties.cmake b/CMLibCommonTargetProperties.cmake index c157e77..ad71042 100644 --- a/CMLibCommonTargetProperties.cmake +++ b/CMLibCommonTargetProperties.cmake @@ -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() diff --git a/CMLibCompilerFlags.cmake b/CMLibCompilerFlags.cmake index bafde09..af11cc3 100644 --- a/CMLibCompilerFlags.cmake +++ b/CMLibCompilerFlags.cmake @@ -1,15 +1,20 @@ # 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) check_cxx_compiler_flag("${flag}" CHECK_CXX_FLAG) @@ -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")