From 5b9931456f595b0a2163fabb65dceac99e0e220f Mon Sep 17 00:00:00 2001 From: Anders Leino Date: Wed, 8 Jan 2025 08:30:18 +0200 Subject: Add backtraces to examples (#5973) * examples: Log stack trace on exceptions For now, this is only implemented on Windows. This helps to address #5520. * examples: Print log file if there is any * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Yong He --- examples/CMakeLists.txt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'examples/CMakeLists.txt') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e46f41e7a..759d99994 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,4 +1,6 @@ function(example dir) + cmake_parse_arguments(ARG "WIN32_EXECUTABLE" "" "" ${ARGN}) + set(debug_dir ${CMAKE_CURRENT_BINARY_DIR}/${dir}) file( @@ -30,6 +32,22 @@ function(example dir) ) endif() + # Libraries providing a main function that prints stack traces on exceptions + if(CMAKE_SYSTEM_NAME MATCHES "Windows") + # On Windows we have two different versions: main for "console applications" and + # WinMain for normal Windows applications. + if(${ARG_WIN32_EXECUTABLE}) + set(main_wrapper_libraries example-winmain) + else() + set(main_wrapper_libraries example-main) + endif() + # Add stack printing support + set(main_wrapper_libraries ${main_wrapper_libraries} stacktrace-windows) + set(main_wrapper_libraries ${main_wrapper_libraries} dbghelp.lib) + else() + set(main_wrapper_libraries example-main) + endif() + slang_add_target( ${dir} EXECUTABLE @@ -42,7 +60,9 @@ function(example dir) gfx-util platform $<$:CUDA::cuda_driver> + ${main_wrapper_libraries} EXTRA_COMPILE_DEFINITIONS_PRIVATE + SLANG_EXAMPLE_NAME=${dir} $<$:SLANG_ENABLE_XLIB> REQUIRED_BY all-examples OPTIONAL_REQUIRES ${copy_assets_target} copy-prebuilt-binaries @@ -68,6 +88,9 @@ if(SLANG_ENABLE_EXAMPLES) $<$:CUDA::cuda_driver> FOLDER examples ) + slang_add_target(example-main STATIC FOLDER examples) + slang_add_target(example-winmain STATIC FOLDER examples EXCLUDE_FROM_ALL) + slang_add_target(stacktrace-windows STATIC FOLDER examples EXCLUDE_FROM_ALL) add_custom_target( all-examples -- cgit v1.2.3