From 09b7794d7155d7c285ec8c0939a309a0221c9b34 Mon Sep 17 00:00:00 2001
From: WerWolv <werwolv98@gmail.com>
Date: Fri, 17 Feb 2023 17:52:10 +0100
Subject: [PATCH] build: Added option to disable stack traces

---
 CMakeLists.txt            |  1 +
 cmake/build_helpers.cmake | 42 ++++++++++++++++++++-------------------
 2 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 94655a8fc698..fc5c0e73d3ed 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,6 +9,7 @@ option(IMHEX_IGNORE_BAD_CLONE "Disable the bad clone prevention checks" OFF)
 option(IMHEX_PATTERNS_PULL_MASTER "Download latest files from master branch of the ImHex-Patterns repo" OFF)
 option(IMHEX_IGNORE_BAD_COMPILER "Allow compiling with an unsupported compiler" OFF)
 option(IMHEX_USE_GTK_FILE_PICKER "Use GTK file picker instead of xdg-desktop-portals" OFF)
+option(IMHEX_DISABLE_STACKTRACE "Disables support for printing stack traces" OFF)
 
 # Basic compiler and cmake configurations
 set(CMAKE_CXX_STANDARD 23)
diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake
index 13a08ecc7514..ec258a8e3439 100644
--- a/cmake/build_helpers.cmake
+++ b/cmake/build_helpers.cmake
@@ -515,25 +515,27 @@ macro(addBundledLibraries)
         set(MAGIC_INCLUDE_DIRS ${MAGIC_INCLUDEDIR})
     endif()
 
-    if (WIN32)
-        message(STATUS "StackWalk enabled!")
-        set(LIBBACKTRACE_LIBRARIES DbgHelp.lib)
-    else ()
-        find_package(Backtrace)
-        if (${Backtrace_FOUND})
-            message(STATUS "Backtrace enabled! Header: ${Backtrace_HEADER}")
-
-            if (Backtrace_HEADER STREQUAL "execinfo.h")
-                set(LIBBACKTRACE_LIBRARIES)
-                set(LIBBACKTRACE_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
-                add_compile_definitions(BACKTRACE_HEADER=\"${Backtrace_HEADER}\")
-                add_compile_definitions(HEX_HAS_EXECINFO)
-            elseif (Backtrace_HEADER STREQUAL "backtrace.h")
-                set(LIBBACKTRACE_LIBRARIES backtrace)
-                set(LIBBACKTRACE_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
-                add_compile_definitions(BACKTRACE_HEADER=\"${Backtrace_HEADER}\")
-                add_compile_definitions(HEX_HAS_BACKTRACE)
-            endif ()
-        endif()
+    if (NOT IMHEX_DISABLE_STACKTRACE)
+        if (WIN32)
+            message(STATUS "StackWalk enabled!")
+            set(LIBBACKTRACE_LIBRARIES DbgHelp.lib)
+        else ()
+            find_package(Backtrace)
+            if (${Backtrace_FOUND})
+                message(STATUS "Backtrace enabled! Header: ${Backtrace_HEADER}")
+
+                if (Backtrace_HEADER STREQUAL "execinfo.h")
+                    set(LIBBACKTRACE_LIBRARIES)
+                    set(LIBBACKTRACE_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
+                    add_compile_definitions(BACKTRACE_HEADER=\"${Backtrace_HEADER}\")
+                    add_compile_definitions(HEX_HAS_EXECINFO)
+                elseif (Backtrace_HEADER STREQUAL "backtrace.h")
+                    set(LIBBACKTRACE_LIBRARIES backtrace)
+                    set(LIBBACKTRACE_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
+                    add_compile_definitions(BACKTRACE_HEADER=\"${Backtrace_HEADER}\")
+                    add_compile_definitions(HEX_HAS_BACKTRACE)
+                endif ()
+            endif()
+        endif ()
     endif ()
 endmacro()
