From fe3702223f0d5c2bd36a3d4d98c423157f422c10 Mon Sep 17 00:00:00 2001
From: Alexandre Bouvier <contact@amb.tf>
Date: Thu, 2 Nov 2023 18:53:40 +0100
Subject: [PATCH] cmake: prefer system simpleini library

---
 CMakeLists.txt                     |  1 +
 CMakeModules/FindSimpleIni.cmake   | 19 +++++++++++++++++++
 externals/CMakeLists.txt           |  4 +++-
 src/frontend_common/CMakeLists.txt |  2 +-
 src/frontend_common/config.h       |  1 +
 5 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 CMakeModules/FindSimpleIni.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e5cac8fe9..ec7975b87 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -290,6 +290,7 @@ find_package(lz4 REQUIRED)
 find_package(nlohmann_json 3.8 REQUIRED)
 find_package(Opus 1.3 MODULE)
 find_package(RenderDoc MODULE)
+find_package(SimpleIni MODULE)
 find_package(stb MODULE)
 find_package(VulkanMemoryAllocator CONFIG)
 find_package(ZLIB 1.2 REQUIRED)
diff --git a/CMakeModules/FindSimpleIni.cmake b/CMakeModules/FindSimpleIni.cmake
new file mode 100644
index 000000000..ce75d7690
--- /dev/null
+++ b/CMakeModules/FindSimpleIni.cmake
@@ -0,0 +1,19 @@
+# SPDX-FileCopyrightText: 2023 Alexandre Bouvier <contact@amb.tf>
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+find_path(SimpleIni_INCLUDE_DIR SimpleIni.h)
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(SimpleIni
+    REQUIRED_VARS SimpleIni_INCLUDE_DIR
+)
+
+if (SimpleIni_FOUND AND NOT TARGET SimpleIni::SimpleIni)
+    add_library(SimpleIni::SimpleIni INTERFACE IMPORTED)
+    set_target_properties(SimpleIni::SimpleIni PROPERTIES
+        INTERFACE_INCLUDE_DIRECTORIES "${SimpleIni_INCLUDE_DIR}"
+    )
+endif()
+
+mark_as_advanced(SimpleIni_INCLUDE_DIR)
diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt
index 515e3f2a4..fc922c31b 100644
--- a/externals/CMakeLists.txt
+++ b/externals/CMakeLists.txt
@@ -292,4 +292,6 @@ if (YUZU_CRASH_DUMPS AND NOT TARGET libbreakpad_client)
 endif()
 
 # SimpleIni
-add_subdirectory(simpleini)
+if (NOT TARGET SimpleIni::SimpleIni)
+    add_subdirectory(simpleini)
+endif()
diff --git a/src/frontend_common/CMakeLists.txt b/src/frontend_common/CMakeLists.txt
index 1537271fc..22e9337c4 100644
--- a/src/frontend_common/CMakeLists.txt
+++ b/src/frontend_common/CMakeLists.txt
@@ -7,4 +7,4 @@ add_library(frontend_common STATIC
 )
 
 create_target_directory_groups(frontend_common)
-target_link_libraries(frontend_common PUBLIC core SimpleIni PRIVATE common Boost::headers)
+target_link_libraries(frontend_common PUBLIC core SimpleIni::SimpleIni PRIVATE common Boost::headers)
diff --git a/src/frontend_common/config.h b/src/frontend_common/config.h
index 20a1a8056..b3812af17 100644
--- a/src/frontend_common/config.h
+++ b/src/frontend_common/config.h
@@ -7,6 +7,7 @@
 #include <string>
 #include "common/settings.h"
 
+#define SI_NO_CONVERSION
 #include <SimpleIni.h>
 #include <boost/algorithm/string/replace.hpp>