Skip to content

Commit df97eda

Browse files
cursoragenttimfox
andcommitted
feat(cmake): optional ENABLE_LTO (IPO) for Release-style builds
- New cmake/ETLLTO.cmake: CheckIPOSupported, gate on ENABLE_LTO, forbid combination with ENABLE_ASAN. - ENABLE_LTO option declared after LEG_BUNDLED_LIB so bundled libs stay default-on when configuring with -DENABLE_LTO only. - README: document -DENABLE_LTO=ON for packagers. Co-authored-by: Tim Fox <timfox@outlook.com>
1 parent 0f62c62 commit df97eda

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ LEG_BUNDLED_LIB(FREETYPE Freetype2 "BUILD_CLIENT AND FEATURE_FREETYPE")
136136
LEG_BUNDLED_LIB(SQLITE3 SQlite3 "BUILD_CLIENT OR BUILD_MOD")
137137
LEG_BUNDLED_LIB(CJSON cJSON "BUILD_ENGINE OR BUILD_MOD")
138138

139+
option(ENABLE_LTO "Enable IPO/LTO for Release-style builds (toolchain must support it)" OFF)
140+
139141
#-----------------------------------------------------------------
140142
# Setup
141143
#-----------------------------------------------------------------
@@ -174,6 +176,9 @@ include(cmake/ETLTargets.cmake)
174176
# Platform specific compiler settings
175177
include(cmake/ETLPlatform.cmake)
176178

179+
# Optional LTO (after compiler flags are established)
180+
include(cmake/ETLLTO.cmake)
181+
177182
# Source globs
178183
include(cmake/ETLSources.cmake)
179184

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ make
151151
sudo make install # optional; set install vars first
152152
```
153153

154+
**Release packaging — optional LTO**
155+
156+
For **Release** / **RelWithDebInfo** / **MinSizeRel**, you can enable **link-time optimization** if your toolchain supports it (CMake IPO):
157+
158+
```sh
159+
cmake -DENABLE_LTO=ON ..
160+
```
161+
162+
Leave it **off** for normal development (faster links). It cannot be combined with **`ENABLE_ASAN`**.
163+
154164
**Notes**
155165

156166
- 32-bit builds may need **multilib / `-devel`** packages.

cmake/ETLLTO.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#-----------------------------------------------------------------
2+
# Optional link-time optimization (IPO / LTO)
3+
#-----------------------------------------------------------------
4+
#
5+
# Off by default: LTO increases link time and needs a capable toolchain.
6+
# When enabled, applies to Release-style configurations only.
7+
8+
if(NOT ENABLE_LTO)
9+
return()
10+
endif()
11+
12+
if(ENABLE_ASAN)
13+
message(FATAL_ERROR "ENABLE_LTO cannot be used together with ENABLE_ASAN.")
14+
endif()
15+
16+
if(CMAKE_VERSION VERSION_LESS "3.9")
17+
message(FATAL_ERROR "ENABLE_LTO requires CMake 3.9 or newer (CheckIPOSupported).")
18+
endif()
19+
20+
include(CheckIPOSupported)
21+
check_ipo_supported(RESULT _etl_ipo_supported OUTPUT _etl_ipo_error)
22+
23+
if(NOT _etl_ipo_supported)
24+
message(WARNING "ENABLE_LTO=ON but IPO is not supported by this toolchain: ${_etl_ipo_error}")
25+
return()
26+
endif()
27+
28+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
29+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON)
30+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL ON)
31+
32+
message(STATUS "Link-time optimization (IPO/LTO) enabled for Release, RelWithDebInfo, MinSizeRel")

0 commit comments

Comments
 (0)