Skip to content

Commit 7629e7d

Browse files
committed
CMake: add lto options
1 parent ee2a895 commit 7629e7d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

cmake/common_compiler_flags.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ function( common_compiler_flags )
7878
$<${IS_GNU}:-fno-gnu-unique>
7979
>
8080

81+
$<${IS_LTO}:
82+
$<${IS_MSVC}: /GL >
83+
$<${IS_CLANG}: -flto=${LTO_TYPE} >
84+
$<${IS_APPLECLANG}: -flto=${LTO_TYPE} >
85+
$<${IS_GNU}: -flto >
86+
>
87+
8188
# MSVC only
8289
$<${IS_MSVC}:
8390
# /MP isn't valid for clang-cl with msvc frontend
@@ -170,6 +177,13 @@ function( common_compiler_flags )
170177
$<${IS_CLANG}:-s>
171178
$<${IS_APPLECLANG}:-Wl,-S -Wl,-x -Wl,-dead_strip>
172179
>
180+
181+
$<${IS_LTO}:
182+
$<${IS_MSVC}: /LTCG:INCREMENTAL >
183+
$<${IS_CLANG}: -flto=${LTO_TYPE} >
184+
$<${IS_APPLECLANG}: -flto=${LTO_TYPE} >
185+
$<${IS_GNU}: -flto >
186+
>
173187
)
174188

175189
endfunction()

cmake/godotcpp.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/web.cmake)
3232
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/windows.cmake)
3333
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/python_callouts.cmake)
3434

35+
# IPO Check for when GODOT_LTO is set to fail more gracefully.
36+
include(CheckIPOSupported)
37+
3538
# Detect number of processors
3639
include(ProcessorCount)
3740
ProcessorCount(PROC_MAX)
@@ -127,6 +130,9 @@ function( godotcpp_options )
127130

128131
#TODO optimize
129132

133+
set( GODOT_LTO "none" CACHE STRING "Link-time optimization" )
134+
set_property( CACHE GODOT_LTO PROPERTY STRINGS "none;auto;thin;full" )
135+
130136
option( GODOT_DEV_BUILD "Developer build with dev-only debugging code (DEV_ENABLED)" OFF )
131137

132138
#[[ debug_symbols
@@ -159,6 +165,28 @@ endfunction()
159165

160166
# Function to configure and generate the targets
161167
function( godotcpp_generate )
168+
#[[ Link-Time Optimization ]]
169+
if( GODOT_LTO STREQUAL "none") # Default Value
170+
set( IS_LTO 0 )
171+
elseif( GODOT_LTO STREQUAL "thin" )
172+
set( IS_LTO 1 )
173+
set( LTO_TYPE "thin" )
174+
else() # auto or full
175+
set( IS_LTO 1 )
176+
set( LTO_TYPE "full" )
177+
endif()
178+
if( IS_LTO )
179+
check_ipo_supported(RESULT result OUTPUT output LANGUAGES CXX )
180+
if( NOT result)
181+
message( FATAL_ERROR "GODOT_LTO=${GODOT_LTO}, but IPO/LTO is not supported by the current compiler")
182+
endif()
183+
if( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
184+
message( STATUS "Using LTO: ${LTO_TYPE}" )
185+
else( )
186+
message( STATUS "Using LTO" )
187+
endif( )
188+
endif()
189+
162190
#[[ Multi-Threaded MSVC Compilation
163191
When using the MSVC compiler the build command -j <n> only specifies
164192
parallel jobs or targets, and not multi-threaded compilation To speed up

0 commit comments

Comments
 (0)