ProcessDebugSymbols.cmake 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. if(${CMAKE_ARGC} LESS 9)
  9. message(FATAL_ERROR "DetachDebugSymbols script called with the wrong number of arguments: ${CMAKE_ARGC}")
  10. endif()
  11. # cmake command arguments (not including the first 2 arguments of '-P', 'DetachDebugSymbols.cmake')
  12. set(GNU_STRIP_TOOL ${CMAKE_ARGV3}) # GNU_STRIP_TOOL : The location of the gnu strip tool (e.g. /usr/bin/strip)
  13. set(GNU_OBJCOPY ${CMAKE_ARGV4}) # GNU_OBJCOPY : The location of the gnu objcopy tool (e.g. /usr/bin/objcopy)
  14. set(STRIP_TARGET ${CMAKE_ARGV5}) # STRIP_TARGET : The built binary to process
  15. set(DEBUG_SYMBOL_EXT ${CMAKE_ARGV6}) # DEBUG_SYMBOL_EXT : When extracting debug information, the extension to use for the debug database file
  16. set(TARGET_TYPE ${CMAKE_ARGV7}) # TARGET_TYPE : The target type (STATIC_LIBRARY, MODULE_LIBRARY, SHARED_LIBRARY, EXECUTABLE, or APPLICATION)
  17. set(DEBUG_SYMBOL_OPTION ${CMAKE_ARGV8}) # DEBUG_SYMBOL_OPTION : Either
  18. # - 'DISCARD' : strip debug information except from debug builds
  19. # - 'DETACH' : detach debug information
  20. # This script only supports executables, applications, modules, static libraries, and shared libraries
  21. if (NOT ${TARGET_TYPE} STREQUAL "STATIC_LIBRARY" AND
  22. NOT ${TARGET_TYPE} STREQUAL "MODULE_LIBRARY" AND
  23. NOT ${TARGET_TYPE} STREQUAL "SHARED_LIBRARY" AND
  24. NOT ${TARGET_TYPE} STREQUAL "EXECUTABLE" AND
  25. NOT ${TARGET_TYPE} STREQUAL "APPLICATION")
  26. return()
  27. endif()
  28. # When specifying the location of the debug info file, just use the filename so that the debugger will search
  29. # for the name through the set of known locations. Using the absolute filename will cause the debugger to look
  30. # only for that absolute path, which is not portable
  31. get_filename_component(file_path ${STRIP_TARGET} DIRECTORY)
  32. get_filename_component(filename_only ${STRIP_TARGET} NAME)
  33. if (${DEBUG_SYMBOL_OPTION} STREQUAL "DISCARD")
  34. message(VERBOSE "Stripping debug symbols from ${STRIP_TARGET}")
  35. elseif (${DEBUG_SYMBOL_OPTION} STREQUAL "DETACH")
  36. if (${TARGET_TYPE} STREQUAL "STATIC_LIBRARY")
  37. # We will not detach the debug information from static libraries since we are unable to attach them back
  38. # to that static library. Instead, static libraries will retain their debug information.
  39. return ()
  40. endif()
  41. message(VERBOSE "Detaching debug symbols from ${STRIP_TARGET} into ${filename_only}.${DEBUG_SYMBOL_EXT}")
  42. endif()
  43. # Step 1: Extract the debug information into the debug datafile (For 'DETACH' actions)
  44. if (${DEBUG_SYMBOL_OPTION} STREQUAL "DETACH")
  45. execute_process(COMMAND
  46. ${GNU_OBJCOPY} --only-keep-debug ${STRIP_TARGET} ${STRIP_TARGET}.${DEBUG_SYMBOL_EXT}
  47. )
  48. endif()
  49. # Step 2: Stripping out the debug symbols (For both 'DISCARD' and 'DETACH')
  50. execute_process(COMMAND
  51. ${GNU_STRIP_TOOL} --strip-debug ${STRIP_TARGET}
  52. )
  53. # Step 3: Re-link the debug information file from stemp 1 (For 'DETACH' actions)
  54. if (${DEBUG_SYMBOL_OPTION} STREQUAL "DETACH")
  55. execute_process(COMMAND ${GNU_OBJCOPY} --remove-section=.gnu_debuglink ${STRIP_TARGET})
  56. execute_process(COMMAND
  57. ${GNU_OBJCOPY} --add-gnu-debuglink=${filename_only}.${DEBUG_SYMBOL_EXT} ${STRIP_TARGET}
  58. WORKING_DIRECTORY
  59. ${file_path}
  60. )
  61. endif()