--- platform/osx/detect.py.orig	2023-09-24 16:26:39.000000000 -0400
+++ platform/osx/detect.py	2023-10-09 15:23:23.000000000 -0400
@@ -153,10 +153,107 @@
 
     ## Dependencies
 
-    if env["builtin_libtheora"]:
+    # freetype depends on libpng and zlib, so bundling one of them while keeping others
+    # as shared libraries leads to weird issues
+    if (
+        env["builtin_freetype"] or
+        env["builtin_libpng"] or
+        env["builtin_zlib"]
+    ):
+        env["builtin_freetype"] = True
+        env["builtin_libpng"] = True
+        env["builtin_zlib"] = True
+
+    if not env["builtin_freetype"]:
+        env.ParseConfig("pkg-config freetype2 --cflags --libs")
+
+    if not env["builtin_libpng"]:
+        env.ParseConfig("pkg-config libpng16 --cflags --libs")
+
+    if not env["builtin_zlib"]:
+        env.ParseConfig("pkg-config zlib --cflags --libs")
+
+    if not env["builtin_bullet"]:
+        # We need at least version 2.90
+        min_bullet_version = "2.90"
+
+        import subprocess
+
+        bullet_version = subprocess.check_output(["pkg-config", "bullet", "--modversion"]).strip()
+        if str(bullet_version) < min_bullet_version:
+            # Abort as system bullet was requested but too old
+            print(
+                "Bullet: System version {0} does not match minimal requirements ({1}). Aborting.".format(
+                    bullet_version, min_bullet_version
+                )
+            )
+            sys.exit(255)
+        env.ParseConfig("pkg-config bullet --cflags --libs")
+
+    if False:  # not env['builtin_assimp']:
+        # FIXME: Add min version check
+        env.ParseConfig("pkg-config assimp --cflags --libs")
+
+    if not env["builtin_enet"]:
+        env.ParseConfig("pkg-config libenet --cflags --libs")
+
+    if not env["builtin_squish"]:
+        # No pkgconfig file so far, hardcode expected lib name.
+        env.Append(LIBS=["squish"])
+        #env.ParseConfig("pkg-config libsquish --cflags --libs")
+
+    if not env["builtin_zstd"]:
+        env.ParseConfig("pkg-config libzstd --cflags --libs")
+
+    # Sound and video libraries
+    # Keep the order as it triggers chained dependencies (ogg needed by others, etc.)
+
+    if not env["builtin_libtheora"]:
+        env["builtin_libogg"] = False  # Needed to link against system libtheora
+        env["builtin_libvorbis"] = False  # Needed to link against system libtheora
+        env.ParseConfig("pkg-config theora theoradec --cflags --libs")
+    else:
         if env["arch"] != "arm64":
             env["x86_libtheora_opt_gcc"] = True
 
+
+    if not env["builtin_libvpx"]:
+        env.ParseConfig("pkg-config vpx --cflags --libs")
+
+    if not env["builtin_libvorbis"]:
+        env["builtin_libogg"] = False  # Needed to link against system libvorbis
+        env.ParseConfig("pkg-config vorbis vorbisfile --cflags --libs")
+
+    if not env["builtin_opus"]:
+        env["builtin_libogg"] = False  # Needed to link against system opus
+        env.ParseConfig("pkg-config opus opusfile --cflags --libs")
+
+    if not env["builtin_libogg"]:
+        env.ParseConfig("pkg-config ogg --cflags --libs")
+
+    if not env["builtin_libwebp"]:
+        env.ParseConfig("pkg-config libwebp --cflags --libs")
+
+    if not env["builtin_mbedtls"]:
+        # mbedTLS does not provide a pkgconfig config yet. See https://github.com/ARMmbed/mbedtls/issues/228
+        env.Append(LIBS=["mbedtls", "mbedcrypto", "mbedx509"])
+
+    if not env["builtin_wslay"]:
+        env.ParseConfig("pkg-config libwslay --cflags --libs")
+
+    if not env["builtin_miniupnpc"]:
+        env.ParseConfig("pkg-config miniupnpc --cflags --libs")
+
+    # On Linux wchar_t should be 32-bits
+    # 16-bit library shouldn't be required due to compiler optimisations
+    if not env["builtin_pcre2"]:
+        env.ParseConfig("pkg-config libpcre2-32 --cflags --libs")
+
+    # Embree is only used in tools build on x86_64 and aarch64.
+    if env["tools"] and not env["builtin_embree"]:
+        # No pkgconfig file so far, hardcode expected lib name.
+        env.Append(LIBS=["embree3"])
+
     ## Flags
 
     env.Prepend(CPPPATH=["#platform/osx"])