From d77aabe5ed49b79b589b78beb5bcf3dc41b2ac6a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anders=20=C3=85strand?= <anders.astrand@percona.com>
Date: Wed, 17 Dec 2025 14:55:50 +0100
Subject: [PATCH] Generate pkg-config for server module development

---
 src/Makefile.global.in |  1 +
 src/Makefile.shlib     |  1 -
 src/backend/.gitignore |  1 +
 src/backend/Makefile   | 37 +++++++++++++++++++++++++------------
 4 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 371cd7eba2c..3c52525b5ba 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -176,6 +176,7 @@ includedir_server = $(pkgincludedir)/server
 includedir_internal = $(pkgincludedir)/internal
 pgxsdir = $(pkglibdir)/pgxs
 bitcodedir = $(pkglibdir)/bitcode
+pkgconfigdir = $(libdir)/pkgconfig
 
 
 ##########################################################################
diff --git a/src/Makefile.shlib b/src/Makefile.shlib
index 3825af5b228..5664c653319 100644
--- a/src/Makefile.shlib
+++ b/src/Makefile.shlib
@@ -80,7 +80,6 @@ shlib_bare	= lib$(NAME)$(DLSUFFIX)
 # Testing the soname variable is a reliable way to determine whether a
 # linkable library is being built.
 soname		= $(shlib_major)
-pkgconfigdir = $(libdir)/pkgconfig
 else
 # Naming convention for dynamically loadable modules
 shlib		= $(NAME)$(DLSUFFIX)
diff --git a/src/backend/.gitignore b/src/backend/.gitignore
index 931df585bc3..4000e599721 100644
--- a/src/backend/.gitignore
+++ b/src/backend/.gitignore
@@ -1,3 +1,4 @@
 /postgres
 /postgres.def
 /postgres.imp
+/postgresql-server.pc
diff --git a/src/backend/Makefile b/src/backend/Makefile
index 7344c8c7f5c..63b8a20dc49 100644
--- a/src/backend/Makefile
+++ b/src/backend/Makefile
@@ -59,7 +59,7 @@ override LDFLAGS := $(LDFLAGS) $(LDFLAGS_EX) $(LDFLAGS_EX_BE)
 
 ##########################################################################
 
-all: submake-libpgport submake-catalog-headers submake-utils-headers postgres $(POSTGRES_IMP)
+all: submake-libpgport submake-catalog-headers submake-utils-headers postgres postgresql-server.pc $(POSTGRES_IMP)
 
 ifneq ($(PORTNAME), cygwin)
 ifneq ($(PORTNAME), win32)
@@ -95,6 +95,22 @@ libpostgres.a: postgres
 
 endif # win32
 
+postgresql-server.pc: $(MAKEFILE_LIST)
+	echo 'prefix=$(prefix)' >$@
+	echo 'exec_prefix=$(patsubst $(prefix),$${prefix},$(exec_prefix))' >>$@
+	echo 'libdir=$(patsubst $(exec_prefix)/%,$${exec_prefix}/%,$(libdir))' >>$@
+	echo 'includedir=$(patsubst $(prefix)/%,$${prefix}/%,$(includedir))' >>$@
+	echo >>$@
+	echo 'Name: postgresql-server' >>$@
+	echo 'Description: PostgreSQL server module' >>$@
+	echo 'URL: $(PACKAGE_URL)' >>$@
+	echo 'Version: $(VERSION)' >>$@
+	echo 'Requires: ' >>$@
+	echo 'Requires.private: ' >>$@
+	echo 'Cflags: -I$${includedir} -I$${includedir}/server -I$${includedir}/internal $(CFLAGS_SL) $(CFLAGS_SL_MODULE)' >>$@
+	echo 'Libs: -L$${libdir} $(LDFLAGS_SL) $(CFLAGS_SL_MODULE)' >>$@
+	echo 'Libs.private: ' >>$@
+
 $(top_builddir)/src/port/libpgport_srv.a: | submake-libpgport
 
 
@@ -171,7 +187,7 @@ generated-parser-sources:
 
 ##########################################################################
 
-install: all installdirs install-bin
+install: all installdirs install-bin install-lib-pc
 ifeq ($(PORTNAME), cygwin)
 ifeq ($(MAKE_DLL), true)
 	$(INSTALL_DATA) libpostgres.a '$(DESTDIR)$(libdir)/libpostgres.a'
@@ -205,18 +221,14 @@ endif
 
 .PHONY: install-bin
 
+install-lib-pc: postgresql-server.pc installdirs
+	$(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/postgresql-server.pc'
+
+.PHONY: install-lib-pc
+
 installdirs:
 	$(MKDIR_P) '$(DESTDIR)$(bindir)' '$(DESTDIR)$(datadir)'
-ifeq ($(PORTNAME), cygwin)
-ifeq ($(MAKE_DLL), true)
-	$(MKDIR_P) '$(DESTDIR)$(libdir)'
-endif
-endif
-ifeq ($(PORTNAME), win32)
-ifeq ($(MAKE_DLL), true)
-	$(MKDIR_P) '$(DESTDIR)$(libdir)'
-endif
-endif
+	$(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)'
 ifeq ($(MAKE_EXPORTS), true)
 	$(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
 	$(MKDIR_P) '$(DESTDIR)$(pgxsdir)/$(MKLDEXPORT_DIR)'
@@ -227,6 +239,7 @@ endif
 
 uninstall:
 	rm -f '$(DESTDIR)$(bindir)/postgres$(X)'
+	rm -f '$(DESTDIR)$(pkgconfigdir)/postgresql-server.pc'
 ifeq ($(MAKE_EXPORTS), true)
 	rm -f '$(DESTDIR)$(pkglibdir)/$(POSTGRES_IMP)'
 	rm -f '$(DESTDIR)$(pgxsdir)/$(MKLDEXPORT_DIR)/mkldexport.sh'
-- 
2.43.0

