From 4bce547b527a9e1dcab43f0bb811fcd348ed491e Mon Sep 17 00:00:00 2001
From: Andrew Udvare <audvare@gmail.com>
Date: Tue, 12 May 2026 03:14:54 -0400
Subject: [PATCH 12/20] Add USE_SYSTEM_PUGIXML opt-in for the bundled pugixml
 library

pugixml is a small XML parser ubiquitously packaged on Linux
distributions (Gentoo: dev-libs/pugixml, exporting pugixml::pugixml).
Vita3K already consumes the namespaced `pugixml::pugixml` target, so
the migration is a straight find_package + IMPORTED_GLOBAL promotion
with no wrapper required.

Signed-off-by: Andrew Udvare <audvare@gmail.com>
---
 CMakeLists.txt          | 10 ++++++++++
 external/CMakeLists.txt |  5 ++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0825918c..3437131c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -348,6 +348,16 @@ if(USE_SYSTEM_TRACY)
 	endif()
 endif()
 
+option(USE_SYSTEM_PUGIXML "Use the system-installed pugixml library instead of the bundled submodule" OFF)
+if(USE_SYSTEM_PUGIXML)
+	find_package(pugixml CONFIG REQUIRED)
+	foreach(_pugi_imp pugixml::pugixml pugixml::shared pugixml::static)
+		if(TARGET ${_pugi_imp})
+			set_target_properties(${_pugi_imp} PROPERTIES IMPORTED_GLOBAL TRUE)
+		endif()
+	endforeach()
+endif()
+
 add_subdirectory(external)
 add_subdirectory(vita3k)
 add_subdirectory(tools/gen-modules)
diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt
index 5b82ad7d..5aaa1150 100644
--- a/external/CMakeLists.txt
+++ b/external/CMakeLists.txt
@@ -20,6 +20,7 @@ function(check_submodules_present)
 		"USE_SYSTEM_CAPSTONE:external/capstone"
 		"USE_SYSTEM_FFMPEG:external/ffmpeg"
 		"USE_SYSTEM_FMT:external/fmt"
+		"USE_SYSTEM_PUGIXML:external/pugixml"
 		"USE_SYSTEM_SDL3:external/sdl"
 		"USE_SYSTEM_STB:external/stb"
 		"USE_SYSTEM_TRACY:external/tracy"
@@ -269,7 +270,9 @@ target_include_directories(dlmalloc PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/dlmalloc
 target_compile_definitions(dlmalloc PUBLIC ONLY_MSPACES=1)
 target_compile_definitions(dlmalloc PUBLIC USE_LOCK=0)
 
-add_subdirectory(pugixml)
+if(NOT USE_SYSTEM_PUGIXML)
+	add_subdirectory(pugixml)
+endif()
 
 add_library(CLI11 INTERFACE)
 target_include_directories(CLI11 INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/cli11")
-- 
2.54.0

