From 2e24b2d24ceba357e11f1a8821075f65c87ab8ad Mon Sep 17 00:00:00 2001
From: ralfulrich <ralf.ulrich@kit.edu>
Date: Thu, 15 Oct 2020 15:26:08 +0200
Subject: [PATCH] added gdb option in cmake macro

---
 CMakeModules/CorsikaUtilities.cmake   | 20 ++++++++++++++------
 Documentation/Examples/CMakeLists.txt |  2 +-
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/CMakeModules/CorsikaUtilities.cmake b/CMakeModules/CorsikaUtilities.cmake
index 6f7ea12b3..96d29eeb6 100644
--- a/CMakeModules/CorsikaUtilities.cmake
+++ b/CMakeModules/CorsikaUtilities.cmake
@@ -174,13 +174,15 @@ endfunction (CORSIKA_ADD_TEST)
 # specify the sources.
 #
 # Example: CORSIKA_ADD_EXAMPLE (testSomething
-#              SOURCES source1.cc source2.cc someheader.h)
+#              SOURCES source1.cc source2.cc someheader.h
+#              RUN_OPTION "extra command line options"
+#              RUN_IN_GDB)
 #
 # In all cases, you can further customize the target with
 # target_link_libraries(testSomething ...) and so on.
 #
 function (CORSIKA_ADD_EXAMPLE)
-  cmake_parse_arguments (PARSE_ARGV 1 C8_ADD_EXAMPLE "" "" "SOURCES;RUN_OPTIONS")
+  cmake_parse_arguments (PARSE_ARGV 1 C8_ADD_EXAMPLE "RUN_IN_GDB" "" "SOURCES;RUN_OPTIONS")
   set (name ${ARGV0})
 
   if (NOT C8_ADD_EXAMPLE_SOURCES)
@@ -204,15 +206,21 @@ function (CORSIKA_ADD_EXAMPLE)
     add_custom_target (run_examples)
   endif ()
   add_dependencies (run_examples ${name})
+  if (C8_ADD_EXAMPLE_RUN_IN_GDB)
+    # convert cmake list into real string:
+    string (REPLACE ";" " " run_options_str "${run_options}")
+    # run the command in gdb and study backtrace a bit
+    set (CMD gdb -q --batch -ex "run ${run_options_str}" -ex bt -ex "info locals" -ex "up" -ex "info locals" -ex "up" -ex "info locals" -ex "up" -ex "info locals" -ex quit "${CMAKE_CURRENT_BINARY_DIR}/${name}") 
+  else (C8_ADD_EXAMPLE_RUN_IN_GDB)
+    # just run the command as-is
+    set (CMD ${CMAKE_CURRENT_BINARY_DIR}/${name} ${run_options})
+  endif (C8_ADD_EXAMPLE_RUN_IN_GDB)
   add_custom_command (TARGET run_examples
     POST_BUILD
     COMMAND ${CMAKE_COMMAND} -E echo ""
     COMMAND ${CMAKE_COMMAND} -E echo "**************************************"
     COMMAND ${CMAKE_COMMAND} -E echo "*****   running example: ${name} " ${run_options} VERBATIM
-    COMMAND gdb -q --batch -ex "run ${run_options}" -ex bt -ex "info locals" -ex "up" -ex "info locals" 
-                                                -ex "up"-ex "info locals" -ex "up" -ex "info locals" 
-                                                -ex quit ${CMAKE_CURRENT_BINARY_DIR}/${name} 
-                                                VERBATIM
+    COMMAND ${CMD} VERBATIM
     WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/example_outputs)
   install (TARGETS ${name} DESTINATION share/examples)
 endfunction (CORSIKA_ADD_EXAMPLE)
diff --git a/Documentation/Examples/CMakeLists.txt b/Documentation/Examples/CMakeLists.txt
index d5e2413e1..62c9d6bd3 100644
--- a/Documentation/Examples/CMakeLists.txt
+++ b/Documentation/Examples/CMakeLists.txt
@@ -84,7 +84,7 @@ if (Pythia8_FOUND)
     CORSIKAprocesssequence
     )
 
-  CORSIKA_ADD_EXAMPLE (vertical_EAS RUN_OPTIONS "4 2 10000.")
+  CORSIKA_ADD_EXAMPLE (vertical_EAS RUN_OPTIONS 4 2 10000.)
   target_link_libraries (vertical_EAS
     CORSIKAsetup
     CORSIKAunits
-- 
GitLab