Skip to content

Commit 53ca659

Browse files
authored
cmake: enable LTO when available (#227)
add LTO support in the CMake build system. It is disabled by default. Will be used for release packages in the future
1 parent c4d6980 commit 53ca659

File tree

4 files changed

+60
-63
lines changed

4 files changed

+60
-63
lines changed

CMakeLists.txt

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
# Copyright (c) 2022 Vector Informatik GmbH
2-
#
3-
# Permission is hereby granted, free of charge, to any person obtaining
4-
# a copy of this software and associated documentation files (the
5-
# "Software"), to deal in the Software without restriction, including
6-
# without limitation the rights to use, copy, modify, merge, publish,
7-
# distribute, sublicense, and/or sell copies of the Software, and to
8-
# permit persons to whom the Software is furnished to do so, subject to
9-
# the following conditions:
10-
#
11-
# The above copyright notice and this permission notice shall be
12-
# included in all copies or substantial portions of the Software.
13-
#
14-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
22-
cmake_minimum_required(VERSION 3.12)
1+
# SPDX-FileCopyrightText: 2025 Vector Informatik GmbH
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
cmake_minimum_required(VERSION 3.13)
236

247
###############################################################################
258
# Project definition
@@ -45,6 +28,7 @@ option(SILKIT_LINK_LLD "Use the lld linker for SIL KIT" OFF)
4528
option(SILKIT_USE_SYSTEM_LIBRARIES "Use the libraries installed on the system for third party dependencies" OFF)
4629
option(SILKIT_BUILD_REPRODUCIBLE "Creates a reproducible build by ommiting timestamps/unique build ids" ON)
4730
option(SILKIT_BUILD_LINUX_PACKAGE "Creates SIL Kit builds suitable for package managers in Linux Distributions (.deb)" OFF)
31+
option(SILKIT_BUILD_LTO "Build with link time optimizations" OFF)
4832

4933
if(SILKIT_BUILD_LINUX_PACKAGE)
5034

@@ -59,7 +43,6 @@ if(SILKIT_BUILD_LINUX_PACKAGE)
5943

6044
endif()
6145

62-
6346
set(CMAKE_C_VISIBILITY_PRESET hidden)
6447
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
6548
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
@@ -99,6 +82,7 @@ silkit_enable_coverage(${SILKIT_ENABLE_COVERAGE})
9982
silkit_check_reproducible()
10083
silkit_clean_default_compileflags()
10184
silkit_add_libs_for_atomic64()
85+
silkit_enable_lto(${SILKIT_BUILD_LTO})
10286

10387
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
10488

SilKit/CMakeLists.txt

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
# Copyright (c) 2022 Vector Informatik GmbH
2-
#
3-
# Permission is hereby granted, free of charge, to any person obtaining
4-
# a copy of this software and associated documentation files (the
5-
# "Software"), to deal in the Software without restriction, including
6-
# without limitation the rights to use, copy, modify, merge, publish,
7-
# distribute, sublicense, and/or sell copies of the Software, and to
8-
# permit persons to whom the Software is furnished to do so, subject to
9-
# the following conditions:
10-
#
11-
# The above copyright notice and this permission notice shall be
12-
# included in all copies or substantial portions of the Software.
13-
#
14-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1+
# SPDX-FileCopyrightText: 2025 Vector Informatik GmbH
2+
#
3+
# SPDX-License-Identifier: MIT
214

225
cmake_minimum_required(VERSION 3.13)
236

SilKit/cmake/SilKitBuildSettings.cmake

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
# Copyright (c) 2022 Vector Informatik GmbH
2-
#
3-
# Permission is hereby granted, free of charge, to any person obtaining
4-
# a copy of this software and associated documentation files (the
5-
# "Software"), to deal in the Software without restriction, including
6-
# without limitation the rights to use, copy, modify, merge, publish,
7-
# distribute, sublicense, and/or sell copies of the Software, and to
8-
# permit persons to whom the Software is furnished to do so, subject to
9-
# the following conditions:
10-
#
11-
# The above copyright notice and this permission notice shall be
12-
# included in all copies or substantial portions of the Software.
13-
#
14-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1+
# SPDX-FileCopyrightText: 2022-2024 Vector Informatik GmbH
2+
#
3+
# SPDX-License-Identifier: MIT
214

225
function(silkit_enable_asan isOn)
236
if(NOT isOn)
@@ -234,3 +217,49 @@ int main() {
234217
link_libraries(-latomic)
235218
endif()
236219
endmacro()
220+
221+
macro(silkit_enable_lto enableLto)
222+
include(CheckIPOSupported)
223+
check_ipo_supported(RESULT _lto_is_supported OUTPUT error)
224+
225+
if ((NOT MINGW) AND ${enableLto} AND _lto_is_supported)
226+
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
227+
228+
find_program(LLD_EXECUTABLE NAMES lld ld.lld)
229+
if(LLD_EXECUTABLE)
230+
message("SIL Kit: Found LLD linker: ${LLD_EXECUTABLE}")
231+
# Test if clang can use LLD
232+
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -fuse-ld=lld --version
233+
RESULT_VARIABLE LLD_TEST_RESULT OUTPUT_QUIET ERROR_QUIET)
234+
if(LLD_TEST_RESULT EQUAL 0)
235+
message("SIL Kit: Using LLD linker for LTO")
236+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
237+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
238+
else()
239+
message(WARNING "SIL Kit: LLD found but not compatible with this Clang version")
240+
endif()
241+
else()
242+
# Fallback to Gold linker
243+
find_program(GOLD_EXECUTABLE NAMES gold ld.gold)
244+
if(GOLD_EXECUTABLE)
245+
message("SIL Kit: LLD not found, using Gold linker: ${GOLD_EXECUTABLE}")
246+
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -fuse-ld=gold --version
247+
RESULT_VARIABLE GOLD_TEST_RESULT OUTPUT_QUIET ERROR_QUIET)
248+
if(GOLD_TEST_RESULT EQUAL 0)
249+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
250+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold")
251+
else()
252+
message(WARNING "SIL Kit: Gold linker found but not compatible with this Clang version")
253+
endif()
254+
else()
255+
message(WARNING "SIL Kit: Neither LLD nor Gold linker found - using default linker (may cause LTO issues)")
256+
endif()
257+
endif()
258+
endif()
259+
message(STATUS "SIL Kit: Enabling Link Time Optimization globally.")
260+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
261+
else()
262+
message(STATUS "SIL Kit: Disabling Link Time Optimization.")
263+
endif()
264+
265+
endmacro()

SilKit/cmake/SilKitBuildTools.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ macro(silkit_package_debugsymbols targetName)
5252
)
5353
endif()
5454
endmacro()
55+

0 commit comments

Comments
 (0)