Add SOCI_LTO CMake build option to enable LTO: this results in
noticeable library size reduction if nothing else.

See #846.
This commit is contained in:
Vadim Zeitlin
2020-12-27 13:46:55 +01:00

View File

@@ -22,7 +22,37 @@ option(SOCI_SHARED "Enable build of shared libraries" ON)
option(SOCI_STATIC "Enable build of static libraries" ON)
option(SOCI_TESTS "Enable build of collection of SOCI tests" ON)
option(SOCI_ASAN "Enable address sanitizer on GCC v4.8+/Clang v 3.1+" OFF)
option(SOCI_LTO "Enable link time optimization" OFF)
if (SOCI_LTO)
cmake_minimum_required(VERSION 3.9)
# Check and enable lto support
include(CheckIPOSupported)
check_ipo_supported(RESULT supported)
if (NOT supported)
message(STATUS "IPO / LTO not supported")
endif()
if (supported AND NOT SOCI_ASAN)
message(STATUS "IPO / LTO enabled")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Check for lld as clang lto works best with its own linker
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-fuse-ld=lld" HAS_LLD)
if (HAS_LLD)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fuse-ld=lld")
endif()
endif()
elseif(supported)
message(STATUS "IPO / LTO is supported but conflicts with ASAN and not enabled")
endif()
endif()
###############################################################################
# SOCI CMake modules
@@ -204,4 +234,3 @@ endforeach()
configure_file("${CONFIG_FILE_IN}" "${CONFIG_FILE_OUT}")
message(STATUS "")