From 7cf4225fe4ab8f8caedf5cc0500575377a1d3682 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Mon, 3 Nov 2025 01:21:03 -0500 Subject: [PATCH] Reduces debug executable size by stripping unused .data sections --- modules/compiler/flags.cmake | 11 +++++++---- modules/compiler/gcc.cmake | 12 ++++++++---- modules/helpers.cmake | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/modules/compiler/flags.cmake b/modules/compiler/flags.cmake index aa802fd..217d343 100644 --- a/modules/compiler/flags.cmake +++ b/modules/compiler/flags.cmake @@ -5,14 +5,17 @@ endfunction() function(SetFGLFlags TARGET) - GET_PROPERTY(FGL_FLAGS GLOBAL PROPERTY FGL_FLAGS) - target_compile_options(${TARGET} PUBLIC ${FGL_FLAGS}) - message("Set target ${TARGET} to use flags\n${FGL_FLAGS}") - target_link_options(${TARGET} PUBLIC ${FGL_FLAGS}) + GET_PROPERTY(FGL_COMPILE_FLAGS GLOBAL PROPERTY FGL_COMPILE_FLAGS) + GET_PROPERTY(FGL_LINKS_FLAGS GLOBAL PROPERTY FGL_LINK_FLAGS) + target_compile_options(${TARGET} PUBLIC ${FGL_COMPILE_FLAGS}) + message("Set target ${TARGET} to use flags\n${FGL_COMPILE_FLAGS}") + target_link_options(${TARGET} PUBLIC ${FGL_LINK_FLAGS}) endfunction() function(SetDependencyFlags TARGET) GET_PROPERTY(FGL_CHILD_FLAGS GLOBAL PROPERTY FGL_CHILD_FLAGS) + GET_PROPERTY(FGL_LINKS_FLAGS GLOBAL PROPERTY FGL_LINK_FLAGS) target_compile_options(${TARGET} PUBLIC ${FGL_CHILD_FLAGS}) message("Set dependency ${TARGET} to use flags\n${FGL_CHILD_FLAGS}") + target_link_options(${TARGET} PUBLIC ${FGL_LINK_FLAGS}) endfunction() \ No newline at end of file diff --git a/modules/compiler/gcc.cmake b/modules/compiler/gcc.cmake index 9be0aa3..e99a672 100644 --- a/modules/compiler/gcc.cmake +++ b/modules/compiler/gcc.cmake @@ -117,7 +117,7 @@ if (DEFINED FGL_ENABLE_UBSAN AND FGL_ENABLE_UBSAN EQUAL 1) list(APPEND FGL_CONFIG "-fsanitize=undefined,address,leak,alignment,bounds,vptr") -# list(APPEND FGL_CONFIG "-fsanitize-trap") + # list(APPEND FGL_CONFIG "-fsanitize-trap") endif () if (NOT DEFINED FGL_STATIC_ANALYSIS) @@ -136,7 +136,10 @@ list(APPEND FGL_CONFIG "-ftree-vectorize") list(APPEND FGL_CONFIG "-fmax-errors=2") - LIST(APPEND FGL_CONFIG "-std=c++23") + list(APPEND FGL_CONFIG "-std=c++23") + + list(APPEND FGL_CONFIG "-fdata-sections") + list(APPEND FGL_CONFIG "-ffunction-sections") #AppendWarningFlag("-fanalyzer") #AppendWarningFlag("-Wanalyzer-too-complex") @@ -160,7 +163,6 @@ list(APPEND FGL_SHARED_DEBUG "-gdwarf-4") list(APPEND FGL_SHARED_DEBUG "-fvar-tracking-assignments") - # Optimization flags set(FGL_FINAL_FLAGS_RELEASE "-O2;-s;${FGL_GENERAL_OPTIMIZATION_FLAGS};${FGL_SHARED_OPTIMIZATION_FLAGS}") # System agonistc flags set(FGL_FINAL_FLAGS_RELWITHDEBINFO "-O2;${FGL_GENERAL_OPTIMIZATION_FLAGS};${FGL_SHARED_OPTIMIZATION_FLAGS};${FGL_SHARED_DEBUG}") @@ -176,9 +178,11 @@ # Final sets set(FGL_FLAGS "${FGL_CONFIG};${FGL_FINAL_FLAGS_${UPPER_BUILD_TYPE}};${FGL_WARNINGS}") # Flags for our shit set(FGL_FLAGS "${FGL_OPTIMIZATION_FLAGS_${UPPER_BUILD_TYPE}}" PARENT_SCOPE) - set(FGL_CHILD_FLAGS "${FGL_FINAL_FLAGS_RELEASE}") # Child flags for adding optimization to anything we build ourselves but doesn't follow our standard + set(FGL_CHILD_FLAGS "${FGL_FINAL_FLAGS_RELEASE}" PARENT_SCOPE) # Child flags for adding optimization to anything we build ourselves but doesn't follow our standard # We use release flags since we really don't need to be using debug flags for anything not ours + set(FGL_LINK_FLAGS "-Wl,--gcc-sections;-Wl,--print-gc-sections" PARENT_SCOPE) + SET_PROPERTY(GLOBAL PROPERTY FGL_FLAGS ${FGL_FLAGS}) SET_PROPERTY(GLOBAL PROPERTY FGL_CHILD_FLAGS ${FGL_CHILD_FLAGS}) diff --git a/modules/helpers.cmake b/modules/helpers.cmake index 1443f86..a1cee73 100644 --- a/modules/helpers.cmake +++ b/modules/helpers.cmake @@ -26,6 +26,17 @@ function(ConfigureFGLTarget NAME SRC_DIR INCLUDE_DIR) target_compile_features(${NAME} PUBLIC cxx_std_23) endfunction() +function(SplitDebugSymbols NAME) + if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + add_custom_command(TARGET ${NAME} POST_BUILD + COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $ $.debug + COMMAND ${CMAKE_STRIP} --strip-debug --strip-unneeded $ + COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$.debug $ + COMMENT "Stripping symbols and creating ${NAME}.debug" + ) + endif () +endfunction() + function(AddFGLExecutable NAME SRC_SOURCES_LOCATION) file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS ${SRC_SOURCES_LOCATION}/**.cpp @@ -46,6 +57,9 @@ function(AddFGLExecutable NAME SRC_SOURCES_LOCATION) SetFGLFlags(${NAME}) AddGitInfo(${NAME}) target_compile_definitions(${NAME} PRIVATE FGL_BUILD_TYPE="${CMAKE_BUILD_TYPE}") + + SplitDebugSymbols(${NAME}) + endfunction() function(AddFGLLibrary NAME MODE SRC_SOURCES_LOCATION INCLUDE_SOURCES_LOCATION) @@ -65,6 +79,10 @@ function(AddFGLLibrary NAME MODE SRC_SOURCES_LOCATION INCLUDE_SOURCES_LOCATION) SetFGLFlags(${NAME}) AddGitInfo(${NAME}) target_compile_definitions(${NAME} PRIVATE FGL_BUILD_TYPE="${CMAKE_BUILD_TYPE}") + + if (NOT "${MODE}" STREQUAL "OBJECT") + SplitDebugSymbols(${NAME}) + endif () endfunction() function(AddFGLModule NAME SRC_SOURCES_LOCATION) @@ -75,6 +93,8 @@ function(AddFGLModule NAME SRC_SOURCES_LOCATION) SetFGLFlags(${NAME}) AddGitInfo(${NAME}) target_compile_definitions(${NAME} PRIVATE FGL_BUILD_TYPE="${CMAKE_BUILD_TYPE}") + + SplitDebugSymbols(${NAME}) endfunction() function(AddFGLChildLibrary NAME MODE SRC_SOURCES_LOCATION INCLUDE_SOURCES_LOCATION) @@ -85,4 +105,6 @@ function(AddFGLChildLibrary NAME MODE SRC_SOURCES_LOCATION INCLUDE_SOURCES_LOCAT target_include_directories(${NAME} PUBLIC ${INCLUDE_SOURCES_LOCATION}) target_include_directories(${NAME} PRIVATE ${SRC_SOURCES_LOCATION}) ConfigureFGLTarget(${NAME} ${SRC_SOURCES_LOCATION} ${INCLUDE_SOURCES_LOCATION}) + + SplitDebugSymbols(${NAME}) endfunction()