# See https://github.com/danmar/cppcheck/commit/974dd5d49f4639d379735b4d94d08e09e49e978f
# See https://github.com/danmar/cppcheck/pull/3806
# Generated from https://patch-diff.githubusercontent.com/raw/danmar/cppcheck/pull/3806.patch
# but ${tinyxml2_LIBRARY} removed
From 9779213481b1211147f6a5a97ddc5d3fd99cc70c Mon Sep 17 00:00:00 2001
From: firewave <firewave@users.noreply.github.com>
Date: Sun, 6 Feb 2022 11:35:07 +0100
Subject: [PATCH 1/3] CI-unixish.yml: added building with system provided
 tinyxml2

---
 .github/workflows/CI-unixish.yml | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/CI-unixish.yml b/.github/workflows/CI-unixish.yml
index ca759e29c7c..f41a7d1aa09 100644
--- a/.github/workflows/CI-unixish.yml
+++ b/.github/workflows/CI-unixish.yml
@@ -27,7 +27,7 @@ jobs:
         run: |
           sudo apt-get update
           sudo apt-get install libxml2-utils
-          sudo apt-get install z3 libz3-dev
+          sudo apt-get install libz3-dev libtinyxml2-dev
           sudo apt-get install qtbase5-dev qttools5-dev libqt5charts5-dev qt5-default
 
       - name: Fix missing z3_version.h
@@ -45,6 +45,20 @@ jobs:
           python -m pip install pip --upgrade
           python -m pip install pytest
 
+      - name: CMake build on ubuntu (with GUI / system tinyxml2)
+        if: contains(matrix.os, 'ubuntu')
+        run: |
+          mkdir cmake.output.tinyxml2
+          cd cmake.output.tinyxml2
+          cmake -G "Unix Makefiles" -DUSE_Z3=On -DHAVE_RULES=On -DBUILD_TESTS=On -DBUILD_GUI=On -DWITH_QCHART=On -DUSE_BUNDLED_TINYXML2=Off ..
+          cmake --build . -- -j$(nproc)
+          cd ..
+
+      - name: Run CMake test (system tinyxml2)
+        if: contains(matrix.os, 'ubuntu')
+        run: |
+          cmake --build cmake.output.tinyxml2 --target check -- -j$(nproc)
+
       - name: CMake build on ubuntu (with GUI)
         if: contains(matrix.os, 'ubuntu')
         run: |

From e76c81b1b9bfe194caa8b432181e4e73cafbe8e8 Mon Sep 17 00:00:00 2001
From: firewave <firewave@users.noreply.github.com>
Date: Sun, 6 Feb 2022 11:35:34 +0100
Subject: [PATCH 2/3] only use bundled tinyxml2 in CMake when configured to do
 so

---
 gui/test/benchmark/simple/CMakeLists.txt |  7 ++++++-
 gui/test/xmlreportv2/CMakeLists.txt      |  3 +++
 oss-fuzz/CMakeLists.txt                  | 17 ++++++++++++++---
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/gui/test/benchmark/simple/CMakeLists.txt b/gui/test/benchmark/simple/CMakeLists.txt
index 78cf812702f..036b4e3cbe0 100644
--- a/gui/test/benchmark/simple/CMakeLists.txt
+++ b/gui/test/benchmark/simple/CMakeLists.txt
@@ -1,11 +1,13 @@
 qt5_wrap_cpp(test-benchmark-simple_SRC benchmarksimple.h)
 add_custom_target(build-testbenchmark-simple-deps SOURCES ${test-benchmark-simple_SRC})
 add_dependencies(gui-build-deps build-testbenchmark-simple-deps)
+if(USE_BUNDLED_TINYXML2)
+    list(APPEND test-benchmark-simple_SRC $<TARGET_OBJECTS:tinyxml2_objs>)
+endif()
 add_executable(benchmark-simple
         ${test-benchmark-simple_SRC}
         benchmarksimple.cpp
         $<TARGET_OBJECTS:lib_objs>
-        $<TARGET_OBJECTS:tinyxml2_objs>
         $<TARGET_OBJECTS:simplecpp_objs>
         )
 target_include_directories(benchmark-simple PRIVATE ${CMAKE_SOURCE_DIR}/lib)
@@ -16,4 +18,7 @@ if (HAVE_RULES)
 endif()
 if (USE_Z3)
     target_link_libraries(benchmark-simple ${Z3_LIBRARIES})
+endif()
+if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
+    target_link_libraries(benchmark-simple tinyxml2)
 endif()
\ No newline at end of file
diff --git a/gui/test/xmlreportv2/CMakeLists.txt b/gui/test/xmlreportv2/CMakeLists.txt
index a244ea72247..b09cc432e94 100644
--- a/gui/test/xmlreportv2/CMakeLists.txt
+++ b/gui/test/xmlreportv2/CMakeLists.txt
@@ -22,4 +22,7 @@ if (HAVE_RULES)
 endif()
 if (USE_Z3)
     target_link_libraries(test-xmlreportv2 ${Z3_LIBRARIES})
+endif()
+if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
+    target_link_libraries(test-xmlreportv2 tinyxml2)
 endif()
\ No newline at end of file
diff --git a/oss-fuzz/CMakeLists.txt b/oss-fuzz/CMakeLists.txt
index c0fbd4d1cab..5b2c5e059cd 100644
--- a/oss-fuzz/CMakeLists.txt
+++ b/oss-fuzz/CMakeLists.txt
@@ -1,11 +1,19 @@
 if (ENABLE_OSS_FUZZ AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
-    add_executable(fuzz-client EXCLUDE_FROM_ALL
+    set(fuzz-client_SRC
             main.cpp
             type2.cpp
+    )
+    if(USE_BUNDLED_TINYXML2)
+        list(APPEND fuzz-client_SRC $<TARGET_OBJECTS:tinyxml2_objs>)
+    endif()
+    add_executable(fuzz-client EXCLUDE_FROM_ALL
+            ${fuzz-client_SRC}
             $<TARGET_OBJECTS:simplecpp_objs>
-            $<TARGET_OBJECTS:tinyxml2_objs>
             $<TARGET_OBJECTS:lib_objs>)
-    target_include_directories(fuzz-client PRIVATE ${CMAKE_SOURCE_DIR}/lib ${CMAKE_SOURCE_DIR}/externals/simplecpp ${CMAKE_SOURCE_DIR}/externals/tinyxml2 ${CMAKE_SOURCE_DIR}/externals)
+    target_include_directories(fuzz-client PRIVATE ${CMAKE_SOURCE_DIR}/lib ${CMAKE_SOURCE_DIR}/externals/simplecpp ${CMAKE_SOURCE_DIR}/externals)
+    if(USE_BUNDLED_TINYXML2)
+        target_include_directories(fuzz-client PRIVATE ${CMAKE_SOURCE_DIR}/externals/tinyxml2/)
+    endif()
     target_compile_options(fuzz-client PRIVATE -fsanitize=fuzzer)
     # TODO: target_link_options() requires CMake >= 3.13
     #target_link_options(fuzz-client PRIVATE -fsanitize=fuzzer)
@@ -16,6 +24,9 @@ if (ENABLE_OSS_FUZZ AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
     if (USE_Z3)
         target_link_libraries(fuzz-client PRIVATE ${Z3_LIBRARIES})
     endif()
+    if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
+        target_link_libraries(fuzz-client PRIVATE tinyxml2)
+    endif()
 
     add_executable(translate EXCLUDE_FROM_ALL
             translate.cpp
