Openembedded Devel Discussions
 help / color / mirror / Atom feed
From: Babanpreet Singh <bbnpreetsingh@gmail.com>
To: openembedded-devel@lists.openembedded.org
Cc: Khem Raj <raj.khem@gmail.com>, Ross Burton <ross.burton@arm.com>,
	Babanpreet Singh <bbnpreetsingh@gmail.com>
Subject: [meta-oe][PATCH 1/3] postgresql: make PGXS usable for cross-building extensions
Date: Wed, 11 Nov 2026 20:39:55 +0000	[thread overview]
Message-ID: <20261111203957.7-2-bbnpreetsingh@gmail.com> (raw)
In-Reply-To: <20261111203957.7-1-bbnpreetsingh@gmail.com>

Extensions built with PGXS need pg_config and Makefile.global, but
pg_config is a target binary and the staged Makefile.global has its
paths stripped.

Stage a shell pg_config in crossscripts and fix up the staged PGXS
makefiles so extensions can be built against the sysroot.

Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
---
 .../recipes-dbs/postgresql/files/pg_config    | 43 +++++++++++++
 meta-oe/recipes-dbs/postgresql/postgresql.inc | 62 +++++++++++++++++--
 2 files changed, 99 insertions(+), 6 deletions(-)
 create mode 100755 meta-oe/recipes-dbs/postgresql/files/pg_config

diff --git a/meta-oe/recipes-dbs/postgresql/files/pg_config b/meta-oe/recipes-dbs/postgresql/files/pg_config
new file mode 100755
index 0000000000..1fab031a90
--- /dev/null
+++ b/meta-oe/recipes-dbs/postgresql/files/pg_config
@@ -0,0 +1,43 @@
+#!/bin/sh
+# pg_config for cross builds, the real one is a target binary.
+# Build paths are answered inside the sysroot, install paths as on the target.
+
+sysroot=$(cd "$(dirname "$0")" && pwd -P)
+sysroot=${sysroot%@CROSSSCRIPTS@}
+
+value() {
+	case $1 in
+	--bindir) echo "@BINDIR@" ;;
+	--docdir) echo "@DOCDIR@" ;;
+	--htmldir) echo "@DOCDIR@" ;;
+	--includedir) echo "$sysroot@INCLUDEDIR@" ;;
+	--pkgincludedir) echo "$sysroot@PKGINCLUDEDIR@" ;;
+	--includedir-server) echo "$sysroot@PKGINCLUDEDIR@/server" ;;
+	--libdir) echo "$sysroot@LIBDIR@" ;;
+	--pkglibdir) echo "@PKGLIBDIR@" ;;
+	--localedir) echo "@LOCALEDIR@" ;;
+	--mandir) echo "@MANDIR@" ;;
+	--sharedir) echo "@SHAREDIR@" ;;
+	--sysconfdir) echo "@SYSCONFDIR@" ;;
+	--pgxs) echo "$sysroot@PKGLIBDIR@/pgxs/src/makefiles/pgxs.mk" ;;
+	--version) echo "PostgreSQL @VERSION@" ;;
+	*)
+		echo "pg_config: $1 is not available in a cross build" >&2
+		exit 1
+		;;
+	esac
+}
+
+all="bindir docdir htmldir includedir pkgincludedir includedir-server \
+	libdir pkglibdir localedir mandir sharedir sysconfdir pgxs version"
+
+if [ $# -eq 0 ]; then
+	for opt in $all; do
+		printf '%s = %s\n' "$(echo "$opt" | tr a-z A-Z)" "$(value "--$opt")"
+	done
+	exit 0
+fi
+
+for arg; do
+	value "$arg"
+done
diff --git a/meta-oe/recipes-dbs/postgresql/postgresql.inc b/meta-oe/recipes-dbs/postgresql/postgresql.inc
index 76e90df765..5ac0513085 100644
--- a/meta-oe/recipes-dbs/postgresql/postgresql.inc
+++ b/meta-oe/recipes-dbs/postgresql/postgresql.inc
@@ -30,6 +30,7 @@ SRC_URI = "https://ftp.postgresql.org/pub/source/v${PV}/${BP}.tar.bz2 \
     file://postgresql-setup \
     file://postgresql.service \
     file://run-ptest \
+    file://pg_config \
 "
 
 LEAD_SONAME = "libpq.so"
@@ -284,16 +285,65 @@ do_install:append() {
     install -m 0644 ${UNPACKDIR}/postgresql.service ${D}${systemd_unitdir}/system
     sed -i -e 's,@BINDIR@,${bindir},g' \
         ${D}${systemd_unitdir}/system/postgresql.service
-    # Remove the build path
+    # Make the installed PGXS makefiles usable from extension recipes: take
+    # the toolchain from the environment and drop the build paths. PostgreSQL
+    # 18 skips pg_config when prefix is set, the first expression undoes that.
     if [ -f ${D}${libdir}/${BPN}/pgxs/src/Makefile.global ]; then
-        sed -i -e 's#${RECIPE_SYSROOT}##g' \
-               -e 's#${RECIPE_SYSROOT_NATIVE}##g' \
-               -e 's#${WORKDIR}##g' \
-               -e 's#${TMPDIR}##g' \
-             ${D}${libdir}/${BPN}/pgxs/src/Makefile.global
+        sed -i \
+            -e 's#^ifneq (,$(if $(PGXS),,1)$(prefix))$#ifndef PGXS#' \
+            -e 's#^CC = .*#CC := $(CC)#' \
+            -e 's#^CXX = .*#CXX := $(CXX)#' \
+            -e 's#^CPP = .*#CPP := $(CPP)#' \
+            -e 's#^LD = .*#LD := $(LD)#' \
+            -e 's# -fcanon-prefix-map##g' \
+            -e 's# -f\(file\|debug\|macro\)-prefix-map=[^ ]*##g' \
+            -e 's#^CFLAGS = \(.*\)#CFLAGS := \1 $(CFLAGS)#' \
+            -e 's#^CXXFLAGS = \(.*\)#CXXFLAGS := \1 $(CXXFLAGS)#' \
+            -e 's#^LDFLAGS = \(.*\)#LDFLAGS := \1 $(LDFLAGS)#' \
+            -e '/^CFLAGS :=/s# -I${STAGING_INCDIR}/${PYTHON_DIR}##' \
+            -e 's#${RECIPE_SYSROOT}${includedir}#$(includedir)#g' \
+            -e 's#${RECIPE_SYSROOT}${libdir}#$(libdir)#g' \
+            -e 's#${STAGING_BINDIR_NATIVE}/perl-native/perl#perl#g' \
+            -e 's#${STAGING_BINDIR_NATIVE}/python3-native/python3#python3#g' \
+            -e 's#${STAGING_BINDIR_NATIVE}/##g' \
+            -e 's#${TMPDIR}/hosttools/##g' \
+            -e 's#${RECIPE_SYSROOT_NATIVE}##g' \
+            -e 's#${RECIPE_SYSROOT}##g' \
+            -e 's#${WORKDIR}##g' \
+            -e 's#${TMPDIR}##g' \
+            ${D}${libdir}/${BPN}/pgxs/src/Makefile.global
+        # PGXS installs headers and libraries into directories pg_config
+        # reports inside the sysroot, strip the sysroot there
+        sed -i -e '/^pgxsdir = /a pg_sysroot := $(patsubst %$(pgxsdir)/src/makefiles/pgxs.mk,%,$(PGXS))' \
+            ${D}${libdir}/${BPN}/pgxs/src/Makefile.global
+        sed -i -e 's#$(DESTDIR)$(includedir_server)#$(DESTDIR)$(includedir_server:$(pg_sysroot)%=%)#g' \
+            ${D}${libdir}/${BPN}/pgxs/src/makefiles/pgxs.mk
+        sed -i -e 's#$(DESTDIR)$(libdir)#$(DESTDIR)$(libdir:$(pg_sysroot)%=%)#g' \
+            ${D}${libdir}/${BPN}/pgxs/src/Makefile.shlib
     fi
 }
 
+# pg_config is a target binary, stage a script for extension builds
+SYSROOT_PREPROCESS_FUNCS += "postgresql_sysroot_preprocess"
+postgresql_sysroot_preprocess() {
+    install -d ${SYSROOT_DESTDIR}${bindir_crossscripts}
+    sed -e 's,@CROSSSCRIPTS@,${bindir_crossscripts},g' \
+        -e 's,@BINDIR@,${bindir},g' \
+        -e 's,@DOCDIR@,${docdir}/${BPN},g' \
+        -e 's,@INCLUDEDIR@,${includedir},g' \
+        -e 's,@PKGINCLUDEDIR@,${includedir}/${BPN},g' \
+        -e 's,@LIBDIR@,${libdir},g' \
+        -e 's,@PKGLIBDIR@,${libdir}/${BPN},g' \
+        -e 's,@LOCALEDIR@,${datadir}/locale,g' \
+        -e 's,@MANDIR@,${mandir},g' \
+        -e 's,@SHAREDIR@,${datadir}/${BPN},g' \
+        -e 's,@SYSCONFDIR@,${sysconfdir}/${BPN},g' \
+        -e 's,@VERSION@,${PV},g' \
+        ${UNPACKDIR}/pg_config \
+        > ${SYSROOT_DESTDIR}${bindir_crossscripts}/pg_config
+    chmod 0755 ${SYSROOT_DESTDIR}${bindir_crossscripts}/pg_config
+}
+
 SSTATE_SCAN_FILES += "Makefile.global"
 SSTATE_SCAN_FILES:remove = "*_config"
 
-- 
2.43.0


  reply	other threads:[~2026-11-11 20:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 20:39 [meta-oe][PATCH 0/3] postgresql: add PGXS extension support and pg_cron Babanpreet Singh
2026-11-11 20:39 ` Babanpreet Singh [this message]
2026-11-11 20:39 ` [meta-oe][PATCH 2/3] pgxs.bbclass: add class for PostgreSQL extensions Babanpreet Singh
2026-11-11 20:39 ` [meta-oe][PATCH 3/3] pg-cron: add recipe Babanpreet Singh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261111203957.7-2-bbnpreetsingh@gmail.com \
    --to=bbnpreetsingh@gmail.com \
    --cc=openembedded-devel@lists.openembedded.org \
    --cc=raj.khem@gmail.com \
    --cc=ross.burton@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox