Maelstrom: Make it possible to use a system installation of PhysFS

From 571b3d622b51e1b9aadb5a34dd8738c96ea344a5 Mon Sep 17 00:00:00 2001
From: Pino Toscano <[EMAIL REDACTED]>
Date: Tue, 19 May 2026 09:31:32 +0200
Subject: [PATCH] Make it possible to use a system installation of PhysFS

Add a new USE_VENDORED_PHYSFS CMake variable to determine whether to use
the vendored version of PhysFS (defauling to true to keep the existing
behaviour).

In case an external version is needed, detect it with pkg-config,
as the CMake targets of a system PhysFS 3.2.0 do not seem to be fully
usable. It can be changed later anyway.

Fixes https://github.com/libsdl-org/Maelstrom/issues/68
---
 CMakeLists.txt | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d7a777e4..a1eabd08 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,9 +26,15 @@ if(EMSCRIPTEN)
     set(SDL_EMSCRIPTEN_PERSISTENT_PATH /storage)
 endif()
 
-set(PHYSFS_BUILD_SHARED OFF)
-set(PHYSFS_BUILD_TEST OFF)
-add_subdirectory(external/physfs EXCLUDE_FROM_ALL)
+option(USE_VENDORED_PHYSFS "Use the vendored version of PhysFS" TRUE)
+if (USE_VENDORED_PHYSFS)
+    set(PHYSFS_BUILD_SHARED OFF)
+    set(PHYSFS_BUILD_TEST OFF)
+    add_subdirectory(external/physfs EXCLUDE_FROM_ALL)
+else()
+    find_package(PkgConfig REQUIRED)
+    pkg_check_modules(PhysFS REQUIRED IMPORTED_TARGET physfs)
+endif()
 
 option(USE_VENDORED_SDL "Use the vendored version of SDL" TRUE)
 if (USE_VENDORED_SDL)
@@ -210,7 +216,11 @@ if(STEAM)
 endif()
 
 target_link_libraries(Maelstrom PRIVATE SDLmac)
-target_link_libraries(Maelstrom PRIVATE physfs-static)
+if (USE_VENDORED_PHYSFS)
+    target_link_libraries(Maelstrom PRIVATE physfs-static)
+else()
+    target_link_libraries(Maelstrom PRIVATE PkgConfig::PhysFS)
+endif()
 target_link_libraries(Maelstrom PRIVATE SDL3_net::SDL3_net)
 target_link_libraries(Maelstrom PRIVATE SDL3::SDL3)