From e503bac5f88abc7fc3f70830f6109da1b7937196 Mon Sep 17 00:00:00 2001
From: cjserio <chris@x-plane.com>
Date: Sun, 11 Sep 2022 22:20:34 -0400
Subject: [PATCH 1/2] Added the option to statically link against the MSVC
 runtime library by using the -DSTATIC_RUNTIME=ON argument passed to the cmake
 generator command.

---
 CMakeLists.txt                           | 1 +
 client/CMakeLists.txt                    | 9 +++++++++
 compat/CMakeLists.txt                    | 9 +++++++++
 handler/CMakeLists.txt                   | 9 +++++++++
 minidump/CMakeLists.txt                  | 9 +++++++++
 snapshot/CMakeLists.txt                  | 9 +++++++++
 third_party/getopt/CMakeLists.txt        | 9 +++++++++
 third_party/mini_chromium/CMakeLists.txt | 9 +++++++++
 third_party/zlib/CMakeLists.txt          | 9 +++++++++
 tools/CMakeLists.txt                     | 9 +++++++++
 util/CMakeLists.txt                      | 8 ++++++++
 11 files changed, 90 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c94db5e0..2bd6d754 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,6 +7,7 @@ endif()
 
 if (WIN32)
     enable_language(ASM_MASM)
+    option(STATIC_RUNTIME "Enable linking against the static MSVC runtime library" OFF)
 else ()
     enable_language(ASM)
 endif ()
diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index f3ed2896..f3db2a7e 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -45,6 +45,15 @@ if (WIN32)
     )
 endif (WIN32)
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_library(client STATIC ${CRASHPAD_CLIENT_LIBRARY_FILES})
 
 target_compile_features(client PUBLIC cxx_std_14)
diff --git a/compat/CMakeLists.txt b/compat/CMakeLists.txt
index c0bfdf68..a640908c 100644
--- a/compat/CMakeLists.txt
+++ b/compat/CMakeLists.txt
@@ -52,6 +52,15 @@ if (WIN32)
     )
 endif (WIN32)
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_library(compat_incl INTERFACE)
 add_library(compat OBJECT)
 
diff --git a/handler/CMakeLists.txt b/handler/CMakeLists.txt
index 05c1c530..0cd6e02d 100644
--- a/handler/CMakeLists.txt
+++ b/handler/CMakeLists.txt
@@ -44,6 +44,15 @@ if (ANDROID)
     )
 endif (ANDROID)
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_executable(handler main.cc)
 target_sources(handler PRIVATE ${CRASHPAD_HANDLER_LIBRARY_FILES})
 target_compile_features(handler PUBLIC cxx_std_17)
diff --git a/minidump/CMakeLists.txt b/minidump/CMakeLists.txt
index 1c7009ce..ffc34eeb 100644
--- a/minidump/CMakeLists.txt
+++ b/minidump/CMakeLists.txt
@@ -54,6 +54,15 @@ set(CRASHPAD_MINIDUMP_LIBRARY_FILES
     ./minidump_writer_util.h
 )
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_library(minidump OBJECT ${CRASHPAD_MINIDUMP_LIBRARY_FILES})
 target_compile_features(minidump PUBLIC cxx_std_14)
 target_include_directories(minidump PRIVATE ..)
diff --git a/snapshot/CMakeLists.txt b/snapshot/CMakeLists.txt
index cacc5912..bc378f5e 100644
--- a/snapshot/CMakeLists.txt
+++ b/snapshot/CMakeLists.txt
@@ -166,6 +166,15 @@ if (WIN32)
     )
 endif (WIN32)
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_library(snapshot OBJECT ${CRASHPAD_SNAPSHOT_LIBRARY_FILES})
 
 if (ANDROID)
diff --git a/third_party/getopt/CMakeLists.txt b/third_party/getopt/CMakeLists.txt
index 33e68b7e..5aed063a 100755
--- a/third_party/getopt/CMakeLists.txt
+++ b/third_party/getopt/CMakeLists.txt
@@ -3,6 +3,15 @@ set(CRASHPAD_GETOPT_LIBRARY_FILES
     getopt.h
 )
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_library(crashpad_getopt OBJECT)
 target_sources(crashpad_getopt PRIVATE
     ${CRASHPAD_GETOPT_LIBRARY_FILES})
diff --git a/third_party/mini_chromium/CMakeLists.txt b/third_party/mini_chromium/CMakeLists.txt
index f6c49e13..3d942663 100644
--- a/third_party/mini_chromium/CMakeLists.txt
+++ b/third_party/mini_chromium/CMakeLists.txt
@@ -116,6 +116,15 @@ if (WIN32)
     )
 endif (WIN32)
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_library(mini_chromium OBJECT ${CRASHPAD_MINI_CHROMIUM_LIBRARY_FILES})
 target_compile_features(mini_chromium PUBLIC cxx_std_14)
 target_include_directories(mini_chromium PUBLIC mini_chromium)
diff --git a/third_party/zlib/CMakeLists.txt b/third_party/zlib/CMakeLists.txt
index 698b7b7a..3b867815 100644
--- a/third_party/zlib/CMakeLists.txt
+++ b/third_party/zlib/CMakeLists.txt
@@ -37,6 +37,15 @@ if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "(x86_64)|(AMD64)")
     )
 endif ()
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_library(zlib STATIC)
 target_sources(zlib PRIVATE
     ${CRASHPAD_ZLIB_LIBRARY_FILES}
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index d4466cbf..82b78afb 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -3,6 +3,15 @@ set(CRASHPAD_TOOLS_LIBRARY_FILES
     ./tool_support.h
 )
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
+
 add_library(tools OBJECT ${CRASHPAD_TOOLS_LIBRARY_FILES})
 target_compile_features(tools PUBLIC cxx_std_14)
 
diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt
index 0fda8b40..c5e73788 100644
--- a/util/CMakeLists.txt
+++ b/util/CMakeLists.txt
@@ -309,6 +309,14 @@ if (WIN32)
     endif ()
 endif (WIN32)
 
+if (WIN32 AND STATIC_RUNTIME)
+    add_compile_options(
+        $<$<CONFIG:Debug>:/MTd>
+        $<$<CONFIG:MinSizeRel>:/MT>
+        $<$<CONFIG:Release>:/MT>
+        $<$<CONFIG:RelWithDebInfo>:/MT>
+    )
+endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(util OBJECT ${CRASHPAD_UTIL_LIBRARY_FILES})
 target_compile_features(util PUBLIC cxx_std_17)

From 6ee00ab3a9fe6a542d9a28a5c9f6748ed13fa27b Mon Sep 17 00:00:00 2001
From: cjserio <chris@x-plane.com>
Date: Mon, 12 Sep 2022 12:35:39 -0400
Subject: [PATCH 2/2] Turn off default lib and iterator debugging

---
 client/CMakeLists.txt                    | 7 +++++++
 compat/CMakeLists.txt                    | 7 +++++++
 handler/CMakeLists.txt                   | 3 +++
 minidump/CMakeLists.txt                  | 8 ++++++++
 snapshot/CMakeLists.txt                  | 7 +++++++
 third_party/getopt/CMakeLists.txt        | 8 ++++++++
 third_party/mini_chromium/CMakeLists.txt | 8 ++++++++
 third_party/zlib/CMakeLists.txt          | 8 ++++++++
 tools/CMakeLists.txt                     | 8 ++++++++
 util/CMakeLists.txt                      | 8 ++++++++
 10 files changed, 72 insertions(+)

diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index f3db2a7e..985ad38d 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -52,10 +52,17 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(client STATIC ${CRASHPAD_CLIENT_LIBRARY_FILES})
 
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(client PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 target_compile_features(client PUBLIC cxx_std_14)
 
 target_link_libraries(client PUBLIC mini_chromium util compat)
diff --git a/compat/CMakeLists.txt b/compat/CMakeLists.txt
index a640908c..56a15a6d 100644
--- a/compat/CMakeLists.txt
+++ b/compat/CMakeLists.txt
@@ -59,11 +59,18 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(compat_incl INTERFACE)
 add_library(compat OBJECT)
 
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(compat PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 target_sources(compat PRIVATE ${CRASHPAD_COMPAT_LIBRARY_FILES})
 target_link_libraries(compat PUBLIC util compat_incl)
 
diff --git a/handler/CMakeLists.txt b/handler/CMakeLists.txt
index 0cd6e02d..14b1764d 100644
--- a/handler/CMakeLists.txt
+++ b/handler/CMakeLists.txt
@@ -51,6 +51,9 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_executable(handler main.cc)
diff --git a/minidump/CMakeLists.txt b/minidump/CMakeLists.txt
index ffc34eeb..d3440893 100644
--- a/minidump/CMakeLists.txt
+++ b/minidump/CMakeLists.txt
@@ -61,9 +61,17 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(minidump OBJECT ${CRASHPAD_MINIDUMP_LIBRARY_FILES})
+
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(minidump PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 target_compile_features(minidump PUBLIC cxx_std_14)
 target_include_directories(minidump PRIVATE ..)
 target_link_libraries(minidump PUBLIC mini_chromium compat)
diff --git a/snapshot/CMakeLists.txt b/snapshot/CMakeLists.txt
index bc378f5e..8b8ad73d 100644
--- a/snapshot/CMakeLists.txt
+++ b/snapshot/CMakeLists.txt
@@ -173,10 +173,17 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(snapshot OBJECT ${CRASHPAD_SNAPSHOT_LIBRARY_FILES})
 
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(snapshot PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 if (ANDROID)
     target_compile_features(snapshot PUBLIC cxx_std_17)
 else()
diff --git a/third_party/getopt/CMakeLists.txt b/third_party/getopt/CMakeLists.txt
index 5aed063a..bf3e327e 100755
--- a/third_party/getopt/CMakeLists.txt
+++ b/third_party/getopt/CMakeLists.txt
@@ -10,9 +10,17 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(crashpad_getopt OBJECT)
+
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(crashpad_getopt PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 target_sources(crashpad_getopt PRIVATE
     ${CRASHPAD_GETOPT_LIBRARY_FILES})
 target_compile_features(crashpad_getopt PUBLIC cxx_std_14)
diff --git a/third_party/mini_chromium/CMakeLists.txt b/third_party/mini_chromium/CMakeLists.txt
index 3d942663..da6a03e4 100644
--- a/third_party/mini_chromium/CMakeLists.txt
+++ b/third_party/mini_chromium/CMakeLists.txt
@@ -123,9 +123,17 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(mini_chromium OBJECT ${CRASHPAD_MINI_CHROMIUM_LIBRARY_FILES})
+
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(mini_chromium PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 target_compile_features(mini_chromium PUBLIC cxx_std_14)
 target_include_directories(mini_chromium PUBLIC mini_chromium)
 
diff --git a/third_party/zlib/CMakeLists.txt b/third_party/zlib/CMakeLists.txt
index 3b867815..a5cc2955 100644
--- a/third_party/zlib/CMakeLists.txt
+++ b/third_party/zlib/CMakeLists.txt
@@ -44,12 +44,20 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(zlib STATIC)
 target_sources(zlib PRIVATE
     ${CRASHPAD_ZLIB_LIBRARY_FILES}
 )
+
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(zlib PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 target_compile_features(zlib PUBLIC cxx_std_14)
 
 target_compile_definitions(zlib PUBLIC CRASHPAD_ZLIB_SOURCE_EMBEDDED)
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 82b78afb..7304ac48 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -10,9 +10,17 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(tools OBJECT ${CRASHPAD_TOOLS_LIBRARY_FILES})
+
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(tools PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 target_compile_features(tools PUBLIC cxx_std_14)
 
 target_include_directories(tools PUBLIC ..)
diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt
index c5e73788..0eafee43 100644
--- a/util/CMakeLists.txt
+++ b/util/CMakeLists.txt
@@ -316,9 +316,17 @@ if (WIN32 AND STATIC_RUNTIME)
         $<$<CONFIG:Release>:/MT>
         $<$<CONFIG:RelWithDebInfo>:/MT>
     )
+    add_compile_definitions(
+        $<$<CONFIG:Debug>:_ITERATOR_DEBUG_LEVEL=0>
+    )
 endif (WIN32 AND STATIC_RUNTIME)
 
 add_library(util OBJECT ${CRASHPAD_UTIL_LIBRARY_FILES})
+
+if (WIN32 AND STATIC_RUNTIME)
+    set_target_properties(util PROPERTIES STATIC_LIBRARY_OPTIONS "/NODEFAULTLIB;")
+endif (WIN32 AND STATIC_RUNTIME)
+
 target_compile_features(util PUBLIC cxx_std_17)
 
 if (APPLE)