Skip to content

Commit

Permalink
cmake: Add LTO option
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Nov 13, 2023
1 parent 7400ca5 commit 1212a7e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}")
Expand Down

0 comments on commit 1212a7e

Please sign in to comment.