From 1212a7eaa675c9f7e6e13238e7ea9da4dff721c8 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Mon, 13 Nov 2023 15:25:20 +0000 Subject: [PATCH] cmake: Add `LTO` option --- CMakeLists.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d30cfae98fb778..f4cecf511d9f76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ cmake_dependent_option(BUILD_WALLET_TOOL "Build bitcoin-wallet tool." ON "ENABLE cmake_dependent_option(CXX20 "Enable compilation in C++20 mode." OFF "NOT MSVC" ON) option(THREADLOCAL "Enable features that depend on the C++ thread_local keyword (currently just thread names in debug logs)." ON) option(HARDENING "Attempt to harden the resulting executables." ON) +option(LTO "Build using LTO. To override the default CMake LTO flags, please use the LTO_FLAGS variable." OFF) option(REDUCE_EXPORTS "Attempt to reduce exported symbols in the resulting executables." OFF) option(WERROR "Treat compiler warnings as errors." OFF) @@ -81,6 +82,19 @@ set(CMAKE_CXX_EXTENSIONS OFF) set(configure_warnings) +if(LTO) + if(DEFINED LTO_FLAGS) + set(CMAKE_CXX_COMPILE_OPTIONS_IPO ${LTO_FLAGS}) + endif() + include(CheckIPOSupported) + check_ipo_supported(RESULT result OUTPUT output) + if(result) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + else() + message(FATAL_ERROR "LTO is not supported: ${output}") + endif() +endif() + include(CheckPIESupported) check_pie_supported(OUTPUT_VARIABLE check_pie_output LANGUAGES CXX) if(CMAKE_CXX_LINK_PIE_SUPPORTED) @@ -369,6 +383,14 @@ message("Linker flags for executables .......... ${CMAKE_EXE_LINKER_FLAGS}") message("Linker flags for shared libraries ..... ${CMAKE_SHARED_LINKER_FLAGS}") print_config_flags() message("Use assembly routines ................. ${ASM}") +if(LTO) + list(JOIN CMAKE_CXX_COMPILE_OPTIONS_IPO " " lto_flags) + string(PREPEND lto_flags ", with flags: ") +else() + set(lto_flags) +endif() +message("Build using LTO ....................... ${LTO}${lto_flags}") +unset(lto_flags) message("Attempt to harden executables ......... ${HARDENING}") message("Treat compiler warnings as errors ..... ${WERROR}") message("Use ccache for compiling .............. ${CCACHE}")