|
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 |
21 | 4 |
|
22 | 5 | function(silkit_enable_asan isOn) |
23 | 6 | if(NOT isOn) |
@@ -234,3 +217,49 @@ int main() { |
234 | 217 | link_libraries(-latomic) |
235 | 218 | endif() |
236 | 219 | 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() |
0 commit comments