From 30539371c268d27dcd8a77cf9cc6fcff972b3584 Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 22 Jan 2026 14:46:04 +0100 Subject: [PATCH] more cmake stuffs -add LTO option, enabled by default -set optimization level to O3 and O0 for debug builds -add ASAN and UBSAN options -always compile with gnu debug symbols --- src/CMakeLists.txt | 25 ++++++++++++++++++------- src/dedicated/CMakeLists.txt | 14 ++++++++++++++ src/sdl/CMakeLists.txt | 17 +++++++++++++++-- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f09b95633..77a1f40fa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -236,18 +236,23 @@ set(SRB2_CONFIG_HWRENDER ON CACHE BOOL "Enable hardware rendering through OpenGL.") set(SRB2_CONFIG_STATIC_OPENGL OFF CACHE BOOL "Use statically linked OpenGL. NOT RECOMMENDED.") -set(SRB2_CONFIG_SCREENFBO ON CACHE BOOL - "Enable hardware renderer (OpenGL) FBO downsampling support") set(SRB2_CONFIG_DEDICATED OFF CACHE BOOL "Build a dedicated server.") +set(SRB2_CONFIG_LIBBACKTRACE ON CACHE BOOL + "Enable using libbacktrace for crashlogs.") +set(SRB2_CONFIG_LTO ON CACHE BOOL + "Enable link time optimizations, improves performance at the cost of longer link times.") +set(SRB2_CONFIG_ASAN OFF CACHE BOOL + "Compile with AddressSanitizer (libasan).") +set(SRB2_CONFIG_UBSAN OFF CACHE BOOL + "Compile with UndefinedBehaviorSanitizer (libubsan).") set(SRB2_CONFIG_SATURNPAK ON CACHE BOOL "Enable Saturn specific special packet. Required for using the newer resynch system") set(SRB2_CONFIG_SATURNJOIN ON CACHE BOOL "Enable Saturn specific allowjoin toggle.") - -set(SRB2_CONFIG_LIBBACKTRACE ON CACHE BOOL - "Enable using libbacktrace for crashlogs") +set(SRB2_CONFIG_SCREENFBO ON CACHE BOOL + "Enable hardware renderer (OpenGL) FBO downsampling support") if(${SRB2_CONFIG_DEDICATED}) set(SRB2_CONFIG_HAVE_GME NO) @@ -502,7 +507,6 @@ if(${SRB2_CONFIG_LIBBACKTRACE}) if(${LIBBACKTRACE_FOUND}) set(SRB2_HAVE_LIBBACKTRACE ON) add_definitions(-DHAVE_LIBBACKTRACE) - add_compile_options(-g) else() message(WARNING "You have specified that libbacktrace is available but it was not found.") endif() @@ -550,7 +554,6 @@ if(${SRB2_CONFIG_HWRENDER}) set(SRB2_R_OPENGL_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/hardware/r_opengl/r_opengl.h ) - endif() if(${SRB2_CONFIG_HWRENDER} AND ${SRB2_CONFIG_STATIC_OPENGL}) @@ -589,6 +592,14 @@ endif() # TODO: fix prng and P_CreateBlockmap overflow at some point add_compile_options(-fwrapv) +if (${CMAKE_BUILD_TYPE} MATCHES Debug) + add_compile_options(-O0) +else() + add_compile_options(-O3) +endif() + +add_compile_options(-g) + add_definitions(-DCMAKECONFIG) #add_library(SRB2Core STATIC diff --git a/src/dedicated/CMakeLists.txt b/src/dedicated/CMakeLists.txt index 84e36690f..38fb179f0 100644 --- a/src/dedicated/CMakeLists.txt +++ b/src/dedicated/CMakeLists.txt @@ -119,6 +119,20 @@ target_compile_definitions(SRB2SDL2 PRIVATE -DHAVE_THREADS ) +if(SRB2_CONFIG_LTO AND NOT SRB2_CONFIG_DEBUGMODE) + set_property(TARGET SRB2SDL2 PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) +endif() + +if(SRB2_CONFIG_ASAN) + target_compile_options(SRB2SDL2 PRIVATE -fsanitize=address) + target_link_options(SRB2SDL2 PRIVATE -fsanitize=address) +endif() + +if(SRB2_CONFIG_UBSAN) + target_compile_options(SRB2SDL2 PRIVATE -fsanitize=undefined) + target_link_options(SRB2SDL2 PRIVATE -fsanitize=undefined) +endif() + ## strip debug symbols into separate file when using gcc # if(CMAKE_COMPILER_IS_GNUCC) # if(${CMAKE_BUILD_TYPE} MATCHES Debug) diff --git a/src/sdl/CMakeLists.txt b/src/sdl/CMakeLists.txt index 1c7d52344..c7d612c80 100644 --- a/src/sdl/CMakeLists.txt +++ b/src/sdl/CMakeLists.txt @@ -169,7 +169,6 @@ if(${SDL2_FOUND}) target_link_libraries(SRB2SDL2 PRIVATE ${LIBBACKTRACE_LIBRARIES}) endif() - if(${CMAKE_SYSTEM} MATCHES Linux) target_link_libraries(SRB2SDL2 PRIVATE m @@ -230,6 +229,20 @@ if(${SDL2_FOUND}) -DHAVE_THREADS ) + if(SRB2_CONFIG_LTO AND NOT SRB2_CONFIG_DEBUGMODE) + set_property(TARGET SRB2SDL2 PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() + + if(SRB2_CONFIG_ASAN) + target_compile_options(SRB2SDL2 PRIVATE -fsanitize=address) + target_link_options(SRB2SDL2 PRIVATE -fsanitize=address) + endif() + + if(SRB2_CONFIG_UBSAN) + target_compile_options(SRB2SDL2 PRIVATE -fsanitize=undefined) + target_link_options(SRB2SDL2 PRIVATE -fsanitize=undefined) + endif() + ## strip debug symbols into separate file when using gcc # if(CMAKE_COMPILER_IS_GNUCC) # if(${CMAKE_BUILD_TYPE} MATCHES Debug) @@ -296,7 +309,7 @@ if(${SDL2_FOUND}) else() find_library(SRB2_SDL2_DLL_${dllname} "${defaultname}") list(APPEND win_extra_dll_list ${SRB2_SDL2_DLL_${dllname}}) - endif() + endif() endmacro() getwinlib(SDL2 "SDL2.dll") if(${SRB2_CONFIG_SDL2_USEMIXER})