From 3671468b851352ebc96509197f3362f1c1d27f20 Mon Sep 17 00:00:00 2001
From: Andrew Udvare <audvare@gmail.com>
Date: Wed, 13 May 2026 01:58:34 -0400
Subject: [PATCH 1/3] Makefile.common: add SYSTEM_ZLIB opt-in to link against
 system zlib

When SYSTEM_ZLIB=1 is passed to make, the libretro core links against
the system zlib via pkg-config rather than compiling the bundled
deps/zlib-1.2.11 copy from libchdr. Defaults to off so existing build
configurations are unaffected.

Distributors (e.g., Gentoo) need this so that security fixes to zlib
propagate to the core binary without rebuilding from a vendored copy.

Signed-off-by: Andrew Udvare <audvare@gmail.com>
---
 Makefile.common | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/Makefile.common b/Makefile.common
index 4f34901a2..9705e51ba 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -16,8 +16,11 @@ INCLUDE_DIRS := $(CORE_DIR) \
 	$(LIBCHDR_DIR)/include \
 	$(LZMA_DIR)/include \
 	$(FLAC_DIR)/include \
-	$(FLAC_DIR)/src/include \
-	$(ZLIB_DIR)
+	$(FLAC_DIR)/src/include
+
+ifneq ($(SYSTEM_ZLIB), 1)
+INCLUDE_DIRS += $(ZLIB_DIR)
+endif
 
 OBJECTS_S :=
 
@@ -78,13 +81,18 @@ endif
 
 ifeq ($(ENABLE_ZLIB), 1)
 	FLAGS += -DENABLE_ZLIB=1
+	SOURCES_C += $(SOURCE_DIR)/junzip.c
+ifeq ($(SYSTEM_ZLIB), 1)
+	FLAGS += $(shell pkg-config --cflags zlib)
+	LIBS  += $(shell pkg-config --libs zlib)
+else
 	SOURCES_C += $(ZLIB_DIR)/adler32.c \
 		$(ZLIB_DIR)/crc32.c \
 		$(ZLIB_DIR)/inffast.c \
 		$(ZLIB_DIR)/inflate.c \
 		$(ZLIB_DIR)/inftrees.c \
-		$(ZLIB_DIR)/zutil.c \
-		$(SOURCE_DIR)/junzip.c
+		$(ZLIB_DIR)/zutil.c
+endif
 endif
 
 ifeq ($(ENABLE_TSUNAMI), 1)
-- 
2.54.0

