|
| 1 | +# TheSuperHackers @build JohnsterID 05/01/2026 Add debug symbol stripping for MinGW Release builds |
| 2 | +# Debug Symbol Stripping for MinGW-w64 Release Builds |
| 3 | +# |
| 4 | +# Separates debug symbols from executables into .debug files, matching MSVC PDB workflow. |
| 5 | +# This reduces shipped binary size while preserving symbols for crash analysis. |
| 6 | + |
| 7 | +# Find the required tools for symbol stripping |
| 8 | +if(MINGW) |
| 9 | + # Use the cross-compiler toolchain's objcopy and strip |
| 10 | + # These should be in the same directory as the compiler |
| 11 | + get_filename_component(COMPILER_DIR ${CMAKE_CXX_COMPILER} DIRECTORY) |
| 12 | + |
| 13 | + find_program(MINGW_OBJCOPY |
| 14 | + NAMES ${CMAKE_CXX_COMPILER_TARGET}-objcopy |
| 15 | + ${CMAKE_SYSTEM_PROCESSOR}-w64-mingw32-objcopy |
| 16 | + objcopy |
| 17 | + HINTS ${COMPILER_DIR} |
| 18 | + DOC "MinGW objcopy tool for extracting debug symbols" |
| 19 | + ) |
| 20 | + |
| 21 | + find_program(MINGW_STRIP |
| 22 | + NAMES ${CMAKE_CXX_COMPILER_TARGET}-strip |
| 23 | + ${CMAKE_SYSTEM_PROCESSOR}-w64-mingw32-strip |
| 24 | + strip |
| 25 | + HINTS ${COMPILER_DIR} |
| 26 | + DOC "MinGW strip tool for removing debug symbols" |
| 27 | + ) |
| 28 | + |
| 29 | + if(MINGW_OBJCOPY AND MINGW_STRIP) |
| 30 | + message(STATUS "Debug symbol stripping enabled:") |
| 31 | + message(STATUS " objcopy: ${MINGW_OBJCOPY}") |
| 32 | + message(STATUS " strip: ${MINGW_STRIP}") |
| 33 | + set(DEBUG_STRIP_AVAILABLE TRUE) |
| 34 | + else() |
| 35 | + message(WARNING "Debug symbol stripping not available - tools not found") |
| 36 | + if(NOT MINGW_OBJCOPY) |
| 37 | + message(WARNING " objcopy not found") |
| 38 | + endif() |
| 39 | + if(NOT MINGW_STRIP) |
| 40 | + message(WARNING " strip not found") |
| 41 | + endif() |
| 42 | + set(DEBUG_STRIP_AVAILABLE FALSE) |
| 43 | + endif() |
| 44 | + |
| 45 | + # Function to strip debug symbols from a target and create a separate .debug file |
| 46 | + # |
| 47 | + # This implements a three-step process: |
| 48 | + # 1. Extract debug symbols to separate file |
| 49 | + # 2. Strip debug symbols from main executable |
| 50 | + # 3. Add debug link so debuggers can find the symbols |
| 51 | + # |
| 52 | + # Usage: |
| 53 | + # add_debug_strip_target(target_name) |
| 54 | + # |
| 55 | + # Result (for Release builds only): |
| 56 | + # program.exe - Stripped executable (smaller) |
| 57 | + # program.exe.debug - Debug symbols (can be distributed separately) |
| 58 | + # |
| 59 | + function(add_debug_strip_target target_name) |
| 60 | + if(NOT DEBUG_STRIP_AVAILABLE) |
| 61 | + return() |
| 62 | + endif() |
| 63 | + |
| 64 | + # Only strip Release builds |
| 65 | + # Debug builds keep symbols embedded for development convenience |
| 66 | + if(CMAKE_BUILD_TYPE STREQUAL "Release") |
| 67 | + add_custom_command(TARGET ${target_name} POST_BUILD |
| 68 | + # Step 1: Extract all debug sections to separate file |
| 69 | + COMMAND ${MINGW_OBJCOPY} |
| 70 | + --only-keep-debug |
| 71 | + $<TARGET_FILE:${target_name}> |
| 72 | + $<TARGET_FILE:${target_name}>.debug |
| 73 | + |
| 74 | + # Step 2: Strip debug sections from executable |
| 75 | + COMMAND ${MINGW_STRIP} |
| 76 | + --strip-debug |
| 77 | + --strip-unneeded |
| 78 | + $<TARGET_FILE:${target_name}> |
| 79 | + |
| 80 | + # Step 3: Add GNU debug link (debuggers use this to find symbols) |
| 81 | + COMMAND ${MINGW_OBJCOPY} |
| 82 | + --add-gnu-debuglink=$<TARGET_FILE:${target_name}>.debug |
| 83 | + $<TARGET_FILE:${target_name}> |
| 84 | + |
| 85 | + COMMENT "Stripping debug symbols from ${target_name} (Release)" |
| 86 | + VERBATIM |
| 87 | + ) |
| 88 | + |
| 89 | + message(STATUS "Debug symbol stripping configured for target: ${target_name}") |
| 90 | + endif() |
| 91 | + endfunction() |
| 92 | +endif() |
0 commit comments