fix: CachyOS LTO Compilation

Fix LTO linking issues on CachyOS with GCC 15.x

- Add LTO support to all core library targets (input_common, frontend_common,
  network, shader_recompiler, web_service) that were missing LTO configuration
- Create citron_configure_lto() helper function for consistent LTO handling
- Implement CachyOS-specific detection via /etc/os-release and kernel version
- Apply conservative LTO flags (-flto=auto -ffat-lto-objects) only for
  CachyOS + GCC 15+ to resolve linking errors with newer toolchains
- Other distributions continue using aggressive LTO settings for maximum performance
- Disable LTO on executable targets to prevent main function optimization issues

This resolves "undefined symbol" errors when building with -DCITRON_ENABLE_LTO=ON
on CachyOS while maintaining optimal LTO performance on other distributions.

Fixes linking errors including:
- Core::Frontend::EmuWindow symbols
- Core::System methods
- Settings::values
- Common logging functions

Tested on CachyOS with GCC 15.1.1 + LLD 20.1.8

Signed-off-by: Boss.sfc <boss.sfc@citron-emu.org>
This commit is contained in:
Boss.sfc
2025-07-22 21:37:37 +07:00
parent d0ae8f1582
commit fc480dcb69
11 changed files with 49 additions and 6 deletions

View File

@@ -686,6 +686,37 @@ function(create_target_directory_groups target_name)
endforeach()
endfunction()
# Helper function to configure LTO for a target with CachyOS-specific compatibility
function(citron_configure_lto target_name)
if (CITRON_ENABLE_LTO)
set_property(TARGET ${target_name} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
# Detect CachyOS and apply conservative LTO settings only for CachyOS
set(IS_CACHYOS FALSE)
if (EXISTS "/etc/os-release")
file(READ "/etc/os-release" OS_RELEASE_CONTENT)
if (OS_RELEASE_CONTENT MATCHES "ID=cachyos")
set(IS_CACHYOS TRUE)
endif()
endif()
# Alternative detection via kernel name
if (NOT IS_CACHYOS)
execute_process(COMMAND uname -r OUTPUT_VARIABLE KERNEL_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
if (KERNEL_VERSION MATCHES "cachyos")
set(IS_CACHYOS TRUE)
endif()
endif()
# Apply conservative LTO settings only for CachyOS with GCC 15+
if (IS_CACHYOS AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "15.0")
message(STATUS "Detected CachyOS with GCC ${CMAKE_CXX_COMPILER_VERSION} - using conservative LTO settings for ${target_name}")
target_compile_options(${target_name} PRIVATE "-flto=auto" "-ffat-lto-objects")
target_link_options(${target_name} PRIVATE "-flto=auto" "-ffat-lto-objects")
endif()
endif()
endfunction()
# Prevent boost from linking against libs when building
target_link_libraries(Boost::headers INTERFACE Boost::disable_autolinking)
# Adjustments for MSVC + Ninja

View File

@@ -232,6 +232,8 @@ if (ARCHITECTURE_x86_64 OR ARCHITECTURE_arm64)
target_link_libraries(audio_core PRIVATE dynarmic::dynarmic)
endif()
citron_configure_lto(audio_core)
if (ENABLE_CUBEB)
target_sources(audio_core PRIVATE
sink/cubeb_sink.cpp

View File

@@ -266,4 +266,6 @@ if (CITRON_USE_PRECOMPILED_HEADERS)
target_precompile_headers(common PRIVATE precompiled_headers.h)
endif()
citron_configure_lto(common)
create_target_directory_groups(common)

View File

@@ -1294,8 +1294,6 @@ if (CITRON_USE_PRECOMPILED_HEADERS)
target_precompile_headers(core PRIVATE precompiled_headers.h)
endif()
if (CITRON_ENABLE_LTO)
set_property(TARGET core PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
citron_configure_lto(core)
create_target_directory_groups(core)

View File

@@ -9,3 +9,5 @@ add_library(frontend_common STATIC
create_target_directory_groups(frontend_common)
target_link_libraries(frontend_common PUBLIC core SimpleIni::SimpleIni PRIVATE common Boost::headers)
citron_configure_lto(frontend_common)

View File

@@ -164,3 +164,5 @@ target_link_libraries(hid_core PUBLIC core)
if (CITRON_USE_PRECOMPILED_HEADERS)
target_precompile_headers(hid_core PRIVATE precompiled_headers.h)
endif()
citron_configure_lto(hid_core)

View File

@@ -93,6 +93,8 @@ if (CITRON_USE_PRECOMPILED_HEADERS)
target_precompile_headers(input_common PRIVATE precompiled_headers.h)
endif()
citron_configure_lto(input_common)
if (ANDROID)
target_sources(input_common PRIVATE
drivers/android.cpp

View File

@@ -28,3 +28,5 @@ endif()
if (CITRON_USE_PRECOMPILED_HEADERS)
target_precompile_headers(network PRIVATE precompiled_headers.h)
endif()
citron_configure_lto(network)

View File

@@ -266,3 +266,5 @@ create_target_directory_groups(shader_recompiler)
if (CITRON_USE_PRECOMPILED_HEADERS)
target_precompile_headers(shader_recompiler PRIVATE precompiled_headers.h)
endif()
citron_configure_lto(shader_recompiler)

View File

@@ -388,9 +388,7 @@ if (CITRON_USE_PRECOMPILED_HEADERS)
target_precompile_headers(video_core PRIVATE precompiled_headers.h)
endif()
if (CITRON_ENABLE_LTO)
set_property(TARGET video_core PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
citron_configure_lto(video_core)
if (ANDROID AND ARCHITECTURE_arm64)
target_link_libraries(video_core PRIVATE adrenotools)

View File

@@ -22,3 +22,5 @@ target_link_libraries(web_service PRIVATE common network nlohmann_json::nlohmann
if (CITRON_USE_PRECOMPILED_HEADERS)
target_precompile_headers(web_service PRIVATE precompiled_headers.h)
endif()
citron_configure_lto(web_service)