Skip to content

Commit 2987c36

Browse files
committed
build: add opt-in link-time optimization for release builds
Add a BUILD_LTO option that enables CMake's interprocedural optimization for the Release and MinSizeRel configurations only, leaving Debug and RelWithDebInfo (and every default/OpenWrt build) unchanged. IPO performs cross translation-unit dead code elimination and folds identical functions, trimming a further ~2.4% from libucode (172 KB -> 168 KB stripped on top of the unwind-tables change). Off by default because it lengthens builds and assumes a matching LTO toolchain. Guarded by check_ipo_supported() with a warning if unavailable.
1 parent b706d7a commit 2987c36

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
99
include(CheckFunctionExists)
1010
include(CheckSymbolExists)
1111
include(CheckCSourceCompiles)
12+
include(CheckIPOSupported)
1213
include(GNUInstallDirs)
1314

1415
option(BUILD_OPTIMIZE_SIZE "Optimize for size" ON)
@@ -64,6 +65,23 @@ if(BUILD_NO_UNWIND_TABLES)
6465
)
6566
endif()
6667

68+
# Link-time optimization in release builds. Cross translation-unit dead code
69+
# elimination and folding of identical functions trims a further few percent
70+
# from libucode. Off by default: it lengthens builds and assumes a matching LTO
71+
# toolchain, so OpenWrt and default builds are unaffected unless requested.
72+
# Only Release and MinSizeRel are enabled, Debug and RelWithDebInfo are not.
73+
option(BUILD_LTO "Enable link-time optimization in release builds" OFF)
74+
if(BUILD_LTO)
75+
check_ipo_supported(RESULT lto_supported OUTPUT lto_error)
76+
77+
if(lto_supported)
78+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
79+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL ON)
80+
else()
81+
message(WARNING "BUILD_LTO requested but IPO is unsupported: ${lto_error}")
82+
endif()
83+
endif()
84+
6785
check_c_source_compiles("
6886
int main() {
6987
#ifndef _FORTIFY_SOURCE

0 commit comments

Comments
 (0)