From 4c90fb308348178acbeb10c173c0cadee0fbfb02 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Tue, 3 Dec 2024 23:22:36 +0800 Subject: [PATCH] Split debug info for all targets Work towards https://github.com/shader-slang/slang/issues/5724 --- CMakePresets.json | 28 ++++++++- cmake/SlangTarget.cmake | 127 +++++++++++++++++++++++++++++++++++----- 2 files changed, 138 insertions(+), 17 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 15bac9f444..b81309af4b 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -119,7 +119,15 @@ "generators": ["ZIP"], "variables": { "CPACK_PACKAGE_FILE_NAME": "slang", - "CPACK_COMPONENTS_ALL": "Unspecified;metadata;debug-info;slang-llvm" + "CPACK_COMPONENTS_ALL": "Unspecified;metadata;slang-llvm" + } + }, + { + "name": "base-debug-info", + "inherits": "base", + "variables": { + "CPACK_PACKAGE_FILE_NAME": "slang-debug-info", + "CPACK_COMPONENTS_ALL": "debug-info" } }, { @@ -140,6 +148,24 @@ "configurations": ["Debug"], "packageDirectory": "dist-debug" }, + { + "name": "release-debug-info", + "inherits": "base-debug-info", + "configurations": ["Release"], + "packageDirectory": "dist-release-debug-info" + }, + { + "name": "releaseWithDebugInfo-debug-info", + "inherits": "base-debug-info", + "configurations": ["RelWithDebInfo"], + "packageDirectory": "dist-releaseWithDebugInfo-debug-info" + }, + { + "name": "debug-debug-info", + "inherits": "base-debug-info", + "configurations": ["Debug"], + "packageDirectory": "dist-debug-debug-info" + }, { "name": "generators", "inherits": "release", diff --git a/cmake/SlangTarget.cmake b/cmake/SlangTarget.cmake index 17b0173618..422d47fdab 100644 --- a/cmake/SlangTarget.cmake +++ b/cmake/SlangTarget.cmake @@ -1,5 +1,5 @@ # -# A function to make target creation a little more declarative +# A function to make target specification a little more declarative # # See the comments on the options below for usage # @@ -21,6 +21,8 @@ function(slang_add_target dir type) # Don't include any source in this target, this is a complement to # EXPLICIT_SOURCE, and doesn't interact with EXTRA_SOURCE_DIRS NO_SOURCE + # Don't generate split debug info for this target + NO_SPLIT_DEBUG_INFO ) set(single_value_args # Set the target name, useful for multiple targets from the same @@ -49,7 +51,7 @@ function(slang_add_target dir type) DEBUG_DIR # Install this target as part of a component INSTALL_COMPONENT - # Don't install debug info by default for this target and use this + # Override the debug info component name for installation # explicit name instead, used for externally built things such as # slang-glslang and slang-llvm which have large pdb files DEBUG_INFO_INSTALL_COMPONENT @@ -196,6 +198,15 @@ function(slang_add_target dir type) PDB_OUTPUT_DIRECTORY "${output_dir}/${runtime_subdir}" ) + if(NOT MSVC) + set_target_properties( + ${target} + PROPERTIES + COMPILE_OPTIONS + "$<$,$>:-fdebug-prefix-map=${CMAKE_CURRENT_BINARY_DIR}=${output_dir}>" + ) + endif() + # # Set common compile options and properties # @@ -207,6 +218,65 @@ function(slang_add_target dir type) set_default_compile_options(${target}) endif() + # Set debug info options if not disabled + # Determine if this target produces a binary that can have debug info + if(type MATCHES "^(EXECUTABLE|SHARED|MODULE)$") + set(can_have_debug_info TRUE) + else() + set(can_have_debug_info FALSE) + endif() + + if(NOT ARG_NO_SPLIT_DEBUG_INFO AND can_have_debug_info) + if(MSVC) + target_compile_options( + ${target} + PRIVATE $<$,$>:/Z7> + ) + set_target_properties( + ${target} + PROPERTIES + COMPILE_PDB_NAME "${target}" + COMPILE_PDB_OUTPUT_DIRECTORY "${output_dir}" + ) + else() + # GCC/Clang debug info handling + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options( + ${target} + PRIVATE + $<$,$>: + -g + -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=. + -fdebug-prefix-map=${CMAKE_BINARY_DIR}=. + > + ) + target_link_options( + ${target} + PRIVATE + $<$,$>: + -Wl,--build-id=sha1 + > + ) + # debug info splitting + add_custom_command( + TARGET ${target} + POST_BUILD + COMMAND + ${CMAKE_OBJCOPY} --only-keep-debug + $ $.debug + COMMAND + ${CMAKE_STRIP} --strip-debug $ + COMMAND + ${CMAKE_OBJCOPY} + --add-gnu-debuglink=$.debug + $ + WORKING_DIRECTORY ${output_dir} + VERBATIM + ) + endif() + endif() + endif() + set_target_properties( ${target} PROPERTIES EXCLUDE_FROM_ALL ${ARG_EXCLUDE_FROM_ALL} @@ -417,31 +487,56 @@ function(slang_add_target dir type) PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ${ARGN} ) endmacro() + if(ARG_INSTALL) i() - set(pdb_component "debug-info") + if(DEFINED ARG_DEBUG_INFO_INSTALL_COMPONENT) + set(debug_component "${ARG_DEBUG_INFO_INSTALL_COMPONENT}") + else() + set(debug_component "debug-info") + endif() endif() if(ARG_INSTALL_COMPONENT) i(EXCLUDE_FROM_ALL COMPONENT ${ARG_INSTALL_COMPONENT}) - set(pdb_component "${ARG_INSTALL_COMPONENT}-debug-info") - endif() - if(ARG_DEBUG_INFO_INSTALL_COMPONENT) - set(pdb_component ${ARG_DEBUG_INFO_INSTALL_COMPONENT}) + if(DEFINED ARG_DEBUG_INFO_INSTALL_COMPONENT) + set(debug_component "${ARG_DEBUG_INFO_INSTALL_COMPONENT}") + else() + set(debug_component "${ARG_INSTALL_COMPONENT}-debug-info") + endif() endif() - if(MSVC AND DEFINED pdb_component) - if( - type STREQUAL "EXECUTABLE" - OR type STREQUAL "SHARED" - OR type STREQUAL "MODULE" - ) + + # Install debug info only if target is being installed + if( + (ARG_INSTALL OR ARG_INSTALL_COMPONENT) + AND NOT ARG_NO_SPLIT_DEBUG_INFO + AND can_have_debug_info + ) + if(MSVC) + # Install PDB files for MSVC install( FILES $ DESTINATION ${runtime_subdir} - # Optional, because if we're building without debug info (like - # a release build) then we don't want to fail here. + CONFIGURATIONS Debug RelWithDebInfo OPTIONAL - COMPONENT ${pdb_component} + COMPONENT ${debug_component} + EXCLUDE_FROM_ALL + ) + else() + # Determine correct destination based on target type + if(type STREQUAL "EXECUTABLE") + set(debug_dest ${runtime_subdir}) + else() + set(debug_dest ${library_subdir}) + endif() + + # Install split debug files for GCC/Clang + install( + FILES "$.debug" + DESTINATION ${debug_dest} + CONFIGURATIONS Debug RelWithDebInfo + COMPONENT ${debug_component} EXCLUDE_FROM_ALL + OPTIONAL ) endif() endif()